KeyboardMapperWidget.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. if (auto error_or = save(); error_or.is_error())
  28. show_error_to_user(error_or.release_error());
  29. if (!window()->is_modified())
  30. return true;
  31. }
  32. return result == GUI::MessageBox::ExecResult::No;
  33. }
  34. void KeyboardMapperWidget::create_frame()
  35. {
  36. set_fill_with_background_color(true);
  37. set_layout<GUI::VerticalBoxLayout>();
  38. layout()->set_margins(4);
  39. auto& main_widget = add<GUI::Widget>();
  40. main_widget.set_relative_rect(0, 0, 200, 200);
  41. m_keys.resize(KEY_COUNT);
  42. for (unsigned i = 0; i < KEY_COUNT; i++) {
  43. Gfx::IntRect rect = { keys[i].x, keys[i].y, keys[i].width, keys[i].height };
  44. auto& tmp_button = main_widget.add<KeyButton>();
  45. tmp_button.set_relative_rect(rect);
  46. tmp_button.set_text(keys[i].name);
  47. tmp_button.set_enabled(keys[i].enabled);
  48. tmp_button.on_click = [this, &tmp_button]() {
  49. DeprecatedString value;
  50. if (GUI::InputBox::show(window(), value, "New Character:"sv, "Select Character"sv) == GUI::InputBox::ExecResult::OK) {
  51. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  52. VERIFY(i > 0);
  53. auto index = keys[i].map_index;
  54. VERIFY(index > 0);
  55. tmp_button.set_text(value);
  56. u32* map = map_from_name(m_current_map_name);
  57. if (value.length() == 0)
  58. map[index] = '\0'; // Empty string
  59. else
  60. map[index] = value[0];
  61. window()->set_modified(true);
  62. }
  63. };
  64. m_keys.insert(i, &tmp_button);
  65. }
  66. // Action Buttons
  67. auto& bottom_widget = add<GUI::Widget>();
  68. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  69. bottom_widget.set_fixed_height(40);
  70. // Map Selection
  71. m_map_group = bottom_widget.add<GUI::Widget>();
  72. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  73. m_map_group->set_fixed_width(450);
  74. add_map_radio_button("map"sv, "Default"sv);
  75. add_map_radio_button("shift_map"sv, "Shift"sv);
  76. add_map_radio_button("altgr_map"sv, "AltGr"sv);
  77. add_map_radio_button("alt_map"sv, "Alt"sv);
  78. add_map_radio_button("shift_altgr_map"sv, "Shift+AltGr"sv);
  79. bottom_widget.layout()->add_spacer();
  80. }
  81. void KeyboardMapperWidget::add_map_radio_button(const StringView map_name, const StringView button_text)
  82. {
  83. auto& map_radio_button = m_map_group->add<GUI::RadioButton>(button_text);
  84. map_radio_button.set_name(map_name);
  85. map_radio_button.on_checked = [map_name, this](bool) {
  86. set_current_map(map_name);
  87. };
  88. }
  89. u32* KeyboardMapperWidget::map_from_name(const StringView map_name)
  90. {
  91. u32* map;
  92. if (map_name == "map"sv) {
  93. map = m_character_map.map;
  94. } else if (map_name == "shift_map"sv) {
  95. map = m_character_map.shift_map;
  96. } else if (map_name == "alt_map"sv) {
  97. map = m_character_map.alt_map;
  98. } else if (map_name == "altgr_map"sv) {
  99. map = m_character_map.altgr_map;
  100. } else if (map_name == "shift_altgr_map"sv) {
  101. map = m_character_map.shift_altgr_map;
  102. } else {
  103. VERIFY_NOT_REACHED();
  104. }
  105. return map;
  106. }
  107. ErrorOr<void> KeyboardMapperWidget::load_map_from_file(DeprecatedString const& filename)
  108. {
  109. auto character_map = TRY(Keyboard::CharacterMapFile::load_from_file(filename));
  110. m_filename = filename;
  111. m_character_map = character_map;
  112. set_current_map("map");
  113. for (auto& widget : m_map_group->child_widgets()) {
  114. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  115. radio_button.set_checked(radio_button.name() == "map");
  116. }
  117. window()->set_modified(false);
  118. update_window_title();
  119. return {};
  120. }
  121. ErrorOr<void> KeyboardMapperWidget::load_map_from_system()
  122. {
  123. auto character_map = TRY(Keyboard::CharacterMap::fetch_system_map());
  124. m_filename = DeprecatedString::formatted("/res/keymaps/{}.json", character_map.character_map_name());
  125. m_character_map = character_map.character_map_data();
  126. set_current_map("map");
  127. for (auto& widget : m_map_group->child_widgets()) {
  128. auto& radio_button = static_cast<GUI::RadioButton&>(widget);
  129. radio_button.set_checked(radio_button.name() == "map");
  130. }
  131. update_window_title();
  132. return {};
  133. }
  134. ErrorOr<void> KeyboardMapperWidget::save()
  135. {
  136. return save_to_file(m_filename);
  137. }
  138. ErrorOr<void> KeyboardMapperWidget::save_to_file(StringView filename)
  139. {
  140. JsonObject map_json;
  141. auto add_array = [&](DeprecatedString 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_deprecated_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. DeprecatedString file_content = map_json.to_deprecated_string();
  159. auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Write));
  160. TRY(file->write(file_content.bytes()));
  161. file->close();
  162. window()->set_modified(false);
  163. m_filename = filename;
  164. update_window_title();
  165. return {};
  166. }
  167. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  168. {
  169. for (int i = 0; i < KEY_COUNT; i++) {
  170. if (keys[i].scancode != event.scancode())
  171. continue;
  172. auto& tmp_button = m_keys.at(i);
  173. tmp_button->set_pressed(true);
  174. tmp_button->update();
  175. break;
  176. }
  177. if (m_automatic_modifier && event.modifiers() > 0) {
  178. update_modifier_radio_buttons(event);
  179. }
  180. event.ignore();
  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 DeprecatedString 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_deprecated_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"sv);
  215. window()->set_title(sb.to_deprecated_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. }