Replace xkb_keycode_t 'key' variable name by 'kc'
[platform/upstream/libxkbcommon.git] / test / keyseq.c
1 /*
2  * Copyright © 2012 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 <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27
28 #include <linux/input.h>
29 #include <X11/keysym.h>
30
31 #include "xkbcommon/xkbcommon.h"
32 #include "test.h"
33
34 enum {
35     DOWN,
36     UP,
37     BOTH,
38     NEXT,
39     FINISH,
40 };
41
42 #define EVDEV_OFFSET 8
43
44 /*
45  * Test a sequence of keysyms, resulting from a sequence of key presses,
46  * against the keysyms they're supposed to generate.
47  *
48  * - Each test runs with a clean state.
49  * - Each line in the test is made up of:
50  *   + A keycode, given as a KEY_* from linux/input.h.
51  *   + A direction - DOWN for press, UP for release, BOTH for
52  *     immediate press + release.
53  *   + A sequence of keysyms that should result from this keypress.
54  *
55  * The vararg format is:
56  * <KEY_*>  <DOWN | UP | BOTH>  <XK_* (zero or more)>  <NEXT | FINISH>
57  *
58  * See below for examples.
59  */
60 static int
61 test_key_seq(struct xkb_keymap *keymap, ...)
62 {
63     struct xkb_state *state;
64
65     va_list ap;
66     xkb_keycode_t kc;
67     int op;
68     xkb_keysym_t keysym;
69
70     const xkb_keysym_t *syms;
71     unsigned int nsyms, i;
72     char ksbuf[64];
73
74     state = xkb_state_new(keymap);
75     assert(state);
76
77     va_start(ap, keymap);
78
79     for (;;) {
80         kc = va_arg(ap, int) + EVDEV_OFFSET;
81         op = va_arg(ap, int);
82
83         nsyms = xkb_key_get_syms(state, kc, &syms);
84         fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
85
86         if (op == DOWN || op == BOTH)
87             xkb_state_update_key(state, kc, XKB_KEY_DOWN);
88         if (op == UP || op == BOTH)
89             xkb_state_update_key(state, kc, XKB_KEY_UP);
90
91         for (i = 0; i < nsyms; i++) {
92             keysym = va_arg(ap, int);
93             xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
94             fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
95
96             if (keysym == FINISH || keysym == NEXT) {
97                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
98                 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
99                 goto fail;
100             }
101
102             if (keysym != syms[i]) {
103                 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
104                 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
105                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
106                 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
107                 goto fail;
108             }
109         }
110
111         fprintf(stderr, "]\n");
112
113         keysym = va_arg(ap, int);
114         if (keysym == NEXT)
115             continue;
116         if (keysym == FINISH)
117             break;
118
119         xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
120         fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
121         goto fail;
122     }
123
124     va_end(ap);
125     xkb_state_unref(state);
126     return 1;
127
128 fail:
129     va_end(ap);
130     xkb_state_unref(state);
131     return 0;
132 }
133
134 int
135 main(void)
136 {
137     struct xkb_context *ctx = test_get_context();
138     struct xkb_keymap *keymap;
139
140     assert(ctx);
141     keymap = test_compile_rules(ctx, "evdev", "evdev", "us,il", NULL,
142                                 "grp:alt_shift_toggle,grp:menu_toggle");
143     assert(keymap);
144
145     assert(test_key_seq(keymap,
146                         KEY_H,  BOTH,  XK_h,  NEXT,
147                         KEY_E,  BOTH,  XK_e,  NEXT,
148                         KEY_L,  BOTH,  XK_l,  NEXT,
149                         KEY_L,  BOTH,  XK_l,  NEXT,
150                         KEY_O,  BOTH,  XK_o,  FINISH));
151
152     assert(test_key_seq(keymap,
153                         KEY_H,          BOTH,  XK_h,        NEXT,
154                         KEY_LEFTSHIFT,  DOWN,  XK_Shift_L,  NEXT,
155                         KEY_E,          BOTH,  XK_E,        NEXT,
156                         KEY_L,          BOTH,  XK_L,        NEXT,
157                         KEY_LEFTSHIFT,  UP,    XK_Shift_L,  NEXT,
158                         KEY_L,          BOTH,  XK_l,        NEXT,
159                         KEY_O,          BOTH,  XK_o,        FINISH));
160
161     /* Base modifier cleared on key release... */
162     assert(test_key_seq(keymap,
163                         KEY_H,          BOTH,  XK_h,        NEXT,
164                         KEY_LEFTSHIFT,  DOWN,  XK_Shift_L,  NEXT,
165                         KEY_E,          BOTH,  XK_E,        NEXT,
166                         KEY_L,          BOTH,  XK_L,        NEXT,
167                         KEY_LEFTSHIFT,  DOWN,  XK_Shift_L,  NEXT,
168                         KEY_L,          BOTH,  XK_L,        NEXT,
169                         KEY_O,          BOTH,  XK_O,        FINISH));
170
171     /* ... But only by the keycode that set it. */
172     assert(test_key_seq(keymap,
173                         KEY_H,           BOTH,  XK_h,        NEXT,
174                         KEY_LEFTSHIFT,   DOWN,  XK_Shift_L,  NEXT,
175                         KEY_E,           BOTH,  XK_E,        NEXT,
176                         KEY_L,           BOTH,  XK_L,        NEXT,
177                         KEY_RIGHTSHIFT,  UP,    XK_Shift_R,  NEXT,
178                         KEY_L,           BOTH,  XK_L,        NEXT,
179                         KEY_O,           BOTH,  XK_O,        FINISH));
180
181     /*
182      * A base modifier should only be cleared when no other key affecting
183      * the modifier is down.
184      */
185     assert(test_key_seq(keymap,
186                         KEY_H,           BOTH,  XK_h,        NEXT,
187                         KEY_LEFTSHIFT,   DOWN,  XK_Shift_L,  NEXT,
188                         KEY_E,           BOTH,  XK_E,        NEXT,
189                         KEY_RIGHTSHIFT,  DOWN,  XK_Shift_R,  NEXT,
190                         KEY_L,           BOTH,  XK_L,        NEXT,
191                         KEY_RIGHTSHIFT,  UP,    XK_Shift_R,  NEXT,
192                         KEY_L,           BOTH,  XK_L,        NEXT,
193                         KEY_LEFTSHIFT,   UP,    XK_Shift_L,  NEXT,
194                         KEY_O,           BOTH,  XK_o,        FINISH));
195
196     /* Group switching / locking. */
197     assert(test_key_seq(keymap,
198                         KEY_H,        BOTH,  XK_h,               NEXT,
199                         KEY_E,        BOTH,  XK_e,               NEXT,
200                         KEY_COMPOSE,  BOTH,  XK_ISO_Next_Group,  NEXT,
201                         KEY_K,        BOTH,  XK_hebrew_lamed,    NEXT,
202                         KEY_F,        BOTH,  XK_hebrew_kaph,     NEXT,
203                         KEY_COMPOSE,  BOTH,  XK_ISO_Next_Group,  NEXT,
204                         KEY_O,        BOTH,  XK_o,               FINISH));
205
206     assert(test_key_seq(keymap,
207                         KEY_LEFTSHIFT, DOWN, XK_Shift_L,        NEXT,
208                         KEY_LEFTALT,   DOWN, XK_ISO_Next_Group, NEXT,
209                         KEY_LEFTALT,   UP,   XK_ISO_Next_Group, NEXT,
210                         KEY_LEFTSHIFT, UP,   XK_Shift_L,        FINISH));
211
212     assert(test_key_seq(keymap,
213                         KEY_LEFTALT,   DOWN, XK_Alt_L,          NEXT,
214                         KEY_LEFTSHIFT, DOWN, XK_ISO_Next_Group, NEXT,
215                         KEY_LEFTSHIFT, UP,   XK_ISO_Next_Group, NEXT,
216                         KEY_LEFTALT,   UP,   XK_Alt_L,          FINISH));
217
218     /* Locked modifiers. */
219     assert(test_key_seq(keymap,
220                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
221                         KEY_H,         BOTH,  XK_H,          NEXT,
222                         KEY_E,         BOTH,  XK_E,          NEXT,
223                         KEY_L,         BOTH,  XK_L,          NEXT,
224                         KEY_L,         BOTH,  XK_L,          NEXT,
225                         KEY_O,         BOTH,  XK_O,          FINISH));
226
227     assert(test_key_seq(keymap,
228                         KEY_H,         BOTH,  XK_h,          NEXT,
229                         KEY_E,         BOTH,  XK_e,          NEXT,
230                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
231                         KEY_L,         BOTH,  XK_L,          NEXT,
232                         KEY_L,         BOTH,  XK_L,          NEXT,
233                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
234                         KEY_O,         BOTH,  XK_o,          FINISH));
235
236     assert(test_key_seq(keymap,
237                         KEY_H,         BOTH,  XK_h,          NEXT,
238                         KEY_CAPSLOCK,  DOWN,  XK_Caps_Lock,  NEXT,
239                         KEY_E,         BOTH,  XK_E,          NEXT,
240                         KEY_L,         BOTH,  XK_L,          NEXT,
241                         KEY_L,         BOTH,  XK_L,          NEXT,
242                         KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT,
243                         KEY_O,         BOTH,  XK_O,          FINISH));
244
245     assert(test_key_seq(keymap,
246                         KEY_H,         BOTH,  XK_h,          NEXT,
247                         KEY_E,         BOTH,  XK_e,          NEXT,
248                         KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT,
249                         KEY_L,         BOTH,  XK_l,          NEXT,
250                         KEY_L,         BOTH,  XK_l,          NEXT,
251                         KEY_O,         BOTH,  XK_o,          FINISH));
252
253     /*
254      * A key release affecting a locked modifier should clear it
255      * regardless of the key press.
256      */
257     /* assert(test_key_seq(keymap, */
258     /*                     KEY_H,         BOTH,  XK_h,          NEXT, */
259     /*                     KEY_CAPSLOCK,  DOWN,  XK_Caps_Lock,  NEXT, */
260     /*                     KEY_E,         BOTH,  XK_E,          NEXT, */
261     /*                     KEY_L,         BOTH,  XK_L,          NEXT, */
262     /*                     KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT, */
263     /*                     KEY_L,         BOTH,  XK_L,          NEXT, */
264     /*                     KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT, */
265     /*                     KEY_O,         BOTH,  XK_o,          FINISH)); */
266
267     /* Simple Num Lock sanity check. */
268     assert(test_key_seq(keymap,
269                         KEY_KP1,      BOTH,  XK_KP_End,    NEXT,
270                         KEY_NUMLOCK,  BOTH,  XK_Num_Lock,  NEXT,
271                         KEY_KP1,      BOTH,  XK_KP_1,      NEXT,
272                         KEY_KP2,      BOTH,  XK_KP_2,      NEXT,
273                         KEY_NUMLOCK,  BOTH,  XK_Num_Lock,  NEXT,
274                         KEY_KP2,      BOTH,  XK_KP_Down,   FINISH));
275
276     xkb_map_unref(keymap);
277     xkb_context_unref(ctx);
278     return 0;
279 }