Skip to content

Commit

Permalink
Kernel: Implement AltGr key support
Browse files Browse the repository at this point in the history
  • Loading branch information
xTibor authored and awesomekling committed Dec 31, 2019
1 parent 36f1de3 commit 624116a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
17 changes: 12 additions & 5 deletions Kernel/Devices/KeyboardDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
char *map;
char *shift_map;
char *alt_map;
char *altgr_map;

static char en_map[0x80] = {
0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08, '\t',
Expand Down Expand Up @@ -246,7 +247,7 @@ static KeyCode numpad_key_map[13] = { Key_7, Key_8, Key_9, Key_Invalid, Key_4, K
void KeyboardDevice::key_state_changed(u8 raw, bool pressed)
{
KeyCode key = (m_modifiers & Mod_Shift) ? shifted_key_map[raw] : unshifted_key_map[raw];
char character = (m_modifiers & Mod_Shift) ? shift_map[raw]: (m_modifiers & Mod_Alt) ? alt_map[raw] : map[raw];
char character = (m_modifiers & Mod_Shift) ? shift_map[raw] : (m_modifiers & Mod_Alt) ? alt_map[raw] : (m_modifiers & Mod_AltGr) ? altgr_map[raw] : map[raw];

if (key == Key_NumLock && pressed)
m_num_lock_on = !m_num_lock_on;
Expand Down Expand Up @@ -318,7 +319,10 @@ void KeyboardDevice::handle_irq()
#endif
switch (ch) {
case 0x38:
update_modifier(Mod_Alt, pressed);
if (m_has_e0_prefix)
update_modifier(Mod_AltGr, pressed);
else
update_modifier(Mod_Alt, pressed);
break;
case 0x1d:
update_modifier(Mod_Ctrl, pressed);
Expand Down Expand Up @@ -368,7 +372,7 @@ KeyboardDevice::KeyboardDevice()
{
s_the = this;

KeyboardDevice::set_maps(en_map, en_shift_map, en_map);
KeyboardDevice::set_maps(en_map, en_shift_map, en_map, en_map);

// Empty the buffer of any pending data.
// I don't care what you've been pressing until now!
Expand Down Expand Up @@ -412,19 +416,22 @@ KeyboardClient::~KeyboardClient()
{
}

void KeyboardDevice::set_maps(char* n_map, char* n_shift_map, char* n_alt_map)
void KeyboardDevice::set_maps(const char* n_map, const char* n_shift_map, const char* n_alt_map, const char* n_altgr_map)
{
kfree(map);
kfree(shift_map);
kfree(alt_map);
kfree(altgr_map);

map = (char*) kmalloc(0x80);
shift_map = (char*) kmalloc(0x80);
alt_map = (char*) kmalloc(0x80);
altgr_map = (char*) kmalloc(0x80);

for(int i=0; i < 0x80; i++) {
map[i] = n_map[i];
shift_map[i] = n_shift_map[i];
alt_map[i] = n_alt_map[i];
altgr_map[i] = n_altgr_map[i];
}
}
}
2 changes: 1 addition & 1 deletion Kernel/Devices/KeyboardDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class KeyboardDevice final : public IRQHandler
KeyboardDevice();

void set_client(KeyboardClient* client) { m_client = client; }
void set_maps(char* n_map, char* n_shift_map, char* n_alt_map);
void set_maps(const char* n_map, const char* n_shift_map, const char* n_alt_map, const char* n_altgr_map);

// ^CharacterDevice
virtual ssize_t read(FileDescription&, u8* buffer, ssize_t) override;
Expand Down
4 changes: 3 additions & 1 deletion Kernel/KeyCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ enum KeyModifier {
Mod_Ctrl = 0x02,
Mod_Shift = 0x04,
Mod_Logo = 0x08,
Mod_Mask = 0x0f,
Mod_AltGr = 0x10,
Mod_Mask = 0x1f,

Is_Press = 0x80,
};
Expand All @@ -132,6 +133,7 @@ struct KeyEvent {
bool ctrl() const { return flags & Mod_Ctrl; }
bool shift() const { return flags & Mod_Shift; }
bool logo() const { return flags & Mod_Logo; }
bool altgr() const { return flags & Mod_AltGr; }
unsigned modifiers() const { return flags & Mod_Mask; }
bool is_press() const { return flags & Is_Press; }
};
15 changes: 10 additions & 5 deletions Kernel/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3584,19 +3584,24 @@ int Process::sys$getrandom(void* buffer, size_t buffer_size, unsigned int flags
return 0;
}

int Process::sys$setkeymap(char* map, char* shift_map, char* alt_map)
int Process::sys$setkeymap(const Syscall::SC_setkeymap_params* params)
{
if (!is_superuser())
return -EPERM;

if (!validate_read(map, 0x80))
if (!validate_read_typed(params))
return -EFAULT;

if (!validate_read(params->map, 0x80))
return -EFAULT;
if (!validate_read(params->shift_map, 0x80))
return -EFAULT;
if (!validate_read(shift_map, 0x80))
if (!validate_read(params->alt_map, 0x80))
return -EFAULT;
if (!validate_read(alt_map, 0x80))
if (!validate_read(params->altgr_map, 0x80))
return -EFAULT;

KeyboardDevice::the().set_maps(map, shift_map, alt_map);
KeyboardDevice::the().set_maps(params->map, params->shift_map, params->alt_map, params->altgr_map);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Process : public InlineLinkedListNode<Process>
int sys$set_process_icon(int icon_id);
int sys$realpath(const char* pathname, char*, size_t);
ssize_t sys$getrandom(void*, size_t, unsigned int);
int sys$setkeymap(char* map, char* shift_map, char* alt_map);
int sys$setkeymap(const Syscall::SC_setkeymap_params*);
int sys$module_load(const char* path, size_t path_length);
int sys$module_unload(const char* name, size_t name_length);
int sys$profiling_enable(pid_t);
Expand Down
7 changes: 7 additions & 0 deletions Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ struct SC_futex_params {
const timespec* timeout;
};

struct SC_setkeymap_params {
const char* map;
const char* shift_map;
const char* alt_map;
const char* altgr_map;
};

struct SC_create_thread_params {
unsigned int m_detach_state = 0; // JOINABLE or DETACHED
int m_schedule_priority = 30; // THREAD_PRIORITY_NORMAL
Expand Down

0 comments on commit 624116a

Please sign in to comment.