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

Commit

Permalink
Simple rumble API for haptic
Browse files Browse the repository at this point in the history
 Edgar Simo      2011-02-20 10:27:52 PST

Adding patch that adds a simplified API for the haptic subsystem built ontop of
the "real one" for those who want simple rumble without jumping through hoops.

Adds 4 functions:

- extern DECLSPEC int SDLCALL SDL_HapticRumbleSupported(SDL_Haptic * haptic);
- extern DECLSPEC int SDLCALL SDL_HapticRumbleInit(SDL_Haptic * haptic);
- extern DECLSPEC int SDLCALL SDL_HapticRumblePlay(SDL_Haptic * haptic, float
strength, Uint32 length );
- extern DECLSPEC int SDLCALL SDL_HapticRumbleStop(SDL_Haptic * haptic);

Also provided is test/testrumble.c which does test this.

This has all been tested on linux and has worked, but due to being built ontop
of the other haptic API it should work on all OS the same.
  • Loading branch information
slouken committed Feb 20, 2011
1 parent 45693ea commit 7ae31b0
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 3 deletions.
80 changes: 77 additions & 3 deletions include/SDL_haptic.h
Expand Up @@ -25,7 +25,7 @@
*
* \brief The SDL Haptic subsystem allows you to control haptic (force feedback)
* devices.
*
*
* The basic usage is as follows:
* - Initialize the Subsystem (::SDL_INIT_HAPTIC).
* - Open a Haptic Device.
Expand All @@ -37,7 +37,29 @@
* - (optional) Free the effect with SDL_HapticDestroyEffect().
* - Close the haptic device with SDL_HapticClose().
*
* \par Example:
* \par Simple rumble example:
* \code
* SDL_Haptic *haptic;
*
* // Open the device
* haptic = SDL_HapticOpen( 0 );
* if (haptic == NULL)
* return -1;
*
* // Initialize simple rumble
* if (SDL_HapticRumbleInit( haptic ) != 0)
* return -1;
*
* // Play effect at 50% strength for 2 seconds
* if (SDL_HapticRumblePlay( haptic, 0.5, 2000 ) != 0)
* return -1;
* SDL_Delay( 2000 );
*
* // Clean up
* SDL_HapticClose( haptic );
* \endcode
*
* \par Complete example:
* \code
* int test_haptic( SDL_Joystick * joystick ) {
* SDL_Haptic *haptic;
Expand Down Expand Up @@ -933,7 +955,7 @@ extern DECLSPEC int SDLCALL SDL_HapticNumAxes(SDL_Haptic * haptic);
*
* \param haptic Haptic device to check on.
* \param effect Effect to check to see if it is supported.
* \return 1 if effect is supported, 0 if it isn't or -1 on error.
* \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
*
* \sa SDL_HapticQuery
* \sa SDL_HapticNewEffect
Expand Down Expand Up @@ -1113,6 +1135,58 @@ extern DECLSPEC int SDLCALL SDL_HapticUnpause(SDL_Haptic * haptic);
*/
extern DECLSPEC int SDLCALL SDL_HapticStopAll(SDL_Haptic * haptic);

/**
* \brief Checks to see if rumble is supported on a haptic device..
*
* \param haptic Haptic device to check to see if it supports rumble.
* \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
*
* \sa SDL_HapticRumbleInit
* \sa SDL_HapticRumblePlay
* \sa SDL_HapticRumbleStop
*/
extern DECLSPEC int SDLCALL SDL_HapticRumbleSupported(SDL_Haptic * haptic);

/**
* \brief Initializes the haptic device for simple rumble playback.
*
* \param haptic Haptic device to initialize for simple rumble playback.
* \return 0 on success or -1 on error.
*
* \sa SDL_HapticOpen
* \sa SDL_HapticRumbleSupported
* \sa SDL_HapticRumblePlay
* \sa SDL_HapticRumbleStop
*/
extern DECLSPEC int SDLCALL SDL_HapticRumbleInit(SDL_Haptic * haptic);

/**
* \brief Runs simple rumble on a haptic device
*
* \param haptic Haptic device to play rumble effect on.
* \param strength Strength of the rumble to play as a 0-1 float value.
* \param length Length of the rumble to play in miliseconds.
* \return 0 on success or -1 on error.
*
* \sa SDL_HapticRumbleSupported
* \sa SDL_HapticRumbleInit
* \sa SDL_HapticRumbleStop
*/
extern DECLSPEC int SDLCALL SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length );

