2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #include "SDL_error.h"
24 #include "SDL_haptic.h"
25 #include "../SDL_syshaptic.h"
29 #include "SDL_assert.h"
30 #include "SDL_hints.h"
31 #include "SDL_timer.h"
32 #include "SDL_windowshaptic_c.h"
33 #include "SDL_xinputhaptic_c.h"
34 #include "../../core/windows/SDL_xinput.h"
35 #include "../../joystick/windows/SDL_windowsjoystick_c.h"
36 #include "../../thread/SDL_systhread.h"
41 static SDL_bool loaded_xinput = SDL_FALSE;
45 SDL_XINPUT_HapticInit(void)
47 if (SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE)) {
48 loaded_xinput = (WIN_LoadXInputDLL() == 0);
53 for (i = 0; i < XUSER_MAX_COUNT; i++) {
54 SDL_XINPUT_MaybeAddDevice(i);
61 SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid)
63 const Uint8 userid = (Uint8)dwUserid;
64 SDL_hapticlist_item *item;
65 XINPUT_VIBRATION state;
67 if ((!loaded_xinput) || (dwUserid >= XUSER_MAX_COUNT)) {
71 /* Make sure we don't already have it */
72 for (item = SDL_hapticlist; item; item = item->next) {
73 if (item->bXInputHaptic && item->userid == userid) {
74 return -1; /* Already added */
79 if (XINPUTSETSTATE(dwUserid, &state) != ERROR_SUCCESS) {
80 return -1; /* no force feedback on this device. */
83 item = (SDL_hapticlist_item *)SDL_malloc(sizeof(SDL_hapticlist_item));
85 return SDL_OutOfMemory();
90 /* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */
93 SDL_snprintf(buf, sizeof(buf), "XInput Controller #%u", (unsigned int)(userid + 1));
94 item->name = SDL_strdup(buf);
102 /* Copy the instance over, useful for creating devices. */
103 item->bXInputHaptic = SDL_TRUE;
104 item->userid = userid;
106 return SDL_SYS_AddHapticDevice(item);
110 SDL_XINPUT_MaybeRemoveDevice(const DWORD dwUserid)
112 const Uint8 userid = (Uint8)dwUserid;
113 SDL_hapticlist_item *item;
114 SDL_hapticlist_item *prev = NULL;
116 if ((!loaded_xinput) || (dwUserid >= XUSER_MAX_COUNT)) {
120 for (item = SDL_hapticlist; item != NULL; item = item->next) {
121 if (item->bXInputHaptic && item->userid == userid) {
122 /* found it, remove it. */
123 return SDL_SYS_RemoveHapticDevice(prev, item);
130 /* !!! FIXME: this is a hack, remove this later. */
131 /* Since XInput doesn't offer a way to vibrate for X time, we hook into
132 * SDL_PumpEvents() to check if it's time to stop vibrating with some
134 * In practice, this works for 99% of use cases. But in an ideal world,
135 * we do this in a separate thread so that:
136 * - we aren't bound to when the app chooses to pump the event queue.
137 * - we aren't adding more polling to the event queue
138 * - we can emulate all the haptic effects correctly (start on a delay,
139 * mix multiple effects, etc).
141 * Mostly, this is here to get rumbling to work, and all the other features
142 * are absent in the XInput path for now. :(
145 SDL_RunXInputHaptic(void *arg)
147 struct haptic_hwdata *hwdata = (struct haptic_hwdata *) arg;
149 while (!SDL_AtomicGet(&hwdata->stopThread)) {
151 SDL_LockMutex(hwdata->mutex);
152 /* If we're currently running and need to stop... */
153 if (hwdata->stopTicks) {
154 if ((hwdata->stopTicks != SDL_HAPTIC_INFINITY) && SDL_TICKS_PASSED(SDL_GetTicks(), hwdata->stopTicks)) {
155 XINPUT_VIBRATION vibration = { 0, 0 };
156 hwdata->stopTicks = 0;
157 XINPUTSETSTATE(hwdata->userid, &vibration);
160 SDL_UnlockMutex(hwdata->mutex);
167 SDL_XINPUT_HapticOpenFromUserIndex(SDL_Haptic *haptic, const Uint8 userid)
170 XINPUT_VIBRATION vibration = { 0, 0 }; /* stop any current vibration */
171 XINPUTSETSTATE(userid, &vibration);
173 haptic->supported = SDL_HAPTIC_LEFTRIGHT;
175 haptic->neffects = 1;
176 haptic->nplaying = 1;
178 /* Prepare effects memory. */
179 haptic->effects = (struct haptic_effect *)
180 SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
181 if (haptic->effects == NULL) {
182 return SDL_OutOfMemory();
184 /* Clear the memory */
185 SDL_memset(haptic->effects, 0,
186 sizeof(struct haptic_effect) * haptic->neffects);
188 haptic->hwdata = (struct haptic_hwdata *) SDL_malloc(sizeof(*haptic->hwdata));
189 if (haptic->hwdata == NULL) {
190 SDL_free(haptic->effects);
191 haptic->effects = NULL;
192 return SDL_OutOfMemory();
194 SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
196 haptic->hwdata->bXInputHaptic = 1;
197 haptic->hwdata->userid = userid;
199 haptic->hwdata->mutex = SDL_CreateMutex();
200 if (haptic->hwdata->mutex == NULL) {
201 SDL_free(haptic->effects);
202 SDL_free(haptic->hwdata);
203 haptic->effects = NULL;
204 return SDL_SetError("Couldn't create XInput haptic mutex");
207 SDL_snprintf(threadName, sizeof(threadName), "SDLXInputDev%d", (int)userid);
208 haptic->hwdata->thread = SDL_CreateThreadInternal(SDL_RunXInputHaptic, threadName, 64 * 1024, haptic->hwdata);
210 if (haptic->hwdata->thread == NULL) {
211 SDL_DestroyMutex(haptic->hwdata->mutex);
212 SDL_free(haptic->effects);
213 SDL_free(haptic->hwdata);
214 haptic->effects = NULL;
215 return SDL_SetError("Couldn't create XInput haptic thread");
222 SDL_XINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
224 return SDL_XINPUT_HapticOpenFromUserIndex(haptic, item->userid);
228 SDL_XINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
230 return (haptic->hwdata->userid == joystick->hwdata->userid);
234 SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
236 SDL_hapticlist_item *item;
239 /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
240 for (item = SDL_hapticlist; item != NULL; item = item->next) {
241 if (item->bXInputHaptic && item->userid == joystick->hwdata->userid) {
242 haptic->index = index;
243 return SDL_XINPUT_HapticOpenFromUserIndex(haptic, joystick->hwdata->userid);
248 SDL_SetError("Couldn't find joystick in haptic device list");
253 SDL_XINPUT_HapticClose(SDL_Haptic * haptic)
255 SDL_AtomicSet(&haptic->hwdata->stopThread, 1);
256 SDL_WaitThread(haptic->hwdata->thread, NULL);
257 SDL_DestroyMutex(haptic->hwdata->mutex);
261 SDL_XINPUT_HapticQuit(void)
264 WIN_UnloadXInputDLL();
265 loaded_xinput = SDL_FALSE;
270 SDL_XINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
272 SDL_assert(base->type == SDL_HAPTIC_LEFTRIGHT); /* should catch this at higher level */
273 return SDL_XINPUT_HapticUpdateEffect(haptic, effect, base);
277 SDL_XINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
279 XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
280 SDL_assert(data->type == SDL_HAPTIC_LEFTRIGHT);
281 /* SDL_HapticEffect has max magnitude of 32767, XInput expects 65535 max, so multiply */
282 vib->wLeftMotorSpeed = data->leftright.large_magnitude * 2;
283 vib->wRightMotorSpeed = data->leftright.small_magnitude * 2;
284 SDL_LockMutex(haptic->hwdata->mutex);
285 if (haptic->hwdata->stopTicks) { /* running right now? Update it. */
286 XINPUTSETSTATE(haptic->hwdata->userid, vib);
288 SDL_UnlockMutex(haptic->hwdata->mutex);
293 SDL_XINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
295 XINPUT_VIBRATION *vib = &effect->hweffect->vibration;
296 SDL_assert(effect->effect.type == SDL_HAPTIC_LEFTRIGHT); /* should catch this at higher level */
297 SDL_LockMutex(haptic->hwdata->mutex);
298 if (effect->effect.leftright.length == SDL_HAPTIC_INFINITY || iterations == SDL_HAPTIC_INFINITY) {
299 haptic->hwdata->stopTicks = SDL_HAPTIC_INFINITY;
300 } else if ((!effect->effect.leftright.length) || (!iterations)) {
301 /* do nothing. Effect runs for zero milliseconds. */
303 haptic->hwdata->stopTicks = SDL_GetTicks() + (effect->effect.leftright.length * iterations);
304 if ((haptic->hwdata->stopTicks == SDL_HAPTIC_INFINITY) || (haptic->hwdata->stopTicks == 0)) {
305 haptic->hwdata->stopTicks = 1; /* fix edge cases. */
308 SDL_UnlockMutex(haptic->hwdata->mutex);
309 return (XINPUTSETSTATE(haptic->hwdata->userid, vib) == ERROR_SUCCESS) ? 0 : -1;
313 SDL_XINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
315 XINPUT_VIBRATION vibration = { 0, 0 };
316 SDL_LockMutex(haptic->hwdata->mutex);
317 haptic->hwdata->stopTicks = 0;
318 SDL_UnlockMutex(haptic->hwdata->mutex);
319 return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
323 SDL_XINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
325 SDL_XINPUT_HapticStopEffect(haptic, effect);
329 SDL_XINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
331 return SDL_Unsupported();
335 SDL_XINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
337 return SDL_Unsupported();
341 SDL_XINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
343 return SDL_Unsupported();
347 SDL_XINPUT_HapticPause(SDL_Haptic * haptic)
349 return SDL_Unsupported();
353 SDL_XINPUT_HapticUnpause(SDL_Haptic * haptic)
355 return SDL_Unsupported();
359 SDL_XINPUT_HapticStopAll(SDL_Haptic * haptic)
361 XINPUT_VIBRATION vibration = { 0, 0 };
362 SDL_LockMutex(haptic->hwdata->mutex);
363 haptic->hwdata->stopTicks = 0;
364 SDL_UnlockMutex(haptic->hwdata->mutex);
365 return (XINPUTSETSTATE(haptic->hwdata->userid, &vibration) == ERROR_SUCCESS) ? 0 : -1;
368 #else /* !SDL_HAPTIC_XINPUT */
370 #include "../../core/windows/SDL_windows.h"
372 typedef struct SDL_hapticlist_item SDL_hapticlist_item;
375 SDL_XINPUT_HapticInit(void)
381 SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid)
383 return SDL_Unsupported();
387 SDL_XINPUT_MaybeRemoveDevice(const DWORD dwUserid)
389 return SDL_Unsupported();
393 SDL_XINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item)
395 return SDL_Unsupported();
399 SDL_XINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
401 return SDL_Unsupported();
405 SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
407 return SDL_Unsupported();
411 SDL_XINPUT_HapticClose(SDL_Haptic * haptic)
416 SDL_XINPUT_HapticQuit(void)
421 SDL_XINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base)
423 return SDL_Unsupported();
427 SDL_XINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data)
429 return SDL_Unsupported();
433 SDL_XINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations)
435 return SDL_Unsupported();
439 SDL_XINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
441 return SDL_Unsupported();
445 SDL_XINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
450 SDL_XINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect)
452 return SDL_Unsupported();
456 SDL_XINPUT_HapticSetGain(SDL_Haptic * haptic, int gain)
458 return SDL_Unsupported();
462 SDL_XINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
464 return SDL_Unsupported();
468 SDL_XINPUT_HapticPause(SDL_Haptic * haptic)
470 return SDL_Unsupported();
474 SDL_XINPUT_HapticUnpause(SDL_Haptic * haptic)
476 return SDL_Unsupported();
480 SDL_XINPUT_HapticStopAll(SDL_Haptic * haptic)
482 return SDL_Unsupported();
485 #endif /* SDL_HAPTIC_XINPUT */
487 /* vi: set ts=4 sw=4 expandtab: */