]> git.sesse.net Git - vlc/blob - src/config/keys.c
Fix incorrect return value and leak
[vlc] / src / config / keys.c
1 /*****************************************************************************
2  * keys.c: keys configuration
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  *
6  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 /**
28  * \file
29  * This file defines functions and structures for hotkey handling in vlc
30  */
31
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif
35
36 #include <stdlib.h>
37 #include <limits.h>
38 #include <search.h>
39
40 #include <vlc_common.h>
41 #include <vlc_keys.h>
42 #include "configuration.h"
43 #include "libvlc.h"
44
45 typedef struct key_descriptor_s
46 {
47     const char psz_key_string[20];
48     uint32_t i_key_code;
49 } key_descriptor_t;
50
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            },
62     { "Down",              KEY_DOWN              },
63     { "End",               KEY_END               },
64     { "Enter",             KEY_ENTER             },
65     { "Esc",               KEY_ESC               },
66     { "F1",                KEY_F1                },
67     { "F10",               KEY_F10               },
68     { "F11",               KEY_F11               },
69     { "F12",               KEY_F12               },
70     { "F2",                KEY_F2                },
71     { "F3",                KEY_F3                },
72     { "F4",                KEY_F4                },
73     { "F5",                KEY_F5                },
74     { "F6",                KEY_F6                },
75     { "F7",                KEY_F7                },
76     { "F8",                KEY_F8                },
77     { "F9",                KEY_F9                },
78     { "Home",              KEY_HOME              },
79     { "Insert",            KEY_INSERT            },
80     { "Left",              KEY_LEFT              },
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        },
85     { "Menu",              KEY_MENU              },
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             },
93     { "Space",             ' '                   },
94     { "Tab",               KEY_TAB               },
95     { "Unset",             KEY_UNSET             },
96     { "Up",                KEY_UP                },
97     { "Volume Mute",       KEY_VOLUME_MUTE       },
98     { "Volume Down",       KEY_VOLUME_DOWN       },
99     { "Volume Up",         KEY_VOLUME_UP         },
100 };
101 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
102
103 static int keystrcmp (const void *key, const void *elem)
104 {
105     const char *sa = key, *sb = elem;
106     return strcmp (sa, sb);
107 }
108
109 /* Convert Unicode code point to UTF-8 */
110 static char *utf8_cp (uint_fast32_t cp, char *buf)
111 {
112     if (cp < (1 << 7))
113     {
114         buf[1] = 0;
115         buf[0] = cp;
116     }
117     else if (cp < (1 << 11))
118     {
119         buf[2] = 0;
120         buf[1] = 0x80 | (cp & 0x3F);
121         cp >>= 6;
122         buf[0] = 0xC0 | cp;
123     }
124     else if (cp < (1 << 16))
125     {
126         buf[3] = 0;
127         buf[2] = 0x80 | (cp & 0x3F);
128         cp >>= 6;
129         buf[1] = 0x80 | (cp & 0x3F);
130         cp >>= 6;
131         buf[0] = 0xE0 | cp;
132     }
133     else if (cp < (1 << 21))
134     {
135         buf[4] = 0;
136         buf[3] = 0x80 | (cp & 0x3F);
137         cp >>= 6;
138         buf[2] = 0x80 | (cp & 0x3F);
139         cp >>= 6;
140         buf[1] = 0x80 | (cp & 0x3F);
141         cp >>= 6;
142         buf[0] = 0xE0 | cp;
143     }
144     else
145         return NULL;
146     return buf;
147 }
148
149 /**
150  * Parse a human-readable string representation of a VLC key code.
151  * @return a VLC key code, or KEY_UNSET on failure.
152  */
153 static
154 uint_fast32_t vlc_str2keycode (const char *name)
155 {
156     uint_fast32_t mods = 0;
157     uint32_t code;
158
159     for (;;)
160     {
161         size_t len = strcspn (name, "-+");
162         if (len == 0 || name[len] == '\0')
163             break;
164
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;
175
176         name += len + 1;
177     }
178
179     key_descriptor_t *d = bsearch (name, vlc_keys, KEYS_COUNT,
180                                    sizeof (vlc_keys[0]), keystrcmp);
181     if (d != NULL)
182         code = d->i_key_code;
183     else
184     if (vlc_towc (name, &code) <= 0)
185         code = KEY_UNSET;
186
187     if (code != KEY_UNSET)
188         code |= mods;
189     return code;
190 }
191
192 /**
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.
196  */
197 char *vlc_keycode2str (uint_fast32_t code)
198 {
199     const char *name;
200     char *str, buf[5];
201     uintptr_t key = code & ~KEY_MODIFIER;
202
203     for (size_t i = 0; i < KEYS_COUNT; i++)
204         if (vlc_keys[i].i_key_code == key)
205         {
206             name = vlc_keys[i].psz_key_string;
207             goto found;
208         }
209
210     if (utf8_cp (key, buf) == NULL)
211         return NULL;
212     name = buf;
213
214 found:
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)
221         return NULL;
222     return str;
223 }
224
225
226 /*** VLC key map ***/
227
228 struct action
229 {
230     char name[24];
231     int  value;
232 };
233
234 static const struct action actions[] =
235 {
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, },
337 };
338
339 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
340
341 struct mapping
342 {
343     uint32_t  key; ///< Key code
344     vlc_key_t action; ///< Action ID
345 };
346
347 static int keycmp (const void *a, const void *b)
348 {
349     const struct mapping *ka = a, *kb = b;
350
351 #if (INT_MAX >= 0x7fffffff)
352     return ka->key - kb->key;
353 #else
354     return (ka->key < kb->key) ? -1 : (ka->key > kb->key) ? +1 : 0;
355 #endif
356 }
357
358 struct vlc_actions
359 {
360     void *map; /* Key map */
361     struct hotkey keys[0];
362 };
363
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)
366 {
367     const struct vlc_actions *as = d;
368     const struct mapping **pent;
369     uint32_t keycode = curkey.i_int;
370
371     pent = tfind (&keycode, &as->map, keycmp);
372     if (pent == NULL)
373         return VLC_SUCCESS;
374
375     (void) varname;
376     (void) prevkey;
377     return var_SetInteger (libvlc, "key-action", (*pent)->action);
378 }
379
380
381 /**
382  * Initializes the key map from configuration.
383  */
384 struct vlc_actions *vlc_InitActions (libvlc_int_t *libvlc)
385 {
386     vlc_object_t *obj = VLC_OBJECT(libvlc);
387     struct hotkey *keys;
388     struct vlc_actions *as = malloc (sizeof (*as) + (ACTIONS_COUNT + 1) * sizeof (*keys));
389
390     if (unlikely(as == NULL))
391         return NULL;
392     as->map = NULL;
393     keys = as->keys;
394
395     var_Create (libvlc, "key-pressed", VLC_VAR_INTEGER);
396     var_Create (libvlc, "key-action", VLC_VAR_INTEGER);
397
398     /* Initialize from configuration */
399     for (size_t i = 0; i < ACTIONS_COUNT; i++)
400     {
401 #ifndef NDEBUG
402         if (i > 0
403          && strcmp (actions[i-1].name, actions[i].name) >= 0)
404         {
405             msg_Err (libvlc, "%s and %s are not ordered properly",
406                      actions[i-1].name, actions[i].name);
407             abort ();
408         }
409 #endif
410         keys->psz_action = actions[i].name;
411         keys->i_action = actions[i].value;
412         keys++;
413
414         char *str = var_InheritString (obj, actions[i].name);
415         if (str == NULL)
416             continue;
417
418         for (char *buf, *key = strtok_r (str, "\t", &buf);
419              key != NULL;
420              key = strtok_r (NULL, "\t", &buf))
421         {
422             uint32_t code = vlc_str2keycode (key);
423
424             if (code == KEY_UNSET)
425             {
426                 msg_Warn (obj, "Key \"%s\" unrecognized", key);
427                 continue;
428             }
429
430             struct mapping *entry = malloc (sizeof (*entry));
431             if (entry == NULL)
432                 continue;
433             entry->key = code;
434             entry->action = actions[i].value;
435
436             struct mapping **pent = tsearch (entry, &as->map, keycmp);
437             if (unlikely(pent == NULL))
438                 continue;
439             if (*pent != entry)
440             {
441                 free (entry);
442                 msg_Warn (obj, "Key \"%s\" bound to multiple actions", key);
443             }
444         }
445         free (str);
446     }
447
448     keys->psz_action = NULL;
449     keys->i_action = 0;
450
451     libvlc->p_hotkeys = as->keys;
452     var_AddCallback (libvlc, "key-pressed", vlc_key_to_action, as);
453     return as;
454 }
455
456 /**
457  * Destroys the key map.
458  */
459 void vlc_DeinitActions (libvlc_int_t *libvlc, struct vlc_actions *as)
460 {
461     if (unlikely(as == NULL))
462         return;
463
464     var_DelCallback (libvlc, "key-pressed", vlc_key_to_action, as);
465     tdestroy (as->map, free);
466     free (as);
467     libvlc->p_hotkeys = NULL;
468 }
469
470
471 static int actcmp(const void *key, const void *ent)
472 {
473     const struct action *act = ent;
474     return strcmp(key, act->name);
475 }
476
477 /**
478  * Get the action ID from the action name in the configuration subsystem.
479  * @return the action ID or ACTIONID_NONE on error.
480  */
481 vlc_key_t vlc_GetActionId(const char *name)
482 {
483     const struct action *act;
484
485     act = bsearch(name, actions, ACTIONS_COUNT, sizeof(*act), actcmp);
486     return (act != NULL) ? act->value : ACTIONID_NONE;
487 }