1 /*****************************************************************************
2 * keys.c: keys configuration
3 *****************************************************************************
4 * Copyright (C) 2003-2009 the VideoLAN team
6 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
29 * This file defines functions and structures for hotkey handling in vlc
40 #include <vlc_common.h>
42 #include "configuration.h"
45 typedef struct key_descriptor_s
47 const char psz_key_string[20];
51 static const struct key_descriptor_s vlc_keys[] =
52 { /* Alphabetical order */
53 { "Backspace", KEY_BACKSPACE },
54 { "Browser Back", KEY_BROWSER_BACK },
55 { "Browser Favorites", KEY_BROWSER_FAVORITES },
56 { "Browser Forward", KEY_BROWSER_FORWARD },
57 { "Browser Home", KEY_BROWSER_HOME },
58 { "Browser Refresh", KEY_BROWSER_REFRESH },
59 { "Browser Search", KEY_BROWSER_SEARCH },
60 { "Browser Stop", KEY_BROWSER_STOP },
61 { "Delete", KEY_DELETE },
64 { "Enter", KEY_ENTER },
79 { "Insert", KEY_INSERT },
81 { "Media Next Track", KEY_MEDIA_NEXT_TRACK },
82 { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE },
83 { "Media Prev Track", KEY_MEDIA_PREV_TRACK },
84 { "Media Stop", KEY_MEDIA_STOP },
86 { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN },
87 { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT },
88 { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT },
89 { "Mouse Wheel Up", KEY_MOUSEWHEELUP },
90 { "Page Down", KEY_PAGEDOWN },
91 { "Page Up", KEY_PAGEUP },
92 { "Right", KEY_RIGHT },
95 { "Unset", KEY_UNSET },
97 { "Volume Mute", KEY_VOLUME_MUTE },
98 { "Volume Down", KEY_VOLUME_DOWN },
99 { "Volume Up", KEY_VOLUME_UP },
101 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
103 static int keystrcmp (const void *key, const void *elem)
105 const char *sa = key, *sb = elem;
106 return strcmp (sa, sb);
109 /* Convert Unicode code point to UTF-8 */
110 static char *utf8_cp (uint_fast32_t cp, char *buf)
117 else if (cp < (1 << 11))
120 buf[1] = 0x80 | (cp & 0x3F);
124 else if (cp < (1 << 16))
127 buf[2] = 0x80 | (cp & 0x3F);
129 buf[1] = 0x80 | (cp & 0x3F);
133 else if (cp < (1 << 21))
136 buf[3] = 0x80 | (cp & 0x3F);
138 buf[2] = 0x80 | (cp & 0x3F);
140 buf[1] = 0x80 | (cp & 0x3F);
150 * Parse a human-readable string representation of a VLC key code.
151 * @return a VLC key code, or KEY_UNSET on failure.
154 uint_fast32_t vlc_str2keycode (const char *name)
156 uint_fast32_t mods = 0;
161 size_t len = strcspn (name, "-+");
162 if (len == 0 || name[len] == '\0')
165 if (len == 4 && !strncasecmp (name, "Ctrl", 4))
166 mods |= KEY_MODIFIER_CTRL;
167 if (len == 3 && !strncasecmp (name, "Alt", 3))
168 mods |= KEY_MODIFIER_ALT;
169 if (len == 5 && !strncasecmp (name, "Shift", 5))
170 mods |= KEY_MODIFIER_SHIFT;
171 if (len == 4 && !strncasecmp (name, "Meta", 4))
172 mods |= KEY_MODIFIER_META;
173 if (len == 7 && !strncasecmp (name, "Command", 7))
174 mods |= KEY_MODIFIER_COMMAND;
179 key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
180 sizeof (vlc_keys[0]), keystrcmp);
182 code = d->i_key_code;
184 if (vlc_towc (name, &code) <= 0)
187 if (code != KEY_UNSET)
193 * Format a human-readable and unique representation of a VLC key code
194 * (including modifiers).
195 * @return a heap-allocated string, or NULL on error.
197 char *vlc_keycode2str (uint_fast32_t code)
201 uintptr_t key = code & ~KEY_MODIFIER;
203 for (size_t i = 0; i < KEYS_COUNT; i++)
204 if (vlc_keys[i].i_key_code == key)
206 name = vlc_keys[i].psz_key_string;
210 if (utf8_cp (key, buf) == NULL)
215 if (asprintf (&str, "%s%s%s%s%s%s",
216 (code & KEY_MODIFIER_CTRL) ? "Ctrl+" : "",
217 (code & KEY_MODIFIER_ALT) ? "Alt+" : "",
218 (code & KEY_MODIFIER_SHIFT) ? "Shift+" : "",
219 (code & KEY_MODIFIER_META) ? "Meta+" : "",
220 (code & KEY_MODIFIER_COMMAND) ? "Command+" : "", name) == -1)
226 /*** VLC key map ***/
234 static const struct action actions[] =
236 /* *MUST* be sorted (ASCII order) */
237 { "key-aspect-ratio", ACTIONID_ASPECT_RATIO, },
238 { "key-audio-track", ACTIONID_AUDIO_TRACK, },
239 { "key-audiodelay-down", ACTIONID_AUDIODELAY_DOWN, },
240 { "key-audiodelay-up", ACTIONID_AUDIODELAY_UP, },
241 { "key-audiodevice-cycle", ACTIONID_AUDIODEVICE_CYCLE, },
242 { "key-chapter-next", ACTIONID_CHAPTER_NEXT, },
243 { "key-chapter-prev", ACTIONID_CHAPTER_PREV, },
244 { "key-crop", ACTIONID_CROP, },
245 { "key-crop-bottom", ACTIONID_CROP_BOTTOM, },
246 { "key-crop-left", ACTIONID_CROP_LEFT, },
247 { "key-crop-right", ACTIONID_CROP_RIGHT, },
248 { "key-crop-top", ACTIONID_CROP_TOP, },
249 { "key-decr-scalefactor", ACTIONID_SCALE_DOWN, },
250 { "key-deinterlace", ACTIONID_DEINTERLACE, },
251 { "key-disc-menu", ACTIONID_DISC_MENU, },
252 { "key-faster", ACTIONID_FASTER, },
253 { "key-frame-next", ACTIONID_FRAME_NEXT, },
254 { "key-incr-scalefactor", ACTIONID_SCALE_UP, },
255 { "key-intf-hide", ACTIONID_INTF_HIDE, },
256 { "key-intf-show", ACTIONID_INTF_SHOW, },
257 { "key-jump+extrashort", ACTIONID_JUMP_FORWARD_EXTRASHORT, },
258 { "key-jump+long", ACTIONID_JUMP_FORWARD_LONG, },
259 { "key-jump+medium", ACTIONID_JUMP_FORWARD_MEDIUM, },
260 { "key-jump+short", ACTIONID_JUMP_FORWARD_SHORT, },
261 { "key-jump-extrashort", ACTIONID_JUMP_BACKWARD_EXTRASHORT, },
262 { "key-jump-long", ACTIONID_JUMP_BACKWARD_LONG, },
263 { "key-jump-medium", ACTIONID_JUMP_BACKWARD_MEDIUM, },
264 { "key-jump-short", ACTIONID_JUMP_BACKWARD_SHORT, },
265 { "key-leave-fullscreen", ACTIONID_LEAVE_FULLSCREEN, },
266 { "key-loop", ACTIONID_LOOP, },
267 { "key-menu-down", ACTIONID_MENU_DOWN, },
268 { "key-menu-left", ACTIONID_MENU_LEFT, },
269 { "key-menu-off", ACTIONID_MENU_OFF, },
270 { "key-menu-on", ACTIONID_MENU_ON, },
271 { "key-menu-right", ACTIONID_MENU_RIGHT, },
272 { "key-menu-select", ACTIONID_MENU_SELECT, },
273 { "key-menu-up", ACTIONID_MENU_UP, },
274 { "key-nav-activate", ACTIONID_NAV_ACTIVATE, },
275 { "key-nav-down", ACTIONID_NAV_DOWN, },
276 { "key-nav-left", ACTIONID_NAV_LEFT, },
277 { "key-nav-right", ACTIONID_NAV_RIGHT, },
278 { "key-nav-up", ACTIONID_NAV_UP, },
279 { "key-next", ACTIONID_NEXT, },
280 { "key-pause", ACTIONID_PAUSE, },
281 { "key-play", ACTIONID_PLAY, },
282 { "key-play-bookmark1", ACTIONID_PLAY_BOOKMARK1, },
283 { "key-play-bookmark10", ACTIONID_PLAY_BOOKMARK10, },
284 { "key-play-bookmark2", ACTIONID_PLAY_BOOKMARK2, },
285 { "key-play-bookmark3", ACTIONID_PLAY_BOOKMARK3, },
286 { "key-play-bookmark4", ACTIONID_PLAY_BOOKMARK4, },
287 { "key-play-bookmark5", ACTIONID_PLAY_BOOKMARK5, },
288 { "key-play-bookmark6", ACTIONID_PLAY_BOOKMARK6, },
289 { "key-play-bookmark7", ACTIONID_PLAY_BOOKMARK7, },
290 { "key-play-bookmark8", ACTIONID_PLAY_BOOKMARK8, },
291 { "key-play-bookmark9", ACTIONID_PLAY_BOOKMARK9, },
292 { "key-play-pause", ACTIONID_PLAY_PAUSE, },
293 { "key-position", ACTIONID_POSITION, },
294 { "key-prev", ACTIONID_PREV, },
295 { "key-quit", ACTIONID_QUIT, },
296 { "key-random", ACTIONID_RANDOM, },
297 { "key-rate-faster-fine", ACTIONID_RATE_FASTER_FINE, },
298 { "key-rate-normal", ACTIONID_RATE_NORMAL, },
299 { "key-rate-slower-fine", ACTIONID_RATE_SLOWER_FINE, },
300 { "key-record", ACTIONID_RECORD, },
301 { "key-set-bookmark1", ACTIONID_SET_BOOKMARK1, },
302 { "key-set-bookmark10", ACTIONID_SET_BOOKMARK10, },
303 { "key-set-bookmark2", ACTIONID_SET_BOOKMARK2, },
304 { "key-set-bookmark3", ACTIONID_SET_BOOKMARK3, },
305 { "key-set-bookmark4", ACTIONID_SET_BOOKMARK4, },
306 { "key-set-bookmark5", ACTIONID_SET_BOOKMARK5, },
307 { "key-set-bookmark6", ACTIONID_SET_BOOKMARK6, },
308 { "key-set-bookmark7", ACTIONID_SET_BOOKMARK7, },
309 { "key-set-bookmark8", ACTIONID_SET_BOOKMARK8, },
310 { "key-set-bookmark9", ACTIONID_SET_BOOKMARK9, },
311 { "key-slower", ACTIONID_SLOWER, },
312 { "key-snapshot", ACTIONID_SNAPSHOT, },
313 { "key-stop", ACTIONID_STOP, },
314 { "key-subdelay-down", ACTIONID_SUBDELAY_DOWN, },
315 { "key-subdelay-up", ACTIONID_SUBDELAY_UP, },
316 { "key-subpos-down", ACTIONID_SUBPOS_DOWN, },
317 { "key-subpos-up", ACTIONID_SUBPOS_UP, },
318 { "key-subtitle-track", ACTIONID_SUBTITLE_TRACK, },
319 { "key-title-next", ACTIONID_TITLE_NEXT, },
320 { "key-title-prev", ACTIONID_TITLE_PREV, },
321 { "key-toggle-autoscale", ACTIONID_TOGGLE_AUTOSCALE, },
322 { "key-toggle-fullscreen", ACTIONID_TOGGLE_FULLSCREEN, },
323 { "key-uncrop-bottom", ACTIONID_UNCROP_BOTTOM, },
324 { "key-uncrop-left", ACTIONID_UNCROP_LEFT, },
325 { "key-uncrop-right", ACTIONID_UNCROP_RIGHT, },
326 { "key-uncrop-top", ACTIONID_UNCROP_TOP, },
327 { "key-unzoom", ACTIONID_UNZOOM, },
328 { "key-vol-down", ACTIONID_VOL_DOWN, },
329 { "key-vol-mute", ACTIONID_VOL_MUTE, },
330 { "key-vol-up", ACTIONID_VOL_UP, },
331 { "key-wallpaper", ACTIONID_WALLPAPER, },
332 { "key-zoom", ACTIONID_ZOOM, },
333 { "key-zoom-double", ACTIONID_ZOOM_DOUBLE, },
334 { "key-zoom-half", ACTIONID_ZOOM_HALF, },
335 { "key-zoom-original", ACTIONID_ZOOM_ORIGINAL, },
336 { "key-zoom-quarter", ACTIONID_ZOOM_QUARTER, },
339 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
343 uint32_t key; ///< Key code
344 vlc_key_t action; ///< Action ID
347 static int keycmp (const void *a, const void *b)
349 const struct mapping *ka = a, *kb = b;
351 #if (INT_MAX >= 0x7fffffff)
352 return ka->key - kb->key;
354 return (ka->key < kb->key) ? -1 : (ka->key > kb->key) ? +1 : 0;
360 void *map; /* Key map */
361 struct hotkey keys[0];
364 static int vlc_key_to_action (vlc_object_t *libvlc, const char *varname,
365 vlc_value_t prevkey, vlc_value_t curkey, void *d)
367 const struct vlc_actions *as = d;
368 const struct mapping **pent;
369 uint32_t keycode = curkey.i_int;
371 pent = tfind (&keycode, &as->map, keycmp);
377 return var_SetInteger (libvlc, "key-action", (*pent)->action);
382 * Initializes the key map from configuration.
384 struct vlc_actions *vlc_InitActions (libvlc_int_t *libvlc)
386 vlc_object_t *obj = VLC_OBJECT(libvlc);
388 struct vlc_actions *as = malloc (sizeof (*as) + (ACTIONS_COUNT + 1) * sizeof (*keys));
390 if (unlikely(as == NULL))
395 var_Create (libvlc, "key-pressed", VLC_VAR_INTEGER);
396 var_Create (libvlc, "key-action", VLC_VAR_INTEGER);
398 /* Initialize from configuration */
399 for (size_t i = 0; i < ACTIONS_COUNT; i++)
403 && strcmp (actions[i-1].name, actions[i].name) >= 0)
405 msg_Err (libvlc, "%s and %s are not ordered properly",
406 actions[i-1].name, actions[i].name);
410 keys->psz_action = actions[i].name;
411 keys->i_action = actions[i].value;
414 char *str = var_InheritString (obj, actions[i].name);
418 for (char *buf, *key = strtok_r (str, "\t", &buf);
420 key = strtok_r (NULL, "\t", &buf))
422 uint32_t code = vlc_str2keycode (key);
424 if (code == KEY_UNSET)
426 msg_Warn (obj, "Key \"%s\" unrecognized", key);
430 struct mapping *entry = malloc (sizeof (*entry));
434 entry->action = actions[i].value;
436 struct mapping **pent = tsearch (entry, &as->map, keycmp);
437 if (unlikely(pent == NULL))
442 msg_Warn (obj, "Key \"%s\" bound to multiple actions", key);
448 keys->psz_action = NULL;
451 libvlc->p_hotkeys = as->keys;
452 var_AddCallback (libvlc, "key-pressed", vlc_key_to_action, as);
457 * Destroys the key map.
459 void vlc_DeinitActions (libvlc_int_t *libvlc, struct vlc_actions *as)
461 if (unlikely(as == NULL))
464 var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, as);
465 tdestroy (as->map, free);
467 libvlc->p_hotkeys = NULL;
471 static int actcmp(const void *key, const void *ent)
473 const struct action *act = ent;
474 return strcmp(key, act->name);
478 * Get the action ID from the action name in the configuration subsystem.
479 * @return the action ID or ACTIONID_NONE on error.
481 vlc_key_t vlc_GetActionId(const char *name)
483 const struct action *act;
485 act = bsearch(name, actions, ACTIONS_COUNT, sizeof(*act), actcmp);
486 return (act != NULL) ? act->value : ACTIONID_NONE;