src/haptic/linux/SDL_syshaptic.c
branchgsoc2008_force_feedback
changeset 2496 8f840a6cdf01
parent 2495 66c02abeef0e
child 2500 5251d0510b7e
equal deleted inserted replaced
2495:66c02abeef0e 2496:8f840a6cdf01
    35 #include <sys/types.h>
    35 #include <sys/types.h>
    36 #include <sys/stat.h>
    36 #include <sys/stat.h>
    37 #include <fcntl.h>
    37 #include <fcntl.h>
    38 #include <linux/limits.h>
    38 #include <linux/limits.h>
    39 #include <string.h>
    39 #include <string.h>
       
    40 #include <errno.h>
    40 
    41 
    41 
    42 
    42 #define MAX_HAPTICS  32
    43 #define MAX_HAPTICS  32
    43 
    44 
    44 
    45 
    86    unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
    87    unsigned long features[1 + FF_MAX/sizeof(unsigned long)];
    87 
    88 
    88    ret = 0;
    89    ret = 0;
    89 
    90 
    90    if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
    91    if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(unsigned long) * 4), features) < 0) {
    91       SDL_SetError("Unable to get device's haptic abilities.");
    92       SDL_SetError("Unable to get device's haptic abilities: %s", strerror(errno));
    92       return -1;
    93       return -1;
    93    }
    94    }
    94 
    95 
    95    EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
    96    EV_TEST(FF_CONSTANT,   SDL_HAPTIC_CONSTANT);
    96    EV_TEST(FF_PERIODIC,   SDL_HAPTIC_SINE |
    97    EV_TEST(FF_PERIODIC,   SDL_HAPTIC_SINE |
   210    haptic->hwdata->fd = fd;
   211    haptic->hwdata->fd = fd;
   211    haptic->supported = EV_IsHaptic(fd);
   212    haptic->supported = EV_IsHaptic(fd);
   212 
   213 
   213    /* Set the effects */
   214    /* Set the effects */
   214    if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
   215    if (ioctl(fd, EVIOCGEFFECTS, &haptic->neffects) < 0) {
   215       SDL_SetError("Unable to query haptic device memory.");
   216       SDL_SetError("Unable to query haptic device memory: %s", strerror(errno));
   216       goto open_err;
   217       goto open_err;
   217    }
   218    }
   218    haptic->effects = (struct haptic_effect *)
   219    haptic->effects = (struct haptic_effect *)
   219          SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   220          SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
   220    if (haptic->effects == NULL) {
   221    if (haptic->effects == NULL) {
   247    int fd;
   248    int fd;
   248 
   249 
   249    /* Open the character device */
   250    /* Open the character device */
   250    fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
   251    fd = open(SDL_hapticlist[haptic->index].fname, O_RDWR, 0);
   251    if (fd < 0) {
   252    if (fd < 0) {
   252       SDL_SetError("Unable to open %s\n", SDL_hapticlist[haptic->index]);
   253       SDL_SetError("Unable to open %s: %s",
       
   254             SDL_hapticlist[haptic->index], strerror(errno));
   253       return -1;
   255       return -1;
   254    }
   256    }
   255    
   257    
   256    return SDL_SYS_HapticOpenFromFD(haptic,fd);
   258    return SDL_SYS_HapticOpenFromFD(haptic,fd);
   257 } 
   259 } 
   475 
   477 
   476          break;
   478          break;
   477 
   479 
   478 
   480 
   479       default:
   481       default:
   480          SDL_SetError("Unknown haptic effect type.");
   482          SDL_SetError("Unknown haptic effect type");
   481          return -1;
   483          return -1;
   482    }
   484    }
   483 
   485 
   484    return 0;
   486    return 0;
   485 }
   487 }
   509    }
   511    }
   510    linux_effect->id = -1; /* Have the kernel give it an id */
   512    linux_effect->id = -1; /* Have the kernel give it an id */
   511 
   513 
   512    /* Upload the effect */
   514    /* Upload the effect */
   513    if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
   515    if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
   514       SDL_SetError("Error uploading effect to the haptic device.");
   516       SDL_SetError("Error uploading effect to the haptic device: %s", strerror(errno));
   515       goto new_effect_err;
   517       goto new_effect_err;
   516    }
   518    }
   517 
   519 
   518    return 0;
   520    return 0;
   519 
   521 
   541    }
   543    }
   542    linux_effect.id = effect->hweffect->effect.id;
   544    linux_effect.id = effect->hweffect->effect.id;
   543 
   545 
   544    /* See if it can be uploaded. */
   546    /* See if it can be uploaded. */
   545    if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
   547    if (ioctl(haptic->hwdata->fd, EVIOCSFF, &linux_effect) < 0) {
   546       SDL_SetError("Error updating the haptic effect.");
   548       SDL_SetError("Error updating the haptic effect: %s", strerror(errno));
   547       return -1;
   549       return -1;
   548    }
   550    }
   549 
   551 
   550    /* Copy the new effect into memory. */
   552    /* Copy the new effect into memory. */
   551    SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) );
   553    SDL_memcpy( &effect->hweffect->effect, &linux_effect, sizeof(struct ff_effect) );
   566    run.type = EV_FF;
   568    run.type = EV_FF;
   567    run.code = effect->hweffect->effect.id;
   569    run.code = effect->hweffect->effect.id;
   568    run.value = 1;
   570    run.value = 1;
   569 
   571 
   570    if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
   572    if (write(haptic->hwdata->fd, (const void*) &run, sizeof(run)) < 0) {
   571       SDL_SetError("Unable to run the haptic effect.");
   573       SDL_SetError("Unable to run the haptic effect: %s", strerror(errno));
   572       return -1;
   574       return -1;
   573    }
   575    }
   574 
   576 
   575    return 0;
   577    return 0;
   576 }
   578 }
   587    stop.type = EV_FF;
   589    stop.type = EV_FF;
   588    stop.code = effect->hweffect->effect.id;
   590    stop.code = effect->hweffect->effect.id;
   589    stop.value = 0;
   591    stop.value = 0;
   590 
   592 
   591    if (write(haptic->hwdata->fd, (const void*) &stop, sizeof(stop)) < 0) {
   593    if (write(haptic->hwdata->fd, (const void*) &stop, sizeof(stop)) < 0) {
   592       SDL_SetError("Unable to stop the haptic effect.");
   594       SDL_SetError("Unable to stop the haptic effect: %s", strerror(errno));
   593       return -1;
   595       return -1;
   594    }
   596    }
   595 
   597 
   596    return 0;
   598    return 0;
   597 }
   599 }
   602  */
   604  */
   603 void
   605 void
   604 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   606 SDL_SYS_HapticDestroyEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   605 {
   607 {
   606    if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
   608    if (ioctl(haptic->hwdata->fd, EVIOCRMFF, effect->hweffect->effect.id) < 0) {
   607       SDL_SetError("Error removing the effect from the haptic device.");
   609       SDL_SetError("Error removing the effect from the haptic device: %s",
       
   610             strerror(errno));
   608    }
   611    }
   609    SDL_free(effect->hweffect);
   612    SDL_free(effect->hweffect);
   610    effect->hweffect = NULL;
   613    effect->hweffect = NULL;
   611 }
   614 }
   612 
   615 
   647    ie.type = EV_FF;
   650    ie.type = EV_FF;
   648    ie.code = FF_GAIN;
   651    ie.code = FF_GAIN;
   649    ie.value = (0xFFFFUL * gain) / 100;
   652    ie.value = (0xFFFFUL * gain) / 100;
   650 
   653 
   651    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
   654    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
   652       SDL_SetError("Error setting gain.");
   655       SDL_SetError("Error setting gain: %s", strerror(errno));
   653       return -1;
   656       return -1;
   654    }
   657    }
   655 
   658 
   656    return 0;
   659    return 0;
   657 }
   660 }
   668    ie.type = EV_FF;
   671    ie.type = EV_FF;
   669    ie.code = FF_AUTOCENTER;
   672    ie.code = FF_AUTOCENTER;
   670    ie.value = (0xFFFFUL * autocenter) / 100;
   673    ie.value = (0xFFFFUL * autocenter) / 100;
   671 
   674 
   672    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
   675    if (write(haptic->hwdata->fd, &ie, sizeof(ie)) < 0) {
   673       SDL_SetError("Error setting autocenter.");
   676       SDL_SetError("Error setting autocenter: %s", strerror(errno));
   674       return -1;
   677       return -1;
   675    }
   678    }
   676 
   679 
   677    return 0;
   680    return 0;
   678 }
   681 }