1 /*****************************************************************************
2 * keys.c: keys configuration
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
6 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
29 * This file defines functions and structures for hotkey handling in vlc
42 #include <vlc_common.h>
44 #include "configuration.h"
47 typedef struct key_descriptor_s
49 const char psz_key_string[20];
53 static const struct key_descriptor_s vlc_keys[] =
54 { /* Alphabetical order */
55 { N_("Backspace"), KEY_BACKSPACE },
56 { N_("Brightness Down"), KEY_BRIGHTNESS_DOWN },
57 { N_("Brightness Up"), KEY_BRIGHTNESS_UP },
58 { N_("Browser Back"), KEY_BROWSER_BACK },
59 { N_("Browser Favorites"), KEY_BROWSER_FAVORITES },
60 { N_("Browser Forward"), KEY_BROWSER_FORWARD },
61 { N_("Browser Home"), KEY_BROWSER_HOME },
62 { N_("Browser Refresh"), KEY_BROWSER_REFRESH },
63 { N_("Browser Search"), KEY_BROWSER_SEARCH },
64 { N_("Browser Stop"), KEY_BROWSER_STOP },
65 { N_("Delete"), KEY_DELETE },
66 { N_("Down"), KEY_DOWN },
67 { N_("End"), KEY_END },
68 { N_("Enter"), KEY_ENTER },
69 { N_("Esc"), KEY_ESC },
71 { N_("F10"), KEY_F10 },
72 { N_("F11"), KEY_F11 },
73 { N_("F12"), KEY_F12 },
82 { N_("Home"), KEY_HOME },
83 { N_("Insert"), KEY_INSERT },
84 { N_("Left"), KEY_LEFT },
85 { N_("Media Angle"), KEY_MEDIA_ANGLE },
86 { N_("Media Audio Track"), KEY_MEDIA_AUDIO },
87 { N_("Media Forward"), KEY_MEDIA_FORWARD },
88 { N_("Media Menu"), KEY_MEDIA_MENU },
89 { N_("Media Next Frame"), KEY_MEDIA_FRAME_NEXT },
90 { N_("Media Next Track"), KEY_MEDIA_NEXT_TRACK },
91 { N_("Media Play Pause"), KEY_MEDIA_PLAY_PAUSE },
92 { N_("Media Prev Frame"), KEY_MEDIA_FRAME_PREV },
93 { N_("Media Prev Track"), KEY_MEDIA_PREV_TRACK },
94 { N_("Media Record"), KEY_MEDIA_RECORD },
95 { N_("Media Repeat"), KEY_MEDIA_REPEAT },
96 { N_("Media Rewind"), KEY_MEDIA_REWIND },
97 { N_("Media Select"), KEY_MEDIA_SELECT },
98 { N_("Media Shuffle"), KEY_MEDIA_SHUFFLE },
99 { N_("Media Stop"), KEY_MEDIA_STOP },
100 { N_("Media Subtitle"), KEY_MEDIA_SUBTITLE },
101 { N_("Media Time"), KEY_MEDIA_TIME },
102 { N_("Media View"), KEY_MEDIA_VIEW },
103 { N_("Menu"), KEY_MENU },
104 { N_("Mouse Wheel Down"), KEY_MOUSEWHEELDOWN },
105 { N_("Mouse Wheel Left"), KEY_MOUSEWHEELLEFT },
106 { N_("Mouse Wheel Right"), KEY_MOUSEWHEELRIGHT },
107 { N_("Mouse Wheel Up"), KEY_MOUSEWHEELUP },
108 { N_("Page Down"), KEY_PAGEDOWN },
109 { N_("Page Up"), KEY_PAGEUP },
110 { N_("Right"), KEY_RIGHT },
111 { N_("Space"), ' ' },
112 { N_("Tab"), KEY_TAB },
113 { N_("Unset"), KEY_UNSET },
114 { N_("Up"), KEY_UP },
115 { N_("Volume Down"), KEY_VOLUME_DOWN },
116 { N_("Volume Mute"), KEY_VOLUME_MUTE },
117 { N_("Volume Up"), KEY_VOLUME_UP },
118 { N_("Zoom In"), KEY_ZOOM_IN },
119 { N_("Zoom Out"), KEY_ZOOM_OUT },
121 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
123 static int keystrcmp (const void *key, const void *elem)
125 const char *sa = key, *sb = elem;
126 return strcmp (sa, sb);
129 /* Convert Unicode code point to UTF-8 */
130 static char *utf8_cp (uint_fast32_t cp, char *buf)
137 else if (cp < (1 << 11))
140 buf[1] = 0x80 | (cp & 0x3F);
144 else if (cp < (1 << 16))
147 buf[2] = 0x80 | (cp & 0x3F);
149 buf[1] = 0x80 | (cp & 0x3F);
153 else if (cp < (1 << 21))
156 buf[3] = 0x80 | (cp & 0x3F);
158 buf[2] = 0x80 | (cp & 0x3F);
160 buf[1] = 0x80 | (cp & 0x3F);
170 * Parse a human-readable string representation of a VLC key code.
171 * @note This only works with the American English representation
172 * (a.k.a. C or POSIX), not with the local representation returned from
174 * @return a VLC key code, or KEY_UNSET on failure.
176 uint_fast32_t vlc_str2keycode (const char *name)
178 uint_fast32_t mods = 0;
183 size_t len = strcspn (name, "-+");
184 if (len == 0 || name[len] == '\0')
187 if (len == 4 && !strncasecmp (name, "Ctrl", 4))
188 mods |= KEY_MODIFIER_CTRL;
189 if (len == 3 && !strncasecmp (name, "Alt", 3))
190 mods |= KEY_MODIFIER_ALT;
191 if (len == 5 && !strncasecmp (name, "Shift", 5))
192 mods |= KEY_MODIFIER_SHIFT;
193 if (len == 4 && !strncasecmp (name, "Meta", 4))
194 mods |= KEY_MODIFIER_META;
195 if (len == 7 && !strncasecmp (name, "Command", 7))
196 mods |= KEY_MODIFIER_COMMAND;
201 key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
202 sizeof (vlc_keys[0]), keystrcmp);
204 code = d->i_key_code;
206 if (vlc_towc (name, &code) <= 0)
209 if (code != KEY_UNSET)
214 static char *nooptext (const char *txt)
220 * Format a human-readable and unique representation of a VLC key code
221 * (including modifiers).
222 * @param code key code to translate to a string
223 * @param locale true to get a localized string,
224 * false to get a C string suitable for 'vlcrc'
225 * @return a heap-allocated string, or NULL on error.
227 char *vlc_keycode2str (uint_fast32_t code, bool locale)
229 char *(*tr) (const char *) = locale ? vlc_gettext : nooptext;
232 uintptr_t key = code & ~KEY_MODIFIER;
234 for (size_t i = 0; i < KEYS_COUNT; i++)
235 if (vlc_keys[i].i_key_code == key)
237 name = vlc_keys[i].psz_key_string;
241 if (utf8_cp (key, buf) == NULL)
246 if (asprintf (&str, "%s%s%s%s%s%s",
247 (code & KEY_MODIFIER_CTRL) ? tr(N_("Ctrl+")) : "",
248 (code & KEY_MODIFIER_ALT) ? tr(N_("Alt+")) : "",
249 (code & KEY_MODIFIER_SHIFT) ? tr(N_("Shift+")) : "",
250 (code & KEY_MODIFIER_META) ? tr(N_("Meta+")) : "",
251 (code & KEY_MODIFIER_COMMAND) ? tr(N_("Command+")) : "",
258 /*** VLC key map ***/
263 char name[MAXACTION];
267 static const struct action actions[] =
269 /* *MUST* be sorted (ASCII order) */
270 { "aspect-ratio", ACTIONID_ASPECT_RATIO, },
271 { "audio-track", ACTIONID_AUDIO_TRACK, },
272 { "audiodelay-down", ACTIONID_AUDIODELAY_DOWN, },
273 { "audiodelay-up", ACTIONID_AUDIODELAY_UP, },
274 { "audiodevice-cycle", ACTIONID_AUDIODEVICE_CYCLE, },
275 { "chapter-next", ACTIONID_CHAPTER_NEXT, },
276 { "chapter-prev", ACTIONID_CHAPTER_PREV, },
277 { "crop", ACTIONID_CROP, },
278 { "crop-bottom", ACTIONID_CROP_BOTTOM, },
279 { "crop-left", ACTIONID_CROP_LEFT, },
280 { "crop-right", ACTIONID_CROP_RIGHT, },
281 { "crop-top", ACTIONID_CROP_TOP, },
282 { "decr-scalefactor", ACTIONID_SCALE_DOWN, },
283 { "deinterlace", ACTIONID_DEINTERLACE, },
284 { "deinterlace-mode", ACTIONID_DEINTERLACE_MODE, },
285 { "disc-menu", ACTIONID_DISC_MENU, },
286 { "faster", ACTIONID_FASTER, },
287 { "frame-next", ACTIONID_FRAME_NEXT, },
288 { "incr-scalefactor", ACTIONID_SCALE_UP, },
289 { "intf-boss", ACTIONID_INTF_BOSS, },
290 { "intf-show", ACTIONID_INTF_TOGGLE_FSC, },
291 { "jump+extrashort", ACTIONID_JUMP_FORWARD_EXTRASHORT, },
292 { "jump+long", ACTIONID_JUMP_FORWARD_LONG, },
293 { "jump+medium", ACTIONID_JUMP_FORWARD_MEDIUM, },
294 { "jump+short", ACTIONID_JUMP_FORWARD_SHORT, },
295 { "jump-extrashort", ACTIONID_JUMP_BACKWARD_EXTRASHORT, },
296 { "jump-long", ACTIONID_JUMP_BACKWARD_LONG, },
297 { "jump-medium", ACTIONID_JUMP_BACKWARD_MEDIUM, },
298 { "jump-short", ACTIONID_JUMP_BACKWARD_SHORT, },
299 { "leave-fullscreen", ACTIONID_LEAVE_FULLSCREEN, },
300 { "loop", ACTIONID_LOOP, },
301 { "menu-down", ACTIONID_MENU_DOWN, },
302 { "menu-left", ACTIONID_MENU_LEFT, },
303 { "menu-off", ACTIONID_MENU_OFF, },
304 { "menu-on", ACTIONID_MENU_ON, },
305 { "menu-right", ACTIONID_MENU_RIGHT, },
306 { "menu-select", ACTIONID_MENU_SELECT, },
307 { "menu-up", ACTIONID_MENU_UP, },
308 { "nav-activate", ACTIONID_NAV_ACTIVATE, },
309 { "nav-down", ACTIONID_NAV_DOWN, },
310 { "nav-left", ACTIONID_NAV_LEFT, },
311 { "nav-right", ACTIONID_NAV_RIGHT, },
312 { "nav-up", ACTIONID_NAV_UP, },
313 { "next", ACTIONID_NEXT, },
314 { "pause", ACTIONID_PAUSE, },
315 { "play", ACTIONID_PLAY, },
316 { "play-bookmark1", ACTIONID_PLAY_BOOKMARK1, },
317 { "play-bookmark10", ACTIONID_PLAY_BOOKMARK10, },
318 { "play-bookmark2", ACTIONID_PLAY_BOOKMARK2, },
319 { "play-bookmark3", ACTIONID_PLAY_BOOKMARK3, },
320 { "play-bookmark4", ACTIONID_PLAY_BOOKMARK4, },
321 { "play-bookmark5", ACTIONID_PLAY_BOOKMARK5, },
322 { "play-bookmark6", ACTIONID_PLAY_BOOKMARK6, },
323 { "play-bookmark7", ACTIONID_PLAY_BOOKMARK7, },
324 { "play-bookmark8", ACTIONID_PLAY_BOOKMARK8, },
325 { "play-bookmark9", ACTIONID_PLAY_BOOKMARK9, },
326 { "play-pause", ACTIONID_PLAY_PAUSE, },
327 { "position", ACTIONID_POSITION, },
328 { "prev", ACTIONID_PREV, },
329 { "program-sid", ACTIONID_PROGRAM_SID, },
330 { "quit", ACTIONID_QUIT, },
331 { "random", ACTIONID_RANDOM, },
332 { "rate-faster-fine", ACTIONID_RATE_FASTER_FINE, },
333 { "rate-normal", ACTIONID_RATE_NORMAL, },
334 { "rate-slower-fine", ACTIONID_RATE_SLOWER_FINE, },
335 { "record", ACTIONID_RECORD, },
336 { "set-bookmark1", ACTIONID_SET_BOOKMARK1, },
337 { "set-bookmark10", ACTIONID_SET_BOOKMARK10, },
338 { "set-bookmark2", ACTIONID_SET_BOOKMARK2, },
339 { "set-bookmark3", ACTIONID_SET_BOOKMARK3, },
340 { "set-bookmark4", ACTIONID_SET_BOOKMARK4, },
341 { "set-bookmark5", ACTIONID_SET_BOOKMARK5, },
342 { "set-bookmark6", ACTIONID_SET_BOOKMARK6, },
343 { "set-bookmark7", ACTIONID_SET_BOOKMARK7, },
344 { "set-bookmark8", ACTIONID_SET_BOOKMARK8, },
345 { "set-bookmark9", ACTIONID_SET_BOOKMARK9, },
346 { "slower", ACTIONID_SLOWER, },
347 { "snapshot", ACTIONID_SNAPSHOT, },
348 { "stop", ACTIONID_STOP, },
349 { "subdelay-down", ACTIONID_SUBDELAY_DOWN, },
350 { "subdelay-up", ACTIONID_SUBDELAY_UP, },
351 { "subpos-down", ACTIONID_SUBPOS_DOWN, },
352 { "subpos-up", ACTIONID_SUBPOS_UP, },
353 { "subtitle-track", ACTIONID_SUBTITLE_TRACK, },
354 { "title-next", ACTIONID_TITLE_NEXT, },
355 { "title-prev", ACTIONID_TITLE_PREV, },
356 { "toggle-autoscale", ACTIONID_TOGGLE_AUTOSCALE, },
357 { "toggle-fullscreen", ACTIONID_TOGGLE_FULLSCREEN, },
358 { "uncrop-bottom", ACTIONID_UNCROP_BOTTOM, },
359 { "uncrop-left", ACTIONID_UNCROP_LEFT, },
360 { "uncrop-right", ACTIONID_UNCROP_RIGHT, },
361 { "uncrop-top", ACTIONID_UNCROP_TOP, },
362 { "unzoom", ACTIONID_UNZOOM, },
363 { "vol-down", ACTIONID_VOL_DOWN, },
364 { "vol-mute", ACTIONID_VOL_MUTE, },
365 { "vol-up", ACTIONID_VOL_UP, },
366 { "wallpaper", ACTIONID_WALLPAPER, },
367 { "zoom", ACTIONID_ZOOM, },
368 { "zoom-double", ACTIONID_ZOOM_DOUBLE, },
369 { "zoom-half", ACTIONID_ZOOM_HALF, },
370 { "zoom-original", ACTIONID_ZOOM_ORIGINAL, },
371 { "zoom-quarter", ACTIONID_ZOOM_QUARTER, },
373 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
377 uint32_t key; ///< Key code
378 vlc_action_t action; ///< Action ID
381 static int keycmp (const void *a, const void *b)
383 const struct mapping *ka = a, *kb = b;
385 #if (INT_MAX >= 0x7fffffff)
386 return ka->key - kb->key;
388 return (ka->key < kb->key) ? -1 : (ka->key > kb->key) ? +1 : 0;
394 void *map; /* Key map */
395 void *global_map; /* Grabbed/global key map */
396 struct hotkey keys[0];
399 static int vlc_key_to_action (vlc_object_t *obj, const char *varname,
400 vlc_value_t prevkey, vlc_value_t curkey, void *d)
402 void *const *map = d;
403 const struct mapping **pent;
404 uint32_t keycode = curkey.i_int;
406 pent = tfind (&keycode, map, keycmp);
412 return var_SetInteger (obj, "key-action", (*pent)->action);
416 * Sets up all key mappings for a given action.
417 * \param map tree (of struct mapping entries) to write mappings to
418 * \param confname VLC configuration item to read mappings from
419 * \param action action ID
421 static void vlc_MapAction (vlc_object_t *obj, void **map,
422 const char *confname, vlc_action_t action)
424 char *keys = var_InheritString (obj, confname);
428 for (char *buf, *key = strtok_r (keys, "\t", &buf);
430 key = strtok_r (NULL, "\t", &buf))
432 uint32_t code = vlc_str2keycode (key);
433 if (code == KEY_UNSET)
435 msg_Warn (obj, "Key \"%s\" unrecognized", key);
439 struct mapping *entry = malloc (sizeof (*entry));
443 entry->action = action;
445 struct mapping **pent = tsearch (entry, map, keycmp);
446 if (unlikely(pent == NULL))
451 msg_Warn (obj, "Key \"%s\" bound to multiple actions", key);
459 * Initializes the key map from configuration.
461 struct vlc_actions *vlc_InitActions (libvlc_int_t *libvlc)
463 vlc_object_t *obj = VLC_OBJECT(libvlc);
465 struct vlc_actions *as = malloc (sizeof (*as) + (ACTIONS_COUNT + 1) * sizeof (*keys));
467 if (unlikely(as == NULL))
470 as->global_map = NULL;
473 var_Create (obj, "key-pressed", VLC_VAR_INTEGER);
474 var_Create (obj, "global-key-pressed", VLC_VAR_INTEGER);
475 var_Create (obj, "key-action", VLC_VAR_INTEGER);
477 /* Initialize from configuration */
478 for (size_t i = 0; i < ACTIONS_COUNT; i++)
482 && strcmp (actions[i-1].name, actions[i].name) >= 0)
484 msg_Err (libvlc, "key-%s and key-%s are not ordered properly",
485 actions[i-1].name, actions[i].name);
489 keys->psz_action = actions[i].name;
492 char name[12 + MAXACTION];
494 snprintf (name, sizeof (name), "global-key-%s", actions[i].name);
495 vlc_MapAction (obj, &as->map, name + 7, actions[i].value);
496 vlc_MapAction (obj, &as->global_map, name, actions[i].value);
499 keys->psz_action = NULL;
501 libvlc->p_hotkeys = as->keys;
502 var_AddCallback (obj, "key-pressed", vlc_key_to_action, &as->map);
503 var_AddCallback (obj, "global-key-pressed", vlc_key_to_action,
509 * Destroys the key map.
511 void vlc_DeinitActions (libvlc_int_t *libvlc, struct vlc_actions *as)
513 if (unlikely(as == NULL))
516 var_DelCallback (libvlc, "global-key-pressed", vlc_key_to_action,
518 var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, &as->map);
520 tdestroy (as->global_map, free);
521 tdestroy (as->map, free);
523 libvlc->p_hotkeys = NULL;
527 static int actcmp(const void *key, const void *ent)
529 const struct action *act = ent;
530 return strcmp(key, act->name);
534 * Get the action ID from the action name in the configuration subsystem.
535 * @return the action ID or ACTIONID_NONE on error.
537 vlc_action_t vlc_GetActionId (const char *name)
539 const struct action *act;
541 if (strncmp (name, "key-", 4))
542 return ACTIONID_NONE;
545 act = bsearch(name, actions, ACTIONS_COUNT, sizeof(*act), actcmp);
546 return (act != NULL) ? act->value : ACTIONID_NONE;