KeyboardMapperWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2021, Rasmus Nylander <RasmusNylander.SerenityOS@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "KeyboardMapperWidget.h"
  8. #include "KeyPositions.h"
  9. #include <LibCore/Stream.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/InputBox.h>
  12. #include <LibGUI/MessageBox.h>
  13. #include <LibGUI/RadioButton.h>
  14. #include <LibKeyboard/CharacterMap.h>
  15. #include <LibKeyboard/CharacterMapFile.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 = map_from_name(m_current_map_name);
  46. if (value.length() == 0)
  47. map[index] = '\0'; // Empty string
  48. else
  49. map[index] = value[0];
  50. m_modified = true;
  51. update_window_title();
  52. }
  53. };
  54. m_keys.insert(i, &tmp_button);
  55. }
  56. // Action Buttons
  57. auto& bottom_widget = add<GUI::Widget>();
  58. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  59. bottom_widget.set_fixed_height(40);
  60. // Map Selection
  61. m_map_group = bottom_widget.add<GUI::Widget>();
  62. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  63. m_map_group->set_fixed_width(450);
  64. add_map_radio_button("map", "Default");
  65. add_map_radio_button("shift_map", "Shift");
  66. add_map_radio_button("altgr_map", "AltGr");
  67. add_map_radio_button("alt_map", "Alt");
  68. add_map_radio_button("shift_altgr_map", "Shift+AltGr");
  69. bottom_widget.layout()->add_spacer();
  70. }
  71. void KeyboardMapperWidget::add_map_radio_button(const StringView map_name, const StringView button_text)
  72. {
  73. auto& map_radio_button = m_map_group->add<GUI::RadioButton>(button_text);
  74. map_radio_button.set_name(map_name);
  75. map_radio_button.on_checked = [map_name, this](bool) {
  76. set_current_map(map_name);
  77. };
  78. }
  79. u32* KeyboardMapperWidget::map_from_name(const StringView map_name)
  80. {
  81. u32* map;
  82. if (map_name == "map"sv) {
  83. map = m_character_map.map;
  84. } else if (map_name == "shift_map"sv) {
  85. map = m_character_map.shift_map;
  86. } else if (map_name == "alt_map"sv) {
  87. map = m_character_map.alt_map;
  88. } else if (map_name == "altgr_map"sv) {
  89. map = m_character_map.altgr_map;
  90. } else if (map_name == "shift_altgr_map"sv) {
  91. map = m_character_map.shift_altgr_map;
  92. } else {
  93. VERIFY_NOT_REACHED();
  94. }
  95. return map;
  96. }
  97. ErrorOr<void> KeyboardMapperWidget::load_map_from_file(const String& filename)
  98. {
  99. auto character_map = TRY(Keyboard::CharacterMapFile::load_from_file(filename));
  100. m_filename = filename;
  101. m_character_map = character_map;
  102. set_current_map("map");
  103. for (auto& widget : m_map_group->child_widgets()) {
  104. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  105. radio_button.set_checked(radio_button.name() == "map");
  106. }
  107. update_window_title();
  108. return {};
  109. }
  110. ErrorOr<void> KeyboardMapperWidget::load_map_from_system()
  111. {
  112. auto character_map = TRY(Keyboard::CharacterMap::fetch_system_map());
  113. m_filename = String::formatted("/res/keymaps/{}.json", character_map.character_map_name());
  114. m_character_map = character_map.character_map_data();
  115. set_current_map("map");
  116. for (auto& widget : m_map_group->child_widgets()) {
  117. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  118. radio_button.set_checked(radio_button.name() == "map");
  119. }
  120. update_window_title();
  121. return {};
  122. }
  123. ErrorOr<void> KeyboardMapperWidget::save()
  124. {
  125. return save_to_file(m_filename);
  126. }
  127. ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
  128. {
  129. JsonObject map_json;
  130. auto add_array = [&](String name, u32* values) {
  131. JsonArray items;
  132. for (int i = 0; i < 90; i++) {
  133. StringBuilder sb;
  134. if (values[i])
  135. sb.append_code_point(values[i]);
  136. JsonValue val(sb.to_string());
  137. items.append(move(val));
  138. }
  139. map_json.set(name, move(items));
  140. };
  141. add_array("map", m_character_map.map);
  142. add_array("shift_map", m_character_map.shift_map);
  143. add_array("alt_map", m_character_map.alt_map);
  144. add_array("altgr_map", m_character_map.altgr_map);
  145. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  146. // Write to file.
  147. String file_content = map_json.to_string();
  148. auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Write));
  149. TRY(file->write(file_content.bytes()));
  150. file->close();
  151. m_modified = false;
  152. m_filename = filename;
  153. update_window_title();
  154. return {};
  155. }
  156. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  157. {
  158. for (int i = 0; i < KEY_COUNT; i++) {
  159. if (keys[i].scancode != event.scancode())
  160. continue;
  161. auto& tmp_button = m_keys.at(i);
  162. tmp_button->set_pressed(true);
  163. tmp_button->update();
  164. break;
  165. }
  166. if (m_automatic_modifier && event.modifiers() > 0) {
  167. update_modifier_radio_buttons(event);
  168. }
  169. }
  170. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  171. {
  172. for (int i = 0; i < KEY_COUNT; i++) {
  173. if (keys[i].scancode != event.scancode())
  174. continue;
  175. auto& tmp_button = m_keys.at(i);
  176. tmp_button->set_pressed(false);
  177. tmp_button->update();
  178. break;
  179. }
  180. if (m_automatic_modifier) {
  181. update_modifier_radio_buttons(event);
  182. }
  183. }
  184. void KeyboardMapperWidget::set_current_map(const String current_map)
  185. {
  186. m_current_map_name = current_map;
  187. u32* map = map_from_name(m_current_map_name);
  188. for (unsigned k = 0; k < KEY_COUNT; k++) {
  189. auto index = keys[k].map_index;
  190. if (index == 0)
  191. continue;
  192. StringBuilder sb;
  193. sb.append_code_point(map[index]);
  194. m_keys.at(k)->set_text(sb.to_string());
  195. }
  196. this->update();
  197. }
  198. void KeyboardMapperWidget::update_window_title()
  199. {
  200. StringBuilder sb;
  201. sb.append(m_filename);
  202. if (m_modified)
  203. sb.append(" (*)");
  204. sb.append(" - Keyboard Mapper");
  205. window()->set_title(sb.to_string());
  206. }
  207. void KeyboardMapperWidget::show_error_to_user(Error error)
  208. {
  209. GUI::MessageBox::show_error(window(), error.string_literal());
  210. }
  211. void KeyboardMapperWidget::set_automatic_modifier(bool checked)
  212. {
  213. m_automatic_modifier = checked;
  214. }
  215. void KeyboardMapperWidget::update_modifier_radio_buttons(GUI::KeyEvent& event)
  216. {
  217. GUI::RadioButton* radio_button;
  218. if (event.shift() && event.altgr()) {
  219. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("shift_altgr_map");
  220. } else if (event.altgr()) {
  221. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("altgr_map");
  222. } else if (event.alt()) {
  223. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("alt_map");
  224. } else if (event.shift()) {
  225. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("shift_map");
  226. } else {
  227. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("map");
  228. }
  229. if (radio_button)
  230. radio_button->set_checked(true);
  231. }