Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1532 lines (1285 loc) · 44.2 KB

SDL_syshaptic.c

File metadata and controls

1532 lines (1285 loc) · 44.2 KB
 
1
2
/*
Simple DirectMedia Layer
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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_HAPTIC_DINPUT
Mar 10, 2013
Mar 10, 2013
25
26
#include "SDL_assert.h"
#include "SDL_hints.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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 */
#define MAX_HAPTICS 32
/*
* List of available haptic devices.
*/
static struct
{
DIDEVICEINSTANCE instance;
char *name;
SDL_Haptic *haptic;
DIDEVCAPS capabilities;
Mar 10, 2013
Mar 10, 2013
44
45
Uint8 bXInputHaptic; // Supports force feedback via XInput.
Uint8 userid; // XInput userid index for this joystick
46
47
48
49
50
51
52
53
} SDL_hapticlist[MAX_HAPTICS];
/*
* Haptic system hardware data.
*/
struct haptic_hwdata
{
Nov 29, 2012
Nov 29, 2012
54
LPDIRECTINPUTDEVICE8 device;
55
56
DWORD axes[3]; /* Axes to use. */
int is_joystick; /* Device is loaded as joystick. */
Mar 10, 2013
Mar 10, 2013
57
58
Uint8 bXInputHaptic; // Supports force feedback via XInput.
Uint8 userid; // XInput userid index for this joystick
59
60
61
62
63
64
65
66
67
68
};
/*
* Haptic system effect data.
*/
struct haptic_hweffect
{
DIEFFECT effect;
LPDIRECTINPUTEFFECT ref;
Mar 10, 2013
Mar 10, 2013
69
XINPUT_VIBRATION vibration;
70
71
72
73
74
75
76
};
/*
* Internal stuff.
*/
static SDL_bool coinitialized = SDL_FALSE;
Nov 29, 2012
Nov 29, 2012
77
static LPDIRECTINPUT8 dinput = NULL;
Mar 10, 2013
Mar 10, 2013
78
static SDL_bool loaded_xinput = SDL_FALSE;
79
80
81
82
83
84
85
86
87
88
89
/*
* External stuff.
*/
extern HWND SDL_HelperWindow;
/*
* Prototypes.
*/
Mar 31, 2013
Mar 31, 2013
90
static int DI_SetError(const char *str, HRESULT err);
91
92
93
static int DI_GUIDIsSame(const GUID * a, const GUID * b);
static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
DIDEVICEINSTANCE instance);
Nov 29, 2012
Nov 29, 2012
94
95
static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
LPDIRECTINPUTDEVICE8 device8);
Mar 10, 2013
Mar 10, 2013
96
static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);
/* 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.
*/
Mar 31, 2013
Mar 31, 2013
113
static int
114
115
116
117
118
119
DI_SetError(const char *str, HRESULT err)
{
/*
SDL_SetError("Haptic: %s - %s: %s", str,
DXGetErrorString8A(err), DXGetErrorDescription8A(err));
*/
Mar 31, 2013
Mar 31, 2013
120
return SDL_SetError("Haptic error %s", str);
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}
/*
* 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)
{
Mar 10, 2013
Mar 10, 2013
140
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
141
142
143
144
HRESULT ret;
HINSTANCE instance;
if (dinput != NULL) { /* Already open. */
Mar 31, 2013
Mar 31, 2013
145
return SDL_SetError("Haptic: SubSystem already open.");
146
147
148
149
150
151
152
153
154
}
/* Clear all the memory. */
SDL_memset(SDL_hapticlist, 0, sizeof(SDL_hapticlist));
SDL_numhaptics = 0;
ret = WIN_CoInitialize();
if (FAILED(ret)) {
Mar 31, 2013
Mar 31, 2013
155
return DI_SetError("Coinitialize", ret);
156
157
158
159
}
coinitialized = SDL_TRUE;
Nov 29, 2012
Nov 29, 2012
160
161
ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
&IID_IDirectInput8, (LPVOID) & dinput);
162
163
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
164
return DI_SetError("CoCreateInstance", ret);
165
166
167
168
169
170
}
/* Because we used CoCreateInstance, we need to Initialize it, first. */
instance = GetModuleHandle(NULL);
if (instance == NULL) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
171
172
return SDL_SetError("GetModuleHandle() failed with error code %d.",
GetLastError());
Nov 29, 2012
Nov 29, 2012
174
ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
175
176
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
177
return DI_SetError("Initializing DirectInput device", ret);
178
179
180
}
/* Look for haptic devices. */
Nov 29, 2012
Nov 29, 2012
181
ret = IDirectInput8_EnumDevices(dinput,
182
183
184
185
186
187
188
0,
EnumHapticsCallback,
NULL,
DIEDFL_FORCEFEEDBACK |
DIEDFL_ATTACHEDONLY);
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
189
return DI_SetError("Enumerating DirectInput devices", ret);
Mar 10, 2013
Mar 10, 2013
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
if (!env || SDL_atoi(env)) {
loaded_xinput = (WIN_LoadXInputDLL() == 0);
}
if (loaded_xinput) {
DWORD i;
const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
for (i = 0; (i < SDL_XINPUT_MAX_DEVICES) && (SDL_numhaptics < MAX_HAPTICS); i++) {
XINPUT_CAPABILITIES caps;
if (XINPUTGETCAPABILITIES(i, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) {
if ((!bIs14OrLater) || (caps.Flags & XINPUT_CAPS_FFB_SUPPORTED)) {
/* !!! FIXME: I'm not bothering to query for a real name right now. */
char buf[64];
SDL_snprintf(buf, sizeof (buf), "XInput Controller #%u", i+1);
SDL_hapticlist[SDL_numhaptics].name = SDL_strdup(buf);
SDL_hapticlist[SDL_numhaptics].bXInputHaptic = 1;
SDL_hapticlist[SDL_numhaptics].userid = (Uint8) i;
SDL_numhaptics++;
}
}
}
}
216
217
218
219
220
221
222
223
224
225
return SDL_numhaptics;
}
/*
* Callback to find the haptic devices.
*/
static BOOL CALLBACK
EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
{
HRESULT ret;
Nov 29, 2012
Nov 29, 2012
226
LPDIRECTINPUTDEVICE8 device;
227
228
229
230
231
232
/* Copy the instance over, useful for creating devices. */
SDL_memcpy(&SDL_hapticlist[SDL_numhaptics].instance, pdidInstance,
sizeof(DIDEVICEINSTANCE));
/* Open the device */
Nov 29, 2012
Nov 29, 2012
233
ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance,
234
235
236
237
238
239
240
241
&device, NULL);
if (FAILED(ret)) {
/* DI_SetError("Creating DirectInput device",ret); */
return DIENUM_CONTINUE;
}
/* Get capabilities. */
SDL_hapticlist[SDL_numhaptics].capabilities.dwSize = sizeof(DIDEVCAPS);
Nov 29, 2012
Nov 29, 2012
242
ret = IDirectInputDevice8_GetCapabilities(device,
243
244
245
246
&SDL_hapticlist[SDL_numhaptics].
capabilities);
if (FAILED(ret)) {
/* DI_SetError("Getting device capabilities",ret); */
Nov 29, 2012
Nov 29, 2012
247
IDirectInputDevice8_Release(device);
248
249
250
251
252
253
254
return DIENUM_CONTINUE;
}
/* Copy the name */
SDL_hapticlist[SDL_numhaptics].name = WIN_StringToUTF8(SDL_hapticlist[SDL_numhaptics].instance.tszProductName);
/* Close up device and count it. */
Nov 29, 2012
Nov 29, 2012
255
IDirectInputDevice8_Release(device);
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
SDL_numhaptics++;
/* Watch out for hard limit. */
if (SDL_numhaptics >= MAX_HAPTICS)
return DIENUM_STOP;
return DIENUM_CONTINUE;
}
/*
* Return the name of a haptic device, does not need to be opened.
*/
const char *
SDL_SYS_HapticName(int index)
{
return SDL_hapticlist[index].name;
}
/*
* 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);
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.
Nov 29, 2012
Nov 29, 2012
335
* - Create DirectInputDevice8 interface.
336
* - Release DirectInputDevice interface.
Nov 29, 2012
Nov 29, 2012
337
* - Call SDL_SYS_HapticOpenFromDevice8
338
339
340
341
342
343
*/
static int
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
{
HRESULT ret;
int ret2;
Nov 29, 2012
Nov 29, 2012
344
LPDIRECTINPUTDEVICE8 device;
345
346
347
348
349
350
351
352
353
354
355
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
SDL_OutOfMemory();
goto creat_err;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* Open the device */
Nov 29, 2012
Nov 29, 2012
356
ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
357
358
359
360
361
362
&device, NULL);
if (FAILED(ret)) {
DI_SetError("Creating DirectInput device", ret);
goto creat_err;
}
Nov 29, 2012
Nov 29, 2012
363
364
365
/* Now get the IDirectInputDevice8 interface, instead. */
ret = IDirectInputDevice8_QueryInterface(device,
&IID_IDirectInputDevice8,
366
367
368
(LPVOID *) & haptic->hwdata->
device);
/* Done with the temporary one now. */
Nov 29, 2012
Nov 29, 2012
369
IDirectInputDevice8_Release(device);
370
371
372
373
374
if (FAILED(ret)) {
DI_SetError("Querying DirectInput interface", ret);
goto creat_err;
}
Nov 29, 2012
Nov 29, 2012
375
ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, haptic->hwdata->device);
376
377
378
379
380
381
382
if (ret2 < 0) {
goto query_err;
}
return 0;
query_err:
Nov 29, 2012
Nov 29, 2012
383
IDirectInputDevice8_Release(haptic->hwdata->device);
384
385
386
387
388
389
390
391
creat_err:
if (haptic->hwdata != NULL) {
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
return -1;
}
Mar 10, 2013
Mar 10, 2013
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
static int
SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid)
{
XINPUT_VIBRATION vibration = { 0, 0 }; /* stop any current vibration */
XINPUTSETSTATE(userid, &vibration);
/* !!! FIXME: we can probably do more than SINE if we figure out how to set up the left and right motors properly. */
haptic->supported = SDL_HAPTIC_SINE;
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) {
Mar 31, 2013
Mar 31, 2013
408
return SDL_OutOfMemory();
Mar 10, 2013
Mar 10, 2013
409
410
411
412
413
414
415
416
417
}
/* 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;
Mar 31, 2013
Mar 31, 2013
418
return SDL_OutOfMemory();
Mar 10, 2013
Mar 10, 2013
419
420
421
422
423
424
425
426
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
haptic->hwdata->bXInputHaptic = 1;
haptic->hwdata->userid = userid;
return 0;
}
427
428
429
430
431
432
433
434
435
436
437
438
/*
* Opens the haptic device from the file descriptor.
*
* Steps:
* - Set cooperative level.
* - Set data format.
* - Acquire exclusiveness.
* - Reset actuators.
* - Get supported featuers.
*/
static int
Nov 29, 2012
Nov 29, 2012
439
440
SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
LPDIRECTINPUTDEVICE8 device8)
441
442
443
444
{
HRESULT ret;
DIPROPDWORD dipdw;
Nov 29, 2012
Nov 29, 2012
445
446
/* We'll use the device8 from now on. */
haptic->hwdata->device = device8;
447
448
/* Grab it exclusively to use force feedback stuff. */
Nov 29, 2012
Nov 29, 2012
449
ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
450
451
452
453
454
455
456
457
458
SDL_HelperWindow,
DISCL_EXCLUSIVE |
DISCL_BACKGROUND);
if (FAILED(ret)) {
DI_SetError("Setting cooperative level to exclusive", ret);
goto acquire_err;
}
/* Set data format. */
Nov 29, 2012
Nov 29, 2012
459
ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
460
461
462
463
464
465
466
&c_dfDIJoystick2);
if (FAILED(ret)) {
DI_SetError("Setting data format", ret);
goto acquire_err;
}
/* Get number of axes. */
Nov 29, 2012
Nov 29, 2012
467
ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
468
469
470
471
472
473
474
475
DI_DeviceObjectCallback,
haptic, DIDFT_AXIS);
if (FAILED(ret)) {
DI_SetError("Getting device axes", ret);
goto acquire_err;
}
/* Acquire the device. */
Nov 29, 2012
Nov 29, 2012
476
ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
477
478
479
480
481
482
if (FAILED(ret)) {
DI_SetError("Acquiring DirectInput device", ret);
goto acquire_err;
}
/* Reset all actuators - just in case. */
Nov 29, 2012
Nov 29, 2012
483
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
484
485
486
487
488
489
490
DISFFC_RESET);
if (FAILED(ret)) {
DI_SetError("Resetting device", ret);
goto acquire_err;
}
/* Enabling actuators. */
Nov 29, 2012
Nov 29, 2012
491
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
492
493
494
495
496
497
498
DISFFC_SETACTUATORSON);
if (FAILED(ret)) {
DI_SetError("Enabling actuators", ret);
goto acquire_err;
}
/* Get supported effects. */
Nov 29, 2012
Nov 29, 2012
499
ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
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;
Nov 29, 2012
Nov 29, 2012
517
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
518
519
520
521
522
523
524
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;
Nov 29, 2012
Nov 29, 2012
525
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
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
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:
Nov 29, 2012
Nov 29, 2012
556
IDirectInputDevice8_Unacquire(haptic->hwdata->device);
557
558
559
560
561
562
563
564
565
566
567
return -1;
}
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
Mar 10, 2013
Mar 10, 2013
568
569
570
571
572
if (SDL_hapticlist[haptic->index].bXInputHaptic) {
return SDL_SYS_HapticOpenFromXInput(haptic, SDL_hapticlist[haptic->index].userid);
}
return SDL_SYS_HapticOpenFromInstance(haptic, SDL_hapticlist[haptic->index].instance);
573
574
575
576
577
578
579
580
581
582
583
584
585
}
/*
* Opens a haptic device from first mouse it finds for usage.
*/
int
SDL_SYS_HapticMouse(void)
{
int i;
/* Grab the first mouse haptic device we find. */
for (i = 0; i < SDL_numhaptics; i++) {
Nov 27, 2012
Nov 27, 2012
586
if (SDL_hapticlist[i].capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
587
588
589
590
591
592
593
594
595
596
597
598
599
600
return i;
}
}
return -1;
}
/*
* Checks to see if a joystick has haptic features.
*/
int
SDL_SYS_JoystickIsHaptic(SDL_Joystick * joystick)
{
Mar 10, 2013
Mar 10, 2013
601
602
603
const struct joystick_hwdata *hwdata = joystick->hwdata;
return ( (hwdata->bXInputHaptic) ||
((hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) != 0) );
604
605
606
607
608
609
610
611
612
}
/*
* Checks to see if the haptic device and joystick and in reality the same.
*/
int
SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
Mar 10, 2013
Mar 10, 2013
613
614
615
616
617
if ((joystick->hwdata->bXInputHaptic == haptic->hwdata->bXInputHaptic) && (haptic->hwdata->userid == joystick->hwdata->userid)) {
return 1;
} else {
HRESULT ret;
DIDEVICEINSTANCE hap_instance, joy_instance;
Mar 10, 2013
Mar 10, 2013
619
620
621
622
623
hap_instance.dwSize = sizeof(DIDEVICEINSTANCE);
joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
/* Get the device instances. */
ret = IDirectInputDevice8_GetDeviceInfo(haptic->hwdata->device,
Mar 10, 2013
Mar 10, 2013
625
626
627
628
629
630
631
632
if (FAILED(ret)) {
return 0;
}
ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
&joy_instance);
if (FAILED(ret)) {
return 0;
}
Mar 10, 2013
Mar 10, 2013
634
635
636
if (DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance))
return 1;
}
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
return 0;
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
int i, ret;
HRESULT idret;
DIDEVICEINSTANCE joy_instance;
joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
/* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
Mar 10, 2013
Mar 10, 2013
654
655
656
657
658
659
660
661
if (joystick->hwdata->bXInputDevice) {
const Uint8 userid = joystick->hwdata->userid;
for (i=0; i<SDL_numhaptics; i++) {
if ((SDL_hapticlist[i].bXInputHaptic) && (SDL_hapticlist[i].userid == userid)) {
SDL_assert(joystick->hwdata->bXInputHaptic);
haptic->index = i;
break;
}
Mar 10, 2013
Mar 10, 2013
663
664
665
666
667
668
669
670
671
672
673
674
} else {
for (i=0; i<SDL_numhaptics; i++) {
idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
&joy_instance);
if (FAILED(idret)) {
return -1;
}
if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
&joy_instance.guidInstance)) {
haptic->index = i;
break;
}
675
676
677
678
679
680
681
682
683
684
}
}
if (i >= SDL_numhaptics) {
return -1;
}
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
if (haptic->hwdata == NULL) {
Mar 31, 2013
Mar 31, 2013
685
return SDL_OutOfMemory();
686
687
688
689
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* Now open the device. */
Mar 10, 2013
Mar 10, 2013
690
691
692
693
694
if (!joystick->hwdata->bXInputHaptic) {
ret = SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice);
if (ret < 0) {
return -1;
}
695
696
697
698
}
/* It's using the joystick device. */
haptic->hwdata->is_joystick = 1;
Mar 10, 2013
Mar 10, 2013
699
700
haptic->hwdata->bXInputHaptic = joystick->hwdata->bXInputHaptic;
haptic->hwdata->userid = joystick->hwdata->userid;
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
return 0;
}
/*
* 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 */
Mar 10, 2013
Mar 10, 2013
720
721
722
723
724
725
if (!haptic->hwdata->bXInputHaptic) {
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);
}
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
}
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
}
/*
* Clean up after system specific haptic stuff
*/
void
SDL_SYS_HapticQuit(void)
{
int i;
Mar 10, 2013
Mar 10, 2013
743
744
745
746
747
if (loaded_xinput) {
WIN_UnloadXInputDLL();
loaded_xinput = SDL_FALSE;
}
748
749
750
751
752
753
754
755
for (i = 0; i < SDL_arraysize(SDL_hapticlist); ++i) {
if (SDL_hapticlist[i].name) {
SDL_free(SDL_hapticlist[i].name);
SDL_hapticlist[i].name = NULL;
}
}
if (dinput != NULL) {
Nov 29, 2012
Nov 29, 2012
756
IDirectInput8_Release(dinput);
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
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) {
Mar 31, 2013
Mar 31, 2013
803
return SDL_OutOfMemory();
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
}
SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
effect->rglDirection = rglDir;
switch (dir->type) {
case SDL_HAPTIC_POLAR:
effect->dwFlags |= DIEFF_POLAR;
rglDir[0] = dir->dir[0];
return 0;
case SDL_HAPTIC_CARTESIAN:
effect->dwFlags |= DIEFF_CARTESIAN;
rglDir[0] = dir->dir[0];
if (naxes > 1)
rglDir[1] = dir->dir[1];
if (naxes > 2)
rglDir[2] = dir->dir[2];
return 0;
case SDL_HAPTIC_SPHERICAL:
effect->dwFlags |= DIEFF_SPHERICAL;
rglDir[0] = dir->dir[0];
if (naxes > 1)
rglDir[1] = dir->dir[1];
if (naxes > 2)
rglDir[2] = dir->dir[2];
return 0;
default:
Mar 31, 2013
Mar 31, 2013
831
return SDL_SetError("Haptic: Unknown direction type.");
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
857
858
859
860
861
862
863
864
865
866
}
}
#define CONVERT(x) (((x) > 0x7FFF) ? 10000 : ((x)*10000) / 0x7FFF)
/*
* Creates the DIEFFECT from a SDL_HapticEffect.
*/
static int
SDL_SYS_ToDIEFFECT(SDL_Haptic * haptic, DIEFFECT * dest,
SDL_HapticEffect * src)
{
int i;
DICONSTANTFORCE *constant;
DIPERIODIC *periodic;
DICONDITION *condition; /* Actually an array of conditions - one per axis. */
DIRAMPFORCE *ramp;
DICUSTOMFORCE *custom;
DIENVELOPE *envelope;
SDL_HapticConstant *hap_constant;
SDL_HapticPeriodic *hap_periodic;
SDL_HapticCondition *hap_condition;
SDL_HapticRamp *hap_ramp;
SDL_HapticCustom *hap_custom;
DWORD *axes;
/* Set global stuff. */
SDL_memset(dest, 0, sizeof(DIEFFECT));
dest->dwSize = sizeof(DIEFFECT); /* Set the structure size. */
dest->dwSamplePeriod = 0; /* Not used by us. */
dest->dwGain = 10000; /* Gain is set globally, not locally. */
dest->dwFlags = DIEFF_OBJECTOFFSETS; /* Seems obligatory. */
/* Envelope. */
envelope = SDL_malloc(sizeof(DIENVELOPE));
if (envelope == NULL) {
Mar 31, 2013
Mar 31, 2013
867
return SDL_OutOfMemory();
868
869
870
871
872
873
874
875
876
877
}
SDL_memset(envelope, 0, sizeof(DIENVELOPE));
dest->lpEnvelope = envelope;
envelope->dwSize = sizeof(DIENVELOPE); /* Always should be this. */
/* Axes. */
dest->cAxes = haptic->naxes;
if (dest->cAxes > 0) {
axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
if (axes == NULL) {
Mar 31, 2013
Mar 31, 2013
878
return SDL_OutOfMemory();
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
}
axes[0] = haptic->hwdata->axes[0]; /* Always at least one axis. */
if (dest->cAxes > 1) {
axes[1] = haptic->hwdata->axes[1];
}
if (dest->cAxes > 2) {
axes[2] = haptic->hwdata->axes[2];
}
dest->rgdwAxes = axes;
}
/* The big type handling switch, even bigger then linux's version. */
switch (src->type) {
case SDL_HAPTIC_CONSTANT:
hap_constant = &src->constant;
constant = SDL_malloc(sizeof(DICONSTANTFORCE));
if (constant == NULL) {
Mar 31, 2013
Mar 31, 2013
897
return SDL_OutOfMemory();
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
934
935
936
937
938
939
}
SDL_memset(constant, 0, sizeof(DICONSTANTFORCE));
/* Specifics */
constant->lMagnitude = CONVERT(hap_constant->level);
dest->cbTypeSpecificParams = sizeof(DICONSTANTFORCE);
dest->lpvTypeSpecificParams = constant;
/* Generics */
dest->dwDuration = hap_constant->length * 1000; /* In microseconds. */
dest->dwTriggerButton = DIGetTriggerButton(hap_constant->button);
dest->dwTriggerRepeatInterval = hap_constant->interval;
dest->dwStartDelay = hap_constant->delay * 1000; /* In microseconds. */
/* Direction. */
if (SDL_SYS_SetDirection(dest, &hap_constant->direction, dest->cAxes)
< 0) {
return -1;
}
/* Envelope */
if ((hap_constant->attack_length == 0)
&& (hap_constant->fade_length == 0)) {
SDL_free(dest->lpEnvelope);
dest->lpEnvelope = NULL;
} else {
envelope->dwAttackLevel = CONVERT(hap_constant->attack_level);
envelope->dwAttackTime = hap_constant->attack_length * 1000;
envelope->dwFadeLevel = CONVERT(hap_constant->fade_level);
envelope->dwFadeTime = hap_constant->fade_length * 1000;
}
break;
case SDL_HAPTIC_SINE:
case SDL_HAPTIC_SQUARE:
case SDL_HAPTIC_TRIANGLE:
case SDL_HAPTIC_SAWTOOTHUP:
case SDL_HAPTIC_SAWTOOTHDOWN:
hap_periodic = &src->periodic;
periodic = SDL_malloc(sizeof(DIPERIODIC));
if (periodic == NULL) {
Mar 31, 2013
Mar 31, 2013
940
return SDL_OutOfMemory();
941
942
943
944
945
946
947
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
}
SDL_memset(periodic, 0, sizeof(DIPERIODIC));
/* Specifics */
periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
periodic->lOffset = CONVERT(hap_periodic->offset);
periodic->dwPhase = hap_periodic->phase;
periodic->dwPeriod = hap_periodic->period * 1000;
dest->cbTypeSpecificParams = sizeof(DIPERIODIC);
dest->lpvTypeSpecificParams = periodic;
/* Generics */
dest->dwDuration = hap_periodic->length * 1000; /* In microseconds. */
dest->dwTriggerButton = DIGetTriggerButton(hap_periodic->button);
dest->dwTriggerRepeatInterval = hap_periodic->interval;
dest->dwStartDelay = hap_periodic->delay * 1000; /* In microseconds. */
/* Direction. */
if (SDL_SYS_SetDirection(dest, &hap_periodic->direction, dest->cAxes)
< 0) {
return -1;
}
/* Envelope */
if ((hap_periodic->attack_length == 0)
&& (hap_periodic->fade_length == 0)) {
SDL_free(dest->lpEnvelope);
dest->lpEnvelope = NULL;
} else {
envelope->dwAttackLevel = CONVERT(hap_periodic->attack_level);
envelope->dwAttackTime = hap_periodic->attack_length * 1000;
envelope->dwFadeLevel = CONVERT(hap_periodic->fade_level);
envelope->dwFadeTime = hap_periodic->fade_length * 1000;
}
break;
case SDL_HAPTIC_SPRING:
case SDL_HAPTIC_DAMPER:
case SDL_HAPTIC_INERTIA:
case SDL_HAPTIC_FRICTION:
hap_condition = &src->condition;
condition = SDL_malloc(sizeof(DICONDITION) * dest->cAxes);
if (condition == NULL) {
Mar 31, 2013
Mar 31, 2013
985
return SDL_OutOfMemory();
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
}
SDL_memset(condition, 0, sizeof(DICONDITION));
/* Specifics */
for (i = 0; i < (int) dest->cAxes; i++) {
condition[i].lOffset = CONVERT(hap_condition->center[i]);
condition[i].lPositiveCoefficient =
CONVERT(hap_condition->right_coeff[i]);
condition[i].lNegativeCoefficient =
CONVERT(hap_condition->left_coeff[i]);
condition[i].dwPositiveSaturation =
CONVERT(hap_condition->right_sat[i]);
condition[i].dwNegativeSaturation =
CONVERT(hap_condition->left_sat[i]);
condition[i].lDeadBand = CONVERT(hap_condition->deadband[i]);