+static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
+ xcb_intern_atom_reply_t *atom_reply;
+ size_t content_max_words = 256;
+
+ xcb_window_t root = root_screen->root;
+
+ atom_reply = xcb_intern_atom_reply(
+ conn, xcb_intern_atom(conn, 0, strlen("_XKB_RULES_NAMES"), "_XKB_RULES_NAMES"), NULL);
+ if (atom_reply == NULL)
+ return -1;
+
+ xcb_get_property_cookie_t prop_cookie;
+ xcb_get_property_reply_t *prop_reply;
+ prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
+ XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
+ prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
+ if (prop_reply == NULL) {
+ free(atom_reply);
+ return -1;
+ }
+ if (xcb_get_property_value_length(prop_reply) > 0 && prop_reply->bytes_after > 0) {
+ /* We received an incomplete value. Ask again but with a properly
+ * adjusted size. */
+ content_max_words += ceil(prop_reply->bytes_after / 4.0);
+ /* Repeat the request, with adjusted size */
+ free(prop_reply);
+ prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
+ XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
+ prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
+ if (prop_reply == NULL) {
+ free(atom_reply);
+ return -1;
+ }
+ }
+ if (xcb_get_property_value_length(prop_reply) == 0) {
+ free(atom_reply);
+ free(prop_reply);
+ return -1;
+ }
+
+ const char *walk = (const char *)xcb_get_property_value(prop_reply);
+ int remaining = xcb_get_property_value_length(prop_reply);
+ for (int i = 0; i < 5 && remaining > 0; i++) {
+ const int len = strnlen(walk, remaining);
+ remaining -= len;
+ switch (i) {
+ case 0:
+ asprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
+ break;
+ case 1:
+ asprintf((char **)&(xkb_names->model), "%.*s", len, walk);
+ break;
+ case 2:
+ asprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
+ break;
+ case 3:
+ asprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
+ break;
+ case 4:
+ asprintf((char **)&(xkb_names->options), "%.*s", len, walk);
+ break;
+ }
+ DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
+ walk += (len + 1);
+ }
+
+ free(atom_reply);
+ free(prop_reply);
+ return 0;
+}
+