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>
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
diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index b7259e6..7ce7d9e 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -716,11 +716,17 @@ xkb_state_ref(struct xkb_state *state);
_X_EXPORT void
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.
*/
_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,
diff --git a/src/state.c b/src/state.c
index 613bae2..ea5b390 100644
--- a/src/state.c
+++ b/src/state.c
@@ -72,7 +72,8 @@ struct xkb_filter {
union xkb_action action;
xkb_keycode_t keycode;
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;
struct xkb_filter *next;
};
@@ -134,14 +135,14 @@ xkb_filter_new(struct xkb_state *state)
static int
xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
- int down)
+ enum xkb_key_direction direction)
{
if (keycode != filter->keycode) {
filter->action.group.flags &= ~XkbSA_ClearLocks;
return 1;
}
- if (down) {
+ if (direction == XKB_KEY_DOWN) {
filter->refcnt++;
return 0;
}
@@ -186,14 +187,14 @@ xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t keycode,
static int
xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t keycode,
- int down)
+ enum xkb_key_direction direction)
{
if (keycode != filter->keycode) {
filter->action.mods.flags &= ~XkbSA_ClearLocks;
return 1;
}
- if (down) {
+ if (direction == XKB_KEY_DOWN) {
filter->refcnt++;
return 0;
}
@@ -229,12 +230,12 @@ xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t keycode,
static int
xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t keycode,
- int down)
+ enum xkb_key_direction direction)
{
if (keycode != filter->keycode)
return 1;
- if (down) {
+ if (direction == XKB_KEY_DOWN) {
filter->refcnt++;
return 0;
}
@@ -272,11 +273,11 @@ enum xkb_key_latch_state {
static int
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;
- 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
* 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
@@ -309,7 +310,7 @@ xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t keycode,
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
* currently have the same modifiers locked, then release them and
* 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! */
}
}
- 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
* key held down, so keep the base modifier state active (from
* 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.
*/
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;
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++) {
if (!filters[i].func)
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;
act = xkb_key_get_action(state, key);
@@ -519,9 +521,10 @@ xkb_state_update_derived(struct xkb_state *state)
* new modifiers.
*/
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);
}
diff --git a/test/state.c b/test/state.c
index 000e47f..bc02148 100644
--- a/test/state.c
+++ b/test/state.c
@@ -99,14 +99,14 @@ test_update_key(struct xkb_desc *xkb)
assert(state);
/* 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");
print_state(state);
assert(xkb_state_mod_name_is_active(state, "Control",
XKB_STATE_DEPRESSED));
/* 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");
print_state(state);
assert(xkb_state_mod_name_is_active(state, "Control",
@@ -115,7 +115,7 @@ test_update_key(struct xkb_desc *xkb)
XKB_STATE_DEPRESSED));
/* 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");
print_state(state);
assert(!xkb_state_mod_name_is_active(state, "Control",
@@ -124,13 +124,13 @@ test_update_key(struct xkb_desc *xkb)
XKB_STATE_DEPRESSED));
/* 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",
XKB_STATE_EFFECTIVE));
/* Caps locked */
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for Caps Lock:\n");
print_state(state);
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);
/* Caps unlocked */
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
XKB_STATE_EFFECTIVE));
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");
assert(ctrl != XKB_MOD_INVALID);
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
- xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
assert(base_mods == 0);
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);
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);
assert(base_mods == (1 << shift));
latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);