Make keysym tables const
[qemu] / keymaps.c
index bd89328..38e50f0 100644 (file)
--- a/keymaps.c
+++ b/keymaps.c
@@ -1,8 +1,8 @@
 /*
  * QEMU keysym to keycode conversion using rdesktop keymaps
- * 
+ *
  * Copyright (c) 2004 Johannes Schindelin
- * 
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
@@ -24,7 +24,7 @@
 
 static int get_keysym(const char *name)
 {
-    name2keysym_t *p;
+    const name2keysym_t *p;
     for(p = name2keysym; p->name != NULL; p++) {
         if (!strcmp(p->name, name))
             return p->keysym;
@@ -32,6 +32,12 @@ static int get_keysym(const char *name)
     return 0;
 }
 
+struct key_range {
+    int start;
+    int end;
+    struct key_range *next;
+};
+
 #define MAX_NORMAL_KEYCODE 512
 #define MAX_EXTRA_COUNT 256
 typedef struct {
@@ -41,8 +47,34 @@ typedef struct {
        uint16_t keycode;
     } keysym2keycode_extra[MAX_EXTRA_COUNT];
     int extra_count;
+    struct key_range *keypad_range;
+    struct key_range *numlock_range;
 } kbd_layout_t;
 
+static void add_to_key_range(struct key_range **krp, int code) {
+    struct key_range *kr;
+    for (kr = *krp; kr; kr = kr->next) {
+       if (code >= kr->start && code <= kr->end)
+           break;
+       if (code == kr->start - 1) {
+           kr->start--;
+           break;
+       }
+       if (code == kr->end + 1) {
+           kr->end++;
+           break;
+       }
+    }
+    if (kr == NULL) {
+       kr = qemu_mallocz(sizeof(*kr));
+       if (kr) {
+           kr->start = kr->end = code;
+           kr->next = *krp;
+           *krp = kr;
+       }
+    }
+}
+
 static kbd_layout_t *parse_keyboard_layout(const char *language,
                                           kbd_layout_t * k)
 {
@@ -87,7 +119,15 @@ static kbd_layout_t *parse_keyboard_layout(const char *language,
                     //             fprintf(stderr, "Warning: unknown keysym %s\n", line);
                } else {
                    const char *rest = end_of_keysym + 1;
-                   int keycode = strtol(rest, NULL, 0);
+                   char *rest2;
+                   int keycode = strtol(rest, &rest2, 0);
+
+                   if (rest && strstr(rest, "numlock")) {
+                       add_to_key_range(&k->keypad_range, keycode);
+                       add_to_key_range(&k->numlock_range, keysym);
+                       //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
+                   }
+
                    /* if(keycode&0x80)
                       keycode=(keycode<<8)^0x80e0; */
                    if (keysym < MAX_NORMAL_KEYCODE) {
@@ -143,3 +183,25 @@ static int keysym2scancode(void *kbd_layout, int keysym)
     }
     return 0;
 }
+
+static inline int keycode_is_keypad(void *kbd_layout, int keycode)
+{
+    kbd_layout_t *k = kbd_layout;
+    struct key_range *kr;
+
+    for (kr = k->keypad_range; kr; kr = kr->next)
+        if (keycode >= kr->start && keycode <= kr->end)
+            return 1;
+    return 0;
+}
+
+static inline int keysym_is_numlock(void *kbd_layout, int keysym)
+{
+    kbd_layout_t *k = kbd_layout;
+    struct key_range *kr;
+
+    for (kr = k->numlock_range; kr; kr = kr->next)
+        if (keysym >= kr->start && keysym <= kr->end)
+            return 1;
+    return 0;
+}