diff --git a/gfx/common/win32_common.cpp b/gfx/common/win32_common.cpp index 03fc815257..153d0cc0f6 100644 --- a/gfx/common/win32_common.cpp +++ b/gfx/common/win32_common.cpp @@ -50,7 +50,7 @@ static unsigned g_pos_y = CW_USEDEFAULT; static bool g_resized; bool g_inited; bool g_quit; -HWND g_hwnd; +static HWND g_hwnd; extern void *dinput_wgl; extern void *curD3D; @@ -678,10 +678,46 @@ static HANDLE GetFocus(void) } #endif +uintptr_t win32_get_handle(void) +{ + return (uintptr_t)GetFocus(); +} + bool win32_has_focus(void) { if (!g_inited) return false; - return GetFocus() == g_hwnd; + return win32_get_handle() == g_hwnd; +} + +#ifdef _XBOX +/* stub */ +BOOL SetWindowText(HWND hWnd, LPCTSTR lpString) +{ + return TRUE; +} + +BOOL IsIconic(HWND hWnd) +{ + return FALSE; +} +#endif + +void win32_state_set(bool *quit, bool *restore_desktop, bool *inited) +{ + if (*quit) + g_quit = *quit; + if (*restore_desktop) + g_restore_desktop = *restore_desktop; + if (*inited) + g_inited = *inited; +} + +void win32_destroy(void) +{ +#ifndef _XBOX + UnregisterClass("RetroArch", GetModuleHandle(NULL)); +#endif + g_hwnd = NULL; } diff --git a/gfx/common/win32_common.h b/gfx/common/win32_common.h index 2c2533baa1..d6b4b2868f 100644 --- a/gfx/common/win32_common.h +++ b/gfx/common/win32_common.h @@ -36,7 +36,6 @@ extern unsigned g_resize_height; extern bool g_quit; extern bool g_inited; extern bool g_restore_desktop; -extern HWND g_hwnd; LRESULT win32_handle_keyboard_event(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); @@ -80,4 +79,16 @@ bool win32_has_focus(void); void win32_check_window(bool *quit, bool *resize, unsigned *width, unsigned *height); +#ifdef _XBOX +BOOL SetWindowText(HWND hWnd, LPCTSTR lpString); + +BOOL IsIconic(HWND hWnd); +#endif + +void win32_state_set(bool *quit, bool *restore_desktop, bool *inited); + +uintptr_t win32_get_handle(void); + +void win32_destroy(void); + #endif diff --git a/gfx/d3d/d3d.cpp b/gfx/d3d/d3d.cpp index 9712a9e36a..a0f80bc31d 100644 --- a/gfx/d3d/d3d.cpp +++ b/gfx/d3d/d3d.cpp @@ -161,7 +161,7 @@ void d3d_make_d3dpp(void *data, #endif info->rgb32 ? D3DFMT_X8R8G8B8 : D3DFMT_LIN_R5G6B5; #else - d3dpp->hDeviceWindow = g_hwnd; + d3dpp->hDeviceWindow = (HWND)win32_get_handle(); d3dpp->BackBufferFormat = !d3dpp->Windowed ? D3DFMT_X8R8G8B8 : D3DFMT_UNKNOWN; #endif @@ -230,6 +230,7 @@ static bool d3d_init_base(void *data, const video_info_t *info) { D3DPRESENT_PARAMETERS d3dpp; d3d_video_t *d3d = (d3d_video_t*)data; + HWND handle = (HWND)win32_get_handle(); d3d_make_d3dpp(d3d, info, &d3dpp); @@ -247,7 +248,7 @@ static bool d3d_init_base(void *data, const video_info_t *info) if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice( d3d->cur_mon_id, D3DDEVTYPE_HAL, - g_hwnd, + handle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3d->dev))) @@ -258,7 +259,7 @@ static bool d3d_init_base(void *data, const video_info_t *info) if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice( d3d->cur_mon_id, D3DDEVTYPE_HAL, - g_hwnd, + handle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3d->dev))) @@ -647,20 +648,22 @@ static bool d3d_construct(d3d_video_t *d3d, if (!info->fullscreen || windowed_full) { + HWND handle = (HWND)win32_get_handle(); + if (!info->fullscreen && settings->ui.menubar_enable) { RECT rc_temp = {0, 0, (LONG)win_height, 0x7FFF}; - SetMenu(g_hwnd, LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_MENU))); - SendMessage(g_hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rc_temp); + SetMenu(handle, LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_MENU))); + SendMessage(handle, WM_NCCALCSIZE, FALSE, (LPARAM)&rc_temp); g_resize_height = win_height += rc_temp.top + rect.top; - SetWindowPos(g_hwnd, NULL, 0, 0, win_width, win_height, SWP_NOMOVE); + SetWindowPos(handle, NULL, 0, 0, win_width, win_height, SWP_NOMOVE); } - ShowWindow(g_hwnd, SW_RESTORE); - UpdateWindow(g_hwnd); - SetForegroundWindow(g_hwnd); - SetFocus(g_hwnd); + ShowWindow(handle, SW_RESTORE); + UpdateWindow(handle); + SetForegroundWindow(handle); + SetFocus(handle); } #endif @@ -819,7 +822,8 @@ error: static void d3d_free(void *data) { - d3d_video_t *d3d = (d3d_video_t*)data; + d3d_video_t *d3d = (d3d_video_t*)data; + HWND handle = (HWND)win32_get_handle(); if (!d3d) return; @@ -844,15 +848,13 @@ static void d3d_free(void *data) d3d->g_pD3D->Release(); #ifdef HAVE_MONITOR - win32_monitor_from_window(g_hwnd, true); + win32_monitor_from_window(handle, true); #endif if (d3d) delete d3d; -#ifndef _XBOX - UnregisterClass("RetroArch", GetModuleHandle(NULL)); -#endif + win32_destroy(); } #ifndef DONT_HAVE_STATE_TRACKER @@ -1469,6 +1471,7 @@ static bool d3d_frame(void *data, const void *frame, driver_t *driver = driver_get_ptr(); settings_t *settings = config_get_ptr(); const font_renderer_t *font_ctx = driver->font_osd_driver; + HWND handle = (HWND)win32_get_handle(); (void)i; @@ -1480,11 +1483,10 @@ static bool d3d_frame(void *data, const void *frame, rarch_perf_init(&d3d_frame, "d3d_frame"); retro_perf_start(&d3d_frame); -#ifndef _XBOX /* We cannot recover in fullscreen. */ - if (d3d->needs_restore && IsIconic(g_hwnd)) + if (d3d->needs_restore && IsIconic(handle)) return true; -#endif + if (d3d->needs_restore && !d3d_restore(d3d)) { RARCH_ERR("[D3D]: Failed to restore.\n"); diff --git a/gfx/drivers_context/d3d_ctx.cpp b/gfx/drivers_context/d3d_ctx.cpp index bb0bb44c3e..49cf7f2493 100644 --- a/gfx/drivers_context/d3d_ctx.cpp +++ b/gfx/drivers_context/d3d_ctx.cpp @@ -84,16 +84,13 @@ static void gfx_ctx_d3d_update_title(void *data) char buf[128] = {0}; char buffer_fps[128] = {0}; settings_t *settings = config_get_ptr(); + HWND handle = (HWND)win32_get_handle(); + + (void)handle; if (video_monitor_get_fps(buf, sizeof(buf), buffer_fps, sizeof(buffer_fps))) - { -#ifndef _XBOX - d3d_video_t *d3d = (d3d_video_t*)data; - - SetWindowText(g_hwnd, buf); -#endif - } + SetWindowText(handle, buf); if (settings->fps_show) { diff --git a/gfx/drivers_context/wgl_ctx.cpp b/gfx/drivers_context/wgl_ctx.cpp index 0e397fd5c3..3df17a715c 100644 --- a/gfx/drivers_context/wgl_ctx.cpp +++ b/gfx/drivers_context/wgl_ctx.cpp @@ -103,6 +103,7 @@ static void setup_pixel_format(HDC hdc) void create_gl_context(HWND hwnd) { bool core_context; + bool about_to_quit = true; const struct retro_hw_render_callback *hw_render = (const struct retro_hw_render_callback*)video_driver_callback(); driver_t *driver = driver_get_ptr(); @@ -138,24 +139,27 @@ void create_gl_context(HWND hwnd) if (!wglShareLists(g_hrc, g_hw_hrc)) { RARCH_LOG("[WGL]: Failed to share contexts.\n"); - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); } } else - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); } } if (g_hrc) { if (wglMakeCurrent(g_hdc, g_hrc)) - g_inited = true; + { + bool inited = true; + win32_state_set(NULL, NULL, &inited); + } else - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); } else { - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); return; } @@ -204,7 +208,7 @@ void create_gl_context(HWND hwnd) wglDeleteContext(g_hrc); g_hrc = context; if (!wglMakeCurrent(g_hdc, g_hrc)) - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); } else RARCH_ERR("[WGL]: Failed to create core context. Falling back to legacy context.\n"); @@ -215,7 +219,7 @@ void create_gl_context(HWND hwnd) if (!g_hw_hrc) { RARCH_ERR("[WGL]: Failed to create shared context.\n"); - g_quit = true; + win32_state_set(&about_to_quit, NULL, NULL); } } } @@ -266,12 +270,13 @@ static void gfx_ctx_wgl_update_window_title(void *data) char buf[128] = {0}; char buf_fps[128] = {0}; settings_t *settings = config_get_ptr(); + HWND handle = (HWND)win32_get_handle(); (void)data; if (video_monitor_get_fps(buf, sizeof(buf), buf_fps, sizeof(buf_fps))) - SetWindowText(g_hwnd, buf); + SetWindowText(handle, buf); if (settings->fps_show) rarch_main_msg_queue_push(buf_fps, 1, 1, false); } @@ -279,8 +284,9 @@ static void gfx_ctx_wgl_update_window_title(void *data) static void gfx_ctx_wgl_get_video_size(void *data, unsigned *width, unsigned *height) { (void)data; + HWND handle = (HWND)win32_get_handle(); - if (!g_hwnd) + if (!handle) { unsigned mon_id; RECT mon_rect; @@ -301,6 +307,8 @@ static void gfx_ctx_wgl_get_video_size(void *data, unsigned *width, unsigned *he static bool gfx_ctx_wgl_init(void *data) { + bool about_to_quit = false; + bool restore_desktop = false; WNDCLASSEX wndclass = {0}; (void)data; @@ -308,8 +316,7 @@ static bool gfx_ctx_wgl_init(void *data) if (g_inited) return false; - g_quit = false; - g_restore_desktop = false; + win32_state_set(&about_to_quit, &restore_desktop, NULL); win32_monitor_init(); if (!win32_window_init(&wndclass, true)) @@ -341,7 +348,9 @@ error: static void gfx_ctx_wgl_destroy(void *data) { + bool not_inited = false; driver_t *driver = driver_get_ptr(); + HWND handle = (HWND)win32_get_handle(); (void)data; @@ -360,26 +369,26 @@ static void gfx_ctx_wgl_destroy(void *data) } } - if (g_hwnd && g_hdc) + if (handle && g_hdc) { - ReleaseDC(g_hwnd, g_hdc); + ReleaseDC(handle, g_hdc); g_hdc = NULL; } - if (g_hwnd) + if (handle) { - win32_monitor_from_window(g_hwnd, true); - UnregisterClass("RetroArch", GetModuleHandle(NULL)); - g_hwnd = NULL; + win32_monitor_from_window(handle, true); + win32_destroy(); } if (g_restore_desktop) { + bool restore_desktop = false; win32_monitor_get_info(); - g_restore_desktop = false; + win32_state_set(NULL, &restore_desktop, NULL); } - g_inited = false; + win32_state_set(NULL, NULL, ¬_inited); g_major = g_minor = 0; p_swap_interval = NULL; }