comparison src/haptic/SDL_haptic.c @ 2560:2274406ba792 gsoc2008_force_feedback

Made ValidHaptic cleaner.
author Edgar Simo <bobbens@gmail.com>
date Thu, 31 Jul 2008 09:02:43 +0000
parents 2d88b82ce781
children 3696b9ce8a37
comparison
equal deleted inserted replaced
2559:42b682e85546 2560:2274406ba792
59 59
60 /* 60 /*
61 * Checks to see if the haptic device is valid 61 * Checks to see if the haptic device is valid
62 */ 62 */
63 static int 63 static int
64 ValidHaptic(SDL_Haptic ** haptic) 64 ValidHaptic(SDL_Haptic * haptic)
65 { 65 {
66 int i;
66 int valid; 67 int valid;
68
69 valid = 0;
70 for (i=0; i<SDL_numhaptics; i++) {
71 if (SDL_haptics[i] == haptic) {
72 valid = 1;
73 break;
74 }
75 }
67 76
68 if (*haptic == NULL) {
69 SDL_SetError("Haptic: Device hasn't been opened yet");
70 valid = 0;
71 } else {
72 valid = 1;
73 }
74 return valid; 77 return valid;
75 } 78 }
76 79
77 80
78 /* 81 /*
176 * Returns the index to a haptic device. 179 * Returns the index to a haptic device.
177 */ 180 */
178 int 181 int
179 SDL_HapticIndex(SDL_Haptic * haptic) 182 SDL_HapticIndex(SDL_Haptic * haptic)
180 { 183 {
181 if (!ValidHaptic(&haptic)) { 184 if (!ValidHaptic(haptic)) {
182 return -1; 185 return -1;
183 } 186 }
184 187
185 return haptic->index; 188 return haptic->index;
186 } 189 }
297 SDL_HapticClose(SDL_Haptic * haptic) 300 SDL_HapticClose(SDL_Haptic * haptic)
298 { 301 {
299 int i; 302 int i;
300 303
301 /* Must be valid */ 304 /* Must be valid */
302 if (!ValidHaptic(&haptic)) { 305 if (!ValidHaptic(haptic)) {
303 return; 306 return;
304 } 307 }
305 308
306 /* Check if it's still in use */ 309 /* Check if it's still in use */
307 if (--haptic->ref_count < 0) { 310 if (--haptic->ref_count < 0) {
347 * Returns the number of effects a haptic device has. 350 * Returns the number of effects a haptic device has.
348 */ 351 */
349 int 352 int
350 SDL_HapticNumEffects(SDL_Haptic * haptic) 353 SDL_HapticNumEffects(SDL_Haptic * haptic)
351 { 354 {
352 if (!ValidHaptic(&haptic)) { 355 if (!ValidHaptic(haptic)) {
353 return -1; 356 return -1;
354 } 357 }
355 358
356 return haptic->neffects; 359 return haptic->neffects;
357 } 360 }
361 * Returns the number of effects a haptic device can play. 364 * Returns the number of effects a haptic device can play.
362 */ 365 */
363 int 366 int
364 SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic) 367 SDL_HapticNumEffectsPlaying(SDL_Haptic * haptic)
365 { 368 {
366 if (!ValidHaptic(&haptic)) { 369 if (!ValidHaptic(haptic)) {
367 return -1; 370 return -1;
368 } 371 }
369 372
370 return haptic->nplaying; 373 return haptic->nplaying;
371 } 374 }
375 * Returns supported effects by the device. 378 * Returns supported effects by the device.
376 */ 379 */
377 unsigned int 380 unsigned int
378 SDL_HapticQuery(SDL_Haptic * haptic) 381 SDL_HapticQuery(SDL_Haptic * haptic)
379 { 382 {
380 if (!ValidHaptic(&haptic)) { 383 if (!ValidHaptic(haptic)) {
381 return -1; 384 return -1;
382 } 385 }
383 386
384 return haptic->supported; 387 return haptic->supported;
385 } 388 }
389 * Returns the number of axis on the device. 392 * Returns the number of axis on the device.
390 */ 393 */
391 int 394 int
392 SDL_HapticNumAxes(SDL_Haptic * haptic) 395 SDL_HapticNumAxes(SDL_Haptic * haptic)
393 { 396 {
394 if (!ValidHaptic(&haptic)) { 397 if (!ValidHaptic(haptic)) {
395 return -1; 398 return -1;
396 } 399 }
397 400
398 return haptic->naxes; 401 return haptic->naxes;
399 } 402 }
402 * Checks to see if the device can support the effect. 405 * Checks to see if the device can support the effect.
403 */ 406 */
404 int 407 int
405 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect) 408 SDL_HapticEffectSupported(SDL_Haptic * haptic, SDL_HapticEffect * effect)
406 { 409 {
407 if (!ValidHaptic(&haptic)) { 410 if (!ValidHaptic(haptic)) {
408 return -1; 411 return -1;
409 } 412 }
410 413
411 if ((haptic->supported & effect->type) != 0) 414 if ((haptic->supported & effect->type) != 0)
412 return SDL_TRUE; 415 return SDL_TRUE;
420 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect) 423 SDL_HapticNewEffect(SDL_Haptic * haptic, SDL_HapticEffect * effect)
421 { 424 {
422 int i; 425 int i;
423 426
424 /* Check for device validity. */ 427 /* Check for device validity. */
425 if (!ValidHaptic(&haptic)) { 428 if (!ValidHaptic(haptic)) {
426 return -1; 429 return -1;
427 } 430 }
428 431
429 /* Check to see if effect is supported */ 432 /* Check to see if effect is supported */
430 if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) { 433 if (SDL_HapticEffectSupported(haptic,effect)==SDL_FALSE) {
467 * Updates an effect. 470 * Updates an effect.
468 */ 471 */
469 int 472 int
470 SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data) 473 SDL_HapticUpdateEffect(SDL_Haptic * haptic, int effect, SDL_HapticEffect * data)
471 { 474 {
472 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { 475 if (!ValidHaptic(haptic) || !ValidEffect(haptic,effect)) {
473 return -1; 476 return -1;
474 } 477 }
475 478
476 /* Can't change type dynamically. */ 479 /* Can't change type dynamically. */
477 if (data->type != haptic->effects[effect].effect.type) { 480 if (data->type != haptic->effects[effect].effect.type) {
493 * Runs the haptic effect on the device. 496 * Runs the haptic effect on the device.
494 */ 497 */
495 int 498 int
496 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations) 499 SDL_HapticRunEffect(SDL_Haptic * haptic, int effect, Uint32 iterations)
497 { 500 {
498 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { 501 if (!ValidHaptic(haptic) || !ValidEffect(haptic,effect)) {
499 return -1; 502 return -1;
500 } 503 }
501 504
502 /* Run the effect */ 505 /* Run the effect */
503 if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect], iterations) < 0) { 506 if (SDL_SYS_HapticRunEffect(haptic,&haptic->effects[effect], iterations) < 0) {
511 * Stops the haptic effect on the device. 514 * Stops the haptic effect on the device.
512 */ 515 */
513 int 516 int
514 SDL_HapticStopEffect(SDL_Haptic * haptic, int effect) 517 SDL_HapticStopEffect(SDL_Haptic * haptic, int effect)
515 { 518 {
516 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { 519 if (!ValidHaptic(haptic) || !ValidEffect(haptic,effect)) {
517 return -1; 520 return -1;
518 } 521 }
519 522
520 /* Stop the effect */ 523 /* Stop the effect */
521 if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) { 524 if (SDL_SYS_HapticStopEffect(haptic,&haptic->effects[effect]) < 0) {
529 * Gets rid of a haptic effect. 532 * Gets rid of a haptic effect.
530 */ 533 */
531 void 534 void
532 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect) 535 SDL_HapticDestroyEffect(SDL_Haptic * haptic, int effect)
533 { 536 {
534 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { 537 if (!ValidHaptic(haptic) || !ValidEffect(haptic,effect)) {
535 return; 538 return;
536 } 539 }
537 540
538 /* Not allocated */ 541 /* Not allocated */
539 if (haptic->effects[effect].hweffect == NULL) { 542 if (haptic->effects[effect].hweffect == NULL) {
547 * Gets the status of a haptic effect. 550 * Gets the status of a haptic effect.
548 */ 551 */
549 int 552 int
550 SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect) 553 SDL_HapticGetEffectStatus(SDL_Haptic *haptic, int effect)
551 { 554 {
552 if (!ValidHaptic(&haptic) || !ValidEffect(haptic,effect)) { 555 if (!ValidHaptic(haptic) || !ValidEffect(haptic,effect)) {
553 return -1; 556 return -1;
554 } 557 }
555 558
556 if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) { 559 if ((haptic->supported & SDL_HAPTIC_STATUS) == 0) {
557 SDL_SetError("Haptic: Device does not support status queries."); 560 SDL_SetError("Haptic: Device does not support status queries.");
568 SDL_HapticSetGain(SDL_Haptic * haptic, int gain ) 571 SDL_HapticSetGain(SDL_Haptic * haptic, int gain )
569 { 572 {
570 const char *env; 573 const char *env;
571 int real_gain, max_gain; 574 int real_gain, max_gain;
572 575
573 if (!ValidHaptic(&haptic)) { 576 if (!ValidHaptic(haptic)) {
574 return -1; 577 return -1;
575 } 578 }
576 579
577 if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) { 580 if ((haptic->supported & SDL_HAPTIC_GAIN) == 0) {
578 SDL_SetError("Haptic: Device does not support setting gain."); 581 SDL_SetError("Haptic: Device does not support setting gain.");
611 * Makes the device autocenter, 0 disables. 614 * Makes the device autocenter, 0 disables.
612 */ 615 */
613 int 616 int
614 SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter ) 617 SDL_HapticSetAutocenter(SDL_Haptic * haptic, int autocenter )
615 { 618 {
616 if (!ValidHaptic(&haptic)) { 619 if (!ValidHaptic(haptic)) {
617 return -1; 620 return -1;
618 } 621 }
619 622
620 if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) { 623 if ((haptic->supported & SDL_HAPTIC_AUTOCENTER) == 0) {
621 SDL_SetError("Haptic: Device does not support setting autocenter."); 624 SDL_SetError("Haptic: Device does not support setting autocenter.");