vrmodel

annotate 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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@4 2 #include <stdlib.h>
nuclear@4 3 #include <string.h>
nuclear@4 4 #include <limits.h>
nuclear@4 5 #include <errno.h>
nuclear@4 6 #include <assert.h>
nuclear@4 7 #include <unistd.h>
nuclear@4 8 #include <fcntl.h>
nuclear@4 9 #include <sys/socket.h>
nuclear@4 10 #include <arpa/inet.h>
nuclear@4 11 #include <sys/select.h>
nuclear@4 12 #include <sys/time.h>
nuclear@4 13 #include <sys/types.h>
nuclear@4 14 #include <linux/joystick.h>
nuclear@4 15 #include "proto.h"
nuclear@4 16
nuclear@4 17 struct client {
nuclear@4 18 int s;
nuclear@4 19 struct sockaddr_in addr;
nuclear@4 20 struct client *next;
nuclear@4 21 };
nuclear@4 22
nuclear@4 23 int open_js(const char *jspath);
nuclear@4 24 int handle_js(int fd);
nuclear@4 25 int start_srv(int port);
nuclear@4 26 int handle_net(int s);
nuclear@4 27
nuclear@4 28 static int opt_dport = DEF_PORT;
nuclear@4 29 static const char *opt_jspath = "/dev/input/js0";
nuclear@4 30
nuclear@4 31 static struct client *clients;
nuclear@4 32
nuclear@4 33 static int js_num_axes;
nuclear@4 34 static float *js_axis_state;
nuclear@4 35 static unsigned int js_bn_state;
nuclear@4 36
nuclear@0 37
nuclear@0 38 int main(int argc, char **argv)
nuclear@0 39 {
nuclear@4 40 int js, sock;
nuclear@4 41 int maxfd;
nuclear@4 42
nuclear@4 43 if((js = open_js(opt_jspath)) == -1) {
nuclear@4 44 return 1;
nuclear@4 45 }
nuclear@4 46 if((sock = start_srv(opt_dport)) == -1) {
nuclear@4 47 return 1;
nuclear@4 48 }
nuclear@4 49 maxfd = js > sock ? js : sock;
nuclear@4 50
nuclear@4 51 for(;;) {
nuclear@4 52 fd_set rdset;
nuclear@4 53
nuclear@4 54 FD_ZERO(&rdset);
nuclear@4 55 FD_SET(js, &rdset);
nuclear@4 56 FD_SET(sock, &rdset);
nuclear@4 57
nuclear@4 58 while(select(maxfd + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
nuclear@4 59
nuclear@4 60 if(FD_ISSET(js, &rdset)) {
nuclear@4 61 handle_js(js);
nuclear@4 62 }
nuclear@4 63 if(FD_ISSET(sock, &rdset)) {
nuclear@4 64 handle_net(sock);
nuclear@4 65 }
nuclear@4 66 }
nuclear@4 67
nuclear@4 68 close(js);
nuclear@4 69 close(sock);
nuclear@4 70 return 0;
nuclear@0 71 }
nuclear@4 72
nuclear@4 73 int open_js(const char *jspath)
nuclear@4 74 {
nuclear@4 75 int fd;
nuclear@4 76 char buf[256];
nuclear@4 77 int axes;
nuclear@4 78
nuclear@4 79 if((fd = open(jspath, O_RDONLY)) == -1) {
nuclear@4 80 fprintf(stderr, "failed to open js device: %s: %s\n", jspath, strerror(errno));
nuclear@4 81 return -1;
nuclear@4 82 }
nuclear@4 83 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
nuclear@4 84
nuclear@4 85 if(ioctl(fd, JSIOCGAXES, &axes) == -1) {
nuclear@4 86 fprintf(stderr, "failed to get number of joystick axes\n");
nuclear@4 87 return -1;
nuclear@4 88 }
nuclear@4 89 if(ioctl(fd, JSIOCGNAME(sizeof buf), buf) == -1) {
nuclear@4 90 strcpy(buf, "unknown");
nuclear@4 91 }
nuclear@4 92 printf("opened joystick: %s: %s (%d axes)\n", jspath, buf, axes);
nuclear@4 93
nuclear@4 94 js_axis_state = malloc(axes * sizeof *js_axis_state);
nuclear@4 95 assert(js_axis_state);
nuclear@4 96 js_num_axes = axes;
nuclear@4 97
nuclear@4 98 return fd;
nuclear@4 99 }
nuclear@4 100
nuclear@4 101 int handle_js(int fd)
nuclear@4 102 {
nuclear@4 103 int evtype;
nuclear@4 104 struct js_event ev;
nuclear@4 105 struct client *c;
nuclear@4 106
nuclear@4 107 if(read(fd, &ev, sizeof ev) <= 0) {
nuclear@4 108 return -1;
nuclear@4 109 }
nuclear@4 110
nuclear@4 111 switch(ev.type) {
nuclear@4 112 case JS_EVENT_AXIS:
nuclear@4 113 js_axis_state[ev.number] = (float)ev.value / (float)SHRT_MAX;
nuclear@4 114 evtype = EV_MOTION;
nuclear@4 115 break;
nuclear@4 116
nuclear@4 117 case JS_EVENT_BUTTON:
nuclear@4 118 if(ev.value) {
nuclear@4 119 js_bn_state |= 1 << ev.number;
nuclear@4 120 } else {
nuclear@4 121 js_bn_state &= ~(1 << ev.number);
nuclear@4 122 }
nuclear@4 123 evtype = EV_BUTTON;
nuclear@4 124 break;
nuclear@4 125
nuclear@4 126 default:
nuclear@4 127 break;
nuclear@4 128 }
nuclear@4 129
nuclear@4 130 /* send client events */
nuclear@4 131 c = clients;
nuclear@4 132 while(c) {
nuclear@4 133 struct message m;
nuclear@4 134
nuclear@4 135 m.magic = MAGIC;
nuclear@4 136 m.type = evtype;
nuclear@4 137 m.data.event.x = js_axis_state[0];
nuclear@4 138 m.data.event.y = js_num_axes > 1 ? js_axis_state[1] : 0;
nuclear@4 139 m.data.event.z = js_num_axes > 2 ? js_axis_state[2] : 0;
nuclear@4 140
nuclear@4 141 sendto(c->s, &m, sizeof m, 0, (struct sockaddr*)&c->addr, sizeof c->addr);
nuclear@4 142 c = c->next;
nuclear@4 143 }
nuclear@4 144 return 0;
nuclear@4 145 }
nuclear@4 146
nuclear@4 147 int start_srv(int port)
nuclear@4 148 {
nuclear@4 149 int s;
nuclear@4 150 struct sockaddr_in sa;
nuclear@4 151
nuclear@4 152 if((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
nuclear@4 153 perror("failed to create socket");
nuclear@4 154 return -1;
nuclear@4 155 }
nuclear@4 156
nuclear@4 157 memset(&sa, 0, sizeof sa);
nuclear@4 158 sa.sin_family = AF_INET;
nuclear@4 159 sa.sin_addr.s_addr = htonl(INADDR_ANY);
nuclear@4 160 sa.sin_port = htons(opt_dport);
nuclear@4 161
nuclear@4 162 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
nuclear@4 163 perror("failed to bind socket");
nuclear@4 164 close(s);
nuclear@4 165 return -1;
nuclear@4 166 }
nuclear@4 167
nuclear@4 168 return s;
nuclear@4 169 }
nuclear@4 170
nuclear@4 171 int handle_net(int s)
nuclear@4 172 {
nuclear@4 173 return 0;
nuclear@4 174 }