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 typedef struct State {
133 char *x11_layout, *x11_model, *x11_variant, *x11_options;
134 char *vc_keymap, *vc_keymap_toggle;
139 static usec_t remain_until = 0;
141 static int free_and_set(char **s, const char *v) {
147 r = strdup_or_null(isempty(v) ? NULL : v, &t);
157 static void free_data_locale(void) {
160 for (p = 0; p < _PROP_MAX; p++) {
166 static void free_data_x11(void) {
167 free(state.x11_layout);
168 free(state.x11_model);
169 free(state.x11_variant);
170 free(state.x11_options);
172 state.x11_layout = state.x11_model = state.x11_variant = state.x11_options = NULL;
175 static void free_data_vconsole(void) {
176 free(state.vc_keymap);
177 free(state.vc_keymap_toggle);
179 state.vc_keymap = state.vc_keymap_toggle = NULL;
182 static void simplify(void) {
185 for (p = 1; p < _PROP_MAX; p++)
186 if (isempty(data[p]) || streq_ptr(data[PROP_LANG], data[p])) {
192 static int read_data_locale(void) {
197 r = parse_env_file("/etc/locale.conf", NEWLINE,
198 "LANG", &data[PROP_LANG],
199 "LANGUAGE", &data[PROP_LANGUAGE],
200 "LC_CTYPE", &data[PROP_LC_CTYPE],
201 "LC_NUMERIC", &data[PROP_LC_NUMERIC],
202 "LC_TIME", &data[PROP_LC_TIME],
203 "LC_COLLATE", &data[PROP_LC_COLLATE],
204 "LC_MONETARY", &data[PROP_LC_MONETARY],
205 "LC_MESSAGES", &data[PROP_LC_MESSAGES],
206 "LC_PAPER", &data[PROP_LC_PAPER],
207 "LC_NAME", &data[PROP_LC_NAME],
208 "LC_ADDRESS", &data[PROP_LC_ADDRESS],
209 "LC_TELEPHONE", &data[PROP_LC_TELEPHONE],
210 "LC_MEASUREMENT", &data[PROP_LC_MEASUREMENT],
211 "LC_IDENTIFICATION", &data[PROP_LC_IDENTIFICATION],
217 /* Fill in what we got passed from systemd. */
219 for (p = 0; p < _PROP_MAX; p++) {
224 e = getenv(names[p]);
243 static void free_data(void) {
245 free_data_vconsole();
249 static int read_data_vconsole(void) {
252 free_data_vconsole();
254 r = parse_env_file("/etc/vconsole.conf", NEWLINE,
255 "KEYMAP", &state.vc_keymap,
256 "KEYMAP_TOGGLE", &state.vc_keymap_toggle,
259 if (r < 0 && r != -ENOENT)
265 static int read_data_x11(void) {
268 bool in_section = false;
272 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
274 if (errno == ENOENT) {
277 f = fopen("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf", "re");
292 while (fgets(line, sizeof(line), f)) {
298 if (l[0] == 0 || l[0] == '#')
301 if (in_section && first_word(l, "Option")) {
304 a = strv_split_quoted(l);
310 if (strv_length(a) == 3) {
312 if (streq(a[1], "XkbLayout")) {
313 free(state.x11_layout);
314 state.x11_layout = a[2];
316 } else if (streq(a[1], "XkbModel")) {
317 free(state.x11_model);
318 state.x11_model = a[2];
320 } else if (streq(a[1], "XkbVariant")) {
321 free(state.x11_variant);
322 state.x11_variant = a[2];
324 } else if (streq(a[1], "XkbOptions")) {
325 free(state.x11_options);
326 state.x11_options = a[2];
333 } else if (!in_section && first_word(l, "Section")) {
336 a = strv_split_quoted(l);
342 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
346 } else if (in_section && first_word(l, "EndSection"))
355 static int read_data(void) {
358 r = read_data_locale();
359 q = read_data_vconsole();
362 return r < 0 ? r : q < 0 ? q : p;
365 static int write_data_locale(void) {
369 r = load_env_file("/etc/locale.conf", &l);
370 if (r < 0 && r != -ENOENT)
373 for (p = 0; p < _PROP_MAX; p++) {
378 if (isempty(data[p])) {
379 l = strv_env_unset(l, names[p]);
383 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
388 u = strv_env_set(l, t);
398 if (strv_isempty(l)) {
401 if (unlink("/etc/locale.conf") < 0)
402 return errno == ENOENT ? 0 : -errno;
407 r = write_env_file("/etc/locale.conf", l);
413 static void push_data(DBusConnection *bus) {
414 char **l_set = NULL, **l_unset = NULL, **t;
415 int c_set = 0, c_unset = 0, p;
417 DBusMessage *m = NULL, *reply = NULL;
418 DBusMessageIter iter, sub;
420 dbus_error_init(&error);
424 l_set = new0(char*, _PROP_MAX);
425 l_unset = new0(char*, _PROP_MAX);
426 if (!l_set || !l_unset) {
427 log_error("Out of memory");
431 for (p = 0; p < _PROP_MAX; p++) {
434 if (isempty(data[p]))
435 l_unset[c_set++] = (char*) names[p];
439 if (asprintf(&s, "%s=%s", names[p], data[p]) < 0) {
440 log_error("Out of memory");
444 l_set[c_unset++] = s;
448 assert(c_set + c_unset == _PROP_MAX);
449 m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment");
451 log_error("Could not allocate message.");
455 dbus_message_iter_init_append(m, &iter);
457 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
458 log_error("Out of memory.");
462 STRV_FOREACH(t, l_unset)
463 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
464 log_error("Out of memory.");
468 if (!dbus_message_iter_close_container(&iter, &sub) ||
469 !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
470 log_error("Out of memory.");
474 STRV_FOREACH(t, l_set)
475 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) {
476 log_error("Out of memory.");
480 if (!dbus_message_iter_close_container(&iter, &sub)) {
481 log_error("Out of memory.");
485 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
487 log_error("Failed to set locale information: %s", bus_error_message(&error));
493 dbus_message_unref(m);
496 dbus_message_unref(reply);
498 dbus_error_free(&error);
504 static int write_data_vconsole(void) {
508 r = load_env_file("/etc/vconsole.conf", &l);
509 if (r < 0 && r != -ENOENT)
512 if (isempty(state.vc_keymap))
513 l = strv_env_unset(l, "KEYMAP");
517 s = strappend("KEYMAP=", state.vc_keymap);
523 u = strv_env_set(l, s);
533 if (isempty(state.vc_keymap_toggle))
534 l = strv_env_unset(l, "KEYMAP_TOGGLE");
538 s = strappend("KEYMAP_TOGGLE=", state.vc_keymap_toggle);
544 u = strv_env_set(l, s);
554 if (strv_isempty(l)) {
557 if (unlink("/etc/vconsole.conf") < 0)
558 return errno == ENOENT ? 0 : -errno;
563 r = write_env_file("/etc/vconsole.conf", l);
569 static int write_data_x11(void) {
574 if (isempty(state.x11_layout) &&
575 isempty(state.x11_model) &&
576 isempty(state.x11_variant) &&
577 isempty(state.x11_options)) {
580 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
582 /* Symlink this to /dev/null, so that s-s-k (if it is
583 * still running) doesn't recreate this. */
584 symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
587 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
588 return errno == ENOENT ? 0 : -errno;
593 mkdir_parents("/etc/X11/xorg.conf.d", 0755);
595 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
599 fchmod(fileno(f), 0644);
601 fputs("# Read and parsed by systemd-localed. It's probably wise not to edit this file\n"
602 "# manually too freely.\n"
603 "Section \"InputClass\"\n"
604 " Identifier \"system-keyboard\"\n"
605 " MatchIsKeyboard \"on\"\n", f);
607 if (!isempty(state.x11_layout))
608 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", state.x11_layout);
610 if (!isempty(state.x11_model))
611 fprintf(f, " Option \"XkbModel\" \"%s\"\n", state.x11_model);
613 if (!isempty(state.x11_variant))
614 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", state.x11_variant);
616 if (!isempty(state.x11_options))
617 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", state.x11_options);
619 fputs("EndSection\n", f);
622 if (ferror(f) || rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
624 unlink("/etc/X11/xorg.conf.d/00-keyboard.conf");
629 unlink("/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
631 /* Symlink this to /dev/null, so that s-s-k (if it is
632 * still running) doesn't recreate this. */
633 symlink("/dev/null", "/etc/X11/xorg.conf.d/00-system-setup-keyboard.conf");
645 static int load_vconsole_keymap(DBusConnection *bus, DBusError *error) {
646 DBusMessage *m = NULL, *reply = NULL;
647 const char *name = "systemd-vconsole-setup.service", *mode = "replace";
654 dbus_error_init(&_error);
658 m = dbus_message_new_method_call(
659 "org.freedesktop.systemd1",
660 "/org/freedesktop/systemd1",
661 "org.freedesktop.systemd1.Manager",
664 log_error("Could not allocate message.");
669 if (!dbus_message_append_args(m,
670 DBUS_TYPE_STRING, &name,
671 DBUS_TYPE_STRING, &mode,
672 DBUS_TYPE_INVALID)) {
673 log_error("Could not append arguments to message.");
678 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
680 log_error("Failed to issue method call: %s", bus_error_message(error));
689 dbus_message_unref(m);
692 dbus_message_unref(reply);
694 if (error == &_error)
695 dbus_error_free(error);
700 static char *strnulldash(const char *s) {
701 return s == NULL || *s == 0 || (s[0] == '-' && s[1] == 0) ? NULL : (char*) s;
704 static int read_next_mapping(FILE *f, unsigned *n, char ***a) {
714 if (!fgets(line, sizeof(line), f)) {
717 return errno ? -errno : -EIO;
725 if (l[0] == 0 || l[0] == '#')
728 b = strv_split_quoted(l);
732 if (strv_length(b) < 5) {
733 log_error("Invalid line "SYSTEMD_KBD_MODEL_MAP":%u, ignoring.", *n);
744 static int convert_vconsole_to_x11(DBusConnection *connection) {
745 bool modified = false;
749 if (isempty(state.vc_keymap)) {
752 !isempty(state.x11_layout) ||
753 !isempty(state.x11_model) ||
754 !isempty(state.x11_variant) ||
755 !isempty(state.x11_options);
762 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
770 r = read_next_mapping(f, &n, &a);
779 if (!streq(state.vc_keymap, a[0])) {
784 if (!streq_ptr(state.x11_layout, strnulldash(a[1])) ||
785 !streq_ptr(state.x11_model, strnulldash(a[2])) ||
786 !streq_ptr(state.x11_variant, strnulldash(a[3])) ||
787 !streq_ptr(state.x11_options, strnulldash(a[4]))) {
789 if (free_and_set(&state.x11_layout, strnulldash(a[1])) < 0 ||
790 free_and_set(&state.x11_model, strnulldash(a[2])) < 0 ||
791 free_and_set(&state.x11_variant, strnulldash(a[3])) < 0 ||
792 free_and_set(&state.x11_options, strnulldash(a[4])) < 0) {
810 DBusMessage *changed;
813 r = write_data_x11();
815 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
817 changed = bus_properties_changed_new(
818 "/org/freedesktop/locale1",
819 "org.freedesktop.locale1",
828 b = dbus_connection_send(connection, changed, NULL);
829 dbus_message_unref(changed);
838 static int convert_x11_to_vconsole(DBusConnection *connection) {
839 bool modified = false;
843 if (isempty(state.x11_layout)) {
846 !isempty(state.vc_keymap) ||
847 !isempty(state.vc_keymap_toggle);
853 unsigned best_matching = 0;
854 char *new_keymap = NULL;
856 f = fopen(SYSTEMD_KBD_MODEL_MAP, "re");
862 unsigned matching = 0;
865 r = read_next_mapping(f, &n, &a);
874 /* Determine how well matching this entry is */
875 if (streq_ptr(state.x11_layout, a[1]))
876 /* If we got an exact match, this is best */
881 x = strcspn(state.x11_layout, ",");
883 /* We have multiple X layouts, look
884 * for an entry that matches our key
885 * with the everything but the first
886 * layout stripped off. */
889 strncmp(state.x11_layout, a[1], x) == 0)
894 /* If that didn't work, strip
895 * off the other layouts from
898 w = strcspn(a[1], ",");
900 if (x > 0 && x == w &&
901 memcmp(state.x11_layout, a[1], x) == 0)
907 streq_ptr(state.x11_model, a[2])) {
910 if (streq_ptr(state.x11_variant, a[3])) {
913 if (streq_ptr(state.x11_options, a[4]))
918 /* The best matching entry so far, then let's
920 if (matching > best_matching) {
921 best_matching = matching;
924 new_keymap = strdup(a[0]);
938 if (!streq_ptr(state.vc_keymap, new_keymap)) {
939 free(state.vc_keymap);
940 state.vc_keymap = new_keymap;
942 free(state.vc_keymap_toggle);
943 state.vc_keymap_toggle = NULL;
952 DBusMessage *changed;
955 r = write_data_vconsole();
957 log_error("Failed to set virtual console keymap: %s", strerror(-r));
959 changed = bus_properties_changed_new(
960 "/org/freedesktop/locale1",
961 "org.freedesktop.locale1",
963 "VConsoleKeymapToggle\0");
968 b = dbus_connection_send(connection, changed, NULL);
969 dbus_message_unref(changed);
974 return load_vconsole_keymap(connection, NULL);
980 static int append_locale(DBusMessageIter *i, const char *property, void *userdata) {
984 l = new0(char*, _PROP_MAX+1);
988 for (p = 0; p < _PROP_MAX; p++) {
991 if (isempty(data[p]))
994 if (asprintf(&t, "%s=%s", names[p], data[p]) < 0) {
1002 r = bus_property_append_strv(i, property, (void*) l);
1008 static const BusProperty bus_locale_properties[] = {
1009 { "Locale", append_locale, "as", 0 },
1010 { "X11Layout", bus_property_append_string, "s", offsetof(State, x11_layout), true },
1011 { "X11Model", bus_property_append_string, "s", offsetof(State, x11_model), true },
1012 { "X11Variant", bus_property_append_string, "s", offsetof(State, x11_variant), true },
1013 { "X11Options", bus_property_append_string, "s", offsetof(State, x11_options), true },
1014 { "VConsoleKeymap", bus_property_append_string, "s", offsetof(State, vc_keymap), true },
1015 { "VConsoleKeymapToggle", bus_property_append_string, "s", offsetof(State, vc_keymap_toggle), true },
1019 static const BusBoundProperties bps[] = {
1020 { "org.freedesktop.locale1", bus_locale_properties, &state },
1024 static DBusHandlerResult locale_message_handler(
1025 DBusConnection *connection,
1026 DBusMessage *message,
1029 DBusMessage *reply = NULL, *changed = NULL;
1036 dbus_error_init(&error);
1038 if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetLocale")) {
1039 char **l = NULL, **i;
1040 dbus_bool_t interactive;
1041 DBusMessageIter iter;
1042 bool modified = false;
1043 bool passed[_PROP_MAX];
1046 if (!dbus_message_iter_init(message, &iter))
1047 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1049 r = bus_parse_strv_iter(&iter, &l);
1054 return bus_send_error_reply(connection, message, NULL, r);
1057 if (!dbus_message_iter_next(&iter) ||
1058 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
1060 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1063 dbus_message_iter_get_basic(&iter, &interactive);
1067 /* Check whether a variable changed and if so valid */
1068 STRV_FOREACH(i, l) {
1071 for (p = 0; p < _PROP_MAX; p++) {
1074 k = strlen(names[p]);
1075 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1079 if (!streq_ptr(*i + k + 1, data[p]))
1088 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1092 /* Check whether a variable is unset */
1094 for (p = 0; p < _PROP_MAX; p++)
1095 if (!isempty(data[p]) && !passed[p]) {
1103 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-locale", interactive, &error);
1106 return bus_send_error_reply(connection, message, &error, r);
1109 STRV_FOREACH(i, l) {
1110 for (p = 0; p < _PROP_MAX; p++) {
1113 k = strlen(names[p]);
1114 if (startswith(*i, names[p]) && (*i)[k] == '=') {
1117 t = strdup(*i + k + 1);
1133 for (p = 0; p < _PROP_MAX; p++) {
1143 r = write_data_locale();
1145 log_error("Failed to set locale: %s", strerror(-r));
1146 return bus_send_error_reply(connection, message, NULL, r);
1149 push_data(connection);
1151 log_info("Changed locale information.");
1153 changed = bus_properties_changed_new(
1154 "/org/freedesktop/locale1",
1155 "org.freedesktop.locale1",
1160 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetVConsoleKeyboard")) {
1162 const char *keymap, *keymap_toggle;
1163 dbus_bool_t convert, interactive;
1165 if (!dbus_message_get_args(
1168 DBUS_TYPE_STRING, &keymap,
1169 DBUS_TYPE_STRING, &keymap_toggle,
1170 DBUS_TYPE_BOOLEAN, &convert,
1171 DBUS_TYPE_BOOLEAN, &interactive,
1173 return bus_send_error_reply(connection, message, &error, -EINVAL);
1175 if (isempty(keymap))
1178 if (isempty(keymap_toggle))
1179 keymap_toggle = NULL;
1181 if (!streq_ptr(keymap, state.vc_keymap) ||
1182 !streq_ptr(keymap_toggle, state.vc_keymap_toggle)) {
1184 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1186 return bus_send_error_reply(connection, message, &error, r);
1188 if (free_and_set(&state.vc_keymap, keymap) < 0 ||
1189 free_and_set(&state.vc_keymap_toggle, keymap_toggle) < 0)
1192 r = write_data_vconsole();
1194 log_error("Failed to set virtual console keymap: %s", strerror(-r));
1195 return bus_send_error_reply(connection, message, NULL, r);
1198 log_info("Changed virtual console keymap to '%s'", strempty(state.vc_keymap));
1200 r = load_vconsole_keymap(connection, NULL);
1202 log_error("Failed to request keymap reload: %s", strerror(-r));
1204 changed = bus_properties_changed_new(
1205 "/org/freedesktop/locale1",
1206 "org.freedesktop.locale1",
1208 "VConsoleKeymapToggle\0");
1213 r = convert_vconsole_to_x11(connection);
1216 log_error("Failed to convert keymap data: %s", strerror(-r));
1220 } else if (dbus_message_is_method_call(message, "org.freedesktop.locale1", "SetX11Keyboard")) {
1222 const char *layout, *model, *variant, *options;
1223 dbus_bool_t convert, interactive;
1225 if (!dbus_message_get_args(
1228 DBUS_TYPE_STRING, &layout,
1229 DBUS_TYPE_STRING, &model,
1230 DBUS_TYPE_STRING, &variant,
1231 DBUS_TYPE_STRING, &options,
1232 DBUS_TYPE_BOOLEAN, &convert,
1233 DBUS_TYPE_BOOLEAN, &interactive,
1235 return bus_send_error_reply(connection, message, &error, -EINVAL);
1237 if (isempty(layout))
1243 if (isempty(variant))
1246 if (isempty(options))
1249 if (!streq_ptr(layout, state.x11_layout) ||
1250 !streq_ptr(model, state.x11_model) ||
1251 !streq_ptr(variant, state.x11_variant) ||
1252 !streq_ptr(options, state.x11_options)) {
1254 r = verify_polkit(connection, message, "org.freedesktop.locale1.set-keyboard", interactive, &error);
1256 return bus_send_error_reply(connection, message, &error, r);
1258 if (free_and_set(&state.x11_layout, layout) < 0 ||
1259 free_and_set(&state.x11_model, model) < 0 ||
1260 free_and_set(&state.x11_variant, variant) < 0 ||
1261 free_and_set(&state.x11_options, options) < 0)
1264 r = write_data_x11();
1266 log_error("Failed to set X11 keyboard layout: %s", strerror(-r));
1267 return bus_send_error_reply(connection, message, NULL, r);
1270 log_info("Changed X11 keyboard layout to '%s'", strempty(state.x11_layout));
1272 changed = bus_properties_changed_new(
1273 "/org/freedesktop/locale1",
1274 "org.freedesktop.locale1",
1283 r = convert_x11_to_vconsole(connection);
1286 log_error("Failed to convert keymap data: %s", strerror(-r));
1290 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
1292 if (!(reply = dbus_message_new_method_return(message)))
1295 if (!dbus_connection_send(connection, reply, NULL))
1298 dbus_message_unref(reply);
1303 if (!dbus_connection_send(connection, changed, NULL))
1306 dbus_message_unref(changed);
1309 return DBUS_HANDLER_RESULT_HANDLED;
1313 dbus_message_unref(reply);
1316 dbus_message_unref(changed);
1318 dbus_error_free(&error);
1320 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1323 static int connect_bus(DBusConnection **_bus) {
1324 static const DBusObjectPathVTable locale_vtable = {
1325 .message_function = locale_message_handler
1328 DBusConnection *bus = NULL;
1333 dbus_error_init(&error);
1335 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
1337 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
1342 dbus_connection_set_exit_on_disconnect(bus, FALSE);
1344 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/locale1", &locale_vtable, NULL) ||
1345 !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
1346 log_error("Not enough memory");
1351 r = dbus_bus_request_name(bus, "org.freedesktop.locale1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
1352 if (dbus_error_is_set(&error)) {
1353 log_error("Failed to register name on bus: %s", bus_error_message(&error));
1358 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1359 log_error("Failed to acquire name.");
1370 dbus_connection_close(bus);
1371 dbus_connection_unref(bus);
1373 dbus_error_free(&error);
1378 int main(int argc, char *argv[]) {
1380 DBusConnection *bus = NULL;
1381 bool exiting = false;
1383 log_set_target(LOG_TARGET_AUTO);
1384 log_parse_environment();
1389 if (argc == 2 && streq(argv[1], "--introspect")) {
1390 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
1391 "<node>\n", stdout);
1392 fputs(locale_interface, stdout);
1393 fputs("</node>\n", stdout);
1398 log_error("This program takes no arguments.");
1405 log_error("Failed to read locale data: %s", strerror(-r));
1409 r = connect_bus(&bus);
1413 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
1416 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
1419 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
1421 bus_async_unregister_and_exit(bus, "org.freedesktop.locale1");
1431 dbus_connection_flush(bus);
1432 dbus_connection_close(bus);
1433 dbus_connection_unref(bus);
1436 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;