main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CharacterMapFileListModel.h"
  7. #include <AK/JsonObject.h>
  8. #include <AK/QuickSort.h>
  9. #include <LibCore/ConfigFile.h>
  10. #include <LibCore/DirIterator.h>
  11. #include <LibCore/File.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/Button.h>
  16. #include <LibGUI/CheckBox.h>
  17. #include <LibGUI/ComboBox.h>
  18. #include <LibGUI/Label.h>
  19. #include <LibGUI/Menu.h>
  20. #include <LibGUI/Menubar.h>
  21. #include <LibGUI/MessageBox.h>
  22. #include <LibGUI/WindowServerConnection.h>
  23. #include <LibKeyboard/CharacterMap.h>
  24. #include <spawn.h>
  25. int main(int argc, char** argv)
  26. {
  27. if (pledge("stdio rpath cpath wpath recvfd sendfd unix proc exec", nullptr) < 0) {
  28. perror("pledge");
  29. return 1;
  30. }
  31. auto app = GUI::Application::construct(argc, argv);
  32. if (pledge("stdio rpath cpath wpath recvfd sendfd proc exec", nullptr) < 0) {
  33. perror("pledge");
  34. return 1;
  35. }
  36. auto config = Core::ConfigFile::get_for_app("KeyboardSettings");
  37. if (unveil(config->filename().characters(), "rwc") < 0) {
  38. perror("unveil");
  39. return 1;
  40. }
  41. if (unveil("/res", "r") < 0) {
  42. perror("unveil");
  43. return 1;
  44. }
  45. if (unveil("/bin/keymap", "x") < 0) {
  46. perror("unveil");
  47. return 1;
  48. }
  49. if (unveil("/proc/keymap", "r") < 0) {
  50. perror("unveil");
  51. return 1;
  52. }
  53. if (unveil(nullptr, nullptr)) {
  54. perror("unveil");
  55. return 1;
  56. }
  57. auto app_icon = GUI::Icon::default_icon("app-keyboard-settings");
  58. auto proc_keymap = Core::File::construct("/proc/keymap");
  59. if (!proc_keymap->open(Core::OpenMode::ReadOnly))
  60. VERIFY_NOT_REACHED();
  61. auto json = JsonValue::from_string(proc_keymap->read_all());
  62. VERIFY(json.has_value());
  63. JsonObject keymap_object = json.value().as_object();
  64. VERIFY(keymap_object.has("keymap"));
  65. String current_keymap = keymap_object.get("keymap").to_string();
  66. dbgln("KeyboardSettings thinks the current keymap is: {}", current_keymap);
  67. Vector<String> character_map_files;
  68. Core::DirIterator iterator("/res/keymaps/", Core::DirIterator::Flags::SkipDots);
  69. if (iterator.has_error()) {
  70. GUI::MessageBox::show(nullptr, String::formatted("Error on reading mapping file list: {}", iterator.error_string()), "Keyboard settings", GUI::MessageBox::Type::Error);
  71. return -1;
  72. }
  73. while (iterator.has_next()) {
  74. auto name = iterator.next_path();
  75. name.replace(".json", "");
  76. character_map_files.append(name);
  77. }
  78. quick_sort(character_map_files);
  79. size_t initial_keymap_index = SIZE_MAX;
  80. for (size_t i = 0; i < character_map_files.size(); ++i) {
  81. if (character_map_files[i].equals_ignoring_case(current_keymap))
  82. initial_keymap_index = i;
  83. }
  84. VERIFY(initial_keymap_index < character_map_files.size());
  85. auto window = GUI::Window::construct();
  86. window->set_title("Keyboard Settings");
  87. window->resize(300, 78);
  88. window->set_resizable(false);
  89. window->set_minimizable(false);
  90. window->set_icon(app_icon.bitmap_for_size(16));
  91. auto& root_widget = window->set_main_widget<GUI::Widget>();
  92. root_widget.set_layout<GUI::VerticalBoxLayout>();
  93. root_widget.set_fill_with_background_color(true);
  94. root_widget.layout()->set_spacing(0);
  95. root_widget.layout()->set_margins(4);
  96. auto& character_map_file_selection_container = root_widget.add<GUI::Widget>();
  97. character_map_file_selection_container.set_layout<GUI::HorizontalBoxLayout>();
  98. character_map_file_selection_container.set_fixed_height(22);
  99. auto& character_map_file_label = character_map_file_selection_container.add<GUI::Label>();
  100. character_map_file_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  101. character_map_file_label.set_fixed_width(130);
  102. character_map_file_label.set_text("Character Mapping File:");
  103. auto& character_map_file_combo = character_map_file_selection_container.add<GUI::ComboBox>();
  104. character_map_file_combo.set_only_allow_values_from_model(true);
  105. character_map_file_combo.set_model(*CharacterMapFileListModel::create(character_map_files));
  106. character_map_file_combo.set_selected_index(initial_keymap_index);
  107. auto& num_lock_checkbox = root_widget.add<GUI::CheckBox>("Enable Num Lock on login");
  108. num_lock_checkbox.set_checked(config->read_bool_entry("StartupEnable", "NumLock", true));
  109. root_widget.layout()->add_spacer();
  110. auto apply_settings = [&](bool quit) {
  111. String character_map_file = character_map_file_combo.text();
  112. if (character_map_file.is_empty()) {
  113. GUI::MessageBox::show(window, "Please select character mapping file.", "Keyboard settings", GUI::MessageBox::Type::Error);
  114. return;
  115. }
  116. pid_t child_pid;
  117. const char* argv[] = { "/bin/keymap", character_map_file.characters(), nullptr };
  118. if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  119. perror("posix_spawn");
  120. exit(1);
  121. }
  122. config->write_bool_entry("StartupEnable", "NumLock", num_lock_checkbox.is_checked());
  123. config->sync();
  124. if (quit)
  125. app->quit();
  126. };
  127. auto& bottom_widget = root_widget.add<GUI::Widget>();
  128. bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
  129. bottom_widget.layout()->add_spacer();
  130. bottom_widget.set_fixed_height(30);
  131. bottom_widget.set_content_margins({ 4, 0 });
  132. auto& ok_button = bottom_widget.add<GUI::Button>();
  133. ok_button.set_text("OK");
  134. ok_button.set_fixed_width(60);
  135. ok_button.on_click = [&](auto) {
  136. apply_settings(true);
  137. };
  138. auto& cancel_button = bottom_widget.add<GUI::Button>();
  139. cancel_button.set_text("Cancel");
  140. cancel_button.set_fixed_width(60);
  141. cancel_button.on_click = [&](auto) {
  142. app->quit();
  143. };
  144. auto& apply_button = bottom_widget.add<GUI::Button>();
  145. apply_button.set_text("Apply");
  146. apply_button.set_fixed_width(60);
  147. apply_button.on_click = [&](auto) {
  148. apply_settings(false);
  149. };
  150. auto quit_action = GUI::CommonActions::make_quit_action(
  151. [&](auto&) {
  152. app->quit();
  153. });
  154. auto menubar = GUI::Menubar::construct();
  155. auto& file_menu = window->add_menu("&File");
  156. file_menu.add_action(quit_action);
  157. auto& help_menu = window->add_menu("&Help");
  158. help_menu.add_action(GUI::CommonActions::make_about_action("Keyboard Settings", app_icon, window));
  159. window->show();
  160. return app->exec();
  161. }