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_("Browser Back"), KEY_BROWSER_BACK },
57 { N_("Browser Favorites"), KEY_BROWSER_FAVORITES },
58 { N_("Browser Forward"), KEY_BROWSER_FORWARD },
59 { N_("Browser Home"), KEY_BROWSER_HOME },
60 { N_("Browser Refresh"), KEY_BROWSER_REFRESH },
61 { N_("Browser Search"), KEY_BROWSER_SEARCH },
62 { N_("Browser Stop"), KEY_BROWSER_STOP },
63 { N_("Delete"), KEY_DELETE },
64 { N_("Down"), KEY_DOWN },
65 { N_("End"), KEY_END },
66 { N_("Enter"), KEY_ENTER },
67 { N_("Esc"), KEY_ESC },
69 { N_("F10"), KEY_F10 },
70 { N_("F11"), KEY_F11 },
71 { N_("F12"), KEY_F12 },
80 { N_("Home"), KEY_HOME },
81 { N_("Insert"), KEY_INSERT },
82 { N_("Left"), KEY_LEFT },
83 { N_("Media Next Track"), KEY_MEDIA_NEXT_TRACK },
84 { N_("Media Play Pause"), KEY_MEDIA_PLAY_PAUSE },
85 { N_("Media Prev Track"), KEY_MEDIA_PREV_TRACK },
86 { N_("Media Stop"), KEY_MEDIA_STOP },
87 { N_("Menu"), KEY_MENU },
88 { N_("Mouse Wheel Down"), KEY_MOUSEWHEELDOWN },
89 { N_("Mouse Wheel Left"), KEY_MOUSEWHEELLEFT },
90 { N_("Mouse Wheel Right"), KEY_MOUSEWHEELRIGHT },
91 { N_("Mouse Wheel Up"), KEY_MOUSEWHEELUP },
92 { N_("Page Down"), KEY_PAGEDOWN },
93 { N_("Page Up"), KEY_PAGEUP },
94 { N_("Right"), KEY_RIGHT },
96 { N_("Tab"), KEY_TAB },
97 { N_("Unset"), KEY_UNSET },
99 { N_("Volume Down"), KEY_VOLUME_DOWN },
100 { N_("Volume Mute"), KEY_VOLUME_MUTE },
101 { N_("Volume Up"), KEY_VOLUME_UP },
103 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
105 static int keystrcmp (const void *key, const void *elem)
107 const char *sa = key, *sb = elem;
108 return strcmp (sa, sb);
111 /* Convert Unicode code point to UTF-8 */
112 static char *utf8_cp (uint_fast32_t cp, char *buf)
119 else if (cp < (1 << 11))
122 buf[1] = 0x80 | (cp & 0x3F);
126 else if (cp < (1 << 16))
129 buf[2] = 0x80 | (cp & 0x3F);
131 buf[1] = 0x80 | (cp & 0x3F);
135 else if (cp < (1 << 21))
138 buf[3] = 0x80 | (cp & 0x3F);
140 buf[2] = 0x80 | (cp & 0x3F);
142 buf[1] = 0x80 | (cp & 0x3F);
152 * Parse a human-readable string representation of a VLC key code.
153 * @note This only works with the American English representation
154 * (a.k.a. C or POSIX), not with the local representation returned from
156 * @return a VLC key code, or KEY_UNSET on failure.
158 uint_fast32_t vlc_str2keycode (const char *name)
160 uint_fast32_t mods = 0;
165 size_t len = strcspn (name, "-+");
166 if (len == 0 || name[len] == '\0')
169 if (len == 4 && !strncasecmp (name, "Ctrl", 4))
170 mods |= KEY_MODIFIER_CTRL;
171 if (len == 3 && !strncasecmp (name, "Alt", 3))
172 mods |= KEY_MODIFIER_ALT;
173 if (len == 5 && !strncasecmp (name, "Shift", 5))
174 mods |= KEY_MODIFIER_SHIFT;
175 if (len == 4 && !strncasecmp (name, "Meta", 4))
176 mods |= KEY_MODIFIER_META;
177 if (len == 7 && !strncasecmp (name, "Command", 7))
178 mods |= KEY_MODIFIER_COMMAND;
183 key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
184 sizeof (vlc_keys[0]), keystrcmp);
186 code = d->i_key_code;
188 if (vlc_towc (name, &code) <= 0)
191 if (code != KEY_UNSET)
196 static char *nooptext (const char *txt)
202 * Format a human-readable and unique representation of a VLC key code
203 * (including modifiers).
204 * @param code key code to translate to a string
205 * @param locale true to get a localized string,
206 * false to get a C string suitable for 'vlcrc'
207 * @return a heap-allocated string, or NULL on error.
209 char *vlc_keycode2str (uint_fast32_t code, bool locale)
211 char *(*tr) (const char *) = locale ? vlc_gettext : nooptext;
214 uintptr_t key = code & ~KEY_MODIFIER;
216 for (size_t i = 0; i < KEYS_COUNT; i++)
217 if (vlc_keys[i].i_key_code == key)
219 name = vlc_keys[i].psz_key_string;
223 if (utf8_cp (key, buf) == NULL)
228 if (asprintf (&str, "%s%s%s%s%s%s",
229 (code & KEY_MODIFIER_CTRL) ? tr(N_("Ctrl+")) : "",
230 (code & KEY_MODIFIER_ALT) ? tr(N_("Alt+")) : "",
231 (code & KEY_MODIFIER_SHIFT) ? tr(N_("Shift+")) : "",
232 (code & KEY_MODIFIER_META) ? tr(N_("Meta+")) : "",
233 (code & KEY_MODIFIER_COMMAND) ? tr(N_("Command+")) : "",
240 /*** VLC key map ***/
245 char name[MAXACTION];
249 static const struct action actions[] =
251 /* *MUST* be sorted (ASCII order) */
252 { "aspect-ratio", ACTIONID_ASPECT_RATIO, },
253 { "audio-track", ACTIONID_AUDIO_TRACK, },
254 { "audiodelay-down", ACTIONID_AUDIODELAY_DOWN, },
255 { "audiodelay-up", ACTIONID_AUDIODELAY_UP, },
256 { "audiodevice-cycle", ACTIONID_AUDIODEVICE_CYCLE, },
257 { "chapter-next", ACTIONID_CHAPTER_NEXT, },
258 { "chapter-prev", ACTIONID_CHAPTER_PREV, },
259 { "crop", ACTIONID_CROP, },
260 { "crop-bottom", ACTIONID_CROP_BOTTOM, },
261 { "crop-left", ACTIONID_CROP_LEFT, },
262 { "crop-right", ACTIONID_CROP_RIGHT, },
263 { "crop-top", ACTIONID_CROP_TOP, },
264 { "decr-scalefactor", ACTIONID_SCALE_DOWN, },
265 { "deinterlace", ACTIONID_DEINTERLACE, },
266 { "disc-menu", ACTIONID_DISC_MENU, },
267 { "faster", ACTIONID_FASTER, },
268 { "frame-next", ACTIONID_FRAME_NEXT, },
269 { "incr-scalefactor", ACTIONID_SCALE_UP, },
270 { "intf-boss", ACTIONID_INTF_BOSS, },
271 { "intf-show", ACTIONID_INTF_TOGGLE_FSC, },
272 { "jump+extrashort", ACTIONID_JUMP_FORWARD_EXTRASHORT, },
273 { "jump+long", ACTIONID_JUMP_FORWARD_LONG, },
274 { "jump+medium", ACTIONID_JUMP_FORWARD_MEDIUM, },
275 { "jump+short", ACTIONID_JUMP_FORWARD_SHORT, },
276 { "jump-extrashort", ACTIONID_JUMP_BACKWARD_EXTRASHORT, },
277 { "jump-long", ACTIONID_JUMP_BACKWARD_LONG, },
278 { "jump-medium", ACTIONID_JUMP_BACKWARD_MEDIUM, },
279 { "jump-short", ACTIONID_JUMP_BACKWARD_SHORT, },
280 { "leave-fullscreen", ACTIONID_LEAVE_FULLSCREEN, },
281 { "loop", ACTIONID_LOOP, },
282 { "menu-down", ACTIONID_MENU_DOWN, },
283 { "menu-left", ACTIONID_MENU_LEFT, },
284 { "menu-off", ACTIONID_MENU_OFF, },
285 { "menu-on", ACTIONID_MENU_ON, },
286 { "menu-right", ACTIONID_MENU_RIGHT, },
287 { "menu-select", ACTIONID_MENU_SELECT, },
288 { "menu-up", ACTIONID_MENU_UP, },
289 { "nav-activate", ACTIONID_NAV_ACTIVATE, },
290 { "nav-down", ACTIONID_NAV_DOWN, },
291 { "nav-left", ACTIONID_NAV_LEFT, },
292 { "nav-right", ACTIONID_NAV_RIGHT, },
293 { "nav-up", ACTIONID_NAV_UP, },
294 { "next", ACTIONID_NEXT, },
295 { "pause", ACTIONID_PAUSE, },
296 { "play", ACTIONID_PLAY, },
297 { "play-bookmark1", ACTIONID_PLAY_BOOKMARK1, },
298 { "play-bookmark10", ACTIONID_PLAY_BOOKMARK10, },
299 { "play-bookmark2", ACTIONID_PLAY_BOOKMARK2, },
300 { "play-bookmark3", ACTIONID_PLAY_BOOKMARK3, },
301 { "play-bookmark4", ACTIONID_PLAY_BOOKMARK4, },
302 { "play-bookmark5", ACTIONID_PLAY_BOOKMARK5, },
303 { "play-bookmark6", ACTIONID_PLAY_BOOKMARK6, },
304 { "play-bookmark7", ACTIONID_PLAY_BOOKMARK7, },
305 { "play-bookmark8", ACTIONID_PLAY_BOOKMARK8, },
306 { "play-bookmark9", ACTIONID_PLAY_BOOKMARK9, },
307 { "play-pause", ACTIONID_PLAY_PAUSE, },
308 { "position", ACTIONID_POSITION, },
309 { "prev", ACTIONID_PREV, },
310 { "quit", ACTIONID_QUIT, },
311 { "random", ACTIONID_RANDOM, },
312 { "rate-faster-fine", ACTIONID_RATE_FASTER_FINE, },
313 { "rate-normal", ACTIONID_RATE_NORMAL, },
314 { "rate-slower-fine", ACTIONID_RATE_SLOWER_FINE, },
315 { "record", ACTIONID_RECORD, },
316 { "set-bookmark1", ACTIONID_SET_BOOKMARK1, },
317 { "set-bookmark10", ACTIONID_SET_BOOKMARK10, },
318 { "set-bookmark2", ACTIONID_SET_BOOKMARK2, },
319 { "set-bookmark3", ACTIONID_SET_BOOKMARK3, },
320 { "set-bookmark4", ACTIONID_SET_BOOKMARK4, },
321 { "set-bookmark5", ACTIONID_SET_BOOKMARK5, },
322 { "set-bookmark6", ACTIONID_SET_BOOKMARK6, },
323 { "set-bookmark7", ACTIONID_SET_BOOKMARK7, },
324 { "set-bookmark8", ACTIONID_SET_BOOKMARK8, },
325 { "set-bookmark9", ACTIONID_SET_BOOKMARK9, },
326 { "slower", ACTIONID_SLOWER, },
327 { "snapshot", ACTIONID_SNAPSHOT, },
328 { "stop", ACTIONID_STOP, },
329 { "subdelay-down", ACTIONID_SUBDELAY_DOWN, },
330 { "subdelay-up", ACTIONID_SUBDELAY_UP, },
331 { "subpos-down", ACTIONID_SUBPOS_DOWN, },
332 { "subpos-up", ACTIONID_SUBPOS_UP, },
333 { "subtitle-track", ACTIONID_SUBTITLE_TRACK, },
334 { "title-next", ACTIONID_TITLE_NEXT, },
335 { "title-prev", ACTIONID_TITLE_PREV, },
336 { "toggle-autoscale", ACTIONID_TOGGLE_AUTOSCALE, },
337 { "toggle-fullscreen", ACTIONID_TOGGLE_FULLSCREEN, },
338 { "uncrop-bottom", ACTIONID_UNCROP_BOTTOM, },
339 { "uncrop-left", ACTIONID_UNCROP_LEFT, },
340 { "uncrop-right", ACTIONID_UNCROP_RIGHT, },
341 { "uncrop-top", ACTIONID_UNCROP_TOP, },
342 { "unzoom", ACTIONID_UNZOOM, },
343 { "vol-down", ACTIONID_VOL_DOWN, },
344 { "vol-mute", ACTIONID_VOL_MUTE, },
345 { "vol-up", ACTIONID_VOL_UP, },
346 { "wallpaper", ACTIONID_WALLPAPER, },
347 { "zoom", ACTIONID_ZOOM, },
348 { "zoom-double", ACTIONID_ZOOM_DOUBLE, },
349 { "zoom-half", ACTIONID_ZOOM_HALF, },
350 { "zoom-original", ACTIONID_ZOOM_ORIGINAL, },
351 { "zoom-quarter", ACTIONID_ZOOM_QUARTER, },
353 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
357 uint32_t key; ///< Key code
358 vlc_action_t action; ///< Action ID
361 static int keycmp (const void *a, const void *b)
363 const struct mapping *ka = a, *kb = b;
365 #if (INT_MAX >= 0x7fffffff)
366 return ka->key - kb->key;
368 return (ka->key < kb->key) ? -1 : (ka->key > kb->key) ? +1 : 0;
374 void *map; /* Key map */
375 void *global_map; /* Grabbed/global key map */
376 struct hotkey keys[0];
379 static int vlc_key_to_action (vlc_object_t *obj, const char *varname,
380 vlc_value_t prevkey, vlc_value_t curkey, void *d)
382 void *const *map = d;
383 const struct mapping **pent;
384 uint32_t keycode = curkey.i_int;
386 pent = tfind (&keycode, map, keycmp);
392 return var_SetInteger (obj, "key-action", (*pent)->action);
396 * Sets up all key mappings for a given action.
397 * \param map tree (of struct mapping entries) to write mappings to
398 * \param confname VLC configuration item to read mappings from
399 * \param action action ID
401 static void vlc_MapAction (vlc_object_t *obj, void **map,
402 const char *confname, vlc_action_t action)
404 char *keys = var_InheritString (obj, confname);
408 for (char *buf, *key = strtok_r (keys, "\t", &buf);
410 key = strtok_r (NULL, "\t", &buf))
412 uint32_t code = vlc_str2keycode (key);
413 if (code == KEY_UNSET)
415 msg_Warn (obj, "Key \"%s\" unrecognized", key);
419 struct mapping *entry = malloc (sizeof (*entry));
423 entry->action = action;
425 struct mapping **pent = tsearch (entry, map, keycmp);
426 if (unlikely(pent == NULL))
431 msg_Warn (obj, "Key \"%s\" bound to multiple actions", key);
439 * Initializes the key map from configuration.
441 struct vlc_actions *vlc_InitActions (libvlc_int_t *libvlc)
443 vlc_object_t *obj = VLC_OBJECT(libvlc);
445 struct vlc_actions *as = malloc (sizeof (*as) + (ACTIONS_COUNT + 1) * sizeof (*keys));
447 if (unlikely(as == NULL))
450 as->global_map = NULL;
453 var_Create (obj, "key-pressed", VLC_VAR_INTEGER);
454 var_Create (obj, "global-key-pressed", VLC_VAR_INTEGER);
455 var_Create (obj, "key-action", VLC_VAR_INTEGER);
457 /* Initialize from configuration */
458 for (size_t i = 0; i < ACTIONS_COUNT; i++)
462 && strcmp (actions[i-1].name, actions[i].name) >= 0)
464 msg_Err (libvlc, "key-%s and key-%s are not ordered properly",
465 actions[i-1].name, actions[i].name);
469 keys->psz_action = actions[i].name;
472 char name[12 + MAXACTION];
474 snprintf (name, sizeof (name), "global-key-%s", actions[i].name);
475 vlc_MapAction (obj, &as->map, name + 7, actions[i].value);
476 vlc_MapAction (obj, &as->global_map, name, actions[i].value);
479 keys->psz_action = NULL;
481 libvlc->p_hotkeys = as->keys;
482 var_AddCallback (obj, "key-pressed", vlc_key_to_action, &as->map);
483 var_AddCallback (obj, "global-key-pressed", vlc_key_to_action,
489 * Destroys the key map.
491 void vlc_DeinitActions (libvlc_int_t *libvlc, struct vlc_actions *as)
493 if (unlikely(as == NULL))
496 var_DelCallback (libvlc, "global-key-pressed", vlc_key_to_action,
498 var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, &as->map);
500 tdestroy (as->global_map, free);
501 tdestroy (as->map, free);
503 libvlc->p_hotkeys = NULL;
507 static int actcmp(const void *key, const void *ent)
509 const struct action *act = ent;
510 return strcmp(key, act->name);
514 * Get the action ID from the action name in the configuration subsystem.
515 * @return the action ID or ACTIONID_NONE on error.
517 vlc_action_t vlc_GetActionId (const char *name)
519 const struct action *act;
521 if (strncmp (name, "key-", 4))
522 return ACTIONID_NONE;
525 act = bsearch(name, actions, ACTIONS_COUNT, sizeof(*act), actcmp);
526 return (act != NULL) ? act->value : ACTIONID_NONE;