Skip to content

Commit 76238c8

Browse files
committed
(win32_common) Several bugfixes and perforamnce improvements, including:
- new_file[32768] — downsized to PATH_MAX_LENGTH - win32_clip_window — WINDOWINFO was heap-allocated with malloc for no reason. Changed to stack variable with correct sizeof(WINDOWINFO) - DragQueryFileW - Correct size argument now - win32_meta_key_to_name — returns NULL for unmapped keys, but strlen was called on its return value unconditionally. Added a NULL guard - win32_get_refresh_rate — removed GetVersionEx (deprecated since Vista, absent in some SDKs) since the whole block is already #if-gated on Win7+. Also fixed a missing Denominator != 0 guard before the division - win32_get_metrics — previously opened and released a HDC once per case branch (5 separate GetDC/ReleaseDC round-trips). Now opens one HDC, queries what's needed, and releases it once - win32_get_refresh_rate — the two GetModuleHandle("user32.dll") calls (one per function pointer) each hit a hash-table lookup. Combined them so user32 is retrieved once only when either pointer is missing - win32_load_content_from_gui — the if/else block around DialogBoxParam was identical in both branches (the only difference was showing the mouse cursor). Deduplicated into a single dialog call with conditional cursor show/hide around it - pick_core_proc — removed shadowed const core_info_t *info variables and redundant &array[i]-to-pointer casts - win32_resize_after_display_change — abs() on monitor RECT deltas is unnecessary (right ≥ left always); removed the call
1 parent fc9c12c commit 76238c8

1 file changed

Lines changed: 97 additions & 121 deletions

File tree

gfx/common/win32_common.c

Lines changed: 97 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,14 @@ typedef struct win32_common_state
294294
unsigned monitor_count;
295295
} win32_common_state_t;
296296

297-
/* TODO/FIXME - globals */
297+
/* Module-level state: resize dimensions, refresh rate, and main window handle.
298+
* These are written from the window message loop and read by the video driver. */
298299
unsigned g_win32_resize_width = 0;
299300
unsigned g_win32_resize_height = 0;
300-
float g_win32_refresh_rate = 0;
301+
float g_win32_refresh_rate = 0.0f;
301302
ui_window_win32_t main_window;
302303

303-
/* TODO/FIXME - static globals */
304+
/* Module-level flags byte (WIN32_CMN_FLAG_*). */
304305
uint8_t g_win32_flags = 0;
305306
static HMONITOR win32_monitor_last;
306307
static HMONITOR win32_monitor_all[MAX_MONITORS];
@@ -334,28 +335,23 @@ static INT_PTR_COMPAT CALLBACK pick_core_proc(
334335
{
335336
case WM_INITDIALOG:
336337
{
337-
const core_info_t *info = NULL;
338338
HWND hwndList;
339339
unsigned i;
340340

341-
/* Add items to list. */
341+
/* Add items to list. */
342342
core_info_get_list(&core_info_list);
343343
core_info_list_get_supported_cores(core_info_list,
344344
path_get(RARCH_PATH_CONTENT), &core_info, &list_size);
345345

346346
hwndList = GetDlgItem(hDlg, ID_CORELISTBOX);
347347

348348
for (i = 0; i < list_size; i++)
349-
{
350-
const core_info_t *info = (const core_info_t*)&core_info[i];
351349
SendMessage(hwndList, LB_ADDSTRING, 0,
352-
(LPARAM)info->display_name);
353-
}
350+
(LPARAM)core_info[i].display_name);
354351

355352
/* Select the first item in the list */
356353
SendMessage(hwndList, LB_SETCURSEL, 0, 0);
357-
info = (const core_info_t*)&core_info[0];
358-
path_set(RARCH_PATH_CORE, info->path);
354+
path_set(RARCH_PATH_CORE, core_info[0].path);
359355

360356
SetFocus(hwndList);
361357
return TRUE;
@@ -373,17 +369,14 @@ static INT_PTR_COMPAT CALLBACK pick_core_proc(
373369
{
374370
case LBN_SELCHANGE:
375371
{
376-
const core_info_t *info = NULL;
377-
HWND hwndList = GetDlgItem(
378-
hDlg, ID_CORELISTBOX);
379-
int lbItem = (int)
372+
HWND hwndList = GetDlgItem(hDlg, ID_CORELISTBOX);
373+
int lbItem = (int)
380374
SendMessage(hwndList, LB_GETCURSEL, 0, 0);
381375

382376
core_info_get_list(&core_info_list);
383377
core_info_list_get_supported_cores(core_info_list,
384378
path_get(RARCH_PATH_CONTENT), &core_info, &list_size);
385-
info = (const core_info_t*)&core_info[lbItem];
386-
path_set(RARCH_PATH_CORE, info->path);
379+
path_set(RARCH_PATH_CORE, core_info[lbItem].path);
387380
}
388381
break;
389382
}
@@ -572,38 +565,25 @@ static bool win32_load_content_from_gui(const char *szFilename)
572565
settings_t *settings = config_get_ptr();
573566
bool video_is_fs = settings->bools.video_fullscreen;
574567
video_driver_state_t *video_st = video_state_get_ptr();
568+
bool needs_cursor = video_is_fs
569+
&& video_st->poke
570+
&& video_st->poke->show_mouse;
575571

576-
if ( video_is_fs
577-
&& video_st->poke
578-
&& video_st->poke->show_mouse)
579-
{
580-
/* Show mouse cursor for dialog */
572+
if (needs_cursor)
581573
video_st->poke->show_mouse(video_st->data, true);
582574

583-
/* Pick one core that could be compatible, ew */
584-
if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_PICKCORE),
585-
main_window.hwnd, pick_core_proc, (LPARAM)NULL) == IDOK)
586-
{
587-
task_push_load_content_with_current_core_from_companion_ui(
588-
NULL, &content_info, CORE_TYPE_PLAIN, NULL, NULL);
589-
okay = true;
590-
}
591-
592-
/* Hide mouse cursor after dialog */
593-
video_st->poke->show_mouse(video_st->data, false);
594-
}
595-
else
575+
/* Pick one core that could be compatible. */
576+
if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_PICKCORE),
577+
main_window.hwnd, pick_core_proc, (LPARAM)NULL) == IDOK)
596578
{
597-
/* Pick one core that could be compatible, ew */
598-
if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_PICKCORE),
599-
main_window.hwnd, pick_core_proc, (LPARAM)NULL) == IDOK)
600-
{
601-
task_push_load_content_with_current_core_from_companion_ui(
602-
NULL, &content_info, CORE_TYPE_PLAIN, NULL, NULL);
603-
okay = true;
604-
}
579+
task_push_load_content_with_current_core_from_companion_ui(
580+
NULL, &content_info, CORE_TYPE_PLAIN, NULL, NULL);
581+
okay = true;
605582
}
606583

