test/testrumble.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Feb 2011 10:54:44 -0800
changeset 5360 220d3af9121f
child 5535 96594ac5fd1a
permissions -rw-r--r--
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.
slouken@5360
     1
/*
slouken@5360
     2
Copyright (c) 2011, Edgar Simo Serra
slouken@5360
     3
All rights reserved.
slouken@5360
     4
slouken@5360
     5
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
slouken@5360
     6
slouken@5360
     7
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
slouken@5360
     8
    * 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.
slouken@5360
     9
    * 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.
slouken@5360
    10
slouken@5360
    11
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.
slouken@5360
    12
*/
slouken@5360
    13
slouken@5360
    14
/*
slouken@5360
    15
 * includes
slouken@5360
    16
 */
slouken@5360
    17
#include <stdlib.h>
slouken@5360
    18
#include "SDL.h"
slouken@5360
    19
#include "SDL_haptic.h"
slouken@5360
    20
slouken@5360
    21
#include <stdio.h>              /* printf */
slouken@5360
    22
#include <string.h>             /* strstr */
slouken@5360
    23
#include <ctype.h>              /* isdigit */
slouken@5360
    24
slouken@5360
    25
slouken@5360
    26
static SDL_Haptic *haptic;
slouken@5360
    27
slouken@5360
    28
slouken@5360
    29
/**
slouken@5360
    30
 * @brief The entry point of this force feedback demo.
slouken@5360
    31
 * @param[in] argc Number of arguments.
slouken@5360
    32
 * @param[in] argv Array of argc arguments.
slouken@5360
    33
 */
slouken@5360
    34
int
slouken@5360
    35
main(int argc, char **argv)
slouken@5360
    36
{
slouken@5360
    37
    int i;
slouken@5360
    38
    char *name;
slouken@5360
    39
    int index;
slouken@5360
    40
    SDL_HapticEffect efx[5];
slouken@5360
    41
    int id[5];
slouken@5360
    42
    int nefx;
slouken@5360
    43
    unsigned int supported;
slouken@5360
    44
slouken@5360
    45
    name = NULL;
slouken@5360
    46
    index = -1;
slouken@5360
    47
    if (argc > 1) {
slouken@5360
    48
        name = argv[1];
slouken@5360
    49
        if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
slouken@5360
    50
            printf("USAGE: %s [device]\n"
slouken@5360
    51
                   "If device is a two-digit number it'll use it as an index, otherwise\n"
slouken@5360
    52
                   "it'll use it as if it were part of the device's name.\n",
slouken@5360
    53
                   argv[0]);
slouken@5360
    54
            return 0;
slouken@5360
    55
        }
slouken@5360
    56
slouken@5360
    57
        i = strlen(name);
slouken@5360
    58
        if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
slouken@5360
    59
            index = atoi(name);
slouken@5360
    60
            name = NULL;
slouken@5360
    61
        }
slouken@5360
    62
    }
slouken@5360
    63
slouken@5360
    64
    /* Initialize the force feedbackness */
slouken@5360
    65
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
slouken@5360
    66
             SDL_INIT_HAPTIC);
slouken@5360
    67
    printf("%d Haptic devices detected.\n", SDL_NumHaptics());
slouken@5360
    68
    if (SDL_NumHaptics() > 0) {
slouken@5360
    69
        /* We'll just use index or the first force feedback device found */
slouken@5360
    70
        if (name == NULL) {
slouken@5360
    71
            i = (index != -1) ? index : 0;
slouken@5360
    72
        }
slouken@5360
    73
        /* Try to find matching device */
slouken@5360
    74
        else {
slouken@5360
    75
            for (i = 0; i < SDL_NumHaptics(); i++) {
slouken@5360
    76
                if (strstr(SDL_HapticName(i), name) != NULL)
slouken@5360
    77
                    break;
slouken@5360
    78
            }
slouken@5360
    79
slouken@5360
    80
            if (i >= SDL_NumHaptics()) {
slouken@5360
    81
                printf("Unable to find device matching '%s', aborting.\n",
slouken@5360
    82
                       name);
slouken@5360
    83
                return 1;
slouken@5360
    84
            }
slouken@5360
    85
        }
slouken@5360
    86
slouken@5360
    87
        haptic = SDL_HapticOpen(i);
slouken@5360
    88
        if (haptic == NULL) {
slouken@5360
    89
            printf("Unable to create the haptic device: %s\n",
slouken@5360
    90
                   SDL_GetError());
slouken@5360
    91
            return 1;
slouken@5360
    92
        }
slouken@5360
    93
        printf("Device: %s\n", SDL_HapticName(i));
slouken@5360
    94
    } else {
slouken@5360
    95
        printf("No Haptic devices found!\n");
slouken@5360
    96
        return 1;
slouken@5360
    97
    }
slouken@5360
    98
slouken@5360
    99
    /* We only want force feedback errors. */
slouken@5360
   100
    SDL_ClearError();
slouken@5360
   101
slouken@5360
   102
    if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
slouken@5360
   103
        printf("\nRumble not supported!\n");
slouken@5360
   104
        return 1;
slouken@5360
   105
    }
slouken@5360
   106
    if (SDL_HapticRumbleInit(haptic) != 0) {
slouken@5360
   107
        printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
slouken@5360
   108
        return 1;
slouken@5360
   109
    }
slouken@5360
   110
    printf("Playing 2 second rumble at 0.5 magnitude.\n");
slouken@5360
   111
    if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
slouken@5360
   112
       printf("\nFailed to play rumble: %s\n", SDL_GetError() );
slouken@5360
   113
       return 1;
slouken@5360
   114
    }
slouken@5360
   115
    SDL_Delay(2000);
slouken@5360
   116
    printf("Stopping rumble.\n");
slouken@5360
   117
    SDL_HapticRumbleStop(haptic);
slouken@5360
   118
    SDL_Delay(2000);
slouken@5360
   119
    printf("Playing 2 second rumble at 0.3 magnitude.\n");
slouken@5360
   120
    if (SDL_HapticRumblePlay(haptic, 0.3, 5000) != 0) {
slouken@5360
   121
       printf("\nFailed to play rumble: %s\n", SDL_GetError() );
slouken@5360
   122
       return 1;
slouken@5360
   123
    }
slouken@5360
   124
    SDL_Delay(2000);
slouken@5360
   125
slouken@5360
   126
    /* Quit */
slouken@5360
   127
    if (haptic != NULL)
slouken@5360
   128
        SDL_HapticClose(haptic);
slouken@5360
   129
    SDL_Quit();
slouken@5360
   130
slouken@5360
   131
    return 0;
slouken@5360
   132
}
slouken@5360
   133