Simple rumble API for haptic
authorSam Lantinga <slouken@libsdl.org>
Sun, 20 Feb 2011 10:54:44 -0800
changeset 5360220d3af9121f
parent 5359 d5fe4ed3a28d
child 5361 1d9e27849ed9
Simple rumble API for haptic

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.
include/SDL_haptic.h
src/haptic/SDL_haptic.c
src/haptic/SDL_syshaptic.h
test/Makefile.in
test/testrumble.c
     1.1 --- a/include/SDL_haptic.h	Sun Feb 20 10:42:51 2011 -0800
     1.2 +++ b/include/SDL_haptic.h	Sun Feb 20 10:54:44 2011 -0800
     1.3 @@ -25,7 +25,7 @@
     1.4   *  
     1.5   *  \brief The SDL Haptic subsystem allows you to control haptic (force feedback)
     1.6   *         devices.
     1.7 - *  
     1.8 + * 
     1.9   *  The basic usage is as follows:
    1.10   *   - Initialize the Subsystem (::SDL_INIT_HAPTIC).
    1.11   *   - Open a Haptic Device.
    1.12 @@ -37,7 +37,29 @@
    1.13   *   - (optional) Free the effect with SDL_HapticDestroyEffect().
    1.14   *   - Close the haptic device with SDL_HapticClose().
    1.15   *
    1.16 - * \par Example:
    1.17 + * \par Simple rumble example:
    1.18 + * \code
    1.19 + *    SDL_Haptic *haptic;
    1.20 + *
    1.21 + *    // Open the device
    1.22 + *    haptic = SDL_HapticOpen( 0 );
    1.23 + *    if (haptic == NULL)
    1.24 + *       return -1;
    1.25 + *
    1.26 + *    // Initialize simple rumble
    1.27 + *    if (SDL_HapticRumbleInit( haptic ) != 0)
    1.28 + *       return -1;
    1.29 + *
    1.30 + *    // Play effect at 50% strength for 2 seconds
    1.31 + *    if (SDL_HapticRumblePlay( haptic, 0.5, 2000 ) != 0)
    1.32 + *       return -1;
    1.33 + *    SDL_Delay( 2000 );
    1.34 + *
    1.35 + *    // Clean up
    1.36 + *    SDL_HapticClose( haptic );
    1.37 + * \endcode
    1.38 + *
    1.39 + * \par Complete example:
    1.40   * \code
    1.41   * int test_haptic( SDL_Joystick * joystick ) {
    1.42   *    SDL_Haptic *haptic;
    1.43 @@ -933,7 +955,7 @@
    1.44   *  
    1.45   *  \param haptic Haptic device to check on.
    1.46   *  \param effect Effect to check to see if it is supported.
    1.47 - *  \return 1 if effect is supported, 0 if it isn't or -1 on error.
    1.48 + *  \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
    1.49   *  
    1.50   *  \sa SDL_HapticQuery
    1.51   *  \sa SDL_HapticNewEffect
    1.52 @@ -1113,6 +1135,58 @@
    1.53   */
    1.54  extern DECLSPEC int SDLCALL SDL_HapticStopAll(SDL_Haptic * haptic);
    1.55  
    1.56 +/**
    1.57 + *  \brief Checks to see if rumble is supported on a haptic device..
    1.58 + *
    1.59 + *  \param haptic Haptic device to check to see if it supports rumble.
    1.60 + *  \return SDL_TRUE if effect is supported, SDL_FALSE if it isn't or -1 on error.
    1.61 + *
    1.62 + *  \sa SDL_HapticRumbleInit
    1.63 + *  \sa SDL_HapticRumblePlay
    1.64 + *  \sa SDL_HapticRumbleStop
    1.65 + */
    1.66 +extern DECLSPEC int SDLCALL SDL_HapticRumbleSupported(SDL_Haptic * haptic);
    1.67 +
    1.68 +/**
    1.69 + *  \brief Initializes the haptic device for simple rumble playback.
    1.70 + *
    1.71 + *  \param haptic Haptic device to initialize for simple rumble playback.
    1.72 + *  \return 0 on success or -1 on error.
    1.73 + *
    1.74 + *  \sa SDL_HapticOpen
    1.75 + *  \sa SDL_HapticRumbleSupported
    1.76 + *  \sa SDL_HapticRumblePlay
    1.77 + *  \sa SDL_HapticRumbleStop
    1.78 + */
    1.79 +extern DECLSPEC int SDLCALL SDL_HapticRumbleInit(SDL_Haptic * haptic);
    1.80 +
    1.81 +/**
    1.82 + *  \brief Runs simple rumble on a haptic device
    1.83 + *
    1.84 + *  \param haptic Haptic device to play rumble effect on.
    1.85 + *  \param strength Strength of the rumble to play as a 0-1 float value.
    1.86 + *  \param length Length of the rumble to play in miliseconds.
    1.87 + *  \return 0 on success or -1 on error.
    1.88 + *
    1.89 + *  \sa SDL_HapticRumbleSupported
    1.90 + *  \sa SDL_HapticRumbleInit
    1.91 + *  \sa SDL_HapticRumbleStop
    1.92 + */
    1.93 +extern DECLSPEC int SDLCALL SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length );
    1.94 +
    1.95 +/**
    1.96 + *  \brief Stops the simple rumble on a haptic device.
    1.97 + *
    1.98 + *  \param haptic Haptic to stop the rumble on.
    1.99 + *  \return 0 on success or -1 on error.
   1.100 + *
   1.101 + *  \sa SDL_HapticRumbleSupported
   1.102 + *  \sa SDL_HapticRumbleInit
   1.103 + *  \sa SDL_HapticRumblePlay
   1.104 + */
   1.105 +extern DECLSPEC int SDLCALL SDL_HapticRumbleStop(SDL_Haptic * haptic);
   1.106 +
   1.107 +
   1.108  
   1.109  /* Ends C function definitions when using C++ */
   1.110  #ifdef __cplusplus
     2.1 --- a/src/haptic/SDL_haptic.c	Sun Feb 20 10:42:51 2011 -0800
     2.2 +++ b/src/haptic/SDL_haptic.c	Sun Feb 20 10:54:44 2011 -0800
     2.3 @@ -143,6 +143,7 @@
     2.4  
     2.5      /* Initialize the haptic device */
     2.6      SDL_memset(haptic, 0, (sizeof *haptic));
     2.7 +    haptic->rumble_id = -1;
     2.8      haptic->index = device_index;
     2.9      if (SDL_SYS_HapticOpen(haptic) < 0) {
    2.10          SDL_free(haptic);
    2.11 @@ -292,6 +293,7 @@
    2.12  
    2.13      /* Initialize the haptic device */
    2.14      SDL_memset(haptic, 0, sizeof(SDL_Haptic));
    2.15 +    haptic->rumble_id = -1;
    2.16      if (SDL_SYS_HapticOpenFromJoystick(haptic, joystick) < 0) {
    2.17          SDL_free(haptic);
    2.18          return NULL;
    2.19 @@ -706,3 +708,110 @@
    2.20  
    2.21      return SDL_SYS_HapticStopAll(haptic);
    2.22  }
    2.23 +
    2.24 +static int
    2.25 +SDL_HapticRumbleCreate(SDL_HapticEffect * efx)
    2.26 +{
    2.27 +   SDL_memset(efx, 0, sizeof(SDL_HapticEffect));
    2.28 +   efx->type = SDL_HAPTIC_SINE;
    2.29 +   efx->periodic.period = 1000;
    2.30 +   efx->periodic.magnitude = 0x4000;
    2.31 +   efx->periodic.length = 5000;
    2.32 +   efx->periodic.attack_length = 0;
    2.33 +   efx->periodic.fade_length = 0;
    2.34 +}
    2.35 +
    2.36 +/*
    2.37 + * Checks to see if rumble is supported.
    2.38 + */
    2.39 +int
    2.40 +SDL_HapticRumbleSupported(SDL_Haptic * haptic)
    2.41 +{
    2.42 +    SDL_HapticEffect efx;
    2.43 +
    2.44 +    if (!ValidHaptic(haptic)) {
    2.45 +        return -1;
    2.46 +    }
    2.47 +
    2.48 +    SDL_HapticRumbleCreate(&efx);
    2.49 +    return SDL_HapticEffectSupported(haptic, &efx);
    2.50 +}
    2.51 +
    2.52 +/*
    2.53 + * Initializes the haptic device for simple rumble playback.
    2.54 + */
    2.55 +int
    2.56 +SDL_HapticRumbleInit(SDL_Haptic * haptic)
    2.57 +{
    2.58 +    if (!ValidHaptic(haptic)) {
    2.59 +        return -1;
    2.60 +    }
    2.61 +
    2.62 +    /* Already allocated. */
    2.63 +    if (haptic->rumble_id >= 0) {
    2.64 +        return 0;
    2.65 +    }
    2.66 +
    2.67 +    /* Copy over. */
    2.68 +    SDL_HapticRumbleCreate(&haptic->rumble_effect);
    2.69 +    haptic->rumble_id = SDL_HapticNewEffect(haptic, &haptic->rumble_effect);
    2.70 +    if (haptic->rumble_id >= 0) {
    2.71 +        return 0;
    2.72 +    }
    2.73 +    return -1;
    2.74 +}
    2.75 +
    2.76 +/*
    2.77 + * Runs simple rumble on a haptic device
    2.78 + */
    2.79 +int
    2.80 +SDL_HapticRumblePlay(SDL_Haptic * haptic, float strength, Uint32 length)
    2.81 +{
    2.82 +    int ret;
    2.83 +    SDL_HapticPeriodic *efx;
    2.84 +
    2.85 +    if (!ValidHaptic(haptic)) {
    2.86 +        return -1;
    2.87 +    }
    2.88 +
    2.89 +    if (haptic->rumble_id < 0) {
    2.90 +        SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
    2.91 +        return -1;
    2.92 +    }
    2.93 +
    2.94 +    /* Clamp strength. */
    2.95 +    if (strength > 1.0f) {
    2.96 +        strength = 1.0f;
    2.97 +    }
    2.98 +    else if (strength < 0.0f) {
    2.99 +        strength = 0.0f;
   2.100 +    }
   2.101 +
   2.102 +    /* New effect. */
   2.103 +    efx = &haptic->rumble_effect.periodic;
   2.104 +    efx->magnitude = (Sint16)(32767.0f*strength);
   2.105 +    efx->length = length;
   2.106 +    ret = SDL_HapticUpdateEffect(haptic, haptic->rumble_id, &haptic->rumble_effect);
   2.107 +
   2.108 +    return SDL_HapticRunEffect(haptic, haptic->rumble_id, 1);
   2.109 +}
   2.110 +
   2.111 +/*
   2.112 + * Stops the simple rumble on a haptic device.
   2.113 + */
   2.114 +int
   2.115 +SDL_HapticRumbleStop(SDL_Haptic * haptic)
   2.116 +{
   2.117 +    if (!ValidHaptic(haptic)) {
   2.118 +        return -1;
   2.119 +    }
   2.120 +
   2.121 +    if (haptic->rumble_id < 0) {
   2.122 +        SDL_SetError("Haptic: Rumble effect not initialized on haptic device");
   2.123 +        return -1;
   2.124 +    }
   2.125 +
   2.126 +    return SDL_HapticStopEffect(haptic, haptic->rumble_id);
   2.127 +}
   2.128 +
   2.129 +
     3.1 --- a/src/haptic/SDL_syshaptic.h	Sun Feb 20 10:42:51 2011 -0800
     3.2 +++ b/src/haptic/SDL_syshaptic.h	Sun Feb 20 10:54:44 2011 -0800
     3.3 @@ -52,6 +52,9 @@
     3.4  
     3.5      struct haptic_hwdata *hwdata;       /* Driver dependent */
     3.6      int ref_count;              /* Count for multiple opens */
     3.7 +
     3.8 +    int rumble_id;              /* ID of rumble effect for simple rumble API. */
     3.9 +    SDL_HapticEffect rumble_effect; /* Rumble effect. */
    3.10  };
    3.11  
    3.12  /* 
     4.1 --- a/test/Makefile.in	Sun Feb 20 10:42:51 2011 -0800
     4.2 +++ b/test/Makefile.in	Sun Feb 20 10:54:44 2011 -0800
     4.3 @@ -28,6 +28,7 @@
     4.4  	testgl2$(EXE) \
     4.5  	testgles$(EXE) \
     4.6  	testhaptic$(EXE) \
     4.7 +   testrumble$(EXE) \
     4.8  	testhread$(EXE) \
     4.9  	testiconv$(EXE) \
    4.10  	testime$(EXE) \
    4.11 @@ -133,6 +134,9 @@
    4.12  testhaptic$(EXE): $(srcdir)/testhaptic.c
    4.13  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    4.14  
    4.15 +testrumble$(EXE): $(srcdir)/testrumble.c
    4.16 +	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    4.17 +
    4.18  testhread$(EXE): $(srcdir)/testhread.c
    4.19  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    4.20  
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/testrumble.c	Sun Feb 20 10:54:44 2011 -0800
     5.3 @@ -0,0 +1,133 @@
     5.4 +/*
     5.5 +Copyright (c) 2011, Edgar Simo Serra
     5.6 +All rights reserved.
     5.7 +
     5.8 +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     5.9 +
    5.10 +    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    5.11 +    * 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.
    5.12 +    * 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.
    5.13 +
    5.14 +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.
    5.15 +*/
    5.16 +
    5.17 +/*
    5.18 + * includes
    5.19 + */
    5.20 +#include <stdlib.h>
    5.21 +#include "SDL.h"
    5.22 +#include "SDL_haptic.h"
    5.23 +
    5.24 +#include <stdio.h>              /* printf */
    5.25 +#include <string.h>             /* strstr */
    5.26 +#include <ctype.h>              /* isdigit */
    5.27 +
    5.28 +
    5.29 +static SDL_Haptic *haptic;
    5.30 +
    5.31 +
    5.32 +/**
    5.33 + * @brief The entry point of this force feedback demo.
    5.34 + * @param[in] argc Number of arguments.
    5.35 + * @param[in] argv Array of argc arguments.
    5.36 + */
    5.37 +int
    5.38 +main(int argc, char **argv)
    5.39 +{
    5.40 +    int i;
    5.41 +    char *name;
    5.42 +    int index;
    5.43 +    SDL_HapticEffect efx[5];
    5.44 +    int id[5];
    5.45 +    int nefx;
    5.46 +    unsigned int supported;
    5.47 +
    5.48 +    name = NULL;
    5.49 +    index = -1;
    5.50 +    if (argc > 1) {
    5.51 +        name = argv[1];
    5.52 +        if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    5.53 +            printf("USAGE: %s [device]\n"
    5.54 +                   "If device is a two-digit number it'll use it as an index, otherwise\n"
    5.55 +                   "it'll use it as if it were part of the device's name.\n",
    5.56 +                   argv[0]);
    5.57 +            return 0;
    5.58 +        }
    5.59 +
    5.60 +        i = strlen(name);
    5.61 +        if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
    5.62 +            index = atoi(name);
    5.63 +            name = NULL;
    5.64 +        }
    5.65 +    }
    5.66 +
    5.67 +    /* Initialize the force feedbackness */
    5.68 +    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    5.69 +             SDL_INIT_HAPTIC);
    5.70 +    printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    5.71 +    if (SDL_NumHaptics() > 0) {
    5.72 +        /* We'll just use index or the first force feedback device found */
    5.73 +        if (name == NULL) {
    5.74 +            i = (index != -1) ? index : 0;
    5.75 +        }
    5.76 +        /* Try to find matching device */
    5.77 +        else {
    5.78 +            for (i = 0; i < SDL_NumHaptics(); i++) {
    5.79 +                if (strstr(SDL_HapticName(i), name) != NULL)
    5.80 +                    break;
    5.81 +            }
    5.82 +
    5.83 +            if (i >= SDL_NumHaptics()) {
    5.84 +                printf("Unable to find device matching '%s', aborting.\n",
    5.85 +                       name);
    5.86 +                return 1;
    5.87 +            }
    5.88 +        }
    5.89 +
    5.90 +        haptic = SDL_HapticOpen(i);
    5.91 +        if (haptic == NULL) {
    5.92 +            printf("Unable to create the haptic device: %s\n",
    5.93 +                   SDL_GetError());
    5.94 +            return 1;
    5.95 +        }
    5.96 +        printf("Device: %s\n", SDL_HapticName(i));
    5.97 +    } else {
    5.98 +        printf("No Haptic devices found!\n");
    5.99 +        return 1;
   5.100 +    }
   5.101 +
   5.102 +    /* We only want force feedback errors. */
   5.103 +    SDL_ClearError();
   5.104 +
   5.105 +    if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
   5.106 +        printf("\nRumble not supported!\n");
   5.107 +        return 1;
   5.108 +    }
   5.109 +    if (SDL_HapticRumbleInit(haptic) != 0) {
   5.110 +        printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
   5.111 +        return 1;
   5.112 +    }
   5.113 +    printf("Playing 2 second rumble at 0.5 magnitude.\n");
   5.114 +    if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
   5.115 +       printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   5.116 +       return 1;
   5.117 +    }
   5.118 +    SDL_Delay(2000);
   5.119 +    printf("Stopping rumble.\n");
   5.120 +    SDL_HapticRumbleStop(haptic);
   5.121 +    SDL_Delay(2000);
   5.122 +    printf("Playing 2 second rumble at 0.3 magnitude.\n");
   5.123 +    if (SDL_HapticRumblePlay(haptic, 0.3, 5000) != 0) {
   5.124 +       printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   5.125 +       return 1;
   5.126 +    }
   5.127 +    SDL_Delay(2000);
   5.128 +
   5.129 +    /* Quit */
   5.130 +    if (haptic != NULL)
   5.131 +        SDL_HapticClose(haptic);
   5.132 +    SDL_Quit();
   5.133 +
   5.134 +    return 0;
   5.135 +}
   5.136 +