584+
if (needs_cursor)
585+
video_st->poke->show_mouse(video_st->data, false);
586+
607587
return okay;
608588
}
609589
}
@@ -633,7 +613,8 @@ static bool win32_drag_query_file(HWND hwnd, WPARAM wparam)
633613
char *szFilename = NULL;
634614
wszFilename[0] = L'\0';
635615

636-
DragQueryFileW((HDROP)wparam, 0, wszFilename, sizeof(wszFilename));
616+
DragQueryFileW((HDROP)wparam, 0, wszFilename,
617+
sizeof(wszFilename) / sizeof(wszFilename[0]));
637618
szFilename = utf16_to_utf8_string_alloc(wszFilename);
638619
ret = win32_load_content_from_gui(szFilename);
639620
if (szFilename)
@@ -652,8 +633,8 @@ static void win32_resize_after_display_change(HWND hwnd, HMONITOR monitor)
652633
info.cbSize = sizeof(info);
653634
if (GetMonitorInfo(monitor, &info))
654635
SetWindowPos(hwnd, 0, 0, 0,
655-
abs(info.rcMonitor.right - info.rcMonitor.left),
656-
abs(info.rcMonitor.bottom - info.rcMonitor.top),
636+
info.rcMonitor.right - info.rcMonitor.left,
637+
info.rcMonitor.bottom - info.rcMonitor.top,
657638
SWP_NOMOVE);
658639
}
659640

