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
This commit is contained in:
Lennart Poettering
2017-09-01 09:09:32 +09:00
committed by Yu Watanabe
parent aac8c0c382
commit c83f349c5f
3 changed files with 31 additions and 13 deletions
+26 -6
View 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;
@@ -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;
}
+3
View File
@@ -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);