mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
163 lines
5 KiB
C++
163 lines
5 KiB
C++
/*******************************************************************************
|
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2014.
|
|
|
|
This file is part of Sortix.
|
|
|
|
Sortix 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.
|
|
|
|
Sortix 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
|
|
Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
kb/kblayout.cpp
|
|
Engine that executes a keyboard layout program.
|
|
|
|
*******************************************************************************/
|
|
|
|
#include <assert.h>
|
|
#include <endian.h>
|
|
#include <errno.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <sortix/kblayout.h>
|
|
|
|
#include <sortix/kernel/log.h>
|
|
|
|
#include "kblayout.h"
|
|
|
|
namespace Sortix {
|
|
|
|
KeyboardLayoutExecutor::KeyboardLayoutExecutor()
|
|
{
|
|
memset(&header, 0, sizeof(header));
|
|
memset(&modifier_counts, 0, sizeof(modifier_counts));
|
|
loaded = false;
|
|
actions = NULL;
|
|
keys_down = NULL;
|
|
modifiers = 0;
|
|
saved_data = 0;
|
|
saved_data_size = 0;
|
|
}
|
|
|
|
KeyboardLayoutExecutor::~KeyboardLayoutExecutor()
|
|
{
|
|
delete[] actions;
|
|
delete[] keys_down;
|
|
delete[] saved_data;
|
|
}
|
|
|
|
bool KeyboardLayoutExecutor::Upload(const uint8_t* input_data, size_t input_size)
|
|
{
|
|
size_t data_size = input_size;
|
|
uint8_t* data = new uint8_t[data_size];
|
|
if ( !data )
|
|
return false;
|
|
memcpy(data, input_data, data_size);
|
|
struct kblayout new_header;
|
|
if ( data_size < sizeof(struct kblayout) )
|
|
return delete[] data, errno = EINVAL, false;
|
|
memcpy(&new_header, data, sizeof(new_header));
|
|
if ( strncmp(new_header.magic, "sortix-kblayout-1", sizeof(new_header.magic)) != 0 )
|
|
return delete[] data, errno = EINVAL, false;
|
|
new_header.num_modifiers = le32toh(new_header.num_modifiers);
|
|
new_header.num_scancodes = le32toh(new_header.num_scancodes);
|
|
if ( KBLAYOUT_MAX_NUM_MODIFIERS <= new_header.num_modifiers )
|
|
return errno = EINVAL, false;
|
|
// TODO: Limit num_scancodes and max_actions.
|
|
size_t remaining_size = data_size - sizeof(new_header);
|
|
// TODO: Check for overflow.
|
|
size_t table_length = (size_t) (1 << new_header.num_modifiers) *
|
|
(size_t) new_header.num_scancodes;
|
|
size_t table_size = sizeof(struct kblayout_action) * table_length;
|
|
if ( remaining_size < table_size )
|
|
return delete[] data, errno = EINVAL, false;
|
|
struct kblayout_action* new_actions = new struct kblayout_action[table_length];
|
|
if ( !new_actions )
|
|
return delete[] data, false;
|
|
uint8_t* new_keys_down = new uint8_t[new_header.num_scancodes];
|
|
if ( !new_keys_down )
|
|
return delete[] data, delete[] new_actions, false;
|
|
memcpy(new_actions, data + sizeof(new_header), table_size);
|
|
for ( size_t i = 0; i < table_length; i++ )
|
|
new_actions[i] = le32toh(new_actions[i]);
|
|
memcpy(&header, &new_header, sizeof(header));
|
|
delete[] actions;
|
|
actions = new_actions;
|
|
memset(new_keys_down, 0, sizeof(keys_down[0]) * new_header.num_scancodes);
|
|
delete[] keys_down;
|
|
keys_down = new_keys_down;
|
|
memset(&modifier_counts, 0, sizeof(modifier_counts));
|
|
modifiers = 0;
|
|
delete[] saved_data;
|
|
saved_data = data;
|
|
saved_data_size = data_size;
|
|
loaded = true;
|
|
return true;
|
|
}
|
|
|
|
bool KeyboardLayoutExecutor::Download(const uint8_t** data, size_t* data_size)
|
|
{
|
|
if ( !saved_data )
|
|
return errno = EINIT, false;
|
|
return *data = saved_data, *data_size = saved_data_size, true;
|
|
}
|
|
|
|
uint32_t KeyboardLayoutExecutor::Translate(int kbkey)
|
|
{
|
|
if ( !loaded )
|
|
return 0;
|
|
unsigned int abskbkey = kbkey < 0 ? - (unsigned int) kbkey : kbkey;
|
|
if ( header.num_scancodes <= abskbkey )
|
|
return 0;
|
|
|
|
bool repeated_key = 0 <= kbkey && keys_down[abskbkey];
|
|
keys_down[abskbkey] = 0 <= kbkey ? 1 : 0;
|
|
|
|
size_t action_index = abskbkey * (1 << header.num_modifiers) + modifiers;
|
|
struct kblayout_action* action = &actions[action_index];
|
|
if ( 0 < kbkey && action->type == KBLAYOUT_ACTION_TYPE_CODEPOINT )
|
|
return action->codepoint;
|
|
// TODO: Properly implement dead keys!
|
|
if ( 0 < kbkey && action->type == KBLAYOUT_ACTION_TYPE_DEAD )
|
|
return action->codepoint;
|
|
if ( repeated_key )
|
|
return 0;
|
|
if ( action->type == KBLAYOUT_ACTION_TYPE_MODIFY &&
|
|
action->codepoint < header.num_modifiers )
|
|
{
|
|
if ( kbkey < 0 )
|
|
{
|
|
if ( --modifier_counts[action->codepoint] == 0 )
|
|
modifiers &= ~(1 << action->codepoint);
|
|
}
|
|
else
|
|
{
|
|
modifier_counts[action->codepoint]++;
|
|
modifiers |= 1 << action->codepoint;
|
|
}
|
|
return 0;
|
|
}
|
|
if ( 0 <= kbkey &&
|
|
action->type == KBLAYOUT_ACTION_TYPE_TOGGLE &&
|
|
action->codepoint < header.num_modifiers )
|
|
{
|
|
if ( (modifier_counts[action->codepoint] = !modifier_counts[action->codepoint]) )
|
|
modifiers |= 1 << action->codepoint;
|
|
else
|
|
modifiers &= ~(1 << action->codepoint);
|
|
return 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace Sortix
|