test/testhaptic.c
changeset 7639 9406b7dd2f2d
parent 7629 a0a3bd77cbc4
child 7677 871d43c6968a
     1.1 --- a/test/testhaptic.c	Mon Aug 12 22:00:21 2013 -0700
     1.2 +++ b/test/testhaptic.c	Wed Aug 14 23:30:10 2013 -0700
     1.3 @@ -26,7 +26,6 @@
     1.4   * includes
     1.5   */
     1.6  #include <stdlib.h>
     1.7 -#include <stdio.h>              /* printf */
     1.8  #include <string.h>             /* strstr */
     1.9  #include <ctype.h>              /* isdigit */
    1.10  
    1.11 @@ -62,12 +61,15 @@
    1.12      int nefx;
    1.13      unsigned int supported;
    1.14  
    1.15 +    /* Enable standard application logging */
    1.16 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    1.17 +
    1.18      name = NULL;
    1.19      index = -1;
    1.20      if (argc > 1) {
    1.21          name = argv[1];
    1.22          if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    1.23 -            printf("USAGE: %s [device]\n"
    1.24 +            SDL_Log("USAGE: %s [device]\n"
    1.25                     "If device is a two-digit number it'll use it as an index, otherwise\n"
    1.26                     "it'll use it as if it were part of the device's name.\n",
    1.27                     argv[0]);
    1.28 @@ -84,7 +86,7 @@
    1.29      /* Initialize the force feedbackness */
    1.30      SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    1.31               SDL_INIT_HAPTIC);
    1.32 -    printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    1.33 +    SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
    1.34      if (SDL_NumHaptics() > 0) {
    1.35          /* We'll just use index or the first force feedback device found */
    1.36          if (name == NULL) {
    1.37 @@ -98,7 +100,7 @@
    1.38              }
    1.39  
    1.40              if (i >= SDL_NumHaptics()) {
    1.41 -                printf("Unable to find device matching '%s', aborting.\n",
    1.42 +                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
    1.43                         name);
    1.44                  return 1;
    1.45              }
    1.46 @@ -106,14 +108,14 @@
    1.47  
    1.48          haptic = SDL_HapticOpen(i);
    1.49          if (haptic == NULL) {
    1.50 -            printf("Unable to create the haptic device: %s\n",
    1.51 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
    1.52                     SDL_GetError());
    1.53              return 1;
    1.54          }
    1.55 -        printf("Device: %s\n", SDL_HapticName(i));
    1.56 +        SDL_Log("Device: %s\n", SDL_HapticName(i));
    1.57          HapticPrintSupported(haptic);
    1.58      } else {
    1.59 -        printf("No Haptic devices found!\n");
    1.60 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
    1.61          return 1;
    1.62      }
    1.63  
    1.64 @@ -125,10 +127,10 @@
    1.65      nefx = 0;
    1.66      supported = SDL_HapticQuery(haptic);
    1.67  
    1.68 -    printf("\nUploading effects\n");
    1.69 +    SDL_Log("\nUploading effects\n");
    1.70      /* First we'll try a SINE effect. */
    1.71      if (supported & SDL_HAPTIC_SINE) {
    1.72 -        printf("   effect %d: Sine Wave\n", nefx);
    1.73 +        SDL_Log("   effect %d: Sine Wave\n", nefx);
    1.74          efx[nefx].type = SDL_HAPTIC_SINE;
    1.75          efx[nefx].periodic.period = 1000;
    1.76          efx[nefx].periodic.magnitude = 0x4000;
    1.77 @@ -137,14 +139,14 @@
    1.78          efx[nefx].periodic.fade_length = 1000;
    1.79          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
    1.80          if (id[nefx] < 0) {
    1.81 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
    1.82 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
    1.83              abort_execution();
    1.84          }
    1.85          nefx++;
    1.86      }
    1.87      /* Now we'll try a SAWTOOTHUP */
    1.88      if (supported & SDL_HAPTIC_SAWTOOTHUP) {
    1.89 -        printf("   effect %d: Sawtooth Up\n", nefx);
    1.90 +        SDL_Log("   effect %d: Sawtooth Up\n", nefx);
    1.91          efx[nefx].type = SDL_HAPTIC_SAWTOOTHUP;
    1.92          efx[nefx].periodic.period = 500;
    1.93          efx[nefx].periodic.magnitude = 0x5000;
    1.94 @@ -153,14 +155,14 @@
    1.95          efx[nefx].periodic.fade_length = 1000;
    1.96          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
    1.97          if (id[nefx] < 0) {
    1.98 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
    1.99 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.100              abort_execution();
   1.101          }
   1.102          nefx++;
   1.103      }
   1.104      /* Now the classical constant effect. */
   1.105      if (supported & SDL_HAPTIC_CONSTANT) {
   1.106 -        printf("   effect %d: Constant Force\n", nefx);
   1.107 +        SDL_Log("   effect %d: Constant Force\n", nefx);
   1.108          efx[nefx].type = SDL_HAPTIC_CONSTANT;
   1.109          efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
   1.110          efx[nefx].constant.direction.dir[0] = 20000;    /* Force comes from the south-west. */
   1.111 @@ -170,14 +172,14 @@
   1.112          efx[nefx].constant.fade_length = 1000;
   1.113          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.114          if (id[nefx] < 0) {
   1.115 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.116 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.117              abort_execution();
   1.118          }
   1.119          nefx++;
   1.120      }
   1.121      /* The cute spring effect. */
   1.122      if (supported & SDL_HAPTIC_SPRING) {
   1.123 -        printf("   effect %d: Condition Spring\n", nefx);
   1.124 +        SDL_Log("   effect %d: Condition Spring\n", nefx);
   1.125          efx[nefx].type = SDL_HAPTIC_SPRING;
   1.126          efx[nefx].condition.length = 5000;
   1.127          for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   1.128 @@ -189,14 +191,14 @@
   1.129          }
   1.130          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.131          if (id[nefx] < 0) {
   1.132 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.133 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.134              abort_execution();
   1.135          }
   1.136          nefx++;
   1.137      }
   1.138      /* The pretty awesome inertia effect. */
   1.139      if (supported & SDL_HAPTIC_INERTIA) {
   1.140 -        printf("   effect %d: Condition Inertia\n", nefx);
   1.141 +        SDL_Log("   effect %d: Condition Inertia\n", nefx);
   1.142          efx[nefx].type = SDL_HAPTIC_SPRING;
   1.143          efx[nefx].condition.length = 5000;
   1.144          for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
   1.145 @@ -207,7 +209,7 @@
   1.146          }
   1.147          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.148          if (id[nefx] < 0) {
   1.149 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.150 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.151              abort_execution();
   1.152          }
   1.153          nefx++;
   1.154 @@ -215,24 +217,24 @@
   1.155  
   1.156      /* Finally we'll try a left/right effect. */
   1.157      if (supported & SDL_HAPTIC_LEFTRIGHT) {
   1.158 -        printf("   effect %d: Left/Right\n", nefx);
   1.159 +        SDL_Log("   effect %d: Left/Right\n", nefx);
   1.160          efx[nefx].type = SDL_HAPTIC_LEFTRIGHT;
   1.161          efx[nefx].leftright.length = 5000;
   1.162          efx[nefx].leftright.large_magnitude = 0x3000;
   1.163          efx[nefx].leftright.small_magnitude = 0xFFFF;
   1.164          id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
   1.165          if (id[nefx] < 0) {
   1.166 -            printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.167 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
   1.168              abort_execution();
   1.169          }
   1.170          nefx++;
   1.171      }
   1.172  
   1.173  
   1.174 -    printf
   1.175 +    SDL_Log
   1.176          ("\nNow playing effects for 5 seconds each with 1 second delay between\n");
   1.177      for (i = 0; i < nefx; i++) {
   1.178 -        printf("   Playing effect %d\n", i);
   1.179 +        SDL_Log("   Playing effect %d\n", i);
   1.180          SDL_HapticRunEffect(haptic, id[i], 1);
   1.181          SDL_Delay(6000);        /* Effects only have length 5000 */
   1.182      }
   1.183 @@ -252,7 +254,7 @@
   1.184  static void
   1.185  abort_execution(void)
   1.186  {
   1.187 -    printf("\nAborting program execution.\n");
   1.188 +    SDL_Log("\nAborting program execution.\n");
   1.189  
   1.190      SDL_HapticClose(haptic);
   1.191      SDL_Quit();
   1.192 @@ -270,42 +272,42 @@
   1.193      unsigned int supported;
   1.194  
   1.195      supported = SDL_HapticQuery(haptic);
   1.196 -    printf("   Supported effects [%d effects, %d playing]:\n",
   1.197 +    SDL_Log("   Supported effects [%d effects, %d playing]:\n",
   1.198             SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
   1.199      if (supported & SDL_HAPTIC_CONSTANT)
   1.200 -        printf("      constant\n");
   1.201 +        SDL_Log("      constant\n");
   1.202      if (supported & SDL_HAPTIC_SINE)
   1.203 -        printf("      sine\n");
   1.204 +        SDL_Log("      sine\n");
   1.205      /* !!! FIXME: put this back when we have more bits in 2.1 */
   1.206      /*if (supported & SDL_HAPTIC_SQUARE)
   1.207 -        printf("      square\n");*/
   1.208 +        SDL_Log("      square\n");*/
   1.209      if (supported & SDL_HAPTIC_TRIANGLE)
   1.210 -        printf("      triangle\n");
   1.211 +        SDL_Log("      triangle\n");
   1.212      if (supported & SDL_HAPTIC_SAWTOOTHUP)
   1.213 -        printf("      sawtoothup\n");
   1.214 +        SDL_Log("      sawtoothup\n");
   1.215      if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
   1.216 -        printf("      sawtoothdown\n");
   1.217 +        SDL_Log("      sawtoothdown\n");
   1.218      if (supported & SDL_HAPTIC_RAMP)
   1.219 -        printf("      ramp\n");
   1.220 +        SDL_Log("      ramp\n");
   1.221      if (supported & SDL_HAPTIC_FRICTION)
   1.222 -        printf("      friction\n");
   1.223 +        SDL_Log("      friction\n");
   1.224      if (supported & SDL_HAPTIC_SPRING)
   1.225 -        printf("      spring\n");
   1.226 +        SDL_Log("      spring\n");
   1.227      if (supported & SDL_HAPTIC_DAMPER)
   1.228 -        printf("      damper\n");
   1.229 +        SDL_Log("      damper\n");
   1.230      if (supported & SDL_HAPTIC_INERTIA)
   1.231 -        printf("      intertia\n");
   1.232 +        SDL_Log("      inertia\n");
   1.233      if (supported & SDL_HAPTIC_CUSTOM)
   1.234 -        printf("      custom\n");
   1.235 +        SDL_Log("      custom\n");
   1.236      if (supported & SDL_HAPTIC_LEFTRIGHT)
   1.237 -        printf("      left/right\n");
   1.238 -    printf("   Supported capabilities:\n");
   1.239 +        SDL_Log("      left/right\n");
   1.240 +    SDL_Log("   Supported capabilities:\n");
   1.241      if (supported & SDL_HAPTIC_GAIN)
   1.242 -        printf("      gain\n");
   1.243 +        SDL_Log("      gain\n");
   1.244      if (supported & SDL_HAPTIC_AUTOCENTER)
   1.245 -        printf("      autocenter\n");
   1.246 +        SDL_Log("      autocenter\n");
   1.247      if (supported & SDL_HAPTIC_STATUS)
   1.248 -        printf("      status\n");
   1.249 +        SDL_Log("      status\n");
   1.250  }
   1.251  
   1.252  #else
   1.253 @@ -313,7 +315,7 @@
   1.254  int
   1.255  main(int argc, char *argv[])
   1.256  {
   1.257 -    fprintf(stderr, "SDL compiled without Haptic support.\n");
   1.258 +    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
   1.259      exit(1);
   1.260  }
   1.261