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