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
32 #include <vlc_common.h>
34 #include "configuration.h"
37 typedef struct key_descriptor_s
39 const char psz_key_string[20];
43 static const struct key_descriptor_s vlc_keys[] =
45 { "Unset", KEY_UNSET },
46 { "Backspace", KEY_BACKSPACE },
48 { "Enter", KEY_ENTER },
52 { "Right", KEY_RIGHT },
69 { "Insert", KEY_INSERT },
70 { "Delete", KEY_DELETE },
72 { "Page Up", KEY_PAGEUP },
73 { "Page Down", KEY_PAGEDOWN },
74 { "Browser Back", KEY_BROWSER_BACK },
75 { "Browser Forward", KEY_BROWSER_FORWARD },
76 { "Browser Refresh", KEY_BROWSER_REFRESH },
77 { "Browser Stop", KEY_BROWSER_STOP },
78 { "Browser Search", KEY_BROWSER_SEARCH },
79 { "Browser Favorites", KEY_BROWSER_FAVORITES },
80 { "Browser Home", KEY_BROWSER_HOME },
81 { "Volume Mute", KEY_VOLUME_MUTE },
82 { "Volume Down", KEY_VOLUME_DOWN },
83 { "Volume Up", KEY_VOLUME_UP },
84 { "Media Next Track", KEY_MEDIA_NEXT_TRACK },
85 { "Media Prev Track", KEY_MEDIA_PREV_TRACK },
86 { "Media Stop", KEY_MEDIA_STOP },
87 { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE },
88 { "Mouse Wheel Up", KEY_MOUSEWHEELUP },
89 { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN },
90 { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT },
91 { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT },
93 enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) };
95 static int cmpkey (const void *key, const void *elem)
97 return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code;
100 /* Convert Unicode code point to UTF-8 */
101 static char *utf8_cp (uint_fast32_t cp, char *buf)
108 else if (cp < (1 << 11))
111 buf[1] = 0x80 | (cp & 0x3F);
115 else if (cp < (1 << 16))
118 buf[2] = 0x80 | (cp & 0x3F);
120 buf[1] = 0x80 | (cp & 0x3F);
124 else if (cp < (1 << 21))
127 buf[3] = 0x80 | (cp & 0x3F);
129 buf[2] = 0x80 | (cp & 0x3F);
131 buf[1] = 0x80 | (cp & 0x3F);
140 uint_fast32_t ConfigStringToKey (const char *name)
142 uint_fast32_t mods = 0;
147 size_t len = strcspn (name, "-+");
148 if (name[len] == '\0')
151 if (len == 4 && !strncasecmp (name, "Ctrl", 4))
152 mods |= KEY_MODIFIER_CTRL;
153 if (len == 3 && !strncasecmp (name, "Alt", 3))
154 mods |= KEY_MODIFIER_ALT;
155 if (len == 5 && !strncasecmp (name, "Shift", 5))
156 mods |= KEY_MODIFIER_SHIFT;
157 if (len == 4 && !strncasecmp (name, "Meta", 4))
158 mods |= KEY_MODIFIER_META;
159 if (len == 7 && !strncasecmp (name, "Command", 7))
160 mods |= KEY_MODIFIER_COMMAND;
165 for (size_t i = 0; i < vlc_num_keys; i++)
166 if (!strcasecmp( vlc_keys[i].psz_key_string, name))
167 return vlc_keys[i].i_key_code | mods;
169 return (vlc_towc (name, &cp) > 0) ? (mods | cp) : 0;
172 char *vlc_keycode2str (uint_fast32_t code)
175 uintptr_t key = code & ~KEY_MODIFIER;
177 key_descriptor_t *d = bsearch ((void *)key, vlc_keys, vlc_num_keys,
178 sizeof (vlc_keys[0]), cmpkey);
179 if (d == NULL && utf8_cp (key, buf) == NULL)
182 if (asprintf (&str, "%s%s%s%s%s%s",
183 (code & KEY_MODIFIER_CTRL) ? "Ctrl+" : "",
184 (code & KEY_MODIFIER_ALT) ? "Alt+" : "",
185 (code & KEY_MODIFIER_SHIFT) ? "Shift+" : "",
186 (code & KEY_MODIFIER_META) ? "Meta+" : "",
187 (code & KEY_MODIFIER_COMMAND) ? "Command+" : "",
188 (d != NULL) ? d->psz_key_string : buf) == -1)