diff --git a/driver_loops.c b/driver_loops.c index bb5216a..92821b4 100644 --- a/driver_loops.c +++ b/driver_loops.c @@ -1027,10 +1027,7 @@ static void *uinput_poll_loop(void *arg){ upload.request_id = e.value; ioctl(loop_context->uinput_fd, UI_BEGIN_FF_UPLOAD, &upload); pthread_mutex_lock(&loop_context->device_mutex); - upload.retval = lg4ff_upload_effect(&loop_context->ffb_device, &upload.effect, &upload.old, loop_context->context.log_effects); - if(loop_context->context.play_on_upload && upload.retval == 0){ - lg4ff_play_effect(&loop_context->ffb_device, upload.effect.id, 1, loop_context->context.log_effects); - } + upload.retval = lg4ff_upload_effect(&loop_context->ffb_device, &upload.effect, &upload.old, loop_context->context.log_effects, loop_context->context.play_on_upload); pthread_mutex_unlock(&loop_context->device_mutex); ioctl(loop_context->uinput_fd, UI_END_FF_UPLOAD, &upload); break; diff --git a/force_feedback.c b/force_feedback.c index 519e2bd..7472534 100644 --- a/force_feedback.c +++ b/force_feedback.c @@ -43,7 +43,37 @@ uint64_t get_time_ms(){ return tp.tv_nsec / 1000000 + tp.tv_sec * 1000; } -int lg4ff_upload_effect(struct lg4ff_device *device, struct ff_effect *effect, struct ff_effect *old, bool log) +int lg4ff_play_effect(struct lg4ff_device *device, int effect_id, int value, bool log) +{ + struct lg4ff_effect_state *state; + uint64_t now = get_time_ms(); + + state = &device->states[effect_id]; + + if (value > 0) { + if (test_bit(FF_EFFECT_STARTED, &state->flags)) { + STOP_EFFECT(state); + } else { + device->effects_used++; + } + __set_bit(FF_EFFECT_STARTED, &state->flags); + state->start_at = now; + state->count = value; + if(log){ + STDOUT("--play--\n"); + log_effect(&state->effect); + } + } else { + if (test_bit(FF_EFFECT_STARTED, &state->flags)) { + STOP_EFFECT(state); + device->effects_used--; + } + } + + return 0; +} + +int lg4ff_upload_effect(struct lg4ff_device *device, struct ff_effect *effect, struct ff_effect *old, bool log, bool play_on_upload) { struct lg4ff_effect_state *state; uint64_t now = get_time_ms(); @@ -70,34 +100,8 @@ int lg4ff_upload_effect(struct lg4ff_device *device, struct ff_effect *effect, s log_effect(effect); } - return 0; -} - -int lg4ff_play_effect(struct lg4ff_device *device, int effect_id, int value, bool log) -{ - struct lg4ff_effect_state *state; - uint64_t now = get_time_ms(); - - state = &device->states[effect_id]; - - if (value > 0) { - if (test_bit(FF_EFFECT_STARTED, &state->flags)) { - STOP_EFFECT(state); - } else { - device->effects_used++; - } - __set_bit(FF_EFFECT_STARTED, &state->flags); - state->start_at = now; - state->count = value; - if(log){ - STDOUT("--play--\n"); - log_effect(&state->effect); - } - } else { - if (test_bit(FF_EFFECT_STARTED, &state->flags)) { - STOP_EFFECT(state); - device->effects_used--; - } + if(play_on_upload && !test_bit(FF_EFFECT_PLAYING, &state->flags)){ + lg4ff_play_effect(device, effect->id, 1, log); } return 0; diff --git a/force_feedback.h b/force_feedback.h index 12edd8e..adff174 100644 --- a/force_feedback.h +++ b/force_feedback.h @@ -176,7 +176,7 @@ struct lg4ff_device{ }; void lg4ff_init_slots(struct lg4ff_device *device); -int lg4ff_upload_effect(struct lg4ff_device *device, struct ff_effect *effect, struct ff_effect *old, bool log); +int lg4ff_upload_effect(struct lg4ff_device *device, struct ff_effect *effect, struct ff_effect *old, bool log, bool play_on_upload); int lg4ff_play_effect(struct lg4ff_device *device, int effect_id, int value, bool log); int lg4ff_timer(struct lg4ff_device *device);