-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathops.c
129 lines (115 loc) · 2.85 KB
/
ops.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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ops.h"
#include "parser.h"
#include "ctrl_defs.h"
char *find_axis_name(int ctrl)
{
int i = 0;
while(axis_map[i].ctrl >= 0){
if(axis_map[i].ctrl == ctrl){
return strdup(axis_map[i].desc);
}
++i;
}
char *new_name = NULL;
if(asprintf(&new_name, "ABS_%03X", ctrl) < 0){
printf("Asprintf failed in find_axis_name.\n");
return NULL;
}
return new_name;
}
char *find_button_name(int ctrl)
{
int i = 0;
while(button_map[i].ctrl >= 0){
if(button_map[i].ctrl == ctrl){
return strdup(button_map[i].desc);
}
++i;
}
char *new_name = NULL;
if(asprintf(&new_name, "BUTTON_%02X", ctrl) < 0){
printf("Asprintf failed in find_button_name.\n");
return(NULL);
}
return new_name;
}
int find_button_number(const char *name)
{
int i = 0;
while(button_map[i].ctrl >= 0){
if(strcmp(button_map[i].desc, name) == 0){
return button_map[i].ctrl;
}
++i;
}
return -1;
}
void print_name_def(t_name_def *v, const char *prefix)
{
printf("%s%s %s => %d\n", prefix, (v->type == AXIS)?"Axis":"Button", v->name, v->val);
}
void print_op(t_op *op, const char *prefix)
{
switch(op->source->type){
case BUTTON:
printf("%s Button %s (%d) will be mapped to %s (%d).\n", prefix, op->source->name,
op->source->val, find_button_name(op->map.target), op->map.target);
break;
case AXIS:
if(op->map_type == AXIS_2_BUTTON){
printf("%s Axis %s (%d) will be mapped to buttons %s (%d)/ %s (%d).\n", prefix, op->source->name,
op->source->val,
find_button_name(op->map.axis_map.button_neg), op->map.axis_map.button_neg,
find_button_name(op->map.axis_map.button_pos), op->map.axis_map.button_pos);
}else{
printf("%s Axis %s (%d) will be mapped to axis %s (%d).\n", prefix, op->source->name,
op->source->val, find_axis_name(op->map.target), op->map.target);
}
break;
}
}
void print_config(void)
{
printf("Device: '%s'.\n", config.device);
t_name_def *tmp = config.ctrl_maps;
printf("Names defined:\n");
while(tmp != NULL){
print_name_def(tmp, "Name def: ");
tmp = tmp->next;
}
t_state *tmp1 = config.state;
t_op *tmp2;
while(tmp1 != NULL){
printf("State:\n");
print_name_def(tmp1->condition, " ");
tmp2 = tmp1->ops;
while(tmp2 != NULL){
print_op(tmp2, " ");
tmp2 = tmp2->next;
}
tmp1 = tmp1->next;
}
printf("Axis Maps:\n");
tmp2 = config.axis_maps;
while(tmp2 != NULL){
print_op(tmp2, " ");
tmp2 = tmp2->next;
}
}
#ifdef STANDALONE
int main(int argc, char *argv[])
{
if(argc != 2){
printf("Specify a file to parse.\n");
return 0;
}
parse_config(argv[1]);
print_config();
clean_up_config();
return 0;
}
#endif