]> git.sesse.net Git - vlc/commitdiff
Gather key functions and tables in a single file
authorRémi Denis-Courmont <remi@remlab.net>
Wed, 7 Oct 2009 17:37:12 +0000 (20:37 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Wed, 7 Oct 2009 19:43:26 +0000 (22:43 +0300)
include/vlc_keys.h
src/Makefile.am
src/config/configuration.h
src/config/file.c
src/config/keys.c [new file with mode: 0644]
src/libvlccore.sym
src/test/keys.c

index cc2cca5380c70f43d88e2d6ceb4d12eaf049860a..f24dd6455a11d5750ac7c8e3ba88729a0ac73b51 100644 (file)
 #ifndef VLC_KEYS_H
 #define VLC_KEYS_H 1
 
-#ifdef __APPLE__
-#include <stdint.h>
-#endif
-
 /**
  * \file
- * This file defines keys, functions and structures for hotkey handling in vlc
- *
+ * This file defines keys and functions
  */
 
 #define KEY_MODIFIER         0xFF000000
 #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 <stdlib.h>
-
-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,
index a508b86287c7a32424064591add5a1a419365a06..92e6ce95627c4f051a4636ce6218e41c47028847 100644 (file)
@@ -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 \
index 6533c211fd98c0a480ea9c813a58d8ffd1da5998..79e041e047fe211e873520873f2d1caffd934e12 100644 (file)
@@ -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"
index 6c8340ee8939a34a1c8427b9752275185d75fd64..5d0f3772a3995b32a1f72ddc16558cb4c691258e 100644 (file)
@@ -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 (file)
index 0000000..07980e6
--- /dev/null
@@ -0,0 +1,251 @@
+/*****************************************************************************
+ * keys.c: keys configuration
+ *****************************************************************************
+ * Copyright (C) 2003-2009 the VideoLAN team
+ *
+ * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
+ *
+ * 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 <config.h>
+#endif
+
+/**
+ * \file
+ * This file defines functions and structures for hotkey handling in vlc
+ */
+
+#include <vlc_common.h>
+#include <vlc_keys.h>
+#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;
+}
index 2f3901627108257769ac237d704e7d6b4af9d14d..ac57220a73e335b0e6df902d799ca1e655c7d47c 100644 (file)
@@ -608,3 +608,5 @@ text_style_Delete
 text_style_Duplicate
 text_style_New
 xml_Delete
+KeyToString
+StringToKey
index 807366e2f05d8928ea386cef1bb1782ddd7fabc4..03843041bec6a71937702a3a518d3b60deb770a7 100644 (file)
@@ -26,8 +26,8 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-#include <vlc_common.h>
-#include <vlc_keys.h>
+#define __LIBVLC__
+#include "../config/keys.c"
 
 int main (void)
 {