diff options
author | Valtteri Koskivuori <vkoskiv@gmail.com> | 2020-08-07 00:35:17 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-07 09:11:04 +0200 |
commit | b0ab6b9e3f5a32da41aefa9cd2dc173be0cfa5d1 (patch) | |
tree | 4aab129adef98d9158fed71346741bb3c1809e61 /Applications | |
parent | bcb4cb4543acf13f0ccb518da1b340b5d7424c6d (diff) | |
download | serenity-b0ab6b9e3f5a32da41aefa9cd2dc173be0cfa5d1.zip |
KeyboardSettings: Preselect the current keymap in the dropdown menu.
This is the little omission that I originally set out to fix, and
sure enough, I got to delve into Kernel land and back to implement
it. Lots of fun!
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/KeyboardSettings/main.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Applications/KeyboardSettings/main.cpp b/Applications/KeyboardSettings/main.cpp index 6f53644c44..dd0eb5ca02 100644 --- a/Applications/KeyboardSettings/main.cpp +++ b/Applications/KeyboardSettings/main.cpp @@ -26,8 +26,10 @@ #include "CharacterMapFileListModel.h" #include <AK/QuickSort.h> +#include <AK/JsonObject.h> #include <LibCore/ArgsParser.h> #include <LibCore/DirIterator.h> +#include <LibCore/File.h> #include <LibGUI/AboutDialog.h> #include <LibGUI/Action.h> #include <LibGUI/Application.h> @@ -67,6 +69,11 @@ int main(int argc, char** argv) return 1; } + if (unveil("/proc/keymap", "r") < 0) { + perror("unveil"); + return 1; + } + if (unveil(nullptr, nullptr)) { perror("unveil"); return 1; @@ -74,6 +81,17 @@ int main(int argc, char** argv) auto app_icon = GUI::Icon::default_icon("app-keyboard-settings"); + auto proc_keymap = Core::File::construct("/proc/keymap"); + if (!proc_keymap->open(Core::IODevice::OpenMode::ReadOnly)) + ASSERT_NOT_REACHED(); + + auto json = JsonValue::from_string(proc_keymap->read_all()); + ASSERT(json.has_value()); + JsonObject keymap_object = json.value().as_object(); + ASSERT(keymap_object.has("keymap")); + String current_keymap = keymap_object.get("keymap").to_string(); + dbg() << "KeyboardSettings thinks the current keymap is: " << current_keymap; + Vector<String> character_map_files; Core::DirIterator iterator("/res/keymaps/", Core::DirIterator::Flags::SkipDots); if (iterator.has_error()) { @@ -88,6 +106,13 @@ int main(int argc, char** argv) } quick_sort(character_map_files); + 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; + } + ASSERT(initial_keymap_index < character_map_files.size()); + auto window = GUI::Window::construct(); window->set_title("Keyboard settings"); window->resize(300, 70); @@ -115,6 +140,7 @@ int main(int argc, char** argv) character_map_file_combo.set_preferred_size(0, 22); character_map_file_combo.set_only_allow_values_from_model(true); character_map_file_combo.set_model(*CharacterMapFileListModel::create(character_map_files)); + character_map_file_combo.set_selected_index(initial_keymap_index); root_widget.layout()->add_spacer(); |