4 * Joystick handling code
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Steve Baker, <sjbaker1@airmail.net>
8 * Copied for Platform code by Evan Felix <karcaw at gmail.com>
9 * Creation date: Thur Feb 2 2012
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
25 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 * FreeBSD port by Stephen Montgomery-Smith <stephen@math.missouri.edu>
32 * Redone by John Fay 2/4/04 with another look from the PLIB "js" library.
33 * Many thanks for Steve Baker for permission to pull from that library.
36 #include <GL/freeglut.h>
37 #include "../fg_internal.h"
38 #ifdef HAVE_SYS_PARAM_H
39 # include <sys/param.h>
45 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47 static int hatmap_x[9] = {0, 0, 1, 1, 1, 0, -1, -1, -1};
48 static int hatmap_y[9] = {0, 1, 1, 0, -1, -1, -1, 0, 1};
52 * fghJoystickFindUSBdev (and its helper, fghJoystickWalkUSBdev) try to locate
53 * the full name of a USB device. If /dev/usbN isn't readable, we punt and
54 * return the uhidN device name. We warn the user of this situation once.
56 static char *fghJoystickWalkUSBdev(int f, char *dev, char *out, int outlen)
58 struct usb_device_info di;
62 for (a = 1; a < USB_MAX_DEVICES; a++) {
64 if (ioctl(f, USB_DEVICEINFO, &di) != 0)
66 for (i = 0; i < USB_MAX_DEVNAMES; i++)
67 if (di.udi_devnames[i][0] &&
68 strcmp(di.udi_devnames[i], dev) == 0) {
69 cp = calloc(1, strlen(di.udi_vendor) + strlen(di.udi_product) + 2);
70 strcpy(cp, di.udi_vendor);
72 strcat(cp, di.udi_product);
73 strncpy(out, cp, outlen - 1);
82 static int fghJoystickFindUSBdev(char *name, char *out, int outlen)
87 static int protection_warned = 0;
89 for (i = 0; i < 16; i++) {
90 snprintf(buf, sizeof(buf), "%s%d", USBDEV, i);
91 f = open(buf, O_RDONLY);
93 cp = fghJoystickWalkUSBdev(f, name, out, outlen);
98 else if (errno == EACCES) {
99 if (!protection_warned) {
100 fgWarning("Can't open %s for read!", buf);
101 protection_warned = 1;
108 static int fghJoystickInitializeHID(struct os_specific_s *os,
109 int *num_axes, int *num_buttons)
111 int size, is_joystick;
112 # ifdef HAVE_USBHID_H
119 if ((rd = hid_get_report_desc(os->fd)) == 0)
121 fgWarning("error: %s: %s", os->fname, strerror(errno));
127 # ifdef HAVE_USBHID_H
128 if (ioctl(os->fd, USB_GET_REPORT_ID, &report_id) < 0)
130 /*** XXX {report_id} may not be the right variable? ***/
131 fgWarning("error: %s%d: %s", UHIDDEV, report_id, strerror(errno));
135 size = hid_report_size(rd, hid_input, report_id);
137 size = hid_report_size(rd, 0, hid_input);
139 os->hid_data_buf = calloc(1, size);
143 # ifdef HAVE_USBHID_H
144 d = hid_start_parse(rd, 1 << hid_input, report_id);
146 d = hid_start_parse(rd, 1 << hid_input);
148 while (hid_get_item(d, &h))
150 int usage, page, interesting_hid;
152 page = HID_PAGE(h.usage);
153 usage = HID_USAGE(h.usage);
155 /* This test is somewhat too simplistic, but this is how MicroSoft
156 * does, so I guess it works for all joysticks/game pads. */
157 is_joystick = is_joystick ||
158 (h.kind == hid_collection &&
159 page == HUP_GENERIC_DESKTOP &&
160 (usage == HUG_JOYSTICK || usage == HUG_GAME_PAD));
162 if (h.kind != hid_input)
168 interesting_hid = TRUE;
169 if (page == HUP_GENERIC_DESKTOP)
180 if (*num_axes < _JS_MAX_AXES)
182 os->axes_usage[*num_axes] = usage;
187 /* Allocate two axes for a hat */
188 if (*num_axes + 1 < _JS_MAX_AXES)
190 os->axes_usage[*num_axes] = usage;
192 os->axes_usage[*num_axes] = usage;
197 interesting_hid = FALSE;
201 else if (page == HUP_BUTTON)
203 interesting_hid = (usage > 0) &&
204 (usage <= _JS_MAX_BUTTONS);
206 if (interesting_hid && usage - 1 > *num_buttons)
207 *num_buttons = usage - 1;
213 os->hids = calloc(1, sizeof(struct hid_item));
219 return os->hids != NULL;
225 /*this should be defined in a header file */
226 #define MAX_NUM_JOYSTICKS 2
227 extern SFG_Joystick *fgJoystick [ MAX_NUM_JOYSTICKS ];
229 void fgPlatformJoystickRawRead( SFG_Joystick* joy, int* buttons, float* axes )
233 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
236 if ( joy->pJoystick.os->is_analog )
238 int status = read ( joy->pJoystick.os->fd, &joy->pJoystick.os->ajs, sizeof(joy->pJoystick.os->ajs) );
239 if ( status != sizeof(joy->pJoystick.os->ajs) ) {
240 perror ( joy->pJoystick.os->fname );
241 joy->error = GL_TRUE;
244 if ( buttons != NULL )
245 *buttons = ( joy->pJoystick.os->ajs.b1 ? 1 : 0 ) | ( joy->pJoystick.os->ajs.b2 ? 2 : 0 );
249 axes[0] = (float) joy->pJoystick.os->ajs.x;
250 axes[1] = (float) joy->pJoystick.os->ajs.y;
257 while ( ( len = read ( joy->pJoystick.os->fd, joy->pJoystick.os->hid_data_buf, joy->pJoystick.os->hid_dlen ) ) == joy->pJoystick.os->hid_dlen )
261 for ( h = joy->pJoystick.os->hids; h; h = h->next )
263 int d = hid_get_data ( joy->pJoystick.os->hid_data_buf, h );
265 int page = HID_PAGE ( h->usage );
266 int usage = HID_USAGE ( h->usage );
268 if ( page == HUP_GENERIC_DESKTOP )
271 for ( i = 0; i < joy->num_axes; i++ )
272 if (joy->pJoystick.os->axes_usage[i] == usage)
274 if (usage == HUG_HAT_SWITCH)
278 joy->pJoystick.os->cache_axes[i] = (float)hatmap_x[d];
279 joy->pJoystick.os->cache_axes[i + 1] = (float)hatmap_y[d];
283 joy->pJoystick.os->cache_axes[i] = (float)d;
288 else if (page == HUP_BUTTON)
290 if (usage > 0 && usage < _JS_MAX_BUTTONS + 1)
293 joy->pJoystick.os->cache_buttons |= (1 << ( usage - 1 ));
295 joy->pJoystick.os->cache_buttons &= ~(1 << ( usage - 1 ));
300 if ( len < 0 && errno != EAGAIN )
302 perror( joy->pJoystick.os->fname );
305 if ( buttons != NULL ) *buttons = joy->pJoystick.os->cache_buttons;
307 memcpy ( axes, joy->pJoystick.os->cache_axes, sizeof(float) * joy->num_axes );
315 status = read ( joy->pJoystick.fd, &joy->pJoystick.js, sizeof(struct js_event) );
317 if ( status != sizeof( struct js_event ) )
319 if ( errno == EAGAIN )
321 /* Use the old values */
323 *buttons = joy->pJoystick.tmp_buttons;
325 memcpy( axes, joy->pJoystick.tmp_axes,
326 sizeof( float ) * joy->num_axes );
330 fgWarning ( "%s", joy->pJoystick.fname );
331 joy->error = GL_TRUE;
335 switch ( joy->pJoystick.js.type & ~JS_EVENT_INIT )
337 case JS_EVENT_BUTTON:
338 if( joy->pJoystick.js.value == 0 ) /* clear the flag */
339 joy->pJoystick.tmp_buttons &= ~( 1 << joy->pJoystick.js.number );
341 joy->pJoystick.tmp_buttons |= ( 1 << joy->pJoystick.js.number );
345 if ( joy->pJoystick.js.number < joy->num_axes )
347 joy->pJoystick.tmp_axes[ joy->pJoystick.js.number ] = ( float )joy->pJoystick.js.value;
350 memcpy( axes, joy->pJoystick.tmp_axes, sizeof(float) * joy->num_axes );
355 fgWarning ( "PLIB_JS: Unrecognised /dev/js return!?!" );
357 /* use the old values */
359 if ( buttons != NULL ) *buttons = joy->pJoystick.tmp_buttons;
361 memcpy ( axes, joy->pJoystick.tmp_axes, sizeof(float) * joy->num_axes );
367 *buttons = joy->pJoystick.tmp_buttons;
371 status = read( joy->pJoystick.fd, &joy->pJoystick.js, JS_RETURN );
373 if ( status != JS_RETURN )
375 fgWarning( "%s", joy->pJoystick.fname );
376 joy->error = GL_TRUE;
381 # if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
382 *buttons = ( joy->pJoystick.js.b1 ? 1 : 0 ) | ( joy->pJoystick.js.b2 ? 2 : 0 ); /* XXX Should not be here -- BSD is handled earlier */
384 *buttons = joy->pJoystick.js.buttons;
389 axes[ 0 ] = (float) joy->pJoystick.js.x;
390 axes[ 1 ] = (float) joy->pJoystick.js.y;
396 void fgPlatformJoystickOpen( SFG_Joystick* joy )
398 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__)
406 # if defined( __linux__ ) || TARGET_HOST_SOLARIS
412 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__)
413 for( i = 0; i < _JS_MAX_AXES; i++ )
414 joy->pJoystick.os->cache_axes[ i ] = 0.0f;
416 joy->pJoystick.os->cache_buttons = 0;
418 joy->pJoystick.os->fd = open( joy->pJoystick.os->fname, O_RDONLY | O_NONBLOCK);
420 if( joy->pJoystick.os->fd < 0 && errno == EACCES )
421 fgWarning ( "%s exists but is not readable by you", joy->pJoystick.os->fname );
423 joy->error =( joy->pJoystick.os->fd < 0 );
429 joy->num_buttons = 0;
430 if( joy->pJoystick.os->is_analog )
433 char joyfname[ 1024 ];
434 int noargs, in_no_axes;
436 float axes [ _JS_MAX_AXES ];
437 int buttons[ _JS_MAX_AXES ];
440 joy->num_buttons = 32;
442 fghJoystickRawRead( joy, buttons, axes );
443 joy->error = axes[ 0 ] < -1000000000.0f;
447 snprintf( joyfname, sizeof(joyfname), "%s/.joy%drc", getenv( "HOME" ), joy->id );
449 joyfile = fopen( joyfname, "r" );
450 joy->error =( joyfile == NULL );
454 noargs = fscanf( joyfile, "%d%f%f%f%f%f%f", &in_no_axes,
455 &joy->min[ 0 ], &joy->center[ 0 ], &joy->max[ 0 ],
456 &joy->min[ 1 ], &joy->center[ 1 ], &joy->max[ 1 ] );
457 joy->error = noargs != 7 || in_no_axes != _JS_MAX_AXES;
462 for( i = 0; i < _JS_MAX_AXES; i++ )
464 joy->dead_band[ i ] = 0.0f;
465 joy->saturate [ i ] = 1.0f;
468 return; /* End of analog code */
472 if( ! fghJoystickInitializeHID( joy->pJoystick.os, &joy->num_axes,
473 &joy->num_buttons ) )
475 close( joy->pJoystick.os->fd );
476 joy->error = GL_TRUE;
480 cp = strrchr( joy->pJoystick.os->fname, '/' );
483 if( fghJoystickFindUSBdev( &cp[1], joy->name, sizeof( joy->name ) ) ==
485 strcpy( joy->name, &cp[1] );
488 if( joy->num_axes > _JS_MAX_AXES )
489 joy->num_axes = _JS_MAX_AXES;
491 for( i = 0; i < _JS_MAX_AXES; i++ )
493 /* We really should get this from the HID, but that data seems
494 * to be quite unreliable for analog-to-USB converters. Punt for
497 if( joy->pJoystick.os->axes_usage[ i ] == HUG_HAT_SWITCH )
499 joy->max [ i ] = 1.0f;
500 joy->center[ i ] = 0.0f;
501 joy->min [ i ] = -1.0f;
505 joy->max [ i ] = 255.0f;
506 joy->center[ i ] = 127.0f;
507 joy->min [ i ] = 0.0f;
510 joy->dead_band[ i ] = 0.0f;
511 joy->saturate[ i ] = 1.0f;
516 #if defined( __linux__ ) || TARGET_HOST_SOLARIS
517 /* Default for older Linux systems. */
519 joy->num_buttons = 32;
522 for( i = 0; i < _JS_MAX_AXES; i++ )
523 joy->pJoystick.tmp_axes[ i ] = 0.0f;
525 joy->pJoystick.tmp_buttons = 0;
528 joy->pJoystick.fd = open( joy->pJoystick.fname, O_RDONLY );
530 joy->error =( joy->pJoystick.fd < 0 );
535 /* Set the correct number of axes for the linux driver */
537 /* Melchior Franz's fixes for big-endian Linuxes since writing
538 * to the upper byte of an uninitialized word doesn't work.
541 ioctl( joy->pJoystick.fd, JSIOCGAXES, &u );
543 ioctl( joy->pJoystick.fd, JSIOCGBUTTONS, &u );
544 joy->num_buttons = u;
545 ioctl( joy->pJoystick.fd, JSIOCGNAME( sizeof( joy->name ) ), joy->name );
546 fcntl( joy->pJoystick.fd, F_SETFL, O_NONBLOCK );
550 * The Linux driver seems to return 512 for all axes
551 * when no stick is present - but there is a chance
552 * that could happen by accident - so it's gotta happen
553 * on both axes for at least 100 attempts.
555 * PWO: shouldn't be that done somehow wiser on the kernel level?
562 fghJoystickRawRead( joy, NULL, joy->center );
564 } while( !joy->error &&
566 joy->center[ 0 ] == 512.0f &&
567 joy->center[ 1 ] == 512.0f );
569 if ( counter >= 100 )
570 joy->error = GL_TRUE;
573 for( i = 0; i < _JS_MAX_AXES; i++ )
576 joy->max [ i ] = 32767.0f;
577 joy->center[ i ] = 0.0f;
578 joy->min [ i ] = -32767.0f;
580 joy->max[ i ] = joy->center[ i ] * 2.0f;
581 joy->min[ i ] = 0.0f;
583 joy->dead_band[ i ] = 0.0f;
584 joy->saturate [ i ] = 1.0f;
590 void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident )
592 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__)
593 fgJoystick[ ident ]->id = ident;
594 fgJoystick[ ident ]->error = GL_FALSE;
596 fgJoystick[ ident ]->pJoystick.os = calloc( 1, sizeof( struct os_specific_s ) );
597 memset( fgJoystick[ ident ]->pJoystick.os, 0, sizeof( struct os_specific_s ) );
598 if( ident < USB_IDENT_OFFSET )
599 fgJoystick[ ident ]->pJoystick.os->is_analog = 1;
600 if( fgJoystick[ ident ]->pJoystick.os->is_analog )
601 snprintf( fgJoystick[ ident ]->pJoystick.os->fname, sizeof(fgJoystick[ ident ]->pJoystick.os->fname), "%s%d", AJSDEV, ident );
603 snprintf( fgJoystick[ ident ]->pJoystick.os->fname, sizeof(fgJoystick[ ident ]->pJoystick.os->fname), "%s%d", UHIDDEV,
604 ident - USB_IDENT_OFFSET );
605 #elif defined( __linux__ )
606 fgJoystick[ ident ]->id = ident;
607 fgJoystick[ ident ]->error = GL_FALSE;
609 snprintf( fgJoystick[ident]->pJoystick.fname, sizeof(fgJoystick[ident]->pJoystick.fname), "/dev/input/js%d", ident );
611 if( access( fgJoystick[ ident ]->pJoystick.fname, F_OK ) != 0 )
612 snprintf( fgJoystick[ ident ]->pJoystick.fname, sizeof(fgJoystick[ ident ]->pJoystick.fname), "/dev/js%d", ident );
617 void fgPlatformJoystickClose ( int ident )
619 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__)
620 if( fgJoystick[ident]->pJoystick.os )
622 if( ! fgJoystick[ ident ]->error )
623 close( fgJoystick[ ident ]->pJoystick.os->fd );
625 if( fgJoystick[ ident ]->pJoystick.os->hids )
626 free (fgJoystick[ ident ]->pJoystick.os->hids);
627 if( fgJoystick[ ident ]->pJoystick.os->hid_data_buf )
628 free( fgJoystick[ ident ]->pJoystick.os->hid_data_buf );
630 free( fgJoystick[ident]->pJoystick.os );
634 if( ! fgJoystick[ident]->error )
635 close( fgJoystick[ ident ]->pJoystick.fd );