253735c2a69f124dee31500a073d50cf0ea52cb0
[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
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>  <XK_* (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 keycode;
66     int op;
67     xkb_keysym_t keysym;
68
69     const xkb_keysym_t *syms;
70     unsigned int nsyms, i;
71     char ksbuf[16];
72
73     state = xkb_state_new(keymap);
74     assert(state);
75
76     va_start(ap, keymap);
77
78     for (;;) {
79         keycode = va_arg(ap, int) + EVDEV_OFFSET;
80         op = va_arg(ap, int);
81
82         nsyms = xkb_key_get_syms(state, keycode, &syms);
83         fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, keycode);
84
85         if (op == DOWN || op == BOTH)
86             xkb_state_update_key(state, keycode, XKB_KEY_DOWN);
87         if (op == UP || op == BOTH)
88             xkb_state_update_key(state, keycode, 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;
137     struct xkb_keymap *keymap;
138     const struct xkb_rule_names names = {
139         .rules = "evdev",
140         .model = "evdev",
141         .layout = "us,il",
142         .variant = "",
143         .options = "grp:menu_toggle",
144     };
145     char *XXX_remove_me_str;
146
147     ctx = xkb_context_new(0);
148     assert(ctx);
149     keymap = xkb_map_new_from_names(ctx, &names, 0);
150     assert(keymap);
151
152     XXX_remove_me_str = xkb_map_get_as_string(keymap);
153     fprintf(stderr, "keymap is:\n%s\n\n", XXX_remove_me_str);
154
155     assert(test_key_seq(keymap,
156                         KEY_H,  BOTH,  XK_h,  NEXT,
157                         KEY_E,  BOTH,  XK_e,  NEXT,
158                         KEY_L,  BOTH,  XK_l,  NEXT,
159                         KEY_L,  BOTH,  XK_l,  NEXT,
160                         KEY_O,  BOTH,  XK_o,  FINISH));
161
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,  UP,    XK_Shift_L,  NEXT,
168                         KEY_L,          BOTH,  XK_l,        NEXT,
169                         KEY_O,          BOTH,  XK_o,        FINISH));
170
171     /* Base modifier cleared on key release... */
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_LEFTSHIFT,  DOWN,  XK_Shift_L,  NEXT,
178                         KEY_L,          BOTH,  XK_L,        NEXT,
179                         KEY_O,          BOTH,  XK_O,        FINISH));
180
181     /* ... But only by the keycode that set it. */
182     assert(test_key_seq(keymap,
183                         KEY_H,           BOTH,  XK_h,        NEXT,
184                         KEY_LEFTSHIFT,   DOWN,  XK_Shift_L,  NEXT,
185                         KEY_E,           BOTH,  XK_E,        NEXT,
186                         KEY_L,           BOTH,  XK_L,        NEXT,
187                         KEY_RIGHTSHIFT,  UP,    XK_Shift_R,  NEXT,
188                         KEY_L,           BOTH,  XK_L,        NEXT,
189                         KEY_O,           BOTH,  XK_O,        FINISH));
190
191     /*
192      * A base modifier should only be cleared when no other key affecting
193      * the modifier is down.
194      */
195     assert(test_key_seq(keymap,
196                         KEY_H,           BOTH,  XK_h,        NEXT,
197                         KEY_LEFTSHIFT,   DOWN,  XK_Shift_L,  NEXT,
198                         KEY_E,           BOTH,  XK_E,        NEXT,
199                         KEY_RIGHTSHIFT,  DOWN,  XK_Shift_R,  NEXT,
200                         KEY_L,           BOTH,  XK_L,        NEXT,
201                         KEY_RIGHTSHIFT,  UP,    XK_Shift_R,  NEXT,
202                         KEY_L,           BOTH,  XK_L,        NEXT,
203                         KEY_LEFTSHIFT,   UP,    XK_Shift_L,  NEXT,
204                         KEY_O,           BOTH,  XK_o,        FINISH));
205
206     /* Group switching / locking. */
207     assert(test_key_seq(keymap,
208                         KEY_H,        BOTH,  XK_h,               NEXT,
209                         KEY_E,        BOTH,  XK_e,               NEXT,
210                         KEY_COMPOSE,  BOTH,  XK_ISO_Next_Group,  NEXT,
211                         KEY_K,        BOTH,  XK_hebrew_lamed,    NEXT,
212                         KEY_F,        BOTH,  XK_hebrew_kaph,     NEXT,
213                         KEY_COMPOSE,  BOTH,  XK_ISO_Next_Group,  NEXT,
214                         KEY_O,        BOTH,  XK_o,               FINISH));
215
216     /* Locked modifiers. */
217     assert(test_key_seq(keymap,
218                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
219                         KEY_H,         BOTH,  XK_H,          NEXT,
220                         KEY_E,         BOTH,  XK_E,          NEXT,
221                         KEY_L,         BOTH,  XK_L,          NEXT,
222                         KEY_L,         BOTH,  XK_L,          NEXT,
223                         KEY_O,         BOTH,  XK_O,          FINISH));
224
225     assert(test_key_seq(keymap,
226                         KEY_H,         BOTH,  XK_h,          NEXT,
227                         KEY_E,         BOTH,  XK_e,          NEXT,
228                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
229                         KEY_L,         BOTH,  XK_L,          NEXT,
230                         KEY_L,         BOTH,  XK_L,          NEXT,
231                         KEY_CAPSLOCK,  BOTH,  XK_Caps_Lock,  NEXT,
232                         KEY_O,         BOTH,  XK_o,          FINISH));
233
234     assert(test_key_seq(keymap,
235                         KEY_H,         BOTH,  XK_h,          NEXT,
236                         KEY_CAPSLOCK,  DOWN,  XK_Caps_Lock,  NEXT,
237                         KEY_E,         BOTH,  XK_E,          NEXT,
238                         KEY_L,         BOTH,  XK_L,          NEXT,
239                         KEY_L,         BOTH,  XK_L,          NEXT,
240                         KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT,
241                         KEY_O,         BOTH,  XK_O,          FINISH));
242
243     assert(test_key_seq(keymap,
244                         KEY_H,         BOTH,  XK_h,          NEXT,
245                         KEY_E,         BOTH,  XK_e,          NEXT,
246                         KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT,
247                         KEY_L,         BOTH,  XK_l,          NEXT,
248                         KEY_L,         BOTH,  XK_l,          NEXT,
249                         KEY_O,         BOTH,  XK_o,          FINISH));
250
251     /*
252      * A key release affecting a locked modifier should clear it
253      * regardless of the key press.
254      */
255     /* assert(test_key_seq(keymap, */
256     /*                     KEY_H,         BOTH,  XK_h,          NEXT, */
257     /*                     KEY_CAPSLOCK,  DOWN,  XK_Caps_Lock,  NEXT, */
258     /*                     KEY_E,         BOTH,  XK_E,          NEXT, */
259     /*                     KEY_L,         BOTH,  XK_L,          NEXT, */
260     /*                     KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT, */
261     /*                     KEY_L,         BOTH,  XK_L,          NEXT, */
262     /*                     KEY_CAPSLOCK,  UP,    XK_Caps_Lock,  NEXT, */
263     /*                     KEY_O,         BOTH,  XK_o,          FINISH)); */
264
265     /* Simple Num Lock sanity check. */
266     assert(test_key_seq(keymap,
267                         KEY_KP1,      BOTH,  XK_KP_End,    NEXT,
268                         KEY_NUMLOCK,  BOTH,  XK_Num_Lock,  NEXT,
269                         KEY_KP1,      BOTH,  XK_KP_1,      NEXT,
270                         KEY_KP2,      BOTH,  XK_KP_2,      NEXT,
271                         KEY_NUMLOCK,  BOTH,  XK_Num_Lock,  NEXT,
272                         KEY_KP2,      BOTH,  XK_KP_Down,   FINISH));
273
274     xkb_map_unref(keymap);
275     xkb_context_unref(ctx);
276     return 0;
277 }