Split the XInput and DirectInput code so Windows RT can use the existing XInput support.

--HG--
rename : src/audio/directsound/directx.h => src/core/windows/SDL_directx.h
rename : src/haptic/windows/SDL_syshaptic.c => src/haptic/windows/SDL_windowshaptic.c
rename : src/haptic/windows/SDL_syshaptic_c.h => src/haptic/windows/SDL_windowshaptic_c.h
rename : src/joystick/windows/SDL_dxjoystick.c => src/joystick/windows/SDL_dinputjoystick.c
rename : src/joystick/windows/SDL_dxjoystick_c.h => src/joystick/windows/SDL_dinputjoystick_c.h
rename : src/joystick/windows/SDL_dxjoystick_c.h => src/joystick/windows/SDL_windowsjoystick_c.h
This commit is contained in:
Sam Lantinga 2014-07-03 15:39:55 -07:00
parent 8540c8da93
commit 9cdeec59f3
34 changed files with 3982 additions and 3171 deletions

View file

@ -0,0 +1,47 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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_haptic.h"
#include "SDL_windowshaptic_c.h"
extern int SDL_DINPUT_HapticInit(void);
extern int SDL_DINPUT_MaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance);
extern int SDL_DINPUT_MaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance);
extern int SDL_DINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item);
extern int SDL_DINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick);
extern int SDL_DINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick);
extern void SDL_DINPUT_HapticClose(SDL_Haptic * haptic);
extern void SDL_DINPUT_HapticQuit(void);
extern int SDL_DINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base);
extern int SDL_DINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data);
extern int SDL_DINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations);
extern int SDL_DINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect);
extern void SDL_DINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect);
extern int SDL_DINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect);
extern int SDL_DINPUT_HapticSetGain(SDL_Haptic * haptic, int gain);
extern int SDL_DINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
extern int SDL_DINPUT_HapticPause(SDL_Haptic * haptic);
extern int SDL_DINPUT_HapticUnpause(SDL_Haptic * haptic);
extern int SDL_DINPUT_HapticStopAll(SDL_Haptic * haptic);
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -1,28 +0,0 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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.
*/
extern int DirectInputHaptic_MaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance);
extern int DirectInputHaptic_MaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance);
extern int XInputHaptic_MaybeAddDevice(const DWORD dwUserid);
extern int XInputHaptic_MaybeRemoveDevice(const DWORD dwUserid);
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -0,0 +1,445 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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"
#if SDL_HAPTIC_DINPUT || SDL_HAPTIC_XINPUT
#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_windowsjoystick_c.h" /* For joystick hwdata */
#include "../../joystick/windows/SDL_xinputjoystick_c.h" /* For xinput rumble */
#include "SDL_windowshaptic_c.h"
#include "SDL_dinputhaptic_c.h"
#include "SDL_xinputhaptic_c.h"
/*
* Internal stuff.
*/
SDL_hapticlist_item *SDL_hapticlist = NULL;
static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
static int numhaptics = 0;
/*
* Initializes the haptic subsystem.
*/
int
SDL_SYS_HapticInit(void)
{
if (SDL_DINPUT_HapticInit() < 0) {
return -1;
}
if (SDL_XINPUT_HapticInit() < 0) {
return -1;
}
return numhaptics;
}
int
SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item)
{
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
SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item)
{
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;
}
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);
--device_index;
item = item->next;
}
return item;
}
/*
* Return the name of a haptic device, does not need to be opened.
*/
const char *
SDL_SYS_HapticName(int index)
{
SDL_hapticlist_item *item = HapticByDevIndex(index);
return item->name;
}
/*
* Opens a haptic device for usage.
*/
int
SDL_SYS_HapticOpen(SDL_Haptic * haptic)
{
SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
if (item->bXInputHaptic) {
return SDL_XINPUT_HapticOpen(haptic, item);
} else {
return SDL_DINPUT_HapticOpen(haptic, item);
}
}
/*
* Opens a haptic device from first mouse it finds for usage.
*/
int
SDL_SYS_HapticMouse(void)
{
#if SDL_HAPTIC_DINPUT
SDL_hapticlist_item *item;
int index = 0;
/* Grab the first mouse haptic device we find. */
for (item = SDL_hapticlist; item != NULL; item = item->next) {
if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
return index;
}
++index;
}
#endif /* SDL_HAPTIC_DINPUT */
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;
#if SDL_HAPTIC_XINPUT
if (hwdata->bXInputHaptic) {
return 1;
}
#endif
#if SDL_HAPTIC_DINPUT
if (hwdata->Capabilities.dwFlags & DIDC_FORCEFEEDBACK) {
return 1;
}
#endif
return 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) {
return SDL_XINPUT_JoystickSameHaptic(haptic, joystick);
} else {
return SDL_DINPUT_JoystickSameHaptic(haptic, joystick);
}
}
/*
* Opens a SDL_Haptic from a SDL_Joystick.
*/
int
SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
{
if (joystick->hwdata->bXInputDevice) {
return SDL_XINPUT_HapticOpenFromJoystick(haptic, joystick);
} else {
return SDL_DINPUT_HapticOpenFromJoystick(haptic, joystick);
}
}
/*
* 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) {
SDL_XINPUT_HapticClose(haptic);
} else {
SDL_DINPUT_HapticClose(haptic);
}
/* Free */
SDL_free(haptic->hwdata);
haptic->hwdata = NULL;
}
}
/*
* Clean up after system specific haptic stuff
*/
void
SDL_SYS_HapticQuit(void)
{
SDL_hapticlist_item *item;
SDL_hapticlist_item *next = NULL;
SDL_Haptic *hapticitem = NULL;
extern SDL_Haptic *SDL_haptics;
for (hapticitem = SDL_haptics; hapticitem; hapticitem = hapticitem->next) {
if ((hapticitem->hwdata->bXInputHaptic) && (hapticitem->hwdata->thread)) {
/* we _have_ to stop the thread before we free the XInput DLL! */
hapticitem->hwdata->stopThread = 1;
SDL_WaitThread(hapticitem->hwdata->thread, NULL);
hapticitem->hwdata->thread = NULL;
}
}
for (item = SDL_hapticlist; item; item = next) {
/* Opened and not closed haptics are leaked, this is on purpose.
* Close your haptic devices after usage. */
/* !!! FIXME: (...is leaking on purpose a good idea?) - No, of course not. */
next = item->next;
SDL_free(item->name);
SDL_free(item);
}
SDL_XINPUT_HapticQuit();
SDL_DINPUT_HapticQuit();
}
/*
* Creates a new haptic effect.
*/
int
SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
SDL_HapticEffect * base)
{
int result;
/* Alloc the effect. */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
if (effect->hweffect == NULL) {
SDL_OutOfMemory();
return -1;
}
SDL_zerop(effect->hweffect);
if (haptic->hwdata->bXInputHaptic) {
result = SDL_XINPUT_HapticNewEffect(haptic, effect, base);
} else {
result = SDL_DINPUT_HapticNewEffect(haptic, effect, base);
}
if (result < 0) {
SDL_free(effect->hweffect);
effect->hweffect = NULL;
}
return result;
}
/*
* Updates an effect.
*/
int
SDL_SYS_HapticUpdateEffect(SDL_Haptic * haptic,
struct haptic_effect *effect,
SDL_HapticEffect * data)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticUpdateEffect(haptic, effect, data);
} else {
return SDL_DINPUT_HapticUpdateEffect(haptic, effect, data);
}
}
/*
* Runs an effect.
*/
int
SDL_SYS_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect,
Uint32 iterations)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticRunEffect(haptic, effect, iterations);
} else {
return SDL_DINPUT_HapticRunEffect(haptic, effect, iterations);
}
}
/*
* Stops an effect.
*/
int
SDL_SYS_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticStopEffect(haptic, effect);
} else {
return SDL_DINPUT_HapticStopEffect(haptic, effect);
}
}
/*
* Frees the effect.
*/
void
SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect)
{
if (haptic->hwdata->bXInputHaptic) {
SDL_XINPUT_HapticDestroyEffect(haptic, effect);
} else {
SDL_DINPUT_HapticDestroyEffect(haptic, effect);
}
SDL_free(effect->hweffect);
effect->hweffect = NULL;
}
/*
* Gets the status of a haptic effect.
*/
int
SDL_SYS_HapticGetEffectStatus(SDL_Haptic * haptic,
struct haptic_effect *effect)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticGetEffectStatus(haptic, effect);
} else {
return SDL_DINPUT_HapticGetEffectStatus(haptic, effect);
}
}
/*
* Sets the gain.
*/
int
SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticSetGain(haptic, gain);
} else {
return SDL_DINPUT_HapticSetGain(haptic, gain);
}
}
/*
* Sets the autocentering.
*/
int
SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticSetAutocenter(haptic, autocenter);
} else {
return SDL_DINPUT_HapticSetAutocenter(haptic, autocenter);
}
}
/*
* Pauses the device.
*/
int
SDL_SYS_HapticPause(SDL_Haptic * haptic)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticPause(haptic);
} else {
return SDL_DINPUT_HapticPause(haptic);
}
}
/*
* Pauses the device.
*/
int
SDL_SYS_HapticUnpause(SDL_Haptic * haptic)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticUnpause(haptic);
} else {
return SDL_DINPUT_HapticUnpause(haptic);
}
}
/*
* Stops all the playing effects on the device.
*/
int
SDL_SYS_HapticStopAll(SDL_Haptic * haptic)
{
if (haptic->hwdata->bXInputHaptic) {
return SDL_XINPUT_HapticStopAll(haptic);
} else {
return SDL_DINPUT_HapticStopAll(haptic);
}
}
#endif /* SDL_HAPTIC_DINPUT || SDL_HAPTIC_XINPUT */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -0,0 +1,88 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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"
#ifndef _SDL_windowshaptic_c_h
#define _SDL_windowshaptic_c_h
#include "SDL_thread.h"
#include "../../core/windows/SDL_directx.h"
#include "../../core/windows/SDL_xinput.h"
/*
* Haptic system hardware data.
*/
struct haptic_hwdata
{
#if SDL_HAPTIC_DINPUT
LPDIRECTINPUTDEVICE8 device;
#endif
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
{
#if SDL_HAPTIC_DINPUT
DIEFFECT effect;
LPDIRECTINPUTEFFECT ref;
#endif
#if SDL_HAPTIC_XINPUT
XINPUT_VIBRATION vibration;
#endif
};
/*
* List of available haptic devices.
*/
typedef struct SDL_hapticlist_item
{
char *name;
SDL_Haptic *haptic;
#if SDL_HAPTIC_DINPUT
DIDEVICEINSTANCE instance;
DIDEVCAPS capabilities;
#endif
SDL_bool bXInputHaptic; /* Supports force feedback via XInput. */
Uint8 userid; /* XInput userid index for this joystick */
struct SDL_hapticlist_item *next;
} SDL_hapticlist_item;
extern SDL_hapticlist_item *SDL_hapticlist;
extern int SDL_SYS_AddHapticDevice(SDL_hapticlist_item *item);
extern int SDL_SYS_RemoveHapticDevice(SDL_hapticlist_item *prev, SDL_hapticlist_item *item);
#endif /* _SDL_windowshaptic_c_h */
/* vi: set ts=4 sw=4 expandtab: */

View file

@ -0,0 +1,491 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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_assert.h"
#include "SDL_error.h"
#include "SDL_haptic.h"
#include "SDL_hints.h"
#include "SDL_timer.h"
#include "SDL_windowshaptic_c.h"
#include "SDL_xinputhaptic_c.h"
#include "../SDL_syshaptic.h"
#include "../../core/windows/SDL_xinput.h"
#include "../../joystick/windows/SDL_windowsjoystick_c.h"
#if SDL_HAPTIC_XINPUT
/*
* Internal stuff.
*/
static SDL_bool loaded_xinput = SDL_FALSE;
int
SDL_XINPUT_HapticInit(void)
{
const char *env = SDL_GetHint(SDL_HINT_XINPUT_ENABLED);
if (!env || SDL_atoi(env)) {
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;
while (!hwdata->stopThread) {
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);
#if defined(__WIN32__) && !defined(HAVE_LIBC) /* !!! FIXME: this is nasty. */
#undef SDL_CreateThread
#if SDL_DYNAMIC_API
haptic->hwdata->thread = SDL_CreateThread_REAL(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
#else
haptic->hwdata->thread = SDL_CreateThread(SDL_RunXInputHaptic, threadName, haptic->hwdata, NULL, NULL);
#endif
#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;
}
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)
{
haptic->hwdata->stopThread = 1;
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 */
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: */

View file

@ -0,0 +1,47 @@
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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_haptic.h"
#include "SDL_windowshaptic_c.h"
extern int SDL_XINPUT_HapticInit(void);
extern int SDL_XINPUT_MaybeAddDevice(const DWORD dwUserid);
extern int SDL_XINPUT_MaybeRemoveDevice(const DWORD dwUserid);
extern int SDL_XINPUT_HapticOpen(SDL_Haptic * haptic, SDL_hapticlist_item *item);
extern int SDL_XINPUT_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick);
extern int SDL_XINPUT_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick);
extern void SDL_XINPUT_HapticClose(SDL_Haptic * haptic);
extern void SDL_XINPUT_HapticQuit(void);
extern int SDL_XINPUT_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * base);
extern int SDL_XINPUT_HapticUpdateEffect(SDL_Haptic * haptic, struct haptic_effect *effect, SDL_HapticEffect * data);
extern int SDL_XINPUT_HapticRunEffect(SDL_Haptic * haptic, struct haptic_effect *effect, Uint32 iterations);
extern int SDL_XINPUT_HapticStopEffect(SDL_Haptic * haptic, struct haptic_effect *effect);
extern void SDL_XINPUT_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect *effect);
extern int SDL_XINPUT_HapticGetEffectStatus(SDL_Haptic * haptic, struct haptic_effect *effect);
extern int SDL_XINPUT_HapticSetGain(SDL_Haptic * haptic, int gain);
extern int SDL_XINPUT_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter);
extern int SDL_XINPUT_HapticPause(SDL_Haptic * haptic);
extern int SDL_XINPUT_HapticUnpause(SDL_Haptic * haptic);
extern int SDL_XINPUT_HapticStopAll(SDL_Haptic * haptic);
/* vi: set ts=4 sw=4 expandtab: */