mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +00:00
LibKeyboard: Replace char data type to u32 for code point
This commit is contained in:
parent
cfaed04464
commit
25e14911c5
5 changed files with 39 additions and 35 deletions
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "CharacterMap.h"
|
#include "CharacterMap.h"
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
#include <Kernel/Syscall.h>
|
#include <Kernel/Syscall.h>
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
# include <LibKeyboard/CharacterMapFile.h>
|
# include <LibKeyboard/CharacterMapFile.h>
|
||||||
|
@ -51,35 +52,35 @@ int CharacterMap::set_system_map()
|
||||||
return syscall(SC_setkeymap, ¶ms);
|
return syscall(SC_setkeymap, ¶ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
char CharacterMap::get_char(KeyEvent event)
|
u32 CharacterMap::get_char(KeyEvent event)
|
||||||
{
|
{
|
||||||
auto modifiers = event.modifiers();
|
auto modifiers = event.modifiers();
|
||||||
auto index = event.scancode & 0xFF; // Index is last byte of scan code.
|
auto index = event.scancode & 0xFF; // Index is last byte of scan code.
|
||||||
auto caps_lock_on = event.caps_lock_on;
|
auto caps_lock_on = event.caps_lock_on;
|
||||||
|
|
||||||
char character;
|
u32 code_point;
|
||||||
if (modifiers & Mod_Shift)
|
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)
|
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)
|
else if (modifiers & Mod_AltGr)
|
||||||
character = m_character_map_data.altgr_map[index];
|
code_point = m_character_map_data.altgr_map[index];
|
||||||
else
|
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 (caps_lock_on && (modifiers == 0 || modifiers == Mod_Shift)) {
|
||||||
if (character >= 'a' && character <= 'z')
|
if (code_point >= 'a' && code_point <= 'z')
|
||||||
character &= ~0x20;
|
code_point &= ~0x20;
|
||||||
else if (character >= 'A' && character <= 'Z')
|
else if (code_point >= 'A' && code_point <= 'Z')
|
||||||
character |= 0x20;
|
code_point |= 0x20;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.e0_prefix && event.key == Key_Slash) {
|
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.
|
// 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)
|
void CharacterMap::set_character_map_data(CharacterMapData character_map_data)
|
||||||
|
|
|
@ -38,7 +38,7 @@ public:
|
||||||
CharacterMap(const String& file_name);
|
CharacterMap(const String& file_name);
|
||||||
|
|
||||||
int set_system_map();
|
int set_system_map();
|
||||||
char get_char(KeyEvent);
|
u32 get_char(KeyEvent);
|
||||||
void set_character_map_data(CharacterMapData character_map_data);
|
void set_character_map_data(CharacterMapData character_map_data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -26,15 +26,17 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Types.h>
|
||||||
|
|
||||||
#define CHAR_MAP_SIZE 0x80
|
#define CHAR_MAP_SIZE 0x80
|
||||||
|
|
||||||
namespace Keyboard {
|
namespace Keyboard {
|
||||||
|
|
||||||
struct CharacterMapData {
|
struct CharacterMapData {
|
||||||
char map[CHAR_MAP_SIZE];
|
u32 map[CHAR_MAP_SIZE];
|
||||||
char shift_map[CHAR_MAP_SIZE];
|
u32 shift_map[CHAR_MAP_SIZE];
|
||||||
char alt_map[CHAR_MAP_SIZE];
|
u32 alt_map[CHAR_MAP_SIZE];
|
||||||
char altgr_map[CHAR_MAP_SIZE];
|
u32 altgr_map[CHAR_MAP_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "CharacterMapFile.h"
|
#include "CharacterMapFile.h"
|
||||||
|
#include <AK/Utf8View.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
|
|
||||||
namespace Keyboard {
|
namespace Keyboard {
|
||||||
|
@ -52,34 +53,34 @@ Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& file_n
|
||||||
ASSERT(json_result.has_value());
|
ASSERT(json_result.has_value());
|
||||||
auto json = json_result.value().as_object();
|
auto json = json_result.value().as_object();
|
||||||
|
|
||||||
ByteBuffer map = read_map(json, "map");
|
Vector<u32> map = read_map(json, "map");
|
||||||
ByteBuffer shift_map = read_map(json, "shift_map");
|
Vector<u32> shift_map = read_map(json, "shift_map");
|
||||||
ByteBuffer alt_map = read_map(json, "alt_map");
|
Vector<u32> alt_map = read_map(json, "alt_map");
|
||||||
ByteBuffer altgr_map = read_map(json, "altgr_map");
|
Vector<u32> altgr_map = read_map(json, "altgr_map");
|
||||||
|
|
||||||
CharacterMapData character_map;
|
CharacterMapData character_map;
|
||||||
for (int i = 0; i < CHAR_MAP_SIZE; i++) {
|
for (int i = 0; i < CHAR_MAP_SIZE; i++) {
|
||||||
character_map.map[i] = map[i];
|
character_map.map[i] = map.at(i);
|
||||||
character_map.shift_map[i] = shift_map[i];
|
character_map.shift_map[i] = shift_map.at(i);
|
||||||
character_map.alt_map[i] = alt_map[i];
|
character_map.alt_map[i] = alt_map.at(i);
|
||||||
if (altgr_map) {
|
if (altgr_map.is_empty()) {
|
||||||
character_map.altgr_map[i] = altgr_map[i];
|
|
||||||
} else {
|
|
||||||
// AltGr map was not found, using Alt map as fallback.
|
// 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;
|
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))
|
if (!json.has(name))
|
||||||
return nullptr;
|
return {};
|
||||||
|
|
||||||
ByteBuffer buffer;
|
Vector<u32> buffer;
|
||||||
buffer.grow(CHAR_MAP_SIZE);
|
buffer.resize(CHAR_MAP_SIZE);
|
||||||
|
|
||||||
auto map_arr = json.get(name).as_array();
|
auto map_arr = json.get(name).as_array();
|
||||||
for (int i = 0; i < map_arr.size(); i++) {
|
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) {
|
} else if (key_value.length() == 1) {
|
||||||
buffer[i] = key_value.characters()[0];
|
buffer[i] = key_value.characters()[0];
|
||||||
} else {
|
} else {
|
||||||
dbg() << "Unknown character in " << name.characters() << "[" << i << "] = " << key_value.characters() << ".";
|
Utf8View m_utf8_view(key_value.characters());
|
||||||
ASSERT_NOT_REACHED();
|
buffer[i] = *m_utf8_view.begin();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ public:
|
||||||
static Optional<CharacterMapData> load_from_file(const String& file_name);
|
static Optional<CharacterMapData> load_from_file(const String& file_name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static ByteBuffer read_map(const JsonObject& json, const String& name);
|
static Vector<u32> read_map(const JsonObject& json, const String& name);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue