Debian Codegraph @

3rdparty/joystick/jsBSD.cxx

raw symbols SCIP
1 /*
2 PLIB - A Suite of Portable Game Libraries
3 Copyright (C) 1998,2002 Steve Baker
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 For further information visit http://plib.sourceforge.net
20
21 $Id: jsBSD.cxx 2133 2008-07-18 14:32:22Z fayjf $
22 */
23
24 /*
25 * Inspired by the X-Mame USB HID joystick driver for NetBSD and
26 * FreeBSD by Krister Walfridsson <[email protected]>.
27 * Incorporates the original analog joystick driver for BSD by
28 * Stephen Montgomery-Smith <[email protected]>, with
29 * NetBSD mods courtesy of Rene Hexel.
30 *
31 * Bert Driehuis <[email protected]>
32 *
33 * Notes:
34 * Hats are mapped to two axes for now. A cleaner implementation requires
35 * an API extension, and to be useful for my devious purposes, FlightGear
36 * would need to understand that.
37 */
38
39 #include "FlightGear_js.h"
40
41 #if defined(__NetBSD__) || defined(__FreeBSD__)
42 #define HAVE_USB_JS 1
43 #endif
44
45 #ifndef TRUE
46 #define TRUE 1
47 #define FALSE 0
48 #endif
49
50 #include <simgear/props/props.hxx> /* for jsSetError and SG_WARN */
51
52 #include <sys/param.h>
53 #include <fcntl.h>
54
55 #include <string.h>
56 #include <errno.h>
57 #include <sys/ioctl.h>
58 #if defined(__FreeBSD__)
59 # include <sys/joystick.h>
60 #else
61 # include <machine/joystick.h> // For analog joysticks
62 #endif
63 #ifdef HAVE_USB_JS
64 #if defined(__NetBSD__)
65 #ifdef HAVE_USBHID_H
66 #include <usbhid.h>
67 #else
68 #include <usb.h>
69 #endif
70 #elif defined(__FreeBSD__)
71 extern "C" {
72 # if __FreeBSD_version < 500000
73 # include <libusbhid.h>
74 # else
75 # define HAVE_USBHID_H 1
76 # include <usbhid.h>
77 # include <dev/usb/usb_ioctl.h>
78 # endif
79 }
80 #endif
81 #include <dev/usb/usb.h>
82 #include <dev/usb/usbhid.h>
83
84 /* Compatibility with older usb.h revisions */
85 #if !defined(USB_MAX_DEVNAMES) && defined(MAXDEVNAMES)
86 #define USB_MAX_DEVNAMES MAXDEVNAMES
87 #endif
88 #endif
89
90 static int hatmap_x[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
91 static int hatmap_y[9] = { 0, 1, 1, 0, -1, -1, -1, 0, 1 };
92 struct os_specific_s {
93 char fname [128 ];
94 int fd;
95 int is_analog;
96 // The following structure members are specific to analog joysticks
97 struct joystick ajs;
98 #ifdef HAVE_USB_JS
99 // The following structure members are specific to USB joysticks
100 struct hid_item *hids;
101 int hid_dlen;
102 int hid_offset;
103 char *hid_data_buf;
104 int axes_usage [ _JS_MAX_AXES ] ;
105 #endif
106 // We keep button and axes state ourselves, as they might not be updated
107 // on every read of a USB device
108 int cache_buttons ;
109 float cache_axes [ _JS_MAX_AXES ] ;
110 float axes_minimum [ _JS_MAX_AXES ] ;
111 float axes_maximum [ _JS_MAX_AXES ] ;
112 };
113
114 // Idents lower than USB_IDENT_OFFSET are for analog joysticks.
115 #define USB_IDENT_OFFSET 2
116
117 #define USBDEV "/dev/usb"
118 #define UHIDDEV "/dev/uhid"
119 #define AJSDEV "/dev/joy"
120
121 #ifdef HAVE_USB_JS
122 /*
123 * findusbdev (and its helper, walkusbdev) try to locate the full name
124 * of a USB device. If /dev/usbN isn't readable, we punt and return the
125 * uhidN device name. We warn the user of this situation once.
126 */
127 static char *
128 walkusbdev(int f, char *dev, char *out, int outlen)
129 {
130 return NULL;
131 }
132
133 static int
134 findusbdev(char *name, char *out, int outlen)
135 {
136 return 0;
137 }
138
139 static int joy_initialize_hid(struct os_specific_s *os,
140 int *num_axes, int *num_buttons)
141 {
142 int size, is_joystick;
143 #ifdef HAVE_USBHID_H
144 int report_id = 0;
145 #endif
146 struct hid_data *d;
147 struct hid_item h;
148 report_desc_t rd;
149
150 if ((rd = hid_get_report_desc(os->fd)) == 0)
151 {
152 fprintf(stderr, "error: %s: %s", os->fname, strerror(errno));
153 return FALSE;
154 }
155
156 os->hids = NULL;
157
158 #ifdef HAVE_USBHID_H
159 if (ioctl(os->fd, USB_GET_REPORT_ID, &report_id) < 0)
160 {
161 fprintf(stderr, "error: %s: %s", os->fname, strerror(errno));
162 return FALSE;
163 }
164
165 size = hid_report_size(rd, hid_input, report_id);
166 #else
167 size = hid_report_size(rd, 0, hid_input);
168 #endif
169 os->hid_data_buf = new char[size];
170 os->hid_dlen = size;
171
172 is_joystick = 0;
173 #ifdef HAVE_USBHID_H
174 d = hid_start_parse(rd, 1 << hid_input, report_id);
175 #else
176 d = hid_start_parse(rd, 1 << hid_input);
177 #endif
178 while (hid_get_item(d, &h))
179 {
180 int usage, page, interesting_hid;
181
182 page = HID_PAGE(h.usage);
183 usage = HID_USAGE(h.usage);
184
185 /* This test is somewhat too simplistic, but this is how MicroSoft
186 * does, so I guess it works for all joysticks/game pads. */
187 is_joystick = is_joystick ||
188 (h.kind == hid_collection &&
189 page == HUP_GENERIC_DESKTOP &&
190 (usage == HUG_JOYSTICK || usage == HUG_GAME_PAD));
191
192 if (h.kind != hid_input)
193 continue;
194
195 if (!is_joystick)
196 continue;
197
198 interesting_hid = TRUE;
199 if (page == HUP_GENERIC_DESKTOP)
200 {
201 switch(usage) {
202 case HUG_X:
203 case HUG_RX:
204 case HUG_Y:
205 case HUG_RY:
206 case HUG_Z:
207 case HUG_RZ:
208 case HUG_SLIDER:
209 if (*num_axes < _JS_MAX_AXES)
210 {
211 os->axes_usage[*num_axes] = usage;
212 os->axes_minimum[*num_axes] = h.logical_minimum;
213 os->axes_maximum[*num_axes] = h.logical_maximum;
214 (*num_axes)++;
215 }
216 break;
217 case HUG_HAT_SWITCH:
218 if (*num_axes + 1 < _JS_MAX_AXES) // Allocate two axes for a hat
219 {
220 os->axes_usage[*num_axes] = usage;
221 (*num_axes)++;
222 os->axes_usage[*num_axes] = usage;
223 (*num_axes)++;
224 }
225 break;
226 default:
227 interesting_hid = FALSE;
228 }
229 }
230 else if (page == HUP_BUTTON)
231 {
232 interesting_hid = (usage > 0) && (usage <= _JS_MAX_BUTTONS);
233
234 if (interesting_hid && usage - 1 > *num_buttons)
235 {
236 *num_buttons = usage - 1;
237 }
238 }
239
240 if (interesting_hid)
241 {
242 h.next = os->hids;
243 os->hids = new struct hid_item;
244 *os->hids = h;
245 }
246 }
247 hid_end_parse(d);
248
249 return (os->hids != NULL);
250 }
251 #endif
252
253 void jsJoystick::open ()
254 {
255 char *cp;
256
257 name [0] = '\0' ;
258
259 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
260 os->cache_axes [ i ] = 0.0f ;
261
262 os->cache_buttons = 0 ;
263
264 os->fd = ::open ( os->fname, O_RDONLY | O_NONBLOCK) ;
265
266 if (os->fd < 0 && errno == EACCES)
267 fprintf(stderr, "%s exists but is not readable by you\n", os->fname);
268
269 error = ( os->fd < 0 ) ;
270
271 if ( error )
272 return ;
273
274 num_axes = 0;
275 num_buttons = 0;
276 if ( os->is_analog )
277 {
278 num_axes = 2 ;
279 num_buttons = 32 ;
280 FILE *joyfile ;
281 char joyfname [ 1024 ] ;
282 int noargs, in_no_axes ;
283
284 float axes [ _JS_MAX_AXES ] ;
285 int buttons [ _JS_MAX_AXES ] ;
286
287 rawRead ( buttons, axes ) ;
288 error = axes[0] < -1000000000.0f && axes[1] < -1000000000.0f ;
289 if ( error )
290 return ;
291
292 sprintf( joyfname, "%s/.joy%drc", ::getenv ( "HOME" ), id ) ;
293
294 joyfile = fopen ( joyfname, "r" ) ;
295 error = ( joyfile == NULL ) ;
296 if ( error )
297 {
298 jsSetError ( SG_WARN, "unable to open calibration file, you can generate "
299 "the calibration file with the plib-jscal utility" );
300 return ;
301 }
302
303 noargs = fscanf ( joyfile, "%d%f%f%f%f%f%f", &in_no_axes,
304 &min [ 0 ], &center [ 0 ], &max [ 0 ],
305 &min [ 1 ], &center [ 1 ], &max [ 1 ] ) ;
306 error = noargs != 7 || in_no_axes != _JS_MAX_AXES ;
307 fclose ( joyfile ) ;
308 if ( error )
309 return ;
310
311 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
312 {
313 dead_band [ i ] = 0.0f ;
314 saturate [ i ] = 1.0f ;
315 }
316
317 return; // End of analog code
318 }
319
320 #ifdef HAVE_USB_JS
321 if ( !joy_initialize_hid(os, &num_axes, &num_buttons ) )
322 {
323 ::close(os->fd);
324 error = 1;
325 return;
326 }
327
328 cp = strrchr(os->fname, '/');
329 if (cp) {
330 if (findusbdev(&cp[1], name, sizeof(name)) == 0)
331 strcpy(name, &cp[1]);
332 }
333
334 if ( num_axes > _JS_MAX_AXES )
335 num_axes = _JS_MAX_AXES ;
336
337 for ( int i = 0 ; i < _JS_MAX_AXES ; i++ )
338 {
339 if ( os->axes_usage [ i ] == HUG_HAT_SWITCH )
340 {
341 max [ i ] = 1.0f ;
342 center [ i ] = 0.0f ;
343 min [ i ] = -1.0f ;
344 }
345 else
346 {
347 max [ i ] = os->axes_maximum [ i ];
348 min [ i ] = os->axes_minimum [ i ];
349 center [ i ] = (max [ i ] + min [ i ]) / 2;
350 }
351 dead_band [ i ] = 0.0f ;
352 saturate [ i ] = 1.0f ;
353 }
354 #endif
355 }
356
357
358
359 void jsJoystick::close ()
360 {
361 if (os) {
362 if ( ! error )
363 ::close ( os->fd ) ;
364 #ifdef HAVE_USB_JS
365 if (os->hids)
366 delete os->hids;
367 if (os->hid_data_buf)
368 delete os->hid_data_buf;
369 #endif
370 delete os;
371 }
372 }
373
374
375 jsJoystick::jsJoystick ( int ident )
376 {
377 id = ident ;
378 error = 0;
379
380 os = new struct os_specific_s;
381 memset(os, 0, sizeof(struct os_specific_s));
382 if (ident < USB_IDENT_OFFSET)
383 os->is_analog = 1;
384 if (os->is_analog)
385 sprintf(os->fname, "%s%d", AJSDEV, ident);
386 else
387 sprintf(os->fname, "%s%d", UHIDDEV, ident - USB_IDENT_OFFSET);
388 open () ;
389 }
390
391
392 void jsJoystick::rawRead ( int *buttons, float *axes )
393 {
394 int len, usage, page, d;
395 struct hid_item *h;
396
397 if ( error )
398 {
399 if ( buttons )
400 *buttons = 0 ;
401
402 if ( axes )
403 for ( int i = 0 ; i < num_axes ; i++ )
404 axes[i] = 1500.0f ;
405
406 return ;
407 }
408
409 if ( os->is_analog )
410 {
411 int status = ::read ( os->fd, &os->ajs, sizeof(os->ajs) );
412 if ( status != sizeof(os->ajs) ) {
413 perror ( os->fname ) ;
414 setError () ;
415 return ;
416 }
417 if ( buttons != NULL )
418 *buttons = ( os->ajs.b1 ? 1 : 0 ) | ( os->ajs.b2 ? 2 : 0 ) ;
419
420 if ( axes != NULL )
421 {
422 if ( os->ajs.x >= -1000000000 )
423 os->cache_axes[0] = os->ajs.x;
424 if ( os->ajs.y >= -1000000000 )
425 os->cache_axes[1] = os->ajs.y;
426
427 axes[0] = os->cache_axes[0];
428 axes[1] = os->cache_axes[1];
429 }
430
431 return;
432 }
433
434 #ifdef HAVE_USB_JS
435 while ((len = ::read(os->fd, os->hid_data_buf, os->hid_dlen)) == os->hid_dlen)
436 {
437 for (h = os->hids; h; h = h->next)
438 {
439 d = hid_get_data(os->hid_data_buf, h);
440
441 page = HID_PAGE(h->usage);
442 usage = HID_USAGE(h->usage);
443
444 if (page == HUP_GENERIC_DESKTOP)
445 {
446 for (int i = 0; i < num_axes; i++)
447 if (os->axes_usage[i] == usage)
448 {
449 if (usage == HUG_HAT_SWITCH)
450 {
451 if (d < 0 || d > 8)
452 d = 0; // safety
453 os->cache_axes[i] = (float)hatmap_x[d];
454 os->cache_axes[i + 1] = (float)hatmap_y[d];
455 }
456 else
457 {
458 os->cache_axes[i] = (float)d;
459 }
460 break;
461 }
462 }
463 else if (page == HUP_BUTTON)
464 {
465 if (usage > 0 && usage < _JS_MAX_BUTTONS + 1)
466 {
467 if (d)
468 os->cache_buttons |= (1 << usage - 1) ;
469 else
470 os->cache_buttons &= ~(1 << usage - 1) ;
471 }
472 }
473 }
474 }
475 if (len < 0 && errno != EAGAIN)
476 {
477 perror( os->fname ) ;
478 setError () ;
479 error = 1;
480 }
481 if ( buttons != NULL ) *buttons = os->cache_buttons ;
482 if ( axes != NULL )
483 memcpy ( axes, os->cache_axes, sizeof(float) * num_axes ) ;
484 #endif
485 }
486
487 void jsInit () {}