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