1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
//========================================================================
// GLFW - An OpenGL framework
// File: x11_joystick.c
// Platform: X11 (Unix)
// API version: 2.6
// WWW: http://glfw.sourceforge.net
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Camilla Berglund
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
//========================================================================
// Note: Only Linux joystick input is supported at the moment. Other
// systems will behave as if there are no joysticks connected.
//========================================================================
#ifdef linux
#define _GLFW_USE_LINUX_JOYSTICKS
#endif // linux
//************************************************************************
//**** GLFW internal functions ****
//************************************************************************
#ifdef _GLFW_USE_LINUX_JOYSTICKS
//------------------------------------------------------------------------
// Here are the Linux joystick driver v1.x interface definitions that we
// use (we do not want to rely on <linux/joystick.h>):
//------------------------------------------------------------------------
#include <sys/ioctl.h>
#include <fcntl.h>
#include <errno.h>
// Joystick event types
#define JS_EVENT_BUTTON 0x01 /* button pressed/released */
#define JS_EVENT_AXIS 0x02 /* joystick moved */
#define JS_EVENT_INIT 0x80 /* initial state of device */
// Joystick event structure
struct js_event {
unsigned int time; /* (u32) event timestamp in milliseconds */
signed short value; /* (s16) value */
unsigned char type; /* (u8) event type */
unsigned char number; /* (u8) axis/button number */
};
// Joystick IOCTL commands
#define JSIOCGVERSION _IOR('j', 0x01, int) /* get driver version (u32) */
#define JSIOCGAXES _IOR('j', 0x11, char) /* get number of axes (u8) */
#define JSIOCGBUTTONS _IOR('j', 0x12, char) /* get number of buttons (u8) */
#endif // _GLFW_USE_LINUX_JOYSTICKS
//========================================================================
// _glfwInitJoysticks() - Initialize joystick interface
//========================================================================
void _glfwInitJoysticks( void )
{
#ifdef _GLFW_USE_LINUX_JOYSTICKS
int k, n, fd, joy_count;
char *joy_base_name, joy_dev_name[ 20 ];
int driver_version = 0x000800;
char ret_data;
#endif // _GLFW_USE_LINUX_JOYSTICKS
int i;
// Start by saying that there are no sticks
for( i = 0; i <= GLFW_JOYSTICK_LAST; ++ i )
{
_glfwJoy[ i ].Present = GL_FALSE;
}
#ifdef _GLFW_USE_LINUX_JOYSTICKS
// Try to open joysticks (nonblocking)
joy_count = 0;
for( k = 0; k <= 1 && joy_count <= GLFW_JOYSTICK_LAST; ++ k )
{
// Pick joystick base name
switch( k )
{
case 0:
joy_base_name = "/dev/input/js"; // USB sticks
break;
case 1:
joy_base_name = "/dev/js"; // "Legacy" sticks
break;
default:
continue; // (should never happen)
}
// Try to open a few of these sticks
for( i = 0; i <= 50 && joy_count <= GLFW_JOYSTICK_LAST; ++ i )
{
sprintf( joy_dev_name, "%s%d", joy_base_name, i );
fd = open( joy_dev_name, O_NONBLOCK );
if( fd != -1 )
{
// Remember fd
_glfwJoy[ joy_count ].fd = fd;
// Check that the joystick driver version is 1.0+
ioctl( fd, JSIOCGVERSION, &driver_version );
if( driver_version < 0x010000 )
{
// It's an old 0.x interface (we don't support it)
close( fd );
continue;
}
// Get number of joystick axes
ioctl( fd, JSIOCGAXES, &ret_data );
_glfwJoy[ joy_count ].NumAxes = (int) ret_data;
// Get number of joystick buttons
ioctl( fd, JSIOCGBUTTONS, &ret_data );
_glfwJoy[ joy_count ].NumButtons = (int) ret_data;
// Allocate memory for joystick state
_glfwJoy[ joy_count ].Axis =
(float *) malloc( sizeof(float) *
_glfwJoy[ joy_count ].NumAxes );
if( _glfwJoy[ joy_count ].Axis == NULL )
{
close( fd );
continue;
}
_glfwJoy[ joy_count ].Button =
(char *) malloc( sizeof(char) *
_glfwJoy[ joy_count ].NumButtons );
if( _glfwJoy[ joy_count ].Button == NULL )
{
free( _glfwJoy[ joy_count ].Axis );
close( fd );
continue;
}
// Clear joystick state
for( n = 0; n < _glfwJoy[ joy_count ].NumAxes; ++ n )
{
_glfwJoy[ joy_count ].Axis[ n ] = 0.0f;
}
for( n = 0; n < _glfwJoy[ joy_count ].NumButtons; ++ n )
{
_glfwJoy[ joy_count ].Button[ n ] = GLFW_RELEASE;
}
// The joystick is supported and connected
_glfwJoy[ joy_count ].Present = GL_TRUE;
joy_count ++;
}
}
}
#endif // _GLFW_USE_LINUX_JOYSTICKS
}
//========================================================================
// _glfwTerminateJoysticks() - Close all opened joystick handles
//========================================================================
void _glfwTerminateJoysticks( void )
{
#ifdef _GLFW_USE_LINUX_JOYSTICKS
int i;
// Close any opened joysticks
for( i = 0; i <= GLFW_JOYSTICK_LAST; ++ i )
{
if( _glfwJoy[ i ].Present )
{
close( _glfwJoy[ i ].fd );
free( _glfwJoy[ i ].Axis );
free( _glfwJoy[ i ].Button );
_glfwJoy[ i ].Present = GL_FALSE;
}
}
#endif // _GLFW_USE_LINUX_JOYSTICKS
}
//========================================================================
// _glfwPollJoystickEvents() - Empty joystick event queue
//========================================================================
static void _glfwPollJoystickEvents( void )
{
#ifdef _GLFW_USE_LINUX_JOYSTICKS
struct js_event e;
int i;
// Get joystick events for all GLFW joysticks
for( i = 0; i <= GLFW_JOYSTICK_LAST; ++ i )
{
// Is the stick present?
if( _glfwJoy[ i ].Present )
{
// Read all queued events (non-blocking)
while( read(_glfwJoy[i].fd, &e, sizeof(struct js_event)) > 0 )
{
// We don't care if it's an init event or not
e.type &= ~JS_EVENT_INIT;
// Check event type
switch( e.type )
{
case JS_EVENT_AXIS:
_glfwJoy[ i ].Axis[ e.number ] = (float) e.value /
32767.0f;
// We need to change the sign for the Y axes, so that
// positive = up/forward, according to the GLFW spec.
if( e.number & 1 )
{
_glfwJoy[ i ].Axis[ e.number ] =
-_glfwJoy[ i ].Axis[ e.number ];
}
break;
case JS_EVENT_BUTTON:
_glfwJoy[ i ].Button[ e.number ] =
e.value ? GLFW_PRESS : GLFW_RELEASE;
break;
default:
break;
}
}
}
}
#endif // _GLFW_USE_LINUX_JOYSTICKS
}
//************************************************************************
//**** Platform implementation functions ****
//************************************************************************
//========================================================================
// _glfwPlatformGetJoystickParam() - Determine joystick capabilities
//========================================================================
int _glfwPlatformGetJoystickParam( int joy, int param )
{
// Is joystick present?
if( !_glfwJoy[ joy ].Present )
{
return 0;
}
switch( param )
{
case GLFW_PRESENT:
return GL_TRUE;
case GLFW_AXES:
return _glfwJoy[ joy ].NumAxes;
case GLFW_BUTTONS:
return _glfwJoy[ joy ].NumButtons;
default:
break;
}
return 0;
}
//========================================================================
// _glfwPlatformGetJoystickPos() - Get joystick axis positions
//========================================================================
int _glfwPlatformGetJoystickPos( int joy, float *pos, int numaxes )
{
int i;
// Is joystick present?
if( !_glfwJoy[ joy ].Present )
{
return 0;
}
// Update joystick state
_glfwPollJoystickEvents();
// Does the joystick support less axes than requested?
if( _glfwJoy[ joy ].NumAxes < numaxes )
{
numaxes = _glfwJoy[ joy ].NumAxes;
}
// Copy axis positions from internal state
for( i = 0; i < numaxes; ++ i )
{
pos[ i ] = _glfwJoy[ joy ].Axis[ i ];
}
return numaxes;
}
//========================================================================
// _glfwPlatformGetJoystickButtons() - Get joystick button states
//========================================================================
int _glfwPlatformGetJoystickButtons( int joy, unsigned char *buttons,
int numbuttons )
{
int i;
// Is joystick present?
if( !_glfwJoy[ joy ].Present )
{
return 0;
}
// Update joystick state
_glfwPollJoystickEvents();
// Does the joystick support less buttons than requested?
if( _glfwJoy[ joy ].NumButtons < numbuttons )
{
numbuttons = _glfwJoy[ joy ].NumButtons;
}
// Copy button states from internal state
for( i = 0; i < numbuttons; ++ i )
{
buttons[ i ] = _glfwJoy[ joy ].Button[ i ];
}
return numbuttons;
}
|