rc_proto: packed struct
[protos/xbee.git] / joystick.c
1 /*
2  * Copyright (c) 2012, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include <linux/input.h>
39 #include <linux/joystick.h>
40
41 #include "joystick.h"
42
43 int joystick_init(const char *devname, struct joystick_info *joyinfo)
44 {
45         int fd;
46
47         memset(joyinfo, 0, sizeof(*joyinfo));
48         joyinfo->nb_axis = 2;
49         joyinfo->nb_buttons = 2;
50         joyinfo->version = 0x000800;
51         strcpy(joyinfo->name, "Unknown");
52
53         if ((fd = open(devname, O_RDONLY)) < 0) {
54                 perror("jstest");
55                 return -1;
56         }
57
58         ioctl(fd, JSIOCGVERSION, &joyinfo->version);
59         ioctl(fd, JSIOCGAXES, &joyinfo->nb_axis);
60         ioctl(fd, JSIOCGBUTTONS, &joyinfo->nb_buttons);
61         ioctl(fd, JSIOCGNAME(JOYSTICK_NAME_LENGTH), joyinfo->name);
62         ioctl(fd, JSIOCGAXMAP, joyinfo->axmap);
63         ioctl(fd, JSIOCGBTNMAP, joyinfo->btnmap);
64
65         joyinfo->fd = fd;
66         return 0;
67 }
68
69 void joystick_dump(struct joystick_info *joyinfo)
70 {
71         int i;
72
73         if (joyinfo->nb_axis) {
74                 printf("Axes: ");
75                 for (i = 0; i < joyinfo->nb_axis; i++)
76                         printf("%2d:%6d ", i, joyinfo->axis[i]);
77         }
78
79         if (joyinfo->nb_buttons) {
80                 printf("Buttons: ");
81                 for (i = 0; i < joyinfo->nb_buttons; i++)
82                         printf("%2d:%s ", i, joyinfo->buttons[i] ? "on " : "off");
83         }
84
85         printf("\n");
86
87 }
88
89 void joystick_input(int fd, short event, void *arg)
90 {
91         struct joystick_info *joyinfo;
92         struct js_event js;
93
94         joyinfo = arg;
95
96         if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
97                 perror("\njstest: error reading");
98                 return;
99         }
100
101         switch(js.type & ~JS_EVENT_INIT) {
102                 case JS_EVENT_BUTTON:
103                         if (js.number >= joyinfo->nb_buttons) {
104                                 fprintf(stderr,
105                                         "\njstest: button number too high");
106                                 return;
107                         }
108                         joyinfo->buttons[js.number] = js.value;
109                         break;
110                 case JS_EVENT_AXIS:
111                         if (js.number >= joyinfo->nb_axis) {
112                                 fprintf(stderr,
113                                         "\njstest: axe number too high");
114                                 return;
115                         }
116                         joyinfo->axis[js.number] = js.value;
117                         break;
118         }
119 }