bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU keysym to keycode conversion using rdesktop keymaps |
| 3 | * |
| 4 | * Copyright (c) 2004 Johannes Schindelin |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | static int get_keysym(const char *name) |
| 26 | { |
| 27 | name2keysym_t *p; |
| 28 | for(p = name2keysym; p->name != NULL; p++) { |
| 29 | if (!strcmp(p->name, name)) |
| 30 | return p->keysym; |
| 31 | } |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | #define MAX_NORMAL_KEYCODE 512 |
| 36 | #define MAX_EXTRA_COUNT 256 |
| 37 | typedef struct { |
| 38 | uint16_t keysym2keycode[MAX_NORMAL_KEYCODE]; |
| 39 | struct { |
| 40 | int keysym; |
| 41 | uint16_t keycode; |
| 42 | } keysym2keycode_extra[MAX_EXTRA_COUNT]; |
| 43 | int extra_count; |
| 44 | } kbd_layout_t; |
| 45 | |
| 46 | static kbd_layout_t *parse_keyboard_layout(const char *language, |
| 47 | kbd_layout_t * k) |
| 48 | { |
| 49 | FILE *f; |
| 50 | char file_name[1024]; |
| 51 | char line[1024]; |
| 52 | int len; |
| 53 | |
| 54 | snprintf(file_name, sizeof(file_name), |
| 55 | "%s/keymaps/%s", bios_dir, language); |
| 56 | |
| 57 | if (!k) |
| 58 | k = qemu_mallocz(sizeof(kbd_layout_t)); |
| 59 | if (!k) |
| 60 | return 0; |
| 61 | if (!(f = fopen(file_name, "r"))) { |
| 62 | fprintf(stderr, |
| 63 | "Could not read keymap file: '%s'\n", file_name); |
| 64 | return 0; |
| 65 | } |
| 66 | for(;;) { |
| 67 | if (fgets(line, 1024, f) == NULL) |
| 68 | break; |
| 69 | len = strlen(line); |
| 70 | if (len > 0 && line[len - 1] == '\n') |
| 71 | line[len - 1] = '\0'; |
| 72 | if (line[0] == '#') |
| 73 | continue; |
| 74 | if (!strncmp(line, "map ", 4)) |
| 75 | continue; |
| 76 | if (!strncmp(line, "include ", 8)) { |
| 77 | parse_keyboard_layout(line + 8, k); |
| 78 | } else { |
| 79 | char *end_of_keysym = line; |
| 80 | while (*end_of_keysym != 0 && *end_of_keysym != ' ') |
| 81 | end_of_keysym++; |
| 82 | if (*end_of_keysym) { |
| 83 | int keysym; |
| 84 | *end_of_keysym = 0; |
| 85 | keysym = get_keysym(line); |
| 86 | if (keysym == 0) { |
| 87 | // fprintf(stderr, "Warning: unknown keysym %s\n", line); |
| 88 | } else { |
| 89 | const char *rest = end_of_keysym + 1; |
| 90 | int keycode = strtol(rest, NULL, 0); |
| 91 | /* if(keycode&0x80) |
| 92 | keycode=(keycode<<8)^0x80e0; */ |
| 93 | if (keysym < MAX_NORMAL_KEYCODE) { |
| 94 | //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode); |
| 95 | k->keysym2keycode[keysym] = keycode; |
| 96 | } else { |
| 97 | if (k->extra_count >= MAX_EXTRA_COUNT) { |
| 98 | fprintf(stderr, |
| 99 | "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n", |
| 100 | line, keysym); |
| 101 | } else { |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 102 | #if 0 |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 103 | fprintf(stderr, "Setting %d: %d,%d\n", |
| 104 | k->extra_count, keysym, keycode); |
bellard | 2423686 | 2006-04-30 21:28:36 +0000 | [diff] [blame] | 105 | #endif |
bellard | 3d11d0e | 2004-12-12 16:56:30 +0000 | [diff] [blame] | 106 | k->keysym2keycode_extra[k->extra_count]. |
| 107 | keysym = keysym; |
| 108 | k->keysym2keycode_extra[k->extra_count]. |
| 109 | keycode = keycode; |
| 110 | k->extra_count++; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | fclose(f); |
| 118 | return k; |
| 119 | } |
| 120 | |
| 121 | static void *init_keyboard_layout(const char *language) |
| 122 | { |
| 123 | return parse_keyboard_layout(language, 0); |
| 124 | } |
| 125 | |
| 126 | static int keysym2scancode(void *kbd_layout, int keysym) |
| 127 | { |
| 128 | kbd_layout_t *k = kbd_layout; |
| 129 | if (keysym < MAX_NORMAL_KEYCODE) { |
| 130 | if (k->keysym2keycode[keysym] == 0) |
| 131 | fprintf(stderr, "Warning: no scancode found for keysym %d\n", |
| 132 | keysym); |
| 133 | return k->keysym2keycode[keysym]; |
| 134 | } else { |
| 135 | int i; |
| 136 | #ifdef XK_ISO_Left_Tab |
| 137 | if (keysym == XK_ISO_Left_Tab) |
| 138 | keysym = XK_Tab; |
| 139 | #endif |
| 140 | for (i = 0; i < k->extra_count; i++) |
| 141 | if (k->keysym2keycode_extra[i].keysym == keysym) |
| 142 | return k->keysym2keycode_extra[i].keycode; |
| 143 | } |
| 144 | return 0; |
| 145 | } |