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