test/testrumble.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 13 Jul 2013 21:13:09 +0200
changeset 7437 964e8876ad3b
parent 6771 55337ff4256f
child 7517 965d57022c01
permissions -rw-r--r--
Fixed compiler warnings in test programs.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /*
    13 Copyright (c) 2011, Edgar Simo Serra
    14 All rights reserved.
    15 
    16 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
    17 
    18     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    19     * 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.
    20     * 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.
    21 
    22 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.
    23 */
    24 
    25 /*
    26  * includes
    27  */
    28 #include <stdlib.h>
    29 #include <stdio.h>              /* printf */
    30 #include <string.h>             /* strstr */
    31 #include <ctype.h>              /* isdigit */
    32 
    33 #include "SDL.h"
    34 
    35 #ifndef SDL_HAPTIC_DISABLED
    36 
    37 #include "SDL_haptic.h"
    38 
    39 static SDL_Haptic *haptic;
    40 
    41 
    42 /**
    43  * @brief The entry point of this force feedback demo.
    44  * @param[in] argc Number of arguments.
    45  * @param[in] argv Array of argc arguments.
    46  */
    47 int
    48 main(int argc, char **argv)
    49 {
    50     int i;
    51     char *name;
    52     int index;
    53 
    54     name = NULL;
    55     index = -1;
    56     if (argc > 1) {
    57         name = argv[1];
    58         if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    59             printf("USAGE: %s [device]\n"
    60                    "If device is a two-digit number it'll use it as an index, otherwise\n"
    61                    "it'll use it as if it were part of the device's name.\n",
    62                    argv[0]);
    63             return 0;
    64         }
    65 
    66         i = strlen(name);
    67         if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
    68             index = atoi(name);
    69             name = NULL;
    70         }
    71     }
    72 
    73     /* Initialize the force feedbackness */
    74     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    75              SDL_INIT_HAPTIC);
    76     printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    77     if (SDL_NumHaptics() > 0) {
    78         /* We'll just use index or the first force feedback device found */
    79         if (name == NULL) {
    80             i = (index != -1) ? index : 0;
    81         }
    82         /* Try to find matching device */
    83         else {
    84             for (i = 0; i < SDL_NumHaptics(); i++) {
    85                 if (strstr(SDL_HapticName(i), name) != NULL)
    86                     break;
    87             }
    88 
    89             if (i >= SDL_NumHaptics()) {
    90                 printf("Unable to find device matching '%s', aborting.\n",
    91                        name);
    92                 return 1;
    93             }
    94         }
    95 
    96         haptic = SDL_HapticOpen(i);
    97         if (haptic == NULL) {
    98             printf("Unable to create the haptic device: %s\n",
    99                    SDL_GetError());
   100             return 1;
   101         }
   102         printf("Device: %s\n", SDL_HapticName(i));
   103     } else {
   104         printf("No Haptic devices found!\n");
   105         return 1;
   106     }
   107 
   108     /* We only want force feedback errors. */
   109     SDL_ClearError();
   110 
   111     if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
   112         printf("\nRumble not supported!\n");
   113         return 1;
   114     }
   115     if (SDL_HapticRumbleInit(haptic) != 0) {
   116         printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
   117         return 1;
   118     }
   119     printf("Playing 2 second rumble at 0.5 magnitude.\n");
   120     if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
   121        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   122        return 1;
   123     }
   124     SDL_Delay(2000);
   125     printf("Stopping rumble.\n");
   126     SDL_HapticRumbleStop(haptic);
   127     SDL_Delay(2000);
   128     printf("Playing 2 second rumble at 0.3 magnitude.\n");
   129     if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
   130        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   131        return 1;
   132     }
   133     SDL_Delay(2000);
   134 
   135     /* Quit */
   136     if (haptic != NULL)
   137         SDL_HapticClose(haptic);
   138     SDL_Quit();
   139 
   140     return 0;
   141 }
   142 
   143 #else
   144 
   145 int
   146 main(int argc, char *argv[])
   147 {
   148     fprintf(stderr, "SDL compiled without Haptic support.\n");
   149     exit(1);
   150 }
   151 
   152 #endif