KeyboardMapperWidget.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "KeyboardMapperWidget.h"
  7. #include "KeyPositions.h"
  8. #include <LibCore/File.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/InputBox.h>
  11. #include <LibGUI/MessageBox.h>
  12. #include <LibGUI/RadioButton.h>
  13. #include <LibKeyboard/CharacterMap.h>
  14. #include <LibKeyboard/CharacterMapFile.h>
  15. #include <string.h>
  16. KeyboardMapperWidget::KeyboardMapperWidget()
  17. {
  18. create_frame();
  19. }
  20. KeyboardMapperWidget::~KeyboardMapperWidget()
  21. {
  22. }
  23. void KeyboardMapperWidget::create_frame()
  24. {
  25. set_fill_with_background_color(true);
  26. set_layout<GUI::VerticalBoxLayout>();
  27. layout()->set_margins(4);
  28. auto& main_widget = add<GUI::Widget>();
  29. main_widget.set_relative_rect(0, 0, 200, 200);
  30. m_keys.resize(KEY_COUNT);
  31. for (unsigned i = 0; i < KEY_COUNT; i++) {
  32. Gfx::IntRect rect = { keys[i].x, keys[i].y, keys[i].width, keys[i].height };
  33. auto& tmp_button = main_widget.add<KeyButton>();
  34. tmp_button.set_relative_rect(rect);
  35. tmp_button.set_text(keys[i].name);
  36. tmp_button.set_enabled(keys[i].enabled);
  37. tmp_button.on_click = [this, &tmp_button]() {
  38. String value;
  39. if (GUI::InputBox::show(window(), value, "New Character:", "Select Character") == GUI::InputBox::ExecOK) {
  40. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  41. VERIFY(i > 0);
  42. auto index = keys[i].map_index;
  43. VERIFY(index > 0);
  44. tmp_button.set_text(value);
  45. u32* map;
  46. if (m_current_map_name == "map") {
  47. map = m_character_map.map;
  48. } else if (m_current_map_name == "shift_map") {
  49. map = m_character_map.shift_map;
  50. } else if (m_current_map_name == "alt_map") {
  51. map = m_character_map.alt_map;
  52. } else if (m_current_map_name == "altgr_map") {
  53. map = m_character_map.altgr_map;
  54. } else if (m_current_map_name == "shift_altgr_map") {
  55. map = m_character_map.shift_altgr_map;
  56. } else {
  57. VERIFY_NOT_REACHED();
  58. }
  59. if (value.length() == 0)
  60. map[index] = '\0'; // Empty string
  61. else
  62. map[index] = value[0];
  63. m_modified = true;
  64. update_window_title();
  65. }
  66. };
  67. m_keys.insert(i, &tmp_button);
  68. }
  69. // Action Buttons
  70. auto& bottom_widget = add<GUI::Widget>();
  71. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  72. bottom_widget.set_fixed_height(40);
  73. // Map Selection
  74. m_map_group = bottom_widget.add<GUI::Widget>();
  75. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  76. m_map_group->set_fixed_width(450);
  77. auto& radio_map = m_map_group->add<GUI::RadioButton>("Default");
  78. radio_map.set_name("map");
  79. radio_map.on_checked = [&](bool) {
  80. set_current_map("map");
  81. };
  82. auto& radio_shift = m_map_group->add<GUI::RadioButton>("Shift");
  83. radio_shift.set_name("shift_map");
  84. radio_shift.on_checked = [this](bool) {
  85. set_current_map("shift_map");
  86. };
  87. auto& radio_altgr = m_map_group->add<GUI::RadioButton>("AltGr");
  88. radio_altgr.set_name("altgr_map");
  89. radio_altgr.on_checked = [this](bool) {
  90. set_current_map("altgr_map");
  91. };
  92. auto& radio_alt = m_map_group->add<GUI::RadioButton>("Alt");
  93. radio_alt.set_name("alt_map");
  94. radio_alt.on_checked = [this](bool) {
  95. set_current_map("alt_map");
  96. };
  97. auto& radio_shift_altgr = m_map_group->add<GUI::RadioButton>("Shift+AltGr");
  98. radio_shift_altgr.set_name("shift_altgr_map");
  99. radio_shift_altgr.on_checked = [this](bool) {
  100. set_current_map("shift_altgr_map");
  101. };
  102. bottom_widget.layout()->add_spacer();
  103. }
  104. void KeyboardMapperWidget::load_from_file(String filename)
  105. {
  106. auto result = Keyboard::CharacterMapFile::load_from_file(filename);
  107. if (!result.has_value()) {
  108. auto error_message = String::formatted("Failed to load character map from file {}", filename);
  109. GUI::MessageBox::show(window(), error_message, "Error", GUI::MessageBox::Type::Error);
  110. return;
  111. }
  112. m_filename = filename;
  113. m_character_map = result.value();
  114. set_current_map("map");
  115. for (auto& widget : m_map_group->child_widgets()) {
  116. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  117. radio_button.set_checked(radio_button.name() == "map");
  118. }
  119. update_window_title();
  120. }
  121. void KeyboardMapperWidget::load_from_system()
  122. {
  123. auto result = Keyboard::CharacterMap::fetch_system_map();
  124. VERIFY(!result.is_error());
  125. m_filename = String::formatted("/res/keymaps/{}.json", result.value().character_map_name());
  126. m_character_map = result.value().character_map_data();
  127. set_current_map("map");
  128. for (auto& widget : m_map_group->child_widgets()) {
  129. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  130. radio_button.set_checked(radio_button.name() == "map");
  131. }
  132. update_window_title();
  133. }
  134. void KeyboardMapperWidget::save()
  135. {
  136. save_to_file(m_filename);
  137. }
  138. void KeyboardMapperWidget::save_to_file(const StringView& filename)
  139. {
  140. JsonObject map_json;
  141. auto add_array = [&](String name, u32* values) {
  142. JsonArray items;
  143. for (int i = 0; i < 90; i++) {
  144. StringBuilder sb;
  145. if (values[i])
  146. sb.append_code_point(values[i]);
  147. JsonValue val(sb.to_string());
  148. items.append(move(val));
  149. }
  150. map_json.set(name, move(items));
  151. };
  152. add_array("map", m_character_map.map);
  153. add_array("shift_map", m_character_map.shift_map);
  154. add_array("alt_map", m_character_map.alt_map);
  155. add_array("altgr_map", m_character_map.altgr_map);
  156. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  157. // Write to file.
  158. String file_content = map_json.to_string();
  159. auto file = Core::File::construct(filename);
  160. file->open(Core::OpenMode::WriteOnly);
  161. if (!file->is_open()) {
  162. StringBuilder sb;
  163. sb.append("Failed to open ");
  164. sb.append(filename);
  165. sb.append(" for write. Error: ");
  166. sb.append(file->error_string());
  167. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  168. return;
  169. }
  170. bool result = file->write(file_content);
  171. if (!result) {
  172. int error_number = errno;
  173. StringBuilder sb;
  174. sb.append("Unable to save file. Error: ");
  175. sb.append(strerror(error_number));
  176. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  177. return;
  178. }
  179. m_modified = false;
  180. m_filename = filename;
  181. update_window_title();
  182. }
  183. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  184. {
  185. for (int i = 0; i < KEY_COUNT; i++) {
  186. auto& tmp_button = m_keys.at(i);
  187. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  188. tmp_button->update();
  189. }
  190. }
  191. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  192. {
  193. for (int i = 0; i < KEY_COUNT; i++) {
  194. if (keys[i].scancode == event.scancode()) {
  195. auto& tmp_button = m_keys.at(i);
  196. tmp_button->set_pressed(false);
  197. tmp_button->update();
  198. break;
  199. }
  200. }
  201. }
  202. void KeyboardMapperWidget::set_current_map(const String current_map)
  203. {
  204. m_current_map_name = current_map;
  205. u32* map;
  206. if (m_current_map_name == "map") {
  207. map = m_character_map.map;
  208. } else if (m_current_map_name == "shift_map") {
  209. map = m_character_map.shift_map;
  210. } else if (m_current_map_name == "alt_map") {
  211. map = m_character_map.alt_map;
  212. } else if (m_current_map_name == "altgr_map") {
  213. map = m_character_map.altgr_map;
  214. } else if (m_current_map_name == "shift_altgr_map") {
  215. map = m_character_map.shift_altgr_map;
  216. } else {
  217. VERIFY_NOT_REACHED();
  218. }
  219. for (unsigned k = 0; k < KEY_COUNT; k++) {
  220. auto index = keys[k].map_index;
  221. if (index == 0)
  222. continue;
  223. StringBuilder sb;
  224. sb.append_code_point(map[index]);
  225. m_keys.at(k)->set_text(sb.to_string());
  226. }
  227. this->update();
  228. }
  229. void KeyboardMapperWidget::update_window_title()
  230. {
  231. StringBuilder sb;
  232. sb.append(m_filename);
  233. if (m_modified)
  234. sb.append(" (*)");
  235. sb.append(" - Keyboard Mapper");
  236. window()->set_title(sb.to_string());
  237. }