Add enum xkb_key_direction instead of bool

Use XKB_KEY_UP instead of 0 and XKB_KEY_DOWN instead of 1.

Signed-off-by: Daniel Stone <daniel@fooishbar.org>
Reported-by: Ran Benita <ran234@gmail.com>
master
Daniel Stone 2012-03-27 12:11:45 +01:00
parent 7f471a702e
commit ede8473490
3 changed files with 37 additions and 28 deletions

View File

@ -716,11 +716,17 @@ xkb_state_ref(struct xkb_state *state);
_X_EXPORT void _X_EXPORT void
xkb_state_unref(struct xkb_state *state); xkb_state_unref(struct xkb_state *state);
enum xkb_key_direction {
XKB_KEY_UP,
XKB_KEY_DOWN,
};
/** /**
* Updates a state object to reflect the given key being pressed or released. * Updates a state object to reflect the given key being pressed or released.
*/ */
_X_EXPORT void _X_EXPORT void
xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down); xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
enum xkb_key_direction direction);
/** /**
* Modifier and group types for state objects. This enum is bitmaskable, * Modifier and group types for state objects. This enum is bitmaskable,

View File

@ -72,7 +72,8 @@ struct xkb_filter {
union xkb_action action; union xkb_action action;
xkb_keycode_t keycode; xkb_keycode_t keycode;
uint32_t priv; uint32_t priv;
int (*func)(struct xkb_filter *filter, xkb_keycode_t key, int down); int (*func)(struct xkb_filter *filter, xkb_keycode_t key,
enum xkb_key_direction direction);
int refcnt; int refcnt;
struct xkb_filter *next; struct xkb_filter *next;
}; };
@ -134,14 +135,14 @@ xkb_filter_new(struct xkb_state *state)
static int static int
xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode, xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
int down) enum xkb_key_direction direction)
{ {
if (keycode != filter->keycode) { if (keycode != filter->keycode) {
filter->action.group.flags &= ~XkbSA_ClearLocks; filter->action.group.flags &= ~XkbSA_ClearLocks;
return 1; return 1;
} }
if (down) { if (direction == XKB_KEY_DOWN) {
filter->refcnt++; filter->refcnt++;
return 0; return 0;
} }
@ -186,14 +187,14 @@ xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t keycode,
static int static int
xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode, xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
int down) enum xkb_key_direction direction)
{ {
if (keycode != filter->keycode) { if (keycode != filter->keycode) {
filter->action.mods.flags &= ~XkbSA_ClearLocks; filter->action.mods.flags &= ~XkbSA_ClearLocks;
return 1; return 1;
} }
if (down) { if (direction == XKB_KEY_DOWN) {
filter->refcnt++; filter->refcnt++;
return 0; return 0;
} }
@ -229,12 +230,12 @@ xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t keycode,
static int static int
xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode, xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
int down) enum xkb_key_direction direction)
{ {
if (keycode != filter->keycode) if (keycode != filter->keycode)
return 1; return 1;
if (down) { if (direction == XKB_KEY_DOWN) {
filter->refcnt++; filter->refcnt++;
return 0; return 0;
} }
@ -272,11 +273,11 @@ enum xkb_key_latch_state {
static int static int
xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode, xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
int down) enum xkb_key_direction direction)
{ {
enum xkb_key_latch_state latch = filter->priv; enum xkb_key_latch_state latch = filter->priv;
if (down && latch == LATCH_PENDING) { if (direction == XKB_KEY_DOWN && latch == LATCH_PENDING) {
/* If this is a new keypress and we're awaiting our single latched /* If this is a new keypress and we're awaiting our single latched
* keypress, then either break the latch if any random key is pressed, * keypress, then either break the latch if any random key is pressed,
* or promote it to a lock or plain base set if it's the same * or promote it to a lock or plain base set if it's the same
@ -309,7 +310,7 @@ xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
return 1; return 1;
} }
} }
else if (!down && keycode == filter->keycode) { else if (direction == XKB_KEY_UP && keycode == filter->keycode) {
/* Our key got released. If we've set it to clear locks, and we /* Our key got released. If we've set it to clear locks, and we
* currently have the same modifiers locked, then release them and * currently have the same modifiers locked, then release them and
* don't actually latch. Else we've actually hit the latching * don't actually latch. Else we've actually hit the latching
@ -335,7 +336,7 @@ xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
/* XXX beep beep! */ /* XXX beep beep! */
} }
} }
else if (down && latch == LATCH_KEY_DOWN) { else if (direction == XKB_KEY_DOWN && latch == LATCH_KEY_DOWN) {
/* Someone's pressed another key while we've still got the latching /* Someone's pressed another key while we've still got the latching
* key held down, so keep the base modifier state active (from * key held down, so keep the base modifier state active (from
* xkb_filter_mod_latch_new), but don't trip the latch, just clear * xkb_filter_mod_latch_new), but don't trip the latch, just clear
@ -373,7 +374,8 @@ xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t keycode,
* apply a new filter from the key action. * apply a new filter from the key action.
*/ */
static void static void
xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key, int down) xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key,
enum xkb_key_direction direction)
{ {
struct xkb_filter *filters = state->filters; struct xkb_filter *filters = state->filters;
union xkb_action *act = NULL; union xkb_action *act = NULL;
@ -385,10 +387,10 @@ xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t key, int down)
for (i = 0; i < state->num_filters; i++) { for (i = 0; i < state->num_filters; i++) {
if (!filters[i].func) if (!filters[i].func)
continue; continue;
send &= (*filters[i].func)(&filters[i], key, down); send &= (*filters[i].func)(&filters[i], key, direction);
} }
if (!send || !down) if (!send || direction == XKB_KEY_UP)
return; return;
act = xkb_key_get_action(state, key); act = xkb_key_get_action(state, key);
@ -519,9 +521,10 @@ xkb_state_update_derived(struct xkb_state *state)
* new modifiers. * new modifiers.
*/ */
void void
xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down) xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key,
enum xkb_key_direction direction)
{ {
xkb_filter_apply_all(state, key, down); xkb_filter_apply_all(state, key, direction);
xkb_state_update_derived(state); xkb_state_update_derived(state);
} }

