From: RĂ©mi Denis-Courmont Date: Wed, 7 Oct 2009 17:37:12 +0000 (+0300) Subject: Gather key functions and tables in a single file X-Git-Tag: 1.1.0-ff~2981 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=9142f0c610d9eea3ed0224cb4bf9fa19087222d7;p=vlc Gather key functions and tables in a single file --- diff --git a/include/vlc_keys.h b/include/vlc_keys.h index cc2cca5380..f24dd6455a 100644 --- a/include/vlc_keys.h +++ b/include/vlc_keys.h @@ -24,14 +24,9 @@ #ifndef VLC_KEYS_H #define VLC_KEYS_H 1 -#ifdef __APPLE__ -#include -#endif - /** * \file - * This file defines keys, functions and structures for hotkey handling in vlc - * + * This file defines keys and functions */ #define KEY_MODIFIER 0xFF000000 @@ -91,170 +86,8 @@ #define KEY_MOUSEWHEELLEFT 0x00F20000 #define KEY_MOUSEWHEELRIGHT 0x00F30000 -typedef struct key_descriptor_s -{ - const char *psz_key_string; - uint32_t i_key_code; -} key_descriptor_t; - -#define ADD_KEY(a) { a, *a } - -static const struct key_descriptor_s vlc_modifiers[] = -{ - { "Alt", KEY_MODIFIER_ALT }, - { "Shift", KEY_MODIFIER_SHIFT }, - { "Ctrl", KEY_MODIFIER_CTRL }, - { "Meta", KEY_MODIFIER_META }, - { "Command", KEY_MODIFIER_COMMAND } -}; -enum { vlc_num_modifiers=sizeof(vlc_modifiers) - /sizeof(struct key_descriptor_s) }; - -static const struct key_descriptor_s vlc_keys[] = -{ - { "Unset", KEY_UNSET }, - { "Space", ' ' }, - { "!", '!' }, - { "\"", '\"' }, - { "#", '#' }, - { "$", '$' }, - { "%", '%' }, - { "&", '&' }, - { "'", '\'' }, - { "(", ')' }, - { ")", ')' }, - { "*", '*' }, - { "+", '+' }, - { ",", ',' }, - { "-", '-' }, - { ".", '.' }, - { "/", '/' }, - { "0", '0' }, - { "1", '1' }, - { "2", '2' }, - { "3", '3' }, - { "4", '4' }, - { "5", '5' }, - { "6", '6' }, - { "7", '7' }, - { "8", '8' }, - { "9", '9' }, - { ":", ':' }, - { ";", ';' }, - { "<", '<' }, - { "=", '=' }, - { ">", '>' }, - { "?", '?' }, - { "@", '@' }, - { "[", '[' }, - { "\\", '\\' }, - { "]", ']' }, - { "^", '^' }, - { "_", '_' }, - { "`", '`' }, - { "a", 'a' }, - { "b", 'b' }, - { "c", 'c' }, - { "d", 'd' }, - { "e", 'e' }, - { "f", 'f' }, - { "g", 'g' }, - { "h", 'h' }, - { "i", 'i' }, - { "j", 'j' }, - { "k", 'k' }, - { "l", 'l' }, - { "m", 'm' }, - { "n", 'n' }, - { "o", 'o' }, - { "p", 'p' }, - { "q", 'q' }, - { "r", 'r' }, - { "s", 's' }, - { "t", 't' }, - { "u", 'u' }, - { "v", 'v' }, - { "w", 'w' }, - { "x", 'x' }, - { "y", 'y' }, - { "z", 'z' }, - { "Left", KEY_LEFT }, - { "Right", KEY_RIGHT }, - { "Up", KEY_UP }, - { "Down", KEY_DOWN }, - { "Enter", KEY_ENTER }, - { "F1", KEY_F1 }, - { "F2", KEY_F2 }, - { "F3", KEY_F3 }, - { "F4", KEY_F4 }, - { "F5", KEY_F5 }, - { "F6", KEY_F6 }, - { "F7", KEY_F7 }, - { "F8", KEY_F8 }, - { "F9", KEY_F9 }, - { "F10", KEY_F10 }, - { "F11", KEY_F11 }, - { "F12", KEY_F12 }, - { "Home", KEY_HOME }, - { "End", KEY_END }, - { "Insert", KEY_INSERT }, - { "Delete", KEY_DELETE }, - { "Menu", KEY_MENU }, - { "Esc", KEY_ESC }, - { "Page Up", KEY_PAGEUP }, - { "Page Down", KEY_PAGEDOWN }, - { "Tab", KEY_TAB }, - { "Backspace", KEY_BACKSPACE }, - { "Browser Back", KEY_BROWSER_BACK }, - { "Browser Forward", KEY_BROWSER_FORWARD }, - { "Browser Refresh", KEY_BROWSER_REFRESH }, - { "Browser Stop", KEY_BROWSER_STOP }, - { "Browser Search", KEY_BROWSER_SEARCH }, - { "Browser Favorites", KEY_BROWSER_FAVORITES }, - { "Browser Home", KEY_BROWSER_HOME }, - { "Volume Mute", KEY_VOLUME_MUTE }, - { "Volume Down", KEY_VOLUME_DOWN }, - { "Volume Up", KEY_VOLUME_UP }, - { "Media Next Track", KEY_MEDIA_NEXT_TRACK }, - { "Media Prev Track", KEY_MEDIA_PREV_TRACK }, - { "Media Stop", KEY_MEDIA_STOP }, - { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE }, - { "Mouse Wheel Up", KEY_MOUSEWHEELUP }, - { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN }, - { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT }, - { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT }, -}; -enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) }; - -#include - -static inline int cmpkey (const void *key, const void *elem) -{ - return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code; -} - -static inline const char *KeyToString( uint32_t i_key ) -{ - key_descriptor_t *d; - - d = (key_descriptor_t *) - bsearch ((void *)(uintptr_t)i_key, vlc_keys, vlc_num_keys, - sizeof (vlc_keys[0]), cmpkey); - return d ? d->psz_key_string : NULL; -} - -static inline int StringToKey( char *psz_key ) -{ - size_t i; - for ( i = 0; i < vlc_num_keys; ++i ) - { - if ( !strcmp( vlc_keys[i].psz_key_string, psz_key )) - { - return vlc_keys[i].i_key_code; - } - } - return 0; -} +VLC_EXPORT( const char *, KeyToString, (uint_fast32_t i_key) ) LIBVLC_USED; +VLC_EXPORT( uint_fast32_t, StringToKey, (char *psz_key) ) LIBVLC_USED; typedef enum vlc_key { ACTIONID_QUIT = 1, diff --git a/src/Makefile.am b/src/Makefile.am index a508b86287..92e6ce9562 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -420,6 +420,7 @@ SOURCES_libvlc_common = \ config/chain.c \ config/file.c \ config/intf.c \ + config/keys.c \ config/cmdline.c \ misc/events.c \ misc/image.c \ diff --git a/src/config/configuration.h b/src/config/configuration.h index 6533c211fd..79e041e047 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -52,7 +52,8 @@ static inline int IsConfigFloatType (int type) return type == CONFIG_ITEM_FLOAT; } -int ConfigStringToKey( const char * ); +uint_fast32_t ConfigStringToKey( const char * ); +char *ConfigKeyToString( uint_fast32_t ); /* The configuration file */ #define CONFIG_FILE "vlcrc" diff --git a/src/config/file.c b/src/config/file.c index 6c8340ee89..5d0f3772a3 100644 --- a/src/config/file.c +++ b/src/config/file.c @@ -44,8 +44,6 @@ #include "configuration.h" #include "modules/modules.h" -static char *ConfigKeyToString( int ); - static inline char *strdupnull (const char *src) { return src ? strdup (src) : NULL; @@ -746,69 +744,3 @@ int __config_SaveConfigFile( vlc_object_t *p_this, const char *psz_module_name ) { return SaveConfigFile( p_this, psz_module_name, false ); } - -int ConfigStringToKey( const char *psz_key ) -{ - int i_key = 0; - size_t i; - const char *psz_parser = strchr( psz_key, '-' ); - while( psz_parser && psz_parser != psz_key ) - { - for( i = 0; i < vlc_num_modifiers; ++i ) - { - if( !strncasecmp( vlc_modifiers[i].psz_key_string, psz_key, - strlen( vlc_modifiers[i].psz_key_string ) ) ) - { - i_key |= vlc_modifiers[i].i_key_code; - } - } - psz_key = psz_parser + 1; - psz_parser = strchr( psz_key, '-' ); - } - for( i = 0; i < vlc_num_keys; ++i ) - { - if( !strcasecmp( vlc_keys[i].psz_key_string, psz_key ) ) - { - i_key |= vlc_keys[i].i_key_code; - break; - } - } - return i_key; -} - -char *ConfigKeyToString( int i_key ) -{ - // Worst case appears to be 45 characters: - // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites" - enum { keylen=64 }; - char *psz_key = malloc( keylen ); - char *p; - size_t index; - - if ( !psz_key ) - { - return NULL; - } - *psz_key = '\0'; - p = psz_key; - - for( index = 0; index < vlc_num_modifiers; ++index ) - { - if( i_key & vlc_modifiers[index].i_key_code ) - { - p += snprintf( p, keylen-(psz_key-p), "%s-", - vlc_modifiers[index].psz_key_string ); - } - } - for( index = 0; index < vlc_num_keys; ++index ) - { - if( (int)( i_key & ~KEY_MODIFIER ) == vlc_keys[index].i_key_code ) - { - p += snprintf( p, keylen-(psz_key-p), "%s", - vlc_keys[index].psz_key_string ); - break; - } - } - return psz_key; -} - diff --git a/src/config/keys.c b/src/config/keys.c new file mode 100644 index 0000000000..07980e682d --- /dev/null +++ b/src/config/keys.c @@ -0,0 +1,251 @@ +/***************************************************************************** + * keys.c: keys configuration + ***************************************************************************** + * Copyright (C) 2003-2009 the VideoLAN team + * + * Authors: Sigmund Augdal Helberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include +#endif + +/** + * \file + * This file defines functions and structures for hotkey handling in vlc + */ + +#include +#include +#include "configuration.h" + +typedef struct key_descriptor_s +{ + const char *psz_key_string; + uint32_t i_key_code; +} key_descriptor_t; + +static const struct key_descriptor_s vlc_modifiers[] = +{ + { "Alt", KEY_MODIFIER_ALT }, + { "Shift", KEY_MODIFIER_SHIFT }, + { "Ctrl", KEY_MODIFIER_CTRL }, + { "Meta", KEY_MODIFIER_META }, + { "Command", KEY_MODIFIER_COMMAND } +}; +enum { vlc_num_modifiers=sizeof(vlc_modifiers) + /sizeof(struct key_descriptor_s) }; + +static const struct key_descriptor_s vlc_keys[] = +{ + { "Unset", KEY_UNSET }, + { "Space", ' ' }, + { "!", '!' }, + { "\"", '\"' }, + { "#", '#' }, + { "$", '$' }, + { "%", '%' }, + { "&", '&' }, + { "'", '\'' }, + { "(", ')' }, + { ")", ')' }, + { "*", '*' }, + { "+", '+' }, + { ",", ',' }, + { "-", '-' }, + { ".", '.' }, + { "/", '/' }, + { "0", '0' }, + { "1", '1' }, + { "2", '2' }, + { "3", '3' }, + { "4", '4' }, + { "5", '5' }, + { "6", '6' }, + { "7", '7' }, + { "8", '8' }, + { "9", '9' }, + { ":", ':' }, + { ";", ';' }, + { "<", '<' }, + { "=", '=' }, + { ">", '>' }, + { "?", '?' }, + { "@", '@' }, + { "[", '[' }, + { "\\", '\\' }, + { "]", ']' }, + { "^", '^' }, + { "_", '_' }, + { "`", '`' }, + { "a", 'a' }, + { "b", 'b' }, + { "c", 'c' }, + { "d", 'd' }, + { "e", 'e' }, + { "f", 'f' }, + { "g", 'g' }, + { "h", 'h' }, + { "i", 'i' }, + { "j", 'j' }, + { "k", 'k' }, + { "l", 'l' }, + { "m", 'm' }, + { "n", 'n' }, + { "o", 'o' }, + { "p", 'p' }, + { "q", 'q' }, + { "r", 'r' }, + { "s", 's' }, + { "t", 't' }, + { "u", 'u' }, + { "v", 'v' }, + { "w", 'w' }, + { "x", 'x' }, + { "y", 'y' }, + { "z", 'z' }, + { "Left", KEY_LEFT }, + { "Right", KEY_RIGHT }, + { "Up", KEY_UP }, + { "Down", KEY_DOWN }, + { "Enter", KEY_ENTER }, + { "F1", KEY_F1 }, + { "F2", KEY_F2 }, + { "F3", KEY_F3 }, + { "F4", KEY_F4 }, + { "F5", KEY_F5 }, + { "F6", KEY_F6 }, + { "F7", KEY_F7 }, + { "F8", KEY_F8 }, + { "F9", KEY_F9 }, + { "F10", KEY_F10 }, + { "F11", KEY_F11 }, + { "F12", KEY_F12 }, + { "Home", KEY_HOME }, + { "End", KEY_END }, + { "Insert", KEY_INSERT }, + { "Delete", KEY_DELETE }, + { "Menu", KEY_MENU }, + { "Esc", KEY_ESC }, + { "Page Up", KEY_PAGEUP }, + { "Page Down", KEY_PAGEDOWN }, + { "Tab", KEY_TAB }, + { "Backspace", KEY_BACKSPACE }, + { "Browser Back", KEY_BROWSER_BACK }, + { "Browser Forward", KEY_BROWSER_FORWARD }, + { "Browser Refresh", KEY_BROWSER_REFRESH }, + { "Browser Stop", KEY_BROWSER_STOP }, + { "Browser Search", KEY_BROWSER_SEARCH }, + { "Browser Favorites", KEY_BROWSER_FAVORITES }, + { "Browser Home", KEY_BROWSER_HOME }, + { "Volume Mute", KEY_VOLUME_MUTE }, + { "Volume Down", KEY_VOLUME_DOWN }, + { "Volume Up", KEY_VOLUME_UP }, + { "Media Next Track", KEY_MEDIA_NEXT_TRACK }, + { "Media Prev Track", KEY_MEDIA_PREV_TRACK }, + { "Media Stop", KEY_MEDIA_STOP }, + { "Media Play Pause", KEY_MEDIA_PLAY_PAUSE }, + { "Mouse Wheel Up", KEY_MOUSEWHEELUP }, + { "Mouse Wheel Down", KEY_MOUSEWHEELDOWN }, + { "Mouse Wheel Left", KEY_MOUSEWHEELLEFT }, + { "Mouse Wheel Right", KEY_MOUSEWHEELRIGHT }, +}; +enum { vlc_num_keys=sizeof(vlc_keys)/sizeof(struct key_descriptor_s) }; + +static int cmpkey (const void *key, const void *elem) +{ + return ((uintptr_t)key) - ((key_descriptor_t *)elem)->i_key_code; +} + +const char *KeyToString (uint_fast32_t sym) +{ + key_descriptor_t *d; + + d = (key_descriptor_t *) + bsearch ((void *)(uintptr_t)sym, vlc_keys, vlc_num_keys, + sizeof (vlc_keys[0]), cmpkey); + return d ? d->psz_key_string : NULL; +} + +uint_fast32_t StringToKey (char *name) +{ + for (size_t i = 0; i < vlc_num_keys; i++) + if (!strcmp (vlc_keys[i].psz_key_string, name)) + return vlc_keys[i].i_key_code; + + return 0; +} + +uint_fast32_t ConfigStringToKey (const char *name) +{ + uint_fast32_t mods = 0; + + const char *psz_parser = name; + for (;;) + { + psz_parser = strchr (psz_parser, '-'); + if (psz_parser == NULL || psz_parser == name) + break; + + for (size_t i = 0; i < vlc_num_modifiers; i++) + { + if (!strncasecmp (vlc_modifiers[i].psz_key_string, name, + strlen (vlc_modifiers[i].psz_key_string))) + { + mods |= vlc_modifiers[i].i_key_code; + } + } + name = psz_parser + 1; + } + + for (size_t i = 0; i < vlc_num_keys; i++) + if (!strcasecmp( vlc_keys[i].psz_key_string, name)) + return vlc_keys[i].i_key_code | mods; + + return 0; +} + +char *ConfigKeyToString (uint_fast32_t i_key) +{ + // Worst case appears to be 45 characters: + // "Command-Meta-Ctrl-Shift-Alt-Browser Favorites" + char *psz_key = malloc (64); + if (!psz_key) + return NULL; + + char *p = psz_key, *psz_end = psz_key + 54; + *p = '\0'; + + for (size_t i = 0; i < vlc_num_modifiers; i++) + { + if (i_key & vlc_modifiers[i].i_key_code) + { + p += snprintf (p, psz_end - p, "%s-", + vlc_modifiers[i].psz_key_string); + } + } + for (size_t i = 0; i < vlc_num_keys; i++) + { + if ((i_key & ~KEY_MODIFIER) == vlc_keys[i].i_key_code) + { + p += snprintf (p, psz_end - p, "%s", + vlc_keys[i].psz_key_string); + break; + } + } + return psz_key; +} diff --git a/src/libvlccore.sym b/src/libvlccore.sym index 2f39016271..ac57220a73 100644 --- a/src/libvlccore.sym +++ b/src/libvlccore.sym @@ -608,3 +608,5 @@ text_style_Delete text_style_Duplicate text_style_New xml_Delete +KeyToString +StringToKey diff --git a/src/test/keys.c b/src/test/keys.c index 807366e2f0..03843041be 100644 --- a/src/test/keys.c +++ b/src/test/keys.c @@ -26,8 +26,8 @@ #include #include -#include -#include +#define __LIBVLC__ +#include "../config/keys.c" int main (void) {