From 39e84fd5b23ddcb6fe489e55918709558f7ae0eb Mon Sep 17 00:00:00 2001 From: Autechre Date: Thu, 21 Jan 2021 02:59:30 +0100 Subject: [PATCH] Refactor Win32 menubar code - (#11914) * Get rid of camelCase notation * Turn functions into static functions if not used outside * Rewrite while (true) as for (;;) * Some general cleanups --- gfx/common/win32_common.c | 251 +++++++++++++++++++------------------- 1 file changed, 126 insertions(+), 125 deletions(-) diff --git a/gfx/common/win32_common.c b/gfx/common/win32_common.c index fff3f04e2f..616652dd90 100644 --- a/gfx/common/win32_common.c +++ b/gfx/common/win32_common.c @@ -1695,110 +1695,120 @@ void win32_set_style(MONITORINFOEX *current_mon, HMONITOR *hm_to_use, #ifdef HAVE_MENU /* Given a Win32 Resource ID, return a RetroArch menu ID (for renaming the menu item) */ -enum msg_hash_enums menu_id_to_label_enum(unsigned int menuId) +static enum msg_hash_enums menu_id_to_label_enum(unsigned int menuId) { switch (menuId) { - case ID_M_LOAD_CONTENT: return MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST; - case ID_M_RESET: return MENU_ENUM_LABEL_VALUE_RESTART_CONTENT; - case ID_M_QUIT: return MENU_ENUM_LABEL_VALUE_INPUT_META_QUIT_KEY; - case ID_M_MENU_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_MENU_TOGGLE; - case ID_M_PAUSE_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_PAUSE_TOGGLE; - case ID_M_LOAD_CORE: return MENU_ENUM_LABEL_VALUE_CORE_LIST; - case ID_M_LOAD_STATE: return MENU_ENUM_LABEL_VALUE_LOAD_STATE; - case ID_M_SAVE_STATE: return MENU_ENUM_LABEL_VALUE_SAVE_STATE; - case ID_M_DISK_CYCLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_EJECT_TOGGLE; - case ID_M_DISK_NEXT: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_NEXT; - case ID_M_DISK_PREV: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_PREV; - case ID_M_WINDOW_SCALE_1X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_2X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_3X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_4X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_5X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_6X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_7X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_8X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_9X: return MSG_UNKNOWN; - case ID_M_WINDOW_SCALE_10X: return MSG_UNKNOWN; - case ID_M_FULL_SCREEN: return MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY; - case ID_M_MOUSE_GRAB: return MENU_ENUM_LABEL_VALUE_INPUT_META_GRAB_MOUSE_TOGGLE; - case ID_M_STATE_INDEX_AUTO: return MSG_UNKNOWN; - case ID_M_TAKE_SCREENSHOT: return MENU_ENUM_LABEL_VALUE_INPUT_META_SCREENSHOT; - case ID_M_MUTE_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_MUTE; - default: return MSG_UNKNOWN; + case ID_M_LOAD_CONTENT: + return MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST; + case ID_M_RESET: + return MENU_ENUM_LABEL_VALUE_RESTART_CONTENT; + case ID_M_QUIT: + return MENU_ENUM_LABEL_VALUE_INPUT_META_QUIT_KEY; + case ID_M_MENU_TOGGLE: + return MENU_ENUM_LABEL_VALUE_INPUT_META_MENU_TOGGLE; + case ID_M_PAUSE_TOGGLE: + return MENU_ENUM_LABEL_VALUE_INPUT_META_PAUSE_TOGGLE; + case ID_M_LOAD_CORE: + return MENU_ENUM_LABEL_VALUE_CORE_LIST; + case ID_M_LOAD_STATE: + return MENU_ENUM_LABEL_VALUE_LOAD_STATE; + case ID_M_SAVE_STATE: + return MENU_ENUM_LABEL_VALUE_SAVE_STATE; + case ID_M_DISK_CYCLE: + return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_EJECT_TOGGLE; + case ID_M_DISK_NEXT: + return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_NEXT; + case ID_M_DISK_PREV: + return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_PREV; + case ID_M_FULL_SCREEN: + return MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY; + case ID_M_MOUSE_GRAB: + return MENU_ENUM_LABEL_VALUE_INPUT_META_GRAB_MOUSE_TOGGLE; + case ID_M_TAKE_SCREENSHOT: + return MENU_ENUM_LABEL_VALUE_INPUT_META_SCREENSHOT; + case ID_M_MUTE_TOGGLE: + return MENU_ENUM_LABEL_VALUE_INPUT_META_MUTE; + default: + break; } + + return MSG_UNKNOWN; } /* Given a RetroArch menu ID, get its shortcut key (meta key) */ -unsigned int menu_id_to_meta_key(unsigned int menuId) +static unsigned int menu_id_to_meta_key(unsigned int menu_id) { - switch (menuId) + switch (menu_id) { - case ID_M_LOAD_CONTENT: return 0; - case ID_M_RESET: return RARCH_RESET; - case ID_M_QUIT: return RARCH_QUIT_KEY; - case ID_M_MENU_TOGGLE: return RARCH_MENU_TOGGLE; - case ID_M_PAUSE_TOGGLE: return RARCH_PAUSE_TOGGLE; - case ID_M_LOAD_CORE: return 0; - case ID_M_LOAD_STATE: return RARCH_LOAD_STATE_KEY; - case ID_M_SAVE_STATE: return RARCH_SAVE_STATE_KEY; - case ID_M_DISK_CYCLE: return RARCH_DISK_EJECT_TOGGLE; - case ID_M_DISK_NEXT: return RARCH_DISK_NEXT; - case ID_M_DISK_PREV: return RARCH_DISK_PREV; - case ID_M_WINDOW_SCALE_1X: return 0; - case ID_M_WINDOW_SCALE_2X: return 0; - case ID_M_WINDOW_SCALE_3X: return 0; - case ID_M_WINDOW_SCALE_4X: return 0; - case ID_M_WINDOW_SCALE_5X: return 0; - case ID_M_WINDOW_SCALE_6X: return 0; - case ID_M_WINDOW_SCALE_7X: return 0; - case ID_M_WINDOW_SCALE_8X: return 0; - case ID_M_WINDOW_SCALE_9X: return 0; - case ID_M_WINDOW_SCALE_10X: return 0; - case ID_M_FULL_SCREEN: return RARCH_FULLSCREEN_TOGGLE_KEY; - case ID_M_MOUSE_GRAB: return RARCH_GRAB_MOUSE_TOGGLE; - case ID_M_STATE_INDEX_AUTO: return 0; - case ID_M_TAKE_SCREENSHOT: return RARCH_SCREENSHOT; - case ID_M_MUTE_TOGGLE: return RARCH_MUTE; - default: return 0; + case ID_M_RESET: + return RARCH_RESET; + case ID_M_QUIT: + return RARCH_QUIT_KEY; + case ID_M_MENU_TOGGLE: + return RARCH_MENU_TOGGLE; + case ID_M_PAUSE_TOGGLE: + return RARCH_PAUSE_TOGGLE; + case ID_M_LOAD_STATE: + return RARCH_LOAD_STATE_KEY; + case ID_M_SAVE_STATE: + return RARCH_SAVE_STATE_KEY; + case ID_M_DISK_CYCLE: + return RARCH_DISK_EJECT_TOGGLE; + case ID_M_DISK_NEXT: + return RARCH_DISK_NEXT; + case ID_M_DISK_PREV: + return RARCH_DISK_PREV; + case ID_M_FULL_SCREEN: + return RARCH_FULLSCREEN_TOGGLE_KEY; + case ID_M_MOUSE_GRAB: + return RARCH_GRAB_MOUSE_TOGGLE; + case ID_M_TAKE_SCREENSHOT: + return RARCH_SCREENSHOT; + case ID_M_MUTE_TOGGLE: + return RARCH_MUTE; + default: + break; } + + return 0; } /* Given a short key (meta key), get its name as a string */ -/* For single character results, may return same pointer with different data inside (modifying the old result) */ -const char* meta_key_to_name(unsigned int metaKey) +/* For single character results, may return same pointer + * with different data inside (modifying the old result) */ +static const char *meta_key_to_name(unsigned int meta_key) { - if (metaKey == 0) - { - return NULL; - } - else + if (meta_key != 0) { int i = 0; - const struct retro_keybind* key = &input_config_binds[0][metaKey]; - int keyCode = key->key; - while (true) + const struct retro_keybind* key = &input_config_binds[0][meta_key]; + int key_code = key->key; + + for (;;) { const struct input_key_map* entry = &input_config_key_map[i]; - if (entry->str == NULL) break; - if (entry->key == keyCode) - { + if (!entry->str) + break; + if (entry->key == key_code) return entry->str; - } i++; } - if (keyCode >= 32 && keyCode < 127) + + if (key_code >= 32 && key_code < 127) { - static char singleChar[2] = "A"; - singleChar[0] = keyCode; - return singleChar; + static char single_char[2] = "A"; + single_char[0] = key_code; + return single_char; } - return NULL; } + + return NULL; } -/* Replaces Menu Item text with localized menu text, and displays the current shortcut key */ -void win32_localize_menu(HMENU menu) +/* Replaces Menu Item text with localized menu text, + * and displays the current shortcut key */ +static void win32_localize_menu(HMENU menu) { int index = 0; #ifndef LEGACY_WIN32 @@ -1806,63 +1816,62 @@ void win32_localize_menu(HMENU menu) #else MENUITEMINFOA menuItemInfo; #endif - while (true) + + for (;;) { BOOL okay; - enum msg_hash_enums labelEnum; + enum msg_hash_enums label_enum; memset(&menuItemInfo, 0, sizeof(menuItemInfo)); - menuItemInfo.cbSize = sizeof(menuItemInfo); + menuItemInfo.cbSize = sizeof(menuItemInfo); menuItemInfo.dwTypeData = NULL; - menuItemInfo.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_SUBMENU; + menuItemInfo.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_SUBMENU; #ifndef LEGACY_WIN32 - okay = GetMenuItemInfoW(menu, index, true, &menuItemInfo); + okay = GetMenuItemInfoW(menu, index, true, &menuItemInfo); #else - okay = GetMenuItemInfoA(menu, index, true, &menuItemInfo); + okay = GetMenuItemInfoA(menu, index, true, &menuItemInfo); #endif - if (!okay) break; + if (!okay) + break; - if (menuItemInfo.hSubMenu != NULL) - { - /* Recursion - call this on submenu items too */ + /* Recursion - call this on submenu items too */ + if (menuItemInfo.hSubMenu) win32_localize_menu(menuItemInfo.hSubMenu); - } - labelEnum = menu_id_to_label_enum(menuItemInfo.wID); - if (labelEnum != MSG_UNKNOWN) + label_enum = menu_id_to_label_enum(menuItemInfo.wID); + if (label_enum != MSG_UNKNOWN) { - const char* newLabel = msg_hash_to_str(labelEnum); - unsigned int metaKey = menu_id_to_meta_key(menuItemInfo.wID); - const char* metaKeyName = meta_key_to_name(metaKey); - const char* newLabel2 = newLabel; - char* newLabelText = NULL; + int len; #ifndef LEGACY_WIN32 wchar_t* newLabel_unicode; #else char* newLabel_ansi; #endif - int len; + const char* newLabel = msg_hash_to_str(label_enum); + unsigned int metaKey = menu_id_to_meta_key(menuItemInfo.wID); + const char* metaKeyName = meta_key_to_name(metaKey); + const char* newLabel2 = newLabel; + char* newLabelText = NULL; /* specific replacements: Load Content = "Ctrl+O" Fullscreen = "Alt+Enter" */ - if (labelEnum == MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST) - { + if (label_enum == + MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST) metaKeyName = "Ctrl+O"; - } - if (labelEnum == MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY) - { + else if (label_enum == + MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY) metaKeyName = "Alt+Enter"; - } /* Append localized name, tab character, and Shortcut Key */ - if (metaKeyName != NULL && 0 != strcmp(metaKeyName, "nul")) + if (metaKeyName && 0 != strcmp(metaKeyName, "nul")) { - int len1 = strlen(newLabel); - int len2 = strlen(metaKeyName); - int bufSize = len1 + len2 + 2; + int len1 = strlen(newLabel); + int len2 = strlen(metaKeyName); + int bufSize = len1 + len2 + 2; newLabelText = (char*)malloc(bufSize); - if (newLabelText != NULL) + + if (newLabelText) { newLabel2 = newLabelText; strcpy(newLabelText, newLabel); @@ -1873,39 +1882,31 @@ void win32_localize_menu(HMENU menu) } } - /* convert string from UTF-8, then assign menu text */ #ifndef LEGACY_WIN32 - newLabel_unicode = utf8_to_utf16_string_alloc(newLabel2); - len = wcslen(newLabel_unicode); - menuItemInfo.cch = len; + /* Convert string from UTF-8, then assign menu text */ + newLabel_unicode = utf8_to_utf16_string_alloc(newLabel2); + len = wcslen(newLabel_unicode); + menuItemInfo.cch = len; menuItemInfo.dwTypeData = newLabel_unicode; SetMenuItemInfoW(menu, index, true, &menuItemInfo); free(newLabel_unicode); #else - newLabel_ansi = utf8_to_local_string_alloc(newLabel2); - len = strlen(newLabel_ansi); - menuItemInfo.cch = len; + newLabel_ansi = utf8_to_local_string_alloc(newLabel2); + len = strlen(newLabel_ansi); + menuItemInfo.cch = len; menuItemInfo.dwTypeData = newLabel_ansi; SetMenuItemInfoA(menu, index, true, &menuItemInfo); free(newLabel_ansi); #endif - if (newLabelText != NULL) - { + if (newLabelText) free(newLabelText); - } } index++; } } - #else - /* Blank version in case RetroArch was built with Win32 Menu but not the menu system (this should never happen) */ -void win32_localize_menu(HMENU menu) -{ - -} - +static void win32_localize_menu(HMENU menu) { } #endif void win32_set_window(unsigned *width, unsigned *height,