KeyboardMapperWidget.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. bool KeyboardMapperWidget::request_close()
  22. {
  23. if (!window()->is_modified())
  24. return true;
  25. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), m_filename);
  26. if (result == GUI::MessageBox::ExecResult::Yes) {
  27. ErrorOr<void> error_or = save();
  28. if (error_or.is_error())
  29. show_error_to_user(error_or.error());
  30. if (!window()->is_modified())
  31. return true;
  32. }
  33. return result == GUI::MessageBox::ExecResult::No;
  34. }
  35. void KeyboardMapperWidget::create_frame()
  36. {
  37. set_fill_with_background_color(true);
  38. set_layout<GUI::VerticalBoxLayout>();
  39. layout()->set_margins(4);
  40. auto& main_widget = add<GUI::Widget>();
  41. main_widget.set_relative_rect(0, 0, 200, 200);
  42. m_keys.resize(KEY_COUNT);
  43. for (unsigned i = 0; i < KEY_COUNT; i++) {
  44. Gfx::IntRect rect = { keys[i].x, keys[i].y, keys[i].width, keys[i].height };
  45. auto& tmp_button = main_widget.add<KeyButton>();
  46. tmp_button.set_relative_rect(rect);
  47. tmp_button.set_text(keys[i].name);
  48. tmp_button.set_enabled(keys[i].enabled);
  49. tmp_button.on_click = [this, &tmp_button]() {
  50. String value;
  51. if (GUI::InputBox::show(window(), value, "New Character:", "Select Character") == GUI::InputBox::ExecResult::OK) {
  52. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  53. VERIFY(i > 0);
  54. auto index = keys[i].map_index;
  55. VERIFY(index > 0);
  56. tmp_button.set_text(value);
  57. u32* map = map_from_name(m_current_map_name);
  58. if (value.length() == 0)
  59. map[index] = '\0'; // Empty string
  60. else
  61. map[index] = value[0];
  62. window()->set_modified(true);
  63. }
  64. };
  65. m_keys.insert(i, &tmp_button);
  66. }
  67. // Action Buttons
  68. auto& bottom_widget = add<GUI::Widget>();
  69. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  70. bottom_widget.set_fixed_height(40);
  71. // Map Selection
  72. m_map_group = bottom_widget.add<GUI::Widget>();
  73. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  74. m_map_group->set_fixed_width(450);
  75. add_map_radio_button("map", "Default");
  76. add_map_radio_button("shift_map", "Shift");
  77. add_map_radio_button("altgr_map", "AltGr");
  78. add_map_radio_button("alt_map", "Alt");
  79. add_map_radio_button("shift_altgr_map", "Shift+AltGr");
  80. bottom_widget.layout()->add_spacer();
  81. }
  82. void KeyboardMapperWidget::add_map_radio_button(const StringView map_name, const StringView button_text)
  83. {
  84. auto& map_radio_button = m_map_group->add<GUI::RadioButton>(button_text);
  85. map_radio_button.set_name(map_name);
  86. map_radio_button.on_checked = [map_name, this](bool) {
  87. set_current_map(map_name);
  88. };
  89. }
  90. u32* KeyboardMapperWidget::map_from_name(const StringView map_name)
  91. {
  92. u32* map;
  93. if (map_name == "map"sv) {
  94. map = m_character_map.map;
  95. } else if (map_name == "shift_map"sv) {
  96. map = m_character_map.shift_map;
  97. } else if (map_name == "alt_map"sv) {
  98. map = m_character_map.alt_map;
  99. } else if (map_name == "altgr_map"sv) {
  100. map = m_character_map.altgr_map;
  101. } else if (map_name == "shift_altgr_map"sv) {
  102. map = m_character_map.shift_altgr_map;
  103. } else {
  104. VERIFY_NOT_REACHED();
  105. }
  106. return map;
  107. }
  108. ErrorOr<void> KeyboardMapperWidget::load_map_from_file(String const& filename)
  109. {
  110. auto character_map = TRY(Keyboard::CharacterMapFile::load_from_file(filename));
  111. m_filename = filename;
  112. m_character_map = character_map;
  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. window()->set_modified(false);
  119. update_window_title();
  120. return {};
  121. }
  122. ErrorOr<void> KeyboardMapperWidget::load_map_from_system()
  123. {
  124. auto character_map = TRY(Keyboard::CharacterMap::fetch_system_map());
  125. m_filename = String::formatted("/res/keymaps/{}.json", character_map.character_map_name());
  126. m_character_map = character_map.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. return {};
  134. }
  135. ErrorOr<void> KeyboardMapperWidget::save()
  136. {
  137. return save_to_file(m_filename);
  138. }
  139. ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
  140. {
  141. JsonObject map_json;
  142. auto add_array = [&](String name, u32* values) {
  143. JsonArray items;
  144. for (int i = 0; i < 90; i++) {
  145. StringBuilder sb;
  146. if (values[i])
  147. sb.append_code_point(values[i]);
  148. JsonValue val(sb.to_string());
  149. items.append(move(val));
  150. }
  151. map_json.set(name, move(items));
  152. };
  153. add_array("map", m_character_map.map);
  154. add_array("shift_map", m_character_map.shift_map);
  155. add_array("alt_map", m_character_map.alt_map);
  156. add_array("altgr_map", m_character_map.altgr_map);
  157. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  158. // Write to file.
  159. String file_content = map_json.to_string();
  160. auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Write));
  161. TRY(file->write(file_content.bytes()));
  162. file->close();
  163. window()->set_modified(false);
  164. m_filename = filename;
  165. update_window_title();
  166. return {};
  167. }
  168. void KeyboardMapperWidget::keydown_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(true);
  175. tmp_button->update();
  176. break;
  177. }
  178. if (m_automatic_modifier && event.modifiers() > 0) {
  179. update_modifier_radio_buttons(event);
  180. }
  181. }
  182. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  183. {
  184. for (int i = 0; i < KEY_COUNT; i++) {
  185. if (keys[i].scancode != event.scancode())
  186. continue;
  187. auto& tmp_button = m_keys.at(i);
  188. tmp_button->set_pressed(false);
  189. tmp_button->update();
  190. break;
  191. }
  192. if (m_automatic_modifier) {
  193. update_modifier_radio_buttons(event);
  194. }
  195. }
  196. void KeyboardMapperWidget::set_current_map(const String current_map)
  197. {
  198. m_current_map_name = current_map;
  199. u32* map = map_from_name(m_current_map_name);
  200. for (unsigned k = 0; k < KEY_COUNT; k++) {
  201. auto index = keys[k].map_index;
  202. if (index == 0)
  203. continue;
  204. StringBuilder sb;
  205. sb.append_code_point(map[index]);
  206. m_keys.at(k)->set_text(sb.to_string());
  207. }
  208. this->update();
  209. }
  210. void KeyboardMapperWidget::update_window_title()
  211. {
  212. StringBuilder sb;
  213. sb.append(m_filename);
  214. sb.append("[*] - Keyboard Mapper");
  215. window()->set_title(sb.to_string());
  216. }
  217. void KeyboardMapperWidget::show_error_to_user(Error error)
  218. {
  219. GUI::MessageBox::show_error(window(), error.string_literal());
  220. }
  221. void KeyboardMapperWidget::set_automatic_modifier(bool checked)
  222. {
  223. m_automatic_modifier = checked;
  224. }
  225. void KeyboardMapperWidget::update_modifier_radio_buttons(GUI::KeyEvent& event)
  226. {
  227. GUI::RadioButton* radio_button;
  228. if (event.shift() && event.altgr()) {
  229. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("shift_altgr_map");
  230. } else if (event.altgr()) {
  231. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("altgr_map");
  232. } else if (event.alt()) {
  233. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("alt_map");
  234. } else if (event.shift()) {
  235. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("shift_map");
  236. } else {
  237. radio_button = m_map_group->find_child_of_type_named<GUI::RadioButton>("map");
  238. }
  239. if (radio_button)
  240. radio_button->set_checked(true);
  241. }