diff --git a/Base/etc/Keyboard.ini b/Base/etc/Keyboard.ini new file mode 100644 index 0000000000..e776e7495b --- /dev/null +++ b/Base/etc/Keyboard.ini @@ -0,0 +1,2 @@ +[Mapping] +Keymap=en-us diff --git a/Base/etc/SystemServer.ini b/Base/etc/SystemServer.ini index c1e8d11794..5095292b25 100644 --- a/Base/etc/SystemServer.ini +++ b/Base/etc/SystemServer.ini @@ -161,6 +161,10 @@ AcceptSocketConnections=1 KeepAlive=1 User=anon +[KeyboardPreferenceLoader] +KeepAlive=0 +User=anon + [TestRunner@ttyS0] Executable=/home/anon/tests/run-tests-and-shutdown.sh StdIO=/dev/ttyS0 diff --git a/Userland/Services/CMakeLists.txt b/Userland/Services/CMakeLists.txt index a1428e6eb8..8fa4fd1710 100644 --- a/Userland/Services/CMakeLists.txt +++ b/Userland/Services/CMakeLists.txt @@ -6,6 +6,7 @@ add_subdirectory(DHCPClient) add_subdirectory(EchoServer) add_subdirectory(FileOperation) add_subdirectory(ImageDecoder) +add_subdirectory(KeyboardPreferenceLoader) add_subdirectory(LaunchServer) add_subdirectory(LookupServer) add_subdirectory(NotificationServer) diff --git a/Userland/Services/KeyboardPreferenceLoader/CMakeLists.txt b/Userland/Services/KeyboardPreferenceLoader/CMakeLists.txt new file mode 100644 index 0000000000..fdf613f09d --- /dev/null +++ b/Userland/Services/KeyboardPreferenceLoader/CMakeLists.txt @@ -0,0 +1,6 @@ +set(SOURCES + main.cpp +) + +serenity_bin(KeyboardPreferenceLoader) +target_link_libraries(KeyboardPreferenceLoader LibCore) diff --git a/Userland/Services/KeyboardPreferenceLoader/main.cpp b/Userland/Services/KeyboardPreferenceLoader/main.cpp new file mode 100644 index 0000000000..83dcbe6bcb --- /dev/null +++ b/Userland/Services/KeyboardPreferenceLoader/main.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +int main() +{ + if (pledge("stdio proc exec rpath", nullptr) < 0) { + perror("pledge"); + return 1; + } + + if (unveil("/bin/keymap", "x") < 0) { + perror("unveil /bin/keymap"); + return 1; + } + + if (unveil("/etc/Keyboard.ini", "r") < 0) { + perror("unveil /etc/Keyboard.ini"); + return 1; + } + + if (unveil(nullptr, nullptr) < 0) { + perror("unveil"); + return 1; + } + + auto mapper_config(Core::ConfigFile::open("/etc/Keyboard.ini")); + auto keymap = mapper_config->read_entry("Mapping", "Keymap", ""); + + pid_t child_pid; + const char* argv[] = { "/bin/keymap", keymap.characters(), nullptr }; + if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast(argv), environ))) { + perror("posix_spawn"); + exit(1); + } +} diff --git a/Userland/Utilities/keymap.cpp b/Userland/Utilities/keymap.cpp index c11e6bb7eb..2239603c63 100644 --- a/Userland/Utilities/keymap.cpp +++ b/Userland/Utilities/keymap.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -12,7 +13,7 @@ int main(int argc, char** argv) { - if (pledge("stdio setkeymap getkeymap rpath", nullptr) < 0) { + if (pledge("stdio setkeymap getkeymap rpath wpath cpath", nullptr) < 0) { perror("pledge"); return 1; } @@ -22,6 +23,11 @@ int main(int argc, char** argv) return 1; } + if (unveil("/etc/Keyboard.ini", "rwc") < 0) { + perror("unveil /etc/Keyboard.ini"); + return 1; + } + const char* path = nullptr; Core::ArgsParser args_parser; args_parser.add_positional_argument(path, "The mapping file to be used", "file", Core::ArgsParser::Required::No); @@ -60,7 +66,12 @@ int main(int argc, char** argv) int rc = character_map.value().set_system_map(); if (rc != 0) { perror("setkeymap"); + return rc; } + auto mapper_config(Core::ConfigFile::open("/etc/Keyboard.ini")); + mapper_config->write_entry("Mapping", "Keymap", path); + mapper_config->sync(); + return rc; }