comparison src/haptic/darwin/SDL_syshaptic.c @ 2535:f0ed8471497d gsoc2008_force_feedback

Added custom effect with support on darwin.
author Edgar Simo <bobbens@gmail.com>
date Sat, 19 Jul 2008 15:57:07 +0000
parents e597de8dccd5
children 8d92ec01f92f
comparison
equal deleted inserted replaced
2534:e597de8dccd5 2535:f0ed8471497d
67 }; 67 };
68 68
69 /* 69 /*
70 * Prototypes. 70 * Prototypes.
71 */ 71 */
72 static void SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect ); 72 static void SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect, int type );
73 73
74 74
75 /* 75 /*
76 * Initializes the haptic subsystem. 76 * Initializes the haptic subsystem.
77 */ 77 */
312 if (haptic->hwdata) { 312 if (haptic->hwdata) {
313 313
314 /* Free the effects. */ 314 /* Free the effects. */
315 for (i=0; i<haptic->neffects; i++) { 315 for (i=0; i<haptic->neffects; i++) {
316 if (haptic->effects[i].hweffect != NULL) { 316 if (haptic->effects[i].hweffect != NULL) {
317 SDL_SYS_HapticFreeFFEFFECT(&haptic->effects[i].hweffect->effect); 317 SDL_SYS_HapticFreeFFEFFECT(&haptic->effects[i].hweffect->effect,
318 haptic->effects[i].effect.type);
318 SDL_free(haptic->effects[i].hweffect); 319 SDL_free(haptic->effects[i].hweffect);
319 } 320 }
320 } 321 }
321 SDL_free(haptic->effects); 322 SDL_free(haptic->effects);
322 haptic->neffects = 0; 323 haptic->neffects = 0;
412 FFENVELOPE *envelope; 413 FFENVELOPE *envelope;
413 SDL_HapticConstant *hap_constant; 414 SDL_HapticConstant *hap_constant;
414 SDL_HapticPeriodic *hap_periodic; 415 SDL_HapticPeriodic *hap_periodic;
415 SDL_HapticCondition *hap_condition; 416 SDL_HapticCondition *hap_condition;
416 SDL_HapticRamp *hap_ramp; 417 SDL_HapticRamp *hap_ramp;
418 SDL_HapticCustom *hap_custom;
417 DWORD *axes; 419 DWORD *axes;
418 420
419 /* Set global stuff. */ 421 /* Set global stuff. */
420 SDL_memset(dest, 0, sizeof(FFEFFECT)); 422 SDL_memset(dest, 0, sizeof(FFEFFECT));
421 dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */ 423 dest->dwSize = sizeof(FFEFFECT); /* Set the structure size. */
458 constant = SDL_malloc( sizeof(FFCONSTANTFORCE) ); 460 constant = SDL_malloc( sizeof(FFCONSTANTFORCE) );
459 if (constant == NULL) { 461 if (constant == NULL) {
460 SDL_OutOfMemory(); 462 SDL_OutOfMemory();
461 return -1; 463 return -1;
462 } 464 }
465 SDL_memset(constant, 0, sizeof(FFCONSTANTFORCE));
463 466
464 /* Specifics */ 467 /* Specifics */
465 constant->lMagnitude = CONVERT(hap_constant->level); 468 constant->lMagnitude = CONVERT(hap_constant->level);
466 dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE); 469 dest->cbTypeSpecificParams = sizeof(FFCONSTANTFORCE);
467 dest->lpvTypeSpecificParams = constant; 470 dest->lpvTypeSpecificParams = constant;
494 periodic = SDL_malloc(sizeof(FFPERIODIC)); 497 periodic = SDL_malloc(sizeof(FFPERIODIC));
495 if (periodic == NULL) { 498 if (periodic == NULL) {
496 SDL_OutOfMemory(); 499 SDL_OutOfMemory();
497 return -1; 500 return -1;
498 } 501 }
502 SDL_memset(periodic, 0, sizeof(FFPERIODIC));
499 503
500 /* Specifics */ 504 /* Specifics */
501 periodic->dwMagnitude = CONVERT(hap_periodic->magnitude); 505 periodic->dwMagnitude = CONVERT(hap_periodic->magnitude);
502 periodic->lOffset = CONVERT(hap_periodic->offset); 506 periodic->lOffset = CONVERT(hap_periodic->offset);
503 periodic->dwPhase = hap_periodic->phase; 507 periodic->dwPhase = hap_periodic->phase;
532 condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes); 536 condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes);
533 if (condition == NULL) { 537 if (condition == NULL) {
534 SDL_OutOfMemory(); 538 SDL_OutOfMemory();
535 return -1; 539 return -1;
536 } 540 }
541 SDL_memset(condition, 0, sizeof(FFCONDITION));
537 542
538 /* Specifics */ 543 /* Specifics */
539 for (i=0; i<dest->cAxes; i++) { 544 for (i=0; i<dest->cAxes; i++) {
540 condition[i].lOffset = CONVERT(hap_condition->center[i]); 545 condition[i].lOffset = CONVERT(hap_condition->center[i]);
541 condition[i].lPositiveCoefficient = CONVERT(hap_condition->right_coeff[i]); 546 condition[i].lPositiveCoefficient = CONVERT(hap_condition->right_coeff[i]);
573 ramp = SDL_malloc(sizeof(FFRAMPFORCE)); 578 ramp = SDL_malloc(sizeof(FFRAMPFORCE));
574 if (ramp == NULL) { 579 if (ramp == NULL) {
575 SDL_OutOfMemory(); 580 SDL_OutOfMemory();
576 return -1; 581 return -1;
577 } 582 }
583 SDL_memset(ramp, 0, sizeof(FFRAMPFORCE));
578 584
579 /* Specifics */ 585 /* Specifics */
580 ramp->lStart = CONVERT(hap_ramp->start); 586 ramp->lStart = CONVERT(hap_ramp->start);
581 ramp->lEnd = CONVERT(hap_ramp->end); 587 ramp->lEnd = CONVERT(hap_ramp->end);
588 dest->cbTypeSpecificParams = sizeof(FFRAMPFORCE);
589 dest->lpvTypeSpecificParams = ramp;
582 590
583 /* Generics */ 591 /* Generics */
584 dest->dwDuration = hap_ramp->length * 1000; /* In microseconds. */ 592 dest->dwDuration = hap_ramp->length * 1000; /* In microseconds. */
585 dest->dwTriggerButton = FFJOFS_BUTTON(hap_ramp->button); 593 dest->dwTriggerButton = FFJOFS_BUTTON(hap_ramp->button);
586 dest->dwTriggerRepeatInterval = hap_ramp->interval; 594 dest->dwTriggerRepeatInterval = hap_ramp->interval;
597 envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level); 605 envelope->dwFadeLevel = CONVERT(hap_ramp->fade_level);
598 envelope->dwFadeTime = hap_ramp->fade_length * 1000; 606 envelope->dwFadeTime = hap_ramp->fade_length * 1000;
599 607
600 break; 608 break;
601 609
610 case SDL_HAPTIC_CUSTOM:
611 hap_custom = &src->custom;
612 custom = SDL_malloc(sizeof(FFCUSTOMFORCE));
613 if (custom == NULL) {
614 SDL_OutOfMemory();
615 return -1;
616 }
617 SDL_memset(custom, 0, sizeof(FFCUSTOMFORCE));
618
619 /* Specifics */
620 custom->cChannels = hap_custom->channels;
621 custom->dwSamplePeriod = hap_custom->period * 1000;
622 custom->cSamples = hap_custom->samples;
623 custom->rglForceData = SDL_malloc(sizeof(LONG)*custom->cSamples*custom->cChannels);
624 for (i=0; i<hap_custom->samples*hap_custom->channels; i++) { /* Copy data. */
625 custom->rglForceData[i] = CONVERT(hap_custom->data[i]);
626 }
627 dest->cbTypeSpecificParams = sizeof(FFCUSTOMFORCE);
628 dest->lpvTypeSpecificParams = custom;
629
630 /* Generics */
631 dest->dwDuration = hap_custom->length * 1000; /* In microseconds. */
632 dest->dwTriggerButton = FFJOFS_BUTTON(hap_custom->button);
633 dest->dwTriggerRepeatInterval = hap_custom->interval;
634 dest->dwStartDelay = hap_custom->delay * 1000; /* In microseconds. */
635
636 /* Direction. */
637 if (SDL_SYS_SetDirection(dest, &hap_custom->direction, dest->cAxes) < 0) {
638 return -1;
639 }
640
641 /* Envelope */
642 envelope->dwAttackLevel = CONVERT(hap_custom->attack_level);
643 envelope->dwAttackTime = hap_custom->attack_length * 1000;
644 envelope->dwFadeLevel = CONVERT(hap_custom->fade_level);
645 envelope->dwFadeTime = hap_custom->fade_length * 1000;
646
647 break;
648
602 649
603 default: 650 default:
604 SDL_SetError("Haptic: Unknown effect type."); 651 SDL_SetError("Haptic: Unknown effect type.");
605 return -1; 652 return -1;
606 } 653 }
611 658
612 /* 659 /*
613 * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT. 660 * Frees an FFEFFECT allocated by SDL_SYS_ToFFEFFECT.
614 */ 661 */
615 static void 662 static void
616 SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect ) 663 SDL_SYS_HapticFreeFFEFFECT( FFEFFECT * effect, int type )
617 { 664 {
665 FFCUSTOMFORCE *custom;
666
618 if (effect->lpEnvelope != NULL) { 667 if (effect->lpEnvelope != NULL) {
619 SDL_free(effect->lpEnvelope); 668 SDL_free(effect->lpEnvelope);
620 effect->lpEnvelope = NULL; 669 effect->lpEnvelope = NULL;
621 } 670 }
622 if (effect->rgdwAxes != NULL) { 671 if (effect->rgdwAxes != NULL) {
623 SDL_free(effect->rgdwAxes); 672 SDL_free(effect->rgdwAxes);
624 effect->rgdwAxes = NULL; 673 effect->rgdwAxes = NULL;
625 } 674 }
626 if (effect->lpvTypeSpecificParams != NULL) { 675 if (effect->lpvTypeSpecificParams != NULL) {
676 if (type == SDL_HAPTIC_CUSTOM) { /* Must free the custom data. */
677 custom = (FFCUSTOMFORCE*) effect->lpvTypeSpecificParams;
678 SDL_free(custom->rglForceData);
679 custom->rglForceData = NULL;
680 }
627 SDL_free(effect->lpvTypeSpecificParams); 681 SDL_free(effect->lpvTypeSpecificParams);
628 effect->lpvTypeSpecificParams = NULL; 682 effect->lpvTypeSpecificParams = NULL;
629 } 683 }
630 if (effect->rglDirection != NULL) { 684 if (effect->rglDirection != NULL) {
631 SDL_free(effect->rglDirection); 685 SDL_free(effect->rglDirection);
723 } 777 }
724 778
725 return 0; 779 return 0;
726 780
727 err_effectdone: 781 err_effectdone:
728 SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect); 782 SDL_SYS_HapticFreeFFEFFECT(&effect->hweffect->effect, effect->effect.type);
729 err_hweffect: 783 err_hweffect:
730 if (effect->hweffect != NULL) { 784 if (effect->hweffect != NULL) {
731 SDL_free(effect->hweffect); 785 SDL_free(effect->hweffect);
732 effect->hweffect = NULL; 786 effect->hweffect = NULL;
733 } 787 }