KeyboardMapperWidget.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "KeyboardMapperWidget.h"
  27. #include "KeyPositions.h"
  28. #include <LibCore/File.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/InputBox.h>
  31. #include <LibGUI/MessageBox.h>
  32. #include <LibGUI/RadioButton.h>
  33. #include <LibKeyboard/CharacterMap.h>
  34. #include <LibKeyboard/CharacterMapFile.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. KeyboardMapperWidget::KeyboardMapperWidget()
  39. {
  40. create_frame();
  41. }
  42. KeyboardMapperWidget::~KeyboardMapperWidget()
  43. {
  44. }
  45. void KeyboardMapperWidget::create_frame()
  46. {
  47. set_fill_with_background_color(true);
  48. set_layout<GUI::VerticalBoxLayout>();
  49. layout()->set_margins({ 4, 4, 4, 4 });
  50. auto& main_widget = add<GUI::Widget>();
  51. main_widget.set_relative_rect(0, 0, 200, 200);
  52. m_keys.resize(KEY_COUNT);
  53. for (unsigned i = 0; i < KEY_COUNT; i++) {
  54. Gfx::IntRect rect = { keys[i].x, keys[i].y, keys[i].width, keys[i].height };
  55. auto& tmp_button = main_widget.add<KeyButton>();
  56. tmp_button.set_relative_rect(rect);
  57. tmp_button.set_text(keys[i].name);
  58. tmp_button.set_enabled(keys[i].enabled);
  59. tmp_button.on_click = [this, &tmp_button]() {
  60. String value;
  61. if (GUI::InputBox::show(value, window(), "New Character:", "Select Character") == GUI::InputBox::ExecOK) {
  62. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  63. ASSERT(i > 0);
  64. auto index = keys[i].map_index;
  65. ASSERT(index > 0);
  66. tmp_button.set_text(value);
  67. u32* map;
  68. if (m_current_map_name == "map") {
  69. map = m_character_map.map;
  70. } else if (m_current_map_name == "shift_map") {
  71. map = m_character_map.shift_map;
  72. } else if (m_current_map_name == "alt_map") {
  73. map = m_character_map.alt_map;
  74. } else if (m_current_map_name == "altgr_map") {
  75. map = m_character_map.altgr_map;
  76. } else if (m_current_map_name == "shift_altgr_map") {
  77. map = m_character_map.shift_altgr_map;
  78. } else {
  79. ASSERT_NOT_REACHED();
  80. }
  81. if (value.length() == 0)
  82. map[index] = '\0'; // Empty string
  83. else
  84. map[index] = value[0];
  85. m_modified = true;
  86. update_window_title();
  87. }
  88. };
  89. m_keys.insert(i, &tmp_button);
  90. }
  91. // Action Buttons
  92. auto& bottom_widget = add<GUI::Widget>();
  93. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  94. bottom_widget.set_fixed_height(40);
  95. // Map Selection
  96. m_map_group = bottom_widget.add<GUI::Widget>();
  97. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  98. m_map_group->set_fixed_width(450);
  99. auto& radio_map = m_map_group->add<GUI::RadioButton>("Default");
  100. radio_map.set_name("map");
  101. radio_map.on_checked = [&](bool) {
  102. set_current_map("map");
  103. };
  104. auto& radio_shift = m_map_group->add<GUI::RadioButton>("Shift");
  105. radio_shift.set_name("shift_map");
  106. radio_shift.on_checked = [this](bool) {
  107. set_current_map("shift_map");
  108. };
  109. auto& radio_altgr = m_map_group->add<GUI::RadioButton>("AltGr");
  110. radio_altgr.set_name("altgr_map");
  111. radio_altgr.on_checked = [this](bool) {
  112. set_current_map("altgr_map");
  113. };
  114. auto& radio_alt = m_map_group->add<GUI::RadioButton>("Alt");
  115. radio_alt.set_name("alt_map");
  116. radio_alt.on_checked = [this](bool) {
  117. set_current_map("alt_map");
  118. };
  119. auto& radio_shift_altgr = m_map_group->add<GUI::RadioButton>("Shift+AltGr");
  120. radio_shift_altgr.set_name("shift_altgr_map");
  121. radio_shift_altgr.on_checked = [this](bool) {
  122. set_current_map("shift_altgr_map");
  123. };
  124. bottom_widget.layout()->add_spacer();
  125. }
  126. void KeyboardMapperWidget::load_from_file(String file_name)
  127. {
  128. auto result = Keyboard::CharacterMapFile::load_from_file(file_name);
  129. ASSERT(result.has_value());
  130. m_file_name = file_name;
  131. m_character_map = result.value();
  132. set_current_map("map");
  133. for (Widget* widget : m_map_group->child_widgets()) {
  134. auto radio_button = (GUI::RadioButton*)widget;
  135. radio_button->set_checked(radio_button->name() == "map");
  136. }
  137. update_window_title();
  138. }
  139. void KeyboardMapperWidget::load_from_system()
  140. {
  141. auto result = Keyboard::CharacterMap::fetch_system_map();
  142. ASSERT(!result.is_error());
  143. m_file_name = String::formatted("/res/keymaps/{}.json", result.value().character_map_name());
  144. m_character_map = result.value().character_map_data();
  145. set_current_map("map");
  146. for (Widget* widget : m_map_group->child_widgets()) {
  147. auto radio_button = (GUI::RadioButton*)widget;
  148. radio_button->set_checked(radio_button->name() == "map");
  149. }
  150. update_window_title();
  151. }
  152. void KeyboardMapperWidget::save()
  153. {
  154. save_to_file(m_file_name);
  155. }
  156. void KeyboardMapperWidget::save_to_file(const StringView& file_name)
  157. {
  158. JsonObject map_json;
  159. auto add_array = [&](String name, u32* values) {
  160. JsonArray items;
  161. for (int i = 0; i < 90; i++) {
  162. AK::StringBuilder sb;
  163. if (values[i])
  164. sb.append_code_point(values[i]);
  165. JsonValue val(sb.to_string());
  166. items.append(move(val));
  167. }
  168. map_json.set(name, move(items));
  169. };
  170. add_array("map", m_character_map.map);
  171. add_array("shift_map", m_character_map.shift_map);
  172. add_array("alt_map", m_character_map.alt_map);
  173. add_array("altgr_map", m_character_map.altgr_map);
  174. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  175. // Write to file.
  176. String file_content = map_json.to_string();
  177. auto file = Core::File::construct(file_name);
  178. file->open(Core::IODevice::WriteOnly);
  179. if (!file->is_open()) {
  180. StringBuilder sb;
  181. sb.append("Failed to open ");
  182. sb.append(file_name);
  183. sb.append(" for write. Error: ");
  184. sb.append(file->error_string());
  185. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  186. return;
  187. }
  188. bool result = file->write(file_content);
  189. if (!result) {
  190. int error_number = errno;
  191. StringBuilder sb;
  192. sb.append("Unable to save file. Error: ");
  193. sb.append(strerror(error_number));
  194. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  195. return;
  196. }
  197. m_modified = false;
  198. m_file_name = file_name;
  199. update_window_title();
  200. }
  201. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  202. {
  203. for (int i = 0; i < KEY_COUNT; i++) {
  204. auto& tmp_button = m_keys.at(i);
  205. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  206. tmp_button->update();
  207. }
  208. }
  209. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  210. {
  211. for (int i = 0; i < KEY_COUNT; i++) {
  212. if (keys[i].scancode == event.scancode()) {
  213. auto& tmp_button = m_keys.at(i);
  214. tmp_button->set_pressed(false);
  215. tmp_button->update();
  216. break;
  217. }
  218. }
  219. }
  220. void KeyboardMapperWidget::set_current_map(const String current_map)
  221. {
  222. m_current_map_name = current_map;
  223. u32* map;
  224. if (m_current_map_name == "map") {
  225. map = m_character_map.map;
  226. } else if (m_current_map_name == "shift_map") {
  227. map = m_character_map.shift_map;
  228. } else if (m_current_map_name == "alt_map") {
  229. map = m_character_map.alt_map;
  230. } else if (m_current_map_name == "altgr_map") {
  231. map = m_character_map.altgr_map;
  232. } else if (m_current_map_name == "shift_altgr_map") {
  233. map = m_character_map.shift_altgr_map;
  234. } else {
  235. ASSERT_NOT_REACHED();
  236. }
  237. for (unsigned k = 0; k < KEY_COUNT; k++) {
  238. auto index = keys[k].map_index;
  239. if (index == 0)
  240. continue;
  241. AK::StringBuilder sb;
  242. sb.append_code_point(map[index]);
  243. m_keys.at(k)->set_text(sb.to_string());
  244. }
  245. this->update();
  246. }
  247. void KeyboardMapperWidget::update_window_title()
  248. {
  249. StringBuilder sb;
  250. sb.append(m_file_name);
  251. if (m_modified)
  252. sb.append(" (*)");
  253. sb.append(" - KeyboardMapper");
  254. window()->set_title(sb.to_string());
  255. }