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"
36 typedef struct key_descriptor_s
38 const char *psz_key_string;
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 },
121 { "Left", KEY_LEFT },
122 { "Right", KEY_RIGHT },
124 { "Down", KEY_DOWN },
125 { "Enter", KEY_ENTER },
138 { "Home", KEY_HOME },
140 { "Insert", KEY_INSERT },
141 { "Delete", KEY_DELETE },
142 { "Menu", KEY_MENU },
144 { "Page Up", KEY_PAGEUP },
145 { "Page Down", KEY_PAGEDOWN },
147 { "Backspace", KEY_BACKSPACE },
148 { "Browser Back", KEY_BROWSER_BACK },
149 { "Browser Forward", KEY_BROWSER_FORWARD },
150 { "Browser Refresh", KEY_BROWSER_REFRESH },
151 { "Browser Stop", KEY_BROWSER_STOP },
152 { "Browser Search", KEY_BROWSER_SEARCH },
153 { "Browser Favorites", KEY_BROWSER_FAVORITES },
154 { "Browser Home", KEY_BROWSER_HOME },
155 { "Volume Mute", KEY_VOLUME_MUTE },
156 { "Volume Down", KEY_VOLUME_DOWN },
157 { "Volume Up", KEY_VOLUME_UP },
158 { "Media Next Track", KEY_MEDIA_NEXT_TRACK },
159 { "Media Prev Track", KEY_MEDIA_PREV_TRACK },
160 { "Media Stop", KEY_MEDIA_STOP },
161 { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE },
162 { "Mouse Wheel Up", KEY_MOUSEWHEELUP },
163 { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN },
164 { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT },
165 { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT },
167 enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) };
169 static int cmpkey (const void *key, const void *elem)
171 return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code;
174 const char *KeyToString (uint_fast32_t sym)
178 d = (key_descriptor_t *)
179 bsearch ((void *)(uintptr_t)sym, vlc_keys, vlc_num_keys,
180 sizeof (vlc_keys[0]), cmpkey);
181 return d ? d->psz_key_string : NULL;
184 uint_fast32_t StringToKey (char *name)
186 for (size_t i = 0; i < vlc_num_keys; i++)
187 if (!strcmp (vlc_keys[i].psz_key_string, name))
188 return vlc_keys[i].i_key_code;
193 uint_fast32_t ConfigStringToKey (const char *name)
195 uint_fast32_t mods = 0;
197 const char *psz_parser = name;
200 psz_parser = strchr (psz_parser, '-');
201 if (psz_parser == NULL || psz_parser == name)
204 for (size_t i = 0; i < vlc_num_modifiers; i++)
206 if (!strncasecmp (vlc_modifiers[i].psz_key_string, name,
207 strlen (vlc_modifiers[i].psz_key_string)))
209 mods |= vlc_modifiers[i].i_key_code;
212 name = psz_parser + 1;
215 for (size_t i = 0; i < vlc_num_keys; i++)
216 if (!strcasecmp( vlc_keys[i].psz_key_string, name))
217 return vlc_keys[i].i_key_code | mods;
222 char *ConfigKeyToString (uint_fast32_t i_key)
224 // Worst case appears to be 45 characters:
225 // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites"
226 char *psz_key = malloc (64);
230 char *p = psz_key, *psz_end = psz_key + 54;
233 for (size_t i = 0; i < vlc_num_modifiers; i++)
235 if (i_key & vlc_modifiers[i].i_key_code)
237 p += snprintf (p, psz_end - p, "%s-",
238 vlc_modifiers[i].psz_key_string);
241 for (size_t i = 0; i < vlc_num_keys; i++)
243 if ((i_key & ~KEY_MODIFIER) == vlc_keys[i].i_key_code)
245 p += snprintf (p, psz_end - p, "%s",
246 vlc_keys[i].psz_key_string);