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