[MPlayer-dev-eng] [PATCH] Add support for binding any key in UTF-8 range.

Reimar Döffinger Reimar.Doeffinger at gmx.de
Sun Mar 10 16:20:37 CET 2013


X11 part is a bit hackish since we need to use
setlocale to get desired behaviour for XLookupString.
---
 input/input.c      |    6 +++++-
 libvo/x11_common.c |   23 ++++++++++++++++++++---
 osdep/getch2.c     |   10 ++++++++++
 osdep/keycodes.h   |    4 ++--
 4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/input/input.c b/input/input.c
index 0e63127..2ffac8b 100644
--- a/input/input.c
+++ b/input/input.c
@@ -38,6 +38,7 @@
 #include "osdep/getch2.h"
 #include "osdep/keycodes.h"
 #include "osdep/timer.h"
+#include "libavutil/common.h"
 #include "libavutil/avstring.h"
 #include "mp_msg.h"
 #include "help_mp.h"
@@ -1474,6 +1475,7 @@ mp_input_get_key_name(int key) {
 
 int
 mp_input_get_key_from_name(const char *name) {
+  uint32_t utf8 = 0;
   int i,ret = 0,len = strlen(name);
   if(len == 1) { // Direct key code
     ret = (unsigned char)name[0];
@@ -1486,7 +1488,9 @@ mp_input_get_key_from_name(const char *name) {
       return key_names[i].key;
   }
 
-  return -1;
+  GET_UTF8(utf8, (uint8_t)*name++, return -1;)
+
+  return *name == 0 && utf8 < KEY_BASE ? utf8 : -1;
 }
 
 static int
diff --git a/libvo/x11_common.c b/libvo/x11_common.c
index b9a80bf..7f8b070 100644
--- a/libvo/x11_common.c
+++ b/libvo/x11_common.c
@@ -21,6 +21,7 @@
 #include <math.h>
 #include <inttypes.h>
 #include <limits.h>
+#include <locale.h>
 
 #include "config.h"
 #include "mp_msg.h"
@@ -427,6 +428,8 @@ int vo_init(void)
         return 1;               // already called
     }
 
+    // Required so that XLookupString returns UTF-8
+    setlocale(LC_CTYPE, "en_US.utf8");
     XSetErrorHandler(x11_errorhandler);
 
     dispName = XDisplayName(mDisplayName);
@@ -810,11 +813,22 @@ static int check_resize(void)
     return rc;
 }
 
+static int to_utf8(const uint8_t *in)
+{
+    uint32_t v = 0;
+    GET_UTF8(v, *in++, goto err;)
+    if (*in || v >= KEY_BASE)
+        goto err;
+    return v;
+err:
+    return 0;
+}
+
 int vo_x11_check_events(Display * mydisplay)
 {
     int ret = 0;
     XEvent Event;
-    char buf[100];
+    uint8_t buf[16] = {0};
     KeySym keySym;
     static XComposeStatus stat;
     static int ctrl_state;
@@ -852,7 +866,7 @@ int vo_x11_check_events(Display * mydisplay)
             case KeyPress:
             case KeyRelease:
                 {
-                    int key;
+                    int key, utf8;
 
 #ifdef CONFIG_GUI
                     if ( use_gui ) { break; }
@@ -863,6 +877,8 @@ int vo_x11_check_events(Display * mydisplay)
                     key =
                         ((keySym & 0xff00) !=
                          0 ? ((keySym & 0x00ff) + 256) : (keySym));
+                    utf8 = to_utf8(buf);
+                    if (utf8) key = 0;
                     if (key == wsLeftCtrl || key == wsRightCtrl) {
                         ctrl_state = Event.type == KeyPress;
                         mplayer_put_key(KEY_CTRL |
@@ -880,7 +896,8 @@ int vo_x11_check_events(Display * mydisplay)
                             (ctrl_state ? MP_KEY_DOWN : 0));
                     }
                     if (!vo_x11_putkey_ext(keySym)) {
-                        vo_x11_putkey(key);
+                        if (utf8) mplayer_put_key(utf8);
+                        else vo_x11_putkey(key);
                     }
                     ret |= VO_EVENT_KEYPRESS;
                 }
diff --git a/osdep/getch2.c b/osdep/getch2.c
index ced7a94..1c2e9fe 100644
--- a/osdep/getch2.c
+++ b/osdep/getch2.c
@@ -59,6 +59,7 @@
 #include "mp_fifo.h"
 #include "keycodes.h"
 #include "getch2.h"
+#include "libavutil/common.h"
 
 #ifdef HAVE_TERMIOS
 static struct termios tio_orig;
@@ -200,6 +201,14 @@ void getch2(void)
                         len = 2;
                 }
                 code = KEY_ENTER;
+            } else if (code >= 0xc0) {
+                uint32_t utf8 = 0;
+                i = 0;
+                GET_UTF8(utf8, (i < getch2_len ? getch2_buf[i++] : 0), goto not_utf8;)
+                if (utf8 < KEY_BASE) {
+                    code = utf8;
+                    len = i;
+                }
             }
         }
         else if (getch2_len > 1) {
@@ -261,6 +270,7 @@ void getch2(void)
                 }
             }
         }
+    not_utf8:
     found:
         getch2_len -= len;
         for (i = 0; i < getch2_len; i++)
diff --git a/osdep/keycodes.h b/osdep/keycodes.h
index 2393a06..78e509e 100644
--- a/osdep/keycodes.h
+++ b/osdep/keycodes.h
@@ -23,7 +23,7 @@
 #ifndef MPLAYER_KEYCODES_H
 #define MPLAYER_KEYCODES_H
 
-#define KEY_BASE 0x100
+#define KEY_BASE 0x1000000
 
 enum {
     KEY_TAB = 9,
@@ -76,7 +76,7 @@ enum {
     KEY_VOLUME_DOWN,
     KEY_MUTE,
     /* Special internal/virtual keys */
-    KEY_CLOSE_WIN = 0x1000,
+    KEY_CLOSE_WIN = KEY_BASE + 0x1000,
 };
 
 /* Control keys short name */
-- 
1.7.10.4



More information about the MPlayer-dev-eng mailing list