Bladeren bron

Userland: Preserve keyboard mapping preference on reboot (#6955)

Ömer Kurttekin 4 jaren geleden
bovenliggende
commit
d922c2f5f3

+ 2 - 0
Base/etc/Keyboard.ini

@@ -0,0 +1,2 @@
+[Mapping]
+Keymap=en-us

+ 4 - 0
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

+ 1 - 0
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)

+ 6 - 0
Userland/Services/KeyboardPreferenceLoader/CMakeLists.txt

@@ -0,0 +1,6 @@
+set(SOURCES
+    main.cpp
+)
+
+serenity_bin(KeyboardPreferenceLoader)
+target_link_libraries(KeyboardPreferenceLoader LibCore)

+ 43 - 0
Userland/Services/KeyboardPreferenceLoader/main.cpp

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2021, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/ConfigFile.h>
+#include <spawn.h>
+#include <stdio.h>
+#include <unistd.h>
+
+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<char**>(argv), environ))) {
+        perror("posix_spawn");
+        exit(1);
+    }
+}

+ 12 - 1
Userland/Utilities/keymap.cpp

@@ -5,6 +5,7 @@
  */
 
 #include <LibCore/ArgsParser.h>
+#include <LibCore/ConfigFile.h>
 #include <LibKeyboard/CharacterMap.h>
 #include <stdio.h>
 #include <string.h>
@@ -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;
 }