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
43 #include <vlc_common.h>
45 #include <vlc_charset.h>
46 #include "configuration.h"
49 typedef struct key_descriptor_s
51 const char psz_key_string
[20];
55 static const struct key_descriptor_s vlc_keys
[] =
56 { /* Alphabetical order */
57 { N_("Backspace"), KEY_BACKSPACE
},
58 { N_("Brightness Down"), KEY_BRIGHTNESS_DOWN
},
59 { N_("Brightness Up"), KEY_BRIGHTNESS_UP
},
60 { N_("Browser Back"), KEY_BROWSER_BACK
},
61 { N_("Browser Favorites"), KEY_BROWSER_FAVORITES
},
62 { N_("Browser Forward"), KEY_BROWSER_FORWARD
},
63 { N_("Browser Home"), KEY_BROWSER_HOME
},
64 { N_("Browser Refresh"), KEY_BROWSER_REFRESH
},
65 { N_("Browser Search"), KEY_BROWSER_SEARCH
},
66 { N_("Browser Stop"), KEY_BROWSER_STOP
},
67 { N_("Delete"), KEY_DELETE
},
68 { N_("Down"), KEY_DOWN
},
69 { N_("End"), KEY_END
},
70 { N_("Enter"), KEY_ENTER
},
71 { N_("Esc"), KEY_ESC
},
73 { N_("F10"), KEY_F10
},
74 { N_("F11"), KEY_F11
},
75 { N_("F12"), KEY_F12
},
84 { N_("Home"), KEY_HOME
},
85 { N_("Insert"), KEY_INSERT
},
86 { N_("Left"), KEY_LEFT
},
87 { N_("Media Angle"), KEY_MEDIA_ANGLE
},
88 { N_("Media Audio Track"), KEY_MEDIA_AUDIO
},
89 { N_("Media Forward"), KEY_MEDIA_FORWARD
},
90 { N_("Media Menu"), KEY_MEDIA_MENU
},
91 { N_("Media Next Frame"), KEY_MEDIA_FRAME_NEXT
},
92 { N_("Media Next Track"), KEY_MEDIA_NEXT_TRACK
},
93 { N_("Media Play Pause"), KEY_MEDIA_PLAY_PAUSE
},
94 { N_("Media Prev Frame"), KEY_MEDIA_FRAME_PREV
},
95 { N_("Media Prev Track"), KEY_MEDIA_PREV_TRACK
},
96 { N_("Media Record"), KEY_MEDIA_RECORD
},
97 { N_("Media Repeat"), KEY_MEDIA_REPEAT
},
98 { N_("Media Rewind"), KEY_MEDIA_REWIND
},
99 { N_("Media Select"), KEY_MEDIA_SELECT
},
100 { N_("Media Shuffle"), KEY_MEDIA_SHUFFLE
},
101 { N_("Media Stop"), KEY_MEDIA_STOP
},
102 { N_("Media Subtitle"), KEY_MEDIA_SUBTITLE
},
103 { N_("Media Time"), KEY_MEDIA_TIME
},
104 { N_("Media View"), KEY_MEDIA_VIEW
},
105 { N_("Menu"), KEY_MENU
},
106 { N_("Mouse Wheel Down"), KEY_MOUSEWHEELDOWN
},
107 { N_("Mouse Wheel Left"), KEY_MOUSEWHEELLEFT
},
108 { N_("Mouse Wheel Right"), KEY_MOUSEWHEELRIGHT
},
109 { N_("Mouse Wheel Up"), KEY_MOUSEWHEELUP
},
110 { N_("Page Down"), KEY_PAGEDOWN
},
111 { N_("Page Up"), KEY_PAGEUP
},
112 { N_("Pause"), KEY_PAUSE
},
113 { N_("Print"), KEY_PRINT
},
114 { N_("Right"), KEY_RIGHT
},
115 { N_("Space"), ' ' },
116 { N_("Tab"), KEY_TAB
},
117 { N_("Unset"), KEY_UNSET
},
118 { N_("Up"), KEY_UP
},
119 { N_("Volume Down"), KEY_VOLUME_DOWN
},
120 { N_("Volume Mute"), KEY_VOLUME_MUTE
},
121 { N_("Volume Up"), KEY_VOLUME_UP
},
122 { N_("Zoom In"), KEY_ZOOM_IN
},
123 { N_("Zoom Out"), KEY_ZOOM_OUT
},
125 #define KEYS_COUNT (sizeof(vlc_keys)/sizeof(vlc_keys[0]))
127 static int keystrcmp (const void *key
, const void *elem
)
129 const char *sa
= key
, *sb
= elem
;
130 return strcmp (sa
, sb
);
133 /* Convert Unicode code point to UTF-8 */
134 static char *utf8_cp (uint_fast32_t cp
, char *buf
)
141 else if (cp
< (1 << 11))
144 buf
[1] = 0x80 | (cp
& 0x3F);
148 else if (cp
< (1 << 16))
151 buf
[2] = 0x80 | (cp
& 0x3F);
153 buf
[1] = 0x80 | (cp
& 0x3F);
157 else if (cp
< (1 << 21))
160 buf
[3] = 0x80 | (cp
& 0x3F);
162 buf
[2] = 0x80 | (cp
& 0x3F);
164 buf
[1] = 0x80 | (cp
& 0x3F);
174 * Parse a human-readable string representation of a VLC key code.
175 * @note This only works with the American English representation
176 * (a.k.a. C or POSIX), not with the local representation returned from
178 * @return a VLC key code, or KEY_UNSET on failure.
180 uint_fast32_t vlc_str2keycode (const char *name
)
182 uint_fast32_t mods
= 0;
187 size_t len
= strcspn (name
, "-+");
188 if (len
== 0 || name
[len
] == '\0')
191 if (len
== 4 && !strncasecmp (name
, "Ctrl", 4))
192 mods
|= KEY_MODIFIER_CTRL
;
193 if (len
== 3 && !strncasecmp (name
, "Alt", 3))
194 mods
|= KEY_MODIFIER_ALT
;
195 if (len
== 5 && !strncasecmp (name
, "Shift", 5))
196 mods
|= KEY_MODIFIER_SHIFT
;
197 if (len
== 4 && !strncasecmp (name
, "Meta", 4))
198 mods
|= KEY_MODIFIER_META
;
199 if (len
== 7 && !strncasecmp (name
, "Command", 7))
200 mods
|= KEY_MODIFIER_COMMAND
;
205 key_descriptor_t
*d
= bsearch (name
, vlc_keys
, KEYS_COUNT
,
206 sizeof (vlc_keys
[0]), keystrcmp
);
208 code
= d
->i_key_code
;
210 if (vlc_towc (name
, &code
) <= 0)
213 if (code
!= KEY_UNSET
)
218 static char *nooptext (const char *txt
)
224 * Format a human-readable and unique representation of a VLC key code
225 * (including modifiers).
226 * @param code key code to translate to a string
227 * @param locale true to get a localized string,
228 * false to get a C string suitable for 'vlcrc'
229 * @return a heap-allocated string, or NULL on error.
231 char *vlc_keycode2str (uint_fast32_t code
, bool locale
)
233 char *(*tr
) (const char *) = locale
? vlc_gettext
: nooptext
;
236 uintptr_t key
= code
& ~KEY_MODIFIER
;
238 for (size_t i
= 0; i
< KEYS_COUNT
; i
++)
239 if (vlc_keys
[i
].i_key_code
== key
)
241 name
= vlc_keys
[i
].psz_key_string
;
245 if (utf8_cp (key
, buf
) == NULL
)
250 if (asprintf (&str
, "%s%s%s%s%s%s",
251 (code
& KEY_MODIFIER_CTRL
) ? tr(N_("Ctrl+")) : "",
252 (code
& KEY_MODIFIER_ALT
) ? tr(N_("Alt+")) : "",
253 (code
& KEY_MODIFIER_SHIFT
) ? tr(N_("Shift+")) : "",
254 (code
& KEY_MODIFIER_META
) ? tr(N_("Meta+")) : "",
255 (code
& KEY_MODIFIER_COMMAND
) ? tr(N_("Command+")) : "",
262 /*** VLC key map ***/
267 char name
[MAXACTION
];
271 static const struct action actions
[] =
273 /* *MUST* be sorted (ASCII order) */
274 { "aspect-ratio", ACTIONID_ASPECT_RATIO
, },
275 { "audio-track", ACTIONID_AUDIO_TRACK
, },
276 { "audiodelay-down", ACTIONID_AUDIODELAY_DOWN
, },
277 { "audiodelay-up", ACTIONID_AUDIODELAY_UP
, },
278 { "audiodevice-cycle", ACTIONID_AUDIODEVICE_CYCLE
, },
279 { "chapter-next", ACTIONID_CHAPTER_NEXT
, },
280 { "chapter-prev", ACTIONID_CHAPTER_PREV
, },
281 { "clear-playlist", ACTIONID_PLAY_CLEAR
, },
282 { "crop", ACTIONID_CROP
, },
283 { "crop-bottom", ACTIONID_CROP_BOTTOM
, },
284 { "crop-left", ACTIONID_CROP_LEFT
, },
285 { "crop-right", ACTIONID_CROP_RIGHT
, },
286 { "crop-top", ACTIONID_CROP_TOP
, },
287 { "decr-scalefactor", ACTIONID_SCALE_DOWN
, },
288 { "deinterlace", ACTIONID_DEINTERLACE
, },
289 { "deinterlace-mode", ACTIONID_DEINTERLACE_MODE
, },
290 { "disc-menu", ACTIONID_DISC_MENU
, },
291 { "faster", ACTIONID_FASTER
, },
292 { "frame-next", ACTIONID_FRAME_NEXT
, },
293 { "incr-scalefactor", ACTIONID_SCALE_UP
, },
294 { "intf-boss", ACTIONID_INTF_BOSS
, },
295 { "intf-popup-menu", ACTIONID_INTF_POPUP_MENU
, },
296 { "intf-show", ACTIONID_INTF_TOGGLE_FSC
, },
297 { "jump+extrashort", ACTIONID_JUMP_FORWARD_EXTRASHORT
, },
298 { "jump+long", ACTIONID_JUMP_FORWARD_LONG
, },
299 { "jump+medium", ACTIONID_JUMP_FORWARD_MEDIUM
, },
300 { "jump+short", ACTIONID_JUMP_FORWARD_SHORT
, },
301 { "jump-extrashort", ACTIONID_JUMP_BACKWARD_EXTRASHORT
, },
302 { "jump-long", ACTIONID_JUMP_BACKWARD_LONG
, },
303 { "jump-medium", ACTIONID_JUMP_BACKWARD_MEDIUM
, },
304 { "jump-short", ACTIONID_JUMP_BACKWARD_SHORT
, },
305 { "leave-fullscreen", ACTIONID_LEAVE_FULLSCREEN
, },
306 { "loop", ACTIONID_LOOP
, },
307 { "nav-activate", ACTIONID_NAV_ACTIVATE
, },
308 { "nav-down", ACTIONID_NAV_DOWN
, },
309 { "nav-left", ACTIONID_NAV_LEFT
, },
310 { "nav-right", ACTIONID_NAV_RIGHT
, },
311 { "nav-up", ACTIONID_NAV_UP
, },
312 { "next", ACTIONID_NEXT
, },
313 { "pause", ACTIONID_PAUSE
, },
314 { "play", ACTIONID_PLAY
, },
315 { "play-bookmark1", ACTIONID_PLAY_BOOKMARK1
, },
316 { "play-bookmark10", ACTIONID_PLAY_BOOKMARK10
, },
317 { "play-bookmark2", ACTIONID_PLAY_BOOKMARK2
, },
318 { "play-bookmark3", ACTIONID_PLAY_BOOKMARK3
, },
319 { "play-bookmark4", ACTIONID_PLAY_BOOKMARK4
, },
320 { "play-bookmark5", ACTIONID_PLAY_BOOKMARK5
, },
321 { "play-bookmark6", ACTIONID_PLAY_BOOKMARK6
, },
322 { "play-bookmark7", ACTIONID_PLAY_BOOKMARK7
, },
323 { "play-bookmark8", ACTIONID_PLAY_BOOKMARK8
, },
324 { "play-bookmark9", ACTIONID_PLAY_BOOKMARK9
, },
325 { "play-pause", ACTIONID_PLAY_PAUSE
, },
326 { "position", ACTIONID_POSITION
, },
327 { "prev", ACTIONID_PREV
, },
328 { "program-sid-next", ACTIONID_PROGRAM_SID_NEXT
, },
329 { "program-sid-prev", ACTIONID_PROGRAM_SID_PREV
, },
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 { "subsync-apply", ACTIONID_SUBSYNC_APPLY
, },
354 { "subsync-markaudio", ACTIONID_SUBSYNC_MARKAUDIO
, },
355 { "subsync-marksub", ACTIONID_SUBSYNC_MARKSUB
, },
356 { "subsync-reset", ACTIONID_SUBSYNC_RESET
, },
357 { "subtitle-text-scale-down", ACTIONID_SUBTITLE_TEXT_SCALE_DOWN
, },
358 { "subtitle-text-scale-normal", ACTIONID_SUBTITLE_TEXT_SCALE_NORMAL
, },
359 { "subtitle-text-scale-up", ACTIONID_SUBTITLE_TEXT_SCALE_UP
, },
360 { "subtitle-toggle", ACTIONID_SUBTITLE_TOGGLE
, },
361 { "subtitle-track", ACTIONID_SUBTITLE_TRACK
, },
362 { "title-next", ACTIONID_TITLE_NEXT
, },
363 { "title-prev", ACTIONID_TITLE_PREV
, },
364 { "toggle-autoscale", ACTIONID_TOGGLE_AUTOSCALE
, },
365 { "toggle-fullscreen", ACTIONID_TOGGLE_FULLSCREEN
, },
366 { "uncrop-bottom", ACTIONID_UNCROP_BOTTOM
, },
367 { "uncrop-left", ACTIONID_UNCROP_LEFT
, },
368 { "uncrop-right", ACTIONID_UNCROP_RIGHT
, },
369 { "uncrop-top", ACTIONID_UNCROP_TOP
, },
370 { "unzoom", ACTIONID_UNZOOM
, },
371 { "viewpoint-fov-in", ACTIONID_VIEWPOINT_FOV_IN
, },
372 { "viewpoint-fov-out", ACTIONID_VIEWPOINT_FOV_OUT
, },
373 { "viewpoint-roll-anticlock", ACTIONID_VIEWPOINT_ROLL_ANTICLOCK
, },
374 { "viewpoint-roll-clock", ACTIONID_VIEWPOINT_ROLL_CLOCK
, },
375 { "viewpoint-zoom-in", ACTIONID_VIEWPOINT_ZOOM_IN
, },
376 { "viewpoint-zoom-out", ACTIONID_VIEWPOINT_ZOOM_OUT
, },
377 { "vol-down", ACTIONID_VOL_DOWN
, },
378 { "vol-mute", ACTIONID_VOL_MUTE
, },
379 { "vol-up", ACTIONID_VOL_UP
, },
380 { "wallpaper", ACTIONID_WALLPAPER
, },
381 { "zoom", ACTIONID_ZOOM
, },
382 { "zoom-double", ACTIONID_ZOOM_DOUBLE
, },
383 { "zoom-half", ACTIONID_ZOOM_HALF
, },
384 { "zoom-original", ACTIONID_ZOOM_ORIGINAL
, },
385 { "zoom-quarter", ACTIONID_ZOOM_QUARTER
, },
387 #define ACTIONS_COUNT (sizeof (actions) / sizeof (actions[0]))
391 uint32_t key
; ///< Key code
392 vlc_action_t action
; ///< Action ID
395 static int keycmp (const void *a
, const void *b
)
397 const struct mapping
*ka
= a
, *kb
= b
;
399 return (ka
->key
< kb
->key
) ? -1 : (ka
->key
> kb
->key
) ? +1 : 0;
404 void *map
; /* Key map */
405 void *global_map
; /* Grabbed/global key map */
406 struct hotkey keys
[1];
409 static int vlc_key_to_action (vlc_object_t
*obj
, const char *varname
,
410 vlc_value_t prevkey
, vlc_value_t curkey
, void *d
)
412 void *const *map
= d
;
413 const struct mapping
**pent
;
414 uint32_t keycode
= curkey
.i_int
;
416 pent
= tfind (&keycode
, map
, keycmp
);
422 return var_SetInteger (obj
, "key-action", (*pent
)->action
);
426 * Adds a mapping from a certain key code to a certain action.
428 static int vlc_AddMapping (void **map
, uint32_t keycode
, vlc_action_t action
)
430 struct mapping
*entry
= malloc (sizeof (*entry
));
433 entry
->key
= keycode
;
434 entry
->action
= action
;
436 struct mapping
**pent
= tsearch (entry
, map
, keycmp
);
437 if (unlikely(pent
== NULL
))
447 static void vlc_AddWheelMapping (void **map
, uint32_t kmore
, uint32_t kless
,
450 vlc_action_t amore
= ACTIONID_NONE
, aless
= ACTIONID_NONE
;
454 case 0: /* volume up/down */
455 amore
= ACTIONID_COMBO_VOL_ZOOM_UP
;
456 aless
= ACTIONID_COMBO_VOL_ZOOM_DOWN
;
458 case 2: /* position latter/earlier */
459 amore
= ACTIONID_JUMP_FORWARD_EXTRASHORT
;
460 aless
= ACTIONID_JUMP_BACKWARD_EXTRASHORT
;
462 case 3: /* position earlier/latter */
463 amore
= ACTIONID_JUMP_BACKWARD_EXTRASHORT
;
464 aless
= ACTIONID_JUMP_FORWARD_EXTRASHORT
;
468 if (amore
!= ACTIONID_NONE
)
469 vlc_AddMapping (map
, kmore
, amore
);
470 if (aless
!= ACTIONID_NONE
)
471 vlc_AddMapping (map
, kless
, aless
);
476 * Sets up all key mappings for a given action.
477 * \param map tree (of struct mapping entries) to write mappings to
478 * \param confname VLC configuration item to read mappings from
479 * \param action action ID
481 static void vlc_InitAction (vlc_object_t
*obj
, void **map
,
482 const char *confname
, vlc_action_t action
)
484 char *keys
= var_InheritString (obj
, confname
);
488 for (char *buf
, *key
= strtok_r (keys
, "\t", &buf
);
490 key
= strtok_r (NULL
, "\t", &buf
))
492 uint32_t code
= vlc_str2keycode (key
);
493 if (code
== KEY_UNSET
)
495 msg_Warn (obj
, "Key \"%s\" unrecognized", key
);
499 if (vlc_AddMapping (map
, code
, action
) == EEXIST
)
500 msg_Warn (obj
, "Key \"%s\" bound to multiple actions", key
);
506 * Initializes the key map from configuration.
508 struct vlc_actions
*vlc_InitActions (libvlc_int_t
*libvlc
)
510 vlc_object_t
*obj
= VLC_OBJECT(libvlc
);
512 struct vlc_actions
*as
= malloc (sizeof (*as
) + ACTIONS_COUNT
* sizeof (*keys
));
514 if (unlikely(as
== NULL
))
517 as
->global_map
= NULL
;
520 var_Create (obj
, "key-pressed", VLC_VAR_INTEGER
);
521 var_Create (obj
, "global-key-pressed", VLC_VAR_INTEGER
);
522 var_Create (obj
, "key-action", VLC_VAR_INTEGER
);
524 /* Initialize from configuration */
525 for (size_t i
= 0; i
< ACTIONS_COUNT
; i
++)
529 && strcmp (actions
[i
-1].name
, actions
[i
].name
) >= 0)
531 msg_Err (libvlc
, "key-%s and key-%s are not ordered properly",
532 actions
[i
-1].name
, actions
[i
].name
);
536 keys
->psz_action
= actions
[i
].name
;
539 char name
[12 + MAXACTION
];
541 snprintf (name
, sizeof (name
), "global-key-%s", actions
[i
].name
);
542 vlc_InitAction (obj
, &as
->map
, name
+ 7, actions
[i
].value
);
543 vlc_InitAction (obj
, &as
->global_map
, name
, actions
[i
].value
);
545 keys
->psz_action
= NULL
;
547 /* Initialize mouse wheel events */
548 vlc_AddWheelMapping (&as
->map
, KEY_MOUSEWHEELRIGHT
, KEY_MOUSEWHEELLEFT
,
549 var_InheritInteger (obj
, "hotkeys-x-wheel-mode"));
550 vlc_AddWheelMapping (&as
->map
, KEY_MOUSEWHEELUP
, KEY_MOUSEWHEELDOWN
,
551 var_InheritInteger (obj
, "hotkeys-y-wheel-mode"));
553 libvlc
->p_hotkeys
= as
->keys
;
554 var_AddCallback (obj
, "key-pressed", vlc_key_to_action
, &as
->map
);
555 var_AddCallback (obj
, "global-key-pressed", vlc_key_to_action
,
561 * Destroys the key map.
563 void vlc_DeinitActions (libvlc_int_t
*libvlc
, struct vlc_actions
*as
)
565 if (unlikely(as
== NULL
))
568 var_DelCallback (libvlc
, "global-key-pressed", vlc_key_to_action
,
570 var_DelCallback (libvlc
, "key-pressed", vlc_key_to_action
, &as
->map
);
572 tdestroy (as
->global_map
, free
);
573 tdestroy (as
->map
, free
);
575 libvlc
->p_hotkeys
= NULL
;
579 static int actcmp(const void *key
, const void *ent
)
581 const struct action
*act
= ent
;
582 return strcmp(key
, act
->name
);
586 * Get the action ID from the action name in the configuration subsystem.
587 * @return the action ID or ACTIONID_NONE on error.
589 vlc_action_t
vlc_GetActionId (const char *name
)
591 const struct action
*act
;
593 if (strncmp (name
, "key-", 4))
594 return ACTIONID_NONE
;
597 act
= bsearch(name
, actions
, ACTIONS_COUNT
, sizeof(*act
), actcmp
);
598 return (act
!= NULL
) ? act
->value
: ACTIONID_NONE
;