2020-06-07 13:38:45 -03:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
|
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-07 13:38:45 -03:00
|
|
|
|
*/
|
|
|
|
|
|
|
2020-08-06 18:35:17 -03:00
|
|
|
|
#include <AK/JsonObject.h>
|
2020-09-18 04:49:51 -03:00
|
|
|
|
#include <AK/QuickSort.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
#include <LibCore/DirIterator.h>
|
2020-08-06 18:35:17 -03:00
|
|
|
|
#include <LibCore/File.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
#include <LibGUI/Action.h>
|
|
|
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
|
#include <LibGUI/Button.h>
|
2021-07-03 20:41:28 -03:00
|
|
|
|
#include <LibGUI/CheckBox.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
#include <LibGUI/ComboBox.h>
|
2021-08-21 17:07:44 -03:00
|
|
|
|
#include <LibGUI/ItemListModel.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
#include <LibGUI/Label.h>
|
|
|
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
|
#include <LibGUI/Menubar.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
#include <LibGUI/MessageBox.h>
|
|
|
|
|
|
#include <LibGUI/WindowServerConnection.h>
|
|
|
|
|
|
#include <LibKeyboard/CharacterMap.h>
|
2020-06-28 14:40:10 -03:00
|
|
|
|
#include <spawn.h>
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
2021-08-29 11:07:33 -03:00
|
|
|
|
// Including this after to avoid LibIPC errors
|
|
|
|
|
|
#include <LibConfig/Client.h>
|
|
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
|
{
|
2021-05-13 18:20:26 -03:00
|
|
|
|
if (pledge("stdio rpath cpath wpath recvfd sendfd unix proc exec", nullptr) < 0) {
|
2020-06-18 17:29:09 -03:00
|
|
|
|
perror("pledge");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2021-08-29 11:07:33 -03:00
|
|
|
|
Config::pledge_domains("KeyboardSettings");
|
2020-06-18 17:29:09 -03:00
|
|
|
|
|
2021-07-03 20:41:28 -03:00
|
|
|
|
if (pledge("stdio rpath cpath wpath recvfd sendfd proc exec", nullptr) < 0) {
|
2020-06-18 17:29:09 -03:00
|
|
|
|
perror("pledge");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-18 04:49:51 -03:00
|
|
|
|
if (unveil("/res", "r") < 0) {
|
2020-06-18 17:29:09 -03:00
|
|
|
|
perror("unveil");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-18 04:49:51 -03:00
|
|
|
|
if (unveil("/bin/keymap", "x") < 0) {
|
2020-06-18 17:29:09 -03:00
|
|
|
|
perror("unveil");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-06 18:35:17 -03:00
|
|
|
|
if (unveil("/proc/keymap", "r") < 0) {
|
|
|
|
|
|
perror("unveil");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-18 17:29:09 -03:00
|
|
|
|
if (unveil(nullptr, nullptr)) {
|
|
|
|
|
|
perror("unveil");
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
auto app_icon = GUI::Icon::default_icon("app-keyboard-settings");
|
|
|
|
|
|
|
2020-08-06 18:35:17 -03:00
|
|
|
|
auto proc_keymap = Core::File::construct("/proc/keymap");
|
2021-05-12 06:26:43 -03:00
|
|
|
|
if (!proc_keymap->open(Core::OpenMode::ReadOnly))
|
2021-02-23 16:42:32 -03:00
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-08-06 18:35:17 -03:00
|
|
|
|
|
|
|
|
|
|
auto json = JsonValue::from_string(proc_keymap->read_all());
|
2021-02-23 16:42:32 -03:00
|
|
|
|
VERIFY(json.has_value());
|
2020-08-06 18:35:17 -03:00
|
|
|
|
JsonObject keymap_object = json.value().as_object();
|
2021-02-23 16:42:32 -03:00
|
|
|
|
VERIFY(keymap_object.has("keymap"));
|
2020-08-06 18:35:17 -03:00
|
|
|
|
String current_keymap = keymap_object.get("keymap").to_string();
|
2020-10-06 09:22:35 -03:00
|
|
|
|
dbgln("KeyboardSettings thinks the current keymap is: {}", current_keymap);
|
2020-08-06 18:35:17 -03:00
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
Vector<String> character_map_files;
|
|
|
|
|
|
Core::DirIterator iterator("/res/keymaps/", Core::DirIterator::Flags::SkipDots);
|
|
|
|
|
|
if (iterator.has_error()) {
|
2020-10-06 09:22:35 -03:00
|
|
|
|
GUI::MessageBox::show(nullptr, String::formatted("Error on reading mapping file list: {}", iterator.error_string()), "Keyboard settings", GUI::MessageBox::Type::Error);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (iterator.has_next()) {
|
|
|
|
|
|
auto name = iterator.next_path();
|
2021-09-10 20:15:44 -03:00
|
|
|
|
character_map_files.append(name.replace(".json", ""));
|
2020-06-07 13:38:45 -03:00
|
|
|
|
}
|
|
|
|
|
|
quick_sort(character_map_files);
|
|
|
|
|
|
|
2020-08-06 18:35:17 -03:00
|
|
|
|
size_t initial_keymap_index = SIZE_MAX;
|
|
|
|
|
|
for (size_t i = 0; i < character_map_files.size(); ++i) {
|
|
|
|
|
|
if (character_map_files[i].equals_ignoring_case(current_keymap))
|
|
|
|
|
|
initial_keymap_index = i;
|
|
|
|
|
|
}
|
2021-02-23 16:42:32 -03:00
|
|
|
|
VERIFY(initial_keymap_index < character_map_files.size());
|
2020-08-06 18:35:17 -03:00
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
auto window = GUI::Window::construct();
|
2021-01-04 19:51:49 -03:00
|
|
|
|
window->set_title("Keyboard Settings");
|
2021-07-27 13:25:05 -03:00
|
|
|
|
window->resize(300, 78);
|
2021-07-26 07:43:23 -03:00
|
|
|
|
window->set_resizable(false);
|
2021-07-26 07:44:05 -03:00
|
|
|
|
window->set_minimizable(false);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
|
|
|
|
|
|
|
|
auto& root_widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
|
|
root_widget.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
|
|
root_widget.set_fill_with_background_color(true);
|
|
|
|
|
|
root_widget.layout()->set_spacing(0);
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-16 21:11:38 -03:00
|
|
|
|
root_widget.layout()->set_margins(4);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
|
|
|
|
|
auto& character_map_file_selection_container = root_widget.add<GUI::Widget>();
|
|
|
|
|
|
character_map_file_selection_container.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-29 21:23:32 -03:00
|
|
|
|
character_map_file_selection_container.set_fixed_height(22);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
|
|
|
|
|
auto& character_map_file_label = character_map_file_selection_container.add<GUI::Label>();
|
|
|
|
|
|
character_map_file_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2020-12-29 21:23:32 -03:00
|
|
|
|
character_map_file_label.set_fixed_width(130);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
character_map_file_label.set_text("Character Mapping File:");
|
|
|
|
|
|
|
|
|
|
|
|
auto& character_map_file_combo = character_map_file_selection_container.add<GUI::ComboBox>();
|
|
|
|
|
|
character_map_file_combo.set_only_allow_values_from_model(true);
|
2021-08-21 17:07:44 -03:00
|
|
|
|
character_map_file_combo.set_model(*GUI::ItemListModel<String>::create(character_map_files));
|
2020-08-06 18:35:17 -03:00
|
|
|
|
character_map_file_combo.set_selected_index(initial_keymap_index);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
2021-07-03 20:41:28 -03:00
|
|
|
|
auto& num_lock_checkbox = root_widget.add<GUI::CheckBox>("Enable Num Lock on login");
|
2021-08-29 11:07:33 -03:00
|
|
|
|
num_lock_checkbox.set_checked(Config::read_bool("KeyboardSettings", "StartupEnable", "NumLock", true));
|
2021-07-03 20:41:28 -03:00
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
root_widget.layout()->add_spacer();
|
|
|
|
|
|
|
|
|
|
|
|
auto apply_settings = [&](bool quit) {
|
|
|
|
|
|
String character_map_file = character_map_file_combo.text();
|
|
|
|
|
|
if (character_map_file.is_empty()) {
|
2020-07-15 23:45:11 -03:00
|
|
|
|
GUI::MessageBox::show(window, "Please select character mapping file.", "Keyboard settings", GUI::MessageBox::Type::Error);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-06-28 14:40:10 -03:00
|
|
|
|
pid_t child_pid;
|
|
|
|
|
|
const char* argv[] = { "/bin/keymap", character_map_file.characters(), nullptr };
|
|
|
|
|
|
if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
|
|
|
|
|
perror("posix_spawn");
|
2020-06-18 17:29:09 -03:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
2021-07-03 20:41:28 -03:00
|
|
|
|
|
2021-08-29 11:07:33 -03:00
|
|
|
|
Config::write_bool("KeyboardSettings", "StartupEnable", "NumLock", num_lock_checkbox.is_checked());
|
2021-07-03 20:41:28 -03:00
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
if (quit)
|
2020-07-04 09:05:19 -03:00
|
|
|
|
app->quit();
|
2020-06-07 13:38:45 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
auto& bottom_widget = root_widget.add<GUI::Widget>();
|
|
|
|
|
|
bottom_widget.set_layout<GUI::HorizontalBoxLayout>();
|
|
|
|
|
|
bottom_widget.layout()->add_spacer();
|
2021-07-27 13:25:05 -03:00
|
|
|
|
bottom_widget.set_fixed_height(30);
|
Userland+LibGUI: Add shorthand versions of the Margins constructor
This allows for typing [8] instead of [8, 8, 8, 8] to specify the same
margin on all edges, for example. The constructors follow CSS' style of
specifying margins. The added constructors are:
- Margins(int all): Sets the same margin on all edges.
- Margins(int vertical, int horizontal): Sets the first argument to top
and bottom margins, and the second argument to left and right margins.
- Margins(int top, int vertical, int bottom): Sets the first argument to
the top margin, the second argument to the left and right margins,
and the third argument to the bottom margin.
2021-08-16 21:11:38 -03:00
|
|
|
|
bottom_widget.set_content_margins({ 4, 0 });
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
|
|
|
|
|
auto& ok_button = bottom_widget.add<GUI::Button>();
|
|
|
|
|
|
ok_button.set_text("OK");
|
2020-12-29 21:23:32 -03:00
|
|
|
|
ok_button.set_fixed_width(60);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
ok_button.on_click = [&](auto) {
|
|
|
|
|
|
apply_settings(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
auto& cancel_button = bottom_widget.add<GUI::Button>();
|
|
|
|
|
|
cancel_button.set_text("Cancel");
|
2020-12-29 21:23:32 -03:00
|
|
|
|
cancel_button.set_fixed_width(60);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
cancel_button.on_click = [&](auto) {
|
2020-07-04 09:05:19 -03:00
|
|
|
|
app->quit();
|
2020-06-07 13:38:45 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-07-20 19:58:35 -03:00
|
|
|
|
auto& apply_button = bottom_widget.add<GUI::Button>();
|
|
|
|
|
|
apply_button.set_text("Apply");
|
|
|
|
|
|
apply_button.set_fixed_width(60);
|
|
|
|
|
|
apply_button.on_click = [&](auto) {
|
|
|
|
|
|
apply_settings(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-06-07 13:38:45 -03:00
|
|
|
|
auto quit_action = GUI::CommonActions::make_quit_action(
|
|
|
|
|
|
[&](auto&) {
|
2020-07-04 09:05:19 -03:00
|
|
|
|
app->quit();
|
2020-06-07 13:38:45 -03:00
|
|
|
|
});
|
|
|
|
|
|
|
2021-04-13 11:18:20 -03:00
|
|
|
|
auto menubar = GUI::Menubar::construct();
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
|
auto& file_menu = window->add_menu("&File");
|
2021-05-01 05:45:39 -03:00
|
|
|
|
file_menu.add_action(quit_action);
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
|
auto& help_menu = window->add_menu("&Help");
|
2021-01-04 19:51:49 -03:00
|
|
|
|
help_menu.add_action(GUI::CommonActions::make_about_action("Keyboard Settings", app_icon, window));
|
2020-06-07 13:38:45 -03:00
|
|
|
|
|
|
|
|
|
|
window->show();
|
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
|
return app->exec();
|
2020-06-07 13:38:45 -03:00
|
|
|
|
}
|