1 /* Title: USB Joystick library
3 Written by Puck2099 (puck2099@gmail.com), (c) 2006.
4 <http://www.gp32wip.com>
6 If you use this library or a part of it, please, let it know.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <stdio.h> /* For the definition of NULL */
25 #include <sys/types.h> // For Device open
27 #include <sys/ioctl.h>
29 #include <unistd.h> // For Device read
32 #include <limits.h> /* For the definition of PATH_MAX */
33 #include <linux/joystick.h>
41 Opens a USB joystick and fills its information.
45 joynumber - Joystick's identifier (0 reserved for GP2X's builtin Joystick).
49 Filled usbjoy structure.
52 struct usbjoy * joy_open (int joynumber) {
55 struct usbjoy * joy = NULL;
57 system ("insmod joydev"); // Loads joydev module
61 else if (joynumber > 0) {
62 sprintf (path, "/dev/input/js%d", joynumber-1);
63 fd = open(path, O_RDONLY, 0);
65 joy = (struct usbjoy *) malloc(sizeof(struct usbjoy));
67 // Joystick's file descriptor
70 // Set the joystick to non-blocking read mode
71 fcntl(joy->fd, F_SETFL, O_NONBLOCK);
74 ioctl(joy->fd, JSIOCGNAME(128*sizeof(char)), joy->name);
77 sprintf (joy->device, path);
80 ioctl(joy->fd, JSIOCGBUTTONS, &joy->numbuttons);
83 ioctl(joy->fd, JSIOCGAXES, &joy->numaxes);
85 // Clean buttons and axes
86 for (i=0; i<32; i++) joy->statebuttons[i] = 0;
87 for (i=0; i<4; i++) joy->stateaxes[i] = 0;
90 printf ("ERROR: No Joystick found\n");
99 Returns Joystick's name.
103 joy - Selected joystick.
107 Joystick's name or NULL if <usbjoy> struct is empty.
109 char * joy_name (struct usbjoy * joy) {
110 if (joy != NULL) return joy->name;
118 Returns Joystick's device.
122 joy - Selected joystick.
126 Joystick's device or NULL if <usbjoy> struct is empty.
128 char * joy_device (struct usbjoy * joy) {
129 if (joy != NULL) return joy->device;
135 Function: joy_buttons
137 Returns Joystick's buttons number.
141 joy - Selected joystick.
145 Joystick's buttons or 0 if <usbjoy> struct is empty.
147 int joy_buttons (struct usbjoy * joy) {
148 if (joy != NULL) return joy->numbuttons;
156 Returns Joystick's axes number.
160 joy - Selected joystick.
164 Joystick's axes or 0 if <usbjoy> struct is empty.
166 int joy_axes (struct usbjoy * joy) {
167 if (joy != NULL) return joy->numaxes;
175 Updates Joystick's internal information (<statebuttons> and <stateaxes> fields).
179 joy - Selected joystick.
183 0 - No events registered (no need to update).
184 1 - Events registered (a button or axe has been pushed).
185 -1 - Error: <usbjoy> struct is empty.
187 int joy_update (struct usbjoy * joy) {
188 struct js_event events[0xff];
192 if ((len=read(joy->fd, events, (sizeof events))) >0) {
193 len /= sizeof(events[0]);
194 for ( i=0; i<len; ++i ) {
195 switch (events[i].type & ~JS_EVENT_INIT) {
197 if (events[i].number == 0) {
198 joy->stateaxes[JOYLEFT] = joy->stateaxes[JOYRIGHT] = 0;
199 if (events[i].value < 0) joy->stateaxes[JOYLEFT] = 1;
200 else if (events[i].value > 0) joy->stateaxes[JOYRIGHT] = 1;
202 else if (events[i].number == 1) {
203 joy->stateaxes[JOYUP] = joy->stateaxes[JOYDOWN] = 0;
204 if (events[i].value < 0) joy->stateaxes[JOYUP] = 1;
205 else if (events[i].value > 0) joy->stateaxes[JOYDOWN] = 1;
209 case JS_EVENT_BUTTON:
210 joy->statebuttons[events[i].number] = events[i].value;
227 Function: joy_getbutton
229 Returns Joystick's button information.
233 button - Button which value you want to know (from 0 to 31).
234 joy - Selected joystick.
238 0 - Button NOT pushed.
240 -1 - Error: <usbjoy> struct is empty.
242 int joy_getbutton (int button, struct usbjoy * joy) {
244 if (button < joy_buttons(joy)) return joy->statebuttons[button];
254 Returns Joystick's axes information.
258 axe - Axe which value you want to know (see <Axes values>).
259 joy - Selected joystick.
263 0 - Direction NOT pushed.
264 1 - Direction pushed.
265 -1 - Error: <usbjoy> struct is empty.
267 int joy_getaxe (int axe, struct usbjoy * joy) {
269 if (axe < 4) return joy->stateaxes[axe];
279 Closes selected joystick's file descriptor and detroys it's fields.
283 joy - Selected joystick.
287 0 - Joystick successfully closed.
288 -1 - Error: <usbjoy> struct is empty.
290 int joy_close (struct usbjoy * joy) {