-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo.c
212 lines (190 loc) · 4.27 KB
/
algo.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
#include <algo.h>
static char mode;
static char acting;
static xmlTextReaderPtr reader;
static xmlTextWriterPtr writer;
#define startnode(s) xmlTextWriterStartElement(writer, (xmlChar *)s);
#define endnode() xmlTextWriterEndElement(writer);
#define attr(s, ...) xmlTextWriterWriteFormatAttribute(writer, (xmlChar *)s, __VA_ARGS__)
#define nodename() ((char *)xmlTextReaderConstName(reader))
#define nodeis(s) !strcmp(nodename(), s)
#define nodeattr(s) (char *)xmlTextReaderGetAttribute(reader, (xmlChar *)s)
#define attris(s, v) (nodeattr(s) && !strcmp(nodeattr(s), v))
#define getattr(s, ...) (nodeattr(s) && sscanf(nodeattr(s), __VA_ARGS__))
void decl_radio(const char *name, int *sel, ...)
{
va_list ap;
int i;
const char *o;
va_start(ap, name);
if (mode == 'w') {
for (i = 0; o = va_arg(ap, const char *); i++) {
startnode("radio");
attr("name", "%s", name);
attr("value", "%s", o);
if (i == *sel) attr("checked", "%s", "");
endnode();
}
}
if (mode == 'r') {
for (i = 0; o = va_arg(ap, const char *); i++) {
if (nodeis("radio") && attris("name", name) &&
attris("value", o) && attris("checked", ""))
*sel = i;
}
}
va_end(ap);
}
void decl_btn(const char *name, void (*cb)(const char *))
{
if (mode == 'w') {
startnode("button");
attr("name", "%s", name);
endnode();
}
if (mode == 'r' && acting &&
nodeis("button") && attris("name", name))
cb(name);
}
void decl_var_int(const char *name, int *p, int min, int max)
{
if (mode == 'w') {
startnode("int");
attr("name", "%s", name);
attr("max", "%d", max);
attr("min", "%d", min);
attr("val", "%d", *p);
endnode();
}
if (mode == 'r' &&
nodeis("int") && attris("name", name))
{
int v;
if (getattr("val", "%d", &v) &&
v >= min && v <= max)
*p = v;
}
}
void decl_var_float(const char *name, float *p, float min, float max)
{
if (mode == 'w') {
startnode("float");
attr("name", "%s", name);
attr("max", "%f", max);
attr("min", "%f", min);
attr("val", "%f", *p);
endnode();
}
if (mode == 'r' &&
nodeis("float") && attris("name", name))
{
float v;
if (getattr("val", "%f", &v) &&
v >= min && v <= max)
*p = v;
}
}
static void all_decl()
{
decl();
}
static void readxml(const char *name)
{
mode = 'r';
reader = xmlReaderForFile(name, NULL, 0);
xmlTextReaderRead(reader); // skip root
while (xmlTextReaderRead(reader) == 1) {
all_decl();
}
xmlFreeTextReader(reader);
}
static void writexml(const char *name)
{
mode = 'w';
writer = xmlNewTextWriterFilename(name, 0);
xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
startnode("root");
all_decl();
endnode();
xmlFreeTextWriter(writer);
}
void fillrect(int x, int y, int w, int h, int val)
{
while (h--) {
memset(img[y] + x*2, val, w*2);
y++;
}
}
static int sock;
int selected(int fd)
{
fd_set fs;
struct timeval tv = {};
FD_ZERO(&fs);
FD_SET(fd, &fs);
return select(fd + 1, &fs, NULL, NULL, &tv) > 0;
}
int udpsock(int port)
{
struct sockaddr_in si;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
si.sin_family = AF_INET;
si.sin_addr.s_addr = htonl(INADDR_ANY);
si.sin_port = htons(port);
bind(fd, (struct sockaddr *)&si, sizeof(si));
return fd;
}
void udpsend(char *ip, int port, char *buf, int len)
{
struct sockaddr_in si;
si.sin_family = AF_INET;
si.sin_addr.s_addr = inet_addr(ip);
si.sin_port = htons(port);
sendto(sock, buf, len, 0, (struct sockaddr *)&si, sizeof(si));
}
static struct sockaddr_in clisa;
void udpreply(char *buf, int len)
{
sendto(sock, buf, len, 0, (struct sockaddr *)&clisa, sizeof(clisa));
}
int udprecv(int fd, char *buf, int len)
{
int salen;
return recvfrom(fd, buf, len, 0, (struct sockaddr *)&clisa, (socklen_t *)&salen);
}
void algo_init()
{
printf("algo_init\n");
sock = udpsock(atoi(getenv("BINDPORT")));
writexml("./default.xml");
init();
}
void algo_fini()
{
close(sock);
}
int algo_proc(void *_img)
{
img = (typeof(img))_img;
process();
if (selected(sock)) {
char buf[128];
udprecv(sock, buf, sizeof(buf));
printf("got %c\n", *buf);
if (*buf == 'q')
return 1;
if (*buf == 'a') {
acting = 1;
readxml("./act.xml");
acting = 0;
}
if (*buf == 's')
writexml("./config.xml");
if (*buf == 'l')
readxml("./config.xml");
if (*buf == 'r')
readxml("./default.xml");
writexml("./cur.xml");
}
return 0;
}