chiark / gitweb /
terminal: unify code for resetting kbd utf8 mode a bit (#6692)
authorLennart Poettering <lennart@poettering.net>
Fri, 1 Sep 2017 00:09:32 +0000 (02:09 +0200)
committerSven Eden <yamakuzure@gmx.net>
Mon, 25 Sep 2017 12:36:36 +0000 (14:36 +0200)
We have the same code at two places, let's unify that at one place.

Follow-up for #6606

src/basic/terminal-util.c
src/basic/terminal-util.h
src/login/logind-session.c

index aec98baabc519b4f1d6a57b1df8445badd3eeeb6..c81aa400b95400c0ae7c3ba468bc699ad0425094 100644 (file)
@@ -246,7 +246,6 @@ int ask_string(char **ret, const char *text, ...) {
 int reset_terminal_fd(int fd, bool switch_to_text) {
         struct termios termios;
         _cleanup_free_ char *utf8 = NULL;
-        int kb;
         int r = 0;
 
         /* Set terminal to some sane defaults */
@@ -265,11 +264,7 @@ int reset_terminal_fd(int fd, bool switch_to_text) {
                 (void) ioctl(fd, KDSETMODE, KD_TEXT);
 
         /* Set default keyboard mode */
-        if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && parse_boolean(utf8) == 0)
-                kb = K_XLATE;
-        else
-                kb = K_UNICODE;
-        (void) ioctl(fd, KDSKBMODE, kb);
+        (void) vt_reset_keyboard(fd);
 
         if (tcgetattr(fd, &termios) < 0) {
                 r = -errno;
@@ -1245,3 +1240,28 @@ bool colors_enabled(void) {
 
         return enabled;
 }
+
+int vt_default_utf8(void) {
+        _cleanup_free_ char *b = NULL;
+        int r;
+
+        /* Read the default VT UTF8 setting from the kernel */
+
+        r = read_one_line_file("/sys/module/vt/parameters/default_utf8", &b);
+        if (r < 0)
+                return r;
+
+        return parse_boolean(b);
+}
+
+int vt_reset_keyboard(int fd) {
+        int kb;
+
+        /* If we can't read the default, then default to unicode. It's 2017 after all. */
+        kb = vt_default_utf8() != 0 ? K_UNICODE : K_XLATE;
+
+        if (ioctl(fd, KDSKBMODE, kb) < 0)
+                return -errno;
+
+        return 0;
+}
index 84ba3a7f70f925005c70577c49d7db6d61af3b6a..5d37a44774101a9c61896a86bc1b6ba6a7b56d65 100644 (file)
@@ -133,3 +133,6 @@ int ptsname_namespace(int pty, char **ret);
 int openpt_in_namespace(pid_t pid, int flags);
 int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
 #endif // 0
+
+int vt_default_utf8(void);
+int vt_reset_keyboard(int fd);
index 59c0c6c0e5ecf56c087045c0c300f4952a6f4079..142ba55e947d100988d208f40281ac8f1c8f61c7 100644 (file)
@@ -33,7 +33,6 @@
 #include "bus-error.h"
 #include "bus-util.h"
 #include "escape.h"
-#include "extract-word.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "format-util.h"
 #include "mkdir.h"
 #include "parse-util.h"
 #include "path-util.h"
-#include "process-util.h"
 #include "string-table.h"
 #include "terminal-util.h"
 #include "user-util.h"
 #include "util.h"
+#include "process-util.h"
 
 #define RELEASE_USEC (20*USEC_PER_SEC)
 
@@ -1216,7 +1215,7 @@ void session_restore_vt(Session *s) {
         };
 
         _cleanup_free_ char *utf8 = NULL;
-        int vt, kb, old_fd;
+        int vt, old_fd;
 
         /* We need to get a fresh handle to the virtual terminal,
          * since the old file-descriptor is potentially in a hung-up
@@ -1235,12 +1234,7 @@ void session_restore_vt(Session *s) {
 
         (void) ioctl(vt, KDSETMODE, KD_TEXT);
 
-        if (read_one_line_file("/sys/module/vt/parameters/default_utf8", &utf8) >= 0 && parse_boolean(utf8) == 0)
-                kb = K_XLATE;
-        else
-                kb = K_UNICODE;
-
-        (void) ioctl(vt, KDSKBMODE, kb);
+        (void) vt_reset_keyboard(vt);
 
         (void) ioctl(vt, VT_SETMODE, &mode);
         (void) fchown(vt, 0, (gid_t) -1);