Skip to content

Latest commit

 

History

History
1774 lines (1491 loc) · 51.8 KB

SDL_syshaptic.c

File metadata and controls

1774 lines (1491 loc) · 51.8 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 2, 2014
Feb 2, 2014
3
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.
*/
Nov 25, 2013
Nov 25, 2013
21
#include "../../SDL_internal.h"
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifdef SDL_HAPTIC_DINPUT
#include "SDL_assert.h"
#include "SDL_thread.h"
#include "SDL_mutex.h"
#include "SDL_timer.h"
#include "SDL_hints.h"
#include "SDL_haptic.h"
#include "../SDL_syshaptic.h"
#include "SDL_joystick.h"
#include "../../joystick/SDL_sysjoystick.h" /* For the real SDL_Joystick */
#include "../../joystick/windows/SDL_dxjoystick_c.h" /* For joystick hwdata */
Feb 6, 2014
Feb 6, 2014
36
#include "SDL_syshaptic_c.h"
37
38
39
40
/*
* List of available haptic devices.
*/
Feb 6, 2014
Feb 6, 2014
41
typedef struct SDL_hapticlist_item
42
43
44
45
46
47
48
{
DIDEVICEINSTANCE instance;
char *name;
SDL_Haptic *haptic;
DIDEVCAPS capabilities;
Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
Uint8 userid; /* XInput userid index for this joystick */
Feb 6, 2014
Feb 6, 2014
49
50
struct SDL_hapticlist_item *next;
} SDL_hapticlist_item;
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
/*
* Haptic system hardware data.
*/
struct haptic_hwdata
{
LPDIRECTINPUTDEVICE8 device;
DWORD axes[3]; /* Axes to use. */
SDL_bool is_joystick; /* Device is loaded as joystick. */
Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
Uint8 userid; /* XInput userid index for this joystick */
SDL_Thread *thread;
SDL_mutex *mutex;
volatile Uint32 stopTicks;
volatile int stopThread;
};
/*
* Haptic system effect data.
*/
struct haptic_hweffect
{
DIEFFECT effect;
LPDIRECTINPUTEFFECT ref;
XINPUT_VIBRATION vibration;
};
/*
* Internal stuff.
*/
static SDL_bool coinitialized = SDL_FALSE;
static LPDIRECTINPUT8 dinput = NULL;
static SDL_bool loaded_xinput = SDL_FALSE;
Feb 6, 2014
Feb 6, 2014
87
88
89
static SDL_hapticlist_item *SDL_hapticlist = NULL;
static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
static int numhaptics = 0;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* External stuff.
*/
extern HWND SDL_HelperWindow;
/*
* Prototypes.
*/
static int DI_SetError(const char *str, HRESULT err);
static int DI_GUIDIsSame(const GUID * a, const GUID * b);
static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
DIDEVICEINSTANCE instance);
static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
LPDIRECTINPUTDEVICE8 device8,
SDL_bool is_joystick);
Feb 6, 2014
Feb 6, 2014
107
static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, const Uint8 userid);
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
static DWORD DIGetTriggerButton(Uint16 button);
static int SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir,
int naxes);
static int SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
SDL_HapticEffect * src);
static void SDL_SYS_HapticFreeDIEFFECT(DIEFFECT * effect, int type);
static REFGUID SDL_SYS_HapticEffectType(SDL_HapticEffect * effect);
static int SDLCALL SDL_RunXInputHaptic(void *arg);
/* Callbacks. */
static BOOL CALLBACK EnumHapticsCallback(const DIDEVICEINSTANCE *
pdidInstance, VOID * pContext);
static BOOL CALLBACK DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv);
/*
* Like SDL_SetError but for DX error codes.
*/
static int
DI_SetError(const char *str, HRESULT err)
{
/*
SDL_SetError("Haptic: %s - %s: %s", str,
DXGetErrorString8A(err), DXGetErrorDescription8A(err));
*/
return SDL_SetError("Haptic error %s", str);
}
/*
* Checks to see if two GUID are the same.
*/
static int
DI_GUIDIsSame(const GUID * a, const GUID * b)
{
return (SDL_memcmp(a, b, sizeof (GUID)) == 0);
}
/*
* Initializes the haptic subsystem.
*/
int
SDL_SYS_HapticInit(void)
{
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
HRESULT ret;
HINSTANCE instance;
if (dinput != NULL) { /* Already open. */
return SDL_SetError("Haptic: SubSystem already open.");
}
ret = WIN_CoInitialize();
if (FAILED(ret)) {
return DI_SetError("Coinitialize", ret);
}
coinitialized = SDL_TRUE;
ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
&IID_IDirectInput8, (LPVOID) & dinput);
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
return DI_SetError("CoCreateInstance", ret);
}
/* Because we used CoCreateInstance, we need to Initialize it, first. */
instance = GetModuleHandle(NULL);
if (instance == NULL) {
SDL_SYS_HapticQuit();
return SDL_SetError("GetModuleHandle() failed with error code %d.",
GetLastError());
}
ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
return DI_SetError("Initializing DirectInput device", ret);
}
/* Look for haptic devices. */
ret = IDirectInput8_EnumDevices(dinput,
0,
EnumHapticsCallback,
NULL,
DIEDFL_FORCEFEEDBACK |
DIEDFL_ATTACHEDONLY);
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
return DI_SetError("Enumerating DirectInput devices", ret);
}
if (!env || SDL_atoi(env)) {
loaded_xinput = (WIN_LoadXInputDLL() == 0);
}
if (loaded_xinput) {
DWORD i;
Feb 6, 2014
Feb 6, 2014
206
207
for (i = 0; i < SDL_XINPUT_MAX_DEVICES; i++) {
XInputHaptic_MaybeAddDevice(i);
208
209
210
}
}
Feb 6, 2014
Feb 6, 2014
211
return numhaptics;
212
213
}
Feb 6, 2014
Feb 6, 2014
214
215
216
int
DirectInputHaptic_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
217
218
219
{
HRESULT ret;
LPDIRECTINPUTDEVICE8 device;
Feb 6, 2014
Feb 6, 2014
220
221
222
const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK;
DIDEVCAPS capabilities;
SDL_hapticlist_item *item = NULL;
223
Feb 6, 2014
Feb 6, 2014
224
225
226
227
228
229
/* Make sure we don't already have it */
for (item = SDL_hapticlist; item; item = item->next) {
if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
return -1; /* Already added */
}
}
230
231
/* Open the device */
Feb 6, 2014
Feb 6, 2014
232
ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL);
233
234
if (FAILED(ret)) {
/* DI_SetError("Creating DirectInput device",ret); */
Feb 6, 2014
Feb 6, 2014
235
return -1;
236
237
238
}
/* Get capabilities. */
Feb 6, 2014
Feb 6, 2014
239
240
241
242
SDL_zero(capabilities);
capabilities.dwSize = sizeof (DIDEVCAPS);
ret = IDirectInputDevice8_GetCapabilities(device, &capabilities);
IDirectInputDevice8_Release(device);
243
244
if (FAILED(ret)) {
/* DI_SetError("Getting device capabilities",ret); */
Feb 6, 2014
Feb 6, 2014
245
return -1;
246
247
}
Feb 6, 2014
Feb 6, 2014
248
249
250
if ((capabilities.dwFlags & needflags) != needflags) {
return -1; /* not a device we can use. */
}
251
Feb 6, 2014
Feb 6, 2014
252
253
254
255
item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
if (item == NULL) {
return SDL_OutOfMemory();
}
256
Feb 6, 2014
Feb 6, 2014
257
SDL_zerop(item);
258
Feb 6, 2014
Feb 6, 2014
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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
if (!item->name) {
SDL_free(item);
return -1;
}
/* Copy the instance over, useful for creating devices. */
SDL_memcpy(&item->instance, pdidInstance, sizeof (DIDEVICEINSTANCE));
SDL_memcpy(&item->capabilities, &capabilities, sizeof (capabilities));
if (SDL_hapticlist_tail == NULL) {
SDL_hapticlist = SDL_hapticlist_tail = item;
} else {
SDL_hapticlist_tail->next = item;
SDL_hapticlist_tail = item;
}
/* Device has been added. */
++numhaptics;
return numhaptics;
}
int
DirectInputHaptic_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
{
SDL_hapticlist_item *item;
SDL_hapticlist_item *prev = NULL;
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
/* found it, remove it. */
const int retval = item->haptic ? item->haptic->index : -1;
if (prev != NULL) {
prev->next = item->next;
} else {
SDL_assert(SDL_hapticlist == item);
SDL_hapticlist = item->next;
}
if (item == SDL_hapticlist_tail) {
SDL_hapticlist_tail = prev;
}
--numhaptics;
/* !!! TODO: Send a haptic remove event? */
SDL_free(item);
return retval;
}
prev = item;
}
return -1;
}
int
XInputHaptic_MaybeAddDevice(const DWORD dwUserid)
{
const Uint8 userid = (Uint8) dwUserid;
XINPUT_CAPABILITIES caps;
const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
SDL_hapticlist_item *item;
if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
return -1;
}
/* Make sure we don't already have it */
for (item = SDL_hapticlist; item; item = item->next) {
if ((item->bXInputHaptic) && (item->userid == userid)) {
return -1; /* Already added */
}
}
if (XINPUTGETCAPABILITIES(dwUserid, XINPUT_FLAG_GAMEPAD, &caps) != ERROR_SUCCESS) {
return -1; /* maybe controller isn't plugged in. */
}
/* XInput < 1.4 is probably only for original XBox360 controllers,
which don't offer the flag, and always have force feedback */
if ( (bIs14OrLater) && ((caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) == 0) ) {
return -1; /* no force feedback on this device. */
}
item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
if (item == NULL) {
return SDL_OutOfMemory();
}
SDL_zerop(item);
/* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */
{
char buf[64];
SDL_snprintf(buf, sizeof (buf), "XInput Controller #%u", (unsigned int) (userid+1));
item->name = SDL_strdup(buf);
}
if (!item->name) {
SDL_free(item);
return -1;
}
/* Copy the instance over, useful for creating devices. */
item->bXInputHaptic = 1;
item->userid = userid;
if (SDL_hapticlist_tail == NULL) {
SDL_hapticlist = SDL_hapticlist_tail = item;
} else {
SDL_hapticlist_tail->next = item;
SDL_hapticlist_tail = item;
}
/* Device has been added. */
++numhaptics;
return numhaptics;
}
int
XInputHaptic_MaybeRemoveDevice(const DWORD dwUserid)
{
const Uint8 userid = (Uint8) dwUserid;
SDL_hapticlist_item *item;
SDL_hapticlist_item *prev = NULL;
if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
return -1;
}
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if ((item->bXInputHaptic) && (item->userid == userid)) {
/* found it, remove it. */
const int retval = item->haptic ? item->haptic->index : -1;
if (prev != NULL) {
prev->next = item->next;
} else {
SDL_assert(SDL_hapticlist == item);
SDL_hapticlist = item->next;
}
if (item == SDL_hapticlist_tail) {
SDL_hapticlist_tail = prev;
}
--numhaptics;
/* !!! TODO: Send a haptic remove event? */
SDL_free(item);
return retval;
}
prev = item;
}
return -1;
413
414
415
}
Feb 6, 2014
Feb 6, 2014
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Callback to find the haptic devices.
*/
static BOOL CALLBACK
EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
(void) pContext;
DirectInputHaptic_MaybeAddDevice(pdidInstance);
return DIENUM_CONTINUE; /* continue enumerating */
}
int
SDL_SYS_NumHaptics()
{
return numhaptics;
}
static SDL_hapticlist_item *
HapticByDevIndex(int device_index)
{
SDL_hapticlist_item *item = SDL_hapticlist;
if ((device_index < 0) || (device_index >= numhaptics)) {
return NULL;
}
while (device_index > 0) {
SDL_assert(item != NULL);
Feb 6, 2014
Feb 6, 2014
445
--device_index;
Feb 6, 2014
Feb 6, 2014
446
447
448
449
450
451
item = item->next;
}
return item;
}
452
453
454
455
456
457
/*
* Return the name of a haptic device, does not need to be opened.
*/
const char *
SDL_SYS_HapticName(int index)
{
Feb 6, 2014
Feb 6, 2014
458
459
SDL_hapticlist_item *item = HapticByDevIndex(index);
return item->name;
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
}
/*
* Callback to get all supported effects.
*/
#define EFFECT_TEST(e,s) \
if (DI_GUIDIsSame(&pei->guid, &(e))) \
haptic->supported |= (s)
static BOOL CALLBACK
DI_EffectCallback(LPCDIEFFECTINFO pei, LPVOID pv)
{
/* Prepare the haptic device. */
SDL_Haptic *haptic = (SDL_Haptic *) pv;
/* Get supported. */
EFFECT_TEST(GUID_Spring, SDL_HAPTIC_SPRING);
EFFECT_TEST(GUID_Damper, SDL_HAPTIC_DAMPER);
EFFECT_TEST(GUID_Inertia, SDL_HAPTIC_INERTIA);
EFFECT_TEST(GUID_Friction, SDL_HAPTIC_FRICTION);
EFFECT_TEST(GUID_ConstantForce, SDL_HAPTIC_CONSTANT);
EFFECT_TEST(GUID_CustomForce, SDL_HAPTIC_CUSTOM);
EFFECT_TEST(GUID_Sine, SDL_HAPTIC_SINE);
/* !!! FIXME: put this back when we have more bits in 2.1 */
/* EFFECT_TEST(GUID_Square, SDL_HAPTIC_SQUARE); */
EFFECT_TEST(GUID_Triangle, SDL_HAPTIC_TRIANGLE);
EFFECT_TEST(GUID_SawtoothUp, SDL_HAPTIC_SAWTOOTHUP);
EFFECT_TEST(GUID_SawtoothDown, SDL_HAPTIC_SAWTOOTHDOWN);
EFFECT_TEST(GUID_RampForce, SDL_HAPTIC_RAMP);
/* Check for more. */
return DIENUM_CONTINUE;
}
/*
* Callback to get supported axes.
*/
static BOOL CALLBACK
DI_DeviceObjectCallback(LPCDIDEVICEOBJECTINSTANCE dev, LPVOID pvRef)
{
SDL_Haptic *haptic = (SDL_Haptic *) pvRef;
if ((dev->dwType & DIDFT_AXIS) && (dev->dwFlags & DIDOI_FFACTUATOR)) {
haptic->hwdata->axes[haptic->naxes] = dev->dwOfs;
haptic->naxes++;
/* Currently using the artificial limit of 3 axes. */
if (haptic->naxes >= 3) {
return DIENUM_STOP;
}
}
return DIENUM_CONTINUE;
}
/*
* Opens the haptic device from the file descriptor.
*
* Steps:
* - Open temporary DirectInputDevice interface.
* - Create DirectInputDevice8 interface.
* - Release DirectInputDevice interface.
* - Call SDL_SYS_HapticOpenFromDevice8
*/
static int
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
{
HRESULT ret;
int ret2;
LPDIRECTINPUTDEVICE8 device;
LPDIRECTINPUTDEVICE8 device8;
/* Open the device */
ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
&device, NULL);
if (FAILED(ret)) {
DI_SetError("Creating DirectInput device", ret);
return -1;
}
/* Now get the IDirectInputDevice8 interface, instead. */
ret = IDirectInputDevice8_QueryInterface(device,
&IID_IDirectInputDevice8,
(LPVOID *) &device8);
/* Done with the temporary one now. */
IDirectInputDevice8_Release(device);
if (FAILED(ret)) {
DI_SetError("Querying DirectInput interface", ret);
return -1;
}
ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, device8, SDL_FALSE);
if (ret2 < 0) {
IDirectInputDevice8_Release(device8);
return -1;
}
return 0;
}
static int
Feb 6, 2014
Feb 6, 2014
564
SDL_SYS_HapticOpenFromXInput(SDL_Haptic *haptic, const Uint8 userid)
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
{
char threadName[32];
XINPUT_VIBRATION vibration = { 0, 0 }; /* stop any current vibration */
XINPUTSETSTATE(userid, &vibration);
haptic->supported = SDL_HAPTIC_LEFTRIGHT;
haptic->neffects = 1;
haptic->nplaying = 1;
/* Prepare effects memory. */
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (haptic->effects == NULL) {
return SDL_OutOfMemory();
}
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
haptic->hwdata = (struct haptic_hwdata *) SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
SDL_free(haptic->effects);
haptic->effects = NULL;
return SDL_OutOfMemory();
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
haptic->hwdata->bXInputHaptic = 1;
haptic->hwdata->userid = userid;
haptic->hwdata->mutex = SDL_CreateMutex();
if (haptic->hwdata->mutex == NULL) {
SDL_free(haptic->effects);
SDL_free(haptic->hwdata);
haptic->effects = NULL;
return SDL_SetError("Couldn't create XInput haptic mutex");
}
SDL_snprintf(threadName, sizeof (threadName), "SDLXInputDev%d", (int) userid);
#if defined(__WIN32__) && !defined(HAVE_LIBC) /* !!! FIXME: this is nasty. */
#undef SDL_CreateThread
Dec 9, 2013
Dec 9, 2013
608
609
610
#if SDL_DYNAMIC_API
haptic->hwdata->thread = SDL_CreateThread_REAL(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
#else
611
haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
Dec 9, 2013
Dec 9, 2013
612
#endif
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#else
haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata);
#endif
if (haptic->hwdata->thread == NULL) {
SDL_DestroyMutex(haptic->hwdata->mutex);
SDL_free(haptic->effects);
SDL_free(haptic->hwdata);
haptic->effects = NULL;
return SDL_SetError("Couldn't create XInput haptic thread");
}
return 0;
}
/*
* Opens the haptic device from the file descriptor.
*
* Steps:
* - Set cooperative level.
* - Set data format.
* - Acquire exclusiveness.
* - Reset actuators.
* - Get supported features.
*/
static int
SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
LPDIRECTINPUTDEVICE8 device8, SDL_bool is_joystick)
{
HRESULT ret;
DIPROPDWORD dipdw;
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* We'll use the device8 from now on. */
haptic->hwdata->device = device8;
haptic->hwdata->is_joystick = is_joystick;
/* Grab it exclusively to use force feedback stuff. */
ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
SDL_HelperWindow,
DISCL_EXCLUSIVE |
DISCL_BACKGROUND);
if (FAILED(ret)) {
DI_SetError("Setting cooperative level to exclusive", ret);
goto acquire_err;
}
/* Set data format. */
ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
&c_dfDIJoystick2);
if (FAILED(ret)) {
DI_SetError("Setting data format", ret);
goto acquire_err;
}
/* Get number of axes. */
ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
DI_DeviceObjectCallback,
haptic, DIDFT_AXIS);
if (FAILED(ret)) {
DI_SetError("Getting device axes", ret);
goto acquire_err;
}
/* Acquire the device. */
ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
if (FAILED(ret)) {
DI_SetError("Acquiring DirectInput device", ret);
goto acquire_err;
}
/* Reset all actuators - just in case. */
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
DISFFC_RESET);
if (FAILED(ret)) {
DI_SetError("Resetting device", ret);
goto acquire_err;
}
/* Enabling actuators. */
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
DISFFC_SETACTUATORSON);
if (FAILED(ret)) {
DI_SetError("Enabling actuators", ret);
goto acquire_err;
}
/* Get supported effects. */
ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
DI_EffectCallback, haptic,
DIEFT_ALL);
if (FAILED(ret)) {
DI_SetError("Enumerating supported effects", ret);
goto acquire_err;
}
if (haptic->supported == 0) { /* Error since device supports nothing. */
SDL_SetError("Haptic: Internal error on finding supported effects.");
goto acquire_err;
}
/* Check autogain and autocenter. */
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = 10000;
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
DIPROP_FFGAIN, &dipdw.diph);
if (!FAILED(ret)) { /* Gain is supported. */
haptic->supported |= SDL_HAPTIC_GAIN;
}
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = DIPROPAUTOCENTER_OFF;
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
DIPROP_AUTOCENTER, &dipdw.diph);
if (!FAILED(ret)) { /* Autocenter is supported. */
haptic->supported |= SDL_HAPTIC_AUTOCENTER;
}
/* Status is always supported. */
haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE;
/* Check maximum effects. */
haptic->neffects = 128; /* This is not actually supported as thus under windows,
there is no way to tell the number of EFFECTS that a
device can hold, so we'll just use a "random" number
instead and put warnings in SDL_haptic.h */
haptic->nplaying = 128; /* Even more impossible to get this then neffects. */
/* Prepare effects memory. */
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (haptic->effects == NULL) {
SDL_OutOfMemory();
goto acquire_err;
}
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
return 0;
/* Error handling */
acquire_err:
IDirectInputDevice8_Unacquire(haptic->hwdata->device);
return -1;
}
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
Feb 6, 2014
Feb 6, 2014
775
776
SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
return (item->bXInputHaptic) ? SDL_SYS_HapticOpenFromXInput(haptic, item->userid) : SDL_SYS_HapticOpenFromInstance(haptic, item->instance);
777
778
779
780
781
782
783
784
785
}
/*
* Opens a haptic device from first mouse it finds for usage.
*/
int
SDL_SYS_HapticMouse(void)
{
Feb 6, 2014
Feb 6, 2014
786
SDL_hapticlist_item *item;
Feb 6, 2014
Feb 6, 2014
787
int index = 0;
788
789
/* Grab the first mouse haptic device we find. */
Feb 6, 2014
Feb 6, 2014
790
791
792
793
for (item = SDL_hapticlist; item != NULL; item = item->next) {
SDL_assert(index >= 0);
if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
return index;
794
}
Feb 6, 2014
Feb 6, 2014
795
++index;
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
}
return -1;
}
/*
* Checks to see if a joystick has haptic features.
*/
int
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
{
const struct joystick_hwdata *hwdata = joystick->hwdata;
return ( (hwdata->bXInputHaptic) ||
((hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) != 0) );
}
/*
* Checks to see if the haptic device and joystick are in reality the same.
*/
int
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
if (joystick->hwdata->bXInputHaptic != haptic->hwdata->bXInputHaptic) {
return 0; /* one is XInput, one is not; not the same device. */
} else if (joystick->hwdata->bXInputHaptic) { /* XInput */
return (haptic->hwdata->userid == joystick->hwdata->userid);
} else { /* DirectInput */
HRESULT ret;
DIDEVICEINSTANCE hap_instance, joy_instance;
hap_instance.dwSize = sizeof(DIDEVICEINSTANCE);
joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
/* Get the device instances. */
ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device,
&hap_instance);
if (FAILED(ret)) {
return 0;
}
ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
&joy_instance);
if (FAILED(ret)) {
return 0;
}
if (DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance))
return 1;
}
return 0;
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
Feb 6, 2014
Feb 6, 2014
857
SDL_hapticlist_item *item;
Feb 6, 2014
Feb 6, 2014
858
int index = 0;
859
860
861
862
/* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
if (joystick->hwdata->bXInputDevice) {
const Uint8 userid = joystick->hwdata->userid;
Feb 6, 2014
Feb 6, 2014
863
864
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if ((item->bXInputHaptic) && (item->userid == userid)) {
865
SDL_assert(joystick->hwdata->bXInputHaptic);
Feb 6, 2014
Feb 6, 2014
866
867
haptic->index = index;
return SDL_SYS_HapticOpenFromXInput(haptic, userid);
868
}
Feb 6, 2014
Feb 6, 2014
869
++index;
870
871
}
} else {
Feb 6, 2014
Feb 6, 2014
872
873
874
875
876
877
878
879
880
881
882
883
HRESULT idret;
DIDEVICEINSTANCE joy_instance;
joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
if (FAILED(idret)) {
return -1;
}
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if (DI_GUIDIsSame(&item->instance.guidInstance, &joy_instance.guidInstance)) {
haptic->index = index;
884
885
return SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
}
Feb 6, 2014
Feb 6, 2014
886
++index;
887
888
}
}
Feb 6, 2014
Feb 6, 2014
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/* No match to our haptic list */
return -1;
}
/*
* Closes the haptic device.
*/
void
SDL_SYS_HapticClose(SDL_Haptic * haptic)
{
if (haptic->hwdata) {
/* Free effects. */
SDL_free(haptic->effects);
haptic->effects = NULL;
haptic->neffects = 0;
/* Clean up */
if (haptic->hwdata->bXInputHaptic) {
haptic->hwdata->stopThread = 1;
SDL_WaitThread(haptic->hwdata->thread, NULL);
SDL_DestroyMutex(haptic->hwdata->mutex);
} else {
IDirectInputDevice8_Unacquire(haptic->hwdata->device);
/* Only release if isn't grabbed by a joystick. */
if (haptic->hwdata->is_joystick == 0) {
IDirectInputDevice8_Release(haptic->hwdata->device);
}
}
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
}
/*
* Clean up after system specific haptic stuff
*/
void
SDL_SYS_HapticQuit(void)
{
Feb 6, 2014
Feb 6, 2014
934
935
SDL_hapticlist_item *item;
SDL_hapticlist_item *next = NULL;
936
937
938
939
940
941
if (loaded_xinput) {
WIN_UnloadXInputDLL();
loaded_xinput = SDL_FALSE;
}
Feb 6, 2014
Feb 6, 2014
942
943
944
945
946
947
for (item = SDL_hapticlist; item; item = next) {
/* Opened and not closed haptics are leaked, this is on purpose.
* Close your haptic devices after usage. */
next = item->next;
SDL_free(item->name);
SDL_free(item);
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
if (dinput != NULL) {
IDirectInput8_Release(dinput);
dinput = NULL;
}
if (coinitialized) {
WIN_CoUninitialize();
coinitialized = SDL_FALSE;
}
}
/*
* Converts an SDL trigger button to an DIEFFECT trigger button.
*/
static DWORD
DIGetTriggerButton(Uint16 button)
{
DWORD dwTriggerButton;
dwTriggerButton = DIEB_NOTRIGGER;
if (button != 0) {
dwTriggerButton = DIJOFS_BUTTON(button - 1);
}
return dwTriggerButton;
}
/*
* Sets the direction.
*/
static int
SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir, int naxes)
{
LONG *rglDir;
/* Handle no axes a part. */
if (naxes == 0) {
effect->dwFlags |= DIEFF_SPHERICAL; /* Set as default. */
effect->rglDirection = NULL;
return 0;
}
/* Has axes. */
rglDir = SDL_malloc(sizeof(LONG) * naxes);
if (rglDir == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(rglDir, 0, sizeof(LONG) * naxes);