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