KeyboardSettingsWidget.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "KeyboardSettingsWidget.h"
  9. #include <AK/JsonObject.h>
  10. #include <AK/QuickSort.h>
  11. #include <Applications/KeyboardSettings/KeyboardWidgetGML.h>
  12. #include <Applications/KeyboardSettings/KeymapDialogGML.h>
  13. #include <LibConfig/Client.h>
  14. #include <LibCore/DirIterator.h>
  15. #include <LibCore/File.h>
  16. #include <LibGUI/Application.h>
  17. #include <LibGUI/ComboBox.h>
  18. #include <LibGUI/Dialog.h>
  19. #include <LibGUI/ItemListModel.h>
  20. #include <LibGUI/Label.h>
  21. #include <LibGUI/MessageBox.h>
  22. #include <LibGUI/Model.h>
  23. #include <LibGUI/Process.h>
  24. #include <LibGUI/Widget.h>
  25. #include <LibGUI/Window.h>
  26. #include <LibGfx/Font/FontDatabase.h>
  27. #include <LibKeyboard/CharacterMap.h>
  28. #include <spawn.h>
  29. class KeymapSelectionDialog final : public GUI::Dialog {
  30. C_OBJECT(KeymapSelectionDialog)
  31. public:
  32. virtual ~KeymapSelectionDialog() override = default;
  33. static String select_keymap(Window* parent_window, Vector<String> const& selected_keymaps)
  34. {
  35. auto dialog = KeymapSelectionDialog::construct(parent_window, selected_keymaps);
  36. dialog->set_title("Add a keymap");
  37. if (dialog->exec() == ExecResult::OK) {
  38. return dialog->selected_keymap();
  39. }
  40. return String::empty();
  41. }
  42. String selected_keymap() { return m_selected_keymap; }
  43. private:
  44. KeymapSelectionDialog(Window* parent_window, Vector<String> const& selected_keymaps)
  45. : Dialog(parent_window)
  46. {
  47. auto& widget = set_main_widget<GUI::Widget>();
  48. if (!widget.load_from_gml(keymap_dialog_gml))
  49. VERIFY_NOT_REACHED();
  50. set_resizable(false);
  51. resize(190, 54);
  52. set_icon(parent_window->icon());
  53. Core::DirIterator iterator("/res/keymaps/", Core::DirIterator::Flags::SkipDots);
  54. if (iterator.has_error()) {
  55. GUI::MessageBox::show(nullptr, String::formatted("Error on reading mapping file list: {}", iterator.error_string()), "Keyboard settings", GUI::MessageBox::Type::Error);
  56. GUI::Application::the()->quit(-1);
  57. }
  58. while (iterator.has_next()) {
  59. auto name = iterator.next_path();
  60. auto basename = name.replace(".json", "", ReplaceMode::FirstOnly);
  61. if (!selected_keymaps.find(basename).is_end())
  62. continue;
  63. m_character_map_files.append(basename);
  64. }
  65. quick_sort(m_character_map_files);
  66. m_selected_keymap = m_character_map_files.first();
  67. m_keymaps_combobox = *widget.find_descendant_of_type_named<GUI::ComboBox>("keymaps_combobox");
  68. m_keymaps_combobox->set_only_allow_values_from_model(true);
  69. m_keymaps_combobox->set_model(*GUI::ItemListModel<String>::create(m_character_map_files));
  70. m_keymaps_combobox->set_selected_index(0);
  71. m_keymaps_combobox->on_change = [&](auto& keymap, auto) {
  72. m_selected_keymap = keymap;
  73. };
  74. auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  75. ok_button.on_click = [this](auto) {
  76. done(ExecResult::OK);
  77. };
  78. auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  79. cancel_button.on_click = [this](auto) {
  80. done(ExecResult::Cancel);
  81. };
  82. }
  83. RefPtr<GUI::ComboBox> m_keymaps_combobox;
  84. Vector<String> m_character_map_files;
  85. String m_selected_keymap;
  86. };
  87. class KeymapModel final : public GUI::Model {
  88. public:
  89. KeymapModel() {};
  90. int row_count(GUI::ModelIndex const&) const override { return m_data.size(); }
  91. int column_count(GUI::ModelIndex const&) const override { return 1; }
  92. GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  93. {
  94. String const& data = m_data.at(index.row());
  95. if (role == GUI::ModelRole::Font && data == m_active_keymap)
  96. return Gfx::FontDatabase::default_font().bold_variant();
  97. return data;
  98. }
  99. void remove_at(size_t index)
  100. {
  101. m_data.remove(index);
  102. invalidate();
  103. }
  104. void add_keymap(String const& keymap)
  105. {
  106. m_data.append(keymap);
  107. invalidate();
  108. }
  109. void set_active_keymap(String const& keymap)
  110. {
  111. m_active_keymap = keymap;
  112. invalidate();
  113. }
  114. String const& active_keymap() { return m_active_keymap; }
  115. String const& keymap_at(size_t index)
  116. {
  117. return m_data[index];
  118. }
  119. Vector<String> const& keymaps() const { return m_data; }
  120. private:
  121. Vector<String> m_data;
  122. String m_active_keymap;
  123. };
  124. KeyboardSettingsWidget::KeyboardSettingsWidget()
  125. {
  126. load_from_gml(keyboard_widget_gml);
  127. auto proc_keymap = Core::File::construct("/proc/keymap");
  128. if (!proc_keymap->open(Core::OpenMode::ReadOnly))
  129. VERIFY_NOT_REACHED();
  130. auto json = JsonValue::from_string(proc_keymap->read_all()).release_value_but_fixme_should_propagate_errors();
  131. auto const& keymap_object = json.as_object();
  132. VERIFY(keymap_object.has("keymap"));
  133. m_initial_active_keymap = keymap_object.get("keymap").to_string();
  134. dbgln("KeyboardSettings thinks the current keymap is: {}", m_initial_active_keymap);
  135. auto mapper_config(Core::ConfigFile::open("/etc/Keyboard.ini").release_value_but_fixme_should_propagate_errors());
  136. auto keymaps = mapper_config->read_entry("Mapping", "Keymaps", "");
  137. auto keymaps_vector = keymaps.split(',');
  138. m_selected_keymaps_listview = find_descendant_of_type_named<GUI::ListView>("selected_keymaps");
  139. m_selected_keymaps_listview->horizontal_scrollbar().set_visible(false);
  140. m_selected_keymaps_listview->set_model(adopt_ref(*new KeymapModel()));
  141. auto& keymaps_list_model = static_cast<KeymapModel&>(*m_selected_keymaps_listview->model());
  142. for (auto& keymap : keymaps_vector) {
  143. m_initial_keymap_list.append(keymap);
  144. keymaps_list_model.add_keymap(keymap);
  145. }
  146. keymaps_list_model.set_active_keymap(m_initial_active_keymap);
  147. m_activate_keymap_button = find_descendant_of_type_named<GUI::Button>("activate_keymap_button");
  148. m_activate_keymap_event = [&]() {
  149. auto& selection = m_selected_keymaps_listview->selection();
  150. if (!selection.is_empty()) {
  151. auto& selected_keymap = keymaps_list_model.keymap_at(selection.first().row());
  152. keymaps_list_model.set_active_keymap(selected_keymap);
  153. set_modified(true);
  154. }
  155. };
  156. m_activate_keymap_button->on_click = [&](auto) {
  157. m_activate_keymap_event();
  158. };
  159. m_selected_keymaps_listview->on_activation = [&](auto) {
  160. m_activate_keymap_event();
  161. };
  162. m_add_keymap_button = find_descendant_of_type_named<GUI::Button>("add_keymap_button");
  163. m_add_keymap_button->on_click = [&](auto) {
  164. auto keymap = KeymapSelectionDialog::select_keymap(window(), keymaps_list_model.keymaps());
  165. if (!keymap.is_empty()) {
  166. keymaps_list_model.add_keymap(keymap);
  167. set_modified(true);
  168. }
  169. };
  170. m_remove_keymap_button = find_descendant_of_type_named<GUI::Button>("remove_keymap_button");
  171. m_remove_keymap_button->on_click = [&](auto) {
  172. auto& selection = m_selected_keymaps_listview->selection();
  173. bool active_keymap_deleted = false;
  174. for (auto& index : selection.indices()) {
  175. if (keymaps_list_model.keymap_at(index.row()) == keymaps_list_model.active_keymap())
  176. active_keymap_deleted = true;
  177. keymaps_list_model.remove_at(index.row());
  178. }
  179. if (active_keymap_deleted)
  180. keymaps_list_model.set_active_keymap(keymaps_list_model.keymap_at(0));
  181. set_modified(true);
  182. };
  183. m_selected_keymaps_listview->on_selection_change = [&]() {
  184. auto& selection = m_selected_keymaps_listview->selection();
  185. m_remove_keymap_button->set_enabled(!selection.is_empty() && keymaps_list_model.keymaps().size() > 1);
  186. m_activate_keymap_button->set_enabled(!selection.is_empty());
  187. };
  188. m_test_typing_area = *find_descendant_of_type_named<GUI::TextEditor>("test_typing_area");
  189. m_test_typing_area->on_focusin = [&]() {
  190. set_keymaps(keymaps_list_model.keymaps(), keymaps_list_model.active_keymap());
  191. };
  192. m_test_typing_area->on_focusout = [&]() {
  193. set_keymaps(m_initial_keymap_list, m_initial_active_keymap);
  194. };
  195. m_clear_test_typing_area_button = find_descendant_of_type_named<GUI::Button>("button_clear_test_typing_area");
  196. m_clear_test_typing_area_button->on_click = [this](auto) {
  197. m_test_typing_area->clear();
  198. m_test_typing_area->set_focus(true);
  199. };
  200. m_num_lock_checkbox = find_descendant_of_type_named<GUI::CheckBox>("num_lock_checkbox");
  201. m_num_lock_checkbox->set_checked(Config::read_bool("KeyboardSettings", "StartupEnable", "NumLock", true));
  202. m_num_lock_checkbox->on_checked = [&](auto) {
  203. set_modified(true);
  204. };
  205. }
  206. KeyboardSettingsWidget::~KeyboardSettingsWidget()
  207. {
  208. set_keymaps(m_initial_keymap_list, m_initial_active_keymap);
  209. }
  210. void KeyboardSettingsWidget::window_activated(bool is_active_window)
  211. {
  212. if (is_active_window && m_test_typing_area->is_focused()) {
  213. auto& keymaps_list_model = static_cast<KeymapModel&>(*m_selected_keymaps_listview->model());
  214. set_keymaps(keymaps_list_model.keymaps(), keymaps_list_model.active_keymap());
  215. } else {
  216. set_keymaps(m_initial_keymap_list, m_initial_active_keymap);
  217. }
  218. }
  219. void KeyboardSettingsWidget::apply_settings()
  220. {
  221. auto& m_keymaps_list_model = static_cast<KeymapModel&>(*m_selected_keymaps_listview->model());
  222. set_keymaps(m_keymaps_list_model.keymaps(), m_keymaps_list_model.active_keymap());
  223. m_initial_keymap_list.clear();
  224. for (auto& keymap : m_keymaps_list_model.keymaps()) {
  225. m_initial_keymap_list.append(keymap);
  226. }
  227. m_initial_active_keymap = m_keymaps_list_model.active_keymap();
  228. Config::write_bool("KeyboardSettings", "StartupEnable", "NumLock", m_num_lock_checkbox->is_checked());
  229. }
  230. void KeyboardSettingsWidget::set_keymaps(Vector<String> const& keymaps, String const& active_keymap)
  231. {
  232. auto keymaps_string = String::join(',', keymaps);
  233. GUI::Process::spawn_or_show_error(window(), "/bin/keymap", Array { "-s", keymaps_string.characters(), "-m", active_keymap.characters() });
  234. }