CharacterMapFile.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "CharacterMapFile.h"
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/Utf8View.h>
  29. #include <LibCore/File.h>
  30. namespace Keyboard {
  31. Optional<CharacterMapData> CharacterMapFile::load_from_file(const String& file_name)
  32. {
  33. auto path = file_name;
  34. if (!path.ends_with(".json")) {
  35. StringBuilder full_path;
  36. full_path.append("/res/keymaps/");
  37. full_path.append(file_name);
  38. full_path.append(".json");
  39. path = full_path.to_string();
  40. }
  41. auto file = Core::File::construct(path);
  42. file->open(Core::IODevice::ReadOnly);
  43. if (!file->is_open()) {
  44. dbgln("Failed to open {}: {}", file_name, file->error_string());
  45. return {};
  46. }
  47. auto file_contents = file->read_all();
  48. auto json_result = JsonValue::from_string(file_contents);
  49. if (!json_result.has_value())
  50. return {};
  51. auto json = json_result.value().as_object();
  52. Vector<u32> map = read_map(json, "map");
  53. Vector<u32> shift_map = read_map(json, "shift_map");
  54. Vector<u32> alt_map = read_map(json, "alt_map");
  55. Vector<u32> altgr_map = read_map(json, "altgr_map");
  56. Vector<u32> shift_altgr_map = read_map(json, "shift_altgr_map");
  57. CharacterMapData character_map;
  58. for (int i = 0; i < CHAR_MAP_SIZE; i++) {
  59. character_map.map[i] = map.at(i);
  60. character_map.shift_map[i] = shift_map.at(i);
  61. character_map.alt_map[i] = alt_map.at(i);
  62. if (altgr_map.is_empty()) {
  63. // AltGr map was not found, using Alt map as fallback.
  64. character_map.altgr_map[i] = alt_map.at(i);
  65. } else {
  66. character_map.altgr_map[i] = altgr_map.at(i);
  67. }
  68. if (shift_altgr_map.is_empty()) {
  69. // Shift+AltGr map was not found, using Alt map as fallback.
  70. character_map.shift_altgr_map[i] = alt_map.at(i);
  71. } else {
  72. character_map.shift_altgr_map[i] = shift_altgr_map.at(i);
  73. }
  74. }
  75. return character_map;
  76. }
  77. Vector<u32> CharacterMapFile::read_map(const JsonObject& json, const String& name)
  78. {
  79. if (!json.has(name))
  80. return {};
  81. Vector<u32> buffer;
  82. buffer.resize(CHAR_MAP_SIZE);
  83. auto map_arr = json.get(name).as_array();
  84. for (int i = 0; i < map_arr.size(); i++) {
  85. auto key_value = map_arr.at(i).as_string();
  86. if (key_value.length() == 0) {
  87. buffer[i] = 0;
  88. } else if (key_value.length() == 1) {
  89. buffer[i] = key_value.characters()[0];
  90. } else {
  91. Utf8View m_utf8_view(key_value.characters());
  92. buffer[i] = *m_utf8_view.begin();
  93. }
  94. }
  95. return buffer;
  96. }
  97. }