2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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/>.
19 #include <grub/util/misc.h>
20 #include <grub/i18n.h>
21 #include <grub/term.h>
22 #include <grub/keyboard_layouts.h>
33 #define CKBCOMP "ckbcomp"
35 static struct option options
[] = {
36 {"output", required_argument
, 0, 'o'},
37 {"help", no_argument
, 0, 'h'},
38 {"version", no_argument
, 0, 'V'},
39 {"verbose", no_argument
, 0, 'v'},
43 struct console_grub_equivalence
49 static struct console_grub_equivalence console_grub_equivalences_shift
[] = {
63 static struct console_grub_equivalence console_grub_equivalences_unshift
[] = {
64 {"KP_0", GRUB_TERM_KEY_INSERT
},
65 {"KP_1", GRUB_TERM_KEY_END
},
66 {"KP_2", GRUB_TERM_KEY_DOWN
},
67 {"KP_3", GRUB_TERM_KEY_NPAGE
},
68 {"KP_4", GRUB_TERM_KEY_LEFT
},
69 {"KP_5", GRUB_TERM_KEY_CENTER
},
70 {"KP_6", GRUB_TERM_KEY_RIGHT
},
71 {"KP_7", GRUB_TERM_KEY_HOME
},
72 {"KP_8", GRUB_TERM_KEY_UP
},
73 {"KP_9", GRUB_TERM_KEY_PPAGE
},
74 {"KP_Period", GRUB_TERM_KEY_DC
},
77 static struct console_grub_equivalence console_grub_equivalences_common
[] = {
78 {"Escape", GRUB_TERM_ESC
},
79 {"Tab", GRUB_TERM_TAB
},
80 {"Delete", GRUB_TERM_BACKSPACE
},
90 {"F1", GRUB_TERM_KEY_F1
},
91 {"F2", GRUB_TERM_KEY_F2
},
92 {"F3", GRUB_TERM_KEY_F3
},
93 {"F4", GRUB_TERM_KEY_F4
},
94 {"F5", GRUB_TERM_KEY_F5
},
95 {"F6", GRUB_TERM_KEY_F6
},
96 {"F7", GRUB_TERM_KEY_F7
},
97 {"F8", GRUB_TERM_KEY_F8
},
98 {"F9", GRUB_TERM_KEY_F9
},
99 {"F10", GRUB_TERM_KEY_F10
},
100 {"F11", GRUB_TERM_KEY_F11
},
101 {"F12", GRUB_TERM_KEY_F12
},
102 {"F13", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
},
103 {"F14", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
},
104 {"F15", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
},
105 {"F16", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
},
106 {"F17", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
},
107 {"F18", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
},
108 {"F19", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
},
109 {"F20", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
},
110 {"F21", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
},
111 {"F22", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
},
112 {"F23", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
},
113 {"F24", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
},
114 {"Console_13", GRUB_TERM_KEY_F1
| GRUB_TERM_ALT
},
115 {"Console_14", GRUB_TERM_KEY_F2
| GRUB_TERM_ALT
},
116 {"Console_15", GRUB_TERM_KEY_F3
| GRUB_TERM_ALT
},
117 {"Console_16", GRUB_TERM_KEY_F4
| GRUB_TERM_ALT
},
118 {"Console_17", GRUB_TERM_KEY_F5
| GRUB_TERM_ALT
},
119 {"Console_18", GRUB_TERM_KEY_F6
| GRUB_TERM_ALT
},
120 {"Console_19", GRUB_TERM_KEY_F7
| GRUB_TERM_ALT
},
121 {"Console_20", GRUB_TERM_KEY_F8
| GRUB_TERM_ALT
},
122 {"Console_21", GRUB_TERM_KEY_F9
| GRUB_TERM_ALT
},
123 {"Console_22", GRUB_TERM_KEY_F10
| GRUB_TERM_ALT
},
124 {"Console_23", GRUB_TERM_KEY_F11
| GRUB_TERM_ALT
},
125 {"Console_24", GRUB_TERM_KEY_F12
| GRUB_TERM_ALT
},
126 {"Console_25", GRUB_TERM_KEY_F1
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
127 {"Console_26", GRUB_TERM_KEY_F2
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
128 {"Console_27", GRUB_TERM_KEY_F3
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
129 {"Console_28", GRUB_TERM_KEY_F4
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
130 {"Console_29", GRUB_TERM_KEY_F5
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
131 {"Console_30", GRUB_TERM_KEY_F6
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
132 {"Console_31", GRUB_TERM_KEY_F7
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
133 {"Console_32", GRUB_TERM_KEY_F8
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
134 {"Console_33", GRUB_TERM_KEY_F9
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
135 {"Console_34", GRUB_TERM_KEY_F10
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
136 {"Console_35", GRUB_TERM_KEY_F11
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
137 {"Console_36", GRUB_TERM_KEY_F12
| GRUB_TERM_SHIFT
| GRUB_TERM_ALT
},
139 {"Insert", GRUB_TERM_KEY_INSERT
},
140 {"Down", GRUB_TERM_KEY_DOWN
},
141 {"Up", GRUB_TERM_KEY_UP
},
142 {"Home", GRUB_TERM_KEY_HOME
},
143 {"End", GRUB_TERM_KEY_END
},
144 {"Right", GRUB_TERM_KEY_RIGHT
},
145 {"Left", GRUB_TERM_KEY_LEFT
},
146 {"Next", GRUB_TERM_KEY_NPAGE
},
147 {"Prior", GRUB_TERM_KEY_PPAGE
},
148 {"Remove", GRUB_TERM_KEY_DC
},
151 /* "Undead" keys since no dead key support in GRUB. */
152 {"dead_acute", '\''},
153 {"dead_circumflex", '^'},
156 {"dead_diaeresis", '"'},
158 /* Following ones don't provide any useful symbols for shell. */
163 {"dead_doubleacute", 0},
165 /* Unused in GRUB. */
167 {"Scroll_Forward", 0},
168 {"Scroll_Backward", 0},
187 {"Show_Registers", 0},
188 {"Control_backslash", 0},
194 static grub_uint8_t linux_to_usb_map
[128] = {
195 /* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE
,
196 /* 0x02 */ GRUB_KEYBOARD_KEY_1
, GRUB_KEYBOARD_KEY_2
,
197 /* 0x04 */ GRUB_KEYBOARD_KEY_3
, GRUB_KEYBOARD_KEY_4
,
198 /* 0x06 */ GRUB_KEYBOARD_KEY_5
, GRUB_KEYBOARD_KEY_6
,
199 /* 0x08 */ GRUB_KEYBOARD_KEY_7
, GRUB_KEYBOARD_KEY_8
,
200 /* 0x0a */ GRUB_KEYBOARD_KEY_9
, GRUB_KEYBOARD_KEY_0
,
201 /* 0x0c */ GRUB_KEYBOARD_KEY_DASH
, GRUB_KEYBOARD_KEY_EQUAL
,
202 /* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE
, GRUB_KEYBOARD_KEY_TAB
,
203 /* 0x10 */ GRUB_KEYBOARD_KEY_Q
, GRUB_KEYBOARD_KEY_W
,
204 /* 0x12 */ GRUB_KEYBOARD_KEY_E
, GRUB_KEYBOARD_KEY_R
,
205 /* 0x14 */ GRUB_KEYBOARD_KEY_T
, GRUB_KEYBOARD_KEY_Y
,
206 /* 0x16 */ GRUB_KEYBOARD_KEY_U
, GRUB_KEYBOARD_KEY_I
,
207 /* 0x18 */ GRUB_KEYBOARD_KEY_O
, GRUB_KEYBOARD_KEY_P
,
208 /* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET
, GRUB_KEYBOARD_KEY_RBRACKET
,
209 /* 0x1c */ GRUB_KEYBOARD_KEY_ENTER
, GRUB_KEYBOARD_KEY_LEFT_CTRL
,
210 /* 0x1e */ GRUB_KEYBOARD_KEY_A
, GRUB_KEYBOARD_KEY_S
,
211 /* 0x20 */ GRUB_KEYBOARD_KEY_D
, GRUB_KEYBOARD_KEY_F
,
212 /* 0x22 */ GRUB_KEYBOARD_KEY_G
, GRUB_KEYBOARD_KEY_H
,
213 /* 0x24 */ GRUB_KEYBOARD_KEY_J
, GRUB_KEYBOARD_KEY_K
,
214 /* 0x26 */ GRUB_KEYBOARD_KEY_L
, GRUB_KEYBOARD_KEY_SEMICOLON
,
215 /* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE
, GRUB_KEYBOARD_KEY_RQUOTE
,
216 /* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT
, GRUB_KEYBOARD_KEY_BACKSLASH
,
217 /* 0x2c */ GRUB_KEYBOARD_KEY_Z
, GRUB_KEYBOARD_KEY_X
,
218 /* 0x2e */ GRUB_KEYBOARD_KEY_C
, GRUB_KEYBOARD_KEY_V
,
219 /* 0x30 */ GRUB_KEYBOARD_KEY_B
, GRUB_KEYBOARD_KEY_N
,
220 /* 0x32 */ GRUB_KEYBOARD_KEY_M
, GRUB_KEYBOARD_KEY_COMMA
,
221 /* 0x34 */ GRUB_KEYBOARD_KEY_DOT
, GRUB_KEYBOARD_KEY_SLASH
,
222 /* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT
, GRUB_KEYBOARD_KEY_NUMMUL
,
223 /* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT
, GRUB_KEYBOARD_KEY_SPACE
,
224 /* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK
, GRUB_KEYBOARD_KEY_F1
,
225 /* 0x3c */ GRUB_KEYBOARD_KEY_F2
, GRUB_KEYBOARD_KEY_F3
,
226 /* 0x3e */ GRUB_KEYBOARD_KEY_F4
, GRUB_KEYBOARD_KEY_F5
,
227 /* 0x40 */ GRUB_KEYBOARD_KEY_F6
, GRUB_KEYBOARD_KEY_F7
,
228 /* 0x42 */ GRUB_KEYBOARD_KEY_F8
, GRUB_KEYBOARD_KEY_F9
,
229 /* 0x44 */ GRUB_KEYBOARD_KEY_F10
, GRUB_KEYBOARD_KEY_NUM_LOCK
,
230 /* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK
, GRUB_KEYBOARD_KEY_NUM7
,
231 /* 0x48 */ GRUB_KEYBOARD_KEY_NUM8
, GRUB_KEYBOARD_KEY_NUM9
,
232 /* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS
, GRUB_KEYBOARD_KEY_NUM4
,
233 /* 0x4c */ GRUB_KEYBOARD_KEY_NUM5
, GRUB_KEYBOARD_KEY_NUM6
,
234 /* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS
, GRUB_KEYBOARD_KEY_NUM1
,
235 /* 0x50 */ GRUB_KEYBOARD_KEY_NUM2
, GRUB_KEYBOARD_KEY_NUM3
,
236 /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT
, GRUB_KEYBOARD_KEY_NUMDOT
,
238 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND
, GRUB_KEYBOARD_KEY_F11
,
239 /* 0x58 */ GRUB_KEYBOARD_KEY_F12
, 0,
243 /* 0x60 */ GRUB_KEYBOARD_KEY_NUMENTER
, GRUB_KEYBOARD_KEY_RIGHT_CTRL
,
244 /* 0x62 */ GRUB_KEYBOARD_KEY_NUMSLASH
, 0,
245 /* 0x64 */ GRUB_KEYBOARD_KEY_RIGHT_ALT
, 0,
246 /* 0x66 */ GRUB_KEYBOARD_KEY_HOME
, GRUB_KEYBOARD_KEY_UP
,
247 /* 0x68 */ GRUB_KEYBOARD_KEY_NPAGE
, GRUB_KEYBOARD_KEY_LEFT
,
248 /* 0x6a */ GRUB_KEYBOARD_KEY_RIGHT
, GRUB_KEYBOARD_KEY_END
,
249 /* 0x6c */ GRUB_KEYBOARD_KEY_DOWN
, GRUB_KEYBOARD_KEY_PPAGE
,
250 /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT
, GRUB_KEYBOARD_KEY_DELETE
257 fprintf (stderr
, "Try `%s --help' for more information.\n", program_name
);
260 Usage: %s [OPTIONS] LAYOUT\n\
261 -o, --output set output base name file. Default is LAYOUT.gkb\n\
262 -h, --help display this message and exit.\n\
263 -V, --version print version information and exit.\n\
264 -v, --verbose print verbose messages.\n\
266 Report bugs to <%s>.\n", program_name
, PACKAGE_BUGREPORT
);
272 add_special_keys (struct grub_keyboard_layout
*layout
)
278 lookup (char *code
, int shift
)
281 struct console_grub_equivalence
*pr
;
284 pr
= console_grub_equivalences_shift
;
286 pr
= console_grub_equivalences_unshift
;
288 for (i
= 0; pr
[i
].layout
!= NULL
; i
++)
289 if (strcmp (code
, pr
[i
].layout
) == 0)
292 for (i
= 0; console_grub_equivalences_common
[i
].layout
!= NULL
; i
++)
293 if (strcmp (code
, console_grub_equivalences_common
[i
].layout
) == 0)
294 return console_grub_equivalences_common
[i
].grub
;
296 fprintf (stderr
, "Unknown key %s\n", code
);
302 get_grub_code (char *layout_code
, int shift
)
306 if (strncmp (layout_code
, "U+", sizeof ("U+") - 1) == 0)
307 sscanf (layout_code
, "U+%x", &code
);
308 else if (strncmp (layout_code
, "+U+", sizeof ("+U+") - 1) == 0)
309 sscanf (layout_code
, "+U+%x", &code
);
311 code
= lookup (layout_code
, shift
);
316 write_file (FILE *out
, struct grub_keyboard_layout
*layout
)
318 grub_uint32_t version
;
321 version
= grub_cpu_to_le32 (GRUB_KEYBOARD_LAYOUTS_VERSION
);
323 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map
); i
++)
324 layout
->keyboard_map
[i
] = grub_cpu_to_le32(layout
->keyboard_map
[i
]);
326 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift
); i
++)
327 layout
->keyboard_map_shift
[i
]
328 = grub_cpu_to_le32(layout
->keyboard_map_shift
[i
]);
330 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_l3
); i
++)
331 layout
->keyboard_map_l3
[i
]
332 = grub_cpu_to_le32(layout
->keyboard_map_l3
[i
]);
334 for (i
= 0; i
< ARRAY_SIZE (layout
->keyboard_map_shift_l3
); i
++)
335 layout
->keyboard_map_shift_l3
[i
]
336 = grub_cpu_to_le32(layout
->keyboard_map_shift_l3
[i
]);
338 fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC
, 1,
339 GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
, out
);
340 fwrite (&version
, sizeof (version
), 1, out
);
341 fwrite (layout
, 1, sizeof (*layout
), out
);
345 write_keymaps (FILE *in
, FILE *out
)
347 struct grub_keyboard_layout layout
;
351 memset (&layout
, 0, sizeof (layout
));
353 /* Process the ckbcomp output and prepare the layouts. */
355 while (fgets (line
, sizeof (line
), in
))
357 if (strncmp (line
, "keycode", sizeof ("keycode") - 1) == 0)
359 unsigned keycode_linux
;
360 unsigned keycode_usb
;
366 sscanf (line
, "keycode %u = %60s %60s %60s %60s", &keycode_linux
,
367 normal
, shift
, normalalt
, shiftalt
);
370 if (keycode_linux
== 0x77 /* Pause */
371 /* Some obscure keys */
372 || keycode_linux
== 0x63 || keycode_linux
== 0x7d
373 || keycode_linux
== 0x7e)
376 /* Not remappable. */
377 if (keycode_linux
== 0x1d /* Left CTRL */
378 || keycode_linux
== 0x61 /* Right CTRL */
379 || keycode_linux
== 0x2a /* Left Shift. */
380 || keycode_linux
== 0x36 /* Right Shift. */
381 || keycode_linux
== 0x38 /* Left ALT. */
382 || keycode_linux
== 0x64 /* Right ALT. */
383 || keycode_linux
== 0x3a /* CapsLock. */
384 || keycode_linux
== 0x45 /* NumLock. */
385 || keycode_linux
== 0x46 /* ScrollLock. */)
388 keycode_usb
= linux_to_usb_map
[keycode_linux
];
390 || keycode_usb
>= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
392 fprintf (stderr
, "Unknown keycode 0x%02x\n", keycode_linux
);
395 if (keycode_usb
< GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE
)
397 layout
.keyboard_map
[keycode_usb
] = get_grub_code (normal
, 0);
398 layout
.keyboard_map_shift
[keycode_usb
] = get_grub_code (shift
, 1);
399 layout
.keyboard_map_l3
[keycode_usb
]
400 = get_grub_code (normalalt
, 0);
401 layout
.keyboard_map_shift_l3
[keycode_usb
]
402 = get_grub_code (shiftalt
, 1);
410 fprintf (stderr
, "ERROR: no keycodes found. Check output of %s.\n",
415 add_special_keys (&layout
);
417 write_file (out
, &layout
);
421 main (int argc
, char *argv
[])
424 char *infile_name
= NULL
;
425 char *outfile_name
= NULL
;
428 set_program_name (argv
[0]);
432 /* Check for options. */
435 int c
= getopt_long (argc
, argv
, "o:i:hVv", options
, 0);
447 infile_name
= optarg
;
451 outfile_name
= optarg
;
455 printf ("%s (%s) %s\n", program_name
, PACKAGE_NAME
,
470 in
= fopen (infile_name
, "r");
475 grub_util_error ("Couldn't open input file: %s\n", strerror (errno
));
478 out
= fopen (outfile_name
, "wb");
486 grub_util_error ("Couldn't open output file: %s\n", strerror (errno
));
489 write_keymaps (in
, out
);