Skip to content

Latest commit

 

History

History
515 lines (431 loc) · 13.5 KB

SDL_sysjoystick.c

File metadata and controls

515 lines (431 loc) · 13.5 KB
 
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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
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 "SDL_config.h"
#ifdef SDL_JOYSTICK_ANDROID
#include <stdio.h> /* For the definition of NULL */
#include "SDL_error.h"
#include "SDL_events.h"
Dec 10, 2013
Dec 10, 2013
29
30
31
32
33
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
34
35
36
#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_assert.h"
Dec 12, 2013
Dec 12, 2013
37
#include "SDL_timer.h"
Dec 24, 2013
Dec 24, 2013
38
#include "SDL_log.h"
Dec 10, 2013
Dec 10, 2013
39
#include "SDL_sysjoystick_c.h"
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
#include "../SDL_joystick_c.h"
#include "../../core/android/SDL_android.h"
#include "android/keycodes.h"
/* As of platform android-14, android/keycodes.h is missing these defines */
#ifndef AKEYCODE_BUTTON_1
#define AKEYCODE_BUTTON_1 188
#define AKEYCODE_BUTTON_2 189
#define AKEYCODE_BUTTON_3 190
#define AKEYCODE_BUTTON_4 191
#define AKEYCODE_BUTTON_5 192
#define AKEYCODE_BUTTON_6 193
#define AKEYCODE_BUTTON_7 194
#define AKEYCODE_BUTTON_8 195
#define AKEYCODE_BUTTON_9 196
#define AKEYCODE_BUTTON_10 197
#define AKEYCODE_BUTTON_11 198
#define AKEYCODE_BUTTON_12 199
#define AKEYCODE_BUTTON_13 200
#define AKEYCODE_BUTTON_14 201
#define AKEYCODE_BUTTON_15 202
#define AKEYCODE_BUTTON_16 203
#endif
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
Dec 10, 2013
Dec 10, 2013
66
#define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
67
68
#define ANDROID_MAX_NBUTTONS 36
Dec 10, 2013
Dec 10, 2013
69
70
71
72
73
74
75
static SDL_joylist_item * JoystickByDeviceId(int device_id);
static SDL_joylist_item *SDL_joylist = NULL;
static SDL_joylist_item *SDL_joylist_tail = NULL;
static int numjoysticks = 0;
static int instance_counter = 0;
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
/* Function to convert Android keyCodes into SDL ones.
* This code manipulation is done to get a sequential list of codes.
* FIXME: This is only suited for the case where we use a fixed number of buttons determined by ANDROID_MAX_NBUTTONS
*/
static int
keycode_to_SDL(int keycode)
{
/* FIXME: If this function gets too unwiedly in the future, replace with a lookup table */
int button = 0;
switch(keycode)
{
/* D-Pad key codes (API 1), these get mapped to 0...4 */
case AKEYCODE_DPAD_UP:
case AKEYCODE_DPAD_DOWN:
case AKEYCODE_DPAD_LEFT:
case AKEYCODE_DPAD_RIGHT:
case AKEYCODE_DPAD_CENTER:
button = keycode - AKEYCODE_DPAD_UP;
break;
/* Some gamepad buttons (API 9), these get mapped to 5...19*/
case AKEYCODE_BUTTON_A:
case AKEYCODE_BUTTON_B:
case AKEYCODE_BUTTON_C:
case AKEYCODE_BUTTON_X:
case AKEYCODE_BUTTON_Y:
case AKEYCODE_BUTTON_Z:
case AKEYCODE_BUTTON_L1:
case AKEYCODE_BUTTON_L2:
case AKEYCODE_BUTTON_R1:
case AKEYCODE_BUTTON_R2:
case AKEYCODE_BUTTON_THUMBL:
case AKEYCODE_BUTTON_THUMBR:
case AKEYCODE_BUTTON_START:
case AKEYCODE_BUTTON_SELECT:
case AKEYCODE_BUTTON_MODE:
button = keycode - AKEYCODE_BUTTON_A + 5;
break;
/* More gamepad buttons (API 12), these get mapped to 20...35*/
case AKEYCODE_BUTTON_1:
case AKEYCODE_BUTTON_2:
case AKEYCODE_BUTTON_3:
case AKEYCODE_BUTTON_4:
case AKEYCODE_BUTTON_5:
case AKEYCODE_BUTTON_6:
case AKEYCODE_BUTTON_7:
case AKEYCODE_BUTTON_8:
case AKEYCODE_BUTTON_9:
case AKEYCODE_BUTTON_10:
case AKEYCODE_BUTTON_11:
case AKEYCODE_BUTTON_12:
case AKEYCODE_BUTTON_13:
case AKEYCODE_BUTTON_14:
case AKEYCODE_BUTTON_15:
case AKEYCODE_BUTTON_16:
button = keycode - AKEYCODE_BUTTON_1 + 20;
break;
default:
return -1;
break;
}
/* This is here in case future generations, probably with six fingers per hand,
* happily add new cases up above and forget to update the max number of buttons.
*/
SDL_assert(button < ANDROID_MAX_NBUTTONS);
return button;
}
int
Dec 10, 2013
Dec 10, 2013
151
Android_OnPadDown(int device_id, int keycode)
152
{
Dec 10, 2013
Dec 10, 2013
153
154
155
156
157
158
159
160
161
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
}
return 0;
}
162
Dec 10, 2013
Dec 10, 2013
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
return -1;
}
int
Android_OnPadUp(int device_id, int keycode)
{
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
}
return 0;
}
178
Dec 10, 2013
Dec 10, 2013
179
180
181
182
183
184
185
186
187
188
return -1;
}
int
Android_OnJoy(int device_id, int axis, float value)
{
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
SDL_joylist_item *item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
189
}
Dec 10, 2013
Dec 10, 2013
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
return 0;
}
int
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
{
SDL_JoystickGUID guid;
SDL_joylist_item *item;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
return -1;
206
}
Dec 10, 2013
Dec 10, 2013
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* the GUID is just the first 16 chars of the name for now */
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
if (item == NULL) {
return -1;
}
SDL_zerop(item);
item->guid = guid;
item->device_id = device_id;
item->name = SDL_strdup(name);
if ( item->name == NULL ) {
SDL_free(item);
return -1;
224
225
}
Dec 10, 2013
Dec 10, 2013
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
item->is_accelerometer = is_accelerometer;
if (nbuttons > -1) {
item->nbuttons = nbuttons;
}
else {
item->nbuttons = ANDROID_MAX_NBUTTONS;
}
item->naxes = naxes;
item->nhats = nhats;
item->nballs = nballs;
item->device_instance = instance_counter++;
if (SDL_joylist_tail == NULL) {
SDL_joylist = SDL_joylist_tail = item;
} else {
SDL_joylist_tail->next = item;
SDL_joylist_tail = item;
}
/* Need to increment the joystick count before we post the event */
++numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = (numjoysticks - 1);
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_Log("Added joystick %s with device_id %d", name, device_id);
return numjoysticks;
}
int
Android_RemoveJoystick(int device_id)
{
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
/* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
while (item != NULL) {
if (item->device_id == device_id) {
break;
277
}
Dec 10, 2013
Dec 10, 2013
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
prev = item;
item = item->next;
}
if (item == NULL) {
return -1;
}
const int retval = item->device_instance;
if (prev != NULL) {
prev->next = item->next;
} else {
SDL_assert(SDL_joylist == item);
SDL_joylist = item->next;
}
if (item == SDL_joylist_tail) {
SDL_joylist_tail = prev;
}
/* Need to decrement the joystick count before we post the event */
--numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = item->device_instance;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_Log("Removed joystick with device_id %d", device_id);
SDL_free(item->name);
SDL_free(item);
return retval;
}
int
SDL_SYS_JoystickInit(void)
{
const char *env;
SDL_SYS_JoystickDetect();
env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
if (!env || SDL_atoi(env)) {
/* Default behavior, accelerometer as joystick */
Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
330
331
}
Dec 10, 2013
Dec 10, 2013
332
333
return (numjoysticks);
334
335
336
337
}
int SDL_SYS_NumJoysticks()
{
Dec 10, 2013
Dec 10, 2013
338
return numjoysticks;
339
340
341
342
}
void SDL_SYS_JoystickDetect()
{
Dec 10, 2013
Dec 10, 2013
343
/* Support for device connect/disconnect is API >= 16 only,
Dec 12, 2013
Dec 12, 2013
344
* so we poll every three seconds
Dec 10, 2013
Dec 10, 2013
345
346
* Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
*/
Dec 12, 2013
Dec 12, 2013
347
348
349
350
351
static Uint32 timeout = 0;
if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
timeout = SDL_GetTicks() + 3000;
Android_JNI_PollInputDevices();
}
352
353
354
355
}
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
Dec 10, 2013
Dec 10, 2013
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
return SDL_TRUE;
}
static SDL_joylist_item *
JoystickByDevIndex(int device_index)
{
SDL_joylist_item *item = SDL_joylist;
if ((device_index < 0) || (device_index >= numjoysticks)) {
return NULL;
}
while (device_index > 0) {
SDL_assert(item != NULL);
device_index--;
item = item->next;
}
return item;
}
static SDL_joylist_item *
JoystickByDeviceId(int device_id)
{
SDL_joylist_item *item = SDL_joylist;
while (item != NULL) {
if (item->device_id == device_id) {
return item;
}
item = item->next;
}
/* Joystick not found, try adding it */
SDL_SYS_JoystickDetect();
while (item != NULL) {
if (item->device_id == device_id) {
return item;
}
item = item->next;
}
return NULL;
400
401
402
403
404
405
}
/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
Dec 10, 2013
Dec 10, 2013
406
return JoystickByDevIndex(device_index)->name;
407
408
409
410
411
}
/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
Dec 10, 2013
Dec 10, 2013
412
return JoystickByDevIndex(device_index)->device_instance;
413
414
415
416
417
418
419
420
421
422
}
/* Function to open a joystick for use.
The joystick to open is specified by the index field of the joystick.
This should fill the nbuttons and naxes fields of the joystick structure.
It returns 0, or -1 if there is an error.
*/
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
Dec 10, 2013
Dec 10, 2013
423
424
425
426
SDL_joylist_item *item = JoystickByDevIndex(device_index);
if (item == NULL ) {
return SDL_SetError("No such device");
427
}
Dec 10, 2013
Dec 10, 2013
428
429
430
431
432
433
434
435
436
437
438
439
440
441
if (item->joystick != NULL) {
return SDL_SetError("Joystick already opened");
}
joystick->instance_id = item->device_instance;
joystick->hwdata = (struct joystick_hwdata *) item;
item->joystick = joystick;
joystick->nhats = item->nhats;
joystick->nballs = item->nballs;
joystick->nbuttons = item->nbuttons;
joystick->naxes = item->naxes;
return (0);
442
443
444
445
446
}
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
Dec 10, 2013
Dec 10, 2013
447
return !joystick->closed && (joystick->hwdata != NULL);
448
449
450
451
452
453
454
455
}
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
int i;
Sint16 value;
float values[3];
Dec 10, 2013
Dec 10, 2013
456
SDL_joylist_item *item = SDL_joylist;
457
Dec 10, 2013
Dec 10, 2013
458
459
460
461
462
463
464
465
466
467
while (item) {
if (item->is_accelerometer) {
if (item->joystick) {
Android_JNI_GetAccelerometerValues(values);
for ( i = 0; i < 3; i++ ) {
value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(item->joystick, i, value);
}
}
break;
468
}
Dec 10, 2013
Dec 10, 2013
469
item = item->next;
470
471
472
473
474
475
476
}
}
/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
Dec 10, 2013
Dec 10, 2013
477
478
479
if (joystick->hwdata) {
((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
joystick->hwdata = NULL;
480
481
482
483
484
485
486
487
}
joystick->closed = 1;
}
/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
Dec 10, 2013
Dec 10, 2013
488
489
SDL_joylist_item *item = NULL;
SDL_joylist_item *next = NULL;
490
Dec 10, 2013
Dec 10, 2013
491
492
493
494
495
for (item = SDL_joylist; item; item = next) {
next = item->next;
SDL_free(item->name);
SDL_free(item);
}
496
Dec 10, 2013
Dec 10, 2013
497
SDL_joylist = SDL_joylist_tail = NULL;
498
Dec 10, 2013
Dec 10, 2013
499
500
numjoysticks = 0;
instance_counter = 0;
501
502
}
Dec 10, 2013
Dec 10, 2013
503
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
504
{
Dec 10, 2013
Dec 10, 2013
505
return JoystickByDevIndex(device_index)->guid;
506
507
}
Dec 10, 2013
Dec 10, 2013
508
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
509
{
Dec 10, 2013
Dec 10, 2013
510
return ((SDL_joylist_item*)joystick->hwdata)->guid;
511
512
513
514
515
}
#endif /* SDL_JOYSTICK_ANDROID */
/* vi: set ts=4 sw=4 expandtab: */