KeyboardMapperWidget.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/CharacterMapFile.h>
  34. #include <fcntl.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. KeyboardMapperWidget::KeyboardMapperWidget()
  38. {
  39. create_frame();
  40. }
  41. KeyboardMapperWidget::~KeyboardMapperWidget()
  42. {
  43. }
  44. void KeyboardMapperWidget::create_frame()
  45. {
  46. set_fill_with_background_color(true);
  47. set_layout<GUI::VerticalBoxLayout>();
  48. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  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 {
  77. ASSERT_NOT_REACHED();
  78. }
  79. if (value.length() == 0)
  80. map[index] = '\0'; // Empty string
  81. else
  82. map[index] = value[0];
  83. m_modified = true;
  84. update_window_title();
  85. }
  86. };
  87. m_keys.insert(i, &tmp_button);
  88. }
  89. // Action Buttons
  90. auto& bottom_widget = add<GUI::Widget>();
  91. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  92. bottom_widget.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  93. bottom_widget.set_preferred_size(0, 40);
  94. // Map Selection
  95. m_map_group = bottom_widget.add<GUI::Widget>();
  96. m_map_group->set_layout<GUI::HorizontalBoxLayout>();
  97. m_map_group->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  98. m_map_group->set_preferred_size(250, 0);
  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. bottom_widget.layout()->add_spacer();
  120. auto& ok_button = bottom_widget.add<GUI::Button>();
  121. ok_button.set_text("Save");
  122. ok_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  123. ok_button.set_preferred_size(80, 0);
  124. ok_button.on_click = [this](auto) {
  125. save();
  126. };
  127. }
  128. void KeyboardMapperWidget::load_from_file(String file_name)
  129. {
  130. auto result = Keyboard::CharacterMapFile::load_from_file(file_name);
  131. if (!result.has_value()) {
  132. ASSERT_NOT_REACHED();
  133. }
  134. m_file_name = file_name;
  135. m_character_map = result.value();
  136. set_current_map("map");
  137. for (Widget* widget : m_map_group->child_widgets()) {
  138. auto radio_button = (GUI::RadioButton*)widget;
  139. radio_button->set_checked(radio_button->name() == "map");
  140. }
  141. update_window_title();
  142. }
  143. void KeyboardMapperWidget::save()
  144. {
  145. save_to_file(m_file_name);
  146. }
  147. void KeyboardMapperWidget::save_to_file(const StringView& file_name)
  148. {
  149. JsonObject map_json;
  150. auto add_array = [&](String name, u32* values) {
  151. JsonArray items;
  152. for (int i = 0; i < 90; i++) {
  153. AK::StringBuilder sb;
  154. sb.append(values[i]);
  155. JsonValue val(sb.to_string());
  156. items.append(move(val));
  157. }
  158. map_json.set(name, move(items));
  159. };
  160. add_array("map", m_character_map.map);
  161. add_array("shift_map", m_character_map.shift_map);
  162. add_array("alt_map", m_character_map.alt_map);
  163. add_array("altgr_map", m_character_map.altgr_map);
  164. // Write to file.
  165. String file_content = map_json.to_string();
  166. auto file = Core::File::construct(file_name);
  167. file->open(Core::IODevice::WriteOnly);
  168. if (!file->is_open()) {
  169. StringBuilder sb;
  170. sb.append("Failed to open ");
  171. sb.append(file_name);
  172. sb.append(" for write. Error: ");
  173. sb.append(file->error_string());
  174. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  175. return;
  176. }
  177. bool result = file->write(file_content);
  178. if (!result) {
  179. int error_number = errno;
  180. StringBuilder sb;
  181. sb.append("Unable to save file. Error: ");
  182. sb.append(strerror(error_number));
  183. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  184. return;
  185. }
  186. m_modified = false;
  187. m_file_name = file_name;
  188. update_window_title();
  189. }
  190. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  191. {
  192. for (int i = 0; i < KEY_COUNT; i++) {
  193. auto& tmp_button = m_keys.at(i);
  194. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  195. tmp_button->update();
  196. }
  197. }
  198. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  199. {
  200. for (int i = 0; i < KEY_COUNT; i++) {
  201. if (keys[i].scancode == event.scancode()) {
  202. auto& tmp_button = m_keys.at(i);
  203. tmp_button->set_pressed(false);
  204. tmp_button->update();
  205. break;
  206. }
  207. }
  208. }
  209. void KeyboardMapperWidget::set_current_map(const String current_map)
  210. {
  211. m_current_map_name = current_map;
  212. u32* map;
  213. if (m_current_map_name == "map") {
  214. map = m_character_map.map;
  215. } else if (m_current_map_name == "shift_map") {
  216. map = m_character_map.shift_map;
  217. } else if (m_current_map_name == "alt_map") {
  218. map = m_character_map.alt_map;
  219. } else if (m_current_map_name == "altgr_map") {
  220. map = m_character_map.altgr_map;
  221. } else {
  222. ASSERT_NOT_REACHED();
  223. }
  224. for (unsigned k = 0; k < KEY_COUNT; k++) {
  225. auto index = keys[k].map_index;
  226. if (index == 0)
  227. continue;
  228. AK::StringBuilder sb;
  229. sb.append_code_point(map[index]);
  230. m_keys.at(k)->set_text(sb.to_string());
  231. }
  232. this->update();
  233. }
  234. void KeyboardMapperWidget::update_window_title()
  235. {
  236. StringBuilder sb;
  237. sb.append(m_file_name);
  238. if (m_modified)
  239. sb.append(" (*)");
  240. sb.append(" - KeyboardMapper");
  241. window()->set_title(sb.to_string());
  242. }