2012-03-20 20:20:07 -06:00
|
|
|
/*
|
|
|
|
* Copyright © 2012 Intel Corporation
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Author: Daniel Stone <daniel@fooishbar.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
#include <linux/input.h>
|
2012-03-30 17:44:39 -06:00
|
|
|
|
2012-03-20 20:20:07 -06:00
|
|
|
#include "xkbcommon/xkbcommon.h"
|
|
|
|
#include "XKBcommonint.h"
|
|
|
|
|
|
|
|
/* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
|
|
|
|
* keycode set (where ESC is 9). */
|
|
|
|
#define EVDEV_OFFSET 8
|
|
|
|
|
2012-03-21 09:25:32 -06:00
|
|
|
static void
|
|
|
|
print_state(struct xkb_state *state)
|
|
|
|
{
|
|
|
|
xkb_group_index_t group;
|
|
|
|
xkb_mod_index_t mod;
|
2012-03-22 08:32:53 -06:00
|
|
|
xkb_led_index_t led;
|
2012-03-21 09:25:32 -06:00
|
|
|
|
2012-03-22 08:32:53 -06:00
|
|
|
if (!state->group && !state->mods && !state->leds) {
|
2012-03-21 09:25:32 -06:00
|
|
|
fprintf(stderr, "\tno state\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
|
2012-03-22 08:32:53 -06:00
|
|
|
if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
|
2012-03-21 09:25:32 -06:00
|
|
|
continue;
|
|
|
|
fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
|
|
|
|
xkb_map_group_get_name(state->xkb, group),
|
|
|
|
group,
|
2012-03-22 08:32:53 -06:00
|
|
|
xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
|
|
|
|
"effective " : "",
|
|
|
|
xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
|
|
|
|
"depressed " : "",
|
|
|
|
xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
|
|
|
|
"latched " : "",
|
|
|
|
xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
|
|
|
|
"locked " : "");
|
2012-03-21 09:25:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
|
2012-03-22 08:32:53 -06:00
|
|
|
if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
|
2012-03-21 09:25:32 -06:00
|
|
|
continue;
|
|
|
|
fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
|
|
|
|
xkb_map_mod_get_name(state->xkb, mod),
|
|
|
|
mod,
|
2012-03-22 08:32:53 -06:00
|
|
|
xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
|
|
|
|
"depressed " : "",
|
|
|
|
xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
|
|
|
|
"latched " : "",
|
|
|
|
xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
|
|
|
|
"locked " : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
|
|
|
|
if (!xkb_state_led_index_is_active(state, led))
|
|
|
|
continue;
|
|
|
|
fprintf(stderr, "\tled %s (%d): active\n",
|
|
|
|
xkb_map_led_get_name(state->xkb, led),
|
|
|
|
led);
|
2012-03-21 09:25:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 05:07:57 -06:00
|
|
|
static void
|
2012-04-03 08:14:16 -06:00
|
|
|
test_update_key(struct xkb_keymap *xkb)
|
2012-03-20 20:20:07 -06:00
|
|
|
{
|
2012-03-27 05:07:57 -06:00
|
|
|
struct xkb_state *state = xkb_state_new(xkb);
|
2012-03-30 17:44:39 -06:00
|
|
|
const xkb_keysym_t *syms;
|
2012-03-27 05:07:57 -06:00
|
|
|
int num_syms;
|
2012-03-20 20:20:07 -06:00
|
|
|
|
2012-03-27 05:07:57 -06:00
|
|
|
assert(state);
|
2012-03-20 20:20:07 -06:00
|
|
|
|
|
|
|
/* LCtrl down */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
|
2012-03-21 09:25:32 -06:00
|
|
|
fprintf(stderr, "dumping state for LCtrl down:\n");
|
|
|
|
print_state(state);
|
|
|
|
assert(xkb_state_mod_name_is_active(state, "Control",
|
|
|
|
XKB_STATE_DEPRESSED));
|
2012-03-20 20:20:07 -06:00
|
|
|
|
|
|
|
/* LCtrl + RAlt down */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
|
2012-03-21 09:25:32 -06:00
|
|
|
fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
|
|
|
|
print_state(state);
|
|
|
|
assert(xkb_state_mod_name_is_active(state, "Control",
|
|
|
|
XKB_STATE_DEPRESSED));
|
|
|
|
assert(xkb_state_mod_name_is_active(state, "Mod1",
|
|
|
|
XKB_STATE_DEPRESSED));
|
2012-03-20 20:20:07 -06:00
|
|
|
|
|
|
|
/* RAlt down */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
|
2012-03-21 09:25:32 -06:00
|
|
|
fprintf(stderr, "dumping state for RAlt down:\n");
|
|
|
|
print_state(state);
|
|
|
|
assert(!xkb_state_mod_name_is_active(state, "Control",
|
|
|
|
XKB_STATE_EFFECTIVE));
|
|
|
|
assert(xkb_state_mod_name_is_active(state, "Mod1",
|
|
|
|
XKB_STATE_DEPRESSED));
|
2012-03-20 20:20:07 -06:00
|
|
|
|
|
|
|
/* none down */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
|
2012-03-21 09:25:32 -06:00
|
|
|
assert(!xkb_state_mod_name_is_active(state, "Mod1",
|
|
|
|
XKB_STATE_EFFECTIVE));
|
2012-03-20 20:20:07 -06:00
|
|
|
|
|
|
|
/* Caps locked */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
|
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
|
2012-03-21 09:25:32 -06:00
|
|
|
fprintf(stderr, "dumping state for Caps Lock:\n");
|
|
|
|
print_state(state);
|
|
|
|
assert(xkb_state_mod_name_is_active(state, "Caps Lock",
|
|
|
|
XKB_STATE_LOCKED));
|
2012-03-22 08:32:53 -06:00
|
|
|
assert(xkb_state_led_name_is_active(state, "Caps Lock"));
|
2012-03-20 20:20:07 -06:00
|
|
|
num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
|
|
|
|
assert(num_syms == 1 && syms[0] == XK_Q);
|
|
|
|
|
|
|
|
/* Caps unlocked */
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
|
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
|
2012-03-21 09:25:32 -06:00
|
|
|
assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
|
|
|
|
XKB_STATE_EFFECTIVE));
|
2012-03-22 08:32:53 -06:00
|
|
|
assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
|
2012-03-20 20:20:07 -06:00
|
|
|
num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
|
|
|
|
assert(num_syms == 1 && syms[0] == XK_q);
|
|
|
|
|
|
|
|
xkb_state_unref(state);
|
2012-03-27 05:07:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-03 08:14:16 -06:00
|
|
|
test_serialisation(struct xkb_keymap *xkb)
|
2012-03-27 05:07:57 -06:00
|
|
|
{
|
|
|
|
struct xkb_state *state = xkb_state_new(xkb);
|
|
|
|
xkb_mod_mask_t base_mods;
|
|
|
|
xkb_mod_mask_t latched_mods;
|
|
|
|
xkb_mod_mask_t locked_mods;
|
|
|
|
xkb_mod_mask_t effective_mods;
|
|
|
|
xkb_mod_index_t caps, shift, ctrl;
|
|
|
|
xkb_group_index_t base_group = 0;
|
|
|
|
xkb_group_index_t latched_group = 0;
|
|
|
|
xkb_group_index_t locked_group = 0;
|
|
|
|
|
|
|
|
assert(state);
|
|
|
|
|
|
|
|
caps = xkb_map_mod_get_index(state->xkb, "Caps Lock");
|
|
|
|
assert(caps != XKB_MOD_INVALID);
|
|
|
|
shift = xkb_map_mod_get_index(state->xkb, "Shift");
|
|
|
|
assert(shift != XKB_MOD_INVALID);
|
|
|
|
ctrl = xkb_map_mod_get_index(state->xkb, "Control");
|
|
|
|
assert(ctrl != XKB_MOD_INVALID);
|
2012-03-20 20:20:07 -06:00
|
|
|
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
|
|
|
|
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
|
2012-03-27 05:07:57 -06:00
|
|
|
base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
|
|
|
|
assert(base_mods == 0);
|
|
|
|
latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
|
|
|
|
assert(latched_mods == 0);
|
|
|
|
locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
|
|
|
|
assert(locked_mods == (1 << caps));
|
|
|
|
effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
|
|
|
|
assert(effective_mods == locked_mods);
|
|
|
|
|
2012-03-27 05:11:45 -06:00
|
|
|
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
|
2012-03-27 05:07:57 -06:00
|
|
|
base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
|
|
|
|
assert(base_mods == (1 << shift));
|
|
|
|
latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
|
|
|
|
assert(latched_mods == 0);
|
|
|
|
locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
|
|
|
|
assert(locked_mods == (1 << caps));
|
|
|
|
effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
|
|
|
|
assert(effective_mods == (base_mods | locked_mods));
|
|
|
|
|
|
|
|
base_mods |= (1 << ctrl);
|
|
|
|
xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
|
|
|
|
base_group, latched_group, locked_group);
|
|
|
|
|
|
|
|
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
|
|
|
|
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
|
|
|
|
|
|
|
|
xkb_state_unref(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-03-30 17:44:39 -06:00
|
|
|
main(void)
|
2012-03-27 05:07:57 -06:00
|
|
|
{
|
2012-03-27 10:22:35 -06:00
|
|
|
struct xkb_context *context;
|
2012-04-03 08:14:16 -06:00
|
|
|
struct xkb_keymap *xkb;
|
2012-03-30 17:44:39 -06:00
|
|
|
struct xkb_rule_names rmlvo = {
|
|
|
|
.rules = "evdev",
|
|
|
|
.model = "pc104",
|
|
|
|
.layout = "us",
|
|
|
|
.variant = NULL,
|
|
|
|
.options = NULL,
|
|
|
|
};
|
2012-03-27 05:07:57 -06:00
|
|
|
|
2012-03-27 10:22:35 -06:00
|
|
|
context = xkb_context_new();
|
|
|
|
assert(context);
|
|
|
|
|
|
|
|
xkb = xkb_map_new_from_names(context, &rmlvo);
|
2012-03-27 05:07:57 -06:00
|
|
|
assert(xkb);
|
|
|
|
|
|
|
|
test_update_key(xkb);
|
|
|
|
test_serialisation(xkb);
|
|
|
|
|
|
|
|
xkb_map_unref(xkb);
|
2012-03-27 10:22:35 -06:00
|
|
|
xkb_context_unref(context);
|
2012-03-20 20:20:07 -06:00
|
|
|
}
|