Add new state API
[platform/upstream/libxkbcommon.git] / test / state.c
1 /*
2  * Copyright © 2012 Intel Corporation
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  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <X11/X.h>
30 #include <X11/Xdefs.h>
31 #include <X11/keysym.h>
32 #include <linux/input.h>
33 #include "xkbcommon/xkbcommon.h"
34 #include "xkbcomp/utils.h"
35 #include "XKBcommonint.h"
36
37 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
38  * keycode set (where ESC is 9). */
39 #define EVDEV_OFFSET 8
40
41 int main(int argc, char *argv[])
42 {
43     struct xkb_rule_names rmlvo;
44     struct xkb_desc *xkb;
45     struct xkb_state *state;
46     int num_syms;
47     xkb_keysym_t *syms;
48
49     rmlvo.rules = "evdev";
50     rmlvo.model = "pc104";
51     rmlvo.layout = "us";
52     rmlvo.variant = NULL;
53     rmlvo.options = NULL;
54
55     xkb = xkb_compile_keymap_from_rules(&rmlvo);
56
57     if (!xkb) {
58         fprintf(stderr, "Failed to compile keymap\n");
59         exit(1);
60     }
61
62     state = xkb_state_new(xkb);
63     assert(state->mods == 0);
64     assert(state->group == 0);
65
66     /* LCtrl down */
67     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
68     assert(state->mods & ControlMask);
69
70     /* LCtrl + RAlt down */
71     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 1);
72     assert(state->mods & Mod1Mask);
73     assert(state->locked_mods == 0);
74     assert(state->latched_mods == 0);
75
76     /* RAlt down */
77     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0);
78     assert(!(state->mods & ControlMask) && (state->mods & Mod1Mask));
79
80     /* none down */
81     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 0);
82     assert(state->mods == 0);
83     assert(state->group == 0);
84
85     /* Caps locked */
86     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
87     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
88     assert(state->mods & LockMask);
89     assert(state->mods == state->locked_mods);
90     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
91     assert(num_syms == 1 && syms[0] == XK_Q);
92
93     /* Caps unlocked */
94     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
95     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
96     assert(state->mods == 0);
97     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
98     assert(num_syms == 1 && syms[0] == XK_q);
99
100     xkb_state_unref(state);
101     xkb_free_keymap(xkb);
102
103     return 0;
104 }