vrmodel
view inptools/test/src/main.c @ 5:9e260c091f75
wrote some server code in the test input tool
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 15 Sep 2014 10:57:31 +0300 |
parents | a32b151fb3c6 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <errno.h>
6 #include <assert.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/socket.h>
10 #include <arpa/inet.h>
11 #include <sys/select.h>
12 #include <sys/time.h>
13 #include <sys/types.h>
14 #include <linux/joystick.h>
15 #include "proto.h"
17 struct client {
18 int active;
19 struct sockaddr_in addr;
20 struct client *next;
21 };
23 int open_js(const char *jspath);
24 int handle_js(int fd);
25 int start_srv(int port);
26 int handle_net(int s);
27 int respond(int s, int rsp, struct sockaddr_in *sa);
28 int respond_msg(int s, struct message *msg, struct sockaddr_in *sa);
29 struct client *add_client(struct sockaddr_in *addr);
30 struct client *find_client(struct sockaddr_in *addr);
32 static int opt_dport = DEF_PORT;
33 static const char *opt_jspath = "/dev/input/js0";
35 static int sock = -1;
36 static struct client *clients;
38 static int js_num_axes;
39 static float *js_axis_state;
40 static unsigned int js_bn_state;
43 int main(int argc, char **argv)
44 {
45 int js;
46 int maxfd;
48 if((js = open_js(opt_jspath)) == -1) {
49 return 1;
50 }
51 if((sock = start_srv(opt_dport)) == -1) {
52 return 1;
53 }
54 maxfd = js > sock ? js : sock;
56 for(;;) {
57 fd_set rdset;
59 FD_ZERO(&rdset);
60 FD_SET(js, &rdset);
61 FD_SET(sock, &rdset);
63 while(select(maxfd + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
65 if(FD_ISSET(js, &rdset)) {
66 handle_js(js);
67 }
68 if(FD_ISSET(sock, &rdset)) {
69 handle_net(sock);
70 }
71 }
73 close(js);
74 close(sock);
75 return 0;
76 }
78 int open_js(const char *jspath)
79 {
80 int fd;
81 char buf[256];
82 int axes;
84 if((fd = open(jspath, O_RDONLY)) == -1) {
85 fprintf(stderr, "failed to open js device: %s: %s\n", jspath, strerror(errno));
86 return -1;
87 }
88 /*fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);*/
90 if(ioctl(fd, JSIOCGAXES, &axes) == -1) {
91 fprintf(stderr, "failed to get number of joystick axes\n");
92 return -1;
93 }
94 if(ioctl(fd, JSIOCGNAME(sizeof buf), buf) == -1) {
95 strcpy(buf, "unknown");
96 }
97 printf("opened joystick: %s: %s (%d axes)\n", jspath, buf, axes);
99 js_axis_state = malloc(axes * sizeof *js_axis_state);
100 assert(js_axis_state);
101 js_num_axes = axes;
103 return fd;
104 }
106 int handle_js(int fd)
107 {
108 int evtype;
109 struct js_event ev;
110 struct client *c;
112 if(read(fd, &ev, sizeof ev) <= 0) {
113 return -1;
114 }
116 switch(ev.type) {
117 case JS_EVENT_AXIS:
118 js_axis_state[ev.number] = (float)ev.value / (float)SHRT_MAX;
119 evtype = EV_MOTION;
120 break;
122 case JS_EVENT_BUTTON:
123 if(ev.value) {
124 js_bn_state |= 1 << ev.number;
125 } else {
126 js_bn_state &= ~(1 << ev.number);
127 }
128 evtype = EV_BUTTON;
129 break;
131 default:
132 break;
133 }
135 /* send client events */
136 c = clients;
137 while(c) {
138 struct message m;
140 m.magic = MAGIC;
141 m.type = evtype;
142 m.data.event.x = js_axis_state[0];
143 m.data.event.y = js_num_axes > 1 ? js_axis_state[1] : 0;
144 m.data.event.z = js_num_axes > 2 ? js_axis_state[2] : 0;
145 m.data.event.bnmask = js_bn_state;
147 sendto(sock, &m, sizeof m, 0, (struct sockaddr*)&c->addr, sizeof c->addr);
148 c = c->next;
149 }
150 return 0;
151 }
153 int start_srv(int port)
154 {
155 int s;
156 struct sockaddr_in sa;
158 if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
159 perror("failed to create socket");
160 return -1;
161 }
163 memset(&sa, 0, sizeof sa);
164 sa.sin_family = AF_INET;
165 sa.sin_addr.s_addr = htonl(INADDR_ANY);
166 sa.sin_port = htons(opt_dport);
168 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
169 perror("failed to bind socket");
170 close(s);
171 return -1;
172 }
174 return s;
175 }
178 int handle_net(int s)
179 {
180 struct message msg;
181 struct sockaddr_in addr;
182 socklen_t addrlen = sizeof addr;
183 struct client *c;
185 if(recvfrom(s, &msg, sizeof msg, 0, (struct sockaddr*)&addr, &addrlen) < sizeof addr) {
186 perror("failed to recieve message");
187 return -1;
188 }
189 if(msg.magic != MAGIC) {
190 fprintf(stderr, "received magic is not good\n");
191 return -1;
192 }
194 c = find_client(&addr);
196 switch(msg.type) {
197 case REQ_DISCOVER:
198 /* a client is looking for us */
199 msg.type = RSP_OK;
200 strcat(msg.data.raw, "test-input");
201 if(respond_msg(s, &msg, &addr) == -1) {
202 return -1;
203 }
204 break;
206 case REQ_START:
207 if(!c) {
208 if(!(c = add_client(&addr))) {
209 respond(s, RSP_ERR, &addr);
210 return -1;
211 }
212 }
213 c->active = 1;
214 if(respond(s, RSP_OK, &addr) == -1) {
215 return -1;
216 }
217 break;
219 case REQ_STOP:
220 if(c) {
221 c->active = 0;
222 }
223 if(respond(s, RSP_OK, &addr) == -1) {
224 return -1;
225 }
226 break;
228 default:
229 break;
230 }
232 return 0;
233 }
235 int respond(int s, int rsp, struct sockaddr_in *sa)
236 {
237 struct message m;
238 memset(&m, 0, sizeof m);
239 m.magic = MAGIC;
240 m.type = rsp;
242 return respond_msg(s, &m, sa);
243 }
245 int respond_msg(int s, struct message *msg, struct sockaddr_in *sa)
246 {
247 if(sendto(s, &msg, sizeof msg, 0, (struct sockaddr*)sa, sizeof *sa) == -1) {
248 perror("failed to send response");
249 return -1;
250 }
251 return 0;
252 }
254 struct client *add_client(struct sockaddr_in *addr)
255 {
256 struct client *c;
258 if(!(c = calloc(1, sizeof *c))) {
259 perror("failed to add client");
260 return 0;
261 }
262 c->addr = *addr;
263 c->next = clients;
264 clients = c;
265 return c;
266 }
268 struct client *find_client(struct sockaddr_in *addr)
269 {
270 struct client *c = clients;
271 while(c) {
272 if(c->addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
273 c->addr.sin_port == addr->sin_port) {
274 return c;
275 }
276 c = c->next;
277 }
278 return 0;
279 }