LibKeyboard: Replace char data type to u32 for code point

This commit is contained in:
Hüseyin ASLITÜRK 2020-06-13 13:51:20 +03:00 committed by Andreas Kling
parent cfaed04464
commit 25e14911c5
5 changed files with 39 additions and 35 deletions

View file

@ -25,6 +25,7 @@
*/
#include "CharacterMap.h"
#include <AK/StringBuilder.h>
#include <Kernel/Syscall.h>
#ifndef KERNEL
# include <LibKeyboard/CharacterMapFile.h>
@ -51,35 +52,35 @@ int CharacterMap::set_system_map()
return syscall(SC_setkeymap, &params);
}
char CharacterMap::get_char(KeyEvent event)
u32 CharacterMap::get_char(KeyEvent event)
{
auto modifiers = event.modifiers();
auto index = event.scancode & 0xFF; // Index is last byte of scan code.
auto caps_lock_on = event.caps_lock_on;
char character;
u32 code_point;
if (modifiers & Mod_Shift)
character = m_character_map_data.shift_map[index];
code_point = m_character_map_data.shift_map[index];
else if (modifiers & Mod_Alt)
character = m_character_map_data.alt_map[index];
code_point = m_character_map_data.alt_map[index];
else if (modifiers & Mod_AltGr)
character = m_character_map_data.altgr_map[index];
code_point = m_character_map_data.altgr_map[index];
else
character = m_character_map_data.map[index];
code_point = m_character_map_data.map[index];
if (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
if (character >= 'a' && character <= 'z')
character &= ~0x20;
else if (character >= 'A' && character <= 'Z')
character |= 0x20;
if (code_point >= 'a' && code_point <= 'z')
code_point &= ~0x20;
else if (code_point >= 'A' && code_point <= 'Z')
code_point |= 0x20;
}
if (event.e0_prefix && event.key == Key_Slash) {
// If Key_Slash (scancode = 0x35) mapped to other form "/", we fix num pad key of "/" with this case.
character = '/';
code_point = '/';
}
return character;
return code_point;
}
void CharacterMap::set_character_map_data(CharacterMapData character_map_data)

View file

@ -38,7 +38,7 @@ public:
CharacterMap(const String& file_name);
int set_system_map();
char get_char(KeyEvent);
u32 get_char(KeyEvent);
void set_character_map_data(CharacterMapData character_map_data);
private:

View file

@ -26,15 +26,17 @@
#pragma once
#include <AK/Types.h>
#define CHAR_MAP_SIZE 0x80
namespace Keyboard {
struct CharacterMapData {
char map[CHAR_MAP_SIZE];
char shift_map[CHAR_MAP_SIZE];
char alt_map[CHAR_MAP_SIZE];
char altgr_map[CHAR_MAP_SIZE];
u32 map[CHAR_MAP_SIZE];
u32 shift_map[CHAR_MAP_SIZE];
u32 alt_map[CHAR_MAP_SIZE];
u32 altgr_map[CHAR_MAP_SIZE];
};
// clang-format off

View file

@ -25,6 +25,7 @@
*/
#include "CharacterMapFile.h"
#include <AK/Utf8View.h>
#include <LibCore/File.h>
namespace Keyboard {
@ -52,34 +53,34 @@ Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& file_n
ASSERT(json_result.has_value());
auto json = json_result.value().as_object();
ByteBuffer map = read_map(json, "map");
ByteBuffer shift_map = read_map(json, "shift_map");
ByteBuffer alt_map = read_map(json, "alt_map");
ByteBuffer altgr_map = read_map(json, "altgr_map");
Vector<u32> map = read_map(json, "map");
Vector<u32> shift_map = read_map(json, "shift_map");
Vector<u32> alt_map = read_map(json, "alt_map");
Vector<u32> altgr_map = read_map(json, "altgr_map");
CharacterMapData character_map;
for (int i = 0; i < CHAR_MAP_SIZE; i++) {
character_map.map[i] = map[i];
character_map.shift_map[i] = shift_map[i];
character_map.alt_map[i] = alt_map[i];
if (altgr_map) {
character_map.altgr_map[i] = altgr_map[i];
} else {
character_map.map[i] = map.at(i);
character_map.shift_map[i] = shift_map.at(i);
character_map.alt_map[i] = alt_map.at(i);
if (altgr_map.is_empty()) {
// AltGr map was not found, using Alt map as fallback.
character_map.altgr_map[i] = alt_map[i];
character_map.altgr_map[i] = alt_map.at(i);
} else {
character_map.altgr_map[i] = altgr_map.at(i);
}
}
return character_map;
}
ByteBuffer CharacterMapFile::read_map(const JsonObject& json, const String& name)
Vector<u32> CharacterMapFile::read_map(const JsonObject& json, const String& name)
{
if (!json.has(name))
return nullptr;
return {};
ByteBuffer buffer;
buffer.grow(CHAR_MAP_SIZE);
Vector<u32> buffer;
buffer.resize(CHAR_MAP_SIZE);
auto map_arr = json.get(name).as_array();
for (int i = 0; i < map_arr.size(); i++) {
@ -89,8 +90,8 @@ ByteBuffer CharacterMapFile::read_map(const JsonObject& json, const String& name
} else if (key_value.length() == 1) {
buffer[i] = key_value.characters()[0];
} else {
dbg() << "Unknown character in " << name.characters() << "[" << i << "] = " << key_value.characters() << ".";
ASSERT_NOT_REACHED();
Utf8View m_utf8_view(key_value.characters());
buffer[i] = *m_utf8_view.begin();
}
}

View file

@ -37,7 +37,7 @@ public:
static Optional<CharacterMapData> load_from_file(const String& file_name);
private:
static ByteBuffer read_map(const JsonObject& json, const String& name);
static Vector<u32> read_map(const JsonObject& json, const String& name);
};
}