View File

@ -99,14 +99,14 @@ test_update_key(struct xkb_desc *xkb)
assert(state); assert(state);
/* LCtrl down */ /* LCtrl down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
fprintf(stderr, "dumping state for LCtrl down:\n"); fprintf(stderr, "dumping state for LCtrl down:\n");
print_state(state); print_state(state);
assert(xkb_state_mod_name_is_active(state, "Control", assert(xkb_state_mod_name_is_active(state, "Control",
XKB_STATE_DEPRESSED)); XKB_STATE_DEPRESSED));
/* LCtrl + RAlt down */ /* LCtrl + RAlt down */
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
fprintf(stderr, "dumping state for LCtrl + RAlt down:\n"); fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
print_state(state); print_state(state);
assert(xkb_state_mod_name_is_active(state, "Control", assert(xkb_state_mod_name_is_active(state, "Control",
@ -115,7 +115,7 @@ test_update_key(struct xkb_desc *xkb)
XKB_STATE_DEPRESSED)); XKB_STATE_DEPRESSED));
/* RAlt down */ /* RAlt down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0); xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for RAlt down:\n"); fprintf(stderr, "dumping state for RAlt down:\n");
print_state(state); print_state(state);
assert(!xkb_state_mod_name_is_active(state, "Control", assert(!xkb_state_mod_name_is_active(state, "Control",
@ -124,13 +124,13 @@ test_update_key(struct xkb_desc *xkb)
XKB_STATE_DEPRESSED)); XKB_STATE_DEPRESSED));
/* none down */ /* none down */
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 0); xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
assert(!xkb_state_mod_name_is_active(state, "Mod1", assert(!xkb_state_mod_name_is_active(state, "Mod1",
XKB_STATE_EFFECTIVE)); XKB_STATE_EFFECTIVE));
/* Caps locked */ /* Caps locked */
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for Caps Lock:\n"); fprintf(stderr, "dumping state for Caps Lock:\n");
print_state(state); print_state(state);
assert(xkb_state_mod_name_is_active(state, "Caps Lock", assert(xkb_state_mod_name_is_active(state, "Caps Lock",
@ -140,8 +140,8 @@ test_update_key(struct xkb_desc *xkb)
assert(num_syms == 1 && syms[0] == XK_Q); assert(num_syms == 1 && syms[0] == XK_Q);
/* Caps unlocked */ /* Caps unlocked */
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
assert(!xkb_state_mod_name_is_active(state, "Caps Lock", assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
XKB_STATE_EFFECTIVE)); XKB_STATE_EFFECTIVE));
assert(!xkb_state_led_name_is_active(state, "Caps Lock")); assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
@ -173,8 +173,8 @@ test_serialisation(struct xkb_desc *xkb)
ctrl = xkb_map_mod_get_index(state->xkb, "Control"); ctrl = xkb_map_mod_get_index(state->xkb, "Control");
assert(ctrl != XKB_MOD_INVALID); assert(ctrl != XKB_MOD_INVALID);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0); xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED); base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
assert(base_mods == 0); assert(base_mods == 0);
latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED); latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
@ -184,7 +184,7 @@ test_serialisation(struct xkb_desc *xkb)
effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE); effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
assert(effective_mods == locked_mods); assert(effective_mods == locked_mods);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, 1); xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED); base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
assert(base_mods == (1 << shift)); assert(base_mods == (1 << shift));
latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED); latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);