keylayouts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/term.h>
  19. #include <grub/err.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/env.h>
  23. #include <grub/time.h>
  24. #include <grub/dl.h>
  25. #include <grub/keyboard_layouts.h>
  26. #include <grub/command.h>
  27. #include <grub/i18n.h>
  28. #include <grub/file.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. static struct grub_keyboard_layout layout_us = {
  31. .keyboard_map = {
  32. /* Keyboard errors. Handled by driver. */
  33. /* 0x00 */ 0, 0, 0, 0,
  34. /* 0x04 */ 'a', 'b', 'c', 'd',
  35. /* 0x08 */ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  36. /* 0x10 */ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  37. /* 0x18 */ 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
  38. /* 0x20 */ '3', '4', '5', '6', '7', '8', '9', '0',
  39. /* 0x28 */ '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
  40. /* According to usage table 0x31 should be mapped to '/'
  41. but testing with real keyboard shows that 0x32 is remapped to '/'.
  42. Map 0x31 to 0.
  43. */
  44. /* 0x30 */ ']', 0, '\\', ';', '\'', '`', ',', '.',
  45. /* 0x39 is CapsLock. Handled by driver. */
  46. /* 0x38 */ '/', 0, GRUB_TERM_KEY_F1, GRUB_TERM_KEY_F2,
  47. /* 0x3c */ GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4,
  48. /* 0x3e */ GRUB_TERM_KEY_F5, GRUB_TERM_KEY_F6,
  49. /* 0x40 */ GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8,
  50. /* 0x42 */ GRUB_TERM_KEY_F9, GRUB_TERM_KEY_F10,
  51. /* 0x44 */ GRUB_TERM_KEY_F11, GRUB_TERM_KEY_F12,
  52. /* PrtScr and ScrollLock. Not handled yet. */
  53. /* 0x46 */ 0, 0,
  54. /* 0x48 is Pause. Not handled yet. */
  55. /* 0x48 */ 0, GRUB_TERM_KEY_INSERT,
  56. /* 0x4a */ GRUB_TERM_KEY_HOME, GRUB_TERM_KEY_PPAGE,
  57. /* 0x4c */ GRUB_TERM_KEY_DC, GRUB_TERM_KEY_END,
  58. /* 0x4e */ GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_RIGHT,
  59. /* 0x50 */ GRUB_TERM_KEY_LEFT, GRUB_TERM_KEY_DOWN,
  60. /* 0x53 is NumLock. Handled by driver. */
  61. /* 0x52 */ GRUB_TERM_KEY_UP, 0,
  62. /* 0x54 */ '/', '*',
  63. /* 0x56 */ '-', '+',
  64. /* 0x58 */ '\n', GRUB_TERM_KEY_END,
  65. /* 0x5a */ GRUB_TERM_KEY_DOWN, GRUB_TERM_KEY_NPAGE,
  66. /* 0x5c */ GRUB_TERM_KEY_LEFT, GRUB_TERM_KEY_CENTER,
  67. /* 0x5e */ GRUB_TERM_KEY_RIGHT, GRUB_TERM_KEY_HOME,
  68. /* 0x60 */ GRUB_TERM_KEY_UP, GRUB_TERM_KEY_PPAGE,
  69. /* 0x62 */ GRUB_TERM_KEY_INSERT, GRUB_TERM_KEY_DC,
  70. /* 0x64 */ '\\'
  71. },
  72. .keyboard_map_shift = {
  73. /* Keyboard errors. Handled by driver. */
  74. /* 0x00 */ 0, 0, 0, 0,
  75. /* 0x04 */ 'A', 'B', 'C', 'D',
  76. /* 0x08 */ 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
  77. /* 0x10 */ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  78. /* 0x18 */ 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
  79. /* 0x20 */ '#', '$', '%', '^', '&', '*', '(', ')',
  80. /* 0x28 */ '\n' | GRUB_TERM_SHIFT, '\e' | GRUB_TERM_SHIFT,
  81. /* 0x2a */ '\b' | GRUB_TERM_SHIFT, '\t' | GRUB_TERM_SHIFT,
  82. /* 0x2c */ ' ' | GRUB_TERM_SHIFT, '_', '+', '{',
  83. /* According to usage table 0x31 should be mapped to '/'
  84. but testing with real keyboard shows that 0x32 is remapped to '/'.
  85. Map 0x31 to 0.
  86. */
  87. /* 0x30 */ '}', 0, '|', ':', '"', '~', '<', '>',
  88. /* 0x39 is CapsLock. Handled by driver. */
  89. /* 0x38 */ '?', 0,
  90. /* 0x3a */ GRUB_TERM_KEY_F1 | GRUB_TERM_SHIFT,
  91. /* 0x3b */ GRUB_TERM_KEY_F2 | GRUB_TERM_SHIFT,
  92. /* 0x3c */ GRUB_TERM_KEY_F3 | GRUB_TERM_SHIFT,
  93. /* 0x3d */ GRUB_TERM_KEY_F4 | GRUB_TERM_SHIFT,
  94. /* 0x3e */ GRUB_TERM_KEY_F5 | GRUB_TERM_SHIFT,
  95. /* 0x3f */ GRUB_TERM_KEY_F6 | GRUB_TERM_SHIFT,
  96. /* 0x40 */ GRUB_TERM_KEY_F7 | GRUB_TERM_SHIFT,
  97. /* 0x41 */ GRUB_TERM_KEY_F8 | GRUB_TERM_SHIFT,
  98. /* 0x42 */ GRUB_TERM_KEY_F9 | GRUB_TERM_SHIFT,
  99. /* 0x43 */ GRUB_TERM_KEY_F10 | GRUB_TERM_SHIFT,
  100. /* 0x44 */ GRUB_TERM_KEY_F11 | GRUB_TERM_SHIFT,
  101. /* 0x45 */ GRUB_TERM_KEY_F12 | GRUB_TERM_SHIFT,
  102. /* PrtScr and ScrollLock. Not handled yet. */
  103. /* 0x46 */ 0, 0,
  104. /* 0x48 is Pause. Not handled yet. */
  105. /* 0x48 */ 0, GRUB_TERM_KEY_INSERT | GRUB_TERM_SHIFT,
  106. /* 0x4a */ GRUB_TERM_KEY_HOME | GRUB_TERM_SHIFT,
  107. /* 0x4b */ GRUB_TERM_KEY_PPAGE | GRUB_TERM_SHIFT,
  108. /* 0x4c */ GRUB_TERM_KEY_DC | GRUB_TERM_SHIFT,
  109. /* 0x4d */ GRUB_TERM_KEY_END | GRUB_TERM_SHIFT,
  110. /* 0x4e */ GRUB_TERM_KEY_NPAGE | GRUB_TERM_SHIFT,
  111. /* 0x4f */ GRUB_TERM_KEY_RIGHT | GRUB_TERM_SHIFT,
  112. /* 0x50 */ GRUB_TERM_KEY_LEFT | GRUB_TERM_SHIFT,
  113. /* 0x51 */ GRUB_TERM_KEY_DOWN | GRUB_TERM_SHIFT,
  114. /* 0x53 is NumLock. Handled by driver. */
  115. /* 0x52 */ GRUB_TERM_KEY_UP | GRUB_TERM_SHIFT, 0,
  116. /* 0x54 */ '/', '*',
  117. /* 0x56 */ '-', '+',
  118. /* 0x58 */ '\n' | GRUB_TERM_SHIFT, '1', '2', '3', '4', '5','6', '7',
  119. /* 0x60 */ '8', '9', '0', '.', '|'
  120. }
  121. };
  122. static struct grub_keyboard_layout *grub_current_layout = &layout_us;
  123. static int
  124. map_key_core (int code, int status, int *alt_gr_consumed)
  125. {
  126. *alt_gr_consumed = 0;
  127. if (code >= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
  128. return 0;
  129. if (status & GRUB_TERM_STATUS_RALT)
  130. {
  131. if (status & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
  132. {
  133. if (grub_current_layout->keyboard_map_shift_l3[code])
  134. {
  135. *alt_gr_consumed = 1;
  136. return grub_current_layout->keyboard_map_shift_l3[code];
  137. }
  138. }
  139. else if (grub_current_layout->keyboard_map_l3[code])
  140. {
  141. *alt_gr_consumed = 1;
  142. return grub_current_layout->keyboard_map_l3[code];
  143. }
  144. }
  145. if (status & (GRUB_TERM_STATUS_LSHIFT | GRUB_TERM_STATUS_RSHIFT))
  146. return grub_current_layout->keyboard_map_shift[code];
  147. else
  148. return grub_current_layout->keyboard_map[code];
  149. }
  150. unsigned
  151. grub_term_map_key (grub_keyboard_key_t code, int status)
  152. {
  153. int alt_gr_consumed = 0;
  154. int key;
  155. if (code >= 0x59 && code <= 0x63 && (status & GRUB_TERM_STATUS_NUM))
  156. {
  157. if (status & (GRUB_TERM_STATUS_RSHIFT | GRUB_TERM_STATUS_LSHIFT))
  158. status &= ~(GRUB_TERM_STATUS_RSHIFT | GRUB_TERM_STATUS_LSHIFT);
  159. else
  160. status |= GRUB_TERM_STATUS_RSHIFT;
  161. }
  162. key = map_key_core (code, status, &alt_gr_consumed);
  163. if (key == 0 || key == GRUB_TERM_SHIFT) {
  164. grub_printf ("Unknown key 0x%x detected\n", code);
  165. return GRUB_TERM_NO_KEY;
  166. }
  167. if (status & GRUB_TERM_STATUS_CAPS)
  168. {
  169. if ((key >= 'a') && (key <= 'z'))
  170. key += 'A' - 'a';
  171. else if ((key >= 'A') && (key <= 'Z'))
  172. key += 'a' - 'A';
  173. }
  174. if ((status & GRUB_TERM_STATUS_LALT) ||
  175. ((status & GRUB_TERM_STATUS_RALT) && !alt_gr_consumed))
  176. key |= GRUB_TERM_ALT;
  177. if (status & (GRUB_TERM_STATUS_LCTRL | GRUB_TERM_STATUS_RCTRL))
  178. key |= GRUB_TERM_CTRL;
  179. return key;
  180. }
  181. static grub_err_t
  182. grub_cmd_keymap (struct grub_command *cmd __attribute__ ((unused)),
  183. int argc, char *argv[])
  184. {
  185. char *filename;
  186. grub_file_t file;
  187. grub_uint32_t version;
  188. grub_uint8_t magic[GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE];
  189. struct grub_keyboard_layout *newmap = NULL;
  190. unsigned i;
  191. if (argc < 1)
  192. return grub_error (GRUB_ERR_BAD_ARGUMENT, "file or layout name required");
  193. if (argv[0][0] != '(' && argv[0][0] != '/' && argv[0][0] != '+')
  194. {
  195. const char *prefix = grub_env_get ("prefix");
  196. if (!prefix)
  197. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("variable `%s' isn't set"), "prefix");
  198. filename = grub_xasprintf ("%s/layouts/%s.gkb", prefix, argv[0]);
  199. if (!filename)
  200. return grub_errno;
  201. }
  202. else
  203. filename = argv[0];
  204. file = grub_file_open (filename);
  205. if (! file)
  206. goto fail;
  207. if (grub_file_read (file, magic, sizeof (magic)) != sizeof (magic))
  208. {
  209. if (!grub_errno)
  210. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
  211. filename);
  212. goto fail;
  213. }
  214. if (grub_memcmp (magic, GRUB_KEYBOARD_LAYOUTS_FILEMAGIC,
  215. GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE) != 0)
  216. {
  217. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid magic");
  218. goto fail;
  219. }
  220. if (grub_file_read (file, &version, sizeof (version)) != sizeof (version))
  221. {
  222. if (!grub_errno)
  223. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
  224. filename);
  225. goto fail;
  226. }
  227. if (version != grub_cpu_to_le32_compile_time (GRUB_KEYBOARD_LAYOUTS_VERSION))
  228. {
  229. grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid version");
  230. goto fail;
  231. }
  232. newmap = grub_malloc (sizeof (*newmap));
  233. if (!newmap)
  234. goto fail;
  235. if (grub_file_read (file, newmap, sizeof (*newmap)) != sizeof (*newmap))
  236. {
  237. if (!grub_errno)
  238. grub_error (GRUB_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
  239. filename);
  240. goto fail;
  241. }
  242. for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map); i++)
  243. newmap->keyboard_map[i] = grub_le_to_cpu32(newmap->keyboard_map[i]);
  244. for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift); i++)
  245. newmap->keyboard_map_shift[i]
  246. = grub_le_to_cpu32(newmap->keyboard_map_shift[i]);
  247. for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_l3); i++)
  248. newmap->keyboard_map_l3[i]
  249. = grub_le_to_cpu32(newmap->keyboard_map_l3[i]);
  250. for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift_l3); i++)
  251. newmap->keyboard_map_shift_l3[i]
  252. = grub_le_to_cpu32(newmap->keyboard_map_shift_l3[i]);
  253. grub_current_layout = newmap;
  254. return GRUB_ERR_NONE;
  255. fail:
  256. if (filename != argv[0])
  257. grub_free (filename);
  258. grub_free (newmap);
  259. if (file)
  260. grub_file_close (file);
  261. return grub_errno;
  262. }
  263. static grub_command_t cmd;
  264. GRUB_MOD_INIT(keylayouts)
  265. {
  266. cmd = grub_register_command ("keymap", grub_cmd_keymap,
  267. 0, N_("Load a keyboard layout."));
  268. }
  269. GRUB_MOD_FINI(keylayouts)
  270. {
  271. grub_unregister_command (cmd);
  272. }