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