2 * Copyright (c) 2012, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
38 #include <linux/input.h>
39 #include <linux/joystick.h>
43 int joystick_init(const char *devname, struct joystick_info *joyinfo)
47 memset(joyinfo, 0, sizeof(*joyinfo));
49 joyinfo->nb_buttons = 2;
50 joyinfo->version = 0x000800;
51 strcpy(joyinfo->name, "Unknown");
53 if ((fd = open(devname, O_RDONLY)) < 0) {
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);
69 void joystick_dump(struct joystick_info *joyinfo)
73 if (joyinfo->nb_axis) {
75 for (i = 0; i < joyinfo->nb_axis; i++)
76 printf("%2d:%6d ", i, joyinfo->axis[i]);
79 if (joyinfo->nb_buttons) {
81 for (i = 0; i < joyinfo->nb_buttons; i++)
82 printf("%2d:%s ", i, joyinfo->buttons[i] ? "on " : "off");
89 void joystick_input(int fd, short event, void *arg)
91 struct joystick_info *joyinfo;
96 if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
97 perror("\njstest: error reading");
101 switch(js.type & ~JS_EVENT_INIT) {
102 case JS_EVENT_BUTTON:
103 if (js.number >= joyinfo->nb_buttons) {
105 "\njstest: button number too high");
108 joyinfo->buttons[js.number] = js.value;
111 if (js.number >= joyinfo->nb_axis) {
113 "\njstest: axe number too high");
116 joyinfo->axis[js.number] = js.value;