src/haptic/linux/SDL_syshaptic.c
branchgsoc2008_force_feedback
changeset 2480 b883974445fc
parent 2479 b9eb2cfe16cd
child 2481 5d0ea4576f20
equal deleted inserted replaced
2479:b9eb2cfe16cd 2480:b883974445fc
    33 #include <sys/types.h>
    33 #include <sys/types.h>
    34 #include <sys/stat.h>
    34 #include <sys/stat.h>
    35 #include <fcntl.h>
    35 #include <fcntl.h>
    36 #include <linux/limits.h>
    36 #include <linux/limits.h>
    37 #include <string.h>
    37 #include <string.h>
       
    38 #include <errno.h>
    38 
    39 
    39 
    40 
    40 #define MAX_HAPTICS  32
    41 #define MAX_HAPTICS  32
    41 
    42 
    42 
    43 
   272 
   273 
   273 /*
   274 /*
   274  * Initializes the linux effect struct from a haptic_effect.
   275  * Initializes the linux effect struct from a haptic_effect.
   275  */
   276  */
   276 static int
   277 static int
   277 SDL_SYS_ToFFEffect( struct ff_effect * dest, struct haptic_effect * src )
   278 SDL_SYS_ToFFEffect( struct ff_effect * dest, SDL_HapticEffect * src )
   278 {
   279 {
       
   280    SDL_HapticConstant *constant;
       
   281    SDL_HapticPeriodic *periodic;
       
   282 
       
   283    /* Clear up */
   279    SDL_memset(dest, 0, sizeof(struct ff_effect));
   284    SDL_memset(dest, 0, sizeof(struct ff_effect));
   280    switch (src->effect.type) {
   285 
       
   286    switch (src->type) {
   281       case SDL_HAPTIC_CONSTANT:
   287       case SDL_HAPTIC_CONSTANT:
       
   288          constant = &src->constant;
       
   289 
       
   290          /* Header */
   282          dest->type = FF_CONSTANT;
   291          dest->type = FF_CONSTANT;
       
   292          dest->direction = constant->direction;
       
   293 
       
   294          /* Replay */
       
   295          dest->replay.length = constant->length;
       
   296          dest->replay.delay = constant->delay;
       
   297 
       
   298          /* Trigger */
       
   299          dest->trigger.button = constant->button;
       
   300          dest->trigger.interval = constant->interval;
       
   301 
       
   302          /* Constant */
       
   303          dest->u.constant.level = constant->level;
       
   304 
       
   305          /* Envelope */
       
   306          dest->u.constant.envelope.attack_length = constant->attack_length;
       
   307          dest->u.constant.envelope.attack_level = constant->attack_level;
       
   308          dest->u.constant.envelope.fade_length = constant->fade_length;
       
   309          dest->u.constant.envelope.fade_level = constant->fade_level;
       
   310 
       
   311          break;
       
   312 
       
   313       case SDL_HAPTIC_PERIODIC:
       
   314          periodic = &src->periodic;
       
   315 
       
   316          /* Header */
       
   317          dest->type = FF_PERIODIC;
       
   318          dest->direction = periodic->direction;
       
   319          
       
   320          /* Replay */
       
   321          dest->replay.length = periodic->length;
       
   322          dest->replay.delay = periodic->delay;
       
   323          
       
   324          /* Trigger */
       
   325          dest->trigger.button = periodic->button;
       
   326          dest->trigger.interval = periodic->interval;
       
   327          
       
   328          /* Constant */
       
   329          dest->u.periodic.waveform = periodic->waveform;
       
   330          dest->u.periodic.period = periodic->period;
       
   331          dest->u.periodic.magnitude = periodic->magnitude;
       
   332          dest->u.periodic.offset = periodic->offset;
       
   333          dest->u.periodic.phase = periodic->phase;
       
   334          
       
   335          /* Envelope */
       
   336          dest->u.periodic.envelope.attack_length = periodic->attack_length;
       
   337          dest->u.periodic.envelope.attack_level = periodic->attack_level;
       
   338          dest->u.periodic.envelope.fade_length = periodic->fade_length;
       
   339          dest->u.periodic.envelope.fade_level = periodic->fade_level;
   283 
   340 
   284          break;
   341          break;
   285 
   342 
   286       default:
   343       default:
   287          SDL_SetError("Unknown haptic effect type.");
   344          SDL_SetError("Unknown haptic effect type.");
   293 
   350 
   294 /*
   351 /*
   295  * Creates a new haptic effect.
   352  * Creates a new haptic effect.
   296  */
   353  */
   297 int
   354 int
   298 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect)
   355 SDL_SYS_HapticNewEffect(SDL_Haptic * haptic, struct haptic_effect * effect,
   299 {
   356       SDL_HapticEffect * base)
       
   357 {
       
   358    struct ff_effect * linux_effect;
       
   359 
   300    /* Allocate the hardware effect */
   360    /* Allocate the hardware effect */
   301    effect->hweffect = (struct haptic_hweffect *) 
   361    effect->hweffect = (struct haptic_hweffect *) 
   302          SDL_malloc(sizeof(struct haptic_hweffect));
   362          SDL_malloc(sizeof(struct haptic_hweffect));
   303    if (effect->hweffect == NULL) {
   363    if (effect->hweffect == NULL) {
   304       SDL_OutOfMemory();
   364       SDL_OutOfMemory();
   305       return -1;
   365       return -1;
   306    }
   366    }
   307 
   367 
   308    /* Prepare the ff_effect */
   368    /* Prepare the ff_effect */
   309    if (SDL_SYS_ToFFEffect( &effect->hweffect->effect, effect ) != 0) {
   369    linux_effect = &effect->hweffect->effect;
       
   370    if (SDL_SYS_ToFFEffect( linux_effect, base ) != 0) {
   310       return -1;
   371       return -1;
   311    }
   372    }
   312    effect->hweffect->effect.id = -1; /* Have the kernel give it an id */
   373    linux_effect->id = -1; /* Have the kernel give it an id */
   313 
   374 
   314    /* Upload the effect */
   375    /* Upload the effect */
   315    if (ioctl(haptic->hwdata->fd, EVIOCSFF, &effect->hweffect->effect) < 0) {
   376    if (ioctl(haptic->hwdata->fd, EVIOCSFF, linux_effect) < 0) {
   316       SDL_SetError("Error uploading effect to the haptic device.");
   377       SDL_SetError("Error uploading effect to the haptic device: %s",
       
   378             strerror(errno));
   317       return -1;
   379       return -1;
   318    }
   380    }
   319 
   381 
   320    return 0;
   382    return 0;
   321 }
   383 }