2020-05-31 16:23:55 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
|
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-31 16:23:55 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "CharacterMapFile.h"
|
2020-11-15 15:11:21 +03:00
|
|
|
|
#include <AK/ByteBuffer.h>
|
2020-06-13 13:51:20 +03:00
|
|
|
|
#include <AK/Utf8View.h>
|
2023-02-08 23:08:01 +03:00
|
|
|
|
#include <LibCore/DeprecatedFile.h>
|
2020-05-31 16:23:55 +03:00
|
|
|
|
|
|
|
|
|
namespace Keyboard {
|
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
|
ErrorOr<CharacterMapData> CharacterMapFile::load_from_file(DeprecatedString const& filename)
|
2020-05-31 16:23:55 +03:00
|
|
|
|
{
|
2021-04-29 22:46:15 +03:00
|
|
|
|
auto path = filename;
|
2022-07-11 20:32:29 +03:00
|
|
|
|
if (!path.ends_with(".json"sv)) {
|
2020-05-31 16:23:55 +03:00
|
|
|
|
StringBuilder full_path;
|
2022-07-11 20:32:29 +03:00
|
|
|
|
full_path.append("/res/keymaps/"sv);
|
2021-04-29 22:46:15 +03:00
|
|
|
|
full_path.append(filename);
|
2022-07-11 20:32:29 +03:00
|
|
|
|
full_path.append(".json"sv);
|
2022-12-06 04:12:49 +03:00
|
|
|
|
path = full_path.to_deprecated_string();
|
2020-05-31 16:23:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-08 23:08:01 +03:00
|
|
|
|
auto file = TRY(Core::DeprecatedFile::open(path, Core::OpenMode::ReadOnly));
|
2020-05-31 16:23:55 +03:00
|
|
|
|
auto file_contents = file->read_all();
|
2021-12-16 19:46:49 +03:00
|
|
|
|
auto json_result = TRY(JsonValue::from_string(file_contents));
|
2022-04-01 20:58:27 +03:00
|
|
|
|
auto const& json = json_result.as_object();
|
2020-05-31 16:23:55 +03:00
|
|
|
|
|
2020-06-13 13:51:20 +03:00
|
|
|
|
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");
|
2021-01-05 11:45:17 +03:00
|
|
|
|
Vector<u32> shift_altgr_map = read_map(json, "shift_altgr_map");
|
2020-05-31 16:23:55 +03:00
|
|
|
|
|
|
|
|
|
CharacterMapData character_map;
|
|
|
|
|
for (int i = 0; i < CHAR_MAP_SIZE; i++) {
|
2020-06-13 13:51:20 +03:00
|
|
|
|
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()) {
|
2020-05-31 16:23:55 +03:00
|
|
|
|
// AltGr map was not found, using Alt map as fallback.
|
2020-06-13 13:51:20 +03:00
|
|
|
|
character_map.altgr_map[i] = alt_map.at(i);
|
|
|
|
|
} else {
|
|
|
|
|
character_map.altgr_map[i] = altgr_map.at(i);
|
2020-05-31 16:23:55 +03:00
|
|
|
|
}
|
2021-01-05 11:45:17 +03:00
|
|
|
|
if (shift_altgr_map.is_empty()) {
|
|
|
|
|
// Shift+AltGr map was not found, using Alt map as fallback.
|
|
|
|
|
character_map.shift_altgr_map[i] = alt_map.at(i);
|
|
|
|
|
} else {
|
|
|
|
|
character_map.shift_altgr_map[i] = shift_altgr_map.at(i);
|
|
|
|
|
}
|
2020-05-31 16:23:55 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return character_map;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
|
Vector<u32> CharacterMapFile::read_map(JsonObject const& json, DeprecatedString const& name)
|
2020-05-31 16:23:55 +03:00
|
|
|
|
{
|
|
|
|
|
if (!json.has(name))
|
2020-06-13 13:51:20 +03:00
|
|
|
|
return {};
|
2020-05-31 16:23:55 +03:00
|
|
|
|
|
2020-06-13 13:51:20 +03:00
|
|
|
|
Vector<u32> buffer;
|
|
|
|
|
buffer.resize(CHAR_MAP_SIZE);
|
2020-05-31 16:23:55 +03:00
|
|
|
|
|
2022-12-21 20:27:50 +03:00
|
|
|
|
auto map_arr = json.get_array(name).value();
|
2021-06-28 12:57:37 +03:00
|
|
|
|
for (size_t i = 0; i < map_arr.size(); i++) {
|
2020-05-31 16:23:55 +03:00
|
|
|
|
auto key_value = map_arr.at(i).as_string();
|
|
|
|
|
if (key_value.length() == 0) {
|
|
|
|
|
buffer[i] = 0;
|
|
|
|
|
} else if (key_value.length() == 1) {
|
|
|
|
|
buffer[i] = key_value.characters()[0];
|
|
|
|
|
} else {
|
2021-09-18 19:02:41 +03:00
|
|
|
|
Utf8View m_utf8_view(key_value);
|
2020-06-13 13:51:20 +03:00
|
|
|
|
buffer[i] = *m_utf8_view.begin();
|
2020-05-31 16:23:55 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|