Debian Codegraph @

test/testhaptic.c

raw
1 /*
2 Copyright (c) 2008, Edgar Simo Serra
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 * 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.
9 * 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.
10
11 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.
12 */
13
14 /*
15 * includes
16 */
17 #include "SDL.h"
18
19 #include <stdlib.h>
20
21 #ifndef SDL_HAPTIC_DISABLED
22
23 static SDL_Haptic *haptic;
24
25 /*
26 * prototypes
27 */
28 static void abort_execution(void);
29 static void HapticPrintSupported(SDL_Haptic *);
30
31 /**
32 * @brief The entry point of this force feedback demo.
33 * @param[in] argc Number of arguments.
34 * @param[in] argv Array of argc arguments.
35 */
36 int main(int argc, char **argv)
37 {
38 int i;
39 char *name;
40 int index;
41 SDL_HapticEffect efx[9];
42 int id[9];
43 int nefx;
44 unsigned int supported;
45
46 /* Enable standard application logging */
47 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
48
49 name = NULL;
50 index = -1;
51 if (argc > 1) {
52 name = argv[1];
53 if ((SDL_strcmp(name, "--help") == 0) || (SDL_strcmp(name, "-h") == 0)) {
54 SDL_Log("USAGE: %s [device]\n"
55 "If device is a two-digit number it'll use it as an index, otherwise\n"
56 "it'll use it as if it were part of the device's name.\n",
57 argv[0]);
58 return 0;
59 }
60
61 i = (int)SDL_strlen(name);
62 if ((i < 3) && SDL_isdigit(name[0]) && ((i == 1) || SDL_isdigit(name[1]))) {
63 index = SDL_atoi(name);
64 name = NULL;
65 }
66 }
67
68 /* Initialize the force feedbackness */
69 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
70 SDL_INIT_HAPTIC);
71 SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
72 if (SDL_NumHaptics() > 0) {
73 /* We'll just use index or the first force feedback device found */
74 if (!name) {
75 i = (index != -1) ? index : 0;
76 }
77 /* Try to find matching device */
78 else {
79 for (i = 0; i < SDL_NumHaptics(); i++) {
80 if (SDL_strstr(SDL_HapticName(i), name) != NULL) {
81 break;
82 }
83 }
84
85 if (i >= SDL_NumHaptics()) {
86 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
87 name);
88 return 1;
89 }
90 }
91
92 haptic = SDL_HapticOpen(i);
93 if (!haptic) {
94 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
95 SDL_GetError());
96 return 1;
97 }
98 SDL_Log("Device: %s\n", SDL_HapticName(i));
99 HapticPrintSupported(haptic);
100 } else {
101 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
102 return 1;
103 }
104
105 /* We only want force feedback errors. */
106 SDL_ClearError();
107
108 /* Create effects. */
109 SDL_memset(efx, 0, sizeof(efx));
110 nefx = 0;
111 supported = SDL_HapticQuery(haptic);
112
113 SDL_Log("\nUploading effects\n");
114 /* First we'll try a SINE effect. */
115 if (supported & SDL_HAPTIC_SINE) {
116 SDL_Log(" effect %d: Sine Wave\n", nefx);
117 efx[nefx].type = SDL_HAPTIC_SINE;
118 efx[nefx].periodic.period = 1000;
119 efx[nefx].periodic.magnitude = -0x2000; /* Negative magnitude and ... */
120 efx[nefx].periodic.phase = 18000; /* ... 180 degrees phase shift => cancel eachother */
121 efx[nefx].periodic.length = 5000;
122 efx[nefx].periodic.attack_length = 1000;
123 efx[nefx].periodic.fade_length = 1000;
124 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
125 if (id[nefx] < 0) {
126 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
127 abort_execution();
128 }
129 nefx++;
130 }
131 /* Now we'll try a SAWTOOTHUP */
132 if (supported & SDL_HAPTIC_SAWTOOTHUP) {
133 SDL_Log(" effect %d: Sawtooth Up\n", nefx);
134 efx[nefx].type = SDL_HAPTIC_SAWTOOTHUP;
135 efx[nefx].periodic.period = 500;
136 efx[nefx].periodic.magnitude = 0x5000;
137 efx[nefx].periodic.length = 5000;
138 efx[nefx].periodic.attack_length = 1000;
139 efx[nefx].periodic.fade_length = 1000;
140 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
141 if (id[nefx] < 0) {
142 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
143 abort_execution();
144 }
145 nefx++;
146 }
147
148 /* Now the classical constant effect. */
149 if (supported & SDL_HAPTIC_CONSTANT) {
150 SDL_Log(" effect %d: Constant Force\n", nefx);
151 efx[nefx].type = SDL_HAPTIC_CONSTANT;
152 efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
153 efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
154 efx[nefx].constant.length = 5000;
155 efx[nefx].constant.level = 0x6000;
156 efx[nefx].constant.attack_length = 1000;
157 efx[nefx].constant.fade_length = 1000;
158 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
159 if (id[nefx] < 0) {
160 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
161 abort_execution();
162 }
163 nefx++;
164 }
165
166 /* The cute spring effect. */
167 if (supported & SDL_HAPTIC_SPRING) {
168 SDL_Log(" effect %d: Condition Spring\n", nefx);
169 efx[nefx].type = SDL_HAPTIC_SPRING;
170 efx[nefx].condition.length = 5000;
171 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
172 efx[nefx].condition.right_sat[i] = 0xFFFF;
173 efx[nefx].condition.left_sat[i] = 0xFFFF;
174 efx[nefx].condition.right_coeff[i] = 0x2000;
175 efx[nefx].condition.left_coeff[i] = 0x2000;
176 efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
177 }
178 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
179 if (id[nefx] < 0) {
180 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
181 abort_execution();
182 }
183 nefx++;
184 }
185 /* The interesting damper effect. */
186 if (supported & SDL_HAPTIC_DAMPER) {
187 SDL_Log(" effect %d: Condition Damper\n", nefx);
188 efx[nefx].type = SDL_HAPTIC_DAMPER;
189 efx[nefx].condition.length = 5000;
190 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
191 efx[nefx].condition.right_sat[i] = 0xFFFF;
192 efx[nefx].condition.left_sat[i] = 0xFFFF;
193 efx[nefx].condition.right_coeff[i] = 0x2000;
194 efx[nefx].condition.left_coeff[i] = 0x2000;
195 }
196 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
197 if (id[nefx] < 0) {
198 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
199 abort_execution();
200 }
201 nefx++;
202 }
203 /* The pretty awesome inertia effect. */
204 if (supported & SDL_HAPTIC_INERTIA) {
205 SDL_Log(" effect %d: Condition Inertia\n", nefx);
206 efx[nefx].type = SDL_HAPTIC_INERTIA;
207 efx[nefx].condition.length = 5000;
208 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
209 efx[nefx].condition.right_sat[i] = 0xFFFF;
210 efx[nefx].condition.left_sat[i] = 0xFFFF;
211 efx[nefx].condition.right_coeff[i] = 0x2000;
212 efx[nefx].condition.left_coeff[i] = 0x2000;
213 efx[nefx].condition.deadband[i] = 0x1000; /* 1/16th of axis-range around the center is 'dead'. */
214 }
215 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
216 if (id[nefx] < 0) {
217 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
218 abort_execution();
219 }
220 nefx++;
221 }
222 /* The hot friction effect. */
223 if (supported & SDL_HAPTIC_FRICTION) {
224 SDL_Log(" effect %d: Condition Friction\n", nefx);
225 efx[nefx].type = SDL_HAPTIC_FRICTION;
226 efx[nefx].condition.length = 5000;
227 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
228 efx[nefx].condition.right_sat[i] = 0xFFFF;
229 efx[nefx].condition.left_sat[i] = 0xFFFF;
230 efx[nefx].condition.right_coeff[i] = 0x2000;
231 efx[nefx].condition.left_coeff[i] = 0x2000;
232 }
233 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
234 if (id[nefx] < 0) {
235 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
236 abort_execution();
237 }
238 nefx++;
239 }
240
241 /* Now we'll try a ramp effect */
242 if (supported & SDL_HAPTIC_RAMP) {
243 SDL_Log(" effect %d: Ramp\n", nefx);
244 efx[nefx].type = SDL_HAPTIC_RAMP;
245 efx[nefx].ramp.direction.type = SDL_HAPTIC_CARTESIAN;
246 efx[nefx].ramp.direction.dir[0] = 1; /* Force comes from */
247 efx[nefx].ramp.direction.dir[1] = -1; /* the north-east. */
248 efx[nefx].ramp.length = 5000;
249 efx[nefx].ramp.start = 0x4000;
250 efx[nefx].ramp.end = -0x4000;
251 efx[nefx].ramp.attack_length = 1000;
252 efx[nefx].ramp.fade_length = 1000;
253 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
254 if (id[nefx] < 0) {
255 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
256 abort_execution();
257 }
258 nefx++;
259 }
260
261 /* Finally we'll try a left/right effect. */
262 if (supported & SDL_HAPTIC_LEFTRIGHT) {
263 SDL_Log(" effect %d: Left/Right\n", nefx);
264 efx[nefx].type = SDL_HAPTIC_LEFTRIGHT;
265 efx[nefx].leftright.length = 5000;
266 efx[nefx].leftright.large_magnitude = 0x3000;
267 efx[nefx].leftright.small_magnitude = 0xFFFF;
268 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
269 if (id[nefx] < 0) {
270 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
271 abort_execution();
272 }
273 nefx++;
274 }
275
276 SDL_Log("\nNow playing effects for 5 seconds each with 1 second delay between\n");
277 for (i = 0; i < nefx; i++) {
278 SDL_Log(" Playing effect %d\n", i);
279 SDL_HapticRunEffect(haptic, id[i], 1);
280 SDL_Delay(6000); /* Effects only have length 5000 */
281 }
282
283 /* Quit */
284 if (haptic) {
285 SDL_HapticClose(haptic);
286 }
287 SDL_Quit();
288
289 return 0;
290 }
291
292 /*
293 * Cleans up a bit.
294 */
295 static void
296 abort_execution(void)
297 {
298 SDL_Log("\nAborting program execution.\n");
299
300 SDL_HapticClose(haptic);
301 SDL_Quit();
302
303 exit(1);
304 }
305
306 /*
307 * Displays information about the haptic device.
308 */
309 static void
310 HapticPrintSupported(SDL_Haptic *ptr)
311 {
312 unsigned int supported;
313
314 supported = SDL_HapticQuery(ptr);
315 SDL_Log(" Supported effects [%d effects, %d playing]:\n",
316 SDL_HapticNumEffects(ptr), SDL_HapticNumEffectsPlaying(ptr));
317 if (supported & SDL_HAPTIC_CONSTANT) {
318 SDL_Log(" constant\n");
319 }
320 if (supported & SDL_HAPTIC_SINE) {
321 SDL_Log(" sine\n");
322 }
323 /* !!! FIXME: put this back when we have more bits in 2.1 */
324 /* if (supported & SDL_HAPTIC_SQUARE)
325 SDL_Log(" square\n"); */
326 if (supported & SDL_HAPTIC_TRIANGLE) {
327 SDL_Log(" triangle\n");
328 }
329 if (supported & SDL_HAPTIC_SAWTOOTHUP) {
330 SDL_Log(" sawtoothup\n");
331 }
332 if (supported & SDL_HAPTIC_SAWTOOTHDOWN) {
333 SDL_Log(" sawtoothdown\n");
334 }
335 if (supported & SDL_HAPTIC_RAMP) {
336 SDL_Log(" ramp\n");
337 }
338 if (supported & SDL_HAPTIC_FRICTION) {
339 SDL_Log(" friction\n");
340 }
341 if (supported & SDL_HAPTIC_SPRING) {
342 SDL_Log(" spring\n");
343 }
344 if (supported & SDL_HAPTIC_DAMPER) {
345 SDL_Log(" damper\n");
346 }
347 if (supported & SDL_HAPTIC_INERTIA) {
348 SDL_Log(" inertia\n");
349 }
350 if (supported & SDL_HAPTIC_CUSTOM) {
351 SDL_Log(" custom\n");
352 }
353 if (supported & SDL_HAPTIC_LEFTRIGHT) {
354 SDL_Log(" left/right\n");
355 }
356 SDL_Log(" Supported capabilities:\n");
357 if (supported & SDL_HAPTIC_GAIN) {
358 SDL_Log(" gain\n");
359 }
360 if (supported & SDL_HAPTIC_AUTOCENTER) {
361 SDL_Log(" autocenter\n");
362 }
363 if (supported & SDL_HAPTIC_STATUS) {
364 SDL_Log(" status\n");
365 }
366 }
367
368 #else
369
370 int
371 main(int argc, char *argv[])
372 {
373 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
374 return 1;
375 }
376
377 #endif