-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_bison.y
267 lines (243 loc) · 8.33 KB
/
config_bison.y
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
%{
#include <stdio.h>
#include <strings.h>
#include "parser.h"
int edslex (void);
void edserror (char const *);
extern t_config config;
%}
%debug
%error-verbose
%union {
char *str;
int num;
t_name_def *name;
t_op *op;
}
%token TOKEN_BUTTON
%token TOKEN_AXIS
%token TOKEN_NEW_AXIS
%token <str> TOKEN_STRING
%token TOKEN_EQ
%token <num> TOKEN_NUMBER
%token TOKEN_IF
%token TOKEN_LPAREN
%token TOKEN_RPAREN
%token TOKEN_LBRACE
%token TOKEN_RBRACE
%token TOKEN_COMA
%token TOKEN_DEVICE
%token TOKEN_GRAB
%token TOKEN_AVOID
%type <name> condition
%type <op> condition_block map_item maps
%type <state> condition_spec
%%
input: /* empty */
| input line
;
line: device_spec
|name_def
| axis_map
| condition_spec
| avoid_spec
;
avoid_spec: TOKEN_AVOID avoid_list
;
avoid_list: TOKEN_BUTTON TOKEN_STRING {
int btn = find_button_number($2);
if(btn >= 0){
printf("Avoid %s.\n", $2);
mark_ctrl(config.virtual_btn_array, BUTTON_ARRAY_LEN, btn, BUTTON_MIN, BUTTON_MAX, -2);
}
}
| avoid_list TOKEN_COMA TOKEN_BUTTON TOKEN_STRING {
int btn = find_button_number($4);
if(btn >= 0){
printf("Avoid %s.\n", $4);
mark_ctrl(config.virtual_btn_array, BUTTON_ARRAY_LEN, btn, BUTTON_MIN, BUTTON_MAX, -2);
}
}
device_spec: TOKEN_DEVICE TOKEN_STRING {
config.device = $2;
config.grab = false;
}
| TOKEN_GRAB TOKEN_DEVICE TOKEN_STRING {
config.device = $3;
config.grab = true;
}
name_def: TOKEN_BUTTON TOKEN_STRING TOKEN_EQ TOKEN_NUMBER {
t_name_def *tmp = (t_name_def*)malloc(sizeof(t_name_def));
tmp->next = config.ctrl_maps;
config.ctrl_maps = tmp;
tmp->type = BUTTON;
tmp->name = $2;
tmp->val = $4;
}
| TOKEN_AXIS TOKEN_STRING TOKEN_EQ TOKEN_NUMBER {
t_name_def *tmp = (t_name_def*)malloc(sizeof(t_name_def));
tmp->next = config.ctrl_maps;
config.ctrl_maps = tmp;
tmp->type = AXIS;
tmp->name = $2;
tmp->val = $4;
}
;
axis_map: TOKEN_AXIS TOKEN_STRING {
t_ctrl_type type;
int num = find_control($2, &type);
if((num >= 0) && (type == AXIS)){
t_name_def *name = (t_name_def*)malloc(sizeof(t_name_def));
name->type = AXIS;
name->name = $2;
name->val = num;
name->next = NULL;
t_op *op = (t_op*)malloc(sizeof(t_op));
op->next = config.axis_maps;
op->source = name;
op->map_type = AXIS_2_BUTTON;
op->map.axis_map.button_neg = 0;
op->map.axis_map.button_pos = 0;
config.axis_maps = op;
}else{
printf("Axis remapping is available only for axes.\n");
}
}
;
condition_spec: TOKEN_IF condition condition_block {
if(($3 != NULL) && ($2 != NULL)){
//Check if the condition is not used already
t_state *tmp = config.state;
bool found = false;
while(tmp){
if(tmp->condition->val == $2->val){
//Condition exists already, merge the oprations
t_op *op = tmp->ops;
while(op){
if(op->next == NULL){
//found the tail, append the new part
op->next = $3;
found = true;
break;
}else{
op = op->next;
}
}
if(found){
break;
}
}
tmp = tmp->next;
}
if(!found){
t_state *tmp = (t_state*)malloc(sizeof(t_state));
tmp->next = config.state;
tmp->condition = $2;
tmp->ops = $3;
config.state = tmp;
}else{
free($2->name);
free($2);
}
}else{
if($2 != NULL){
free($2->name);
free($2);
}
}
}
;
condition: TOKEN_LPAREN TOKEN_STRING TOKEN_RPAREN {
t_ctrl_type type;
int num = find_control($2, &type);
if(num < 0){
$$ = NULL;
}else{
t_name_def *name = (t_name_def*)malloc(sizeof(t_name_def));
name->type = BUTTON;
name->name = $2;
name->val = num;
name->next = NULL;
$$ = name;
}
}
;
condition_block: TOKEN_LBRACE maps TOKEN_RBRACE {
$$ = $2;
}
| TOKEN_LBRACE TOKEN_RBRACE {
$$ = NULL;
}
;
maps: map_item {
$$ = $1;
}
| maps map_item {
$$ = $2;
$$->next = $1;
}
;
map_item: TOKEN_BUTTON TOKEN_STRING {
t_ctrl_type type;
int num = find_control($2, &type);
if((num < 0) || (type != BUTTON)){
free($2);
$$ = NULL;
}else{
t_name_def *name = (t_name_def*)malloc(sizeof(t_name_def));
name->type = type;
name->name = $2;
name->val = num;
name->next = NULL;
t_op *op = (t_op*)malloc(sizeof(t_op));
op->next = NULL;
op->source = name;
op->map_type = BUTTON_2_BUTTON;
op->map.target = 0;
$$ = op;
}
}
| TOKEN_AXIS TOKEN_STRING /* axis name */ {
t_ctrl_type type;
int num = find_control($2, &type);
if(num < 0){
free($2);
$$ = NULL;
}else{
t_name_def *name = (t_name_def*)malloc(sizeof(t_name_def));
name->type = type;
name->name = $2;
name->val = num;
name->next = NULL;
t_op *op = (t_op*)malloc(sizeof(t_op));
op->next = NULL;
op->source = name;
op->map_type = AXIS_2_BUTTON;
op->map.axis_map.button_neg = 0;
op->map.axis_map.button_pos = 0;
$$ = op;
}
}
| TOKEN_NEW_AXIS TOKEN_STRING /* axis name */ {
t_ctrl_type type;
int num = find_control($2, &type);
if(num < 0){
free($2);
$$ = NULL;
}else{
t_name_def *name = (t_name_def*)malloc(sizeof(t_name_def));
name->type = type;
name->name = $2;
name->val = num;
name->next = NULL;
t_op *op = (t_op*)malloc(sizeof(t_op));
op->next = NULL;
op->source = name;
op->map_type = AXIS_2_AXIS;
op->map.target = 0;
$$ = op;
}
}
// | condition_spec
;
%%