KeyboardMapperWidget.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. layout()->set_margins({ 4, 4, 4, 4 });
  49. auto& main_widget = add<GUI::Widget>();
  50. main_widget.set_relative_rect(0, 0, 200, 200);
  51. m_keys.resize(KEY_COUNT);
  52. for (unsigned i = 0; i < KEY_COUNT; i++) {
  53. Gfx::IntRect rect = { keys[i].x, keys[i].y, keys[i].width, keys[i].height };
  54. auto& tmp_button = main_widget.add<KeyButton>();
  55. tmp_button.set_relative_rect(rect);
  56. tmp_button.set_text(keys[i].name);
  57. tmp_button.set_enabled(keys[i].enabled);
  58. tmp_button.on_click = [this, &tmp_button]() {
  59. String value;
  60. if (GUI::InputBox::show(value, window(), "New Character:", "Select Character") == GUI::InputBox::ExecOK) {
  61. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  62. ASSERT(i > 0);
  63. auto index = keys[i].map_index;
  64. ASSERT(index > 0);
  65. tmp_button.set_text(value);
  66. u32* map;
  67. if (m_current_map_name == "map") {
  68. map = m_character_map.map;
  69. } else if (m_current_map_name == "shift_map") {
  70. map = m_character_map.shift_map;
  71. } else if (m_current_map_name == "alt_map") {
  72. map = m_character_map.alt_map;
  73. } else if (m_current_map_name == "altgr_map") {
  74. map = m_character_map.altgr_map;
  75. } else if (m_current_map_name == "shift_altgr_map") {
  76. map = m_character_map.shift_altgr_map;
  77. } else {
  78. ASSERT_NOT_REACHED();
  79. }
  80. if (value.length() == 0)
  81. map[index] = '\0'; // Empty string
  82. else
  83. map[index] = value[0];
  84. m_modified = true;
  85. update_window_title();
  86. }
  87. };
  88. m_keys.insert(i, &tmp_button);
  89. }
  90. // Action Buttons
  91. auto& bottom_widget = add<GUI::Widget>();
  92. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  93. bottom_widget.set_fixed_height(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_fixed_width(450);
  98. auto& radio_map = m_map_group->add<GUI::RadioButton>("Default");
  99. radio_map.set_name("map");
  100. radio_map.on_checked = [&](bool) {
  101. set_current_map("map");
  102. };
  103. auto& radio_shift = m_map_group->add<GUI::RadioButton>("Shift");
  104. radio_shift.set_name("shift_map");
  105. radio_shift.on_checked = [this](bool) {
  106. set_current_map("shift_map");
  107. };
  108. auto& radio_altgr = m_map_group->add<GUI::RadioButton>("AltGr");
  109. radio_altgr.set_name("altgr_map");
  110. radio_altgr.on_checked = [this](bool) {
  111. set_current_map("altgr_map");
  112. };
  113. auto& radio_alt = m_map_group->add<GUI::RadioButton>("Alt");
  114. radio_alt.set_name("alt_map");
  115. radio_alt.on_checked = [this](bool) {
  116. set_current_map("alt_map");
  117. };
  118. auto& radio_shift_altgr = m_map_group->add<GUI::RadioButton>("Shift+AltGr");
  119. radio_shift_altgr.set_name("shift_altgr_map");
  120. radio_shift_altgr.on_checked = [this](bool) {
  121. set_current_map("shift_altgr_map");
  122. };
  123. bottom_widget.layout()->add_spacer();
  124. auto& ok_button = bottom_widget.add<GUI::Button>();
  125. ok_button.set_text("Save");
  126. ok_button.set_fixed_width(80);
  127. ok_button.on_click = [this](auto) {
  128. save();
  129. };
  130. }
  131. void KeyboardMapperWidget::load_from_file(String file_name)
  132. {
  133. auto result = Keyboard::CharacterMapFile::load_from_file(file_name);
  134. if (!result.has_value()) {
  135. ASSERT_NOT_REACHED();
  136. }
  137. m_file_name = file_name;
  138. m_character_map = result.value();
  139. set_current_map("map");
  140. for (Widget* widget : m_map_group->child_widgets()) {
  141. auto radio_button = (GUI::RadioButton*)widget;
  142. radio_button->set_checked(radio_button->name() == "map");
  143. }
  144. update_window_title();
  145. }
  146. void KeyboardMapperWidget::save()
  147. {
  148. save_to_file(m_file_name);
  149. }
  150. void KeyboardMapperWidget::save_to_file(const StringView& file_name)
  151. {
  152. JsonObject map_json;
  153. auto add_array = [&](String name, u32* values) {
  154. JsonArray items;
  155. for (int i = 0; i < 90; i++) {
  156. AK::StringBuilder sb;
  157. sb.append(values[i]);
  158. JsonValue val(sb.to_string());
  159. items.append(move(val));
  160. }
  161. map_json.set(name, move(items));
  162. };
  163. add_array("map", m_character_map.map);
  164. add_array("shift_map", m_character_map.shift_map);
  165. add_array("alt_map", m_character_map.alt_map);
  166. add_array("altgr_map", m_character_map.altgr_map);
  167. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  168. // Write to file.
  169. String file_content = map_json.to_string();
  170. auto file = Core::File::construct(file_name);
  171. file->open(Core::IODevice::WriteOnly);
  172. if (!file->is_open()) {
  173. StringBuilder sb;
  174. sb.append("Failed to open ");
  175. sb.append(file_name);
  176. sb.append(" for write. Error: ");
  177. sb.append(file->error_string());
  178. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  179. return;
  180. }
  181. bool result = file->write(file_content);
  182. if (!result) {
  183. int error_number = errno;
  184. StringBuilder sb;
  185. sb.append("Unable to save file. Error: ");
  186. sb.append(strerror(error_number));
  187. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  188. return;
  189. }
  190. m_modified = false;
  191. m_file_name = file_name;
  192. update_window_title();
  193. }
  194. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  195. {
  196. for (int i = 0; i < KEY_COUNT; i++) {
  197. auto& tmp_button = m_keys.at(i);
  198. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  199. tmp_button->update();
  200. }
  201. }
  202. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  203. {
  204. for (int i = 0; i < KEY_COUNT; i++) {
  205. if (keys[i].scancode == event.scancode()) {
  206. auto& tmp_button = m_keys.at(i);
  207. tmp_button->set_pressed(false);
  208. tmp_button->update();
  209. break;
  210. }
  211. }
  212. }
  213. void KeyboardMapperWidget::set_current_map(const String current_map)
  214. {
  215. m_current_map_name = current_map;
  216. u32* map;
  217. if (m_current_map_name == "map") {
  218. map = m_character_map.map;
  219. } else if (m_current_map_name == "shift_map") {
  220. map = m_character_map.shift_map;
  221. } else if (m_current_map_name == "alt_map") {
  222. map = m_character_map.alt_map;
  223. } else if (m_current_map_name == "altgr_map") {
  224. map = m_character_map.altgr_map;
  225. } else if (m_current_map_name == "shift_altgr_map") {
  226. map = m_character_map.shift_altgr_map;
  227. } else {
  228. ASSERT_NOT_REACHED();
  229. }
  230. for (unsigned k = 0; k < KEY_COUNT; k++) {
  231. auto index = keys[k].map_index;
  232. if (index == 0)
  233. continue;
  234. AK::StringBuilder sb;
  235. sb.append_code_point(map[index]);
  236. m_keys.at(k)->set_text(sb.to_string());
  237. }
  238. this->update();
  239. }
  240. void KeyboardMapperWidget::update_window_title()
  241. {
  242. StringBuilder sb;
  243. sb.append(m_file_name);
  244. if (m_modified)
  245. sb.append(" (*)");
  246. sb.append(" - KeyboardMapper");
  247. window()->set_title(sb.to_string());
  248. }