Mercurial > sdl-ios-xcode
comparison test/testhaptic.c @ 2748:5668c3dfe7bc
Allow testing haptic devices by index.
author | Edgar Simo <bobbens@gmail.com> |
---|---|
date | Thu, 04 Sep 2008 13:43:39 +0000 |
parents | 0906692aa6a4 |
children | 9de326b3099c |
comparison
equal
deleted
inserted
replaced
2747:47519ce71def | 2748:5668c3dfe7bc |
---|---|
17 #include "SDL.h" | 17 #include "SDL.h" |
18 #include "SDL_haptic.h" | 18 #include "SDL_haptic.h" |
19 | 19 |
20 #include <stdio.h> /* printf */ | 20 #include <stdio.h> /* printf */ |
21 #include <string.h> /* strstr */ | 21 #include <string.h> /* strstr */ |
22 | 22 #include <ctype.h> /* isdigit */ |
23 | 23 |
24 | 24 |
25 static SDL_Haptic *haptic; | 25 static SDL_Haptic *haptic; |
26 | 26 |
27 | 27 |
40 int | 40 int |
41 main(int argc, char **argv) | 41 main(int argc, char **argv) |
42 { | 42 { |
43 int i; | 43 int i; |
44 char *name; | 44 char *name; |
45 int index; | |
45 SDL_HapticEffect efx[5]; | 46 SDL_HapticEffect efx[5]; |
46 int id[5]; | 47 int id[5]; |
47 int nefx; | 48 int nefx; |
48 unsigned int supported; | 49 unsigned int supported; |
49 | 50 |
50 name = NULL; | 51 name = NULL; |
52 index = -1; | |
51 if (argc > 1) { | 53 if (argc > 1) { |
52 name = argv[1]; | 54 name = argv[1]; |
53 if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) { | 55 if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) { |
54 printf("USAGE: %s [device name]\n" | 56 printf("USAGE: %s [device]\n" |
55 "If device name is specified, it will try to find a device whose name\n" | 57 "If device is a two-digit number it'll use it as an index, otherwise\n" |
56 "contains device name for testing.\n", argv[0]); | 58 "it'll use it as if it were part of the device's name.\n", |
59 argv[0]); | |
57 return 0; | 60 return 0; |
61 } | |
62 | |
63 i = strlen(name); | |
64 if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) { | |
65 index = atoi(name); | |
66 name = NULL; | |
58 } | 67 } |
59 } | 68 } |
60 | 69 |
61 /* Initialize the force feedbackness */ | 70 /* Initialize the force feedbackness */ |
62 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | | 71 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK | |
63 SDL_INIT_HAPTIC); | 72 SDL_INIT_HAPTIC); |
64 printf("%d Haptic devices detected.\n", SDL_NumHaptics()); | 73 printf("%d Haptic devices detected.\n", SDL_NumHaptics()); |
65 if (SDL_NumHaptics() > 0) { | 74 if (SDL_NumHaptics() > 0) { |
66 /* We'll just use the first force feedback device found */ | 75 /* We'll just use index or the first force feedback device found */ |
67 if (name == NULL) { | 76 if (name == NULL) { |
68 i = 0; | 77 i = (index != -1) ? index : 0; |
69 } | 78 } |
70 /* Try to find matching device */ | 79 /* Try to find matching device */ |
71 else { | 80 else { |
72 for (i = 0; i < SDL_NumHaptics(); i++) { | 81 for (i = 0; i < SDL_NumHaptics(); i++) { |
73 if (strstr(SDL_HapticName(i), name) != NULL) | 82 if (strstr(SDL_HapticName(i), name) != NULL) |
81 } | 90 } |
82 } | 91 } |
83 | 92 |
84 haptic = SDL_HapticOpen(i); | 93 haptic = SDL_HapticOpen(i); |
85 if (haptic == NULL) { | 94 if (haptic == NULL) { |
86 perror("Unable to create the haptic device"); | 95 printf("Unable to create the haptic device: %s\n", |
96 SDL_GetError()); | |
87 return 1; | 97 return 1; |
88 } | 98 } |
89 printf("Device: %s\n", SDL_HapticName(i)); | 99 printf("Device: %s\n", SDL_HapticName(i)); |
90 HapticPrintSupported(haptic); | 100 HapticPrintSupported(haptic); |
91 } else { | 101 } else { |