1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
30 #include "dbus-common.h"
35 " <interface name=\"org.freedesktop.locale1\">\n" \
36 " <property name=\"Locale\" type=\"as\" access=\"read\"/>\n" \
37 " <property name=\"VConsoleKeymap\" type=\"s\" access=\"read\"/>\n" \
38 " <property name=\"VConsoleKeymapToggle\" type=\"s\" access=\"read\"/>\n" \
39 " <property name=\"X11Layout\" type=\"s\" access=\"read\"/>\n" \
40 " <property name=\"X11Model\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"X11Variant\" type=\"s\" access=\"read\"/>\n" \
42 " <property name=\"X11Options\" type=\"s\" access=\"read\"/>\n" \
43 " <method name=\"SetLocale\">\n" \
44 " <arg name=\"locale\" type=\"as\" direction=\"in\"/>\n" \
45 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
47 " <method name=\"SetVConsoleKeyboard\">\n" \
48 " <arg name=\"keymap\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"keymap_toggle\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
51 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
53 " <method name=\"SetX11Keyboard\">\n" \
54 " <arg name=\"layout\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"model\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"variant\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"options\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"convert\" type=\"b\" direction=\"in\"/>\n" \
59 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
63 #define INTROSPECTION \
64 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
67 BUS_PROPERTIES_INTERFACE \
68 BUS_INTROSPECTABLE_INTERFACE \
72 #define INTERFACES_LIST \
73 BUS_GENERIC_INTERFACES_LIST \
74 "org.freedesktop.locale1\0"
76 const char locale_interface[] _introspect_("locale1") = INTERFACE;
79 /* We don't list LC_ALL here on purpose. People should be
80 * using LANG instead. */
95 PROP_LC_IDENTIFICATION,
99 static const char * const names[_PROP_MAX] = {
100 [PROP_LANG] = "LANG",
101 [PROP_LANGUAGE] = "LANGUAGE",
102 [PROP_LC_CTYPE] = "LC_CTYPE",
103 [PROP_LC_NUMERIC] = "LC_NUMERIC",
104 [PROP_LC_TIME] = "LC_TIME",
105 [PROP_LC_COLLATE] = "LC_COLLATE",
106 [PROP_LC_MONETARY] = "LC_MONETARY",
107 [PROP_LC_MESSAGES] = "LC_MESSAGES",
108 [PROP_LC_PAPER] = "LC_PAPER",
109 [PROP_LC_NAME] = "LC_NAME",
110 [PROP_LC_ADDRESS] = "LC_ADDRESS",
111 [PROP_LC_TELEPHONE] = "LC_TELEPHONE",
112 [PROP_LC_MEASUREMENT] = "LC_MEASUREMENT",
113 [PROP_LC_IDENTIFICATION] = "LC_IDENTIFICATION"
116 static char *data[_PROP_MAX] = {
132 static char *x11_layout = NULL, *x11_model = NULL, *x11_variant = NULL, *x11_options = NULL;
133 static char *vc_keymap = NULL, *vc_keymap_toggle = NULL;
135 static usec_t remain_until = 0;
137 static int free_and_set(char **s, const char *v) {
143 r = strdup_or_null(isempty(v) ? NULL : v, &t);
153 static void free_data_locale(void) {
156 for (p = 0; p < _PROP_MAX; p++) {
162 static void free_data_x11(void) {
168 x11_layout = x11_model = x11_variant = x11_options = NULL;
171 static void free_data_vconsole(void) {
173 free(vc_keymap_toggle);
175 vc_keymap = vc_keymap_toggle = NULL;
178 static void simplify(void) {
181 for (p = 1; p < _PROP_MAX; p++)
182 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
188 static int read_data_locale(void) {
193 r = parse_env_file("/etc/locale.conf", NEWLINE,
194 "LANG", &data[PROP_LANG],
195 "LANGUAGE", &data[PROP_LANGUAGE],
196 "LC_CTYPE", &data[PROP_LC_CTYPE],
197 "LC_NUMERIC", &data[PROP_LC_NUMERIC],
198 "LC_TIME", &data[PROP_LC_TIME],
199 "LC_COLLATE", &data[PROP_LC_COLLATE],
200 "LC_MONETARY", &data[PROP_LC_MONETARY],
201 "LC_MESSAGES", &data[PROP_LC_MESSAGES],
202 "LC_PAPER", &data[PROP_LC_PAPER],
203 "LC_NAME", &data[PROP_LC_NAME],
204 "LC_ADDRESS", &data[PROP_LC_ADDRESS],
205 "LC_TELEPHONE", &data[PROP_LC_TELEPHONE],
206 "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT],
207 "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
213 /* Fill in what we got passed from systemd. */
215 for (p = 0; p < _PROP_MAX; p++) {
220 e = getenv(names[p]);
239 static void free_data(void) {
241 free_data_vconsole();
245 static int read_data_vconsole(void) {
248 free_data_vconsole();
250 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
251 "KEYMAP", &vc_keymap,
252 "KEYMAP_TOGGLE", &vc_keymap_toggle,
255 if (r < 0 && r != -ENOENT)
261 static int read_data_x11(void) {
264 bool in_section = false;
268 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
270 if (errno == ENOENT) {
273 f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re");
288 while (fgets(line, sizeof(line), f)) {
294 if (l[0] == 0 || l[0] == '#')
297 if (in_section && first_word(l, "Option")) {
300 a = strv_split_quoted(l);
306 if (strv_length(a) == 3) {
308 if (streq(a[1], "XkbLayout")) {
312 } else if (streq(a[1], "XkbModel")) {
316 } else if (streq(a[1], "XkbVariant")) {
320 } else if (streq(a[1], "XkbOptions")) {
329 } else if (!in_section && first_word(l, "Section")) {
332 a = strv_split_quoted(l);
338 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
342 } else if (in_section && first_word(l, "EndSection"))
351 static int read_data(void) {
354 r = read_data_locale();
355 q = read_data_vconsole();
358 return r < 0 ? r : q < 0 ? q : p;
361 static int write_data_locale(void) {
365 r = load_env_file("/etc/locale.conf", &l);
366 if (r < 0 && r != -ENOENT)
369 for (p = 0; p < _PROP_MAX; p++) {
374 if (isempty(data[p])) {
375 l = strv_env_unset(l, names[p]);
379 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
384 u = strv_env_set(l, t);
394 if (strv_isempty(l)) {
397 if (unlink("/etc/locale.conf") < 0)
398 return errno == ENOENT ? 0 : -errno;
403 r = write_env_file("/etc/locale.conf", l);
409 static void push_data(DBusConnection *bus) {
410 char **l_set = NULL, **l_unset = NULL, **t;
411 int c_set = 0, c_unset = 0, p;
413 DBusMessage *m = NULL, *reply = NULL;
414 DBusMessageIter iter, sub;
416 dbus_error_init(&error);
420 l_set = new0(char*, _PROP_MAX);
421 l_unset = new0(char*, _PROP_MAX);
422 if (!l_set || !l_unset) {
423 log_error("Out of memory");
427 for (p = 0; p < _PROP_MAX; p++) {
430 if (isempty(data[p]))
431 l_unset[c_set++] = (char*) names[p];
435 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
436 log_error("Out of memory");
440 l_set[c_unset++] = s;
444 assert(c_set + c_unset == _PROP_MAX);
445 m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
447 log_error("Could not allocate message.");
451 dbus_message_iter_init_append(m, &iter);
453 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
454 log_error("Out of memory.");
458 STRV_FOREACH(t, l_unset)
459 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
460 log_error("Out of memory.");
464 if (!dbus_message_iter_close_container(&iter, &sub) ||
465 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
466 log_error("Out of memory.");
470 STRV_FOREACH(t, l_set)
471 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
472 log_error("Out of memory.");
476 if (!dbus_message_iter_close_container(&iter, &sub)) {
477 log_error("Out of memory.");
481 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
483 log_error("Failed to set locale information: %s", bus_error_message(&error));
489 dbus_message_unref(m);
492 dbus_message_unref(reply);
494 dbus_error_free(&error);
500 static int write_data_vconsole(void) {
504 r = load_env_file("/etc/vconsole.conf", &l);
505 if (r < 0 && r != -ENOENT)
508 if (isempty(vc_keymap))
509 l = strv_env_unset(l, "KEYMAP");
513 s = strappend("KEYMAP=", vc_keymap);
519 u = strv_env_set(l, s);
529 if (isempty(vc_keymap_toggle))
530 l = strv_env_unset(l, "KEYMAP_TOGGLE");
534 s = strappend("KEYMAP_TOGGLE=", vc_keymap_toggle);
540 u = strv_env_set(l, s);
550 if (strv_isempty(l)) {
553 if (unlink("/etc/vconsole.conf") < 0)
554 return errno == ENOENT ? 0 : -errno;
559 r = write_env_file("/etc/vconsole.conf", l);
565 static int write_data_x11(void) {
570 if (isempty(x11_layout) &&
571 isempty(x11_model) &&
572 isempty(x11_variant) &&
573 isempty(x11_options)) {
576 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
578 /* Symlink this to /dev/null, so that s-s-k (if it is
579 * still running) doesn't recreate this. */
580 symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
583 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
584 return errno == ENOENT ? 0 : -errno;
589 mkdir_parents("/etc/X11/xorg.conf.d", 0755);
591 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
595 fchmod(fileno(f), 0644);
597 fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n"
598 "# manually too freely.\n"
599 "Section \"InputClass\"\n"
600 " Identifier \"system-keyboard\"\n"
601 " MatchIsKeyboard \"on\"\n", f);
603 if (!isempty(x11_layout))
604 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", x11_layout);
606 if (!isempty(x11_model))
607 fprintf(f, " Option \"XkbModel\" \"%s\"\n", x11_model);
609 if (!isempty(x11_variant))
610 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", x11_variant);
612 if (!isempty(x11_options))
613 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", x11_options);
615 fputs("EndSection\n", f);
618 if (ferror(f) || rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
620 unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
625 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
627 /* Symlink this to /dev/null, so that s-s-k (if it is
628 * still running) doesn't recreate this. */
629 symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
641 static int load_vconsole_keymap(DBusConnection *bus, DBusError *error) {
642 DBusMessage *m = NULL, *reply = NULL;
643 const char *name = "systemd-vconsole-setup.service", *mode = "replace";
650 dbus_error_init(&_error);
654 m = dbus_message_new_method_call(
655 "org.freedesktop.systemd1",
656 "/org/freedesktop/systemd1",
657 "org.freedesktop.systemd1.Manager",
660 log_error("Could not allocate message.");
665 if (!dbus_message_append_args(m,
666 DBUS_TYPE_STRING, &name,
667 DBUS_TYPE_STRING, &mode,
668 DBUS_TYPE_INVALID)) {
669 log_error("Could not append arguments to message.");
674 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
676 log_error("Failed to issue method call: %s", bus_error_message(error));
685 dbus_message_unref(m);
688 dbus_message_unref(reply);
690 if (error == &_error)
691 dbus_error_free(error);
696 static char *strnulldash(const char *s) {
697 return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s;
700 static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
710 if (!fgets(line, sizeof(line), f)) {
713 return errno ? -errno : -EIO;
721 if (l[0] == 0 || l[0] == '#')
724 b = strv_split_quoted(l);
728 if (strv_length(b) < 5) {
729 log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n);
740 static int convert_vconsole_to_x11(DBusConnection *connection) {
741 bool modified = false;
745 if (isempty(vc_keymap)) {
748 !isempty(x11_layout) ||
749 !isempty(x11_model) ||
750 !isempty(x11_variant) ||
751 !isempty(x11_options);
758 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
766 r = read_next_mapping(f, &n, &a);
775 if (!streq(vc_keymap, a[0])) {
780 if (!streq_ptr(x11_layout, strnulldash(a[1])) ||
781 !streq_ptr(x11_model, strnulldash(a[2])) ||
782 !streq_ptr(x11_variant, strnulldash(a[3])) ||
783 !streq_ptr(x11_options, strnulldash(a[4]))) {
785 if (free_and_set(&x11_layout, strnulldash(a[1])) < 0 ||
786 free_and_set(&x11_model, strnulldash(a[2])) < 0 ||
787 free_and_set(&x11_variant, strnulldash(a[3])) < 0 ||
788 free_and_set(&x11_options, strnulldash(a[4])) < 0) {
806 DBusMessage *changed;
809 r = write_data_x11();
811 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
813 changed = bus_properties_changed_new(
814 "/org/freedesktop/locale1",
815 "org.freedesktop.locale1",
824 b = dbus_connection_send(connection, changed, NULL);
825 dbus_message_unref(changed);
834 static int convert_x11_to_vconsole(DBusConnection *connection) {
835 bool modified = false;
839 if (isempty(x11_layout)) {
842 !isempty(vc_keymap) ||
843 !isempty(vc_keymap_toggle);
849 unsigned best_matching = 0;
850 char *new_keymap = NULL;
852 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
858 unsigned matching = 0;
861 r = read_next_mapping(f, &n, &a);
870 /* Determine how well matching this entry is */
871 if (streq_ptr(x11_layout, a[1]))
872 /* If we got an exact match, this is best */
877 x = strcspn(x11_layout, ",");
879 /* We have multiple X layouts, look
880 * for an entry that matches our key
881 * with the everything but the first
882 * layout stripped off. */
885 strncmp(x11_layout, a[1], x) == 0)
890 /* If that didn't work, strip
891 * off the other layouts from
894 w = strcspn(a[1], ",");
896 if (x > 0 && x == w &&
897 memcmp(x11_layout, a[1], x) == 0)
903 streq_ptr(x11_model, a[2])) {
906 if (streq_ptr(x11_variant, a[3])) {
909 if (streq_ptr(x11_options, a[4]))
914 /* The best matching entry so far, then let's
916 if (matching > best_matching) {
917 best_matching = matching;
920 new_keymap = strdup(a[0]);
934 if (!streq_ptr(vc_keymap, new_keymap)) {
936 vc_keymap = new_keymap;
938 free(vc_keymap_toggle);
939 vc_keymap_toggle = NULL;
948 DBusMessage *changed;
951 r = write_data_vconsole();
953 log_error("Failed to set virtual console keymap: %s", strerror(-r));
955 changed = bus_properties_changed_new(
956 "/org/freedesktop/locale1",
957 "org.freedesktop.locale1",
959 "VConsoleKeymapToggle\0");
964 b = dbus_connection_send(connection, changed, NULL);
965 dbus_message_unref(changed);
970 return load_vconsole_keymap(connection, NULL);
976 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
980 l = new0(char*, _PROP_MAX+1);
984 for (p = 0; p < _PROP_MAX; p++) {
987 if (isempty(data[p]))
990 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
998 r = bus_property_append_strv(i, property, (void*) l);
1004 static DBusHandlerResult locale_message_handler(
1005 DBusConnection *connection,
1006 DBusMessage *message,
1009 const BusProperty properties[] = {
1010 { "org.freedesktop.locale1", "Locale", append_locale, "as", NULL },
1011 { "org.freedesktop.locale1", "X11Layout", bus_property_append_string, "s", x11_layout },
1012 { "org.freedesktop.locale1", "X11Model", bus_property_append_string, "s", x11_model },
1013 { "org.freedesktop.locale1", "X11Variant", bus_property_append_string, "s", x11_variant },
1014 { "org.freedesktop.locale1", "X11Options", bus_property_append_string, "s", x11_options },
1015 { "org.freedesktop.locale1", "VConsoleKeymap", bus_property_append_string, "s", vc_keymap },
1016 { "org.freedesktop.locale1", "VConsoleKeymapToggle", bus_property_append_string, "s", vc_keymap_toggle },
1017 { NULL, NULL, NULL, NULL, NULL }
1020 DBusMessage *reply = NULL, *changed = NULL;
1027 dbus_error_init(&error);
1029 if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
1030 char **l = NULL, **i;
1031 dbus_bool_t interactive;
1032 DBusMessageIter iter;
1033 bool modified = false;
1034 bool passed[_PROP_MAX];
1037 if (!dbus_message_iter_init(message, &iter))
1038 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1040 r = bus_parse_strv_iter(&iter, &l);
1045 return bus_send_error_reply(connection, message, NULL, r);
1048 if (!dbus_message_iter_next(&iter) ||
1049 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
1051 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1054 dbus_message_iter_get_basic(&iter, &interactive);
1058 /* Check whether a variable changed and if so valid */
1059 STRV_FOREACH(i, l) {
1062 for (p = 0; p < _PROP_MAX; p++) {
1065 k = strlen(names[p]);
1066 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1070 if (!streq_ptr(*i + k + 1, data[p]))
1079 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1083 /* Check whether a variable is unset */
1085 for (p = 0; p < _PROP_MAX; p++)
1086 if (!isempty(data[p]) && !passed[p]) {
1094 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
1097 return bus_send_error_reply(connection, message, &error, r);
1100 STRV_FOREACH(i, l) {
1101 for (p = 0; p < _PROP_MAX; p++) {
1104 k = strlen(names[p]);
1105 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1108 t = strdup(*i + k + 1);
1124 for (p = 0; p < _PROP_MAX; p++) {
1134 r = write_data_locale();
1136 log_error("Failed to set locale: %s", strerror(-r));
1137 return bus_send_error_reply(connection, message, NULL, r);
1140 push_data(connection);
1142 log_info("Changed locale information.");
1144 changed = bus_properties_changed_new(
1145 "/org/freedesktop/locale1",
1146 "org.freedesktop.locale1",
1151 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) {
1153 const char *keymap, *keymap_toggle;
1154 dbus_bool_t convert, interactive;
1156 if (!dbus_message_get_args(
1159 DBUS_TYPE_STRING, &keymap,
1160 DBUS_TYPE_STRING, &keymap_toggle,
1161 DBUS_TYPE_BOOLEAN, &convert,
1162 DBUS_TYPE_BOOLEAN, &interactive,
1164 return bus_send_error_reply(connection, message, &error, -EINVAL);
1166 if (isempty(keymap))
1169 if (isempty(keymap_toggle))
1170 keymap_toggle = NULL;
1172 if (!streq_ptr(keymap, vc_keymap) ||
1173 !streq_ptr(keymap_toggle, vc_keymap_toggle)) {
1175 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1177 return bus_send_error_reply(connection, message, &error, r);
1179 if (free_and_set(&vc_keymap, keymap) < 0 ||
1180 free_and_set(&vc_keymap_toggle, keymap_toggle) < 0)
1183 r = write_data_vconsole();
1185 log_error("Failed to set virtual console keymap: %s", strerror(-r));
1186 return bus_send_error_reply(connection, message, NULL, r);
1189 log_info("Changed virtual console keymap to '%s'", strempty(vc_keymap));
1191 r = load_vconsole_keymap(connection, NULL);
1193 log_error("Failed to request keymap reload: %s", strerror(-r));
1195 changed = bus_properties_changed_new(
1196 "/org/freedesktop/locale1",
1197 "org.freedesktop.locale1",
1199 "VConsoleKeymapToggle\0");
1204 r = convert_vconsole_to_x11(connection);
1207 log_error("Failed to convert keymap data: %s", strerror(-r));
1211 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetX11Keyboard")) {
1213 const char *layout, *model, *variant, *options;
1214 dbus_bool_t convert, interactive;
1216 if (!dbus_message_get_args(
1219 DBUS_TYPE_STRING, &layout,
1220 DBUS_TYPE_STRING, &model,
1221 DBUS_TYPE_STRING, &variant,
1222 DBUS_TYPE_STRING, &options,
1223 DBUS_TYPE_BOOLEAN, &convert,
1224 DBUS_TYPE_BOOLEAN, &interactive,
1226 return bus_send_error_reply(connection, message, &error, -EINVAL);
1228 if (isempty(layout))
1234 if (isempty(variant))
1237 if (isempty(options))
1240 if (!streq_ptr(layout, x11_layout) ||
1241 !streq_ptr(model, x11_model) ||
1242 !streq_ptr(variant, x11_variant) ||
1243 !streq_ptr(options, x11_options)) {
1245 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1247 return bus_send_error_reply(connection, message, &error, r);
1249 if (free_and_set(&x11_layout, layout) < 0 ||
1250 free_and_set(&x11_model, model) < 0 ||
1251 free_and_set(&x11_variant, variant) < 0 ||
1252 free_and_set(&x11_options, options) < 0)
1255 r = write_data_x11();
1257 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
1258 return bus_send_error_reply(connection, message, NULL, r);
1261 log_info("Changed X11 keyboard layout to '%s'", strempty(x11_layout));
1263 changed = bus_properties_changed_new(
1264 "/org/freedesktop/locale1",
1265 "org.freedesktop.locale1",
1274 r = convert_x11_to_vconsole(connection);
1277 log_error("Failed to convert keymap data: %s", strerror(-r));
1281 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
1284 if (!(reply = dbus_message_new_method_return(message)))
1287 if (!dbus_connection_send(connection, reply, NULL))
1290 dbus_message_unref(reply);
1295 if (!dbus_connection_send(connection, changed, NULL))
1298 dbus_message_unref(changed);
1301 return DBUS_HANDLER_RESULT_HANDLED;
1305 dbus_message_unref(reply);
1308 dbus_message_unref(changed);
1310 dbus_error_free(&error);
1312 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1315 static int connect_bus(DBusConnection **_bus) {
1316 static const DBusObjectPathVTable locale_vtable = {
1317 .message_function = locale_message_handler
1320 DBusConnection *bus = NULL;
1325 dbus_error_init(&error);
1327 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1329 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
1334 dbus_connection_set_exit_on_disconnect(bus, FALSE);
1336 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) ||
1337 !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
1338 log_error("Not enough memory");
1343 r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
1344 if (dbus_error_is_set(&error)) {
1345 log_error("Failed to register name on bus: %s", bus_error_message(&error));
1350 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1351 log_error("Failed to acquire name.");
1362 dbus_connection_close(bus);
1363 dbus_connection_unref(bus);
1365 dbus_error_free(&error);
1370 int main(int argc, char *argv[]) {
1372 DBusConnection *bus = NULL;
1373 bool exiting = false;
1375 log_set_target(LOG_TARGET_AUTO);
1376 log_parse_environment();
1381 if (argc == 2 && streq(argv[1], "--introspect")) {
1382 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
1383 "<node>\n", stdout);
1384 fputs(locale_interface, stdout);
1385 fputs("</node>\n", stdout);
1390 log_error("This program takes no arguments.");
1397 log_error("Failed to read locale data: %s", strerror(-r));
1401 r = connect_bus(&bus);
1405 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1408 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
1411 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
1413 bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
1423 dbus_connection_flush(bus);
1424 dbus_connection_close(bus);
1425 dbus_connection_unref(bus);
1428 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;