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
This commit is contained in:
Autechre 2021-01-21 02:59:30 +01:00 committed by GitHub
parent 47784c66f0
commit 39e84fd5b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1695,110 +1695,120 @@ void win32_set_style(MONITORINFOEX *current_mon, HMONITOR *hm_to_use,
#ifdef HAVE_MENU #ifdef HAVE_MENU
/* Given a Win32 Resource ID, return a RetroArch menu ID (for renaming the menu item) */ /* 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) switch (menuId)
{ {
case ID_M_LOAD_CONTENT: return MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST; case ID_M_LOAD_CONTENT:
case ID_M_RESET: return MENU_ENUM_LABEL_VALUE_RESTART_CONTENT; return MENU_ENUM_LABEL_VALUE_LOAD_CONTENT_LIST;
case ID_M_QUIT: return MENU_ENUM_LABEL_VALUE_INPUT_META_QUIT_KEY; case ID_M_RESET:
case ID_M_MENU_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_MENU_TOGGLE; return MENU_ENUM_LABEL_VALUE_RESTART_CONTENT;
case ID_M_PAUSE_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_PAUSE_TOGGLE; case ID_M_QUIT:
case ID_M_LOAD_CORE: return MENU_ENUM_LABEL_VALUE_CORE_LIST; return MENU_ENUM_LABEL_VALUE_INPUT_META_QUIT_KEY;
case ID_M_LOAD_STATE: return MENU_ENUM_LABEL_VALUE_LOAD_STATE; case ID_M_MENU_TOGGLE:
case ID_M_SAVE_STATE: return MENU_ENUM_LABEL_VALUE_SAVE_STATE; return MENU_ENUM_LABEL_VALUE_INPUT_META_MENU_TOGGLE;
case ID_M_DISK_CYCLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_EJECT_TOGGLE; case ID_M_PAUSE_TOGGLE:
case ID_M_DISK_NEXT: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_NEXT; return MENU_ENUM_LABEL_VALUE_INPUT_META_PAUSE_TOGGLE;
case ID_M_DISK_PREV: return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_PREV; case ID_M_LOAD_CORE:
case ID_M_WINDOW_SCALE_1X: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_CORE_LIST;
case ID_M_WINDOW_SCALE_2X: return MSG_UNKNOWN; case ID_M_LOAD_STATE:
case ID_M_WINDOW_SCALE_3X: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_LOAD_STATE;
case ID_M_WINDOW_SCALE_4X: return MSG_UNKNOWN; case ID_M_SAVE_STATE:
case ID_M_WINDOW_SCALE_5X: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_SAVE_STATE;
case ID_M_WINDOW_SCALE_6X: return MSG_UNKNOWN; case ID_M_DISK_CYCLE:
case ID_M_WINDOW_SCALE_7X: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_EJECT_TOGGLE;
case ID_M_WINDOW_SCALE_8X: return MSG_UNKNOWN; case ID_M_DISK_NEXT:
case ID_M_WINDOW_SCALE_9X: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_NEXT;
case ID_M_WINDOW_SCALE_10X: return MSG_UNKNOWN; case ID_M_DISK_PREV:
case ID_M_FULL_SCREEN: return MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY; return MENU_ENUM_LABEL_VALUE_INPUT_META_DISK_PREV;
case ID_M_MOUSE_GRAB: return MENU_ENUM_LABEL_VALUE_INPUT_META_GRAB_MOUSE_TOGGLE; case ID_M_FULL_SCREEN:
case ID_M_STATE_INDEX_AUTO: return MSG_UNKNOWN; return MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY;
case ID_M_TAKE_SCREENSHOT: return MENU_ENUM_LABEL_VALUE_INPUT_META_SCREENSHOT; case ID_M_MOUSE_GRAB:
case ID_M_MUTE_TOGGLE: return MENU_ENUM_LABEL_VALUE_INPUT_META_MUTE; return MENU_ENUM_LABEL_VALUE_INPUT_META_GRAB_MOUSE_TOGGLE;
default: 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:
break;
} }
return MSG_UNKNOWN;
} }
/* Given a RetroArch menu ID, get its shortcut key (meta key) */ /* 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:
case ID_M_RESET: return RARCH_RESET; return RARCH_RESET;
case ID_M_QUIT: return RARCH_QUIT_KEY; case ID_M_QUIT:
case ID_M_MENU_TOGGLE: return RARCH_MENU_TOGGLE; return RARCH_QUIT_KEY;
case ID_M_PAUSE_TOGGLE: return RARCH_PAUSE_TOGGLE; case ID_M_MENU_TOGGLE:
case ID_M_LOAD_CORE: return 0; return RARCH_MENU_TOGGLE;
case ID_M_LOAD_STATE: return RARCH_LOAD_STATE_KEY; case ID_M_PAUSE_TOGGLE:
case ID_M_SAVE_STATE: return RARCH_SAVE_STATE_KEY; return RARCH_PAUSE_TOGGLE;
case ID_M_DISK_CYCLE: return RARCH_DISK_EJECT_TOGGLE; case ID_M_LOAD_STATE:
case ID_M_DISK_NEXT: return RARCH_DISK_NEXT; return RARCH_LOAD_STATE_KEY;
case ID_M_DISK_PREV: return RARCH_DISK_PREV; case ID_M_SAVE_STATE:
case ID_M_WINDOW_SCALE_1X: return 0; return RARCH_SAVE_STATE_KEY;
case ID_M_WINDOW_SCALE_2X: return 0; case ID_M_DISK_CYCLE:
case ID_M_WINDOW_SCALE_3X: return 0; return RARCH_DISK_EJECT_TOGGLE;
case ID_M_WINDOW_SCALE_4X: return 0; case ID_M_DISK_NEXT:
case ID_M_WINDOW_SCALE_5X: return 0; return RARCH_DISK_NEXT;
case ID_M_WINDOW_SCALE_6X: return 0; case ID_M_DISK_PREV:
case ID_M_WINDOW_SCALE_7X: return 0; return RARCH_DISK_PREV;
case ID_M_WINDOW_SCALE_8X: return 0; case ID_M_FULL_SCREEN:
case ID_M_WINDOW_SCALE_9X: return 0; return RARCH_FULLSCREEN_TOGGLE_KEY;
case ID_M_WINDOW_SCALE_10X: return 0; case ID_M_MOUSE_GRAB:
case ID_M_FULL_SCREEN: return RARCH_FULLSCREEN_TOGGLE_KEY; return RARCH_GRAB_MOUSE_TOGGLE;
case ID_M_MOUSE_GRAB: return RARCH_GRAB_MOUSE_TOGGLE; case ID_M_TAKE_SCREENSHOT:
case ID_M_STATE_INDEX_AUTO: return 0; return RARCH_SCREENSHOT;
case ID_M_TAKE_SCREENSHOT: return RARCH_SCREENSHOT; case ID_M_MUTE_TOGGLE:
case ID_M_MUTE_TOGGLE: return RARCH_MUTE; return RARCH_MUTE;
default: return 0; default:
break;
} }
return 0;
} }
/* Given a short key (meta key), get its name as a string */ /* 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) */ /* For single character results, may return same pointer
const char* meta_key_to_name(unsigned int metaKey) * with different data inside (modifying the old result) */
static const char *meta_key_to_name(unsigned int meta_key)
{ {
if (metaKey == 0) if (meta_key != 0)
{
return NULL;
}
else
{ {
int i = 0; int i = 0;
const struct retro_keybind* key = &input_config_binds[0][metaKey]; const struct retro_keybind* key = &input_config_binds[0][meta_key];
int keyCode = key->key; int key_code = key->key;
while (true)
for (;;)
{ {
const struct input_key_map* entry = &input_config_key_map[i]; const struct input_key_map* entry = &input_config_key_map[i];
if (entry->str == NULL) break; if (!entry->str)
if (entry->key == keyCode) break;
{ if (entry->key == key_code)
return entry->str; return entry->str;
}
i++; i++;
} }
if (keyCode >= 32 && keyCode < 127)
if (key_code >= 32 && key_code < 127)
{ {
static char singleChar[2] = "A"; static char single_char[2] = "A";
singleChar[0] = keyCode; single_char[0] = key_code;
return singleChar; return single_char;
} }
return NULL;
} }
return NULL;
} }
/* Replaces Menu Item text with localized menu text, and displays the current shortcut key */ /* Replaces Menu Item text with localized menu text,
void win32_localize_menu(HMENU menu) * and displays the current shortcut key */
static void win32_localize_menu(HMENU menu)
{ {
int index = 0; int index = 0;
#ifndef LEGACY_WIN32 #ifndef LEGACY_WIN32
@ -1806,63 +1816,62 @@ void win32_localize_menu(HMENU menu)
#else #else
MENUITEMINFOA menuItemInfo; MENUITEMINFOA menuItemInfo;
#endif #endif
while (true)
for (;;)
{ {
BOOL okay; BOOL okay;
enum msg_hash_enums labelEnum; enum msg_hash_enums label_enum;
memset(&menuItemInfo, 0, sizeof(menuItemInfo)); memset(&menuItemInfo, 0, sizeof(menuItemInfo));
menuItemInfo.cbSize = sizeof(menuItemInfo); menuItemInfo.cbSize = sizeof(menuItemInfo);
menuItemInfo.dwTypeData = NULL; 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 #ifndef LEGACY_WIN32
okay = GetMenuItemInfoW(menu, index, true, &menuItemInfo); okay = GetMenuItemInfoW(menu, index, true, &menuItemInfo);
#else #else
okay = GetMenuItemInfoA(menu, index, true, &menuItemInfo); okay = GetMenuItemInfoA(menu, index, true, &menuItemInfo);
#endif #endif
if (!okay) break; if (!okay)
break;
if (menuItemInfo.hSubMenu != NULL) /* Recursion - call this on submenu items too */
{ if (menuItemInfo.hSubMenu)
/* Recursion - call this on submenu items too */
win32_localize_menu(menuItemInfo.hSubMenu); win32_localize_menu(menuItemInfo.hSubMenu);
}
labelEnum = menu_id_to_label_enum(menuItemInfo.wID); label_enum = menu_id_to_label_enum(menuItemInfo.wID);
if (labelEnum != MSG_UNKNOWN) if (label_enum != MSG_UNKNOWN)
{ {
const char* newLabel = msg_hash_to_str(labelEnum); int len;
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;
#ifndef LEGACY_WIN32 #ifndef LEGACY_WIN32
wchar_t* newLabel_unicode; wchar_t* newLabel_unicode;
#else #else
char* newLabel_ansi; char* newLabel_ansi;
#endif #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: /* specific replacements:
Load Content = "Ctrl+O" Load Content = "Ctrl+O"
Fullscreen = "Alt+Enter" */ 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"; metaKeyName = "Ctrl+O";
} else if (label_enum ==
if (labelEnum == MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY) MENU_ENUM_LABEL_VALUE_INPUT_META_FULLSCREEN_TOGGLE_KEY)
{
metaKeyName = "Alt+Enter"; metaKeyName = "Alt+Enter";
}
/* Append localized name, tab character, and Shortcut Key */ /* 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 len1 = strlen(newLabel);
int len2 = strlen(metaKeyName); int len2 = strlen(metaKeyName);
int bufSize = len1 + len2 + 2; int bufSize = len1 + len2 + 2;
newLabelText = (char*)malloc(bufSize); newLabelText = (char*)malloc(bufSize);
if (newLabelText != NULL)
if (newLabelText)
{ {
newLabel2 = newLabelText; newLabel2 = newLabelText;
strcpy(newLabelText, newLabel); 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 #ifndef LEGACY_WIN32
newLabel_unicode = utf8_to_utf16_string_alloc(newLabel2); /* Convert string from UTF-8, then assign menu text */
len = wcslen(newLabel_unicode); newLabel_unicode = utf8_to_utf16_string_alloc(newLabel2);
menuItemInfo.cch = len; len = wcslen(newLabel_unicode);
menuItemInfo.cch = len;
menuItemInfo.dwTypeData = newLabel_unicode; menuItemInfo.dwTypeData = newLabel_unicode;
SetMenuItemInfoW(menu, index, true, &menuItemInfo); SetMenuItemInfoW(menu, index, true, &menuItemInfo);
free(newLabel_unicode); free(newLabel_unicode);
#else #else
newLabel_ansi = utf8_to_local_string_alloc(newLabel2); newLabel_ansi = utf8_to_local_string_alloc(newLabel2);
len = strlen(newLabel_ansi); len = strlen(newLabel_ansi);
menuItemInfo.cch = len; menuItemInfo.cch = len;
menuItemInfo.dwTypeData = newLabel_ansi; menuItemInfo.dwTypeData = newLabel_ansi;
SetMenuItemInfoA(menu, index, true, &menuItemInfo); SetMenuItemInfoA(menu, index, true, &menuItemInfo);
free(newLabel_ansi); free(newLabel_ansi);
#endif #endif
if (newLabelText != NULL) if (newLabelText)
{
free(newLabelText); free(newLabelText);
}
} }
index++; index++;
} }
} }
#else #else
/* Blank version in case RetroArch was built with Win32 Menu but not the menu system (this should never happen) */ /* 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 #endif
void win32_set_window(unsigned *width, unsigned *height, void win32_set_window(unsigned *width, unsigned *height,