state: move filter initialization to the dispatcher This removes all of the boilerplate from the *_new functions, and leaves them just as simple functions which perform the effect of the action on state. Signed-off-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
diff --git a/src/state.c b/src/state.c
index 5415b05..4033e49 100644
--- a/src/state.c
+++ b/src/state.c
@@ -182,25 +182,17 @@ xkb_filter_group_set_func(struct xkb_state *state,
return 1;
}
-static int
-xkb_filter_group_set_new(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action)
+static void
+xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
{
- filter->key = key;
- filter->func = xkb_filter_group_set_func;
- filter->action = *action;
-
- if (action->group.flags & ACTION_ABSOLUTE_SWITCH) {
+ if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH) {
+ xkb_group_index_t tmp = filter->action.group.group;
filter->action.group.group = state->base_group;
- state->base_group = action->group.group;
+ state->base_group = tmp;
}
else {
- state->base_group += action->group.group;
+ state->base_group += filter->action.group.group;
}
-
- return 1;
}
static int
@@ -223,22 +215,13 @@ xkb_filter_group_lock_func(struct xkb_state *state,
return 1;
}
-static int
-xkb_filter_group_lock_new(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action)
+static void
+xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
{
- filter->key = key;
- filter->func = xkb_filter_group_lock_func;
- filter->action = *action;
-
- if (action->group.flags & ACTION_ABSOLUTE_SWITCH)
- state->locked_group = action->group.group;
+ if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
+ state->locked_group = filter->action.group.group;
else
- state->locked_group += action->group.group;
-
- return 1;
+ state->locked_group += filter->action.group.group;
}
static int
@@ -268,19 +251,10 @@ xkb_filter_mod_set_func(struct xkb_state *state,
return 1;
}
-static int
-xkb_filter_mod_set_new(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action)
+static void
+xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
{
- filter->key = key;
- filter->func = xkb_filter_mod_set_func;
- filter->action = *action;
-
- state->set_mods = action->mods.mods.mask;
-
- return 1;
+ state->set_mods = filter->action.mods.mods.mask;
}
static int
@@ -305,20 +279,11 @@ xkb_filter_mod_lock_func(struct xkb_state *state,
return 1;
}
-static int
-xkb_filter_mod_lock_new(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action)
+static void
+xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
{
- filter->key = key;
- filter->func = xkb_filter_mod_lock_func;
- filter->action = *action;
- filter->priv = state->locked_mods & action->mods.mods.mask;
-
- state->locked_mods |= action->mods.mods.mask;
-
- return 1;
+ filter->priv = state->locked_mods & filter->action.mods.mods.mask;
+ state->locked_mods |= filter->action.mods.mods.mask;
}
enum xkb_key_latch_state {
@@ -425,36 +390,28 @@ xkb_filter_mod_latch_func(struct xkb_state *state,
return 1;
}
-static int
-xkb_filter_mod_latch_new(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action)
-{
- enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
-
- filter->key = key;
- filter->priv = latch;
- filter->func = xkb_filter_mod_latch_func;
- filter->action = *action;
-
- state->set_mods = action->mods.mods.mask;
-
- return 1;
-}
-
-typedef int (*filter_action_new_func_t)(struct xkb_state *state,
- struct xkb_filter *filter,
- const struct xkb_key *key,
- const union xkb_action *action);
-
-static const filter_action_new_func_t
-filter_action_new_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
- [ACTION_TYPE_MOD_SET] = xkb_filter_mod_set_new,
- [ACTION_TYPE_MOD_LATCH] = xkb_filter_mod_latch_new,
- [ACTION_TYPE_MOD_LOCK] = xkb_filter_mod_lock_new,
- [ACTION_TYPE_GROUP_SET] = xkb_filter_group_set_new,
- [ACTION_TYPE_GROUP_LOCK] = xkb_filter_group_lock_new,
+static void
+xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
+{
+ filter->priv = LATCH_KEY_DOWN;
+ state->set_mods = filter->action.mods.mods.mask;
+}
+
+static const struct {
+ void (*new)(struct xkb_state *state, struct xkb_filter *filter);
+ int (*func)(struct xkb_state *state, struct xkb_filter *filter,
+ const struct xkb_key *key, enum xkb_key_direction direction);
+} filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
+ [ACTION_TYPE_MOD_SET] = { xkb_filter_mod_set_new,
+ xkb_filter_mod_set_func },
+ [ACTION_TYPE_MOD_LATCH] = { xkb_filter_mod_latch_new,
+ xkb_filter_mod_latch_func },
+ [ACTION_TYPE_MOD_LOCK] = { xkb_filter_mod_lock_new,
+ xkb_filter_mod_lock_func },
+ [ACTION_TYPE_GROUP_SET] = { xkb_filter_group_set_new,
+ xkb_filter_group_set_func },
+ [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
+ xkb_filter_group_lock_func },
};
/**
@@ -468,7 +425,7 @@ xkb_filter_apply_all(struct xkb_state *state,
enum xkb_key_direction direction)
{
struct xkb_filter *filter;
- const union xkb_action *act = NULL;
+ const union xkb_action *action;
int send = 1;
/* First run through all the currently active filters and see if any of
@@ -482,16 +439,18 @@ xkb_filter_apply_all(struct xkb_state *state,
if (!send || direction == XKB_KEY_UP)
return;
- act = xkb_key_get_action(state, key);
- if (filter_action_new_funcs[act->type]) {
- filter = xkb_filter_new(state);
- if (!filter)
- goto err;
- send = filter_action_new_funcs[act->type](state, filter, key, act);
- }
+ action = xkb_key_get_action(state, key);
+ if (!filter_action_funcs[action->type].new)
+ return;
-err:
- return;
+ filter = xkb_filter_new(state);
+ if (!filter)
+ return; /* WSGO */
+
+ filter->key = key;
+ filter->func = filter_action_funcs[action->type].func;
+ filter->action = *action;
+ filter_action_funcs[action->type].new(state, filter);
}
XKB_EXPORT struct xkb_state *