(Win32) Create g_win32_flags - reduce amount of global booleans kept

around for state
This commit is contained in:
LibretroAdmin 2022-10-26 17:31:15 +02:00
parent b41f6b2885
commit 18639cca6e
6 changed files with 104 additions and 82 deletions

View File

@ -288,20 +288,16 @@ typedef struct win32_common_state
unsigned taskbar_message;
#endif
unsigned monitor_count;
bool quit;
bool resized;
} win32_common_state_t;
/* TODO/FIXME - globals */
bool g_win32_restore_desktop = false;
bool g_win32_inited = false;
unsigned g_win32_resize_width = 0;
unsigned g_win32_resize_height = 0;
float g_win32_refresh_rate = 0;
ui_window_win32_t main_window;
/* TODO/FIXME - static globals */
static bool taskbar_is_created = false;
uint8_t g_win32_flags = 0;
static HMONITOR win32_monitor_last;
static HMONITOR win32_monitor_all[MAX_MONITORS];
@ -314,14 +310,12 @@ static win32_common_state_t win32_st =
#ifdef HAVE_TASKBAR
0, /* taskbar_message */
#endif
false, /* quit */
0, /* monitor_count */
false /* resized */
};
bool win32_taskbar_is_created(void)
uint8_t win32_get_flags(void)
{
return taskbar_is_created;
return g_win32_flags;
}
static INT_PTR_COMPAT CALLBACK pick_core_proc(
@ -889,9 +883,6 @@ static LRESULT CALLBACK wnd_proc_common(
bool *quit, HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
win32_common_state_t
*g_win32 = (win32_common_state_t*)&win32_st;
switch (message)
{
case WM_SYSCOMMAND:
@ -936,7 +927,7 @@ static LRESULT CALLBACK wnd_proc_common(
case WM_CLOSE:
case WM_DESTROY:
case WM_QUIT:
g_win32->quit = true;
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
*quit = true;
/* fall-through */
case WM_MOVE:
@ -952,7 +943,7 @@ static LRESULT CALLBACK wnd_proc_common(
{
g_win32_resize_width = LOWORD(lparam);
g_win32_resize_height = HIWORD(lparam);
g_win32->resized = true;
g_win32_flags |= WIN32_CMN_FLAG_RESIZED;
}
}
*quit = true;
@ -1034,7 +1025,7 @@ static LRESULT CALLBACK wnd_proc_common_internal(HWND hwnd,
case WM_NCLBUTTONDBLCLK:
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
break;
case WM_DROPFILES:
@ -1051,7 +1042,7 @@ static LRESULT CALLBACK wnd_proc_common_internal(HWND hwnd,
return ret;
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
break;
#ifdef HAVE_CLIP_WINDOW
@ -1111,7 +1102,7 @@ static LRESULT CALLBACK wnd_proc_winraw_common_internal(HWND hwnd,
case WM_NCLBUTTONDBLCLK:
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
break;
case WM_DROPFILES:
@ -1128,7 +1119,7 @@ static LRESULT CALLBACK wnd_proc_winraw_common_internal(HWND hwnd,
return ret;
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
break;
case WM_SETFOCUS:
@ -1243,7 +1234,7 @@ static LRESULT CALLBACK wnd_proc_common_dinput_internal(HWND hwnd,
case WM_NCLBUTTONDBLCLK:
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
#if !defined(_XBOX)
{
@ -1268,7 +1259,7 @@ static LRESULT CALLBACK wnd_proc_common_dinput_internal(HWND hwnd,
return ret;
#ifdef HAVE_TASKBAR
if (g_win32->taskbar_message && message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
break;
#ifdef HAVE_CLIP_WINDOW
@ -1298,14 +1289,12 @@ static LRESULT CALLBACK wnd_proc_common_dinput_internal(HWND hwnd,
LRESULT CALLBACK wnd_proc_d3d_common(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
win32_common_state_t *g_win32 = (win32_common_state_t*)&win32_st;
if (message == WM_CREATE)
{
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return 0;
}
@ -1316,14 +1305,12 @@ LRESULT CALLBACK wnd_proc_d3d_common(HWND hwnd, UINT message,
LRESULT CALLBACK wnd_proc_d3d_winraw(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
win32_common_state_t *g_win32 = (win32_common_state_t*)&win32_st;
if (message == WM_CREATE)
{
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return 0;
}
@ -1335,14 +1322,12 @@ LRESULT CALLBACK wnd_proc_d3d_winraw(HWND hwnd, UINT message,
LRESULT CALLBACK wnd_proc_d3d_dinput(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam)
{
win32_common_state_t *g_win32 = (win32_common_state_t*)&win32_st;
if (message == WM_CREATE)
{
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return 0;
}
@ -1361,10 +1346,13 @@ LRESULT CALLBACK wnd_proc_wgl_dinput(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_wgl_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_wgl_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return 0;
}
@ -1380,10 +1368,13 @@ LRESULT CALLBACK wnd_proc_wgl_winraw(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_wgl_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_wgl_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return 0;
}
@ -1398,7 +1389,10 @@ LRESULT CALLBACK wnd_proc_wgl_common(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_wgl_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_wgl_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1418,7 +1412,10 @@ LRESULT CALLBACK wnd_proc_vk_dinput(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_vk_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_vk_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1436,7 +1433,10 @@ LRESULT CALLBACK wnd_proc_vk_winraw(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_vk_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_vk_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1453,7 +1453,10 @@ LRESULT CALLBACK wnd_proc_vk_common(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_vk_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_vk_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1473,7 +1476,10 @@ LRESULT CALLBACK wnd_proc_gdi_dinput(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_gdi_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_gdi_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1506,7 +1512,7 @@ LRESULT CALLBACK wnd_proc_gdi_dinput(HWND hwnd, UINT message,
#ifdef HAVE_TASKBAR
if ( g_win32->taskbar_message
&& message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
}
@ -1522,7 +1528,10 @@ LRESULT CALLBACK wnd_proc_gdi_winraw(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_gdi_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_gdi_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1555,7 +1564,7 @@ LRESULT CALLBACK wnd_proc_gdi_winraw(HWND hwnd, UINT message,
#ifdef HAVE_TASKBAR
if ( g_win32->taskbar_message
&& message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
}
@ -1570,7 +1579,10 @@ LRESULT CALLBACK wnd_proc_gdi_common(HWND hwnd, UINT message,
if (message == WM_CREATE)
{
create_gdi_context(hwnd, &g_win32->quit);
bool is_quit = false;
create_gdi_context(hwnd, &is_quit);
if (is_quit)
g_win32_flags |= WIN32_CMN_FLAG_QUIT;
if (DragAcceptFiles_func)
DragAcceptFiles_func(hwnd, true);
return 0;
@ -1603,7 +1615,7 @@ LRESULT CALLBACK wnd_proc_gdi_common(HWND hwnd, UINT message,
#ifdef HAVE_TASKBAR
if ( g_win32->taskbar_message
&& message == g_win32->taskbar_message)
taskbar_is_created = true;
g_win32_flags |= WIN32_CMN_FLAG_TASKBAR_CREATED;
#endif
}
@ -1755,7 +1767,7 @@ void win32_monitor_init(void)
EnumDisplayMonitors(NULL, NULL,
win32_monitor_enum_proc, 0);
#endif
g_win32->quit = false;
g_win32_flags &= ~WIN32_CMN_FLAG_QUIT;
}
#if !defined(_XBOX)
@ -1776,14 +1788,14 @@ void win32_check_window(void *data,
bool video_is_threaded = video_driver_is_threaded();
if (video_is_threaded)
ui_companion_win32.application->process_events();
*quit = g_win32->quit;
*quit = g_win32_flags & WIN32_CMN_FLAG_QUIT;
if (g_win32->resized)
if (g_win32_flags & WIN32_CMN_FLAG_RESIZED)
{
*resize = true;
*width = g_win32_resize_width;
*height = g_win32_resize_height;
g_win32->resized = false;
g_win32_flags &= ~WIN32_CMN_FLAG_RESIZED;
}
}
#endif
@ -2042,7 +2054,7 @@ HWND win32_get_window(void) { return NULL; }
#else
bool win32_has_focus(void *data)
{
if (g_win32_inited)
if (g_win32_flags & WIN32_CMN_FLAG_INITED)
if (GetForegroundWindow() == main_window.hwnd)
return true;
@ -2289,8 +2301,6 @@ bool win32_set_video_mode(void *data,
int res = 0;
unsigned mon_id = 0;
HMONITOR hm_to_use = NULL;
win32_common_state_t
*g_win32 = (win32_common_state_t*)&win32_st;
settings_t *settings = config_get_ptr();
bool windowed_full = settings->bools.video_windowed_fullscreen;
@ -2318,7 +2328,8 @@ bool win32_set_video_mode(void *data,
/* Wait until context is created (or failed to do so ...).
* Please don't remove the (res = ) as GetMessage can return -1. */
while (!g_win32_inited && !g_win32->quit
while ( !(g_win32_flags & WIN32_CMN_FLAG_INITED)
&& !(g_win32_flags & WIN32_CMN_FLAG_QUIT)
&& (res = GetMessage(&msg, main_window.hwnd, 0, 0)) != 0)
{
if (res == -1)
@ -2331,7 +2342,7 @@ bool win32_set_video_mode(void *data,
DispatchMessage(&msg);
}
if (g_win32->quit)
if (g_win32_flags & WIN32_CMN_FLAG_QUIT)
return false;
return true;
}
@ -2361,11 +2372,8 @@ bool win32_get_client_rect(RECT* rect)
void win32_window_reset(void)
{
win32_common_state_t
*g_win32 = (win32_common_state_t*)&win32_st;
g_win32->quit = false;
g_win32_restore_desktop = false;
g_win32_flags &= ~(WIN32_CMN_FLAG_QUIT
| WIN32_CMN_FLAG_RESTORE_DESKTOP);
}
void win32_destroy_window(void)

View File

@ -65,12 +65,21 @@
RETRO_BEGIN_DECLS
enum win32_common_flags
{
WIN32_CMN_FLAG_QUIT = (1 << 0),
WIN32_CMN_FLAG_RESIZED = (1 << 1),
WIN32_CMN_FLAG_TASKBAR_CREATED = (1 << 2),
WIN32_CMN_FLAG_RESTORE_DESKTOP = (1 << 3),
WIN32_CMN_FLAG_INITED = (1 << 4)
};
extern uint8_t g_win32_flags;
#if !defined(_XBOX)
extern unsigned g_win32_resize_width;
extern unsigned g_win32_resize_height;
extern float g_win32_refresh_rate;
extern bool g_win32_inited;
extern bool g_win32_restore_desktop;
extern ui_window_win32_t main_window;
void win32_monitor_get_info(void);
@ -151,7 +160,7 @@ void win32_window_reset(void);
void win32_destroy_window(void);
bool win32_taskbar_is_created(void);
uint8_t win32_get_flags(void);
float win32_get_refresh_rate(void *data);

View File

@ -171,6 +171,7 @@ static bool win32_display_server_set_window_opacity(
static bool win32_display_server_set_window_progress(
void *data, int progress, bool finished)
{
uint8_t win32_flags = 0;
HWND hwnd = win32_get_window();
dispserv_win32_t *serv = (dispserv_win32_t*)data;
@ -178,7 +179,8 @@ static bool win32_display_server_set_window_progress(
return false;
#ifdef HAS_TASKBAR_EXT
if (!serv->taskbar_list || !win32_taskbar_is_created())
win32_flags = win32_get_flags();
if (!serv->taskbar_list || !(win32_flags & WIN32_CMN_FLAG_TASKBAR_CREATED))
return false;
if (progress == -1)

View File

@ -68,12 +68,12 @@ static void gfx_ctx_gdi_update_title(void)
static void gfx_ctx_gdi_get_video_size(
unsigned *width, unsigned *height)
{
HWND window = win32_get_window();
HWND window = win32_get_window();
if (window)
{
*width = g_win32_resize_width;
*height = g_win32_resize_height;
*width = g_win32_resize_width;
*height = g_win32_resize_height;
}
else
{
@ -84,8 +84,8 @@ static void gfx_ctx_gdi_get_video_size(
win32_monitor_info(&current_mon, &hm_to_use, &mon_id);
mon_rect = current_mon.rcMonitor;
*width = mon_rect.right - mon_rect.left;
*height = mon_rect.bottom - mon_rect.top;
*width = mon_rect.right - mon_rect.left;
*height = mon_rect.bottom - mon_rect.top;
}
}
@ -93,8 +93,9 @@ static bool gfx_ctx_gdi_init(void)
{
WNDCLASSEX wndclass = {0};
settings_t *settings = config_get_ptr();
uint8_t win32_flags = win32_get_flags();
if (g_win32_inited)
if (win32_flags & WIN32_CMN_FLAG_INITED)
return true;
win32_window_reset();
@ -130,13 +131,13 @@ static void gfx_ctx_gdi_destroy(void)
win32_destroy_window();
}
if (g_win32_restore_desktop)
if (g_win32_flags & WIN32_CMN_FLAG_RESTORE_DESKTOP)
{
win32_monitor_get_info();
g_win32_restore_desktop = false;
g_win32_flags &= ~WIN32_CMN_FLAG_RESTORE_DESKTOP;
}
g_win32_inited = false;
g_win32_flags &= ~WIN32_CMN_FLAG_INITED;
}
static bool gfx_ctx_gdi_set_video_mode(
@ -188,7 +189,7 @@ void create_gdi_context(HWND hwnd, bool *quit)
win32_setup_pixel_format(win32_gdi_hdc, false);
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
}
static void gdi_gfx_create(gdi_t *gdi)

View File

@ -78,7 +78,7 @@ void create_vk_context(HWND hwnd, bool *quit)
width, height, win32_vk_interval))
*quit = true;
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
}
static void gfx_ctx_w_vk_swap_interval(void *data, int interval)
@ -210,27 +210,28 @@ static void gfx_ctx_w_vk_destroy(void *data)
win32_destroy_window();
}
if (g_win32_restore_desktop)
if (g_win32_flags & WIN32_CMN_FLAG_RESTORE_DESKTOP)
{
win32_monitor_get_info();
g_win32_restore_desktop = false;
g_win32_flags &= ~WIN32_CMN_FLAG_RESTORE_DESKTOP;
}
if (vk)
free(vk);
g_win32_inited = false;
g_win32_flags &= ~WIN32_CMN_FLAG_INITED;
}
static void *gfx_ctx_w_vk_init(void *video_driver)
{
WNDCLASSEX wndclass = {0};
gfx_ctx_w_vk_data_t *vk = (gfx_ctx_w_vk_data_t*)calloc(1, sizeof(*vk));
uint8_t win32_flags = win32_get_flags();
if (!vk)
return NULL;
if (g_win32_inited)
if (win32_flags & WIN32_CMN_FLAG_INITED)
gfx_ctx_w_vk_destroy(NULL);
win32_window_reset();

View File

@ -208,7 +208,7 @@ static void create_gl_context(HWND hwnd, bool *quit)
if (win32_hrc)
{
if (wglMakeCurrent(win32_hdc, win32_hrc))
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
else
*quit = true;
}
@ -404,7 +404,7 @@ static void create_gles_context(HWND hwnd, bool *quit)
if (!egl_create_surface(&win32_egl, hwnd))
goto error;
g_win32_inited = true;
g_win32_flags |= WIN32_CMN_FLAG_INITED;
return;
error:
@ -582,10 +582,10 @@ static void gfx_ctx_wgl_destroy(void *data)
win32_destroy_window();
}
if (g_win32_restore_desktop)
if (g_win32_flags & WIN32_CMN_FLAG_RESTORE_DESKTOP)
{
win32_monitor_get_info();
g_win32_restore_desktop = false;
g_win32_flags &= ~WIN32_CMN_FLAG_RESTORE_DESKTOP;
}
#ifdef HAVE_DYNAMIC
@ -597,7 +597,7 @@ static void gfx_ctx_wgl_destroy(void *data)
wgl_adaptive_vsync = false;
win32_core_hw_context_enable = false;
g_win32_inited = false;
g_win32_flags &= ~WIN32_CMN_FLAG_INITED;
win32_major = 0;
win32_minor = 0;
p_swap_interval = NULL;
@ -608,11 +608,12 @@ static void *gfx_ctx_wgl_init(void *video_driver)
{
WNDCLASSEX wndclass = {0};
gfx_ctx_wgl_data_t *wgl = (gfx_ctx_wgl_data_t*)calloc(1, sizeof(*wgl));
uint8_t win32_flags = win32_get_flags();
if (!wgl)
return NULL;
if (g_win32_inited)
if (win32_flags & WIN32_CMN_FLAG_INITED)
gfx_ctx_wgl_destroy(NULL);
#ifdef HAVE_DYNAMIC