From eeee6d85590541d0c7b855d3d097301bfe1a1f06 Mon Sep 17 00:00:00 2001 From: Edgar Simo Date: Sun, 24 Aug 2008 17:17:45 +0000 Subject: [PATCH] Added support for pausing/unpausing haptic devices. --- include/SDL_haptic.h | 41 +++++++++++++++++++++++++++++++ src/haptic/SDL_haptic.c | 34 +++++++++++++++++++++++++ src/haptic/SDL_syshaptic.h | 13 ++++++++++ src/haptic/darwin/SDL_syshaptic.c | 39 ++++++++++++++++++++++++++++- src/haptic/dummy/SDL_syshaptic.c | 14 +++++++++++ src/haptic/linux/SDL_syshaptic.c | 20 +++++++++++++++ src/haptic/win32/SDL_syshaptic.c | 41 ++++++++++++++++++++++++++++++- 7 files changed, 200 insertions(+), 2 deletions(-) diff --git a/include/SDL_haptic.h b/include/SDL_haptic.h index 8f72b5cd3..7c7c81712 100644 --- a/include/SDL_haptic.h +++ b/include/SDL_haptic.h @@ -240,6 +240,15 @@ typedef struct _SDL_Haptic SDL_Haptic; * \sa SDL_HapticGetEffectStatus */ #define SDL_HAPTIC_STATUS (1<<14) /* Device can be queried for effect status */ +/** + * \def SDL_HAPTIC_PAUSE + * + * \brief Device can be paused. + * + * \sa SDL_HapticPause + * \sa SDL_HapticUnpause + */ +#define SDL_HAPTIC_PAUSE (1<<15) /* Device can be paused. */ /* @@ -1068,6 +1077,38 @@ extern DECLSPEC int SDLCALL SDL_HapticSetGain(SDL_Haptic * haptic, int gain); */ extern DECLSPEC int SDLCALL SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter); +/** + * \fn extern DECLSPEC int SDLCALL SDL_HapticPause(SDL_Haptic * haptic) + * + * \brief Pauses the haptic device. + * + * Device must support the SDL_HAPTIC_PAUSE feature. Call SDL_HapticUnpause + * to resume playback. + * + * Do not modify the effects nor add new ones while the device is paused. + * That can cause all sorts of weird errors. + * + * \param haptic Haptic device to pause. + * \return 0 on success or -1 on error. + * + * \sa SDL_HapticUnpause + */ +extern DECLSPEC int SDLCALL SDL_HapticPause(SDL_Haptic * haptic); + +/** + * \fn extern DECLSPEC int SDLCALL SDL_HapticUnpause(SDL_Haptic * haptic) + * + * \brief Unpauses the haptic device. + * + * Call to unpause after SDL_HapticPause. + * + * \param haptic Haptic device to pause. + * \return 0 on success or -1 on error. + * + * \sa SDL_HapticPause + */ +extern DECLSPEC int SDLCALL SDL_HapticUnpause(SDL_Haptic * haptic); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/src/haptic/SDL_haptic.c b/src/haptic/SDL_haptic.c index 8cca96fca..476b49b0c 100644 --- a/src/haptic/SDL_haptic.c +++ b/src/haptic/SDL_haptic.c @@ -638,4 +638,38 @@ SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter ) return 0; } +/* + * Pauses the haptic device. + */ +int +SDL_HapticPause(SDL_Haptic * haptic) +{ + if (!ValidHaptic(haptic)) { + return -1; + } + + if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) { + SDL_SetError("Haptic: Device does not support setting pausing."); + return -1; + } + + return SDL_SYS_HapticPause(haptic); +} + +/* + * Unpauses the haptic device. + */ +int +SDL_HapticUnpause(SDL_Haptic * haptic) +{ + if (!ValidHaptic(haptic)) { + return -1; + } + + if ((haptic->supported & SDL_HAPTIC_PAUSE) == 0) { + return 0; /* Not going to be paused, so we pretend it's unpaused. */ + } + + return SDL_SYS_HapticUnpause(haptic); +} diff --git a/src/haptic/SDL_syshaptic.h b/src/haptic/SDL_syshaptic.h index 2121f9970..d5a6a4daa 100644 --- a/src/haptic/SDL_syshaptic.h +++ b/src/haptic/SDL_syshaptic.h @@ -180,4 +180,17 @@ extern int SDL_SYS_HapticSetGain(SDL_Haptic * haptic, int gain); extern int SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter); +/* + * Pauses the haptic device. + * + * Returns 0 on success, -1 on error. + */ +extern int SDL_SYS_HapticPause(SDL_Haptic * haptic); + +/* + * Unpauses the haptic device. + * + * Returns 0 on success, -1 on error. + */ +extern int SDL_SYS_HapticUnpause(SDL_Haptic * haptic); diff --git a/src/haptic/darwin/SDL_syshaptic.c b/src/haptic/darwin/SDL_syshaptic.c index 6fe7cf651..0f18c02ae 100644 --- a/src/haptic/darwin/SDL_syshaptic.c +++ b/src/haptic/darwin/SDL_syshaptic.c @@ -376,7 +376,7 @@ GetSupportedFeatures(SDL_Haptic* haptic) SDL_memcpy( haptic->hwdata->axes, features.ffAxes, haptic->naxes * sizeof(Uint8)); /* Always supported features. */ - supported |= SDL_HAPTIC_STATUS; + supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE; haptic->supported = supported; return 0;; @@ -1221,7 +1221,44 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) } return 0; +} + + +/* + * Pauses the device. + */ +int +SDL_SYS_HapticPause(SDL_Haptic * haptic) +{ + HRESULT ret; + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_PAUSE); + if (ret != FF_OK) { + SDL_SetError("Haptic: Error pausing device: %s.", FFStrError(ret)); + return -1; + } + + return 0; +} + + +/* + * Unpauses the device. + */ +int +SDL_SYS_HapticUnpause(SDL_Haptic * haptic) +{ + HRESULT ret; + + ret = FFDeviceSendForceFeedbackCommand(haptic->hwdata->device, + FFSFFC_CONTINUE); + if (ret != FF_OK) { + SDL_SetError("Haptic: Error pausing device: %s.", FFStrError(ret)); + return -1; + } + + return 0; } diff --git a/src/haptic/dummy/SDL_syshaptic.c b/src/haptic/dummy/SDL_syshaptic.c index 81dc4490f..4c5aff9e3 100644 --- a/src/haptic/dummy/SDL_syshaptic.c +++ b/src/haptic/dummy/SDL_syshaptic.c @@ -159,5 +159,19 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) return -1; } +int +SDL_SYS_HapticPause(SDL_Haptic * haptic) +{ + SDL_SetError("Logic error: No haptic devices available."); + return -1; +} + +int +SDL_SYS_HapticUnpause(SDL_Haptic * haptic) +{ + SDL_SetError("Logic error: No haptic devices available."); + return -1; +} + #endif /* SDL_HAPTIC_DUMMY || SDL_HAPTIC_DISABLED */ diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c index 13d90eee5..d2c29a9fd 100644 --- a/src/haptic/linux/SDL_syshaptic.c +++ b/src/haptic/linux/SDL_syshaptic.c @@ -890,4 +890,24 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) } +/* + * Pausing is not supported atm by linux. + */ +int +SDL_SYS_HapticPause(SDL_Haptic * haptic) +{ + return -1; +} + + +/* + * Unpausing is not supported atm by linux. + */ +int +SDL_SYS_HapticUnpause(SDL_Haptic * haptic) +{ + return -1; +} + + #endif /* SDL_HAPTIC_LINUX */ diff --git a/src/haptic/win32/SDL_syshaptic.c b/src/haptic/win32/SDL_syshaptic.c index e796fad2a..6d409dbc6 100644 --- a/src/haptic/win32/SDL_syshaptic.c +++ b/src/haptic/win32/SDL_syshaptic.c @@ -466,7 +466,7 @@ SDL_SYS_HapticOpenFromDevice2(SDL_Haptic * haptic, LPDIRECTINPUTDEVICE2 device2) } /* Status is always supported. */ - haptic->supported |= SDL_HAPTIC_STATUS; + haptic->supported |= SDL_HAPTIC_STATUS | SDL_HAPTIC_PAUSE; /* Check maximum effects. */ haptic->neffects = 128; /* This is not actually supported as thus under windows, @@ -1301,7 +1301,46 @@ SDL_SYS_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter) } return 0; +} + + +/* + * Pauses the device. + */ +int +SDL_SYS_HapticPause(SDL_Haptic * haptic) +{ + HRESULT ret; + /* Pause the device. */ + ret = IDirectInputDevice2_SendForceFeedbackCommand( haptic->hwdata->device, + DISFFC_PAUSE ); + if (FAILED(ret)) { + DI_SetError("Pausing the device",ret); + return -1; + } + + return 0; +} + + +/* + * Pauses the device. + */ +int +SDL_SYS_HapticUnpause(SDL_Haptic * haptic) +{ + HRESULT ret; + + /* Unpause the device. */ + ret = IDirectInputDevice2_SendForceFeedbackCommand( haptic->hwdata->device, + DISFFC_CONTINUE ); + if (FAILED(ret)) { + DI_SetError("Pausing the device",ret); + return -1; + } + + return 0; }