Commit 93764acc authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Sort keys by name rather than key code

Names are looked up when starting VLC.
Codes are looked up when editing the preferences.
I prfer to optimize the VLC startup.
parent 70000c98
...@@ -49,18 +49,24 @@ typedef struct key_descriptor_s ...@@ -49,18 +49,24 @@ typedef struct key_descriptor_s
} key_descriptor_t; } key_descriptor_t;
static const struct key_descriptor_s vlc_keys[] = static const struct key_descriptor_s vlc_keys[] =
{ { /* Alphabetical order */
{ "Unset", KEY_UNSET },
{ "Backspace", KEY_BACKSPACE }, { "Backspace", KEY_BACKSPACE },
{ "Tab", KEY_TAB }, { "Browser Back", KEY_BROWSER_BACK },
{ "Browser Favorites", KEY_BROWSER_FAVORITES },
{ "Browser Forward", KEY_BROWSER_FORWARD },
{ "Browser Home", KEY_BROWSER_HOME },
{ "Browser Refresh", KEY_BROWSER_REFRESH },
{ "Browser Search", KEY_BROWSER_SEARCH },
{ "Browser Stop", KEY_BROWSER_STOP },
{ "Delete", KEY_DELETE },
{ "Down", KEY_DOWN },
{ "End", KEY_END },
{ "Enter", KEY_ENTER }, { "Enter", KEY_ENTER },
{ "Esc", KEY_ESC }, { "Esc", KEY_ESC },
{ "Space", ' ' },
{ "Left", KEY_LEFT },
{ "Right", KEY_RIGHT },
{ "Up", KEY_UP },
{ "Down", KEY_DOWN },
{ "F1", KEY_F1 }, { "F1", KEY_F1 },
{ "F10", KEY_F10 },
{ "F11", KEY_F11 },
{ "F12", KEY_F12 },
{ "F2", KEY_F2 }, { "F2", KEY_F2 },
{ "F3", KEY_F3 }, { "F3", KEY_F3 },
{ "F4", KEY_F4 }, { "F4", KEY_F4 },
...@@ -69,40 +75,35 @@ static const struct key_descriptor_s vlc_keys[] = ...@@ -69,40 +75,35 @@ static const struct key_descriptor_s vlc_keys[] =
{ "F7", KEY_F7 }, { "F7", KEY_F7 },
{ "F8", KEY_F8 }, { "F8", KEY_F8 },
{ "F9", KEY_F9 }, { "F9", KEY_F9 },
{ "F10", KEY_F10 },
{ "F11", KEY_F11 },
{ "F12", KEY_F12 },
{ "Home", KEY_HOME }, { "Home", KEY_HOME },
{ "End", KEY_END },
{ "Insert", KEY_INSERT }, { "Insert", KEY_INSERT },
{ "Delete", KEY_DELETE }, { "Left", KEY_LEFT },
{ "Menu", KEY_MENU },
{ "Page Up", KEY_PAGEUP },
{ "Page Down", KEY_PAGEDOWN },
{ "Browser Back", KEY_BROWSER_BACK },
{ "Browser Forward", KEY_BROWSER_FORWARD },
{ "Browser Refresh", KEY_BROWSER_REFRESH },
{ "Browser Stop", KEY_BROWSER_STOP },
{ "Browser Search", KEY_BROWSER_SEARCH },
{ "Browser Favorites", KEY_BROWSER_FAVORITES },
{ "Browser Home", KEY_BROWSER_HOME },
{ "Volume Mute", KEY_VOLUME_MUTE },
{ "Volume Down", KEY_VOLUME_DOWN },
{ "Volume Up", KEY_VOLUME_UP },
{ "Media Next Track", KEY_MEDIA_NEXT_TRACK }, { "Media Next Track", KEY_MEDIA_NEXT_TRACK },
{ "Media Play Pause", KEY_MEDIA_PLAY_PAUSE },
{ "Media Prev Track", KEY_MEDIA_PREV_TRACK }, { "Media Prev Track", KEY_MEDIA_PREV_TRACK },
{ "Media Stop", KEY_MEDIA_STOP }, { "Media Stop", KEY_MEDIA_STOP },
{ "Media Play Pause", KEY_MEDIA_PLAY_PAUSE }, { "Menu", KEY_MENU },
{ "Mouse Wheel Up", KEY_MOUSEWHEELUP },
{ "Mouse Wheel Down", KEY_MOUSEWHEELDOWN }, { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN },
{ "Mouse Wheel Left", KEY_MOUSEWHEELLEFT }, { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT },
{ "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT }, { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT },
{ "Mouse Wheel Up", KEY_MOUSEWHEELUP },
{ "Page Down", KEY_PAGEDOWN },
{ "Page Up", KEY_PAGEUP },
{ "Right", KEY_RIGHT },
{ "Space", ' ' },
{ "Tab", KEY_TAB },
{ "Unset", KEY_UNSET },
{ "Up", KEY_UP },
{ "Volume Mute", KEY_VOLUME_MUTE },
{ "Volume Down", KEY_VOLUME_DOWN },
{ "Volume Up", KEY_VOLUME_UP },
}; };
enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) }; #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
static int cmpkey (const void *key, const void *elem) static int keystrcmp (const void *key, const void *elem)
{ {
return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code; const char *sa = key, *sb = elem;
return strcmp (sa, sb);
} }
/* Convert Unicode code point to UTF-8 */ /* Convert Unicode code point to UTF-8 */
...@@ -153,7 +154,7 @@ static ...@@ -153,7 +154,7 @@ static
uint_fast32_t vlc_str2keycode (const char *name) uint_fast32_t vlc_str2keycode (const char *name)
{ {
uint_fast32_t mods = 0; uint_fast32_t mods = 0;
uint32_t cp; uint32_t code;
for (;;) for (;;)
{ {
...@@ -175,11 +176,17 @@ uint_fast32_t vlc_str2keycode (const char *name) ...@@ -175,11 +176,17 @@ uint_fast32_t vlc_str2keycode (const char *name)
name += len + 1; name += len + 1;
} }
for (size_t i = 0; i < vlc_num_keys; i++) key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
if (!strcasecmp( vlc_keys[i].psz_key_string, name)) sizeof (vlc_keys[0]), keystrcmp);
return vlc_keys[i].i_key_code | mods; if (d != NULL)
code = d->i_key_code;
else
if (vlc_towc (name, &code) <= 0)
code = KEY_UNSET;
return (vlc_towc (name, &cp) > 0) ? (mods | cp) : KEY_UNSET; if (code != KEY_UNSET)
code |= mods;
return code;
} }
/** /**
...@@ -189,23 +196,29 @@ uint_fast32_t vlc_str2keycode (const char *name) ...@@ -189,23 +196,29 @@ uint_fast32_t vlc_str2keycode (const char *name)
*/ */
char *vlc_keycode2str (uint_fast32_t code) char *vlc_keycode2str (uint_fast32_t code)
{ {
const char *name;
char *str, buf[5]; char *str, buf[5];
uintptr_t key = code & ~KEY_MODIFIER; uintptr_t key = code & ~KEY_MODIFIER;
key_descriptor_t *d = bsearch ((void *)key, vlc_keys, vlc_num_keys, for (size_t i = 0; i < KEYS_COUNT; i++)
sizeof (vlc_keys[0]), cmpkey); if (vlc_keys[i].i_key_code == key)
if (d == NULL && utf8_cp (key, buf) == NULL) {
name = vlc_keys[i].psz_key_string;
goto found;
}
if (utf8_cp (key, buf) == NULL)
return NULL; return NULL;
name = buf;
found:
if (asprintf (&str, "%s%s%s%s%s%s", if (asprintf (&str, "%s%s%s%s%s%s",
(code & KEY_MODIFIER_CTRL) ? "Ctrl+" : "", (code & KEY_MODIFIER_CTRL) ? "Ctrl+" : "",
(code & KEY_MODIFIER_ALT) ? "Alt+" : "", (code & KEY_MODIFIER_ALT) ? "Alt+" : "",
(code & KEY_MODIFIER_SHIFT) ? "Shift+" : "", (code & KEY_MODIFIER_SHIFT) ? "Shift+" : "",
(code & KEY_MODIFIER_META) ? "Meta+" : "", (code & KEY_MODIFIER_META) ? "Meta+" : "",
(code & KEY_MODIFIER_COMMAND) ? "Command+" : "", (code & KEY_MODIFIER_COMMAND) ? "Command+" : "", name) == -1)
(d != NULL) ? d->psz_key_string : buf) == -1)
return NULL; return NULL;
return str; return str;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment