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