KeyboardMapperWidget.cpp 9.0 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. 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. }
  125. void KeyboardMapperWidget::load_from_file(String file_name)
  126. {
  127. auto result = Keyboard::CharacterMapFile::load_from_file(file_name);
  128. if (!result.has_value()) {
  129. ASSERT_NOT_REACHED();
  130. }
  131. m_file_name = file_name;
  132. m_character_map = result.value();
  133. set_current_map("map");
  134. for (Widget* widget : m_map_group->child_widgets()) {
  135. auto radio_button = (GUI::RadioButton*)widget;
  136. radio_button->set_checked(radio_button->name() == "map");
  137. }
  138. update_window_title();
  139. }
  140. void KeyboardMapperWidget::save()
  141. {
  142. save_to_file(m_file_name);
  143. }
  144. void KeyboardMapperWidget::save_to_file(const StringView& file_name)
  145. {
  146. JsonObject map_json;
  147. auto add_array = [&](String name, u32* values) {
  148. JsonArray items;
  149. for (int i = 0; i < 90; i++) {
  150. AK::StringBuilder sb;
  151. if (values[i])
  152. sb.append_code_point(values[i]);
  153. JsonValue val(sb.to_string());
  154. items.append(move(val));
  155. }
  156. map_json.set(name, move(items));
  157. };
  158. add_array("map", m_character_map.map);
  159. add_array("shift_map", m_character_map.shift_map);
  160. add_array("alt_map", m_character_map.alt_map);
  161. add_array("altgr_map", m_character_map.altgr_map);
  162. add_array("shift_altgr_map", m_character_map.shift_altgr_map);
  163. // Write to file.
  164. String file_content = map_json.to_string();
  165. auto file = Core::File::construct(file_name);
  166. file->open(Core::IODevice::WriteOnly);
  167. if (!file->is_open()) {
  168. StringBuilder sb;
  169. sb.append("Failed to open ");
  170. sb.append(file_name);
  171. sb.append(" for write. Error: ");
  172. sb.append(file->error_string());
  173. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  174. return;
  175. }
  176. bool result = file->write(file_content);
  177. if (!result) {
  178. int error_number = errno;
  179. StringBuilder sb;
  180. sb.append("Unable to save file. Error: ");
  181. sb.append(strerror(error_number));
  182. GUI::MessageBox::show(window(), sb.to_string(), "Error", GUI::MessageBox::Type::Error);
  183. return;
  184. }
  185. m_modified = false;
  186. m_file_name = file_name;
  187. update_window_title();
  188. }
  189. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  190. {
  191. for (int i = 0; i < KEY_COUNT; i++) {
  192. auto& tmp_button = m_keys.at(i);
  193. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  194. tmp_button->update();
  195. }
  196. }
  197. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  198. {
  199. for (int i = 0; i < KEY_COUNT; i++) {
  200. if (keys[i].scancode == event.scancode()) {
  201. auto& tmp_button = m_keys.at(i);
  202. tmp_button->set_pressed(false);
  203. tmp_button->update();
  204. break;
  205. }
  206. }
  207. }
  208. void KeyboardMapperWidget::set_current_map(const String current_map)
  209. {
  210. m_current_map_name = current_map;
  211. u32* map;
  212. if (m_current_map_name == "map") {
  213. map = m_character_map.map;
  214. } else if (m_current_map_name == "shift_map") {
  215. map = m_character_map.shift_map;
  216. } else if (m_current_map_name == "alt_map") {
  217. map = m_character_map.alt_map;
  218. } else if (m_current_map_name == "altgr_map") {
  219. map = m_character_map.altgr_map;
  220. } else if (m_current_map_name == "shift_altgr_map") {
  221. map = m_character_map.shift_altgr_map;
  222. } else {
  223. ASSERT_NOT_REACHED();
  224. }
  225. for (unsigned k = 0; k < KEY_COUNT; k++) {
  226. auto index = keys[k].map_index;
  227. if (index == 0)
  228. continue;
  229. AK::StringBuilder sb;
  230. sb.append_code_point(map[index]);
  231. m_keys.at(k)->set_text(sb.to_string());
  232. }
  233. this->update();
  234. }
  235. void KeyboardMapperWidget::update_window_title()
  236. {
  237. StringBuilder sb;
  238. sb.append(m_file_name);
  239. if (m_modified)
  240. sb.append(" (*)");
  241. sb.append(" - KeyboardMapper");
  242. window()->set_title(sb.to_string());
  243. }