/**
* \brief Stops the simple rumble on a haptic device.
*
* \param haptic Haptic to stop the rumble on.
* \return 0 on success or -1 on error.
*
* \sa SDL_HapticRumbleSupported
* \sa SDL_HapticRumbleInit
* \sa SDL_HapticRumblePlay
*/
extern DECLSPEC int SDLCALL SDL_HapticRumbleStop(SDL_Haptic * haptic);



/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down
109 changes: 109 additions & 0 deletions src/haptic/SDL_haptic.c
Expand Up @@ -143,6 +143,7 @@ SDL_HapticOpen(int device_index)

/* Initialize the haptic device */
SDL_memset(haptic, 0, (sizeof *haptic));
haptic->rumble_id = -1;
haptic->index = device_index;
if (SDL_SYS_HapticOpen(haptic) < 0) {
SDL_free(haptic);
Expand Down Expand Up @@ -292,6 +293,7 @@ SDL_HapticOpenFromJoystick(SDL_Joystick * joystick)

/* Initialize the haptic device */
SDL_memset(haptic, 0, sizeof(SDL_Haptic));
haptic->rumble_id = -1;
if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
SDL_free(haptic);
return NULL;
Expand Down Expand Up @@ -706,3 +708,110 @@ SDL_HapticStopAll(SDL_Haptic * haptic)

return SDL_SYS_HapticStopAll(haptic);
}

static int
SDL_HapticRumbleCreate(SDL_HapticEffect * efx)
{
SDL_memset(efx, 0, sizeof(SDL_HapticEffect));
efx->type = SDL_HAPTIC_SINE;
efx->periodic.period = 1000;
efx->periodic.magnitude = 0x4000;
efx->periodic.length = 5000;
efx->periodic.attack_length = 0;
efx->periodic.fade_length = 0;
}

/*
* Checks to see if rumble is supported.
*/
int
SDL_HapticRumbleSupported(SDL_Haptic * haptic)
{
SDL_HapticEffect efx;

if (!ValidHaptic(haptic)) {
return -1;
}

SDL_HapticRumbleCreate(&efx);
return SDL_HapticEffectSupported(haptic, &efx);
}

/*
* Initializes the haptic device for simple rumble playback.
*/
int
SDL_HapticRumbleInit(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}

/* Already allocated. */
if (haptic->rumble_id >= 0) {
return 0;
}

/* Copy over. */
SDL_HapticRumbleCreate(&haptic->rumble_effect);
haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
if (haptic->rumble_id >= 0) {
return 0;
}
return -1;
}

/*
* Runs simple rumble on a haptic device
*/
int
SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
{
int ret;
SDL_HapticPeriodic *efx;

if (!ValidHaptic(haptic)) {
return -1;
}

if (haptic->rumble_id < 0) {
SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
return -1;
}

/* Clamp strength. */
if (strength > 1.0f) {
strength = 1.0f;
}
else if (strength < 0.0f) {
strength = 0.0f;
}

/* New effect. */
efx = &haptic->rumble_effect.periodic;
efx->magnitude = (Sint16)(32767.0f*strength);
efx->length = length;
ret = SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect);

return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
}

/*
* Stops the simple rumble on a haptic device.
*/
int
SDL_HapticRumbleStop(SDL_Haptic * haptic)
{
if (!ValidHaptic(haptic)) {
return -1;
}

if (haptic->rumble_id < 0) {
SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
return -1;
}

return SDL_HapticStopEffect(haptic, haptic->rumble_id);
}


3 changes: 3 additions & 0 deletions src/haptic/SDL_syshaptic.h
Expand Up @@ -52,6 +52,9 @@ struct _SDL_Haptic

