-
Notifications
You must be signed in to change notification settings - Fork 2
/
ev_process.c
299 lines (284 loc) · 8.71 KB
/
ev_process.c
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <stdio.h>
#include "ops.h"
#include "parser.h"
#include "ev_process.h"
int send_key_event(struct input_event *template, int key, int state)
{
struct input_event ev = *template;
ev.type = EV_KEY;
ev.code = key;
ev.value = state;
int res = send_event(&ev);
if(res > 0){
config.virtual_btn_array[key - BUTTON_MIN] = state;
}
return res;
}
t_axis2btn axis_state(struct input_event *event)
{
int axis = event->code;
float val = event->value;
t_axis_info info = config.axes[axis];
if(val > (info.center + info.hysteresis)){
return POSITIVE;
}
if(val < (info.center - info.hysteresis)){
return NEGATIVE;
}
return INACTIVE;
}
t_axis2btn axis_state_from_buttons(int neg, int pos)
{
int n = config.virtual_btn_array[neg - BUTTON_MIN];
int p = config.virtual_btn_array[pos - BUTTON_MIN];
if((n == 0) && (p == 1)){
return POSITIVE;
}else if((n == 1) && (p == 0)){
return NEGATIVE;
}
return INACTIVE;
}
int handle_axis_change(struct input_event *event, int axis, t_axis2btn new_state, int neg, int pos)
{
switch(config.axes[axis].current_state){
case INACTIVE:
config.axes[axis].current_state = new_state;
if(new_state == POSITIVE){
return send_key_event(event, pos, 1);
}else if(new_state == NEGATIVE){
return send_key_event(event, neg, 1);
}
//break; //unreachable
case POSITIVE:
config.axes[axis].current_state = new_state;
if(new_state == INACTIVE){
return send_key_event(event, pos, 0);
}else if(new_state == NEGATIVE){
int res = send_key_event(event, pos, 0);
if(res < 0){
return res;
}
return send_key_event(event, neg, 1);
}
//break; //unreachable
case NEGATIVE:
config.axes[axis].current_state = new_state;
if(new_state == POSITIVE){
int res = send_key_event(event, neg, 0);
if(res < 0){
return res;
}
return send_key_event(event, pos, 1);
}else if(new_state == INACTIVE){
return send_key_event(event, neg, 0);
}
//break; //unreachable
}
return -1; //unreachable
}
bool handle_condition_change(struct input_event *event)
{
int key = event->code;
int key_state = event->value;
//Check that the key is not being used as a condition trigger
t_state *state = config.state;
t_op *op;
while(state){
//does the event concern the state?
if(key != state->condition->val){
state = state->next;
continue;
}
//handle the condition entry/leave
op = state->ops;
while(op){
if(op->source->type == BUTTON){
//If the state of the real button differs from the state of the virtual one being mapped to,
// send out an event to get them in line
if(key_state){
//condition activated
// Target button state differs from the last record
if(config.real_btn_array[op->source->val - BUTTON_MIN] !=
config.virtual_btn_array[op->map.target - BUTTON_MIN]){
send_key_event(event, op->map.target, config.real_btn_array[op->source->val - BUTTON_MIN]);
}
}else{
//condition deactivated
if(config.real_btn_array[op->source->val - BUTTON_MIN] !=
config.virtual_btn_array[op->source->val - BUTTON_MIN]){
send_key_event(event, op->source->val, config.real_btn_array[op->source->val - BUTTON_MIN]);
}
}
}else if(op->source->type == AXIS){
if(key_state){
if(op->map_type == AXIS_2_BUTTON){
//check if axis state changed and act accordingly
if(config.axes[op->source->val].current_state !=
axis_state_from_buttons(op->map.axis_map.button_neg, op->map.axis_map.button_pos)){
t_axis2btn new_state = config.axes[op->source->val].current_state;
config.axes[op->source->val].current_state =
axis_state_from_buttons(op->map.axis_map.button_neg, op->map.axis_map.button_pos);
handle_axis_change(event, op->source->val, new_state,
op->map.axis_map.button_neg, op->map.axis_map.button_pos);
}
}else{
//AXIS_2_AXIS
// when the condition is entered, send the last value of the source axis as the new
// target axis value
struct input_event a = *event;
a.type = EV_ABS;
a.code = op->map.target;
a.value = config.axes[op->source->val].current_value;
send_event(&a);
}
}else{
//leaving the condition
t_op *ax = config.axis_maps;
bool has_mapping = false;
// check if the axis has mapping outside the condition
while(ax){
if(ax->source->val != op->source->val){
//not our axis, not interested
ax = ax->next;
continue;
}
if(config.axes[ax->source->val].current_state !=
axis_state_from_buttons(ax->map.axis_map.button_neg, ax->map.axis_map.button_pos)){
t_axis2btn new_state = config.axes[op->source->val].current_state;
config.axes[op->source->val].current_state =
axis_state_from_buttons(ax->map.axis_map.button_neg, ax->map.axis_map.button_pos);
handle_axis_change(event, ax->source->val, new_state,
ax->map.axis_map.button_neg, ax->map.axis_map.button_pos);
has_mapping = true;
}
ax = ax->next;
}
if(!has_mapping){
struct input_event a = *event;
a.type = EV_ABS;
a.code = op->source->val;
a.value = config.axes[a.code].current_value;
send_event(&a);
}
}
}
op = op->next;
}
return true;
}
return false;
}
//need two sets of states real/virtual!!!
bool process_key_event(struct input_event *event)
{
int key = event->code;
int key_state = event->value;
bool handled = false;
if((key < BUTTON_MIN) || (key > BUTTON_MAX)){
printf("Button number %d is out of bounds <%d, %d>.\n", key, BUTTON_MIN, BUTTON_MAX);
return false;
}
config.real_btn_array[key - BUTTON_MIN] = key_state;
//Check if the event doesn't trigger/release any conditions
if(handle_condition_change(event)){
return true;
}
//Go through all conditions and see if there is an action to perform
t_state *state = config.state;
t_op *op;
while(state){
if(config.real_btn_array[state->condition->val - BUTTON_MIN]){
op = state->ops;
while(op){
if((op->source->type == BUTTON) && (op->source->val == key)){
if(send_key_event(event, op->map.target, key_state) > 0){
handled = true;
}
}
op = op->next;
}
}
state = state->next;
}
//If no remapping, send out the original event
if(!handled){
send_key_event(event, key, key_state);
}
return true;
}
bool process_axis_maps(struct input_event *event, t_op *op, bool *handled)
{
int axis = event->code;
while(op){
if(op->source->val != axis){
op = op->next;
continue;
}
if(op->map_type == AXIS_2_BUTTON){
t_axis2btn new_state = axis_state(event);
if(new_state != config.axes[axis].current_state){
handle_axis_change(event, axis, new_state, op->map.axis_map.button_neg, op->map.axis_map.button_pos);
if(handled){
*handled = true;
}
}
}else{
event->code = op->map.target;
send_event(event);
if(handled){
*handled = true;
}
}
op = op->next;
}
return true;
}
bool process_axis_event(struct input_event *event)
{
int axis = event->code;
float val = event->value;
if((axis < 0) || (axis > ABS_MAX)){
printf("Axis Id %d is out of range <0, %d>.\n", axis, ABS_MAX);
return false;
}
config.axes[axis].current_value = val;
t_state *state = config.state;
bool handled = false;
while(state){
if(config.real_btn_array[state->condition->val - BUTTON_MIN]){
process_axis_maps(event, state->ops, &handled);
}
state = state->next;
}
if(handled){
return true;
}
process_axis_maps(event, config.axis_maps, &handled);
if(handled){
return true;
}
//pass through
config.axes[axis].current_value = val;
config.axes[axis].current_state = axis_state(event);
send_event(event);
return true;
}
bool process_event(struct input_event *event)
{
switch(event->type){
case EV_SYN:
send_event(event);
return true;
break;
case EV_KEY:
return process_key_event(event);
break;
case EV_ABS:
return process_axis_event(event);
break;
default:
return false;
break;
}
return false;
}