keymaps.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "keymaps.h"
  25. #include "sysemu/sysemu.h"
  26. static int get_keysym(const name2keysym_t *table,
  27. const char *name)
  28. {
  29. const name2keysym_t *p;
  30. for(p = table; p->name != NULL; p++) {
  31. if (!strcmp(p->name, name))
  32. return p->keysym;
  33. }
  34. if (name[0] == 'U' && strlen(name) == 5) { /* try unicode Uxxxx */
  35. char *end;
  36. int ret = (int)strtoul(name + 1, &end, 16);
  37. if (*end == '\0' && ret > 0)
  38. return ret;
  39. }
  40. return 0;
  41. }
  42. static void add_to_key_range(struct key_range **krp, int code) {
  43. struct key_range *kr;
  44. for (kr = *krp; kr; kr = kr->next) {
  45. if (code >= kr->start && code <= kr->end)
  46. break;
  47. if (code == kr->start - 1) {
  48. kr->start--;
  49. break;
  50. }
  51. if (code == kr->end + 1) {
  52. kr->end++;
  53. break;
  54. }
  55. }
  56. if (kr == NULL) {
  57. kr = g_malloc0(sizeof(*kr));
  58. kr->start = kr->end = code;
  59. kr->next = *krp;
  60. *krp = kr;
  61. }
  62. }
  63. static void add_keysym(char *line, int keysym, int keycode, kbd_layout_t *k) {
  64. if (keysym < MAX_NORMAL_KEYCODE) {
  65. //fprintf(stderr,"Setting keysym %s (%d) to %d\n",line,keysym,keycode);
  66. k->keysym2keycode[keysym] = keycode;
  67. } else {
  68. if (k->extra_count >= MAX_EXTRA_COUNT) {
  69. fprintf(stderr,
  70. "Warning: Could not assign keysym %s (0x%x) because of memory constraints.\n",
  71. line, keysym);
  72. } else {
  73. #if 0
  74. fprintf(stderr, "Setting %d: %d,%d\n",
  75. k->extra_count, keysym, keycode);
  76. #endif
  77. k->keysym2keycode_extra[k->extra_count].
  78. keysym = keysym;
  79. k->keysym2keycode_extra[k->extra_count].
  80. keycode = keycode;
  81. k->extra_count++;
  82. }
  83. }
  84. }
  85. static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
  86. const char *language,
  87. kbd_layout_t * k)
  88. {
  89. FILE *f;
  90. char * filename;
  91. char line[1024];
  92. int len;
  93. filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
  94. f = filename ? fopen(filename, "r") : NULL;
  95. g_free(filename);
  96. if (!f) {
  97. fprintf(stderr,
  98. "Could not read keymap file: '%s'\n", language);
  99. return NULL;
  100. }
  101. if (!k)
  102. k = g_malloc0(sizeof(kbd_layout_t));
  103. for(;;) {
  104. if (fgets(line, 1024, f) == NULL)
  105. break;
  106. len = strlen(line);
  107. if (len > 0 && line[len - 1] == '\n')
  108. line[len - 1] = '\0';
  109. if (line[0] == '#')
  110. continue;
  111. if (!strncmp(line, "map ", 4))
  112. continue;
  113. if (!strncmp(line, "include ", 8)) {
  114. parse_keyboard_layout(table, line + 8, k);
  115. } else {
  116. char *end_of_keysym = line;
  117. while (*end_of_keysym != 0 && *end_of_keysym != ' ')
  118. end_of_keysym++;
  119. if (*end_of_keysym) {
  120. int keysym;
  121. *end_of_keysym = 0;
  122. keysym = get_keysym(table, line);
  123. if (keysym == 0) {
  124. // fprintf(stderr, "Warning: unknown keysym %s\n", line);
  125. } else {
  126. const char *rest = end_of_keysym + 1;
  127. int keycode = strtol(rest, NULL, 0);
  128. if (strstr(rest, "numlock")) {
  129. add_to_key_range(&k->keypad_range, keycode);
  130. add_to_key_range(&k->numlock_range, keysym);
  131. //fprintf(stderr, "keypad keysym %04x keycode %d\n", keysym, keycode);
  132. }
  133. if (strstr(rest, "shift")) {
  134. keycode |= SCANCODE_SHIFT;
  135. }
  136. if (strstr(rest, "altgr")) {
  137. keycode |= SCANCODE_ALTGR;
  138. }
  139. if (strstr(rest, "ctrl")) {
  140. keycode |= SCANCODE_CTRL;
  141. }
  142. add_keysym(line, keysym, keycode, k);
  143. if (strstr(rest, "addupper")) {
  144. char *c;
  145. for (c = line; *c; c++)
  146. *c = qemu_toupper(*c);
  147. keysym = get_keysym(table, line);
  148. if (keysym)
  149. add_keysym(line, keysym, keycode | SCANCODE_SHIFT, k);
  150. }
  151. }
  152. }
  153. }
  154. }
  155. fclose(f);
  156. return k;
  157. }
  158. void *init_keyboard_layout(const name2keysym_t *table, const char *language)
  159. {
  160. return parse_keyboard_layout(table, language, NULL);
  161. }
  162. int keysym2scancode(void *kbd_layout, int keysym)
  163. {
  164. kbd_layout_t *k = kbd_layout;
  165. if (keysym < MAX_NORMAL_KEYCODE) {
  166. if (k->keysym2keycode[keysym] == 0)
  167. fprintf(stderr, "Warning: no scancode found for keysym %d\n",
  168. keysym);
  169. return k->keysym2keycode[keysym];
  170. } else {
  171. int i;
  172. #ifdef XK_ISO_Left_Tab
  173. if (keysym == XK_ISO_Left_Tab)
  174. keysym = XK_Tab;
  175. #endif
  176. for (i = 0; i < k->extra_count; i++)
  177. if (k->keysym2keycode_extra[i].keysym == keysym)
  178. return k->keysym2keycode_extra[i].keycode;
  179. }
  180. return 0;
  181. }
  182. int keycode_is_keypad(void *kbd_layout, int keycode)
  183. {
  184. kbd_layout_t *k = kbd_layout;
  185. struct key_range *kr;
  186. for (kr = k->keypad_range; kr; kr = kr->next)
  187. if (keycode >= kr->start && keycode <= kr->end)
  188. return 1;
  189. return 0;
  190. }
  191. int keysym_is_numlock(void *kbd_layout, int keysym)
  192. {
  193. kbd_layout_t *k = kbd_layout;
  194. struct key_range *kr;
  195. for (kr = k->numlock_range; kr; kr = kr->next)
  196. if (keysym >= kr->start && keysym <= kr->end)
  197. return 1;
  198. return 0;
  199. }