bsnes/hiro/windows/application.cpp

262 lines
7.8 KiB
C++
Raw Normal View History

#if defined(Hiro_Application)
namespace hiro {
static auto Application_keyboardProc(HWND, UINT, WPARAM, LPARAM) -> bool;
static auto Application_processDialogMessage(MSG&) -> void;
static auto CALLBACK Application_windowProc(HWND, UINT, WPARAM, LPARAM) -> LRESULT;
auto pApplication::run() -> void {
MSG msg;
if(Application::state.onMain) {
while(!Application::state.quit) {
Application::doMain();
processEvents();
}
} else {
MSG msg;
while(GetMessage(&msg, 0, 0, 0)) {
Application_processDialogMessage(msg);
}
}
}
auto pApplication::pendingEvents() -> bool {
MSG msg;
return PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
}
auto pApplication::processEvents() -> void {
while(pendingEvents()) {
MSG msg;
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
Application_processDialogMessage(msg);
}
}
}
auto Application_processDialogMessage(MSG& msg) -> void {
if(msg.message == WM_KEYDOWN || msg.message == WM_KEYUP
|| msg.message == WM_SYSKEYDOWN || msg.message == WM_SYSKEYUP) {
if(Application_keyboardProc(msg.hwnd, msg.message, msg.wParam, msg.lParam)) {
DispatchMessage(&msg);
Update to v094 release. byuu says: This release adds support for game libraries, and substantially improves Game Boy and Game Boy Color emulation with cycle-based renderers. Many other changes are also present. It's very important to note that this release now defaults to optimal drivers rather than safe drivers. This is particularly important if you do not have strong OpenGL 3.2 drivers. If performance is bad, go to Settings -> Configuration -> Advanced, change the video driver, and restart higan. In the rare case that you have trouble opening higan, you can edit settings.bml directly and change the setting there. The Windows safe driver is Direct3D, and the Linux safe driver is XShm. Also note that although display emulation shaders are now supported, they have not been included in this release as they are not ready yet. The support has been built-in anyway, so that they can be tested by everyone. Once refined, future releases of higan will come with built-in shaders for each emulated system that simulates the unique display characteristics of each. Changelog (since v093): - sfc: added SA-1 MDR support (fixes SD Gundam G-Next bug) - sfc: remove random/ and config/, merge to system/ with better randomization - gb: improved color emulation palette contrast - gbc: do not sort sprites by X-priority - gbc: allow transparency on BG priority pixels - gbc: VRAM DMA timing and register fixes - gbc: block invalid VRAM DMA transfer source and target addresses - gba: added LCD color emulation (without it, colors are grossly over-saturated) - gba: removed internal frame blending (use shaders to simulate motion blur if desired) - gba: added Game Boy Player support (adds joypad rumble support to supported games) - gba: SOUND_CTL_H is readable - gb/gbc: PPU renderer is now cycle-based (major accuracy improvement) - gb/gbc: OAM DMA runs in parallel with the CPU - gb/gbc: only HRAM can be accessed during OAM DMA - gb/gbc: fixed serialization of games with SRAM - gb/gbc: disallow up+down or left+right at the same time - gb/gbc: added weak hipass filter to remove DC bias - gb/gbc: STAT OAM+Hblank IRQs only trigger during active display - gb/gbc: fixed underflow in window clamping - gb/gbc/gba: audio mixes internally at 2MHz now instead of 4MHz (does not affect accuracy) - gb/gbc/gba: audio volume reduced for consistency with other systems - fc/sfc/gb/gbc/gba: cheat codes are now stored in universal, decrypted format - ethos: replaced file loader with a proper game library - ethos: added display emulation shader support - ethos: added color emulation option to video settings - ethos: program icon upgraded from 48x48 to 512x512 - ethos: settings and tools windows now use tab frames (less wasted screen space) - ethos: default to optimal (video, audio, input) drivers instead of safest drivers - ethos: input mapping system completely rewritten to support hotplugging and unique device mappings - ruby: added fixes for OpenGL 3.2 on AMD graphics cards - ruby: quark shaders now support user settings inside of manifest - ruby: quark shaders can use integral textures (allows display emulation shaders to work with raw colors) - ruby: add joypad rumble support - ruby: XInput (Xbox 360) controllers now support hotplugging - ruby: added Linux udev joypad driver with hotplug support - phoenix: fixed a rare null pointer dereference issue on Windows - port: target -std=c++11 instead of -std=gnu++11 (do not rely on GNU C++ extensions) - port: added out-of-the-box compilation support for BSD/Clang 3.3+ - port: applied a few Debian upstream patches - cheats: updated to mightymo's 2014-01-02 release; decrypted all Game Genie codes
2014-01-20 08:55:17 +00:00
return;
}
}
if(!IsDialogMessage(GetForegroundWindow(), &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
auto pApplication::quit() -> void {
PostQuitMessage(0);
}
auto pApplication::initialize() -> void {
CoInitialize(0);
InitCommonControls();
WNDCLASS wc;
#if defined(Hiro_Window)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(2));
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Application_windowProc;
wc.lpszClassName = L"hiroWindow";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
#endif
#if defined(Hiro_PopupMenu)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(2));
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Menu_windowProc;
wc.lpszClassName = L"hiroPopupMenu";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
#endif
#if defined(Hiro_Canvas)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Canvas_windowProc;
wc.lpszClassName = L"hiroCanvas";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
#endif
#if defined(Hiro_Label)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Label_windowProc;
wc.lpszClassName = L"hiroLabel";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
#endif
#if defined(Hiro_Viewport)
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = GetModuleHandle(0);
wc.lpfnWndProc = Viewport_windowProc;
wc.lpszClassName = L"hiroViewport";
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
#endif
pKeyboard::initialize();
}
static auto Application_keyboardProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> bool {
if(msg != WM_KEYDOWN && msg != WM_SYSKEYDOWN && msg != WM_KEYUP && msg != WM_SYSKEYUP) return false;
GUITHREADINFO info{sizeof(GUITHREADINFO)};
GetGUIThreadInfo(GetCurrentThreadId(), &info);
auto object = (mObject*)GetWindowLongPtr(info.hwndFocus, GWLP_USERDATA);
if(!object) return false;
if(auto window = dynamic_cast<mWindow*>(object)) {
if(auto self = window->self()) {
if(!self->_modalityDisabled()) {
if(auto code = pKeyboard::_translate(wparam, lparam)) {
if(msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) window->doKeyPress(code);
if(msg == WM_KEYUP || msg == WM_SYSKEYUP) window->doKeyRelease(code);
}
}
}
return false;
}
if(auto window = object->parentWindow(true)) {
if(auto self = window->self()) {
if(self->_modalityDisabled()) return false;
}
}
if(msg == WM_KEYDOWN) {
if(0);
#if defined(Hiro_ListView)
else if(auto listView = dynamic_cast<mListView*>(object)) {
if(wparam == VK_RETURN) {
if(listView->selected()) return true; //returning true generates LVN_ITEMACTIVATE message
}
}
#endif
#if defined(Hiro_LineEdit)
else if(auto lineEdit = dynamic_cast<mLineEdit*>(object)) {
if(wparam == VK_RETURN) {
lineEdit->doActivate();
}
}
#endif
#if defined(Hiro_TextEdit)
else if(auto textEdit = dynamic_cast<mTextEdit*>(object)) {
if(wparam == 'A' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+A = select all text
//note: this is not a standard accelerator on Windows
Edit_SetSel(textEdit->self()->hwnd, 0, ~0);
return true;
} else if(wparam == 'V' && GetKeyState(VK_CONTROL) < 0) {
//Ctrl+V = paste text
//note: this formats Unix (LF) and OS9 (CR) line-endings to Windows (CR+LF) line-endings
//this is necessary as the EDIT control only supports Windows line-endings
OpenClipboard(hwnd);
if(auto handle = GetClipboardData(CF_UNICODETEXT)) {
if(auto text = (wchar_t*)GlobalLock(handle)) {
string data = (const char*)utf8_t(text);
data.replace("\r\n", "\n");
data.replace("\r", "\n");
data.replace("\n", "\r\n");
GlobalUnlock(handle);
utf16_t output(data);
if(auto resource = GlobalAlloc(GMEM_MOVEABLE, (wcslen(output) + 1) * sizeof(wchar_t))) {
if(auto write = (wchar_t*)GlobalLock(resource)) {
wcscpy(write, output);
GlobalUnlock(write);
if(SetClipboardData(CF_UNICODETEXT, resource) == NULL) {
GlobalFree(resource);
}
}
}
}
}
CloseClipboard();
return false;
}
}
#endif
}
return false;
}
/*
case WM_GETMINMAXINFO: {
MINMAXINFO* mmi = (MINMAXINFO*)lparam;
mmi->ptMinTrackSize.x = 256 + window.p.frameMargin().width;
mmi->ptMinTrackSize.y = 256 + window.p.frameMargin().height;
return TRUE;
break;
}
*/
static auto CALLBACK Application_windowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) -> LRESULT {
if(Application::state.quit) return DefWindowProc(hwnd, msg, wparam, lparam);
auto object = (mObject*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
if(!object) return DefWindowProc(hwnd, msg, wparam, lparam);
auto window = dynamic_cast<mWindow*>(object);
if(!window) window = object->parentWindow(true);
if(!window) return DefWindowProc(hwnd, msg, wparam, lparam);
auto pWindow = window->self();
if(!pWindow) return DefWindowProc(hwnd, msg, wparam, lparam);
if(pWindow->_modalityDisabled()) return DefWindowProc(hwnd, msg, wparam, lparam);
switch(msg) {
case WM_CLOSE: pWindow->onClose(); return true;
case WM_MOVE: pWindow->onMove(); break;
case WM_SIZE: pWindow->onSize(); break;
case WM_DROPFILES: pWindow->onDrop(wparam); return false;
case WM_ERASEBKGND: if(pWindow->onEraseBackground()) return true; break;
case WM_ENTERMENULOOP: case WM_ENTERSIZEMOVE: pWindow->onModalBegin(); return false;
case WM_EXITMENULOOP: case WM_EXITSIZEMOVE: pWindow->onModalEnd(); return false;
}
return Shared_windowProc(DefWindowProc, hwnd, msg, wparam, lparam);
}
}
#endif