KeyboardMapperWidget.cpp 8.0 KB

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