Patchwork [v6,6/7] add functions to convert key/code to index of mapping table

login
register
mail settings
Submitter Amos Kong
Date Aug. 3, 2012, 2:48 a.m.
Message ID <1343962122-26696-7-git-send-email-akong@redhat.com>
Download mbox | patch
Permalink /patch/174851/
State New
Headers show

Comments

Amos Kong - Aug. 3, 2012, 2:48 a.m.
Those two help functions will return Q_KEY_CODE_MAX
if the code/key is invalid.

Signed-off-by: Amos Kong <akong@redhat.com>
---
 console.h |    5 +++++
 input.c   |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 0 deletions(-)

Patch

diff --git a/console.h b/console.h
index 4334db5..c702b23 100644
--- a/console.h
+++ b/console.h
@@ -6,6 +6,7 @@ 
 #include "notify.h"
 #include "monitor.h"
 #include "trace.h"
+#include "qapi-types.h"
 
 /* keyboard/mouse support */
 
@@ -397,4 +398,8 @@  static inline int vnc_display_pw_expire(DisplayState *ds, time_t expires)
 /* curses.c */
 void curses_display_init(DisplayState *ds, int full_screen);
 
+/* input.c */
+int index_from_key(const char *key);
+int index_from_keycode(int code);
+
 #endif
diff --git a/input.c b/input.c
index 680d756..2518c15 100644
--- a/input.c
+++ b/input.c
@@ -183,6 +183,44 @@  static const int key_defs[] = {
     [Q_KEY_CODE_MAX] = 0,
 };
 
+int index_from_key(const char *key)
+{
+    int i, keycode;
+    char *endp;
+
+    for (i = 0; QKeyCode_lookup[i] != NULL; i++) {
+        if (!strcmp(key, QKeyCode_lookup[i])) {
+            break;
+        }
+    }
+
+    if (strstart(key, "0x", NULL)) {
+        keycode = strtoul(key, &endp, 0);
+        if (*endp == '\0' && keycode >= 0x01 && keycode <= 0xff) {
+            for (i = 0; i < Q_KEY_CODE_MAX; i++) {
+                if (keycode == key_defs[i]) {
+                    break;
+                }
+            }
+        }
+    }
+
+    /* Return Q_KEY_CODE_MAX if the key is invalid */
+    return i;
+}
+
+int index_from_keycode(int code)
+{
+    int i;
+    for (i = 0; i < Q_KEY_CODE_MAX; i++) {
+        if (key_defs[i] == code) {
+            break;
+        }
+    }
+    /* Return Q_KEY_CODE_MAX if the code is invalid */
+    return i;
+}
+
 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
 {
     qemu_put_kbd_event_opaque = opaque;