vrmodel
view inptools/test/src/main.c @ 4:a32b151fb3c6
moving along slowly
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 11 Sep 2014 00:08:23 +0300 |
parents | be91b72ce3f9 |
children | 9e260c091f75 |
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 s;
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);
28 static int opt_dport = DEF_PORT;
29 static const char *opt_jspath = "/dev/input/js0";
31 static struct client *clients;
33 static int js_num_axes;
34 static float *js_axis_state;
35 static unsigned int js_bn_state;
38 int main(int argc, char **argv)
39 {
40 int js, sock;
41 int maxfd;
43 if((js = open_js(opt_jspath)) == -1) {
44 return 1;
45 }
46 if((sock = start_srv(opt_dport)) == -1) {
47 return 1;
48 }
49 maxfd = js > sock ? js : sock;
51 for(;;) {
52 fd_set rdset;
54 FD_ZERO(&rdset);
55 FD_SET(js, &rdset);
56 FD_SET(sock, &rdset);
58 while(select(maxfd + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
60 if(FD_ISSET(js, &rdset)) {
61 handle_js(js);
62 }
63 if(FD_ISSET(sock, &rdset)) {
64 handle_net(sock);
65 }
66 }
68 close(js);
69 close(sock);
70 return 0;
71 }
73 int open_js(const char *jspath)
74 {
75 int fd;
76 char buf[256];
77 int axes;
79 if((fd = open(jspath, O_RDONLY)) == -1) {
80 fprintf(stderr, "failed to open js device: %s: %s\n", jspath, strerror(errno));
81 return -1;
82 }
83 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
85 if(ioctl(fd, JSIOCGAXES, &axes) == -1) {
86 fprintf(stderr, "failed to get number of joystick axes\n");
87 return -1;
88 }
89 if(ioctl(fd, JSIOCGNAME(sizeof buf), buf) == -1) {
90 strcpy(buf, "unknown");
91 }
92 printf("opened joystick: %s: %s (%d axes)\n", jspath, buf, axes);
94 js_axis_state = malloc(axes * sizeof *js_axis_state);
95 assert(js_axis_state);
96 js_num_axes = axes;
98 return fd;
99 }
101 int handle_js(int fd)
102 {
103 int evtype;
104 struct js_event ev;
105 struct client *c;
107 if(read(fd, &ev, sizeof ev) <= 0) {
108 return -1;
109 }
111 switch(ev.type) {
112 case JS_EVENT_AXIS:
113 js_axis_state[ev.number] = (float)ev.value / (float)SHRT_MAX;
114 evtype = EV_MOTION;
115 break;
117 case JS_EVENT_BUTTON:
118 if(ev.value) {
119 js_bn_state |= 1 << ev.number;
120 } else {
121 js_bn_state &= ~(1 << ev.number);
122 }
123 evtype = EV_BUTTON;
124 break;
126 default:
127 break;
128 }
130 /* send client events */
131 c = clients;
132 while(c) {
133 struct message m;
135 m.magic = MAGIC;
136 m.type = evtype;
137 m.data.event.x = js_axis_state[0];
138 m.data.event.y = js_num_axes > 1 ? js_axis_state[1] : 0;
139 m.data.event.z = js_num_axes > 2 ? js_axis_state[2] : 0;
141 sendto(c->s, &m, sizeof m, 0, (struct sockaddr*)&c->addr, sizeof c->addr);
142 c = c->next;
143 }
144 return 0;
145 }
147 int start_srv(int port)
148 {
149 int s;
150 struct sockaddr_in sa;
152 if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
153 perror("failed to create socket");
154 return -1;
155 }
157 memset(&sa, 0, sizeof sa);
158 sa.sin_family = AF_INET;
159 sa.sin_addr.s_addr = htonl(INADDR_ANY);
160 sa.sin_port = htons(opt_dport);
162 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
163 perror("failed to bind socket");
164 close(s);
165 return -1;
166 }
168 return s;
169 }
171 int handle_net(int s)
172 {
173 return 0;
174 }