@@ -674,11 +655,9 @@ static bool win32_browser(
674655
ui_browser_window_state_t browser_state;
675656

676657
/* These need to be big enough to hold the
677-
* path/name of any file the user may select.
678-
* FIXME: We should really handle the
679-
* error case when this isn't big enough. */
658+
* path/name of any file the user may select. */
680659
char new_title[PATH_MAX];
681-
char new_file[32768]; /* TODO/FIXME - is this not way too big? */
660+
char new_file[PATH_MAX_LENGTH]; /* MAX_PATH-length path buffer */
682661

683662
new_title[0] = '\0';
684663
new_file[0] = '\0';
@@ -699,9 +678,8 @@ static bool win32_browser(
699678

700679
result = browser->open(&browser_state);
701680

702-
/* TODO/FIXME - this is weird - why is this called
703-
* after the browser->open call? Seems to have no effect
704-
* anymore here */
681+
/* browser->open() may update browser_state.path in-place;
682+
* copy the final path back to the caller's buffer. */
705683
if (filename && browser_state.path)
706684
strlcpy(filename, browser_state.path, filename_size);
707685

@@ -1897,52 +1875,54 @@ bool win32_window_create(void *data, unsigned style,
18971875
bool win32_get_metrics(void *data,
18981876
enum display_metric_types type, float *value)
18991877
{
1878+
HDC monitor;
1879+
bool ret = true;
1880+
1881+
if (type == DISPLAY_METRIC_NONE)
1882+
{
1883+
*value = 0;
1884+
return false;
1885+
}
1886+
1887+
monitor = GetDC(NULL);
1888+
if (!monitor)
1889+
{
1890+
*value = 0;
1891+
return false;
1892+
}
1893+
19001894
switch (type)
19011895
{
19021896
case DISPLAY_METRIC_PIXEL_WIDTH:
1903-
{
1904-
HDC monitor = GetDC(NULL);
1905-
*value = GetDeviceCaps(monitor, HORZRES);
1906-
ReleaseDC(NULL, monitor);
1907-
}
1908-
return true;
1897+
*value = (float)GetDeviceCaps(monitor, HORZRES);
1898+
break;
19091899
case DISPLAY_METRIC_PIXEL_HEIGHT:
1910-
{
1911-
HDC monitor = GetDC(NULL);
1912-
*value = GetDeviceCaps(monitor, VERTRES);
1913-
ReleaseDC(NULL, monitor);
1914-
}
1915-
return true;
1900+
*value = (float)GetDeviceCaps(monitor, VERTRES);
1901+
break;
19161902
case DISPLAY_METRIC_MM_WIDTH:
1917-
{
1918-
HDC monitor = GetDC(NULL);
1919-
*value = GetDeviceCaps(monitor, HORZSIZE);
1920-
ReleaseDC(NULL, monitor);
1921-
}
1922-
return true;
1903+
*value = (float)GetDeviceCaps(monitor, HORZSIZE);
1904+
break;
19231905
case DISPLAY_METRIC_MM_HEIGHT:
1924-
{
1925-
HDC monitor = GetDC(NULL);
1926-
*value = GetDeviceCaps(monitor, VERTSIZE);
1927-
ReleaseDC(NULL, monitor);
1928-
}
1929-
return true;
1906+
*value = (float)GetDeviceCaps(monitor, VERTSIZE);
1907+
break;
19301908
case DISPLAY_METRIC_DPI:
19311909
/* 25.4 mm in an inch. */
19321910
{
1933-
HDC monitor = GetDC(NULL);
19341911
int pixels_x = GetDeviceCaps(monitor, HORZRES);
19351912
int physical_width = GetDeviceCaps(monitor, HORZSIZE);
1936-
*value = 254 * pixels_x / physical_width / 10;
1937-
ReleaseDC(NULL, monitor);
1913+
*value = (physical_width > 0)
1914+
? (float)(254 * pixels_x) / (float)(physical_width * 10)
1915+
: 0.0f;
19381916
}
1939-
return true;
1940-
case DISPLAY_METRIC_NONE:
1917+
break;
19411918
default:
19421919
*value = 0;
1920+
ret = false;
19431921
break;
19441922
}
1945-
return false;
1923+
1924+
ReleaseDC(NULL, monitor);
1925+
return ret;
19461926
}
19471927
#endif
19481928

@@ -1993,19 +1973,11 @@ void win32_clip_window(bool state)
19931973

19941974
if (state && main_window.hwnd)
19951975
{
1996-
PWINDOWINFO info;
1997-
info = (PWINDOWINFO)malloc(sizeof(*info));
1976+
WINDOWINFO info;
1977+
info.cbSize = sizeof(WINDOWINFO);
19981978

1999-
if (info)
2000-
{
2001-
info->cbSize = sizeof(PWINDOWINFO);
2002-
2003-
if (GetWindowInfo(main_window.hwnd, info))
2004-
clip_rect = info->rcClient;
2005-
2006-
free(info);
2007-
}
2008-
info = NULL;
1979+
if (GetWindowInfo(main_window.hwnd, &info))
1980+
clip_rect = info.rcClient;
20091981

20101982
ClipCursor(&clip_rect);
20111983
}
@@ -2192,7 +2164,7 @@ static void win32_localize_menu(HMENU menu)
21922164
else if (meta_key != 0)
21932165
{
21942166
meta_key_name = win32_meta_key_to_name(meta_key);
2195-
__len = strlen(meta_key_name);
2167+
__len = meta_key_name ? strlen(meta_key_name) : 0;
21962168
}
21972169

21982170
/* Append localized name, tab character, and Shortcut Key */
@@ -2666,33 +2638,33 @@ float win32_get_refresh_rate(void *data)
26662638
{
26672639
float refresh_rate = 0.0f;
26682640
#if _WIN32_WINNT >= 0x0601 || _WIN32_WINDOWS >= 0x0601 /* Win 7 */
2669-
OSVERSIONINFO version_info;
26702641
UINT32 TopologyID;
26712642
unsigned int NumPathArrayElements = 0;
26722643
unsigned int NumModeInfoArrayElements = 0;
26732644
DISPLAYCONFIG_PATH_INFO_CUSTOM *PathInfoArray = NULL;
26742645
DISPLAYCONFIG_MODE_INFO_CUSTOM *ModeInfoArray = NULL;
26752646
#ifdef HAVE_DYLIB
2676-
static QUERYDISPLAYCONFIG pQueryDisplayConfig;
2677-
static GETDISPLAYCONFIGBUFFERSIZES pGetDisplayConfigBufferSizes;
2678-
if (!pQueryDisplayConfig)
2679-
pQueryDisplayConfig = (QUERYDISPLAYCONFIG)GetProcAddress(GetModuleHandle("user32.dll"), "QueryDisplayConfig");
2680-
2681-
if (!pGetDisplayConfigBufferSizes)
2682-
pGetDisplayConfigBufferSizes = (GETDISPLAYCONFIGBUFFERSIZES)GetProcAddress(GetModuleHandle("user32.dll"), "GetDisplayConfigBufferSizes");
2647+
static QUERYDISPLAYCONFIG pQueryDisplayConfig;
2648+
static GETDISPLAYCONFIGBUFFERSIZES pGetDisplayConfigBufferSizes;
2649+
if (!pQueryDisplayConfig || !pGetDisplayConfigBufferSizes)
2650+
{
2651+
HMODULE user32 = GetModuleHandle("user32.dll");
2652+
if (!pQueryDisplayConfig)
2653+
pQueryDisplayConfig = (QUERYDISPLAYCONFIG)GetProcAddress(
2654+
user32, "QueryDisplayConfig");
2655+
if (!pGetDisplayConfigBufferSizes)
2656+
pGetDisplayConfigBufferSizes = (GETDISPLAYCONFIGBUFFERSIZES)GetProcAddress(
2657+
user32, "GetDisplayConfigBufferSizes");
2658+
}
26832659
#else
2684-
static QUERYDISPLAYCONFIG pQueryDisplayConfig = QueryDisplayConfig;
2685-
static GETDISPLAYCONFIGBUFFERSIZES pGetDisplayConfigBufferSizes = GetDisplayConfigBufferSizes;
2660+
static QUERYDISPLAYCONFIG pQueryDisplayConfig = QueryDisplayConfig;
2661+
static GETDISPLAYCONFIGBUFFERSIZES pGetDisplayConfigBufferSizes = GetDisplayConfigBufferSizes;
26862662
#endif
26872663

2688-
version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
2689-
if (!GetVersionEx(&version_info))
2664+
/* Both function pointers must be valid before proceeding. */
2665+
if (!pQueryDisplayConfig || !pGetDisplayConfigBufferSizes)
26902666
return refresh_rate;
26912667

2692-
if (version_info.dwMajorVersion < 6 ||
2693-
(version_info.dwMajorVersion == 6 && version_info.dwMinorVersion < 1))
2694-
return refresh_rate;
2695-
26962668
if (pGetDisplayConfigBufferSizes(
26972669
QDC_DATABASE_CURRENT,
26982670
&NumPathArrayElements,
@@ -2704,15 +2676,19 @@ float win32_get_refresh_rate(void *data)
27042676
ModeInfoArray = (DISPLAYCONFIG_MODE_INFO_CUSTOM *)
27052677
malloc(sizeof(DISPLAYCONFIG_MODE_INFO_CUSTOM) * NumModeInfoArrayElements);
27062678

2707-
if (pQueryDisplayConfig(QDC_DATABASE_CURRENT,
2708-
&NumPathArrayElements,
2709-
PathInfoArray,
2710-
&NumModeInfoArrayElements,
2711-
ModeInfoArray,
2712-
&TopologyID) == ERROR_SUCCESS
2713-
&& NumPathArrayElements >= 1)
2714-
refresh_rate = (float) PathInfoArray[0].targetInfo.refreshRate.Numerator /
2715-
PathInfoArray[0].targetInfo.refreshRate.Denominator;
2679+
if (PathInfoArray && ModeInfoArray)
2680+
{
2681+
if (pQueryDisplayConfig(QDC_DATABASE_CURRENT,
2682+
&NumPathArrayElements,
2683+
PathInfoArray,
2684+
&NumModeInfoArrayElements,
2685+
ModeInfoArray,
2686+
&TopologyID) == ERROR_SUCCESS
2687+
&& NumPathArrayElements >= 1
2688+
&& PathInfoArray[0].targetInfo.refreshRate.Denominator != 0)
2689+
refresh_rate = (float)PathInfoArray[0].targetInfo.refreshRate.Numerator /
2690+
PathInfoArray[0].targetInfo.refreshRate.Denominator;
2691+
}
27162692

27172693
free(ModeInfoArray);
27182694
free(PathInfoArray);

0 commit comments

Comments
 (0)