comparison src/haptic/SDL_haptic.c @ 2480:b883974445fc gsoc2008_force_feedback

Some more error reporting. Added periodic effect. Confirmed it works.
author Edgar Simo <bobbens@gmail.com>
date Tue, 01 Jul 2008 09:22:22 +0000
parents b9eb2cfe16cd
children 9d52368ebcf5
comparison
equal deleted inserted replaced
2479:b9eb2cfe16cd 2480:b883974445fc
164 /* Check if it's still in use */ 164 /* Check if it's still in use */
165 if (--haptic->ref_count < 0) { 165 if (--haptic->ref_count < 0) {
166 return; 166 return;
167 } 167 }
168 168
169 /* Close it */ 169 /* Close it, properly removing effects if needed */
170 for (i=0; i<haptic->neffects; i++) {
171 if (haptic->effects[i].hweffect != NULL) {
172 SDL_HapticDestroyEffect(haptic,i);
173 }
174 }
170 SDL_SYS_HapticClose(haptic); 175 SDL_SYS_HapticClose(haptic);
171 176
172 /* Remove from the list */ 177 /* Remove from the list */
173 for (i = 0; SDL_haptics[i]; ++i) { 178 for (i = 0; SDL_haptics[i]; ++i) {
174 if (haptic == SDL_haptics[i]) { 179 if (haptic == SDL_haptics[i]) {
246 /* Check for device validity. */ 251 /* Check for device validity. */
247 if (!ValidHaptic(&haptic)) { 252 if (!ValidHaptic(&haptic)) {
248 return -1; 253 return -1;
249 } 254 }
250 255
256 /* Check to see if effect is supported */
257 if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
258 SDL_SetError("Haptic effect not supported by haptic device.");
259 return -1;
260 }
261
251 /* See if there's a free slot */ 262 /* See if there's a free slot */
252 for (i=0; i<haptic->neffects; i++) { 263 for (i=0; i<haptic->neffects; i++) {
253 if (haptic->effects[i].hweffect == NULL) { 264 if (haptic->effects[i].hweffect == NULL) {
254 265
255 /* Now let the backend create the real effect */ 266 /* Now let the backend create the real effect */
256 if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i]) != 0) { 267 if (SDL_SYS_HapticNewEffect(haptic,&haptic->effects[i],effect) != 0) {
257 return -1; /* Backend failed to create effect */ 268 return -1; /* Backend failed to create effect */
258 } 269 }
259 return i; 270 return i;
260 } 271 }
261 } 272 }
263 SDL_SetError("Haptic device has no free space left."); 274 SDL_SetError("Haptic device has no free space left.");
264 return -1; 275 return -1;
265 } 276 }
266 277
267 /* 278 /*
279 * Checks to see if an effect is valid.
280 */
281 static int
282 ValidEffect(SDL_Haptic * haptic, int effect)
283 {
284 if ((effect < 0) || (effect >= haptic->neffects)) {
285 SDL_SetError("Invalid haptic effect identifier.");
286 return 0;
287 }
288 return 1;
289 }
290
291 /*
268 * Runs the haptic effect on the device. 292 * Runs the haptic effect on the device.
269 */ 293 */
270 int 294 int
271 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect) 295 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect)
272 { 296 {
273 if (!ValidHaptic(&haptic)) { 297 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
274 return -1; 298 return -1;
275 } 299 }
276 300
277 /* Run the effect */ 301 /* Run the effect */
278 if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) { 302 if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect]) < 0) {
286 * Gets rid of a haptic effect. 310 * Gets rid of a haptic effect.
287 */ 311 */
288 void 312 void
289 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect) 313 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
290 { 314 {
291 if (!ValidHaptic(&haptic)) { 315 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) {
292 return; 316 return;
293 } 317 }
294 318
295 /* Not allocated */ 319 /* Not allocated */
296 if (haptic->effects[effect].hweffect == NULL) { 320 if (haptic->effects[effect].hweffect == NULL) {