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