-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
201 lines (174 loc) · 5.63 KB
/
config.c
File metadata and controls
201 lines (174 loc) · 5.63 KB
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
int config_artSubUni(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->art_subUni = strtoul(value, NULL, 10);
return 0;
}
int config_dmxAddress(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
//do not override if set by commandline argument
if(config->dmx_address > 0){
return 0;
}
config->dmx_address = strtoul(value, NULL, 10);
if(config->dmx_address == 0){
fprintf(stderr, "Invalid address provided, must be between 1 and 512\n");
return 1;
}
if (config->dmx_address + DMX_CHANNELS > 512) {
fprintf(stderr, "DMX Adress is too high for this fixture (%d + %d > 512).\n", config->dmx_address, DMX_CHANNELS);
return 1;
}
return 0;
}
int config_artNet(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->art_net = strtoul(value, NULL, 10);
return 0;
}
int config_bindhost(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
if(config->bindhost){
free(config->bindhost);
}
config->bindhost = strdup(value);
return 0;
}
int config_windowed(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
int b = econfig_getBoolean(value);
if(b < 0){
return 1;
}
config->windowed = (bool)b;
return 0;
}
int config_width(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->window_width = strtoul(value, NULL, 10);
return 0;
}
int config_height(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->window_height = strtoul(value, NULL, 10);
return 0;
}
int config_x_offset(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->x_offset = strtoul(value, NULL, 10);
return 0;
}
int config_y_offset(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
config->y_offset = strtoul(value, NULL, 10);
return 0;
}
int config_gobo_folder(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
if(config->gobo_prefix){
free(config->gobo_prefix);
}
config->gobo_prefix = strdup(value);
return 0;
}
int config_remap(const char* category, char* key, char* value, EConfig* econfig, CONFIG* config){
unsigned u;
char* token = NULL;
for(u = 0; u < DMX_CHANNELS; u++){
if(value && CHANNEL_NAME[u] && !strcmp(CHANNEL_NAME[u], key)){
//FIXME can econfig handle this?
token = strtok(value, " ");
do{
if(!token){
//this should never happen
break;
}
if(!strcmp(token, "fixed")){
config->dmx_config[u].fixed = true;
token = strtok(NULL, " ");
if(token){
config->dmx_config[u].min = strtoul(token, NULL, 0);
}
else{
fprintf(stderr, "Parameter expected for keyword\n");
return -1;
}
}
else if(!strcmp(token, "source")){
token = strtok(NULL, " ");
if(token){
config->dmx_config[u].source = strtoul(token, NULL, 0);
}
else{
fprintf(stderr, "Parameter expected for keyword\n");
return -1;
}
}
else if(!strcmp(token, "min")){
token = strtok(NULL, " ");
if(token){
config->dmx_config[u].min = strtoul(token, NULL, 0);
}
else{
fprintf(stderr, "Parameter expected for keyword\n");
return -1;
}
}
else if(!strcmp(token, "max")){
token = strtok(NULL, " ");
if(token){
config->dmx_config[u].max = strtoul(token, NULL, 0);
}
else{
fprintf(stderr, "Parameter expected for keyword\n");
return -1;
}
}
else if(!strcmp(token, "inverted")){
config->dmx_config[u].inverted = true;
}
token = strtok(NULL, " ");
}
while(token);
fprintf(stderr, "Remapped channel %s\n", key);
return 0;
}
}
fprintf(stderr, "No channel with name %s found to remap\n", key);
return -1;
}
int arg_address(int argc, char** argv, CONFIG* config){
config->dmx_address = strtoul(argv[1], NULL, 10);
if (config->dmx_address == 0) {
return -1;
}
if (config->dmx_address + DMX_CHANNELS > 512) {
fprintf(stderr, "DMX Adress is too high for this fixture (%d + %d > 512).\n", config->dmx_address, DMX_CHANNELS);
return -1;
}
return 0;
}
int arg_help(int argc, char** argv, CONFIG* config){
exit(usage("xlaser"));
return 0;
}
int parse_config(CONFIG* config, char* filepath){
unsigned u;
EConfig* econfig = econfig_init(filepath, config);
unsigned artNetCat = econfig_addCategory(econfig, "artnet");
unsigned genCat = econfig_addCategory(econfig, "general");
unsigned windowCat = econfig_addCategory(econfig, "window");
unsigned remapCat = econfig_addCategory(econfig, "remap");
econfig_addParam(econfig, artNetCat, "net", config_artNet);
econfig_addParam(econfig, artNetCat, "subuni", config_artSubUni);
econfig_addParam(econfig, artNetCat, "address", config_dmxAddress);
econfig_addParam(econfig, genCat, "bindhost", config_bindhost);
econfig_addParam(econfig, genCat, "gobos", config_gobo_folder);
econfig_addParam(econfig, windowCat, "windowed", config_windowed);
econfig_addParam(econfig, windowCat, "width", config_width);
econfig_addParam(econfig, windowCat, "height", config_height);
econfig_addParam(econfig, windowCat, "x_offset", config_x_offset);
econfig_addParam(econfig, windowCat, "y_offset", config_y_offset);
for(u = 0; u < DMX_CHANNELS; u++){
if(CHANNEL_NAME[u]){
econfig_addParam(econfig, remapCat, CHANNEL_NAME[u], config_remap);
}
}
econfig_parse(econfig);
econfig_free(econfig);
return 0;
}
int parse_args(CONFIG* config, int argc, char** argv, char** output){
eargs_addArgument("-d", "--dmx", arg_address, 1);
eargs_addArgument("-h", "--help", arg_help, 0);
return eargs_parse(argc, argv, output, config);
}