Rename keymap allocation API
[platform/upstream/libxkbcommon.git] / test / state.c
1 /*
2  * Copyright © 2012 Intel Corporation
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  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <X11/X.h>
30 #include <X11/Xdefs.h>
31 #include <X11/keysym.h>
32 #include <linux/input.h>
33 #include "xkbcommon/xkbcommon.h"
34 #include "xkbcomp/utils.h"
35 #include "XKBcommonint.h"
36
37 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
38  * keycode set (where ESC is 9). */
39 #define EVDEV_OFFSET 8
40
41 static void
42 print_state(struct xkb_state *state)
43 {
44     xkb_group_index_t group;
45     xkb_mod_index_t mod;
46     xkb_led_index_t led;
47
48     if (!state->group && !state->mods && !state->leds) {
49         fprintf(stderr, "\tno state\n");
50         return;
51     }
52
53     for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
54         if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
55             continue;
56         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
57                 xkb_map_group_get_name(state->xkb, group),
58                 group,
59                 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
60                     "effective " : "",
61                 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
62                     "depressed " : "",
63                 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
64                     "latched " : "",
65                 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
66                     "locked " : "");
67     }
68
69     for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
70         if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
71             continue;
72         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
73                 xkb_map_mod_get_name(state->xkb, mod),
74                 mod,
75                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
76                     "depressed " : "",
77                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
78                     "latched " : "",
79                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
80                     "locked " : "");
81     }
82
83     for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
84         if (!xkb_state_led_index_is_active(state, led))
85             continue;
86         fprintf(stderr, "\tled %s (%d): active\n",
87                 xkb_map_led_get_name(state->xkb, led),
88                 led);
89     }
90 }
91
92 int
93 main(int argc, char *argv[])
94 {
95     struct xkb_rule_names rmlvo;
96     struct xkb_desc *xkb;
97     struct xkb_state *state;
98     int num_syms;
99     xkb_keysym_t *syms;
100
101     rmlvo.rules = "evdev";
102     rmlvo.model = "pc104";
103     rmlvo.layout = "us";
104     rmlvo.variant = NULL;
105     rmlvo.options = NULL;
106
107     xkb = xkb_map_new_from_names(&rmlvo);
108
109     if (!xkb) {
110         fprintf(stderr, "Failed to compile keymap\n");
111         exit(1);
112     }
113
114     state = xkb_state_new(xkb);
115
116     /* LCtrl down */
117     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
118     fprintf(stderr, "dumping state for LCtrl down:\n");
119     print_state(state);
120     assert(xkb_state_mod_name_is_active(state, "Control",
121                                         XKB_STATE_DEPRESSED));
122
123     /* LCtrl + RAlt down */
124     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 1);
125     fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
126     print_state(state);
127     assert(xkb_state_mod_name_is_active(state, "Control",
128                                         XKB_STATE_DEPRESSED));
129     assert(xkb_state_mod_name_is_active(state, "Mod1",
130                                         XKB_STATE_DEPRESSED));
131
132     /* RAlt down */
133     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0);
134     fprintf(stderr, "dumping state for RAlt down:\n");
135     print_state(state);
136     assert(!xkb_state_mod_name_is_active(state, "Control",
137                                          XKB_STATE_EFFECTIVE));
138     assert(xkb_state_mod_name_is_active(state, "Mod1",
139                                         XKB_STATE_DEPRESSED));
140
141     /* none down */
142     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 0);
143     assert(!xkb_state_mod_name_is_active(state, "Mod1",
144                                          XKB_STATE_EFFECTIVE));
145
146     /* Caps locked */
147     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
148     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
149     fprintf(stderr, "dumping state for Caps Lock:\n");
150     print_state(state);
151     assert(xkb_state_mod_name_is_active(state, "Caps Lock",
152                                         XKB_STATE_LOCKED));
153     assert(xkb_state_led_name_is_active(state, "Caps Lock"));
154     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
155     assert(num_syms == 1 && syms[0] == XK_Q);
156
157     /* Caps unlocked */
158     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
159     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
160     assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
161                                          XKB_STATE_EFFECTIVE));
162     assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
163     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
164     assert(num_syms == 1 && syms[0] == XK_q);
165
166     xkb_state_unref(state);
167     xkb_map_unref(xkb);
168
169     return 0;
170 }