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

Latest commit

 

History

History
1501 lines (1259 loc) · 43.6 KB

SDL_syshaptic.c

File metadata and controls

1501 lines (1259 loc) · 43.6 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;
May 18, 2013
May 18, 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
DWORD axes[3]; /* Axes to use. */
Jun 27, 2013
Jun 27, 2013
56
SDL_bool is_joystick; /* Device is loaded as joystick. */
May 18, 2013
May 18, 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
static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
Jun 27, 2013
Jun 27, 2013
95
96
LPDIRECTINPUTDEVICE8 device8,
SDL_bool is_joystick);
Mar 10, 2013
Mar 10, 2013
97
static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid);
98
99
100
101
102
103
104
105
106
107
108
109
110
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);
May 18, 2013
May 18, 2013
111
/*
112
113
* Like SDL_SetError but for DX error codes.
*/
Mar 31, 2013
Mar 31, 2013
114
static int
115
116
117
118
119
120
DI_SetError(const char *str, HRESULT err)
{
/*
SDL_SetError("Haptic: %s - %s: %s", str,
DXGetErrorString8A(err), DXGetErrorDescription8A(err));
*/
Mar 31, 2013
Mar 31, 2013
121
return SDL_SetError("Haptic error %s", str);
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}
/*
* 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
141
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
142
143
144
145
HRESULT ret;
HINSTANCE instance;
if (dinput != NULL) { /* Already open. */
Mar 31, 2013
Mar 31, 2013
146
return SDL_SetError("Haptic: SubSystem already open.");
147
148
149
150
151
152
153
154
155
}
/* 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
156
return DI_SetError("Coinitialize", ret);
157
158
159
160
}
coinitialized = SDL_TRUE;
Nov 29, 2012
Nov 29, 2012
161
162
ret = CoCreateInstance(&CLSID_DirectInput8, NULL, CLSCTX_INPROC_SERVER,
&IID_IDirectInput8, (LPVOID) & dinput);
163
164
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
165
return DI_SetError("CoCreateInstance", ret);
166
167
168
169
170
171
}
/* 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
172
173
return SDL_SetError("GetModuleHandle() failed with error code %d.",
GetLastError());
Nov 29, 2012
Nov 29, 2012
175
ret = IDirectInput8_Initialize(dinput, instance, DIRECTINPUT_VERSION);
176
177
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
178
return DI_SetError("Initializing DirectInput device", ret);
179
180
181
}
/* Look for haptic devices. */
Nov 29, 2012
Nov 29, 2012
182
ret = IDirectInput8_EnumDevices(dinput,
183
184
185
186
187
188
189
0,
EnumHapticsCallback,
NULL,
DIEDFL_FORCEFEEDBACK |
DIEDFL_ATTACHEDONLY);
if (FAILED(ret)) {
SDL_SYS_HapticQuit();
Mar 31, 2013
Mar 31, 2013
190
return DI_SetError("Enumerating DirectInput devices", ret);
Mar 10, 2013
Mar 10, 2013
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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++;
}
}
}
}
217
218
219
220
221
222
223
224
225
226
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
227
LPDIRECTINPUTDEVICE8 device;
228
229
230
231
232
233
/* 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
234
ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance,
235
236
237
238
239
240
241
242
&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
243
ret = IDirectInputDevice8_GetCapabilities(device,
244
245
246
247
&SDL_hapticlist[SDL_numhaptics].
capabilities);
if (FAILED(ret)) {
/* DI_SetError("Getting device capabilities",ret); */
Nov 29, 2012
Nov 29, 2012
248
IDirectInputDevice8_Release(device);
249
250
251
252
253
254
255
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
256
IDirectInputDevice8_Release(device);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
336
* - Create DirectInputDevice8 interface.
337
* - Release DirectInputDevice interface.
Nov 29, 2012
Nov 29, 2012
338
* - Call SDL_SYS_HapticOpenFromDevice8
339
340
341
342
343
344
*/
static int
SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
{
HRESULT ret;
int ret2;
Nov 29, 2012
Nov 29, 2012
345
LPDIRECTINPUTDEVICE8 device;
Jun 27, 2013
Jun 27, 2013
346
LPDIRECTINPUTDEVICE8 device8;
347
348
/* Open the device */
Nov 29, 2012
Nov 29, 2012
349
ret = IDirectInput8_CreateDevice(dinput, &instance.guidInstance,
350
351
352
&device, NULL);
if (FAILED(ret)) {
DI_SetError("Creating DirectInput device", ret);
Jun 27, 2013
Jun 27, 2013
353
return -1;
Nov 29, 2012
Nov 29, 2012
356
357
358
/* Now get the IDirectInputDevice8 interface, instead. */
ret = IDirectInputDevice8_QueryInterface(device,
&IID_IDirectInputDevice8,
Jun 27, 2013
Jun 27, 2013
359
(LPVOID *) &device8);
360
/* Done with the temporary one now. */
Nov 29, 2012
Nov 29, 2012
361
IDirectInputDevice8_Release(device);
362
363
if (FAILED(ret)) {
DI_SetError("Querying DirectInput interface", ret);
Jun 27, 2013
Jun 27, 2013
364
return -1;
Jun 27, 2013
Jun 27, 2013
367
ret2 = SDL_SYS_HapticOpenFromDevice8(haptic, device8, SDL_FALSE);
Jun 27, 2013
Jun 27, 2013
369
370
IDirectInputDevice8_Release(device8);
return -1;
371
372
373
374
375
}
return 0;
}
Mar 10, 2013
Mar 10, 2013
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
392
return SDL_OutOfMemory();
Mar 10, 2013
Mar 10, 2013
393
394
395
396
397
398
399
400
401
}
/* 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
402
return SDL_OutOfMemory();
Mar 10, 2013
Mar 10, 2013
403
404
405
406
407
408
409
410
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
haptic->hwdata->bXInputHaptic = 1;
haptic->hwdata->userid = userid;
return 0;
}
411
412
413
414
415
416
417
418
419
/*
* Opens the haptic device from the file descriptor.
*
* Steps:
* - Set cooperative level.
* - Set data format.
* - Acquire exclusiveness.
* - Reset actuators.
May 1, 2013
May 1, 2013
420
* - Get supported features.
Nov 29, 2012
Nov 29, 2012
423
SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
Jun 27, 2013
Jun 27, 2013
424
LPDIRECTINPUTDEVICE8 device8, SDL_bool is_joystick)
425
426
427
428
{
HRESULT ret;
DIPROPDWORD dipdw;
Jun 27, 2013
Jun 27, 2013
429
430
431
432
433
434
435
/* 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));
Nov 29, 2012
Nov 29, 2012
436
437
/* We'll use the device8 from now on. */
haptic->hwdata->device = device8;
Jun 27, 2013
Jun 27, 2013
438
haptic->hwdata->is_joystick = is_joystick;
439
440
/* Grab it exclusively to use force feedback stuff. */
Nov 29, 2012
Nov 29, 2012
441
ret = IDirectInputDevice8_SetCooperativeLevel(haptic->hwdata->device,
442
443
444
445
446
447
448
449
450
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
451
ret = IDirectInputDevice8_SetDataFormat(haptic->hwdata->device,
452
453
454
455
456
457
458
&c_dfDIJoystick2);
if (FAILED(ret)) {
DI_SetError("Setting data format", ret);
goto acquire_err;
}
/* Get number of axes. */
Nov 29, 2012
Nov 29, 2012
459
ret = IDirectInputDevice8_EnumObjects(haptic->hwdata->device,
460
461
462
463
464
465
466
467
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
468
ret = IDirectInputDevice8_Acquire(haptic->hwdata->device);
469
470
471
472
473
474
if (FAILED(ret)) {
DI_SetError("Acquiring DirectInput device", ret);
goto acquire_err;
}
/* Reset all actuators - just in case. */
Nov 29, 2012
Nov 29, 2012
475
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
476
477
478
479
480
481
482
DISFFC_RESET);
if (FAILED(ret)) {
DI_SetError("Resetting device", ret);
goto acquire_err;
}
/* Enabling actuators. */
Nov 29, 2012
Nov 29, 2012
483
ret = IDirectInputDevice8_SendForceFeedbackCommand(haptic->hwdata->device,
484
485
486
487
488
489
490
DISFFC_SETACTUATORSON);
if (FAILED(ret)) {
DI_SetError("Enabling actuators", ret);
goto acquire_err;
}
/* Get supported effects. */
Nov 29, 2012
Nov 29, 2012
491
ret = IDirectInputDevice8_EnumEffects(haptic->hwdata->device,
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
509
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
510
511
512
513
514
515
516
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
517
ret = IDirectInputDevice8_SetProperty(haptic->hwdata->device,
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
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
548
IDirectInputDevice8_Unacquire(haptic->hwdata->device);
549
550
551
552
553
554
555
556
557
558
559
return -1;
}
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
Mar 10, 2013
Mar 10, 2013
560
561
562
563
564
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);
565
566
567
568
569
570
571
572
573
574
575
576
577
}
/*
* 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
578
if (SDL_hapticlist[i].capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
579
580
581
582
583
584
585
586
587
588
589
590
591
592
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
593
594
595
const struct joystick_hwdata *hwdata = joystick->hwdata;
return ( (hwdata->bXInputHaptic) ||
((hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) != 0) );
596
597
598
599
600
601
602
603
604
}
/*
* 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
605
606
607
608
609
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
611
612
613
614
615
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
617
618
619
620
621
622
623
624
if (FAILED(ret)) {
return 0;
}
ret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice,
&joy_instance);
if (FAILED(ret)) {
return 0;
}
Mar 10, 2013
Mar 10, 2013
626
627
628
if (DI_GUIDIsSame(&hap_instance.guidInstance, &joy_instance.guidInstance))
return 1;
}
629
630
631
632
633
634
635
636
637
638
639
return 0;
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
Jun 27, 2013
Jun 27, 2013
640
int i;
641
642
643
644
645
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
646
647
648
649
650
651
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;
Jun 27, 2013
Jun 27, 2013
652
return SDL_SYS_HapticOpenFromXInput(haptic, SDL_hapticlist[haptic->index].userid);
Mar 10, 2013
Mar 10, 2013
653
}
Mar 10, 2013
Mar 10, 2013
655
656
} else {
for (i=0; i<SDL_numhaptics; i++) {
Jun 27, 2013
Jun 27, 2013
657
idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
Mar 10, 2013
Mar 10, 2013
658
659
660
661
662
663
if (FAILED(idret)) {
return -1;
}
if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
&joy_instance.guidInstance)) {
haptic->index = i;
Jun 27, 2013
Jun 27, 2013
664
return SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
Mar 10, 2013
Mar 10, 2013
665
}
Jun 27, 2013
Jun 27, 2013
668
669
/* No match to our haptic list */
return -1;
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
}
/*
* 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
687
688
689
690
691
692
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);
}
693
694
695
696
697
698
699
700
701
}
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
}
May 18, 2013
May 18, 2013
702
/*
703
704
705
706
707
708
709
* Clean up after system specific haptic stuff
*/
void
SDL_SYS_HapticQuit(void)
{
int i;
Mar 10, 2013
Mar 10, 2013
710
711
712
713
714
if (loaded_xinput) {
WIN_UnloadXInputDLL();
loaded_xinput = SDL_FALSE;
}
715
716
717
718
719
720
721
722
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
723
IDirectInput8_Release(dinput);
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
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
770
return SDL_OutOfMemory();
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
}
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
798
return SDL_SetError("Haptic: Unknown direction type.");
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
}
}
#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
834
return SDL_OutOfMemory();
835
836
837
838
839
840
841
842
843
844
}
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
845
return SDL_OutOfMemory();
846
847
848
849
850
851
852
853
854
855
856
857
}
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;
}
May 1, 2013
May 1, 2013
858
/* The big type handling switch, even bigger then Linux's version. */
859
860
861
862
863
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
864
return SDL_OutOfMemory();
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
}
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
907
return SDL_OutOfMemory();
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
940
941
942
943
944
945
946
947
948
949
950
951
}
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
952
return SDL_OutOfMemory();
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
}
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]);
}
dest->cbTypeSpecificParams = sizeof(DICONDITION) * dest->cAxes;
dest->lpvTypeSpecificParams = condition;
/* Generics */
dest->dwDuration = hap_condition->length * 1000; /* In microseconds. */
dest->dwTriggerButton = DIGetTriggerButton(hap_condition->button);
dest->dwTriggerRepeatInterval = hap_condition->interval;
dest->dwStartDelay = hap_condition->delay * 1000; /* In microseconds. */
/* Direction. */
if (SDL_SYS_SetDirection(dest, &hap_condition->direction, dest->cAxes)
< 0) {
return -1;
}
/* Envelope - Not actually supported by most CONDITION implementations. */
SDL_free(dest->lpEnvelope);
dest->lpEnvelope = NULL;
break;
case SDL_HAPTIC_RAMP:
hap_ramp = &src->ramp;
ramp = SDL_malloc(sizeof(DIRAMPFORCE));
if (ramp == NULL) {
Mar 31, 2013
Mar 31, 2013
994
return SDL_OutOfMemory();
995
996
997
998
999
1000
}
SDL_memset(ramp, 0, sizeof(DIRAMPFORCE));
/* Specifics */
ramp->lStart = CONVERT(hap_ramp->start);
ramp->lEnd = CONVERT(hap_ramp->end);