changeset 28137:28bf0d126da2

Add and use a special lookup function to do table-based translation to MPlayer keycodes.
author reimar
date Sat, 20 Dec 2008 11:52:11 +0000
parents a805c65b7665
children 540a5f41e536
files libvo/video_out.c libvo/video_out.h libvo/w32_common.c libvo/x11_common.c
diffstat 4 files changed, 49 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/libvo/video_out.c	Sat Dec 20 11:49:00 2008 +0000
+++ b/libvo/video_out.c	Sat Dec 20 11:52:11 2008 +0000
@@ -328,6 +328,17 @@
   return vo->config(width, height, d_width, d_height, flags, title, format);
 }
 
+/**
+ * \brief lookup an integer in a table, table must have 0 as the last key
+ * \param key key to search for
+ * \result translation corresponding to key or "to" value of last mapping
+ *         if not found.
+ */
+int lookup_keymap_table(const struct keymap *map, int key) {
+  while (map->from && map->from != key) map++;
+  return map->to;
+}
+
 #if defined(CONFIG_FBDEV) || defined(CONFIG_VESA)
 /* Borrowed from vo_fbdev.c
 Monitor ranges related functions*/
--- a/libvo/video_out.h	Sat Dec 20 11:49:00 2008 +0000
+++ b/libvo/video_out.h	Sat Dec 20 11:52:11 2008 +0000
@@ -251,4 +251,10 @@
 extern char *monitor_vfreq_str;
 extern char *monitor_dotclock_str;
 
+struct keymap {
+  int from;
+  int to;
+};
+int lookup_keymap_table(const struct keymap *map, int key);
+
 #endif /* MPLAYER_VIDEO_OUT_H */
--- a/libvo/w32_common.c	Sat Dec 20 11:49:00 2008 +0000
+++ b/libvo/w32_common.c	Sat Dec 20 11:52:11 2008 +0000
@@ -40,9 +40,24 @@
 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO);
 static BOOL (WINAPI* myEnumDisplayMonitors)(HDC, LPCRECT, MONITORENUMPROC, LPARAM);
 
+static const struct keymap vk_map[] = {
+    // special keys
+    {VK_ESCAPE, KEY_ESC}, {VK_BACK, KEY_BS}, {VK_TAB, KEY_TAB}, {VK_CONTROL, KEY_CTRL},
+
+    // cursor keys
+    {VK_LEFT, KEY_LEFT}, {VK_UP, KEY_UP}, {VK_RIGHT, KEY_RIGHT}, {VK_DOWN, KEY_DOWN},
+
+    // navigation block
+    {VK_INSERT, KEY_INSERT}, {VK_DELETE, KEY_DELETE}, {VK_HOME, KEY_HOME}, {VK_END, KEY_END},
+    {VK_PRIOR, KEY_PAGE_UP}, {VK_NEXT, KEY_PAGE_DOWN},
+
+    {0, 0}
+};
+
 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
     RECT r;
     POINT p;
+    int mpkey;
     switch (message) {
         case WM_ERASEBKGND: // no need to erase background seperately
             return 1;
@@ -88,22 +103,9 @@
             }
             break;
         case WM_KEYDOWN:
-            switch (wParam) {
-                case VK_LEFT:    mplayer_put_key(KEY_LEFT);      break;
-                case VK_UP:      mplayer_put_key(KEY_UP);        break;
-                case VK_RIGHT:   mplayer_put_key(KEY_RIGHT);     break;
-                case VK_DOWN:    mplayer_put_key(KEY_DOWN);      break;
-                case VK_TAB:     mplayer_put_key(KEY_TAB);       break;
-                case VK_CONTROL: mplayer_put_key(KEY_CTRL);      break;
-                case VK_BACK:    mplayer_put_key(KEY_BS);        break;
-                case VK_DELETE:  mplayer_put_key(KEY_DELETE);    break;
-                case VK_INSERT:  mplayer_put_key(KEY_INSERT);    break;
-                case VK_HOME:    mplayer_put_key(KEY_HOME);      break;
-                case VK_END:     mplayer_put_key(KEY_END);       break;
-                case VK_PRIOR:   mplayer_put_key(KEY_PAGE_UP);   break;
-                case VK_NEXT:    mplayer_put_key(KEY_PAGE_DOWN); break;
-                case VK_ESCAPE:  mplayer_put_key(KEY_ESC);       break;
-            }
+            mpkey = lookup_keymap_table(vk_map, wParam);
+            if (mpkey)
+                mplayer_put_key(mpkey);
             break;
         case WM_CHAR:
             mplayer_put_key(wParam);
--- a/libvo/x11_common.c	Sat Dec 20 11:49:00 2008 +0000
+++ b/libvo/x11_common.c	Sat Dec 20 11:52:11 2008 +0000
@@ -543,47 +543,23 @@
 #include "wskeys.h"
 
 #ifdef XF86XK_AudioPause
+static const struct keymap keysym_map[] = {
+    {XF86XK_MenuKB, KEY_MENU},
+    {XF86XK_AudioPlay, KEY_PLAY}, {XF86XK_AudioPause, KEY_PAUSE}, {XF86XK_AudioStop, KEY_STOP},
+    {XF86XK_AudioPrev, KEY_PREV}, {XF86XK_AudioNext, KEY_NEXT},
+    {XF86XK_AudioMute, KEY_MUTE}, {XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN}, {XF86XK_AudioRaiseVolume, KEY_VOLUME_UP},
+    {0, 0}
+};
+
 static void vo_x11_putkey_ext(int keysym)
 {
-    switch (keysym)
-    {
-        case XF86XK_MenuKB:
-            mplayer_put_key(KEY_MENU);
-            break;
-        case XF86XK_AudioPlay:
-            mplayer_put_key(KEY_PLAY);
-            break;
-        case XF86XK_AudioPause:
-            mplayer_put_key(KEY_PAUSE);
-            break;
-        case XF86XK_AudioStop:
-            mplayer_put_key(KEY_STOP);
-            break;
-        case XF86XK_AudioPrev:
-            mplayer_put_key(KEY_PREV);
-            break;
-        case XF86XK_AudioNext:
-            mplayer_put_key(KEY_NEXT);
-            break;
-        case XF86XK_AudioMute:
-            mplayer_put_key(KEY_MUTE);
-            break;
-        case XF86XK_AudioLowerVolume:
-            mplayer_put_key(KEY_VOLUME_DOWN);
-            break;
-        case XF86XK_AudioRaiseVolume:
-            mplayer_put_key(KEY_VOLUME_UP);
-            break;
-        default:
-            break;
-    }
+    int mpkey = lookup_keymap_table(keysym_map, keysym);
+    if (mpkey)
+        mplayer_put_key(mpkey);
 }
 #endif
 
-struct {
-    int x11key;
-    int mpkey;
-} static const keymap[] = {
+static const struct keymap keymap[] = {
     // special keys
     {wsEscape, KEY_ESC}, {wsBackSpace, KEY_BS}, {wsTab, KEY_TAB}, {wsEnter, KEY_ENTER},
 
@@ -629,9 +605,8 @@
         (key >  0   && key <  256 && strchr(passthrough_keys, key)))
         mpkey = key;
 
-    for (i = 0; !mpkey && keymap[i].x11key; i++)
-        if (keymap[i].x11key == key)
-            mpkey = keymap[i].mpkey;
+    if (!mpkey)
+        lookup_keymap_table(keymap, key);
 
     if (mpkey)
         mplayer_put_key(mpkey);