]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
4897d1dc ZJS |
2 | |
3 | #include <errno.h> | |
0d536673 | 4 | #include <stdio_ext.h> |
4897d1dc ZJS |
5 | #include <string.h> |
6 | #include <unistd.h> | |
7 | ||
8 | #include "def.h" | |
9 | #include "env-util.h" | |
10 | #include "fd-util.h" | |
11 | #include "fileio-label.h" | |
12 | #include "fileio.h" | |
13 | #include "keymap-util.h" | |
14 | #include "locale-util.h" | |
15 | #include "macro.h" | |
16 | #include "mkdir.h" | |
17 | #include "string-util.h" | |
18 | #include "strv.h" | |
19 | ||
20 | static bool startswith_comma(const char *s, const char *prefix) { | |
5ad327dd ZJS |
21 | s = startswith(s, prefix); |
22 | if (!s) | |
23 | return false; | |
4897d1dc | 24 | |
4c701096 | 25 | return IN_SET(*s, ',', '\0'); |
4897d1dc ZJS |
26 | } |
27 | ||
28 | static const char* strnulldash(const char *s) { | |
29 | return isempty(s) || streq(s, "-") ? NULL : s; | |
30 | } | |
31 | ||
cabffaf8 ZJS |
32 | static const char* systemd_kbd_model_map(void) { |
33 | const char* s; | |
34 | ||
35 | s = getenv("SYSTEMD_KBD_MODEL_MAP"); | |
36 | if (s) | |
37 | return s; | |
38 | ||
39 | return SYSTEMD_KBD_MODEL_MAP; | |
40 | } | |
41 | ||
42 | static const char* systemd_language_fallback_map(void) { | |
43 | const char* s; | |
44 | ||
45 | s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP"); | |
46 | if (s) | |
47 | return s; | |
48 | ||
49 | return SYSTEMD_LANGUAGE_FALLBACK_MAP; | |
50 | } | |
51 | ||
4897d1dc ZJS |
52 | static void context_free_x11(Context *c) { |
53 | c->x11_layout = mfree(c->x11_layout); | |
54 | c->x11_options = mfree(c->x11_options); | |
55 | c->x11_model = mfree(c->x11_model); | |
56 | c->x11_variant = mfree(c->x11_variant); | |
57 | } | |
58 | ||
59 | static void context_free_vconsole(Context *c) { | |
60 | c->vc_keymap = mfree(c->vc_keymap); | |
61 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); | |
62 | } | |
63 | ||
64 | static void context_free_locale(Context *c) { | |
65 | int p; | |
66 | ||
67 | for (p = 0; p < _VARIABLE_LC_MAX; p++) | |
68 | c->locale[p] = mfree(c->locale[p]); | |
69 | } | |
70 | ||
71 | void context_free(Context *c) { | |
72 | context_free_locale(c); | |
73 | context_free_x11(c); | |
74 | context_free_vconsole(c); | |
65d34266 YW |
75 | |
76 | sd_bus_message_unref(c->locale_cache); | |
77 | sd_bus_message_unref(c->x11_cache); | |
78 | sd_bus_message_unref(c->vc_cache); | |
4897d1dc ZJS |
79 | }; |
80 | ||
df4fd2c7 | 81 | void locale_simplify(char *locale[_VARIABLE_LC_MAX]) { |
4897d1dc ZJS |
82 | int p; |
83 | ||
84 | for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++) | |
df4fd2c7 YW |
85 | if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p])) |
86 | locale[p] = mfree(locale[p]); | |
4897d1dc ZJS |
87 | } |
88 | ||
df4fd2c7 YW |
89 | int locale_read_data(Context *c, sd_bus_message *m) { |
90 | struct stat st; | |
4897d1dc ZJS |
91 | int r; |
92 | ||
df4fd2c7 | 93 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
94 | if (m) { |
95 | if (m == c->locale_cache) | |
96 | return 0; | |
4897d1dc | 97 | |
65d34266 YW |
98 | sd_bus_message_unref(c->locale_cache); |
99 | c->locale_cache = sd_bus_message_ref(m); | |
100 | } | |
df4fd2c7 YW |
101 | |
102 | r = stat("/etc/locale.conf", &st); | |
103 | if (r < 0 && errno != ENOENT) | |
104 | return -errno; | |
105 | ||
106 | if (r >= 0) { | |
107 | usec_t t; | |
108 | ||
109 | /* If mtime is not changed, then we do not need to re-read the file. */ | |
110 | t = timespec_load(&st.st_mtim); | |
111 | if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime) | |
112 | return 0; | |
4897d1dc | 113 | |
df4fd2c7 YW |
114 | c->locale_mtime = t; |
115 | context_free_locale(c); | |
116 | ||
117 | r = parse_env_file(NULL, "/etc/locale.conf", NEWLINE, | |
118 | "LANG", &c->locale[VARIABLE_LANG], | |
119 | "LANGUAGE", &c->locale[VARIABLE_LANGUAGE], | |
120 | "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE], | |
121 | "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC], | |
122 | "LC_TIME", &c->locale[VARIABLE_LC_TIME], | |
123 | "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE], | |
124 | "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY], | |
125 | "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES], | |
126 | "LC_PAPER", &c->locale[VARIABLE_LC_PAPER], | |
127 | "LC_NAME", &c->locale[VARIABLE_LC_NAME], | |
128 | "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS], | |
129 | "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE], | |
130 | "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT], | |
131 | "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION], | |
132 | NULL); | |
133 | if (r < 0) | |
134 | return r; | |
135 | } else { | |
4897d1dc ZJS |
136 | int p; |
137 | ||
df4fd2c7 YW |
138 | c->locale_mtime = USEC_INFINITY; |
139 | context_free_locale(c); | |
140 | ||
4897d1dc ZJS |
141 | /* Fill in what we got passed from systemd. */ |
142 | for (p = 0; p < _VARIABLE_LC_MAX; p++) { | |
143 | const char *name; | |
144 | ||
145 | name = locale_variable_to_string(p); | |
146 | assert(name); | |
147 | ||
148 | r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name))); | |
149 | if (r < 0) | |
150 | return r; | |
151 | } | |
4897d1dc ZJS |
152 | } |
153 | ||
df4fd2c7 YW |
154 | locale_simplify(c->locale); |
155 | return 0; | |
4897d1dc ZJS |
156 | } |
157 | ||
df4fd2c7 YW |
158 | int vconsole_read_data(Context *c, sd_bus_message *m) { |
159 | struct stat st; | |
160 | usec_t t; | |
4897d1dc ZJS |
161 | int r; |
162 | ||
df4fd2c7 | 163 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
164 | if (m) { |
165 | if (m == c->vc_cache) | |
166 | return 0; | |
df4fd2c7 | 167 | |
65d34266 YW |
168 | sd_bus_message_unref(c->vc_cache); |
169 | c->vc_cache = sd_bus_message_ref(m); | |
170 | } | |
df4fd2c7 YW |
171 | |
172 | if (stat("/etc/vconsole.conf", &st) < 0) { | |
173 | if (errno != ENOENT) | |
174 | return -errno; | |
175 | ||
176 | c->vc_mtime = USEC_INFINITY; | |
177 | context_free_vconsole(c); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | /* If mtime is not changed, then we do not need to re-read */ | |
182 | t = timespec_load(&st.st_mtim); | |
183 | if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime) | |
184 | return 0; | |
185 | ||
186 | c->vc_mtime = t; | |
4897d1dc ZJS |
187 | context_free_vconsole(c); |
188 | ||
1a5a177e | 189 | r = parse_env_file(NULL, "/etc/vconsole.conf", NEWLINE, |
4897d1dc ZJS |
190 | "KEYMAP", &c->vc_keymap, |
191 | "KEYMAP_TOGGLE", &c->vc_keymap_toggle, | |
192 | NULL); | |
df4fd2c7 | 193 | if (r < 0) |
4897d1dc ZJS |
194 | return r; |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
df4fd2c7 YW |
199 | int x11_read_data(Context *c, sd_bus_message *m) { |
200 | _cleanup_fclose_ FILE *f = NULL; | |
4897d1dc | 201 | bool in_section = false; |
df4fd2c7 YW |
202 | char line[LINE_MAX]; |
203 | struct stat st; | |
204 | usec_t t; | |
4897d1dc ZJS |
205 | int r; |
206 | ||
df4fd2c7 | 207 | /* Do not try to re-read the file within single bus operation. */ |
65d34266 YW |
208 | if (m) { |
209 | if (m == c->x11_cache) | |
210 | return 0; | |
df4fd2c7 | 211 | |
65d34266 YW |
212 | sd_bus_message_unref(c->x11_cache); |
213 | c->x11_cache = sd_bus_message_ref(m); | |
214 | } | |
df4fd2c7 YW |
215 | |
216 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) { | |
217 | if (errno != ENOENT) | |
218 | return -errno; | |
219 | ||
220 | c->x11_mtime = USEC_INFINITY; | |
221 | context_free_x11(c); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | /* If mtime is not changed, then we do not need to re-read */ | |
226 | t = timespec_load(&st.st_mtim); | |
227 | if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime) | |
228 | return 0; | |
229 | ||
230 | c->x11_mtime = t; | |
4897d1dc ZJS |
231 | context_free_x11(c); |
232 | ||
233 | f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re"); | |
234 | if (!f) | |
df4fd2c7 | 235 | return -errno; |
4897d1dc ZJS |
236 | |
237 | while (fgets(line, sizeof(line), f)) { | |
238 | char *l; | |
239 | ||
240 | char_array_0(line); | |
241 | l = strstrip(line); | |
242 | ||
4c701096 | 243 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
244 | continue; |
245 | ||
246 | if (in_section && first_word(l, "Option")) { | |
247 | _cleanup_strv_free_ char **a = NULL; | |
248 | ||
249 | r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); | |
250 | if (r < 0) | |
251 | return r; | |
252 | ||
253 | if (strv_length(a) == 3) { | |
254 | char **p = NULL; | |
255 | ||
256 | if (streq(a[1], "XkbLayout")) | |
257 | p = &c->x11_layout; | |
258 | else if (streq(a[1], "XkbModel")) | |
259 | p = &c->x11_model; | |
260 | else if (streq(a[1], "XkbVariant")) | |
261 | p = &c->x11_variant; | |
262 | else if (streq(a[1], "XkbOptions")) | |
263 | p = &c->x11_options; | |
264 | ||
265 | if (p) { | |
f9ecfd3b | 266 | free_and_replace(*p, a[2]); |
4897d1dc ZJS |
267 | } |
268 | } | |
269 | ||
270 | } else if (!in_section && first_word(l, "Section")) { | |
271 | _cleanup_strv_free_ char **a = NULL; | |
272 | ||
273 | r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES); | |
274 | if (r < 0) | |
275 | return -ENOMEM; | |
276 | ||
277 | if (strv_length(a) == 2 && streq(a[1], "InputClass")) | |
278 | in_section = true; | |
279 | ||
280 | } else if (in_section && first_word(l, "EndSection")) | |
281 | in_section = false; | |
282 | } | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
4897d1dc | 287 | int locale_write_data(Context *c, char ***settings) { |
4897d1dc | 288 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 YW |
289 | struct stat st; |
290 | int r, p; | |
4897d1dc ZJS |
291 | |
292 | /* Set values will be returned as strv in *settings on success. */ | |
293 | ||
4897d1dc ZJS |
294 | for (p = 0; p < _VARIABLE_LC_MAX; p++) { |
295 | _cleanup_free_ char *t = NULL; | |
296 | char **u; | |
297 | const char *name; | |
298 | ||
299 | name = locale_variable_to_string(p); | |
300 | assert(name); | |
301 | ||
df4fd2c7 | 302 | if (isempty(c->locale[p])) |
4897d1dc | 303 | continue; |
4897d1dc ZJS |
304 | |
305 | if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0) | |
306 | return -ENOMEM; | |
307 | ||
308 | u = strv_env_set(l, t); | |
309 | if (!u) | |
310 | return -ENOMEM; | |
311 | ||
130d3d22 | 312 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
313 | } |
314 | ||
315 | if (strv_isempty(l)) { | |
316 | if (unlink("/etc/locale.conf") < 0) | |
317 | return errno == ENOENT ? 0 : -errno; | |
318 | ||
df4fd2c7 | 319 | c->locale_mtime = USEC_INFINITY; |
4897d1dc ZJS |
320 | return 0; |
321 | } | |
322 | ||
323 | r = write_env_file_label("/etc/locale.conf", l); | |
324 | if (r < 0) | |
325 | return r; | |
326 | ||
ae2a15bc | 327 | *settings = TAKE_PTR(l); |
df4fd2c7 YW |
328 | |
329 | if (stat("/etc/locale.conf", &st) >= 0) | |
330 | c->locale_mtime = timespec_load(&st.st_mtim); | |
331 | ||
4897d1dc ZJS |
332 | return 0; |
333 | } | |
334 | ||
335 | int vconsole_write_data(Context *c) { | |
4897d1dc | 336 | _cleanup_strv_free_ char **l = NULL; |
df4fd2c7 YW |
337 | struct stat st; |
338 | int r; | |
4897d1dc ZJS |
339 | |
340 | r = load_env_file(NULL, "/etc/vconsole.conf", NULL, &l); | |
341 | if (r < 0 && r != -ENOENT) | |
342 | return r; | |
343 | ||
344 | if (isempty(c->vc_keymap)) | |
345 | l = strv_env_unset(l, "KEYMAP"); | |
346 | else { | |
347 | _cleanup_free_ char *s = NULL; | |
348 | char **u; | |
349 | ||
350 | s = strappend("KEYMAP=", c->vc_keymap); | |
351 | if (!s) | |
352 | return -ENOMEM; | |
353 | ||
354 | u = strv_env_set(l, s); | |
355 | if (!u) | |
356 | return -ENOMEM; | |
357 | ||
130d3d22 | 358 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
359 | } |
360 | ||
361 | if (isempty(c->vc_keymap_toggle)) | |
362 | l = strv_env_unset(l, "KEYMAP_TOGGLE"); | |
363 | else { | |
364 | _cleanup_free_ char *s = NULL; | |
365 | char **u; | |
366 | ||
367 | s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle); | |
368 | if (!s) | |
369 | return -ENOMEM; | |
370 | ||
371 | u = strv_env_set(l, s); | |
372 | if (!u) | |
373 | return -ENOMEM; | |
374 | ||
130d3d22 | 375 | strv_free_and_replace(l, u); |
4897d1dc ZJS |
376 | } |
377 | ||
378 | if (strv_isempty(l)) { | |
379 | if (unlink("/etc/vconsole.conf") < 0) | |
380 | return errno == ENOENT ? 0 : -errno; | |
381 | ||
df4fd2c7 | 382 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
383 | return 0; |
384 | } | |
385 | ||
df4fd2c7 YW |
386 | r = write_env_file_label("/etc/vconsole.conf", l); |
387 | if (r < 0) | |
388 | return r; | |
389 | ||
390 | if (stat("/etc/vconsole.conf", &st) >= 0) | |
391 | c->vc_mtime = timespec_load(&st.st_mtim); | |
392 | ||
393 | return 0; | |
4897d1dc ZJS |
394 | } |
395 | ||
396 | int x11_write_data(Context *c) { | |
397 | _cleanup_fclose_ FILE *f = NULL; | |
398 | _cleanup_free_ char *temp_path = NULL; | |
df4fd2c7 | 399 | struct stat st; |
4897d1dc ZJS |
400 | int r; |
401 | ||
402 | if (isempty(c->x11_layout) && | |
403 | isempty(c->x11_model) && | |
404 | isempty(c->x11_variant) && | |
405 | isempty(c->x11_options)) { | |
406 | ||
407 | if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) | |
408 | return errno == ENOENT ? 0 : -errno; | |
409 | ||
df4fd2c7 | 410 | c->vc_mtime = USEC_INFINITY; |
4897d1dc ZJS |
411 | return 0; |
412 | } | |
413 | ||
414 | mkdir_p_label("/etc/X11/xorg.conf.d", 0755); | |
415 | ||
416 | r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path); | |
417 | if (r < 0) | |
418 | return r; | |
419 | ||
0d536673 LP |
420 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
421 | (void) fchmod(fileno(f), 0644); | |
4897d1dc | 422 | |
0d536673 LP |
423 | fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n" |
424 | "# probably wise not to edit this file manually. Use localectl(1) to\n" | |
425 | "# instruct systemd-localed to update it.\n" | |
426 | "Section \"InputClass\"\n" | |
427 | " Identifier \"system-keyboard\"\n" | |
428 | " MatchIsKeyboard \"on\"\n", f); | |
4897d1dc ZJS |
429 | |
430 | if (!isempty(c->x11_layout)) | |
431 | fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout); | |
432 | ||
433 | if (!isempty(c->x11_model)) | |
434 | fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model); | |
435 | ||
436 | if (!isempty(c->x11_variant)) | |
437 | fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant); | |
438 | ||
439 | if (!isempty(c->x11_options)) | |
440 | fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options); | |
441 | ||
0d536673 | 442 | fputs("EndSection\n", f); |
4897d1dc | 443 | |
0675e94a | 444 | r = fflush_sync_and_check(f); |
4897d1dc ZJS |
445 | if (r < 0) |
446 | goto fail; | |
447 | ||
448 | if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) { | |
449 | r = -errno; | |
450 | goto fail; | |
451 | } | |
452 | ||
df4fd2c7 YW |
453 | if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0) |
454 | c->x11_mtime = timespec_load(&st.st_mtim); | |
455 | ||
4897d1dc ZJS |
456 | return 0; |
457 | ||
458 | fail: | |
4897d1dc ZJS |
459 | if (temp_path) |
460 | (void) unlink(temp_path); | |
461 | ||
462 | return r; | |
463 | } | |
464 | ||
465 | static int read_next_mapping(const char* filename, | |
466 | unsigned min_fields, unsigned max_fields, | |
467 | FILE *f, unsigned *n, char ***a) { | |
468 | assert(f); | |
469 | assert(n); | |
470 | assert(a); | |
471 | ||
472 | for (;;) { | |
473 | char line[LINE_MAX]; | |
474 | char *l, **b; | |
475 | int r; | |
476 | size_t length; | |
477 | ||
478 | errno = 0; | |
479 | if (!fgets(line, sizeof(line), f)) { | |
480 | ||
481 | if (ferror(f)) | |
482 | return errno > 0 ? -errno : -EIO; | |
483 | ||
484 | return 0; | |
485 | } | |
486 | ||
487 | (*n)++; | |
488 | ||
489 | l = strstrip(line); | |
4c701096 | 490 | if (IN_SET(l[0], 0, '#')) |
4897d1dc ZJS |
491 | continue; |
492 | ||
493 | r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES); | |
494 | if (r < 0) | |
495 | return r; | |
496 | ||
497 | length = strv_length(b); | |
498 | if (length < min_fields || length > max_fields) { | |
499 | log_error("Invalid line %s:%u, ignoring.", filename, *n); | |
500 | strv_free(b); | |
501 | continue; | |
502 | ||
503 | } | |
504 | ||
505 | *a = b; | |
506 | return 1; | |
507 | } | |
508 | } | |
509 | ||
510 | int vconsole_convert_to_x11(Context *c) { | |
cabffaf8 | 511 | const char *map; |
6f3287b3 | 512 | int modified = -1; |
4897d1dc | 513 | |
cabffaf8 ZJS |
514 | map = systemd_kbd_model_map(); |
515 | ||
4897d1dc | 516 | if (isempty(c->vc_keymap)) { |
4897d1dc ZJS |
517 | modified = |
518 | !isempty(c->x11_layout) || | |
519 | !isempty(c->x11_model) || | |
520 | !isempty(c->x11_variant) || | |
521 | !isempty(c->x11_options); | |
522 | ||
523 | context_free_x11(c); | |
524 | } else { | |
525 | _cleanup_fclose_ FILE *f = NULL; | |
526 | unsigned n = 0; | |
527 | ||
cabffaf8 | 528 | f = fopen(map, "re"); |
4897d1dc ZJS |
529 | if (!f) |
530 | return -errno; | |
531 | ||
532 | for (;;) { | |
533 | _cleanup_strv_free_ char **a = NULL; | |
534 | int r; | |
535 | ||
cabffaf8 | 536 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
537 | if (r < 0) |
538 | return r; | |
539 | if (r == 0) | |
540 | break; | |
541 | ||
542 | if (!streq(c->vc_keymap, a[0])) | |
543 | continue; | |
544 | ||
545 | if (!streq_ptr(c->x11_layout, strnulldash(a[1])) || | |
546 | !streq_ptr(c->x11_model, strnulldash(a[2])) || | |
547 | !streq_ptr(c->x11_variant, strnulldash(a[3])) || | |
548 | !streq_ptr(c->x11_options, strnulldash(a[4]))) { | |
549 | ||
550 | if (free_and_strdup(&c->x11_layout, strnulldash(a[1])) < 0 || | |
551 | free_and_strdup(&c->x11_model, strnulldash(a[2])) < 0 || | |
552 | free_and_strdup(&c->x11_variant, strnulldash(a[3])) < 0 || | |
553 | free_and_strdup(&c->x11_options, strnulldash(a[4])) < 0) | |
554 | return -ENOMEM; | |
555 | ||
556 | modified = true; | |
557 | } | |
558 | ||
559 | break; | |
560 | } | |
561 | } | |
562 | ||
6f3287b3 | 563 | if (modified > 0) |
4897d1dc ZJS |
564 | log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'", |
565 | strempty(c->x11_layout), | |
566 | strempty(c->x11_model), | |
567 | strempty(c->x11_variant), | |
568 | strempty(c->x11_options)); | |
6f3287b3 ZJS |
569 | else if (modified < 0) |
570 | log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".", | |
571 | c->vc_keymap); | |
4897d1dc | 572 | else |
6f3287b3 | 573 | log_debug("X11 keyboard layout did not need to be modified."); |
4897d1dc | 574 | |
6f3287b3 | 575 | return modified > 0; |
4897d1dc ZJS |
576 | } |
577 | ||
578 | int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) { | |
579 | const char *dir; | |
580 | _cleanup_free_ char *n; | |
581 | ||
582 | if (x11_variant) | |
605405c6 | 583 | n = strjoin(x11_layout, "-", x11_variant); |
4897d1dc ZJS |
584 | else |
585 | n = strdup(x11_layout); | |
586 | if (!n) | |
587 | return -ENOMEM; | |
588 | ||
589 | NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) { | |
590 | _cleanup_free_ char *p = NULL, *pz = NULL; | |
591 | bool uncompressed; | |
592 | ||
605405c6 ZJS |
593 | p = strjoin(dir, "xkb/", n, ".map"); |
594 | pz = strjoin(dir, "xkb/", n, ".map.gz"); | |
4897d1dc ZJS |
595 | if (!p || !pz) |
596 | return -ENOMEM; | |
597 | ||
598 | uncompressed = access(p, F_OK) == 0; | |
599 | if (uncompressed || access(pz, F_OK) == 0) { | |
600 | log_debug("Found converted keymap %s at %s", | |
601 | n, uncompressed ? p : pz); | |
602 | ||
ae2a15bc | 603 | *new_keymap = TAKE_PTR(n); |
4897d1dc ZJS |
604 | return 1; |
605 | } | |
606 | } | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
6a6e9c03 | 611 | int find_legacy_keymap(Context *c, char **ret) { |
cabffaf8 ZJS |
612 | const char *map; |
613 | _cleanup_fclose_ FILE *f = NULL; | |
6a6e9c03 | 614 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
615 | unsigned n = 0; |
616 | unsigned best_matching = 0; | |
617 | int r; | |
618 | ||
5ad327dd ZJS |
619 | assert(!isempty(c->x11_layout)); |
620 | ||
cabffaf8 ZJS |
621 | map = systemd_kbd_model_map(); |
622 | ||
623 | f = fopen(map, "re"); | |
4897d1dc ZJS |
624 | if (!f) |
625 | return -errno; | |
626 | ||
627 | for (;;) { | |
628 | _cleanup_strv_free_ char **a = NULL; | |
629 | unsigned matching = 0; | |
630 | ||
cabffaf8 | 631 | r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a); |
4897d1dc ZJS |
632 | if (r < 0) |
633 | return r; | |
634 | if (r == 0) | |
635 | break; | |
636 | ||
637 | /* Determine how well matching this entry is */ | |
5ad327dd | 638 | if (streq(c->x11_layout, a[1])) |
4897d1dc ZJS |
639 | /* If we got an exact match, this is best */ |
640 | matching = 10; | |
641 | else { | |
642 | /* We have multiple X layouts, look for an | |
643 | * entry that matches our key with everything | |
644 | * but the first layout stripped off. */ | |
645 | if (startswith_comma(c->x11_layout, a[1])) | |
646 | matching = 5; | |
647 | else { | |
648 | char *x; | |
649 | ||
650 | /* If that didn't work, strip off the | |
651 | * other layouts from the entry, too */ | |
652 | x = strndupa(a[1], strcspn(a[1], ",")); | |
653 | if (startswith_comma(c->x11_layout, x)) | |
654 | matching = 1; | |
655 | } | |
656 | } | |
657 | ||
658 | if (matching > 0) { | |
659 | if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) { | |
660 | matching++; | |
661 | ||
662 | if (streq_ptr(c->x11_variant, a[3])) { | |
663 | matching++; | |
664 | ||
665 | if (streq_ptr(c->x11_options, a[4])) | |
666 | matching++; | |
667 | } | |
668 | } | |
669 | } | |
670 | ||
671 | /* The best matching entry so far, then let's save that */ | |
672 | if (matching >= MAX(best_matching, 1u)) { | |
673 | log_debug("Found legacy keymap %s with score %u", | |
674 | a[0], matching); | |
675 | ||
676 | if (matching > best_matching) { | |
677 | best_matching = matching; | |
678 | ||
6a6e9c03 | 679 | r = free_and_strdup(&new_keymap, a[0]); |
4897d1dc ZJS |
680 | if (r < 0) |
681 | return r; | |
682 | } | |
683 | } | |
684 | } | |
685 | ||
686 | if (best_matching < 10 && c->x11_layout) { | |
687 | /* The best match is only the first part of the X11 | |
688 | * keymap. Check if we have a converted map which | |
689 | * matches just the first layout. | |
690 | */ | |
691 | char *l, *v = NULL, *converted; | |
692 | ||
693 | l = strndupa(c->x11_layout, strcspn(c->x11_layout, ",")); | |
694 | if (c->x11_variant) | |
695 | v = strndupa(c->x11_variant, strcspn(c->x11_variant, ",")); | |
696 | r = find_converted_keymap(l, v, &converted); | |
697 | if (r < 0) | |
698 | return r; | |
6a6e9c03 ZJS |
699 | if (r > 0) |
700 | free_and_replace(new_keymap, converted); | |
4897d1dc ZJS |
701 | } |
702 | ||
6a6e9c03 ZJS |
703 | *ret = TAKE_PTR(new_keymap); |
704 | return (bool) *ret; | |
4897d1dc ZJS |
705 | } |
706 | ||
707 | int find_language_fallback(const char *lang, char **language) { | |
cabffaf8 | 708 | const char *map; |
4897d1dc ZJS |
709 | _cleanup_fclose_ FILE *f = NULL; |
710 | unsigned n = 0; | |
711 | ||
aa63b56f | 712 | assert(lang); |
4897d1dc ZJS |
713 | assert(language); |
714 | ||
cabffaf8 ZJS |
715 | map = systemd_language_fallback_map(); |
716 | ||
717 | f = fopen(map, "re"); | |
4897d1dc ZJS |
718 | if (!f) |
719 | return -errno; | |
720 | ||
721 | for (;;) { | |
722 | _cleanup_strv_free_ char **a = NULL; | |
723 | int r; | |
724 | ||
cabffaf8 | 725 | r = read_next_mapping(map, 2, 2, f, &n, &a); |
4897d1dc ZJS |
726 | if (r <= 0) |
727 | return r; | |
728 | ||
729 | if (streq(lang, a[0])) { | |
730 | assert(strv_length(a) == 2); | |
1cc6c93a | 731 | *language = TAKE_PTR(a[1]); |
4897d1dc ZJS |
732 | return 1; |
733 | } | |
734 | } | |
735 | ||
736 | assert_not_reached("should not be here"); | |
737 | } | |
738 | ||
739 | int x11_convert_to_vconsole(Context *c) { | |
740 | bool modified = false; | |
741 | ||
742 | if (isempty(c->x11_layout)) { | |
4897d1dc ZJS |
743 | modified = |
744 | !isempty(c->vc_keymap) || | |
745 | !isempty(c->vc_keymap_toggle); | |
746 | ||
aa63b56f | 747 | context_free_vconsole(c); |
4897d1dc | 748 | } else { |
6a837b03 | 749 | _cleanup_free_ char *new_keymap = NULL; |
4897d1dc ZJS |
750 | int r; |
751 | ||
752 | r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap); | |
753 | if (r < 0) | |
754 | return r; | |
755 | else if (r == 0) { | |
756 | r = find_legacy_keymap(c, &new_keymap); | |
757 | if (r < 0) | |
758 | return r; | |
759 | } | |
5ad327dd ZJS |
760 | if (r == 0) |
761 | /* We search for layout-variant match first, but then we also look | |
762 | * for anything which matches just the layout. So it's accurate to say | |
763 | * that we couldn't find anything which matches the layout. */ | |
764 | log_notice("No conversion to virtual console map found for \"%s\".", | |
765 | c->x11_layout); | |
4897d1dc ZJS |
766 | |
767 | if (!streq_ptr(c->vc_keymap, new_keymap)) { | |
6a837b03 | 768 | free_and_replace(c->vc_keymap, new_keymap); |
4897d1dc ZJS |
769 | c->vc_keymap_toggle = mfree(c->vc_keymap_toggle); |
770 | modified = true; | |
6a837b03 | 771 | } |
4897d1dc ZJS |
772 | } |
773 | ||
774 | if (modified) | |
775 | log_info("Changing virtual console keymap to '%s' toggle '%s'", | |
776 | strempty(c->vc_keymap), strempty(c->vc_keymap_toggle)); | |
777 | else | |
778 | log_debug("Virtual console keymap was not modified."); | |
779 | ||
780 | return modified; | |
781 | } |