2 * Copyright © 2013 Ran Benita
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
29 xkb_x11_setup_xkb_extension(xcb_connection_t *conn,
30 uint16_t major_xkb_version,
31 uint16_t minor_xkb_version,
32 enum xkb_x11_setup_xkb_extension_flags flags,
33 uint16_t *major_xkb_version_out,
34 uint16_t *minor_xkb_version_out,
35 uint8_t *base_event_out,
36 uint8_t *base_error_out)
38 uint8_t base_event, base_error;
39 uint16_t server_major, server_minor;
41 if (flags & ~(XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS)) {
42 /* log_err_func(ctx, "unrecognized flags: %#x\n", flags); */
47 const xcb_query_extension_reply_t *reply =
48 xcb_get_extension_data(conn, &xcb_xkb_id);
50 /* log_err_func(ctx, "failed to query for XKB extension\n"); */
54 if (!reply->present) {
55 /* log_err_func(ctx, "failed to start using XKB extension: not available in server\n"); */
59 base_event = reply->first_event;
60 base_error = reply->first_error;
64 xcb_generic_error_t *error = NULL;
65 xcb_xkb_use_extension_cookie_t cookie =
66 xcb_xkb_use_extension(conn, major_xkb_version, minor_xkb_version);
67 xcb_xkb_use_extension_reply_t *reply =
68 xcb_xkb_use_extension_reply(conn, cookie, &error);
71 /* log_err_func(ctx, */
72 /* "failed to start using XKB extension: error code %d\n", */
73 /* error ? error->error_code : -1); */
78 if (!reply->supported) {
79 /* log_err_func(ctx, */
80 /* "failed to start using XKB extension: server doesn't support version %d.%d\n", */
81 /* major_xkb_version, minor_xkb_version); */
86 server_major = reply->serverMajor;
87 server_minor = reply->serverMinor;
93 * The XkbUseExtension() in libX11 has a *bunch* of legacy stuff, but
94 * it doesn't seem like any of it is useful to us.
97 if (major_xkb_version_out)
98 *major_xkb_version_out = server_major;
99 if (minor_xkb_version_out)
100 *minor_xkb_version_out = server_minor;
102 *base_event_out = base_event;
104 *base_error_out = base_error;
110 xkb_x11_get_core_keyboard_device_id(xcb_connection_t *conn)
113 xcb_xkb_get_device_info_cookie_t cookie =
114 xcb_xkb_get_device_info(conn, XCB_XKB_ID_USE_CORE_KBD,
116 xcb_xkb_get_device_info_reply_t *reply =
117 xcb_xkb_get_device_info_reply(conn, cookie, NULL);
122 device_id = reply->deviceID;
128 get_atom_name(xcb_connection_t *conn, xcb_atom_t atom, char **out)
130 xcb_get_atom_name_cookie_t cookie;
131 xcb_get_atom_name_reply_t *reply;
140 cookie = xcb_get_atom_name(conn, atom);
141 reply = xcb_get_atom_name_reply(conn, cookie, NULL);
145 length = xcb_get_atom_name_name_length(reply);
146 name = xcb_get_atom_name_name(reply);
148 *out = strndup(name, length);
159 adopt_atoms(struct xkb_context *ctx, xcb_connection_t *conn,
160 const xcb_atom_t *from, xkb_atom_t *to, const size_t count)
163 xcb_get_atom_name_cookie_t cookies[SIZE];
164 const size_t num_batches = ROUNDUP(count, SIZE) / SIZE;
166 /* Send and collect the atoms in batches of reasonable SIZE. */
167 for (size_t batch = 0; batch < num_batches; batch++) {
168 const size_t start = batch * SIZE;
169 const size_t stop = MIN((batch + 1) * SIZE, count);
172 for (size_t i = start; i < stop; i++)
173 if (from[i] != XCB_ATOM_NONE)
174 cookies[i % SIZE] = xcb_get_atom_name(conn, from[i]);
177 for (size_t i = start; i < stop; i++) {
178 xcb_get_atom_name_reply_t *reply;
180 if (from[i] == XCB_ATOM_NONE) {
181 to[i] = XKB_ATOM_NONE;
185 reply = xcb_get_atom_name_reply(conn, cookies[i % SIZE], NULL);
189 to[i] = xkb_atom_intern(ctx,
190 xcb_get_atom_name_name(reply),
191 xcb_get_atom_name_name_length(reply));
194 if (to[i] == XKB_ATOM_NONE)
200 * If we don't discard the uncollected replies, they just
201 * sit in the XCB queue waiting forever. Sad.
204 for (size_t j = i + 1; j < stop; j++)
205 if (from[j] != XCB_ATOM_NONE)
206 xcb_discard_reply(conn, cookies[j % SIZE].sequence);
215 adopt_atom(struct xkb_context *ctx, xcb_connection_t *conn, xcb_atom_t atom,
218 return adopt_atoms(ctx, conn, &atom, out, 1);