This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_dxjoystick.c
650 lines (546 loc) · 20.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#ifdef SDL_JOYSTICK_DINPUT
/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
* A. Formiga's WINMM driver.
*
* Hats and sliders are completely untested; the app I'm writing this for mostly
* doesn't use them and I don't own any joysticks with them.
*
* We don't bother to use event notification here. It doesn't seem to work
* with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and
* let it return 0 events. */
#include "SDL_error.h"
#include "SDL_events.h"
#include "SDL_joystick.h"
#include "../SDL_sysjoystick.h"
#include "../SDL_joystick_c.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0500
#include <dinput.h>
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifdef _MSC_VER
/* Used for the c_dfDIJoystick2 symbol (no imports are used) */
# pragma comment (lib, "dinput.lib")
#endif
#include <dxerr9.h> /* From DirectX SDK 9c */
#ifdef _MSC_VER
# pragma comment (lib, "dxerr9.lib")
#endif
/* an ISO hack for VisualC++ */
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
60
61
#define INPUT_QSIZE 32 /* Buffer up to 32 input messages */
62
63
64
65
66
#define MAX_JOYSTICKS 8
#define MAX_INPUTS 256 /* each joystick can have up to 256 inputs */
#define AXIS_MIN -32768 /* minimum value for axis coordinate */
#define AXIS_MAX 32767 /* maximum value for axis coordinate */
#define JOY_AXIS_THRESHOLD (((AXIS_MAX)-(AXIS_MIN))/100) /* 1% motion */
67
68
/* external variables referenced. */
69
extern HINSTANCE SDL_Instance;
70
71
72
73
74
extern HWND SDL_Window;
/* local variables */
static LPDIRECTINPUT dinput = NULL;
75
76
77
extern HRESULT(WINAPI * DInputCreate) (HINSTANCE hinst, DWORD dwVersion,
LPDIRECTINPUT * ppDI,
LPUNKNOWN punkOuter);
78
79
80
static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS]; /* array to hold joystick ID values */
static int SYS_NumJoysticks;
static HINSTANCE DInputDLL = NULL;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* local prototypes */
static void SetDIerror(const char *function, HRESULT code);
static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE *
pdidInstance, VOID * pContext);
static BOOL CALLBACK EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev,
LPVOID pvRef);
static Uint8 TranslatePOV(DWORD value);
static int SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis,
Sint16 value);
static int SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat,
Uint8 value);
static int SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick,
Uint8 button, Uint8 state);
97
98
/* local types */
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
typedef enum Type
{ BUTTON, AXIS, HAT } Type;
typedef struct input_t
{
/* DirectInput offset for this input type: */
DWORD ofs;
/* Button, axis or hat: */
Type type;
/* SDL input offset: */
Uint8 num;
} input_t;
/* The private structure used to keep track of a joystick */
struct joystick_hwdata
{
LPDIRECTINPUTDEVICE2 InputDevice;
118
DIDEVCAPS Capabilities;
119
120
121
122
123
124
125
126
int buffered;
input_t Inputs[MAX_INPUTS];
int NumInputs;
};
/* Convert a DirectInput return code to a text message */
static void
127
SetDIerror(const char *function, HRESULT code)
128
{
129
130
SDL_SetError("%s() [%s]: %s", function,
DXGetErrorString9(code), DXGetErrorDescription9(code));
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}
/* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
int
SDL_SYS_JoystickInit(void)
{
HRESULT result;
SYS_NumJoysticks = 0;
146
147
148
result = CoInitialize(NULL);
if (FAILED(result)) {
SetDIerror("CoInitialize", result);
149
150
return (-1);
}
151
152
153
154
155
156
result = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER,
&IID_IDirectInput, &dinput);
if (FAILED(result)) {
SetDIerror("CoCreateInstance", result);
157
158
159
return (-1);
}
160
161
162
163
164
165
166
167
168
169
/* Because we used CoCreateInstance, we need to Initialize it, first. */
result =
IDirectInput_Initialize(dinput, SDL_Instance, DIRECTINPUT_VERSION);
if (FAILED(result)) {
SetDIerror("IDirectInput::Initialize", result);
return (-1);
}
/* Look for joysticks, wheels, head trackers, gamepads, etc.. */
170
171
172
173
174
175
176
177
result = IDirectInput_EnumDevices(dinput,
DIDEVTYPE_JOYSTICK,
EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY);
return SYS_NumJoysticks;
}
178
179
180
181
182
183
184
185
186
187
188
189
190
static BOOL CALLBACK
EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance,
sizeof(DIDEVICEINSTANCE));
SYS_NumJoysticks++;
if (SYS_NumJoysticks >= MAX_JOYSTICKS)
return DIENUM_STOP;
return DIENUM_CONTINUE;
}
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickName(int index)
{
/***-> test for invalid index ? */
return (SYS_Joystick[index].tszProductName);
}
/* 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)
{
HRESULT result;
LPDIRECTINPUTDEVICE device;
209
210
211
212
213
214
DIPROPDWORD dipdw;
ZeroMemory(&dipdw, sizeof(DIPROPDWORD));
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
215
216
217
/* allocate memory for system specific hardware data */
joystick->hwdata =
218
(struct joystick_hwdata *) malloc(sizeof(struct joystick_hwdata));
219
220
221
222
if (joystick->hwdata == NULL) {
SDL_OutOfMemory();
return (-1);
}
223
ZeroMemory(joystick->hwdata, sizeof(struct joystick_hwdata));
224
joystick->hwdata->buffered = 1;
225
joystick->hwdata->Capabilities.dwSize = sizeof(DIDEVCAPS);
226
227
228
result =
IDirectInput_CreateDevice(dinput,
229
230
&SYS_Joystick[joystick->index].guidInstance,
&device, NULL);
231
232
if (FAILED(result)) {
SetDIerror("IDirectInput::CreateDevice", result);
233
234
235
return (-1);
}
236
/* Now get the IDirectInputDevice2 interface, instead. */
237
238
result = IDirectInputDevice_QueryInterface(device,
&IID_IDirectInputDevice2,
239
240
(LPVOID *) & joystick->hwdata->
InputDevice);
241
/* We are done with this object. Use the stored one from now on. */
242
IDirectInputDevice_Release(device);
243
244
245
if (FAILED(result)) {
SetDIerror("IDirectInputDevice::QueryInterface", result);
246
247
248
return (-1);
}
249
250
/* Aquire shared access. Exclusive access is required for forces,
* though. */
251
result =
252
253
IDirectInputDevice2_SetCooperativeLevel(joystick->hwdata->InputDevice,
SDL_Window,
254
DISCL_EXCLUSIVE |
255
DISCL_BACKGROUND);
256
257
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::SetCooperativeLevel", result);
258
259
260
return (-1);
}
261
/* Use the extended data structure: DIJOYSTATE2. */
262
263
result =
IDirectInputDevice2_SetDataFormat(joystick->hwdata->InputDevice,
264
265
266
&c_dfDIJoystick2);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::SetDataFormat", result);
267
268
269
return (-1);
}
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* Get device capabilities */
result =
IDirectInputDevice2_GetCapabilities(joystick->hwdata->InputDevice,
&joystick->hwdata->Capabilities);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::GetCapabilities", result);
return (-1);
}
/* Force capable? */
if (joystick->hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
result = IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::Acquire", result);
return (-1);
}
/* reset all accuators. */
result =
292
293
IDirectInputDevice2_SendForceFeedbackCommand(joystick->
hwdata->InputDevice,
294
295
296
297
298
299
300
DISFFC_RESET);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::SendForceFeedbackCommand",
result);
return (-1);
}
301
302
303
304
305
306
307
308
309
310
result = IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::Unacquire", result);
return (-1);
}
/* Turn on auto-centering for a ForceFeedback device (until told
* otherwise). */
311
312
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
313
314
dipdw.dwData = DIPROPAUTOCENTER_ON;
315
316
result =
IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
317
318
319
320
DIPROP_AUTOCENTER, &dipdw.diph);
if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::SetProperty", result);
321
322
323
324
return (-1);
}
}
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* What buttons and axes does it have? */
IDirectInputDevice2_EnumObjects(joystick->hwdata->InputDevice,
EnumDevObjectsCallback, joystick,
DIDFT_BUTTON | DIDFT_AXIS | DIDFT_POV);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = INPUT_QSIZE;
/* Set the buffer size */
result =
IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
DIPROP_BUFFERSIZE, &dipdw.diph);
if (result == DI_POLLEDDEVICE) {
/* This device doesn't support buffering, so we're forced
* to use less reliable polling. */
joystick->hwdata->buffered = 0;
} else if (FAILED(result)) {
SetDIerror("IDirectInputDevice2::SetProperty", result);
return (-1);
}
348
349
350
return (0);
}
351
352
static BOOL CALLBACK
EnumDevObjectsCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
353
{
354
355
356
SDL_Joystick *joystick = (SDL_Joystick *) pvRef;
HRESULT result;
input_t *in = &joystick->hwdata->Inputs[joystick->hwdata->NumInputs];
357
358
in->ofs = dev->dwOfs;
359
360
361
362
363
364
365
366
367
368
369
370
if (dev->dwType & DIDFT_BUTTON) {
in->type = BUTTON;
in->num = joystick->nbuttons;
joystick->nbuttons++;
} else if (dev->dwType & DIDFT_POV) {
in->type = HAT;
in->num = joystick->nhats;
joystick->nhats++;
} else if (dev->dwType & DIDFT_AXIS) {
DIPROPRANGE diprg;
DIPROPDWORD dilong;
371
372
373
in->type = AXIS;
in->num = joystick->naxes;
374
375
376
377
378
379
380
diprg.diph.dwSize = sizeof(diprg);
diprg.diph.dwHeaderSize = sizeof(diprg.diph);
diprg.diph.dwObj = dev->dwOfs;
diprg.diph.dwHow = DIPH_BYOFFSET;
diprg.lMin = AXIS_MIN;
diprg.lMax = AXIS_MAX;
381
382
383
384
385
386
387
result =
IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
DIPROP_RANGE, &diprg.diph);
if (FAILED(result)) {
return DIENUM_CONTINUE; /* don't use this axis */
}
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Set dead zone to 0. */
dilong.diph.dwSize = sizeof(dilong);
dilong.diph.dwHeaderSize = sizeof(dilong.diph);
dilong.diph.dwObj = dev->dwOfs;
dilong.diph.dwHow = DIPH_BYOFFSET;
dilong.dwData = 0;
result =
IDirectInputDevice2_SetProperty(joystick->hwdata->InputDevice,
DIPROP_DEADZONE, &dilong.diph);
if (FAILED(result)) {
return DIENUM_CONTINUE; /* don't use this axis */
}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
joystick->naxes++;
} else {
/* not supported at this time */
return DIENUM_CONTINUE;
}
joystick->hwdata->NumInputs++;
if (joystick->hwdata->NumInputs == MAX_INPUTS) {
return DIENUM_STOP; /* too many */
}
return DIENUM_CONTINUE;
415
416
417
418
419
420
421
422
423
424
}
/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
* and update joystick device state.
*/
void
SDL_SYS_JoystickUpdate_Polled(SDL_Joystick * joystick)
{
425
DIJOYSTATE2 state;
426
427
428
429
430
HRESULT result;
int i;
result =
IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
431
sizeof(DIJOYSTATE2), &state);
432
433
434
if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
result =
435
436
IDirectInputDevice2_GetDeviceState(joystick->hwdata->InputDevice,
sizeof(DIJOYSTATE2), &state);
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
}
/* Set each known axis, button and POV. */
for (i = 0; i < joystick->hwdata->NumInputs; ++i) {
const input_t *in = &joystick->hwdata->Inputs[i];
switch (in->type) {
case AXIS:
switch (in->ofs) {
case DIJOFS_X:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lX);
break;
case DIJOFS_Y:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lY);
break;
case DIJOFS_Z:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lZ);
break;
case DIJOFS_RX:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lRx);
break;
case DIJOFS_RY:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lRy);
break;
case DIJOFS_RZ:
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.lRz);
break;
case DIJOFS_SLIDER(0):
SDL_PrivateJoystickAxis_Int(joystick, in->num,
(Sint16) state.rglSlider[0]);
break;
case DIJOFS_SLIDER(1):
SDL_PrivateJoystickAxis_Int(joystick, in->num,
476
(Sint16) state.rglSlider[1]);
477
478
479
480
481
482
483
break;
}
break;
case BUTTON:
SDL_PrivateJoystickButton_Int(joystick, in->num,
484
485
(Uint8) (state.rgbButtons[in->ofs -
DIJOFS_BUTTON0]
486
487
488
489
490
? SDL_PRESSED :
SDL_RELEASED));
break;
case HAT:
{
491
492
Uint8 pos = TranslatePOV(state.rgdwPOV[in->ofs -
DIJOFS_POV(0)]);
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
SDL_PrivateJoystickHat_Int(joystick, in->num, pos);
break;
}
}
}
}
void
SDL_SYS_JoystickUpdate_Buffered(SDL_Joystick * joystick)
{
int i;
HRESULT result;
DWORD numevents;
DIDEVICEOBJECTDATA evtbuf[INPUT_QSIZE];
numevents = INPUT_QSIZE;
result =
IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
511
512
sizeof(DIDEVICEOBJECTDATA), evtbuf,
&numevents, 0);
513
514
515
if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
result =
516
IDirectInputDevice2_GetDeviceData(joystick->hwdata->InputDevice,
517
518
519
520
sizeof(DIDEVICEOBJECTDATA),
evtbuf, &numevents, 0);
}
521
522
/* Handle the events or punt */
if (FAILED(result))
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
return;
for (i = 0; i < (int) numevents; ++i) {
int j;
for (j = 0; j < joystick->hwdata->NumInputs; ++j) {
const input_t *in = &joystick->hwdata->Inputs[j];
if (evtbuf[i].dwOfs != in->ofs)
continue;
switch (in->type) {
case AXIS:
SDL_PrivateJoystickAxis(joystick, in->num,
(Sint16) evtbuf[i].dwData);
break;
case BUTTON:
SDL_PrivateJoystickButton(joystick, in->num,
541
542
(Uint8) (evtbuf[i].dwData ?
SDL_PRESSED :
543
SDL_RELEASED));
544
545
546
547
548
549
550
551
552
553
554
break;
case HAT:
{
Uint8 pos = TranslatePOV(evtbuf[i].dwData);
SDL_PrivateJoystickHat(joystick, in->num, pos);
}
}
}
}
}
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
static Uint8
TranslatePOV(DWORD value)
{
const int HAT_VALS[] = {
SDL_HAT_UP,
SDL_HAT_UP | SDL_HAT_RIGHT,
SDL_HAT_RIGHT,
SDL_HAT_DOWN | SDL_HAT_RIGHT,
SDL_HAT_DOWN,
SDL_HAT_DOWN | SDL_HAT_LEFT,
SDL_HAT_LEFT,
SDL_HAT_UP | SDL_HAT_LEFT
};
if (LOWORD(value) == 0xFFFF)
return SDL_HAT_CENTERED;
/* Round the value up: */
value += 4500 / 2;
value %= 36000;
value /= 4500;
if (value >= 8)
return SDL_HAT_CENTERED; /* shouldn't happen */
return HAT_VALS[value];
}
/* SDL_PrivateJoystick* doesn't discard duplicate events, so we need to
* do it. */
static int
SDL_PrivateJoystickAxis_Int(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
{
if (joystick->axes[axis] != value)
return SDL_PrivateJoystickAxis(joystick, axis, value);
return 0;
}
static int
SDL_PrivateJoystickHat_Int(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
{
if (joystick->hats[hat] != value)
return SDL_PrivateJoystickHat(joystick, hat, value);
return 0;
}
static int
SDL_PrivateJoystickButton_Int(SDL_Joystick * joystick, Uint8 button,
Uint8 state)
{
if (joystick->buttons[button] != state)
return SDL_PrivateJoystickButton(joystick, button, state);
return 0;
}
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
HRESULT result;
result = IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
if (result == DIERR_INPUTLOST || result == DIERR_NOTACQUIRED) {
IDirectInputDevice2_Acquire(joystick->hwdata->InputDevice);
IDirectInputDevice2_Poll(joystick->hwdata->InputDevice);
}
if (joystick->hwdata->buffered)
SDL_SYS_JoystickUpdate_Buffered(joystick);
else
SDL_SYS_JoystickUpdate_Polled(joystick);
}
/* Function to close a joystick after use */
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
IDirectInputDevice2_Unacquire(joystick->hwdata->InputDevice);
IDirectInputDevice2_Release(joystick->hwdata->InputDevice);
if (joystick->hwdata != NULL) {
/* free system specific hardware data */
free(joystick->hwdata);
}
}
/* Function to perform any system-specific joystick related cleanup */
void
SDL_SYS_JoystickQuit(void)
{
IDirectInput_Release(dinput);
dinput = NULL;
}
#endif /* SDL_JOYSTICK_DINPUT */
/* vi: set ts=4 sw=4 expandtab: */