Organize src/ and test/ headers
[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 <linux/input.h>
25
26 #include "test.h"
27
28 enum {
29     DOWN,
30     UP,
31     BOTH,
32     NEXT,
33     FINISH,
34 };
35
36 #define EVDEV_OFFSET 8
37
38 /*
39  * Test a sequence of keysyms, resulting from a sequence of key presses,
40  * against the keysyms they're supposed to generate.
41  *
42  * - Each test runs with a clean state.
43  * - Each line in the test is made up of:
44  *   + A keycode, given as a KEY_* from linux/input.h.
45  *   + A direction - DOWN for press, UP for release, BOTH for
46  *     immediate press + release.
47  *   + A sequence of keysyms that should result from this keypress.
48  *
49  * The vararg format is:
50  * <KEY_*>  <DOWN | UP | BOTH>  <XKB_KEY_* (zero or more)>  <NEXT | FINISH>
51  *
52  * See below for examples.
53  */
54 static int
55 test_key_seq(struct xkb_keymap *keymap, ...)
56 {
57     struct xkb_state *state;
58
59     va_list ap;
60     xkb_keycode_t kc;
61     int op;
62     xkb_keysym_t keysym;
63
64     const xkb_keysym_t *syms;
65     unsigned int nsyms, i;
66     char ksbuf[64];
67
68     state = xkb_state_new(keymap);
69     assert(state);
70
71     va_start(ap, keymap);
72
73     for (;;) {
74         kc = va_arg(ap, int) + EVDEV_OFFSET;
75         op = va_arg(ap, int);
76
77         nsyms = xkb_key_get_syms(state, kc, &syms);
78         fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
79
80         if (op == DOWN || op == BOTH)
81             xkb_state_update_key(state, kc, XKB_KEY_DOWN);
82         if (op == UP || op == BOTH)
83             xkb_state_update_key(state, kc, XKB_KEY_UP);
84
85         for (i = 0; i < nsyms; i++) {
86             keysym = va_arg(ap, int);
87             xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
88             fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
89
90             if (keysym == FINISH || keysym == NEXT) {
91                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
92                 fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
93                 goto fail;
94             }
95
96             if (keysym != syms[i]) {
97                 xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
98                 fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
99                 xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
100                 fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
101                 goto fail;
102             }
103         }
104
105         fprintf(stderr, "]\n");
106
107         keysym = va_arg(ap, int);
108         if (keysym == NEXT)
109             continue;
110         if (keysym == FINISH)
111             break;
112
113         xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
114         fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
115         goto fail;
116     }
117
118     va_end(ap);
119     xkb_state_unref(state);
120     return 1;
121
122 fail:
123     va_end(ap);
124     xkb_state_unref(state);
125     return 0;
126 }
127
128 int
129 main(void)
130 {
131     struct xkb_context *ctx = test_get_context();
132     struct xkb_keymap *keymap;
133
134     assert(ctx);
135     keymap = test_compile_rules(ctx, "evdev", "evdev",
136                                 "us,il,ru,de", ",,phonetic,neo",
137                                 "grp:alt_shift_toggle,grp:menu_toggle");
138     assert(keymap);
139
140     assert(test_key_seq(keymap,
141                         KEY_H,  BOTH,  XKB_KEY_h,  NEXT,
142                         KEY_E,  BOTH,  XKB_KEY_e,  NEXT,
143                         KEY_L,  BOTH,  XKB_KEY_l,  NEXT,
144                         KEY_L,  BOTH,  XKB_KEY_l,  NEXT,
145                         KEY_O,  BOTH,  XKB_KEY_o,  FINISH));
146
147     assert(test_key_seq(keymap,
148                         KEY_H,          BOTH,  XKB_KEY_h,        NEXT,
149                         KEY_LEFTSHIFT,  DOWN,  XKB_KEY_Shift_L,  NEXT,
150                         KEY_E,          BOTH,  XKB_KEY_E,        NEXT,
151                         KEY_L,          BOTH,  XKB_KEY_L,        NEXT,
152                         KEY_LEFTSHIFT,  UP,    XKB_KEY_Shift_L,  NEXT,
153                         KEY_L,          BOTH,  XKB_KEY_l,        NEXT,
154                         KEY_O,          BOTH,  XKB_KEY_o,        FINISH));
155
156     /* Base modifier cleared on key release... */
157     assert(test_key_seq(keymap,
158                         KEY_H,          BOTH,  XKB_KEY_h,        NEXT,
159                         KEY_LEFTSHIFT,  DOWN,  XKB_KEY_Shift_L,  NEXT,
160                         KEY_E,          BOTH,  XKB_KEY_E,        NEXT,
161                         KEY_L,          BOTH,  XKB_KEY_L,        NEXT,
162                         KEY_LEFTSHIFT,  DOWN,  XKB_KEY_Shift_L,  NEXT,
163                         KEY_L,          BOTH,  XKB_KEY_L,        NEXT,
164                         KEY_O,          BOTH,  XKB_KEY_O,        FINISH));
165
166     /* ... But only by the keycode that set it. */
167     assert(test_key_seq(keymap,
168                         KEY_H,           BOTH,  XKB_KEY_h,        NEXT,
169                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,  NEXT,
170                         KEY_E,           BOTH,  XKB_KEY_E,        NEXT,
171                         KEY_L,           BOTH,  XKB_KEY_L,        NEXT,
172                         KEY_RIGHTSHIFT,  UP,    XKB_KEY_Shift_R,  NEXT,
173                         KEY_L,           BOTH,  XKB_KEY_L,        NEXT,
174                         KEY_O,           BOTH,  XKB_KEY_O,        FINISH));
175
176     /*
177      * A base modifier should only be cleared when no other key affecting
178      * the modifier is down.
179      */
180     assert(test_key_seq(keymap,
181                         KEY_H,           BOTH,  XKB_KEY_h,        NEXT,
182                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,  NEXT,
183                         KEY_E,           BOTH,  XKB_KEY_E,        NEXT,
184                         KEY_RIGHTSHIFT,  DOWN,  XKB_KEY_Shift_R,  NEXT,
185                         KEY_L,           BOTH,  XKB_KEY_L,        NEXT,
186                         KEY_RIGHTSHIFT,  UP,    XKB_KEY_Shift_R,  NEXT,
187                         KEY_L,           BOTH,  XKB_KEY_L,        NEXT,
188                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Shift_L,  NEXT,
189                         KEY_O,           BOTH,  XKB_KEY_o,        FINISH));
190
191     /* Group switching / locking. */
192     assert(test_key_seq(keymap,
193                         KEY_H,        BOTH,  XKB_KEY_h,               NEXT,
194                         KEY_E,        BOTH,  XKB_KEY_e,               NEXT,
195                         KEY_COMPOSE,  BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
196                         KEY_K,        BOTH,  XKB_KEY_hebrew_lamed,    NEXT,
197                         KEY_F,        BOTH,  XKB_KEY_hebrew_kaph,     NEXT,
198                         KEY_COMPOSE,  BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
199                         KEY_COMPOSE,  BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
200                         KEY_COMPOSE,  BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
201                         KEY_O,        BOTH,  XKB_KEY_o,               FINISH));
202
203     assert(test_key_seq(keymap,
204                         KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L,        NEXT,
205                         KEY_LEFTALT,   DOWN, XKB_KEY_ISO_Next_Group, NEXT,
206                         KEY_LEFTALT,   UP,   XKB_KEY_ISO_Next_Group, NEXT,
207                         KEY_LEFTSHIFT, UP,   XKB_KEY_Shift_L,        FINISH));
208
209     assert(test_key_seq(keymap,
210                         KEY_LEFTALT,   DOWN, XKB_KEY_Alt_L,          NEXT,
211                         KEY_LEFTSHIFT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
212                         KEY_LEFTSHIFT, UP,   XKB_KEY_ISO_Next_Group, NEXT,
213                         KEY_LEFTALT,   UP,   XKB_KEY_Alt_L,          FINISH));
214
215     /* Locked modifiers. */
216     assert(test_key_seq(keymap,
217                         KEY_CAPSLOCK,  BOTH,  XKB_KEY_Caps_Lock,  NEXT,
218                         KEY_H,         BOTH,  XKB_KEY_H,          NEXT,
219                         KEY_E,         BOTH,  XKB_KEY_E,          NEXT,
220                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
221                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
222                         KEY_O,         BOTH,  XKB_KEY_O,          FINISH));
223
224     assert(test_key_seq(keymap,
225                         KEY_H,         BOTH,  XKB_KEY_h,          NEXT,
226                         KEY_E,         BOTH,  XKB_KEY_e,          NEXT,
227                         KEY_CAPSLOCK,  BOTH,  XKB_KEY_Caps_Lock,  NEXT,
228                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
229                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
230                         KEY_CAPSLOCK,  BOTH,  XKB_KEY_Caps_Lock,  NEXT,
231                         KEY_O,         BOTH,  XKB_KEY_o,          FINISH));
232
233     assert(test_key_seq(keymap,
234                         KEY_H,         BOTH,  XKB_KEY_h,          NEXT,
235                         KEY_CAPSLOCK,  DOWN,  XKB_KEY_Caps_Lock,  NEXT,
236                         KEY_E,         BOTH,  XKB_KEY_E,          NEXT,
237                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
238                         KEY_L,         BOTH,  XKB_KEY_L,          NEXT,
239                         KEY_CAPSLOCK,  UP,    XKB_KEY_Caps_Lock,  NEXT,
240                         KEY_O,         BOTH,  XKB_KEY_O,          FINISH));
241
242     assert(test_key_seq(keymap,
243                         KEY_H,         BOTH,  XKB_KEY_h,          NEXT,
244                         KEY_E,         BOTH,  XKB_KEY_e,          NEXT,
245                         KEY_CAPSLOCK,  UP,    XKB_KEY_Caps_Lock,  NEXT,
246                         KEY_L,         BOTH,  XKB_KEY_l,          NEXT,
247                         KEY_L,         BOTH,  XKB_KEY_l,          NEXT,
248                         KEY_O,         BOTH,  XKB_KEY_o,          FINISH));
249
250     /*
251      * A key release affecting a locked modifier should clear it
252      * regardless of the key press.
253      */
254     /* assert(test_key_seq(keymap, */
255     /*                     KEY_H,         BOTH,  XKB_KEY_h,          NEXT, */
256     /*                     KEY_CAPSLOCK,  DOWN,  XKB_KEY_Caps_Lock,  NEXT, */
257     /*                     KEY_E,         BOTH,  XKB_KEY_E,          NEXT, */
258     /*                     KEY_L,         BOTH,  XKB_KEY_L,          NEXT, */
259     /*                     KEY_CAPSLOCK,  UP,    XKB_KEY_Caps_Lock,  NEXT, */
260     /*                     KEY_L,         BOTH,  XKB_KEY_L,          NEXT, */
261     /*                     KEY_CAPSLOCK,  UP,    XKB_KEY_Caps_Lock,  NEXT, */
262     /*                     KEY_O,         BOTH,  XKB_KEY_o,          FINISH)); */
263
264     /* Simple Num Lock sanity check. */
265     assert(test_key_seq(keymap,
266                         KEY_KP1,      BOTH,  XKB_KEY_KP_End,    NEXT,
267                         KEY_NUMLOCK,  BOTH,  XKB_KEY_Num_Lock,  NEXT,
268                         KEY_KP1,      BOTH,  XKB_KEY_KP_1,      NEXT,
269                         KEY_KP2,      BOTH,  XKB_KEY_KP_2,      NEXT,
270                         KEY_NUMLOCK,  BOTH,  XKB_KEY_Num_Lock,  NEXT,
271                         KEY_KP2,      BOTH,  XKB_KEY_KP_Down,   FINISH));
272
273     /* Test that the aliases in the ru(phonetic) symbols map work. */
274     assert(test_key_seq(keymap,
275                         KEY_COMPOSE,     BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
276                         KEY_COMPOSE,     BOTH,  XKB_KEY_ISO_Next_Group,  NEXT,
277                         KEY_1,           BOTH,  XKB_KEY_1,               NEXT,
278                         KEY_Q,           BOTH,  XKB_KEY_Cyrillic_ya,     NEXT,
279                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,         NEXT,
280                         KEY_1,           BOTH,  XKB_KEY_exclam,          NEXT,
281                         KEY_Q,           BOTH,  XKB_KEY_Cyrillic_YA,     NEXT,
282                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Shift_L,         NEXT,
283                         KEY_V,           BOTH,  XKB_KEY_Cyrillic_zhe,    NEXT,
284                         KEY_CAPSLOCK,    BOTH,  XKB_KEY_Caps_Lock,       NEXT,
285                         KEY_1,           BOTH,  XKB_KEY_1,               NEXT,
286                         KEY_V,           BOTH,  XKB_KEY_Cyrillic_ZHE,    NEXT,
287                         KEY_RIGHTSHIFT,  DOWN,  XKB_KEY_Shift_R,         NEXT,
288                         KEY_V,           BOTH,  XKB_KEY_Cyrillic_zhe,    NEXT,
289                         KEY_RIGHTSHIFT,  UP,    XKB_KEY_Shift_R,         NEXT,
290                         KEY_V,           BOTH,  XKB_KEY_Cyrillic_ZHE,    FINISH));
291
292 #define KS(name) xkb_keysym_from_name(name)
293
294     /* Test that levels (1-5) in de(neo) symbols map work. */
295     assert(test_key_seq(keymap,
296                         /* Switch to the group. */
297                         KEY_COMPOSE,     BOTH,  XKB_KEY_ISO_Next_Group,    NEXT,
298                         KEY_COMPOSE,     BOTH,  XKB_KEY_ISO_Next_Group,    NEXT,
299                         KEY_COMPOSE,     BOTH,  XKB_KEY_ISO_Next_Group,    NEXT,
300
301                         /* Level 1. */
302                         KEY_1,           BOTH,  XKB_KEY_1,                 NEXT,
303                         KEY_Q,           BOTH,  XKB_KEY_x,                 NEXT,
304                         KEY_KP7,         BOTH,  XKB_KEY_KP_7,              NEXT,
305                         KEY_ESC,         BOTH,  XKB_KEY_Escape,            NEXT,
306
307                         /* Level 2 with Shift. */
308                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,           NEXT,
309                         KEY_1,           BOTH,  XKB_KEY_degree,            NEXT,
310                         KEY_Q,           BOTH,  XKB_KEY_X,                 NEXT,
311                         KEY_KP7,         BOTH,  KS("U2714"),               NEXT,
312                         KEY_ESC,         BOTH,  XKB_KEY_Escape,            NEXT,
313                         /*
314                          * XXX: de(neo) uses shift(both_capslock) which causes
315                          * the interesting result in the next line. Since it's
316                          * a key release, it doesn't actually lock the modifier,
317                          * and applications by-and-large ignore the keysym on
318                          * release(?). Is this a problem?
319                          */
320                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Caps_Lock,         NEXT,
321
322                         /* Level 2 with the Lock modifier. */
323                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,           NEXT,
324                         KEY_RIGHTSHIFT,  BOTH,  XKB_KEY_Caps_Lock,         NEXT,
325                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Caps_Lock,         NEXT,
326                         KEY_6,           BOTH,  XKB_KEY_6,                 NEXT,
327                         KEY_H,           BOTH,  XKB_KEY_S,                 NEXT,
328                         KEY_KP3,         BOTH,  XKB_KEY_KP_3,              NEXT,
329                         KEY_ESC,         BOTH,  XKB_KEY_Escape,            NEXT,
330                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,           NEXT,
331                         KEY_RIGHTSHIFT,  BOTH,  XKB_KEY_Caps_Lock,         NEXT,
332                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Caps_Lock,         NEXT,
333
334                         /* Level 3. */
335                         KEY_CAPSLOCK,    DOWN,  XKB_KEY_ISO_Level3_Shift,  NEXT,
336                         KEY_6,           BOTH,  XKB_KEY_cent,              NEXT,
337                         KEY_Q,           BOTH,  XKB_KEY_ellipsis,          NEXT,
338                         KEY_KP7,         BOTH,  KS("U2195"),               NEXT,
339                         KEY_ESC,         BOTH,  XKB_KEY_Escape,            NEXT,
340                         KEY_CAPSLOCK,    UP,    XKB_KEY_ISO_Level3_Shift,  NEXT,
341
342                         /* Level 4. */
343                         KEY_CAPSLOCK,    DOWN,  XKB_KEY_ISO_Level3_Shift,  NEXT,
344                         KEY_LEFTSHIFT,   DOWN,  XKB_KEY_Shift_L,           NEXT,
345                         KEY_5,           BOTH,  XKB_KEY_malesymbol,        NEXT,
346                         KEY_E,           BOTH,  XKB_KEY_Greek_lambda,      NEXT,
347                         KEY_SPACE,       BOTH,  XKB_KEY_nobreakspace,      NEXT,
348                         KEY_KP8,         BOTH,  XKB_KEY_intersection,      NEXT,
349                         KEY_ESC,         BOTH,  XKB_KEY_Escape,            NEXT,
350                         KEY_LEFTSHIFT,   UP,    XKB_KEY_Caps_Lock,         NEXT,
351                         KEY_CAPSLOCK,    UP,    XKB_KEY_ISO_Level3_Shift,  NEXT,
352
353                         /* Level 5. */
354                         KEY_RIGHTALT,    DOWN,  XKB_KEY_ISO_Level5_Shift,  NEXT,
355                         /*
356                          * XXX: This doesn't work, but gives level1 keysyms.
357                          * This does work when when de(neo) is the first layout
358                          * (before us,il etc.). It's like that in the X server
359                          * as well. Investigate.
360                          */
361                         KEY_RIGHTALT,    UP,    XKB_KEY_ISO_Level5_Shift,  NEXT,
362
363                         KEY_V,           BOTH,  XKB_KEY_p,               FINISH));
364
365     xkb_map_unref(keymap);
366     xkb_context_unref(ctx);
367     return 0;
368 }