xref: /aosp_15_r20/external/libxkbcommon/test/x11.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2013 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include "test.h"
27*2b949d04SAndroid Build Coastguard Worker #include "xkbcommon/xkbcommon-x11.h"
28*2b949d04SAndroid Build Coastguard Worker 
29*2b949d04SAndroid Build Coastguard Worker int
main(void)30*2b949d04SAndroid Build Coastguard Worker main(void)
31*2b949d04SAndroid Build Coastguard Worker {
32*2b949d04SAndroid Build Coastguard Worker     struct xkb_context *ctx = test_get_context(0);
33*2b949d04SAndroid Build Coastguard Worker     xcb_connection_t *conn;
34*2b949d04SAndroid Build Coastguard Worker     int ret;
35*2b949d04SAndroid Build Coastguard Worker     int32_t device_id;
36*2b949d04SAndroid Build Coastguard Worker     struct xkb_keymap *keymap;
37*2b949d04SAndroid Build Coastguard Worker     struct xkb_state *state;
38*2b949d04SAndroid Build Coastguard Worker     char *dump;
39*2b949d04SAndroid Build Coastguard Worker     int exit_code = 0;
40*2b949d04SAndroid Build Coastguard Worker 
41*2b949d04SAndroid Build Coastguard Worker     /*
42*2b949d04SAndroid Build Coastguard Worker     * The next two steps depend on a running X server with XKB support.
43*2b949d04SAndroid Build Coastguard Worker     * If it fails, it's not necessarily an actual problem with the code.
44*2b949d04SAndroid Build Coastguard Worker     * So we don't want a FAIL here.
45*2b949d04SAndroid Build Coastguard Worker     */
46*2b949d04SAndroid Build Coastguard Worker     conn = xcb_connect(NULL, NULL);
47*2b949d04SAndroid Build Coastguard Worker     if (!conn || xcb_connection_has_error(conn)) {
48*2b949d04SAndroid Build Coastguard Worker         exit_code = SKIP_TEST;
49*2b949d04SAndroid Build Coastguard Worker         goto err_conn;
50*2b949d04SAndroid Build Coastguard Worker     }
51*2b949d04SAndroid Build Coastguard Worker 
52*2b949d04SAndroid Build Coastguard Worker     ret = xkb_x11_setup_xkb_extension(conn,
53*2b949d04SAndroid Build Coastguard Worker                                       XKB_X11_MIN_MAJOR_XKB_VERSION,
54*2b949d04SAndroid Build Coastguard Worker                                       XKB_X11_MIN_MINOR_XKB_VERSION,
55*2b949d04SAndroid Build Coastguard Worker                                       XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
56*2b949d04SAndroid Build Coastguard Worker                                       NULL, NULL, NULL, NULL);
57*2b949d04SAndroid Build Coastguard Worker     if (!ret) {
58*2b949d04SAndroid Build Coastguard Worker         exit_code = SKIP_TEST;
59*2b949d04SAndroid Build Coastguard Worker         goto err_conn;
60*2b949d04SAndroid Build Coastguard Worker     }
61*2b949d04SAndroid Build Coastguard Worker 
62*2b949d04SAndroid Build Coastguard Worker     device_id = xkb_x11_get_core_keyboard_device_id(conn);
63*2b949d04SAndroid Build Coastguard Worker     assert(device_id != -1);
64*2b949d04SAndroid Build Coastguard Worker 
65*2b949d04SAndroid Build Coastguard Worker     keymap = xkb_x11_keymap_new_from_device(ctx, conn, device_id,
66*2b949d04SAndroid Build Coastguard Worker                                             XKB_KEYMAP_COMPILE_NO_FLAGS);
67*2b949d04SAndroid Build Coastguard Worker     assert(keymap);
68*2b949d04SAndroid Build Coastguard Worker 
69*2b949d04SAndroid Build Coastguard Worker     state = xkb_x11_state_new_from_device(keymap, conn, device_id);
70*2b949d04SAndroid Build Coastguard Worker     assert(state);
71*2b949d04SAndroid Build Coastguard Worker 
72*2b949d04SAndroid Build Coastguard Worker     dump = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_USE_ORIGINAL_FORMAT);
73*2b949d04SAndroid Build Coastguard Worker     assert(dump);
74*2b949d04SAndroid Build Coastguard Worker     fputs(dump, stdout);
75*2b949d04SAndroid Build Coastguard Worker 
76*2b949d04SAndroid Build Coastguard Worker     /* TODO: Write some X11-specific tests. */
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     free(dump);
79*2b949d04SAndroid Build Coastguard Worker     xkb_state_unref(state);
80*2b949d04SAndroid Build Coastguard Worker     xkb_keymap_unref(keymap);
81*2b949d04SAndroid Build Coastguard Worker err_conn:
82*2b949d04SAndroid Build Coastguard Worker     xcb_disconnect(conn);
83*2b949d04SAndroid Build Coastguard Worker     xkb_context_unref(ctx);
84*2b949d04SAndroid Build Coastguard Worker 
85*2b949d04SAndroid Build Coastguard Worker     return exit_code;
86*2b949d04SAndroid Build Coastguard Worker }
87