keymaps.c 5.7 KB

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