Skip to content

Latest commit

 

History

History
486 lines (408 loc) · 12.9 KB

SDL_xinputhaptic.c

File metadata and controls

486 lines (408 loc) · 12.9 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 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
25
26
27
28
29
30
31
32
33
34
35
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_internal.h"
#include "SDL_error.h"
#include "SDL_haptic.h"
#include "../SDL_syshaptic.h"
#if SDL_HAPTIC_XINPUT
#include "SDL_assert.h"
#include "SDL_hints.h"
#include "SDL_timer.h"
#include "SDL_windowshaptic_c.h"
#include "SDL_xinputhaptic_c.h"
#include "../../core/windows/SDL_xinput.h"
#include "../../joystick/windows/SDL_windowsjoystick_c.h"
Apr 12, 2016
Apr 12, 2016
36
#include "../../thread/SDL_systhread.h"
37
38
39
40
41
42
43
44
45
46
/*
* Internal stuff.
*/
static SDL_bool loaded_xinput = SDL_FALSE;
int
SDL_XINPUT_HapticInit(void)
{
Oct 8, 2016
Oct 8, 2016
47
if (SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE)) {
48
49
50
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
loaded_xinput = (WIN_LoadXInputDLL() == 0);
}
if (loaded_xinput) {
DWORD i;
for (i = 0; i < XUSER_MAX_COUNT; i++) {
SDL_XINPUT_MaybeAddDevice(i);
}
}
return 0;
}
int
SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid)
{
const Uint8 userid = (Uint8)dwUserid;
SDL_hapticlist_item *item;
XINPUT_VIBRATION state;
if ((!loaded_xinput) || (dwUserid >= XUSER_MAX_COUNT)) {
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 */
}
}
SDL_zero(state);
if (XINPUTSETSTATE(dwUserid, &state) != ERROR_SUCCESS) {
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 = SDL_TRUE;
item->userid = userid;
return SDL_SYS_AddHapticDevice(item);
}
int
SDL_XINPUT_MaybeRemoveDevice(const DWORD dwUserid)
{
const Uint8 userid = (Uint8)dwUserid;
SDL_hapticlist_item *item;
SDL_hapticlist_item *prev = NULL;
if ((!loaded_xinput) || (dwUserid >= XUSER_MAX_COUNT)) {
return -1;
}
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if (item->bXInputHaptic && item->userid == userid) {
/* found it, remove it. */
return SDL_SYS_RemoveHapticDevice(prev, item);
}
prev = item;
}
return -1;
}
/* !!! FIXME: this is a hack, remove this later. */
/* Since XInput doesn't offer a way to vibrate for X time, we hook into
* SDL_PumpEvents() to check if it's time to stop vibrating with some
* frequency.
* In practice, this works for 99% of use cases. But in an ideal world,
* we do this in a separate thread so that:
* - we aren't bound to when the app chooses to pump the event queue.
* - we aren't adding more polling to the event queue
* - we can emulate all the haptic effects correctly (start on a delay,
* mix multiple effects, etc).
*
* Mostly, this is here to get rumbling to work, and all the other features
* are absent in the XInput path for now. :(
*/
static int SDLCALL
SDL_RunXInputHaptic(void *arg)
{
struct haptic_hwdata *hwdata = (struct haptic_hwdata *) arg;
Jan 3, 2016
Jan 3, 2016
149
while (!SDL_AtomicGet(&hwdata->stopThread)) {
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
206
207
SDL_Delay(50);
SDL_LockMutex(hwdata->mutex);
/* If we're currently running and need to stop... */
if (hwdata->stopTicks) {
if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && SDL_TICKS_PASSED(SDL_GetTicks(), hwdata->stopTicks)) {
XINPUT_VIBRATION vibration = { 0, 0 };
hwdata->stopTicks = 0;
XINPUTSETSTATE(hwdata->userid, &vibration);
}
}
SDL_UnlockMutex(hwdata->mutex);
}
return 0;
}
static int
SDL_XINPUT_HapticOpenFromUserIndex(SDL_Haptic *haptic, const Uint8 userid)
{
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);
Apr 12, 2016
Apr 12, 2016
208
haptic->hwdata->thread = SDL_CreateThreadInternal(SDL_RunXInputHaptic, threadName, 64 * 1024, haptic->hwdata);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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;
}
int
SDL_XINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
{
return SDL_XINPUT_HapticOpenFromUserIndex(haptic, item->userid);
}
int
SDL_XINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
return (haptic->hwdata->userid == joystick->hwdata->userid);
}
int
SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
SDL_hapticlist_item *item;
int index = 0;
/* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if (item->bXInputHaptic && item->userid == joystick->hwdata->userid) {
haptic->index = index;
return SDL_XINPUT_HapticOpenFromUserIndex(haptic, joystick->hwdata->userid);
}
++index;
}
SDL_SetError("Couldn't find joystick in haptic device list");
return -1;
}
void
SDL_XINPUT_HapticClose(SDL_Haptic * haptic)
{
Jan 3, 2016
Jan 3, 2016
255
SDL_AtomicSet(&haptic->hwdata->stopThread, 1);
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
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
413
414
415
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
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
476
477
478
479
480
481
482
483
484
485
486
SDL_WaitThread(haptic->hwdata->thread, NULL);
SDL_DestroyMutex(haptic->hwdata->mutex);
}
void
SDL_XINPUT_HapticQuit(void)
{
if (loaded_xinput) {
WIN_UnloadXInputDLL();
loaded_xinput = SDL_FALSE;
}
}
int
SDL_XINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
{
SDL_assert(base->type == SDL_HAPTIC_LEFTRIGHT); /* should catch this at higher level */
return SDL_XINPUT_HapticUpdateEffect(haptic, effect, base);
}
int
SDL_XINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
{
XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
SDL_assert(data->type == SDL_HAPTIC_LEFTRIGHT);
vib->wLeftMotorSpeed = data->leftright.large_magnitude;
vib->wRightMotorSpeed = data->leftright.small_magnitude;
SDL_LockMutex(haptic->hwdata->mutex);
if (haptic->hwdata->stopTicks) { /* running right now? Update it. */
XINPUTSETSTATE(haptic->hwdata->userid, vib);
}
SDL_UnlockMutex(haptic->hwdata->mutex);
return 0;
}
int
SDL_XINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
{
XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
SDL_assert(effect->effect.type == SDL_HAPTIC_LEFTRIGHT); /* should catch this at higher level */
SDL_LockMutex(haptic->hwdata->mutex);
if (effect->effect.leftright.length == SDL_HAPTIC_INFINITY || iterations == SDL_HAPTIC_INFINITY) {
haptic->hwdata->stopTicks = SDL_HAPTIC_INFINITY;
} else if ((!effect->effect.leftright.length) || (!iterations)) {
/* do nothing. Effect runs for zero milliseconds. */
} else {
haptic->hwdata->stopTicks = SDL_GetTicks() + (effect->effect.leftright.length * iterations);
if ((haptic->hwdata->stopTicks == SDL_HAPTIC_INFINITY) || (haptic->hwdata->stopTicks == 0)) {
haptic->hwdata->stopTicks = 1; /* fix edge cases. */
}
}
SDL_UnlockMutex(haptic->hwdata->mutex);
return (XINPUTSETSTATE(haptic->hwdata->userid, vib) == ERROR_SUCCESS) ? 0 : -1;
}
int
SDL_XINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
XINPUT_VIBRATION vibration = { 0, 0 };
SDL_LockMutex(haptic->hwdata->mutex);
haptic->hwdata->stopTicks = 0;
SDL_UnlockMutex(haptic->hwdata->mutex);
return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
}
void
SDL_XINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
SDL_XINPUT_HapticStopEffect(haptic, effect);
}
int
SDL_XINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticPause(SDL_Haptic * haptic)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticUnpause(SDL_Haptic * haptic)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticStopAll(SDL_Haptic * haptic)
{
XINPUT_VIBRATION vibration = { 0, 0 };
SDL_LockMutex(haptic->hwdata->mutex);
haptic->hwdata->stopTicks = 0;
SDL_UnlockMutex(haptic->hwdata->mutex);
return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
}
#else /* !SDL_HAPTIC_XINPUT */
#include "../../core/windows/SDL_windows.h"
typedef struct SDL_hapticlist_item SDL_hapticlist_item;
int
SDL_XINPUT_HapticInit(void)
{
return 0;
}
int
SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_MaybeRemoveDevice(const DWORD dwUserid)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
return SDL_Unsupported();
}
void
SDL_XINPUT_HapticClose(SDL_Haptic * haptic)
{
}
void
SDL_XINPUT_HapticQuit(void)
{
}
int
SDL_XINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
return SDL_Unsupported();
}
void
SDL_XINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
}
int
SDL_XINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticPause(SDL_Haptic * haptic)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticUnpause(SDL_Haptic * haptic)
{
return SDL_Unsupported();
}
int
SDL_XINPUT_HapticStopAll(SDL_Haptic * haptic)
{
return SDL_Unsupported();
}
#endif /* SDL_HAPTIC_XINPUT */
/* vi: set ts=4 sw=4 expandtab: */