struct haptic_hwdata *hwdata; /* Driver dependent */
int ref_count; /* Count for multiple opens */

int rumble_id; /* ID of rumble effect for simple rumble API. */
SDL_HapticEffect rumble_effect; /* Rumble effect. */
};

/*
Expand Down
4 changes: 4 additions & 0 deletions test/Makefile.in
Expand Up @@ -28,6 +28,7 @@ TARGETS = \
testgl2$(EXE) \
testgles$(EXE) \
testhaptic$(EXE) \
testrumble$(EXE) \
testhread$(EXE) \
testiconv$(EXE) \
testime$(EXE) \
Expand Down Expand Up @@ -133,6 +134,9 @@ testgles$(EXE): $(srcdir)/testgles.c $(srcdir)/common.c
testhaptic$(EXE): $(srcdir)/testhaptic.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

testrumble$(EXE): $(srcdir)/testrumble.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

testhread$(EXE): $(srcdir)/testhread.c
$(CC) -o $@ $? $(CFLAGS) $(LIBS)

Expand Down
133 changes: 133 additions & 0 deletions test/testrumble.c
@@ -0,0 +1,133 @@
/*
Copyright (c) 2011, Edgar Simo Serra
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* includes
*/
#include <stdlib.h>
#include "SDL.h"
#include "SDL_haptic.h"

#include <stdio.h> /* printf */
#include <string.h> /* strstr */
#include <ctype.h> /* isdigit */


static SDL_Haptic *haptic;


/**
* @brief The entry point of this force feedback demo.
* @param[in] argc Number of arguments.
* @param[in] argv Array of argc arguments.
*/
int
main(int argc, char **argv)
{
int i;
char *name;
int index;
SDL_HapticEffect efx[5];
int id[5];
int nefx;
unsigned int supported;

name = NULL;
index = -1;
if (argc > 1) {
name = argv[1];
if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
printf("USAGE: %s [device]\n"
"If device is a two-digit number it'll use it as an index, otherwise\n"
"it'll use it as if it were part of the device's name.\n",
argv[0]);
return 0;
}

i = strlen(name);
if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
index = atoi(name);
name = NULL;
}
}

/* Initialize the force feedbackness */
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
SDL_INIT_HAPTIC);
printf("%d Haptic devices detected.\n", SDL_NumHaptics());
if (SDL_NumHaptics() > 0) {
/* We'll just use index or the first force feedback device found */
if (name == NULL) {
i = (index != -1) ? index : 0;
}
/* Try to find matching device */
else {
for (i = 0; i < SDL_NumHaptics(); i++) {
if (strstr(SDL_HapticName(i), name) != NULL)
break;
}

if (i >= SDL_NumHaptics()) {
printf("Unable to find device matching '%s', aborting.\n",
name);
return 1;
}
}

haptic = SDL_HapticOpen(i);
if (haptic == NULL) {
printf("Unable to create the haptic device: %s\n",
SDL_GetError());
return 1;
}
printf("Device: %s\n", SDL_HapticName(i));
} else {
printf("No Haptic devices found!\n");
return 1;
}

/* We only want force feedback errors. */
SDL_ClearError();

if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
printf("\nRumble not supported!\n");
return 1;
}
if (SDL_HapticRumbleInit(haptic) != 0) {
printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
return 1;
}
printf("Playing 2 second rumble at 0.5 magnitude.\n");
if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
printf("\nFailed to play rumble: %s\n", SDL_GetError() );
return 1;
}
SDL_Delay(2000);
printf("Stopping rumble.\n");
SDL_HapticRumbleStop(haptic);
SDL_Delay(2000);
printf("Playing 2 second rumble at 0.3 magnitude.\n");
if (SDL_HapticRumblePlay(haptic, 0.3, 5000) != 0) {
printf("\nFailed to play rumble: %s\n", SDL_GetError() );
return 1;
}
SDL_Delay(2000);

/* Quit */
if (haptic != NULL)
SDL_HapticClose(haptic);
SDL_Quit();

return 0;
}

0 comments on commit 7ae31b0

Please sign in to comment.