/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
VasEBoot_MOD_LICENSE ("GPLv3+");
static struct VasEBoot_keyboard_layout layout_us = {
.keyboard_map = {
/* Keyboard errors. Handled by driver. */
/* 0x00 */ 0, 0, 0, 0,
/* 0x04 */ 'a', 'b', 'c', 'd',
/* 0x08 */ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
/* 0x10 */ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
/* 0x18 */ 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
/* 0x20 */ '3', '4', '5', '6', '7', '8', '9', '0',
/* 0x28 */ '\n', '\e', '\b', '\t', ' ', '-', '=', '[',
/* According to usage table 0x31 should be mapped to '/'
but testing with real keyboard shows that 0x32 is remapped to '/'.
Map 0x31 to 0.
*/
/* 0x30 */ ']', 0, '\\', ';', '\'', '`', ',', '.',
/* 0x39 is CapsLock. Handled by driver. */
/* 0x38 */ '/', 0, VasEBoot_TERM_KEY_F1, VasEBoot_TERM_KEY_F2,
/* 0x3c */ VasEBoot_TERM_KEY_F3, VasEBoot_TERM_KEY_F4,
/* 0x3e */ VasEBoot_TERM_KEY_F5, VasEBoot_TERM_KEY_F6,
/* 0x40 */ VasEBoot_TERM_KEY_F7, VasEBoot_TERM_KEY_F8,
/* 0x42 */ VasEBoot_TERM_KEY_F9, VasEBoot_TERM_KEY_F10,
/* 0x44 */ VasEBoot_TERM_KEY_F11, VasEBoot_TERM_KEY_F12,
/* PrtScr and ScrollLock. Not handled yet. */
/* 0x46 */ 0, 0,
/* 0x48 is Pause. Not handled yet. */
/* 0x48 */ 0, VasEBoot_TERM_KEY_INSERT,
/* 0x4a */ VasEBoot_TERM_KEY_HOME, VasEBoot_TERM_KEY_PPAGE,
/* 0x4c */ VasEBoot_TERM_KEY_DC, VasEBoot_TERM_KEY_END,
/* 0x4e */ VasEBoot_TERM_KEY_NPAGE, VasEBoot_TERM_KEY_RIGHT,
/* 0x50 */ VasEBoot_TERM_KEY_LEFT, VasEBoot_TERM_KEY_DOWN,
/* 0x53 is NumLock. Handled by driver. */
/* 0x52 */ VasEBoot_TERM_KEY_UP, 0,
/* 0x54 */ '/', '*',
/* 0x56 */ '-', '+',
/* 0x58 */ '\n', VasEBoot_TERM_KEY_END,
/* 0x5a */ VasEBoot_TERM_KEY_DOWN, VasEBoot_TERM_KEY_NPAGE,
/* 0x5c */ VasEBoot_TERM_KEY_LEFT, VasEBoot_TERM_KEY_CENTER,
/* 0x5e */ VasEBoot_TERM_KEY_RIGHT, VasEBoot_TERM_KEY_HOME,
/* 0x60 */ VasEBoot_TERM_KEY_UP, VasEBoot_TERM_KEY_PPAGE,
/* 0x62 */ VasEBoot_TERM_KEY_INSERT, VasEBoot_TERM_KEY_DC,
/* 0x64 */ '\\'
},
.keyboard_map_shift = {
/* Keyboard errors. Handled by driver. */
/* 0x00 */ 0, 0, 0, 0,
/* 0x04 */ 'A', 'B', 'C', 'D',
/* 0x08 */ 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
/* 0x10 */ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
/* 0x18 */ 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
/* 0x20 */ '#', '$', '%', '^', '&', '*', '(', ')',
/* 0x28 */ '\n' | VasEBoot_TERM_SHIFT, '\e' | VasEBoot_TERM_SHIFT,
/* 0x2a */ '\b' | VasEBoot_TERM_SHIFT, '\t' | VasEBoot_TERM_SHIFT,
/* 0x2c */ ' ' | VasEBoot_TERM_SHIFT, '_', '+', '{',
/* According to usage table 0x31 should be mapped to '/'
but testing with real keyboard shows that 0x32 is remapped to '/'.
Map 0x31 to 0.
*/
/* 0x30 */ '}', 0, '|', ':', '"', '~', '<', '>',
/* 0x39 is CapsLock. Handled by driver. */
/* 0x38 */ '?', 0,
/* 0x3a */ VasEBoot_TERM_KEY_F1 | VasEBoot_TERM_SHIFT,
/* 0x3b */ VasEBoot_TERM_KEY_F2 | VasEBoot_TERM_SHIFT,
/* 0x3c */ VasEBoot_TERM_KEY_F3 | VasEBoot_TERM_SHIFT,
/* 0x3d */ VasEBoot_TERM_KEY_F4 | VasEBoot_TERM_SHIFT,
/* 0x3e */ VasEBoot_TERM_KEY_F5 | VasEBoot_TERM_SHIFT,
/* 0x3f */ VasEBoot_TERM_KEY_F6 | VasEBoot_TERM_SHIFT,
/* 0x40 */ VasEBoot_TERM_KEY_F7 | VasEBoot_TERM_SHIFT,
/* 0x41 */ VasEBoot_TERM_KEY_F8 | VasEBoot_TERM_SHIFT,
/* 0x42 */ VasEBoot_TERM_KEY_F9 | VasEBoot_TERM_SHIFT,
/* 0x43 */ VasEBoot_TERM_KEY_F10 | VasEBoot_TERM_SHIFT,
/* 0x44 */ VasEBoot_TERM_KEY_F11 | VasEBoot_TERM_SHIFT,
/* 0x45 */ VasEBoot_TERM_KEY_F12 | VasEBoot_TERM_SHIFT,
/* PrtScr and ScrollLock. Not handled yet. */
/* 0x46 */ 0, 0,
/* 0x48 is Pause. Not handled yet. */
/* 0x48 */ 0, VasEBoot_TERM_KEY_INSERT | VasEBoot_TERM_SHIFT,
/* 0x4a */ VasEBoot_TERM_KEY_HOME | VasEBoot_TERM_SHIFT,
/* 0x4b */ VasEBoot_TERM_KEY_PPAGE | VasEBoot_TERM_SHIFT,
/* 0x4c */ VasEBoot_TERM_KEY_DC | VasEBoot_TERM_SHIFT,
/* 0x4d */ VasEBoot_TERM_KEY_END | VasEBoot_TERM_SHIFT,
/* 0x4e */ VasEBoot_TERM_KEY_NPAGE | VasEBoot_TERM_SHIFT,
/* 0x4f */ VasEBoot_TERM_KEY_RIGHT | VasEBoot_TERM_SHIFT,
/* 0x50 */ VasEBoot_TERM_KEY_LEFT | VasEBoot_TERM_SHIFT,
/* 0x51 */ VasEBoot_TERM_KEY_DOWN | VasEBoot_TERM_SHIFT,
/* 0x53 is NumLock. Handled by driver. */
/* 0x52 */ VasEBoot_TERM_KEY_UP | VasEBoot_TERM_SHIFT, 0,
/* 0x54 */ '/', '*',
/* 0x56 */ '-', '+',
/* 0x58 */ '\n' | VasEBoot_TERM_SHIFT, '1', '2', '3', '4', '5','6', '7',
/* 0x60 */ '8', '9', '0', '.', '|'
}
};
static struct VasEBoot_keyboard_layout *VasEBoot_current_layout = &layout_us;
static int
map_key_core (int code, int status, int *alt_gr_consumed)
{
*alt_gr_consumed = 0;
if (code >= VasEBoot_KEYBOARD_LAYOUTS_ARRAY_SIZE)
return 0;
if (status & VasEBoot_TERM_STATUS_RALT)
{
if (status & (VasEBoot_TERM_STATUS_LSHIFT | VasEBoot_TERM_STATUS_RSHIFT))
{
if (VasEBoot_current_layout->keyboard_map_shift_l3[code])
{
*alt_gr_consumed = 1;
return VasEBoot_current_layout->keyboard_map_shift_l3[code];
}
}
else if (VasEBoot_current_layout->keyboard_map_l3[code])
{
*alt_gr_consumed = 1;
return VasEBoot_current_layout->keyboard_map_l3[code];
}
}
if (status & (VasEBoot_TERM_STATUS_LSHIFT | VasEBoot_TERM_STATUS_RSHIFT))
return VasEBoot_current_layout->keyboard_map_shift[code];
else
return VasEBoot_current_layout->keyboard_map[code];
}
unsigned
VasEBoot_term_map_key (VasEBoot_keyboard_key_t code, int status)
{
int alt_gr_consumed = 0;
int key;
if (code >= 0x59 && code <= 0x63 && (status & VasEBoot_TERM_STATUS_NUM))
{
if (status & (VasEBoot_TERM_STATUS_RSHIFT | VasEBoot_TERM_STATUS_LSHIFT))
status &= ~(VasEBoot_TERM_STATUS_RSHIFT | VasEBoot_TERM_STATUS_LSHIFT);
else
status |= VasEBoot_TERM_STATUS_RSHIFT;
}
key = map_key_core (code, status, &alt_gr_consumed);
if (key == 0 || key == VasEBoot_TERM_SHIFT) {
VasEBoot_printf ("Unknown key 0x%x detected\n", code);
return VasEBoot_TERM_NO_KEY;
}
if (status & VasEBoot_TERM_STATUS_CAPS)
{
if ((key >= 'a') && (key <= 'z'))
key += 'A' - 'a';
else if ((key >= 'A') && (key <= 'Z'))
key += 'a' - 'A';
}
if ((status & VasEBoot_TERM_STATUS_LALT) ||
((status & VasEBoot_TERM_STATUS_RALT) && !alt_gr_consumed))
key |= VasEBoot_TERM_ALT;
if (status & (VasEBoot_TERM_STATUS_LCTRL | VasEBoot_TERM_STATUS_RCTRL))
key |= VasEBoot_TERM_CTRL;
return key;
}
static VasEBoot_err_t
VasEBoot_cmd_keymap (struct VasEBoot_command *cmd __attribute__ ((unused)),
int argc, char *argv[])
{
char *filename;
VasEBoot_file_t file;
VasEBoot_uint32_t version;
VasEBoot_uint8_t magic[VasEBoot_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE];
struct VasEBoot_keyboard_layout *newmap = NULL;
unsigned i;
if (argc < 1)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "file or layout name required");
if (argv[0][0] != '(' && argv[0][0] != '/' && argv[0][0] != '+')
{
const char *prefix = VasEBoot_env_get ("prefix");
if (!prefix)
return VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("variable `%s' isn't set"), "prefix");
filename = VasEBoot_xasprintf ("%s/layouts/%s.gkb", prefix, argv[0]);
if (!filename)
return VasEBoot_errno;
}
else
filename = argv[0];
file = VasEBoot_file_open (filename);
if (! file)
goto fail;
if (VasEBoot_file_read (file, magic, sizeof (magic)) != sizeof (magic))
{
if (!VasEBoot_errno)
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
filename);
goto fail;
}
if (VasEBoot_memcmp (magic, VasEBoot_KEYBOARD_LAYOUTS_FILEMAGIC,
VasEBoot_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE) != 0)
{
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "invalid magic");
goto fail;
}
if (VasEBoot_file_read (file, &version, sizeof (version)) != sizeof (version))
{
if (!VasEBoot_errno)
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
filename);
goto fail;
}
if (version != VasEBoot_cpu_to_le32_compile_time (VasEBoot_KEYBOARD_LAYOUTS_VERSION))
{
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, "invalid version");
goto fail;
}
newmap = VasEBoot_malloc (sizeof (*newmap));
if (!newmap)
goto fail;
if (VasEBoot_file_read (file, newmap, sizeof (*newmap)) != sizeof (*newmap))
{
if (!VasEBoot_errno)
VasEBoot_error (VasEBoot_ERR_BAD_ARGUMENT, N_("premature end of file %s"),
filename);
goto fail;
}
for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map); i++)
newmap->keyboard_map[i] = VasEBoot_le_to_cpu32(newmap->keyboard_map[i]);
for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift); i++)
newmap->keyboard_map_shift[i]
= VasEBoot_le_to_cpu32(newmap->keyboard_map_shift[i]);
for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_l3); i++)
newmap->keyboard_map_l3[i]
= VasEBoot_le_to_cpu32(newmap->keyboard_map_l3[i]);
for (i = 0; i < ARRAY_SIZE (newmap->keyboard_map_shift_l3); i++)
newmap->keyboard_map_shift_l3[i]
= VasEBoot_le_to_cpu32(newmap->keyboard_map_shift_l3[i]);
VasEBoot_current_layout = newmap;
return VasEBoot_ERR_NONE;
fail:
if (filename != argv[0])
VasEBoot_free (filename);
VasEBoot_free (newmap);
if (file)
VasEBoot_file_close (file);
return VasEBoot_errno;
}
static VasEBoot_command_t cmd;
VasEBoot_MOD_INIT(keylayouts)
{
cmd = VasEBoot_register_command ("keymap", VasEBoot_cmd_keymap,
0, N_("Load a keyboard layout."));
}
VasEBoot_MOD_FINI(keylayouts)
{
VasEBoot_unregister_command (cmd);
}