show controls gui
[drnoksnes] / usbjoy.c
1 /* Title: USB Joystick library
2    Version 0.2
3    Written by Puck2099 (puck2099@gmail.com), (c) 2006.
4    <http://www.gp32wip.com>
5    
6    If you use this library or a part of it, please, let it know.
7
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.
12    
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.
17
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
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>              /* For the definition of NULL */
25 #include <sys/types.h>          // For Device open
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <fcntl.h>
29 #include <unistd.h>             // For Device read
30
31 #include <string.h>
32 #include <limits.h>             /* For the definition of PATH_MAX */
33 #include <linux/joystick.h>
34
35 #include "usbjoy.h"
36
37
38 /*
39   Function: joy_open
40
41   Opens a USB joystick and fills its information.
42
43   Parameters:
44
45   joynumber - Joystick's identifier (0 reserved for GP2X's builtin Joystick).
46
47   Returns:
48
49   Filled usbjoy structure.
50
51 */
52 struct usbjoy * joy_open (int joynumber) {
53   int fd, i;
54   char path [128];
55   struct usbjoy * joy = NULL;
56
57   system ("insmod joydev"); // Loads joydev module
58
59   if (joynumber == 0) {
60   }
61   else if (joynumber > 0) {
62     sprintf (path, "/dev/input/js%d", joynumber-1);
63     fd = open(path, O_RDONLY, 0);
64     if (fd > 0) {
65       joy = (struct usbjoy *) malloc(sizeof(struct usbjoy));
66
67       // Joystick's file descriptor
68       joy->fd = fd;
69
70       // Set the joystick to non-blocking read mode
71       fcntl(joy->fd, F_SETFL, O_NONBLOCK);
72
73       // Joystick's name
74       ioctl(joy->fd, JSIOCGNAME(128*sizeof(char)), joy->name);
75
76       // Joystick's device
77       sprintf (joy->device, path);
78
79       // Joystick's buttons
80       ioctl(joy->fd, JSIOCGBUTTONS, &joy->numbuttons);
81
82       // Joystick's axes
83       ioctl(joy->fd, JSIOCGAXES, &joy->numaxes);
84
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;
88     }
89     else {
90       printf ("ERROR: No Joystick found\n");
91     }
92   }
93   return joy;
94 }
95
96 /*
97   Function: joy_name
98
99   Returns Joystick's name.
100
101   Parameters:
102
103   joy - Selected joystick.
104
105   Returns:
106
107   Joystick's name or NULL if <usbjoy> struct is empty.
108 */
109 char * joy_name (struct usbjoy * joy) {
110   if (joy != NULL)  return joy->name;
111   else return NULL;
112 }
113
114
115 /*
116   Function: joy_device
117
118   Returns Joystick's device.
119
120   Parameters:
121
122   joy - Selected joystick.
123
124   Returns:
125
126   Joystick's device or NULL if <usbjoy> struct is empty.
127 */
128 char * joy_device (struct usbjoy * joy) {
129   if (joy != NULL)  return joy->device;
130   else return NULL;
131 }
132
133
134 /*
135   Function: joy_buttons
136
137   Returns Joystick's buttons number.
138
139   Parameters:
140
141   joy - Selected joystick.
142
143   Returns:
144
145   Joystick's buttons or 0 if <usbjoy> struct is empty.
146 */
147 int joy_buttons (struct usbjoy * joy) {
148   if (joy != NULL) return joy->numbuttons;
149   else return 0;
150 }
151
152
153 /*
154   Function: joy_axes
155
156   Returns Joystick's axes number.
157
158   Parameters:
159
160   joy - Selected joystick.
161
162   Returns:
163
164   Joystick's axes or 0 if <usbjoy> struct is empty.
165 */
166 int joy_axes (struct usbjoy * joy) {
167   if (joy != NULL) return joy->numaxes;
168   else return 0;
169 }
170
171
172 /*
173   Function: joy_update
174
175   Updates Joystick's internal information (<statebuttons> and <stateaxes> fields).
176
177   Parameters:
178
179   joy - Selected joystick.
180
181   Returns:
182
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.
186 */
187 int joy_update (struct usbjoy * joy) {
188   struct js_event events[0xff];
189   int i, len;
190   int event = 0;
191   if (joy != NULL) {    
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) {
196         case JS_EVENT_AXIS:
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;
201           }
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;
206           }
207           event = 1;
208           break;
209         case JS_EVENT_BUTTON:
210           joy->statebuttons[events[i].number] = events[i].value;
211           event = 1;
212           break;
213         default:
214           break;
215         }
216       }
217     }
218   }
219   else {
220     event = -1;
221   }   
222   return event;
223 }
224
225
226 /*
227   Function: joy_getbutton
228
229   Returns Joystick's button information.
230
231   Parameters:
232
233   button - Button which value you want to know (from 0 to 31).
234   joy - Selected joystick.
235
236   Returns:
237
238   0 - Button NOT pushed.
239   1 - Button pushed.
240   -1 - Error: <usbjoy> struct is empty.
241 */
242 int joy_getbutton (int button, struct usbjoy * joy) {
243   if (joy != NULL) {
244     if (button < joy_buttons(joy)) return joy->statebuttons[button];
245     else return 0;
246   }
247   else return -1;
248 }
249
250
251 /*
252   Function: joy_getaxe
253
254   Returns Joystick's axes information.
255
256   Parameters:
257
258   axe - Axe which value you want to know (see <Axes values>).
259   joy - Selected joystick.
260
261   Returns:
262
263   0 - Direction NOT pushed.
264   1 - Direction pushed.
265   -1 - Error: <usbjoy> struct is empty.
266 */
267 int joy_getaxe (int axe, struct usbjoy * joy) {
268   if (joy != NULL) {
269     if (axe < 4) return joy->stateaxes[axe];
270     else return 0;
271   }
272   else return -1;
273 }
274
275
276 /*
277   Function: joy_close
278
279   Closes selected joystick's file descriptor and detroys it's fields.
280
281   Parameters:
282
283   joy - Selected joystick.
284
285   Returns:
286
287   0 - Joystick successfully closed.
288   -1 - Error: <usbjoy> struct is empty.
289 */
290 int joy_close (struct usbjoy * joy) {
291   if (joy != NULL) {
292     close (joy->fd);
293     free (joy);
294     return 0;
295   }
296   else return -1;
297 }