EGL: destroy the created EGLSurface on Close()
[vlc.git] / src / config / keys.c
blob7fcbc378a174d544dd247e8dba4191d6bcf7aaae
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 *****************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 /**
28 * \file
29 * This file defines functions and structures for hotkey handling in vlc
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif
36 #include <stdlib.h>
37 #include <limits.h>
38 #ifdef HAVE_SEARCH_H
39 # include <search.h>
40 #endif
41 #include <errno.h>
43 #include <vlc_common.h>
44 #include <vlc_keys.h>
45 #include "configuration.h"
46 #include "libvlc.h"
48 typedef struct key_descriptor_s
50 const char psz_key_string[20];
51 uint32_t i_key_code;
52 } key_descriptor_t;
54 static const struct key_descriptor_s vlc_keys[] =
55 { /* Alphabetical order */
56 { N_("Backspace"), KEY_BACKSPACE },
57 { N_("Brightness Down"), KEY_BRIGHTNESS_DOWN },
58 { N_("Brightness Up"), KEY_BRIGHTNESS_UP },
59 { N_("Browser Back"), KEY_BROWSER_BACK },
60 { N_("Browser Favorites"), KEY_BROWSER_FAVORITES },
61 { N_("Browser Forward"), KEY_BROWSER_FORWARD },
62 { N_("Browser Home"), KEY_BROWSER_HOME },
63 { N_("Browser Refresh"), KEY_BROWSER_REFRESH },
64 { N_("Browser Search"), KEY_BROWSER_SEARCH },
65 { N_("Browser Stop"), KEY_BROWSER_STOP },
66 { N_("Delete"), KEY_DELETE },
67 { N_("Down"), KEY_DOWN },
68 { N_("End"), KEY_END },
69 { N_("Enter"), KEY_ENTER },
70 { N_("Esc"), KEY_ESC },
71 { N_("F1"), KEY_F1 },
72 { N_("F10"), KEY_F10 },
73 { N_("F11"), KEY_F11 },
74 { N_("F12"), KEY_F12 },
75 { N_("F2"), KEY_F2 },
76 { N_("F3"), KEY_F3 },
77 { N_("F4"), KEY_F4 },
78 { N_("F5"), KEY_F5 },
79 { N_("F6"), KEY_F6 },
80 { N_("F7"), KEY_F7 },
81 { N_("F8"), KEY_F8 },
82 { N_("F9"), KEY_F9 },
83 { N_("Home"), KEY_HOME },
84 { N_("Insert"), KEY_INSERT },
85 { N_("Left"), KEY_LEFT },
86 { N_("Media Angle"), KEY_MEDIA_ANGLE },
87 { N_("Media Audio Track"), KEY_MEDIA_AUDIO },
88 { N_("Media Forward"), KEY_MEDIA_FORWARD },
89 { N_("Media Menu"), KEY_MEDIA_MENU },
90 { N_("Media Next Frame"), KEY_MEDIA_FRAME_NEXT },
91 { N_("Media Next Track"), KEY_MEDIA_NEXT_TRACK },
92 { N_("Media Play Pause"), KEY_MEDIA_PLAY_PAUSE },
93 { N_("Media Prev Frame"), KEY_MEDIA_FRAME_PREV },
94 { N_("Media Prev Track"), KEY_MEDIA_PREV_TRACK },
95 { N_("Media Record"), KEY_MEDIA_RECORD },
96 { N_("Media Repeat"), KEY_MEDIA_REPEAT },
97 { N_("Media Rewind"), KEY_MEDIA_REWIND },
98 { N_("Media Select"), KEY_MEDIA_SELECT },
99 { N_("Media Shuffle"), KEY_MEDIA_SHUFFLE },
100 { N_("Media Stop"), KEY_MEDIA_STOP },
101 { N_("Media Subtitle"), KEY_MEDIA_SUBTITLE },
102 { N_("Media Time"), KEY_MEDIA_TIME },
103 { N_("Media View"), KEY_MEDIA_VIEW },
104 { N_("Menu"), KEY_MENU },
105 { N_("Mouse Wheel Down"), KEY_MOUSEWHEELDOWN },
106 { N_("Mouse Wheel Left"), KEY_MOUSEWHEELLEFT },
107 { N_("Mouse Wheel Right"), KEY_MOUSEWHEELRIGHT },
108 { N_("Mouse Wheel Up"), KEY_MOUSEWHEELUP },
109 { N_("Page Down"), KEY_PAGEDOWN },
110 { N_("Page Up"), KEY_PAGEUP },
111 { N_("Right"), KEY_RIGHT },
112 { N_("Space"), ' ' },
113 { N_("Tab"), KEY_TAB },
114 { N_("Unset"), KEY_UNSET },
115 { N_("Up"), KEY_UP },
116 { N_("Volume Down"), KEY_VOLUME_DOWN },
117 { N_("Volume Mute"), KEY_VOLUME_MUTE },
118 { N_("Volume Up"), KEY_VOLUME_UP },
119 { N_("Zoom In"), KEY_ZOOM_IN },
120 { N_("Zoom Out"), KEY_ZOOM_OUT },
122 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
124 static int keystrcmp (const void *key, const void *elem)
126 const char *sa = key, *sb = elem;
127 return strcmp (sa, sb);
130 /* Convert Unicode code point to UTF-8 */
131 static char *utf8_cp (uint_fast32_t cp, char *buf)
133 if (cp < (1 << 7))
135 buf[1] = 0;
136 buf[0] = cp;
138 else if (cp < (1 << 11))
140 buf[2] = 0;
141 buf[1] = 0x80 | (cp & 0x3F);
142 cp >>= 6;
143 buf[0] = 0xC0 | cp;
145 else if (cp < (1 << 16))
147 buf[3] = 0;
148 buf[2] = 0x80 | (cp & 0x3F);
149 cp >>= 6;
150 buf[1] = 0x80 | (cp & 0x3F);
151 cp >>= 6;
152 buf[0] = 0xE0 | cp;
154 else if (cp < (1 << 21))
156 buf[4] = 0;
157 buf[3] = 0x80 | (cp & 0x3F);
158 cp >>= 6;
159 buf[2] = 0x80 | (cp & 0x3F);
160 cp >>= 6;
161 buf[1] = 0x80 | (cp & 0x3F);
162 cp >>= 6;
163 buf[0] = 0xE0 | cp;
165 else
166 return NULL;
167 return buf;
171 * Parse a human-readable string representation of a VLC key code.
172 * @note This only works with the American English representation
173 * (a.k.a. C or POSIX), not with the local representation returned from
174 * vlc_keycode2str().
175 * @return a VLC key code, or KEY_UNSET on failure.
177 uint_fast32_t vlc_str2keycode (const char *name)
179 uint_fast32_t mods = 0;
180 uint32_t code;
182 for (;;)
184 size_t len = strcspn (name, "-+");
185 if (len == 0 || name[len] == '\0')
186 break;
188 if (len == 4 && !strncasecmp (name, "Ctrl", 4))
189 mods |= KEY_MODIFIER_CTRL;
190 if (len == 3 && !strncasecmp (name, "Alt", 3))
191 mods |= KEY_MODIFIER_ALT;
192 if (len == 5 && !strncasecmp (name, "Shift", 5))
193 mods |= KEY_MODIFIER_SHIFT;
194 if (len == 4 && !strncasecmp (name, "Meta", 4))
195 mods |= KEY_MODIFIER_META;
196 if (len == 7 && !strncasecmp (name, "Command", 7))
197 mods |= KEY_MODIFIER_COMMAND;
199 name += len + 1;
202 key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
203 sizeof (vlc_keys[0]), keystrcmp);
204 if (d != NULL)
205 code = d->i_key_code;
206 else
207 if (vlc_towc (name, &code) <= 0)
208 code = KEY_UNSET;
210 if (code != KEY_UNSET)
211 code |= mods;
212 return code;
215 static char *nooptext (const char *txt)
217 return (char *)txt;
221 * Format a human-readable and unique representation of a VLC key code
222 * (including modifiers).
223 * @param code key code to translate to a string
224 * @param locale true to get a localized string,
225 * false to get a C string suitable for 'vlcrc'
226 * @return a heap-allocated string, or NULL on error.
228 char *vlc_keycode2str (uint_fast32_t code, bool locale)
230 char *(*tr) (const char *) = locale ? vlc_gettext : nooptext;
231 const char *name;
232 char *str, buf[5];
233 uintptr_t key = code & ~KEY_MODIFIER;
235 for (size_t i = 0; i < KEYS_COUNT; i++)
236 if (vlc_keys[i].i_key_code == key)
238 name = vlc_keys[i].psz_key_string;
239 goto found;
242 if (utf8_cp (key, buf) == NULL)
243 return NULL;
244 name = buf;
246 found:
247 if (asprintf (&str, "%s%s%s%s%s%s",
248 (code & KEY_MODIFIER_CTRL) ? tr(N_("Ctrl+")) : "",
249 (code & KEY_MODIFIER_ALT) ? tr(N_("Alt+")) : "",
250 (code & KEY_MODIFIER_SHIFT) ? tr(N_("Shift+")) : "",
251 (code & KEY_MODIFIER_META) ? tr(N_("Meta+")) : "",
252 (code & KEY_MODIFIER_COMMAND) ? tr(N_("Command+")) : "",
253 tr(name)) == -1)
254 return NULL;
255 return str;
259 /*** VLC key map ***/
261 #define MAXACTION 20
262 struct action
264 char name[MAXACTION];
265 vlc_action_t value;
268 static const struct action actions[] =
270 /* *MUST* be sorted (ASCII order) */
271 { "aspect-ratio", ACTIONID_ASPECT_RATIO, },
272 { "audio-track", ACTIONID_AUDIO_TRACK, },
273 { "audiodelay-down", ACTIONID_AUDIODELAY_DOWN, },
274 { "audiodelay-up", ACTIONID_AUDIODELAY_UP, },
275 { "audiodevice-cycle", ACTIONID_AUDIODEVICE_CYCLE, },
276 { "chapter-next", ACTIONID_CHAPTER_NEXT, },
277 { "chapter-prev", ACTIONID_CHAPTER_PREV, },
278 { "clear-playlist", ACTIONID_PLAY_CLEAR, },
279 { "crop", ACTIONID_CROP, },
280 { "crop-bottom", ACTIONID_CROP_BOTTOM, },
281 { "crop-left", ACTIONID_CROP_LEFT, },
282 { "crop-right", ACTIONID_CROP_RIGHT, },
283 { "crop-top", ACTIONID_CROP_TOP, },
284 { "decr-scalefactor", ACTIONID_SCALE_DOWN, },
285 { "deinterlace", ACTIONID_DEINTERLACE, },
286 { "deinterlace-mode", ACTIONID_DEINTERLACE_MODE, },
287 { "disc-menu", ACTIONID_DISC_MENU, },
288 { "faster", ACTIONID_FASTER, },
289 { "frame-next", ACTIONID_FRAME_NEXT, },
290 { "incr-scalefactor", ACTIONID_SCALE_UP, },
291 { "intf-boss", ACTIONID_INTF_BOSS, },
292 { "intf-popup-menu", ACTIONID_INTF_POPUP_MENU, },
293 { "intf-show", ACTIONID_INTF_TOGGLE_FSC, },
294 { "jump+extrashort", ACTIONID_JUMP_FORWARD_EXTRASHORT, },
295 { "jump+long", ACTIONID_JUMP_FORWARD_LONG, },
296 { "jump+medium", ACTIONID_JUMP_FORWARD_MEDIUM, },
297 { "jump+short", ACTIONID_JUMP_FORWARD_SHORT, },
298 { "jump-extrashort", ACTIONID_JUMP_BACKWARD_EXTRASHORT, },
299 { "jump-long", ACTIONID_JUMP_BACKWARD_LONG, },
300 { "jump-medium", ACTIONID_JUMP_BACKWARD_MEDIUM, },
301 { "jump-short", ACTIONID_JUMP_BACKWARD_SHORT, },
302 { "leave-fullscreen", ACTIONID_LEAVE_FULLSCREEN, },
303 { "loop", ACTIONID_LOOP, },
304 { "nav-activate", ACTIONID_NAV_ACTIVATE, },
305 { "nav-down", ACTIONID_NAV_DOWN, },
306 { "nav-left", ACTIONID_NAV_LEFT, },
307 { "nav-right", ACTIONID_NAV_RIGHT, },
308 { "nav-up", ACTIONID_NAV_UP, },
309 { "next", ACTIONID_NEXT, },
310 { "pause", ACTIONID_PAUSE, },
311 { "play", ACTIONID_PLAY, },
312 { "play-bookmark1", ACTIONID_PLAY_BOOKMARK1, },
313 { "play-bookmark10", ACTIONID_PLAY_BOOKMARK10, },
314 { "play-bookmark2", ACTIONID_PLAY_BOOKMARK2, },
315 { "play-bookmark3", ACTIONID_PLAY_BOOKMARK3, },
316 { "play-bookmark4", ACTIONID_PLAY_BOOKMARK4, },
317 { "play-bookmark5", ACTIONID_PLAY_BOOKMARK5, },
318 { "play-bookmark6", ACTIONID_PLAY_BOOKMARK6, },
319 { "play-bookmark7", ACTIONID_PLAY_BOOKMARK7, },
320 { "play-bookmark8", ACTIONID_PLAY_BOOKMARK8, },
321 { "play-bookmark9", ACTIONID_PLAY_BOOKMARK9, },
322 { "play-pause", ACTIONID_PLAY_PAUSE, },
323 { "position", ACTIONID_POSITION, },
324 { "prev", ACTIONID_PREV, },
325 { "program-sid-next", ACTIONID_PROGRAM_SID_NEXT, },
326 { "program-sid-prev", ACTIONID_PROGRAM_SID_PREV, },
327 { "quit", ACTIONID_QUIT, },
328 { "random", ACTIONID_RANDOM, },
329 { "rate-faster-fine", ACTIONID_RATE_FASTER_FINE, },
330 { "rate-normal", ACTIONID_RATE_NORMAL, },
331 { "rate-slower-fine", ACTIONID_RATE_SLOWER_FINE, },
332 { "record", ACTIONID_RECORD, },
333 { "set-bookmark1", ACTIONID_SET_BOOKMARK1, },
334 { "set-bookmark10", ACTIONID_SET_BOOKMARK10, },
335 { "set-bookmark2", ACTIONID_SET_BOOKMARK2, },
336 { "set-bookmark3", ACTIONID_SET_BOOKMARK3, },
337 { "set-bookmark4", ACTIONID_SET_BOOKMARK4, },
338 { "set-bookmark5", ACTIONID_SET_BOOKMARK5, },
339 { "set-bookmark6", ACTIONID_SET_BOOKMARK6, },
340 { "set-bookmark7", ACTIONID_SET_BOOKMARK7, },
341 { "set-bookmark8", ACTIONID_SET_BOOKMARK8, },
342 { "set-bookmark9", ACTIONID_SET_BOOKMARK9, },
343 { "slower", ACTIONID_SLOWER, },
344 { "snapshot", ACTIONID_SNAPSHOT, },
345 { "stop", ACTIONID_STOP, },
346 { "subdelay-down", ACTIONID_SUBDELAY_DOWN, },
347 { "subdelay-up", ACTIONID_SUBDELAY_UP, },
348 { "subpos-down", ACTIONID_SUBPOS_DOWN, },
349 { "subpos-up", ACTIONID_SUBPOS_UP, },
350 { "subsync-apply", ACTIONID_SUBSYNC_APPLY, },
351 { "subsync-markaudio", ACTIONID_SUBSYNC_MARKAUDIO, },
352 { "subsync-marksub", ACTIONID_SUBSYNC_MARKSUB, },
353 { "subsync-reset", ACTIONID_SUBSYNC_RESET, },
354 { "subtitle-track", ACTIONID_SUBTITLE_TRACK, },
355 { "title-next", ACTIONID_TITLE_NEXT, },
356 { "title-prev", ACTIONID_TITLE_PREV, },
357 { "toggle-autoscale", ACTIONID_TOGGLE_AUTOSCALE, },
358 { "toggle-fullscreen", ACTIONID_TOGGLE_FULLSCREEN, },
359 { "uncrop-bottom", ACTIONID_UNCROP_BOTTOM, },
360 { "uncrop-left", ACTIONID_UNCROP_LEFT, },
361 { "uncrop-right", ACTIONID_UNCROP_RIGHT, },
362 { "uncrop-top", ACTIONID_UNCROP_TOP, },
363 { "unzoom", ACTIONID_UNZOOM, },
364 { "vol-down", ACTIONID_VOL_DOWN, },
365 { "vol-mute", ACTIONID_VOL_MUTE, },
366 { "vol-up", ACTIONID_VOL_UP, },
367 { "wallpaper", ACTIONID_WALLPAPER, },
368 { "zoom", ACTIONID_ZOOM, },
369 { "zoom-double", ACTIONID_ZOOM_DOUBLE, },
370 { "zoom-half", ACTIONID_ZOOM_HALF, },
371 { "zoom-original", ACTIONID_ZOOM_ORIGINAL, },
372 { "zoom-quarter", ACTIONID_ZOOM_QUARTER, },
374 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
376 struct mapping
378 uint32_t key; ///< Key code
379 vlc_action_t action; ///< Action ID
382 static int keycmp (const void *a, const void *b)
384 const struct mapping *ka = a, *kb = b;
386 #if (INT_MAX >= 0x7fffffff)
387 return ka->key - kb->key;
388 #else
389 return (ka->key < kb->key) ? -1 : (ka->key > kb->key) ? +1 : 0;
390 #endif
393 struct vlc_actions
395 void *map; /* Key map */
396 void *global_map; /* Grabbed/global key map */
397 struct hotkey keys[0];
400 static int vlc_key_to_action (vlc_object_t *obj, const char *varname,
401 vlc_value_t prevkey, vlc_value_t curkey, void *d)
403 void *const *map = d;
404 const struct mapping **pent;
405 uint32_t keycode = curkey.i_int;
407 pent = tfind (&keycode, map, keycmp);
408 if (pent == NULL)
409 return VLC_SUCCESS;
411 (void) varname;
412 (void) prevkey;
413 return var_SetInteger (obj, "key-action", (*pent)->action);
417 * Adds a mapping from a certain key code to a certain action.
419 static int vlc_AddMapping (void **map, uint32_t keycode, vlc_action_t action)
421 struct mapping *entry = malloc (sizeof (*entry));
422 if (entry == NULL)
423 return ENOMEM;
424 entry->key = keycode;
425 entry->action = action;
427 struct mapping **pent = tsearch (entry, map, keycmp);
428 if (unlikely(pent == NULL))
429 return ENOMEM;
430 if (*pent != entry)
432 free (entry);
433 return EEXIST;
435 return 0;
439 * Sets up all key mappings for a given action.
440 * \param map tree (of struct mapping entries) to write mappings to
441 * \param confname VLC configuration item to read mappings from
442 * \param action action ID
444 static void vlc_InitAction (vlc_object_t *obj, void **map,
445 const char *confname, vlc_action_t action)
447 char *keys = var_InheritString (obj, confname);
448 if (keys == NULL)
449 return;
451 for (char *buf, *key = strtok_r (keys, "\t", &buf);
452 key != NULL;
453 key = strtok_r (NULL, "\t", &buf))
455 uint32_t code = vlc_str2keycode (key);
456 if (code == KEY_UNSET)
458 msg_Warn (obj, "Key \"%s\" unrecognized", key);
459 continue;
462 if (vlc_AddMapping (map, code, action) == EEXIST)
463 msg_Warn (obj, "Key \"%s\" bound to multiple actions", key);
465 free (keys);
469 * Initializes the key map from configuration.
471 struct vlc_actions *vlc_InitActions (libvlc_int_t *libvlc)
473 vlc_object_t *obj = VLC_OBJECT(libvlc);
474 struct hotkey *keys;
475 struct vlc_actions *as = malloc (sizeof (*as) + (ACTIONS_COUNT + 1) * sizeof (*keys));
477 if (unlikely(as == NULL))
478 return NULL;
479 as->map = NULL;
480 as->global_map = NULL;
481 keys = as->keys;
483 var_Create (obj, "key-pressed", VLC_VAR_INTEGER);
484 var_Create (obj, "global-key-pressed", VLC_VAR_INTEGER);
485 var_Create (obj, "key-action", VLC_VAR_INTEGER);
487 /* Initialize from configuration */
488 for (size_t i = 0; i < ACTIONS_COUNT; i++)
490 #ifndef NDEBUG
491 if (i > 0
492 && strcmp (actions[i-1].name, actions[i].name) >= 0)
494 msg_Err (libvlc, "key-%s and key-%s are not ordered properly",
495 actions[i-1].name, actions[i].name);
496 abort ();
498 #endif
499 keys->psz_action = actions[i].name;
500 keys++;
502 char name[12 + MAXACTION];
504 snprintf (name, sizeof (name), "global-key-%s", actions[i].name);
505 vlc_InitAction (obj, &as->map, name + 7, actions[i].value);
506 vlc_InitAction (obj, &as->global_map, name, actions[i].value);
508 keys->psz_action = NULL;
510 /* Initialize mouse wheel events */
511 int mousemode = var_InheritInteger (obj, "hotkeys-mousewheel-mode");
512 if (mousemode < 2)
514 vlc_AddMapping (&as->map,
515 mousemode ? KEY_MOUSEWHEELRIGHT : KEY_MOUSEWHEELUP,
516 ACTIONID_VOL_UP);
517 vlc_AddMapping (&as->map,
518 mousemode ? KEY_MOUSEWHEELLEFT : KEY_MOUSEWHEELDOWN,
519 ACTIONID_VOL_DOWN);
520 vlc_AddMapping (&as->map,
521 mousemode ? KEY_MOUSEWHEELUP : KEY_MOUSEWHEELRIGHT,
522 ACTIONID_JUMP_FORWARD_EXTRASHORT);
523 vlc_AddMapping (&as->map,
524 mousemode ? KEY_MOUSEWHEELDOWN : KEY_MOUSEWHEELLEFT,
525 ACTIONID_JUMP_BACKWARD_EXTRASHORT);
529 libvlc->p_hotkeys = as->keys;
530 var_AddCallback (obj, "key-pressed", vlc_key_to_action, &as->map);
531 var_AddCallback (obj, "global-key-pressed", vlc_key_to_action,
532 &as->global_map);
533 return as;
537 * Destroys the key map.
539 void vlc_DeinitActions (libvlc_int_t *libvlc, struct vlc_actions *as)
541 if (unlikely(as == NULL))
542 return;
544 var_DelCallback (libvlc, "global-key-pressed", vlc_key_to_action,
545 &as->global_map);
546 var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, &as->map);
548 tdestroy (as->global_map, free);
549 tdestroy (as->map, free);
550 free (as);
551 libvlc->p_hotkeys = NULL;
555 static int actcmp(const void *key, const void *ent)
557 const struct action *act = ent;
558 return strcmp(key, act->name);
562 * Get the action ID from the action name in the configuration subsystem.
563 * @return the action ID or ACTIONID_NONE on error.
565 vlc_action_t vlc_GetActionId (const char *name)
567 const struct action *act;
569 if (strncmp (name, "key-", 4))
570 return ACTIONID_NONE;
571 name += 4;
573 act = bsearch(name, actions, ACTIONS_COUNT, sizeof(*act), actcmp);
574 return (act != NULL) ? act->value : ACTIONID_NONE;