Hash :
cfd978b8
Author :
Date :
2012-08-02T00:40:22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*
* Copyright © 2012 Ran Benita <ran234@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <linux/input.h>
#include "xkbcommon/xkbcommon.h"
#include "test.h"
enum {
DOWN,
UP,
BOTH,
NEXT,
FINISH,
};
#define EVDEV_OFFSET 8
/*
* Test a sequence of keysyms, resulting from a sequence of key presses,
* against the keysyms they're supposed to generate.
*
* - Each test runs with a clean state.
* - Each line in the test is made up of:
* + A keycode, given as a KEY_* from linux/input.h.
* + A direction - DOWN for press, UP for release, BOTH for
* immediate press + release.
* + A sequence of keysyms that should result from this keypress.
*
* The vararg format is:
* <KEY_*> <DOWN | UP | BOTH> <XKB_KEY_* (zero or more)> <NEXT | FINISH>
*
* See below for examples.
*/
static int
test_key_seq(struct xkb_keymap *keymap, ...)
{
struct xkb_state *state;
va_list ap;
xkb_keycode_t kc;
int op;
xkb_keysym_t keysym;
const xkb_keysym_t *syms;
unsigned int nsyms, i;
char ksbuf[64];
state = xkb_state_new(keymap);
assert(state);
va_start(ap, keymap);
for (;;) {
kc = va_arg(ap, int) + EVDEV_OFFSET;
op = va_arg(ap, int);
nsyms = xkb_key_get_syms(state, kc, &syms);
fprintf(stderr, "got %d syms for key 0x%x: [", nsyms, kc);
if (op == DOWN || op == BOTH)
xkb_state_update_key(state, kc, XKB_KEY_DOWN);
if (op == UP || op == BOTH)
xkb_state_update_key(state, kc, XKB_KEY_UP);
for (i = 0; i < nsyms; i++) {
keysym = va_arg(ap, int);
xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
fprintf(stderr, "%s%s", (i != 0) ? ", " : "", ksbuf);
if (keysym == FINISH || keysym == NEXT) {
xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
fprintf(stderr, "Did not expect keysym: %s.\n", ksbuf);
goto fail;
}
if (keysym != syms[i]) {
xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
fprintf(stderr, "Expected keysym: %s. ", ksbuf);;
xkb_keysym_get_name(syms[i], ksbuf, sizeof(ksbuf));
fprintf(stderr, "Got keysym: %s.\n", ksbuf);;
goto fail;
}
}
fprintf(stderr, "]\n");
keysym = va_arg(ap, int);
if (keysym == NEXT)
continue;
if (keysym == FINISH)
break;
xkb_keysym_get_name(keysym, ksbuf, sizeof(ksbuf));
fprintf(stderr, "Expected keysym: %s. Didn't get it.\n", ksbuf);
goto fail;
}
va_end(ap);
xkb_state_unref(state);
return 1;
fail:
va_end(ap);
xkb_state_unref(state);
return 0;
}
int
main(void)
{
struct xkb_context *ctx = test_get_context();
struct xkb_keymap *keymap;
assert(ctx);
keymap = test_compile_rules(ctx, "evdev", "evdev", "us,il", NULL,
"grp:alt_shift_toggle,grp:menu_toggle");
assert(keymap);
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_E, BOTH, XKB_KEY_e, NEXT,
KEY_L, BOTH, XKB_KEY_l, NEXT,
KEY_L, BOTH, XKB_KEY_l, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
KEY_L, BOTH, XKB_KEY_l, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
/* Base modifier cleared on key release... */
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_O, BOTH, XKB_KEY_O, FINISH));
/* ... But only by the keycode that set it. */
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_O, BOTH, XKB_KEY_O, FINISH));
/*
* A base modifier should only be cleared when no other key affecting
* the modifier is down.
*/
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_RIGHTSHIFT, DOWN, XKB_KEY_Shift_R, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_RIGHTSHIFT, UP, XKB_KEY_Shift_R, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
/* Group switching / locking. */
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_E, BOTH, XKB_KEY_e, NEXT,
KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
KEY_K, BOTH, XKB_KEY_hebrew_lamed, NEXT,
KEY_F, BOTH, XKB_KEY_hebrew_kaph, NEXT,
KEY_COMPOSE, BOTH, XKB_KEY_ISO_Next_Group, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
assert(test_key_seq(keymap,
KEY_LEFTSHIFT, DOWN, XKB_KEY_Shift_L, NEXT,
KEY_LEFTALT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
KEY_LEFTALT, UP, XKB_KEY_ISO_Next_Group, NEXT,
KEY_LEFTSHIFT, UP, XKB_KEY_Shift_L, FINISH));
assert(test_key_seq(keymap,
KEY_LEFTALT, DOWN, XKB_KEY_Alt_L, NEXT,
KEY_LEFTSHIFT, DOWN, XKB_KEY_ISO_Next_Group, NEXT,
KEY_LEFTSHIFT, UP, XKB_KEY_ISO_Next_Group, NEXT,
KEY_LEFTALT, UP, XKB_KEY_Alt_L, FINISH));
/* Locked modifiers. */
assert(test_key_seq(keymap,
KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
KEY_H, BOTH, XKB_KEY_H, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_O, BOTH, XKB_KEY_O, FINISH));
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_E, BOTH, XKB_KEY_e, NEXT,
KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_CAPSLOCK, BOTH, XKB_KEY_Caps_Lock, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT,
KEY_E, BOTH, XKB_KEY_E, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_L, BOTH, XKB_KEY_L, NEXT,
KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
KEY_O, BOTH, XKB_KEY_O, FINISH));
assert(test_key_seq(keymap,
KEY_H, BOTH, XKB_KEY_h, NEXT,
KEY_E, BOTH, XKB_KEY_e, NEXT,
KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT,
KEY_L, BOTH, XKB_KEY_l, NEXT,
KEY_L, BOTH, XKB_KEY_l, NEXT,
KEY_O, BOTH, XKB_KEY_o, FINISH));
/*
* A key release affecting a locked modifier should clear it
* regardless of the key press.
*/
/* assert(test_key_seq(keymap, */
/* KEY_H, BOTH, XKB_KEY_h, NEXT, */
/* KEY_CAPSLOCK, DOWN, XKB_KEY_Caps_Lock, NEXT, */
/* KEY_E, BOTH, XKB_KEY_E, NEXT, */
/* KEY_L, BOTH, XKB_KEY_L, NEXT, */
/* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
/* KEY_L, BOTH, XKB_KEY_L, NEXT, */
/* KEY_CAPSLOCK, UP, XKB_KEY_Caps_Lock, NEXT, */
/* KEY_O, BOTH, XKB_KEY_o, FINISH)); */
/* Simple Num Lock sanity check. */
assert(test_key_seq(keymap,
KEY_KP1, BOTH, XKB_KEY_KP_End, NEXT,
KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
KEY_KP1, BOTH, XKB_KEY_KP_1, NEXT,
KEY_KP2, BOTH, XKB_KEY_KP_2, NEXT,
KEY_NUMLOCK, BOTH, XKB_KEY_Num_Lock, NEXT,
KEY_KP2, BOTH, XKB_KEY_KP_Down, FINISH));
xkb_map_unref(keymap);
xkb_context_unref(ctx);
return 0;
}