KeyboardMapperWidget.cpp 7.5 KB

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