test/testrumble.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Mon, 03 Jun 2013 19:24:18 -0700
changeset 7268 b399d0441810
parent 6771 55337ff4256f
child 7437 964e8876ad3b
permissions -rw-r--r--
Add missing VS2012 test projects; update VS2010 and VS2012 solutions; update keybord suite for VS compiler warnings
     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     SDL_HapticEffect efx[5];
    54     int id[5];
    55     int nefx;
    56     unsigned int supported;
    57 
    58     name = NULL;
    59     index = -1;
    60     if (argc > 1) {
    61         name = argv[1];
    62         if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    63             printf("USAGE: %s [device]\n"
    64                    "If device is a two-digit number it'll use it as an index, otherwise\n"
    65                    "it'll use it as if it were part of the device's name.\n",
    66                    argv[0]);
    67             return 0;
    68         }
    69 
    70         i = strlen(name);
    71         if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
    72             index = atoi(name);
    73             name = NULL;
    74         }
    75     }
    76 
    77     /* Initialize the force feedbackness */
    78     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    79              SDL_INIT_HAPTIC);
    80     printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    81     if (SDL_NumHaptics() > 0) {
    82         /* We'll just use index or the first force feedback device found */
    83         if (name == NULL) {
    84             i = (index != -1) ? index : 0;
    85         }
    86         /* Try to find matching device */
    87         else {
    88             for (i = 0; i < SDL_NumHaptics(); i++) {
    89                 if (strstr(SDL_HapticName(i), name) != NULL)
    90                     break;
    91             }
    92 
    93             if (i >= SDL_NumHaptics()) {
    94                 printf("Unable to find device matching '%s', aborting.\n",
    95                        name);
    96                 return 1;
    97             }
    98         }
    99 
   100         haptic = SDL_HapticOpen(i);
   101         if (haptic == NULL) {
   102             printf("Unable to create the haptic device: %s\n",
   103                    SDL_GetError());
   104             return 1;
   105         }
   106         printf("Device: %s\n", SDL_HapticName(i));
   107     } else {
   108         printf("No Haptic devices found!\n");
   109         return 1;
   110     }
   111 
   112     /* We only want force feedback errors. */
   113     SDL_ClearError();
   114 
   115     if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
   116         printf("\nRumble not supported!\n");
   117         return 1;
   118     }
   119     if (SDL_HapticRumbleInit(haptic) != 0) {
   120         printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
   121         return 1;
   122     }
   123     printf("Playing 2 second rumble at 0.5 magnitude.\n");
   124     if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
   125        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   126        return 1;
   127     }
   128     SDL_Delay(2000);
   129     printf("Stopping rumble.\n");
   130     SDL_HapticRumbleStop(haptic);
   131     SDL_Delay(2000);
   132     printf("Playing 2 second rumble at 0.3 magnitude.\n");
   133     if (SDL_HapticRumblePlay(haptic, 0.3, 5000) != 0) {
   134        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   135        return 1;
   136     }
   137     SDL_Delay(2000);
   138 
   139     /* Quit */
   140     if (haptic != NULL)
   141         SDL_HapticClose(haptic);
   142     SDL_Quit();
   143 
   144     return 0;
   145 }
   146 
   147 #else
   148 
   149 int
   150 main(int argc, char *argv[])
   151 {
   152     fprintf(stderr, "SDL compiled without Haptic support.\n");
   153     exit(1);
   154 }
   155 
   156 #endif