From: Lennart Poettering Date: Fri, 1 Sep 2017 00:09:32 +0000 (+0200) Subject: terminal: unify code for resetting kbd utf8 mode a bit (#6692) X-Git-Tag: v235~179 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c83f349c5fd04c469e8227117b9cde6931c80ba5;p=platform%2Fupstream%2Fsystemd.git terminal: unify code for resetting kbd utf8 mode a bit (#6692) We have the same code at two places, let's unify that at one place. Follow-up for #6606 --- diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 0a1638b..4df23ae 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -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; @@ -1232,3 +1227,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; +} diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index b862bfa..8c50ed0 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -117,3 +117,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); + +int vt_default_utf8(void); +int vt_reset_keyboard(int fd); diff --git a/src/login/logind-session.c b/src/login/logind-session.c index c345b1c..736f7d9 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -1137,7 +1137,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 @@ -1156,12 +1156,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);