main.cpp 6.4 KB

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