CharacterMapFile.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CharacterMapFile.h"
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Utf8View.h>
  9. #include <LibCore/File.h>
  10. namespace Keyboard {
  11. Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& filename)
  12. {
  13. auto path = filename;
  14. if (!path.ends_with(".json")) {
  15. StringBuilder full_path;
  16. full_path.append("/res/keymaps/");
  17. full_path.append(filename);
  18. full_path.append(".json");
  19. path = full_path.to_string();
  20. }
  21. auto file = Core::File::construct(path);
  22. file->open(Core::OpenMode::ReadOnly);
  23. if (!file->is_open()) {
  24. dbgln("Failed to open {}: {}", path, file->error_string());
  25. return {};
  26. }
  27. auto file_contents = file->read_all();
  28. auto json_result = JsonValue::from_string(file_contents);
  29. if (json_result.is_error()) {
  30. dbgln("Failed to load character map from file {}", path);
  31. return {};
  32. }
  33. auto json = json_result.value().as_object();
  34. Vector<u32> map = read_map(json, "map");
  35. Vector<u32> shift_map = read_map(json, "shift_map");
  36. Vector<u32> alt_map = read_map(json, "alt_map");
  37. Vector<u32> altgr_map = read_map(json, "altgr_map");
  38. Vector<u32> shift_altgr_map = read_map(json, "shift_altgr_map");
  39. CharacterMapData character_map;
  40. for (int i = 0; i < CHAR_MAP_SIZE; i++) {
  41. character_map.map[i] = map.at(i);
  42. character_map.shift_map[i] = shift_map.at(i);
  43. character_map.alt_map[i] = alt_map.at(i);
  44. if (altgr_map.is_empty()) {
  45. // AltGr map was not found, using Alt map as fallback.
  46. character_map.altgr_map[i] = alt_map.at(i);
  47. } else {
  48. character_map.altgr_map[i] = altgr_map.at(i);
  49. }
  50. if (shift_altgr_map.is_empty()) {
  51. // Shift+AltGr map was not found, using Alt map as fallback.
  52. character_map.shift_altgr_map[i] = alt_map.at(i);
  53. } else {
  54. character_map.shift_altgr_map[i] = shift_altgr_map.at(i);
  55. }
  56. }
  57. return character_map;
  58. }
  59. Vector<u32> CharacterMapFile::read_map(const JsonObject& json, const String& name)
  60. {
  61. if (!json.has(name))
  62. return {};
  63. Vector<u32> buffer;
  64. buffer.resize(CHAR_MAP_SIZE);
  65. auto map_arr = json.get(name).as_array();
  66. for (size_t i = 0; i < map_arr.size(); i++) {
  67. auto key_value = map_arr.at(i).as_string();
  68. if (key_value.length() == 0) {
  69. buffer[i] = 0;
  70. } else if (key_value.length() == 1) {
  71. buffer[i] = key_value.characters()[0];
  72. } else {
  73. Utf8View m_utf8_view(key_value);
  74. buffer[i] = *m_utf8_view.begin();
  75. }
  76. }
  77. return buffer;
  78. }
  79. }