test/testhaptic.c
changeset 2713 0906692aa6a4
child 2748 5668c3dfe7bc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testhaptic.c	Mon Aug 25 09:55:03 2008 +0000
     1.3 @@ -0,0 +1,266 @@
     1.4 +/*
     1.5 +Copyright (c) 2008, Edgar Simo Serra
     1.6 +All rights reserved.
     1.7 +
     1.8 +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     1.9 +
    1.10 +    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    1.11 +    * 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.
    1.12 +    * 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.
    1.13 +
    1.14 +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.
    1.15 +*/
    1.16 +
    1.17 +/*
    1.18 + * includes
    1.19 + */
    1.20 +#include "SDL.h"
    1.21 +#include "SDL_haptic.h"
    1.22 +
    1.23 +#include <stdio.h>              /* printf */
    1.24 +#include <string.h>             /* strstr */
    1.25 +
    1.26 +
    1.27 +
    1.28 +static SDL_Haptic *haptic;
    1.29 +
    1.30 +
    1.31 +/*
    1.32 + * prototypes
    1.33 + */
    1.34 +static void abort_execution(void);
    1.35 +static void HapticPrintSupported(SDL_Haptic * haptic);
    1.36 +
    1.37 +
    1.38 +/**
    1.39 + * @brief The entry point of this force feedback demo.
    1.40 + * @param[in] argc Number of arguments.
    1.41 + * @param[in] argv Array of argc arguments.
    1.42 + */
    1.43 +int
    1.44 +main(int argc, char **argv)
    1.45 +{
    1.46 +    int i;
    1.47 +    char *name;
    1.48 +    SDL_HapticEffect efx[5];
    1.49 +    int id[5];
    1.50 +    int nefx;
    1.51 +    unsigned int supported;
    1.52 +
    1.53 +    name = NULL;
    1.54 +    if (argc > 1) {
    1.55 +        name = argv[1];
    1.56 +        if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    1.57 +            printf("USAGE: %s [device name]\n"
    1.58 +                   "If device name is specified, it will try to find a device whose name\n"
    1.59 +                   "contains device name for testing.\n", argv[0]);
    1.60 +            return 0;
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64 +    /* Initialize the force feedbackness */
    1.65 +    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    1.66 +             SDL_INIT_HAPTIC);
    1.67 +    printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    1.68 +    if (SDL_NumHaptics() > 0) {
    1.69 +        /* We'll just use the first force feedback device found */
    1.70 +        if (name == NULL) {
    1.71 +            i = 0;
    1.72 +        }
    1.73 +        /* Try to find matching device */
    1.74 +        else {
    1.75 +            for (i = 0; i < SDL_NumHaptics(); i++) {
    1.76 +                if (strstr(SDL_HapticName(i), name) != NULL)
    1.77 +                    break;
    1.78 +            }
    1.79 +
    1.80 +            if (i >= SDL_NumHaptics()) {
    1.81 +                printf("Unable to find device matching '%s', aborting.\n",
    1.82 +                       name);
    1.83 +                return 1;
    1.84 +            }
    1.85 +        }
    1.86 +
    1.87 +        haptic = SDL_HapticOpen(i);
    1.88 +        if (haptic == NULL) {
    1.89 +            perror("Unable to create the haptic device");
    1.90 +            return 1;
    1.91 +        }
    1.92 +        printf("Device: %s\n", SDL_HapticName(i));
    1.93 +        HapticPrintSupported(haptic);
    1.94 +    } else {
    1.95 +        printf("No Haptic devices found!\n");
    1.96 +        return 1;
    1.97 +    }
    1.98 +
    1.99 +    /* We only want force feedback errors. */
   1.100 +    SDL_ClearError();
   1.101 +
   1.102 +    /* Create effects. */
   1.103 +    memset(&efx, 0, sizeof(efx));
   1.104 +    nefx = 0;
   1.105 +    supported = SDL_HapticQuery(haptic);
   1.106 +
   1.107 +    printf("\nUploading effects\n");
   1.108 +    /* First we'll try a SINE effect. */
   1.109 +    if (supported & SDL_HAPTIC_SINE) {
   1.110 +        printf("   effect %d: Sine Wave\n", nefx);
   1.111 +        efx[nefx].type = SDL_HAPTIC_SINE;
   1.112 +        efx[nefx].periodic.period = 1000;
   1.113 +        efx[nefx].periodic.magnitude = 0x4000;
   1.114 +        efx[nefx].periodic.length = 5000;
   1.115 +        efx[nefx].periodic.attack_length = 1000;
   1.116 +        efx[nefx].periodic.fade_length = 1000;
   1.117 +        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.118 +        if (id[nefx] < 0) {
   1.119 +            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.120 +            abort_execution();
   1.121 +        }
   1.122 +        nefx++;
   1.123 +    }
   1.124 +    /* Now we'll try a SAWTOOTHUP */
   1.125 +    if (supported & SDL_HAPTIC_SAWTOOTHUP) {
   1.126 +        printf("   effect %d: Sawtooth Up\n", nefx);
   1.127 +        efx[nefx].type = SDL_HAPTIC_SQUARE;
   1.128 +        efx[nefx].periodic.period = 500;
   1.129 +        efx[nefx].periodic.magnitude = 0x5000;
   1.130 +        efx[nefx].periodic.length = 5000;
   1.131 +        efx[nefx].periodic.attack_length = 1000;
   1.132 +        efx[nefx].periodic.fade_length = 1000;
   1.133 +        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.134 +        if (id[nefx] < 0) {
   1.135 +            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.136 +            abort_execution();
   1.137 +        }
   1.138 +        nefx++;
   1.139 +    }
   1.140 +    /* Now the classical constant effect. */
   1.141 +    if (supported & SDL_HAPTIC_CONSTANT) {
   1.142 +        printf("   effect %d: Constant Force\n", nefx);
   1.143 +        efx[nefx].type = SDL_HAPTIC_CONSTANT;
   1.144 +        efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
   1.145 +        efx[nefx].constant.direction.dir[0] = 20000;    /* Force comes from the south-west. */
   1.146 +        efx[nefx].constant.length = 5000;
   1.147 +        efx[nefx].constant.level = 0x6000;
   1.148 +        efx[nefx].constant.attack_length = 1000;
   1.149 +        efx[nefx].constant.fade_length = 1000;
   1.150 +        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.151 +        if (id[nefx] < 0) {
   1.152 +            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.153 +            abort_execution();
   1.154 +        }
   1.155 +        nefx++;
   1.156 +    }
   1.157 +    /* The cute spring effect. */
   1.158 +    if (supported & SDL_HAPTIC_SPRING) {
   1.159 +        printf("   effect %d: Condition Spring\n", nefx);
   1.160 +        efx[nefx].type = SDL_HAPTIC_SPRING;
   1.161 +        efx[nefx].condition.length = 5000;
   1.162 +        for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   1.163 +            efx[nefx].condition.right_sat[i] = 0x7FFF;
   1.164 +            efx[nefx].condition.left_sat[i] = 0x7FFF;
   1.165 +            efx[nefx].condition.right_coeff[i] = 0x2000;
   1.166 +            efx[nefx].condition.left_coeff[i] = 0x2000;
   1.167 +            efx[nefx].condition.center[i] = 0x1000;     /* Displace the center for it to move. */
   1.168 +        }
   1.169 +        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.170 +        if (id[nefx] < 0) {
   1.171 +            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.172 +            abort_execution();
   1.173 +        }
   1.174 +        nefx++;
   1.175 +    }
   1.176 +    /* The pretty awesome inertia effect. */
   1.177 +    if (supported & SDL_HAPTIC_INERTIA) {
   1.178 +        printf("   effect %d: Condition Inertia\n", nefx);
   1.179 +        efx[nefx].type = SDL_HAPTIC_SPRING;
   1.180 +        efx[nefx].condition.length = 5000;
   1.181 +        for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   1.182 +            efx[nefx].condition.right_sat[i] = 0x7FFF;
   1.183 +            efx[nefx].condition.left_sat[i] = 0x7FFF;
   1.184 +            efx[nefx].condition.right_coeff[i] = 0x2000;
   1.185 +            efx[nefx].condition.left_coeff[i] = 0x2000;
   1.186 +        }
   1.187 +        id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.188 +        if (id[nefx] < 0) {
   1.189 +            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.190 +            abort_execution();
   1.191 +        }
   1.192 +        nefx++;
   1.193 +    }
   1.194 +
   1.195 +    printf
   1.196 +        ("\nNow playing effects for 5 seconds each with 1 second delay between\n");
   1.197 +    for (i = 0; i < nefx; i++) {
   1.198 +        printf("   Playing effect %d\n", i);
   1.199 +        SDL_HapticRunEffect(haptic, id[i], 1);
   1.200 +        SDL_Delay(6000);        /* Effects only have length 5000 */
   1.201 +    }
   1.202 +
   1.203 +    /* Quit */
   1.204 +    if (haptic != NULL)
   1.205 +        SDL_HapticClose(haptic);
   1.206 +    SDL_Quit();
   1.207 +
   1.208 +    return 0;
   1.209 +}
   1.210 +
   1.211 +
   1.212 +/*
   1.213 + * Cleans up a bit.
   1.214 + */
   1.215 +static void
   1.216 +abort_execution(void)
   1.217 +{
   1.218 +    printf("\nAborting program execution.\n");
   1.219 +
   1.220 +    SDL_HapticClose(haptic);
   1.221 +    SDL_Quit();
   1.222 +
   1.223 +    exit(1);
   1.224 +}
   1.225 +
   1.226 +
   1.227 +/*
   1.228 + * Displays information about the haptic device.
   1.229 + */
   1.230 +static void
   1.231 +HapticPrintSupported(SDL_Haptic * haptic)
   1.232 +{
   1.233 +    unsigned int supported;
   1.234 +
   1.235 +    supported = SDL_HapticQuery(haptic);
   1.236 +    printf("   Supported effects [%d effects, %d playing]:\n",
   1.237 +           SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
   1.238 +    if (supported & SDL_HAPTIC_CONSTANT)
   1.239 +        printf("      constant\n");
   1.240 +    if (supported & SDL_HAPTIC_SINE)
   1.241 +        printf("      sine\n");
   1.242 +    if (supported & SDL_HAPTIC_SQUARE)
   1.243 +        printf("      square\n");
   1.244 +    if (supported & SDL_HAPTIC_TRIANGLE)
   1.245 +        printf("      triangle\n");
   1.246 +    if (supported & SDL_HAPTIC_SAWTOOTHUP)
   1.247 +        printf("      sawtoothup\n");
   1.248 +    if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
   1.249 +        printf("      sawtoothdown\n");
   1.250 +    if (supported & SDL_HAPTIC_RAMP)
   1.251 +        printf("      ramp\n");
   1.252 +    if (supported & SDL_HAPTIC_FRICTION)
   1.253 +        printf("      friction\n");
   1.254 +    if (supported & SDL_HAPTIC_SPRING)
   1.255 +        printf("      spring\n");
   1.256 +    if (supported & SDL_HAPTIC_DAMPER)
   1.257 +        printf("      damper\n");
   1.258 +    if (supported & SDL_HAPTIC_INERTIA)
   1.259 +        printf("      intertia\n");
   1.260 +    if (supported & SDL_HAPTIC_CUSTOM)
   1.261 +        printf("      custom\n");
   1.262 +    printf("   Supported capabilities:\n");
   1.263 +    if (supported & SDL_HAPTIC_GAIN)
   1.264 +        printf("      gain\n");
   1.265 +    if (supported & SDL_HAPTIC_AUTOCENTER)
   1.266 +        printf("      autocenter\n");
   1.267 +    if (supported & SDL_HAPTIC_STATUS)
   1.268 +        printf("      status\n");
   1.269 +}