Save three more round trips in xkb_x11_keymap_new_from_device()
[platform/upstream/libxkbcommon.git] / src / x11 / util.c
1 /*
2  * Copyright © 2013 Ran Benita
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  */
23
24 #include "config.h"
25
26 #include "x11-priv.h"
27
28 XKB_EXPORT int
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)
37 {
38     uint8_t base_event, base_error;
39     uint16_t server_major, server_minor;
40
41     if (flags & ~(XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS)) {
42         /* log_err_func(ctx, "unrecognized flags: %#x\n", flags); */
43         return 0;
44     }
45
46     {
47         const xcb_query_extension_reply_t *reply =
48             xcb_get_extension_data(conn, &xcb_xkb_id);
49         if (!reply) {
50             /* log_err_func(ctx, "failed to query for XKB extension\n"); */
51             return 0;
52         }
53
54         if (!reply->present) {
55             /* log_err_func(ctx, "failed to start using XKB extension: not available in server\n"); */
56             return 0;
57         }
58
59         base_event = reply->first_event;
60         base_error = reply->first_error;
61     }
62
63     {
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);
69
70         if (!reply) {
71             /* log_err_func(ctx, */
72             /*              "failed to start using XKB extension: error code %d\n", */
73             /*              error ? error->error_code : -1); */
74             free(error);
75             return 0;
76         }
77
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); */
82             free(reply);
83             return 0;
84         }
85
86         server_major = reply->serverMajor;
87         server_minor = reply->serverMinor;
88
89         free(reply);
90     }
91
92     /*
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.
95     */
96
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;
101     if (base_event_out)
102         *base_event_out = base_event;
103     if (base_error_out)
104         *base_error_out = base_error;
105
106     return 1;
107 }
108
109 XKB_EXPORT int32_t
110 xkb_x11_get_core_keyboard_device_id(xcb_connection_t *conn)
111 {
112     int32_t device_id;
113     xcb_xkb_get_device_info_cookie_t cookie =
114         xcb_xkb_get_device_info(conn, XCB_XKB_ID_USE_CORE_KBD,
115                                 0, 0, 0, 0, 0, 0);
116     xcb_xkb_get_device_info_reply_t *reply =
117         xcb_xkb_get_device_info_reply(conn, cookie, NULL);
118
119     if (!reply)
120         return -1;
121
122     device_id = reply->deviceID;
123     free(reply);
124     return device_id;
125 }
126
127 void
128 get_atom_name(xcb_connection_t *conn, xcb_atom_t atom,
129               xcb_get_atom_name_cookie_t *cookie)
130 {
131     if (atom == 0) {
132         cookie->sequence = 0;
133     } else {
134         *cookie = xcb_get_atom_name(conn, atom);
135     }
136 }
137
138 bool
139 get_atom_name_reply(xcb_connection_t *conn, xcb_atom_t atom,
140                     xcb_get_atom_name_cookie_t cookie, char **out)
141 {
142     xcb_get_atom_name_reply_t *reply;
143     int length;
144     char *name;
145
146     if (atom == 0) {
147         *out = NULL;
148         assert(cookie.sequence == 0);
149         return true;
150     }
151
152     reply = xcb_get_atom_name_reply(conn, cookie, NULL);
153     if (!reply)
154         return false;
155
156     length = xcb_get_atom_name_name_length(reply);
157     name = xcb_get_atom_name_name(reply);
158
159     *out = strndup(name, length);
160     if (!*out) {
161         free(reply);
162         return false;
163     }
164
165     free(reply);
166     return true;
167 }
168
169 struct x11_atom_cache {
170     /*
171      * Invalidate the cache based on the XCB connection.
172      * X11 atoms are actually not per connection or client, but per X server
173      * session. But better be safe just in case we survive an X server restart.
174      */
175     xcb_connection_t *conn;
176     struct {
177         xcb_atom_t from;
178         xkb_atom_t to;
179     } cache[256];
180     size_t len;
181 };
182
183 static struct x11_atom_cache *
184 get_cache(struct xkb_context *ctx, xcb_connection_t *conn)
185 {
186     if (!ctx->x11_atom_cache) {
187         ctx->x11_atom_cache = calloc(1, sizeof(struct x11_atom_cache));
188     }
189     /* Can be NULL in case the malloc failed. */
190     struct x11_atom_cache *cache = ctx->x11_atom_cache;
191     if (cache && cache->conn != conn) {
192         cache->conn = conn;
193         cache->len = 0;
194     }
195     return cache;
196 }
197
198 void
199 x11_atom_interner_init(struct x11_atom_interner *interner,
200                        struct xkb_context *ctx, xcb_connection_t *conn)
201 {
202     interner->had_error = false;
203     interner->ctx = ctx;
204     interner->conn = conn;
205     interner->num_pending = 0;
206     interner->num_copies = 0;
207 }
208
209 void
210 x11_atom_interner_adopt_atom(struct x11_atom_interner *interner,
211                              const xcb_atom_t atom, xkb_atom_t *out)
212 {
213     *out = 0;
214
215     /* Can be NULL in case the malloc failed. */
216     struct x11_atom_cache *cache = get_cache(interner->ctx, interner->conn);
217
218 retry:
219
220     /* Already in the cache? */
221     if (cache) {
222         for (size_t c = 0; c < cache->len; c++) {
223             if (cache->cache[c].from == atom) {
224                 *out = cache->cache[c].to;
225                 return;
226             }
227         }
228     }
229
230     /* Already pending? */
231     for (size_t i = 0; i < interner->num_pending; i++) {
232         if (interner->pending[i].from == atom) {
233             if (interner->num_copies == ARRAY_SIZE(interner->copies)) {
234                 x11_atom_interner_round_trip(interner);
235                 goto retry;
236             }
237
238             size_t idx = interner->num_copies++;
239             interner->copies[idx].from = atom;
240             interner->copies[idx].out = out;
241             return;
242         }
243     }
244
245     /* We have to send a GetAtomName request */
246     if (interner->num_pending == ARRAY_SIZE(interner->pending)) {
247         x11_atom_interner_round_trip(interner);
248         assert(interner->num_pending < ARRAY_SIZE(interner->pending));
249     }
250     size_t idx = interner->num_pending++;
251     interner->pending[idx].from = atom;
252     interner->pending[idx].out = out;
253     interner->pending[idx].cookie = xcb_get_atom_name(interner->conn, atom);
254 }
255
256 void
257 x11_atom_interner_adopt_atoms(struct x11_atom_interner *interner,
258                               const xcb_atom_t *from, xkb_atom_t *to,
259                               size_t count)
260 {
261     for (size_t i = 0; i < count; i++) {
262         x11_atom_interner_adopt_atom(interner, from[i], &to[i]);
263     }
264 }
265
266 void x11_atom_interner_round_trip(struct x11_atom_interner *interner) {
267     struct xkb_context *ctx = interner->ctx;
268     xcb_connection_t *conn = interner->conn;
269
270     /* Can be NULL in case the malloc failed. */
271     struct x11_atom_cache *cache = get_cache(ctx, conn);
272
273     for (size_t i = 0; i < interner->num_pending; i++) {
274         xcb_get_atom_name_reply_t *reply;
275
276         reply = xcb_get_atom_name_reply(conn, interner->pending[i].cookie, NULL);
277         if (!reply) {
278             interner->had_error = true;
279             continue;
280         }
281         xcb_atom_t x11_atom = interner->pending[i].from;
282         xkb_atom_t atom = xkb_atom_intern(ctx,
283                                           xcb_get_atom_name_name(reply),
284                                           xcb_get_atom_name_name_length(reply));
285         free(reply);
286
287         if (cache && cache->len < ARRAY_SIZE(cache->cache)) {
288             size_t idx = cache->len++;
289             cache->cache[idx].from = x11_atom;
290             cache->cache[idx].to = atom;
291         }
292
293         *interner->pending[i].out = atom;
294
295         for (size_t j = 0; j < interner->num_copies; j++) {
296             if (interner->copies[j].from == x11_atom)
297                 *interner->copies[j].out = atom;
298         }
299     }
300
301     interner->num_pending = 0;
302     interner->num_copies = 0;
303 }