KeyboardMapperWidget.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <ctype.h>
  35. #include <fcntl.h>
  36. #include <stdio.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::Rect 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 = [&]() {
  60. auto input_box = GUI::InputBox::construct("New Character:", "Select Character", window());
  61. if (input_box->exec() == GUI::InputBox::ExecOK) {
  62. auto value = input_box->text_value();
  63. int i = m_keys.find_first_index(&tmp_button).value_or(0);
  64. ASSERT(i > 0);
  65. auto index = keys[i].map_index;
  66. ASSERT(index > 0);
  67. tmp_button.set_text(value);
  68. char* map;
  69. if (m_current_map_name == "map") {
  70. map = m_character_map.map;
  71. } else if (m_current_map_name == "shift_map") {
  72. map = m_character_map.shift_map;
  73. } else if (m_current_map_name == "alt_map") {
  74. map = m_character_map.alt_map;
  75. } else if (m_current_map_name == "altgr_map") {
  76. map = m_character_map.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_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  94. bottom_widget.set_preferred_size(0, 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_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  99. m_map_group->set_preferred_size(250, 0);
  100. auto& radio_map = m_map_group->add<GUI::RadioButton>("Default");
  101. radio_map.set_name("map");
  102. radio_map.on_checked = [&](bool) {
  103. set_current_map("map");
  104. };
  105. auto& radio_shift = m_map_group->add<GUI::RadioButton>("Shift");
  106. radio_shift.set_name("shift_map");
  107. radio_shift.on_checked = [&](bool) {
  108. set_current_map("shift_map");
  109. };
  110. auto& radio_altgr = m_map_group->add<GUI::RadioButton>("AltGr");
  111. radio_altgr.set_name("altgr_map");
  112. radio_altgr.on_checked = [&](bool) {
  113. set_current_map("altgr_map");
  114. };
  115. auto& radio_alt = m_map_group->add<GUI::RadioButton>("Alt");
  116. radio_alt.set_name("alt_map");
  117. radio_alt.on_checked = [&](bool) {
  118. set_current_map("alt_map");
  119. };
  120. bottom_widget.layout()->add_spacer();
  121. auto& ok_button = bottom_widget.add<GUI::Button>();
  122. ok_button.set_text("Save");
  123. ok_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  124. ok_button.set_preferred_size(80, 0);
  125. ok_button.on_click = [this](auto) {
  126. save();
  127. };
  128. }
  129. void KeyboardMapperWidget::load_from_file(String file_name)
  130. {
  131. auto result = Keyboard::CharacterMapFile::load_from_file(file_name);
  132. if (!result.has_value()) {
  133. ASSERT_NOT_REACHED();
  134. }
  135. m_file_name = file_name;
  136. m_character_map = result.value();
  137. set_current_map("map");
  138. for (Widget* widget : m_map_group->child_widgets()) {
  139. auto radio_button = (GUI::RadioButton*)widget;
  140. radio_button->set_checked(radio_button->name() == "map");
  141. }
  142. update_window_title();
  143. }
  144. void KeyboardMapperWidget::save()
  145. {
  146. save_to_file(m_file_name);
  147. }
  148. void KeyboardMapperWidget::save_to_file(const StringView& file_name)
  149. {
  150. JsonObject map_json;
  151. auto add_array = [&](String name, char* values) {
  152. JsonArray items;
  153. for (int i = 0; i < 90; i++) {
  154. AK::StringBuilder sb;
  155. sb.append(values[i]);
  156. JsonValue val(sb.to_string());
  157. items.append(move(val));
  158. }
  159. map_json.set(name, move(items));
  160. };
  161. add_array("map", m_character_map.map);
  162. add_array("shift_map", m_character_map.shift_map);
  163. add_array("alt_map", m_character_map.alt_map);
  164. add_array("altgr_map", m_character_map.altgr_map);
  165. // Write to file.
  166. String file_content = map_json.to_string();
  167. auto file = Core::File::construct(file_name);
  168. file->open(Core::IODevice::WriteOnly);
  169. if (!file->is_open()) {
  170. StringBuilder sb;
  171. sb.append("Failed to open ");
  172. sb.append(file_name);
  173. sb.append(" for write. Error: ");
  174. sb.append(file->error_string());
  175. GUI::MessageBox::show(sb.to_string(), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  176. return;
  177. }
  178. bool result = file->write(file_content);
  179. if (!result) {
  180. int error_number = errno;
  181. StringBuilder sb;
  182. sb.append("Unable to save file. Error: ");
  183. sb.append(strerror(error_number));
  184. GUI::MessageBox::show(sb.to_string(), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  185. return;
  186. }
  187. m_modified = false;
  188. m_file_name = file_name;
  189. update_window_title();
  190. }
  191. void KeyboardMapperWidget::keydown_event(GUI::KeyEvent& event)
  192. {
  193. for (int i = 0; i < KEY_COUNT; i++) {
  194. auto& tmp_button = m_keys.at(i);
  195. tmp_button->set_pressed(keys[i].scancode == event.scancode());
  196. tmp_button->update();
  197. }
  198. }
  199. void KeyboardMapperWidget::keyup_event(GUI::KeyEvent& event)
  200. {
  201. for (int i = 0; i < KEY_COUNT; i++) {
  202. if (keys[i].scancode == event.scancode()) {
  203. auto& tmp_button = m_keys.at(i);
  204. tmp_button->set_pressed(false);
  205. tmp_button->update();
  206. break;
  207. }
  208. }
  209. }
  210. void KeyboardMapperWidget::set_current_map(const String current_map)
  211. {
  212. m_current_map_name = current_map;
  213. char* map;
  214. if (m_current_map_name == "map") {
  215. map = m_character_map.map;
  216. } else if (m_current_map_name == "shift_map") {
  217. map = m_character_map.shift_map;
  218. } else if (m_current_map_name == "alt_map") {
  219. map = m_character_map.alt_map;
  220. } else if (m_current_map_name == "altgr_map") {
  221. map = m_character_map.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(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. }