Remove reference to toolbox
[vlc/asuraparaju-public.git] / src / config / keys.c
blob637d4b1445f83eff1b9ebfe0c5ca657c42ecfb15
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 *****************************************************************************/
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 #include <vlc_common.h>
33 #include <vlc_keys.h>
34 #include "configuration.h"
36 typedef struct key_descriptor_s
38 const char *psz_key_string;
39 uint32_t i_key_code;
40 } key_descriptor_t;
42 static const struct key_descriptor_s vlc_modifiers[] =
44 { "Alt", KEY_MODIFIER_ALT },
45 { "Shift", KEY_MODIFIER_SHIFT },
46 { "Ctrl", KEY_MODIFIER_CTRL },
47 { "Meta", KEY_MODIFIER_META },
48 { "Command", KEY_MODIFIER_COMMAND }
50 enum { vlc_num_modifiers=sizeof(vlc_modifiers)
51 /sizeof(struct key_descriptor_s) };
53 static const struct key_descriptor_s vlc_keys[] =
55 { "Unset", KEY_UNSET },
56 { "Backspace", KEY_BACKSPACE },
57 { "Tab", KEY_TAB },
58 { "Enter", KEY_ENTER },
59 { "Esc", KEY_ESC },
60 { "Space", ' ' },
61 { "Left", KEY_LEFT },
62 { "Right", KEY_RIGHT },
63 { "Up", KEY_UP },
64 { "Down", KEY_DOWN },
65 { "F1", KEY_F1 },
66 { "F2", KEY_F2 },
67 { "F3", KEY_F3 },
68 { "F4", KEY_F4 },
69 { "F5", KEY_F5 },
70 { "F6", KEY_F6 },
71 { "F7", KEY_F7 },
72 { "F8", KEY_F8 },
73 { "F9", KEY_F9 },
74 { "F10", KEY_F10 },
75 { "F11", KEY_F11 },
76 { "F12", KEY_F12 },
77 { "Home", KEY_HOME },
78 { "End", KEY_END },
79 { "Insert", KEY_INSERT },
80 { "Delete", KEY_DELETE },
81 { "Menu", KEY_MENU },
82 { "Page Up", KEY_PAGEUP },
83 { "Page Down", KEY_PAGEDOWN },
84 { "Browser Back", KEY_BROWSER_BACK },
85 { "Browser Forward", KEY_BROWSER_FORWARD },
86 { "Browser Refresh", KEY_BROWSER_REFRESH },
87 { "Browser Stop", KEY_BROWSER_STOP },
88 { "Browser Search", KEY_BROWSER_SEARCH },
89 { "Browser Favorites", KEY_BROWSER_FAVORITES },
90 { "Browser Home", KEY_BROWSER_HOME },
91 { "Volume Mute", KEY_VOLUME_MUTE },
92 { "Volume Down", KEY_VOLUME_DOWN },
93 { "Volume Up", KEY_VOLUME_UP },
94 { "Media Next Track", KEY_MEDIA_NEXT_TRACK },
95 { "Media Prev Track", KEY_MEDIA_PREV_TRACK },
96 { "Media Stop", KEY_MEDIA_STOP },
97 { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE },
98 { "Mouse Wheel Up", KEY_MOUSEWHEELUP },
99 { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN },
100 { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT },
101 { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT },
103 enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) };
105 static int cmpkey (const void *key, const void *elem)
107 return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code;
110 /* Convert Unicode code point to UTF-8 */
111 static char *utf8_cp (uint_fast32_t cp, char *buf)
113 if (cp < (1 << 7))
115 buf[1] = 0;
116 buf[0] = cp;
118 else if (cp < (1 << 11))
120 buf[2] = 0;
121 buf[1] = 0x80 | (cp & 0x3F);
122 cp >>= 6;
123 buf[0] = 0xC0 | cp;
125 else if (cp < (1 << 16))
127 buf[3] = 0;
128 buf[2] = 0x80 | (cp & 0x3F);
129 cp >>= 6;
130 buf[1] = 0x80 | (cp & 0x3F);
131 cp >>= 6;
132 buf[0] = 0xE0 | cp;
134 else if (cp < (1 << 21))
136 buf[4] = 0;
137 buf[3] = 0x80 | (cp & 0x3F);
138 cp >>= 6;
139 buf[2] = 0x80 | (cp & 0x3F);
140 cp >>= 6;
141 buf[1] = 0x80 | (cp & 0x3F);
142 cp >>= 6;
143 buf[0] = 0xE0 | cp;
145 else
146 return NULL;
147 return buf;
150 /* Convert UTF-8 to Unicode code point */
151 static uint_fast32_t cp_utf8 (const char *utf8)
153 uint8_t f = utf8[0];
154 size_t l = strlen (utf8);
156 if (f < 0x80) /* ASCII (7 bits) */
157 return f;
158 if (f < 0xC0 || l < 2) /* bad */
159 return 0;
160 if (f < 0xE0) /* two bytes (11 bits) */
161 return ((f & 0x1F) << 6) | (utf8[1] & 0x3F);
162 if (l < 3) /* bad */
163 return 0;
164 if (f < 0xF0) /* three bytes (16 bits) */
165 return ((f & 0x0F) << 12) | ((utf8[1] & 0x3F) << 6)
166 | (utf8[2] & 0x3F);
167 if (l < 4)
168 return 0;
169 if (f < 0xF8) /* four bytes (21 bits) */
170 return ((f & 0x07) << 18) | ((utf8[1] & 0x3F) << 12)
171 | ((utf8[2] & 0x3F) << 6) | (utf8[3] & 0x3F);
172 return 0;
175 char *KeyToString (uint_fast32_t sym)
177 key_descriptor_t *d;
179 d = bsearch ((void *)(uintptr_t)sym, vlc_keys, vlc_num_keys,
180 sizeof (vlc_keys[0]), cmpkey);
181 if (d)
182 return strdup (d->psz_key_string);
184 char buf[5];
185 if (utf8_cp (sym, buf))
186 return strdup (buf);
188 return NULL;
191 uint_fast32_t StringToKey (char *name)
193 for (size_t i = 0; i < vlc_num_keys; i++)
194 if (!strcmp (vlc_keys[i].psz_key_string, name))
195 return vlc_keys[i].i_key_code;
197 return cp_utf8 (name);
200 uint_fast32_t ConfigStringToKey (const char *name)
202 uint_fast32_t mods = 0;
204 for (;;)
206 const char *psz_parser = strchr (name, '-');
207 if (psz_parser == NULL || psz_parser == name)
208 break;
210 for (size_t i = 0; i < vlc_num_modifiers; i++)
212 if (!strncasecmp (vlc_modifiers[i].psz_key_string, name,
213 strlen (vlc_modifiers[i].psz_key_string)))
215 mods |= vlc_modifiers[i].i_key_code;
218 name = psz_parser + 1;
221 for (size_t i = 0; i < vlc_num_keys; i++)
222 if (!strcasecmp( vlc_keys[i].psz_key_string, name))
223 return vlc_keys[i].i_key_code | mods;
225 return cp_utf8 (name) | mods;
228 char *ConfigKeyToString (uint_fast32_t i_key)
230 // Worst case appears to be 45 characters:
231 // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
232 char *psz_key = malloc (64);
233 if (!psz_key)
234 return NULL;
236 char *p = psz_key, *psz_end = psz_key + 54;
237 *p = '\0';
239 for (size_t i = 0; i < vlc_num_modifiers; i++)
241 if (i_key & vlc_modifiers[i].i_key_code)
243 p += snprintf (p, psz_end - p, "%s-",
244 vlc_modifiers[i].psz_key_string);
248 key_descriptor_t *d;
249 char buf[5];
251 i_key &= ~KEY_MODIFIER;
252 d = bsearch ((void *)(uintptr_t)i_key, vlc_keys, vlc_num_keys,
253 sizeof (vlc_keys[0]), cmpkey);
254 if (d)
255 p += snprintf (p, psz_end - p, "%s", d->psz_key_string);
256 else if (utf8_cp (i_key, buf))
257 p += snprintf (p, psz_end - p, "%s", buf);
258 else
259 return NULL;
261 return psz_key;