removed all HAVE_ERRNO checks
[freeglut] / src / x11 / fg_joystick_x11.c
1 /*
2  * fg_joystick_x11.c
3  *
4  * Joystick handling code
5  *
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
10  *
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:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
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.
27  */
28
29 /*
30  * FreeBSD port by Stephen Montgomery-Smith <stephen@math.missouri.edu>
31  *
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.
34  */
35
36 #include <GL/freeglut.h>
37 #include "../fg_internal.h"
38 #ifdef HAVE_SYS_PARAM_H
39 #    include <sys/param.h>
40 #endif
41
42
43 /*this should be defined in a header file */
44 #define MAX_NUM_JOYSTICKS  2   
45 extern SFG_Joystick *fgJoystick [ MAX_NUM_JOYSTICKS ];
46
47 void fgPlatformJoystickRawRead( SFG_Joystick* joy, int* buttons, float* axes )
48 {
49     int status;
50
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
52     int len;
53
54     if ( joy->pJoystick.os->is_analog )
55     {
56         int status = read ( joy->pJoystick.os->fd, &joy->pJoystick.os->ajs, sizeof(joy->pJoystick.os->ajs) );
57         if ( status != sizeof(joy->pJoystick.os->ajs) ) {
58             perror ( joy->pJoystick.os->fname );
59             joy->error = GL_TRUE;
60             return;
61         }
62         if ( buttons != NULL )
63             *buttons = ( joy->pJoystick.os->ajs.b1 ? 1 : 0 ) | ( joy->pJoystick.os->ajs.b2 ? 2 : 0 );
64
65         if ( axes != NULL )
66         {
67             axes[0] = (float) joy->pJoystick.os->ajs.x;
68             axes[1] = (float) joy->pJoystick.os->ajs.y;
69         }
70
71         return;
72     }
73
74 #  ifdef HAVE_USB_JS
75     while ( ( len = read ( joy->pJoystick.os->fd, joy->pJoystick.os->hid_data_buf, joy->pJoystick.os->hid_dlen ) ) == joy->pJoystick.os->hid_dlen )
76     {
77         struct hid_item *h;
78
79         for  ( h = joy->pJoystick.os->hids; h; h = h->next )
80         {
81             int d = hid_get_data ( joy->pJoystick.os->hid_data_buf, h );
82
83             int page = HID_PAGE ( h->usage );
84             int usage = HID_USAGE ( h->usage );
85
86             if ( page == HUP_GENERIC_DESKTOP )
87             {
88                 int i;
89                 for ( i = 0; i < joy->num_axes; i++ )
90                     if (joy->pJoystick.os->axes_usage[i] == usage)
91                     {
92                         if (usage == HUG_HAT_SWITCH)
93                         {
94                             if (d < 0 || d > 8)
95                                 d = 0;  /* safety */
96                             joy->pJoystick.os->cache_axes[i] = (float)hatmap_x[d];
97                             joy->pJoystick.os->cache_axes[i + 1] = (float)hatmap_y[d];
98                         }
99                         else
100                         {
101                             joy->pJoystick.os->cache_axes[i] = (float)d;
102                         }
103                         break;
104                     }
105             }
106             else if (page == HUP_BUTTON)
107             {
108                if (usage > 0 && usage < _JS_MAX_BUTTONS + 1)
109                {
110                    if (d)
111                        joy->pJoystick.os->cache_buttons |=  (1 << ( usage - 1 ));
112                    else
113                        joy->pJoystick.os->cache_buttons &= ~(1 << ( usage - 1 ));
114                }
115             }
116         }
117     }
118     if ( len < 0 && errno != EAGAIN )
119     {
120         perror( joy->pJoystick.os->fname );
121         joy->error = 1;
122     }
123     if ( buttons != NULL ) *buttons = joy->pJoystick.os->cache_buttons;
124     if ( axes    != NULL )
125         memcpy ( axes, joy->pJoystick.os->cache_axes, sizeof(float) * joy->num_axes );
126 #  endif
127 #endif
128
129 #ifdef JS_NEW
130
131     while ( 1 )
132     {
133         status = read ( joy->pJoystick.fd, &joy->pJoystick.js, sizeof(struct js_event) );
134
135         if ( status != sizeof( struct js_event ) )
136         {
137             if ( errno == EAGAIN )
138             {
139                 /* Use the old values */
140                 if ( buttons )
141                     *buttons = joy->pJoystick.tmp_buttons;
142                 if ( axes )
143                     memcpy( axes, joy->pJoystick.tmp_axes,
144                             sizeof( float ) * joy->num_axes );
145                 return;
146             }
147
148             fgWarning ( "%s", joy->pJoystick.fname );
149             joy->error = GL_TRUE;
150             return;
151         }
152
153         switch ( joy->pJoystick.js.type & ~JS_EVENT_INIT )
154         {
155         case JS_EVENT_BUTTON:
156             if( joy->pJoystick.js.value == 0 ) /* clear the flag */
157                 joy->pJoystick.tmp_buttons &= ~( 1 << joy->pJoystick.js.number );
158             else
159                 joy->pJoystick.tmp_buttons |= ( 1 << joy->pJoystick.js.number );
160             break;
161
162         case JS_EVENT_AXIS:
163             if ( joy->pJoystick.js.number < joy->num_axes )
164             {
165                 joy->pJoystick.tmp_axes[ joy->pJoystick.js.number ] = ( float )joy->pJoystick.js.value;
166
167                 if( axes )
168                     memcpy( axes, joy->pJoystick.tmp_axes, sizeof(float) * joy->num_axes );
169             }
170             break;
171
172         default:
173             fgWarning ( "PLIB_JS: Unrecognised /dev/js return!?!" );
174
175             /* use the old values */
176
177             if ( buttons != NULL ) *buttons = joy->pJoystick.tmp_buttons;
178             if ( axes    != NULL )
179                 memcpy ( axes, joy->pJoystick.tmp_axes, sizeof(float) * joy->num_axes );
180
181             return;
182         }
183
184         if( buttons )
185             *buttons = joy->pJoystick.tmp_buttons;
186     }
187 #else
188
189     status = read( joy->pJoystick.fd, &joy->pJoystick.js, JS_RETURN );
190
191     if ( status != JS_RETURN )
192     {
193         fgWarning( "%s", joy->pJoystick.fname );
194         joy->error = GL_TRUE;
195         return;
196     }
197
198     if ( buttons )
199 #    if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
200         *buttons = ( joy->pJoystick.js.b1 ? 1 : 0 ) | ( joy->pJoystick.js.b2 ? 2 : 0 );  /* XXX Should not be here -- BSD is handled earlier */
201 #    else
202         *buttons = joy->pJoystick.js.buttons;
203 #    endif
204
205     if ( axes )
206     {
207         axes[ 0 ] = (float) joy->pJoystick.js.x;
208         axes[ 1 ] = (float) joy->pJoystick.js.y;
209     }
210 #endif
211 }
212
213
214 void fgPlatformJoystickOpen( SFG_Joystick* joy )
215 {
216 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
217         int i = 0;
218        char *cp;
219 #endif
220 #ifdef JS_NEW
221        unsigned char u;
222         int i=0;
223 #else
224 #  if defined( __linux__ ) || TARGET_HOST_SOLARIS
225         int i = 0;
226     int counter = 0;
227 #  endif
228 #endif
229
230 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
231     for( i = 0; i < _JS_MAX_AXES; i++ )
232         joy->pJoystick.os->cache_axes[ i ] = 0.0f;
233
234     joy->pJoystick.os->cache_buttons = 0;
235
236     joy->pJoystick.os->fd = open( joy->pJoystick.os->fname, O_RDONLY | O_NONBLOCK);
237
238     if( joy->pJoystick.os->fd < 0 && errno == EACCES )
239         fgWarning ( "%s exists but is not readable by you", joy->pJoystick.os->fname );
240
241     joy->error =( joy->pJoystick.os->fd < 0 );
242
243     if( joy->error )
244         return;
245
246     joy->num_axes = 0;
247     joy->num_buttons = 0;
248     if( joy->pJoystick.os->is_analog )
249     {
250         FILE *joyfile;
251         char joyfname[ 1024 ];
252         int noargs, in_no_axes;
253
254         float axes [ _JS_MAX_AXES ];
255         int buttons[ _JS_MAX_AXES ];
256
257         joy->num_axes    =  2;
258         joy->num_buttons = 32;
259
260         fghJoystickRawRead( joy, buttons, axes );
261         joy->error = axes[ 0 ] < -1000000000.0f;
262         if( joy->error )
263             return;
264
265         snprintf( joyfname, sizeof(joyfname), "%s/.joy%drc", getenv( "HOME" ), joy->id );
266
267         joyfile = fopen( joyfname, "r" );
268         joy->error =( joyfile == NULL );
269         if( joy->error )
270             return;
271
272         noargs = fscanf( joyfile, "%d%f%f%f%f%f%f", &in_no_axes,
273                          &joy->min[ 0 ], &joy->center[ 0 ], &joy->max[ 0 ],
274                          &joy->min[ 1 ], &joy->center[ 1 ], &joy->max[ 1 ] );
275         joy->error = noargs != 7 || in_no_axes != _JS_MAX_AXES;
276         fclose( joyfile );
277         if( joy->error )
278             return;
279
280         for( i = 0; i < _JS_MAX_AXES; i++ )
281         {
282             joy->dead_band[ i ] = 0.0f;
283             joy->saturate [ i ] = 1.0f;
284         }
285
286         return;    /* End of analog code */
287     }
288
289 #    ifdef HAVE_USB_JS
290     if( ! fghJoystickInitializeHID( joy->pJoystick.os, &joy->num_axes,
291                                     &joy->num_buttons ) )
292     {
293         close( joy->pJoystick.os->fd );
294         joy->error = GL_TRUE;
295         return;
296     }
297
298     cp = strrchr( joy->pJoystick.os->fname, '/' );
299     if( cp )
300     {
301         if( fghJoystickFindUSBdev( &cp[1], joy->name, sizeof( joy->name ) ) ==
302             0 )
303             strcpy( joy->name, &cp[1] );
304     }
305
306     if( joy->num_axes > _JS_MAX_AXES )
307         joy->num_axes = _JS_MAX_AXES;
308
309     for( i = 0; i < _JS_MAX_AXES; i++ )
310     {
311         /* We really should get this from the HID, but that data seems
312          * to be quite unreliable for analog-to-USB converters. Punt for
313          * now.
314          */
315         if( joy->pJoystick.os->axes_usage[ i ] == HUG_HAT_SWITCH )
316         {
317             joy->max   [ i ] = 1.0f;
318             joy->center[ i ] = 0.0f;
319             joy->min   [ i ] = -1.0f;
320         }
321         else
322         {
323             joy->max   [ i ] = 255.0f;
324             joy->center[ i ] = 127.0f;
325             joy->min   [ i ] = 0.0f;
326         }
327
328         joy->dead_band[ i ] = 0.0f;
329         joy->saturate[ i ] = 1.0f;
330     }
331 #    endif
332 #endif
333
334 #if defined( __linux__ ) || TARGET_HOST_SOLARIS
335     /* Default for older Linux systems. */
336     joy->num_axes    =  2;
337     joy->num_buttons = 32;
338
339 #    ifdef JS_NEW
340     for( i = 0; i < _JS_MAX_AXES; i++ )
341         joy->pJoystick.tmp_axes[ i ] = 0.0f;
342
343     joy->pJoystick.tmp_buttons = 0;
344 #    endif
345
346     joy->pJoystick.fd = open( joy->pJoystick.fname, O_RDONLY );
347
348     joy->error =( joy->pJoystick.fd < 0 );
349
350     if( joy->error )
351         return;
352
353     /* Set the correct number of axes for the linux driver */
354 #    ifdef JS_NEW
355     /* Melchior Franz's fixes for big-endian Linuxes since writing
356      *  to the upper byte of an uninitialized word doesn't work.
357      *  9 April 2003
358      */
359     ioctl( joy->pJoystick.fd, JSIOCGAXES, &u );
360     joy->num_axes = u;
361     ioctl( joy->pJoystick.fd, JSIOCGBUTTONS, &u );
362     joy->num_buttons = u;
363     ioctl( joy->pJoystick.fd, JSIOCGNAME( sizeof( joy->name ) ), joy->name );
364     fcntl( joy->pJoystick.fd, F_SETFL, O_NONBLOCK );
365 #    endif
366
367     /*
368      * The Linux driver seems to return 512 for all axes
369      * when no stick is present - but there is a chance
370      * that could happen by accident - so it's gotta happen
371      * on both axes for at least 100 attempts.
372      *
373      * PWO: shouldn't be that done somehow wiser on the kernel level?
374      */
375 #    ifndef JS_NEW
376     counter = 0;
377
378     do
379     {
380         fghJoystickRawRead( joy, NULL, joy->center );
381         counter++;
382     } while( !joy->error &&
383              counter < 100 &&
384              joy->center[ 0 ] == 512.0f &&
385              joy->center[ 1 ] == 512.0f );
386
387     if ( counter >= 100 )
388         joy->error = GL_TRUE;
389 #    endif
390
391     for( i = 0; i < _JS_MAX_AXES; i++ )
392     {
393 #    ifdef JS_NEW
394         joy->max   [ i ] =  32767.0f;
395         joy->center[ i ] =      0.0f;
396         joy->min   [ i ] = -32767.0f;
397 #    else
398         joy->max[ i ] = joy->center[ i ] * 2.0f;
399         joy->min[ i ] = 0.0f;
400 #    endif
401         joy->dead_band[ i ] = 0.0f;
402         joy->saturate [ i ] = 1.0f;
403     }
404 #endif
405 }
406
407
408 void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident )
409 {
410 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
411     fgJoystick[ ident ]->id = ident;
412     fgJoystick[ ident ]->error = GL_FALSE;
413
414     fgJoystick[ ident ]->pJoystick.os = calloc( 1, sizeof( struct os_specific_s ) );
415     memset( fgJoystick[ ident ]->pJoystick.os, 0, sizeof( struct os_specific_s ) );
416     if( ident < USB_IDENT_OFFSET )
417         fgJoystick[ ident ]->pJoystick.os->is_analog = 1;
418     if( fgJoystick[ ident ]->pJoystick.os->is_analog )
419         snprintf( fgJoystick[ ident ]->pJoystick.os->fname, sizeof(fgJoystick[ ident ]->pJoystick.os->fname), "%s%d", AJSDEV, ident );
420     else
421         snprintf( fgJoystick[ ident ]->pJoystick.os->fname, sizeof(fgJoystick[ ident ]->pJoystick.os->fname), "%s%d", UHIDDEV,
422                  ident - USB_IDENT_OFFSET );
423 #elif defined( __linux__ )
424     fgJoystick[ ident ]->id = ident;
425     fgJoystick[ ident ]->error = GL_FALSE;
426
427     snprintf( fgJoystick[ident]->pJoystick.fname, sizeof(fgJoystick[ident]->pJoystick.fname), "/dev/input/js%d", ident );
428
429     if( access( fgJoystick[ ident ]->pJoystick.fname, F_OK ) != 0 )
430         snprintf( fgJoystick[ ident ]->pJoystick.fname, sizeof(fgJoystick[ ident ]->pJoystick.fname), "/dev/js%d", ident );
431 #endif
432 }
433
434
435 void fgPlatformJoystickClose ( int ident )
436 {
437 #if defined( __FreeBSD__ ) || defined(__FreeBSD_kernel__) || defined( __NetBSD__ )
438     if( fgJoystick[ident]->pJoystick.os )
439     {
440         if( ! fgJoystick[ ident ]->error )
441             close( fgJoystick[ ident ]->pJoystick.os->fd );
442 #ifdef HAVE_USB_JS
443         if( fgJoystick[ ident ]->pJoystick.os->hids )
444             free (fgJoystick[ ident ]->pJoystick.os->hids);
445         if( fgJoystick[ ident ]->pJoystick.os->hid_data_buf )
446             free( fgJoystick[ ident ]->pJoystick.os->hid_data_buf );
447 #endif
448         free( fgJoystick[ident]->pJoystick.os );
449         }
450 #endif
451
452     if( ! fgJoystick[ident]->error )
453          close( fgJoystick[ ident ]->pJoystick.fd );
454 }
455