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