f279dd033695fb00dfd7fce55c1226b82590508c
[platform/upstream/libxkbcommon.git] / test / interactive-x11.c
1 /*
2  * Copyright © 2013 Ran Benita <ran234@gmail.com>
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 <locale.h>
27
28 #include <xcb/xkb.h>
29
30 #include "xkbcommon/xkbcommon-x11.h"
31 #include "test.h"
32
33 /*
34  * Note: This program only handles the core keyboard device for now.
35  * It should be straigtforward to change struct keyboard to a list of
36  * keyboards with device IDs, as in test/interactive-evdev.c. This would
37  * require:
38  *
39  * - Initially listing the keyboard devices.
40  * - Listening to device changes.
41  * - Matching events to their devices.
42  *
43  * XKB itself knows about xinput1 devices, and most requests and events are
44  * device-specific.
45  *
46  * In order to list the devices and react to changes, you need xinput1/2.
47  * You also need xinput for the key press/release event, since the core
48  * protocol key press event does not carry a device ID to match on.
49  */
50
51 struct keyboard {
52     xcb_connection_t *conn;
53     uint8_t first_xkb_event;
54     struct xkb_context *ctx;
55
56     struct xkb_keymap *keymap;
57     struct xkb_state *state;
58     int32_t device_id;
59 };
60
61 static bool terminate;
62
63 static int
64 select_xkb_events_for_device(xcb_connection_t *conn, int32_t device_id)
65 {
66     enum {
67         required_events =
68             (XCB_XKB_EVENT_TYPE_NEW_KEYBOARD_NOTIFY |
69              XCB_XKB_EVENT_TYPE_MAP_NOTIFY |
70              XCB_XKB_EVENT_TYPE_STATE_NOTIFY),
71
72         required_nkn_details =
73             (XCB_XKB_NKN_DETAIL_KEYCODES),
74
75         required_map_parts =
76             (XCB_XKB_MAP_PART_KEY_TYPES |
77              XCB_XKB_MAP_PART_KEY_SYMS |
78              XCB_XKB_MAP_PART_MODIFIER_MAP |
79              XCB_XKB_MAP_PART_EXPLICIT_COMPONENTS |
80              XCB_XKB_MAP_PART_KEY_ACTIONS |
81              XCB_XKB_MAP_PART_VIRTUAL_MODS |
82              XCB_XKB_MAP_PART_VIRTUAL_MOD_MAP),
83
84         required_state_details =
85             (XCB_XKB_STATE_PART_MODIFIER_BASE |
86              XCB_XKB_STATE_PART_MODIFIER_LATCH |
87              XCB_XKB_STATE_PART_MODIFIER_LOCK |
88              XCB_XKB_STATE_PART_GROUP_BASE |
89              XCB_XKB_STATE_PART_GROUP_LATCH |
90              XCB_XKB_STATE_PART_GROUP_LOCK),
91     };
92
93     static const xcb_xkb_select_events_details_t details = {
94         .affectNewKeyboard = required_nkn_details,
95         .newKeyboardDetails = required_nkn_details,
96         .affectState = required_state_details,
97         .stateDetails = required_state_details,
98     };
99
100     xcb_void_cookie_t cookie =
101         xcb_xkb_select_events_aux_checked(conn,
102                                           device_id,
103                                           required_events,    /* affectWhich */
104                                           0,                  /* clear */
105                                           0,                  /* selectAll */
106                                           required_map_parts, /* affectMap */
107                                           required_map_parts, /* map */
108                                           &details);          /* details */
109
110     xcb_generic_error_t *error = xcb_request_check(conn, cookie);
111     if (error) {
112         free(error);
113         return -1;
114     }
115
116     return 0;
117 }
118
119 static int
120 update_keymap(struct keyboard *kbd)
121 {
122     struct xkb_keymap *new_keymap;
123     struct xkb_state *new_state;
124
125     new_keymap = xkb_x11_keymap_new_from_device(kbd->ctx, kbd->conn,
126                                                 kbd->device_id, 0);
127     if (!new_keymap)
128         goto err_out;
129
130     new_state = xkb_x11_state_new_from_device(new_keymap, kbd->conn,
131                                               kbd->device_id);
132     if (!new_state)
133         goto err_keymap;
134
135     if (kbd->keymap)
136         printf("Keymap updated!\n");
137
138     xkb_state_unref(kbd->state);
139     xkb_keymap_unref(kbd->keymap);
140     kbd->keymap = new_keymap;
141     kbd->state = new_state;
142     return 0;
143
144 err_keymap:
145     xkb_keymap_unref(new_keymap);
146 err_out:
147     return -1;
148 }
149
150 static int
151 init_kbd(struct keyboard *kbd, xcb_connection_t *conn, uint8_t first_xkb_event,
152          int32_t device_id, struct xkb_context *ctx)
153 {
154     int ret;
155
156     kbd->conn = conn;
157     kbd->first_xkb_event = first_xkb_event;
158     kbd->ctx = ctx;
159     kbd->keymap = NULL;
160     kbd->state = NULL;
161     kbd->device_id = device_id;
162
163     ret = update_keymap(kbd);
164     if (ret)
165         goto err_out;
166
167     ret = select_xkb_events_for_device(conn, device_id);
168     if (ret)
169         goto err_state;
170
171     return 0;
172
173 err_state:
174     xkb_state_unref(kbd->state);
175     xkb_keymap_unref(kbd->keymap);
176 err_out:
177     return -1;
178 }
179
180 static void
181 deinit_kbd(struct keyboard *kbd)
182 {
183     xkb_state_unref(kbd->state);
184     xkb_keymap_unref(kbd->keymap);
185 }
186
187 static void
188 process_xkb_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
189 {
190     union xkb_event {
191         struct {
192             uint8_t response_type;
193             uint8_t xkbType;
194             uint16_t sequence;
195             xcb_timestamp_t time;
196             uint8_t deviceID;
197         } any;
198         xcb_xkb_new_keyboard_notify_event_t new_keyboard_notify;
199         xcb_xkb_map_notify_event_t map_notify;
200         xcb_xkb_state_notify_event_t state_notify;
201     } *event = (union xkb_event *) gevent;
202
203     if (event->any.deviceID != kbd->device_id)
204         return;
205
206     /*
207      * XkbNewKkdNotify and XkbMapNotify together capture all sorts of keymap
208      * updates (e.g. xmodmap, xkbcomp, setxkbmap), with minimal redundent
209      * recompilations.
210      */
211     switch (event->any.xkbType) {
212     case XCB_XKB_NEW_KEYBOARD_NOTIFY:
213         if (event->new_keyboard_notify.changed & XCB_XKB_NKN_DETAIL_KEYCODES)
214             update_keymap(kbd);
215         break;
216
217     case XCB_XKB_MAP_NOTIFY:
218         update_keymap(kbd);
219         break;
220
221     case XCB_XKB_STATE_NOTIFY:
222         xkb_state_update_mask(kbd->state,
223                               event->state_notify.baseMods,
224                               event->state_notify.latchedMods,
225                               event->state_notify.lockedMods,
226                               event->state_notify.baseGroup,
227                               event->state_notify.latchedGroup,
228                               event->state_notify.lockedGroup);
229         break;
230     }
231 }
232
233 static void
234 process_event(xcb_generic_event_t *gevent, struct keyboard *kbd)
235 {
236     switch (gevent->response_type) {
237     case XCB_KEY_PRESS: {
238         xcb_key_press_event_t *event = (xcb_key_press_event_t *) gevent;
239         xkb_keycode_t keycode = event->detail;
240
241         test_print_keycode_state(kbd->state, NULL, keycode,
242                                  XKB_CONSUMED_MODE_XKB);
243
244         /* Exit on ESC. */
245         if (keycode == 9)
246             terminate = true;
247         break;
248     }
249     default:
250         if (gevent->response_type == kbd->first_xkb_event)
251             process_xkb_event(gevent, kbd);
252         break;
253     }
254 }
255
256 static int
257 loop(xcb_connection_t *conn, struct keyboard *kbd)
258 {
259     while (!terminate) {
260         xcb_generic_event_t *event;
261
262         switch (xcb_connection_has_error(conn)) {
263         case 0:
264             break;
265         case XCB_CONN_ERROR:
266             fprintf(stderr,
267                     "Closed connection to X server: connection error\n");
268             return -1;
269         case XCB_CONN_CLOSED_EXT_NOTSUPPORTED:
270             fprintf(stderr,
271                     "Closed connection to X server: extension not supported\n");
272             return -1;
273         default:
274             fprintf(stderr,
275                     "Closed connection to X server: error code %d\n",
276                     xcb_connection_has_error(conn));
277             return -1;
278         }
279
280         event = xcb_wait_for_event(conn);
281         if (!event) {
282             continue;
283         }
284
285         process_event(event, kbd);
286         free(event);
287     }
288
289     return 0;
290 }
291
292 static int
293 create_capture_window(xcb_connection_t *conn)
294 {
295     xcb_generic_error_t *error;
296     xcb_void_cookie_t cookie;
297     xcb_screen_t *screen =
298         xcb_setup_roots_iterator(xcb_get_setup(conn)).data;
299     xcb_window_t window = xcb_generate_id(conn);
300     uint32_t values[2] = {
301         screen->white_pixel,
302         XCB_EVENT_MASK_KEY_PRESS,
303     };
304
305     cookie = xcb_create_window_checked(conn, XCB_COPY_FROM_PARENT,
306                                        window, screen->root,
307                                        10, 10, 100, 100, 1,
308                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
309                                        screen->root_visual,
310                                        XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
311                                        values);
312     if ((error = xcb_request_check(conn, cookie)) != NULL) {
313         free(error);
314         return -1;
315     }
316
317     cookie = xcb_map_window_checked(conn, window);
318     if ((error = xcb_request_check(conn, cookie)) != NULL) {
319         free(error);
320         return -1;
321     }
322
323     return 0;
324 }
325
326 int
327 main(int argc, char *argv[])
328 {
329     int ret;
330     xcb_connection_t *conn;
331     uint8_t first_xkb_event;
332     int32_t core_kbd_device_id;
333     struct xkb_context *ctx;
334     struct keyboard core_kbd;
335
336     setlocale(LC_ALL, "");
337
338     conn = xcb_connect(NULL, NULL);
339     if (!conn || xcb_connection_has_error(conn)) {
340         fprintf(stderr, "Couldn't connect to X server: error code %d\n",
341                 conn ? xcb_connection_has_error(conn) : -1);
342         ret = -1;
343         goto err_out;
344     }
345
346     ret = xkb_x11_setup_xkb_extension(conn,
347                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
348                                       XKB_X11_MIN_MINOR_XKB_VERSION,
349                                       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
350                                       NULL, NULL, &first_xkb_event, NULL);
351     if (!ret) {
352         fprintf(stderr, "Couldn't setup XKB extension\n");
353         goto err_conn;
354     }
355
356     ctx = test_get_context(0);
357     if (!ctx) {
358         ret = -1;
359         fprintf(stderr, "Couldn't create xkb context\n");
360         goto err_conn;
361     }
362
363     core_kbd_device_id = xkb_x11_get_core_keyboard_device_id(conn);
364     if (core_kbd_device_id == -1) {
365         ret = -1;
366         fprintf(stderr, "Couldn't find core keyboard device\n");
367         goto err_ctx;
368     }
369
370     ret = init_kbd(&core_kbd, conn, first_xkb_event, core_kbd_device_id, ctx);
371     if (ret) {
372         fprintf(stderr, "Couldn't initialize core keyboard device\n");
373         goto err_ctx;
374     }
375
376     ret = create_capture_window(conn);
377     if (ret) {
378         fprintf(stderr, "Couldn't create a capture window\n");
379         goto err_core_kbd;
380     }
381
382     test_disable_stdin_echo();
383     ret = loop(conn, &core_kbd);
384     test_enable_stdin_echo();
385
386 err_core_kbd:
387     deinit_kbd(&core_kbd);
388 err_ctx:
389     xkb_context_unref(ctx);
390 err_conn:
391     xcb_disconnect(conn);
392 err_out:
393     exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
394 }