From 606015da82fb21ac5e0d8c154ade1114db3a96ce Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 26 Nov 2023 18:22:31 +0100 Subject: [PATCH] Set the gain to 75% before emitting rumbles --- input_dev.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/input_dev.c b/input_dev.c index 513bbc7..1162794 100644 --- a/input_dev.c +++ b/input_dev.c @@ -542,8 +542,6 @@ static void input_udev( } }; - const int has_ff = libevdev_has_event_type(ctx->dev, EV_FF); - // start the incoming events read thread pthread_t incoming_events_thread; const int incoming_events_thread_creation = pthread_create(&incoming_events_thread, NULL, input_read_thread_func, (void*)ctx); @@ -552,6 +550,25 @@ static void input_udev( continue; } + const int has_ff = libevdev_has_event_type(ctx->dev, EV_FF); + + if (has_ff) { + printf("Setting master gain to 75%%...\n"); + + const struct input_event gain = { + .type = EV_FF, + .code = FF_GAIN, + .value = 0xC000, // [0, 0xFFFF]) + }; + + const int gain_set_res = write(fd, (const void*)&gain, sizeof(gain)); + if (gain_set_res != sizeof(gain)) { + fprintf(stderr, "Unable to adjust gain for force-feedback: %d\n", gain_set_res); + } + } else { + fprintf(stderr, "Unable to adjust gain for force-feedback: EV_FF not supported.\n"); + } + // while the incoming events thread run... while ((ctx->flags & INPUT_CTX_FLAGS_READ_TERMINATED) == 0) { if (has_ff) { @@ -572,7 +589,7 @@ static void input_udev( }; const int rumble_stop_res = write(fd, (const void*) &rumble_stop, sizeof(rumble_stop)); - if (rumble_stop_res < 0) { + if (rumble_stop_res != sizeof(rumble_stop)) { fprintf(stderr, "Unable to stop the previous rumble: %d\n", rumble_stop_res); } } @@ -580,7 +597,7 @@ static void input_udev( current_effect.u.rumble.strong_magnitude = rumble_msg->strong_magnitude; current_effect.u.rumble.weak_magnitude = rumble_msg->weak_magnitude; - printf("Rumble strong_magnitude: %u, weak_magnitude: %u\n", (unsigned)current_effect.u.rumble.strong_magnitude, (unsigned)current_effect.u.rumble.weak_magnitude); + printf("Rumble event received -- strong_magnitude: %u, weak_magnitude: %u\n", (unsigned)current_effect.u.rumble.strong_magnitude, (unsigned)current_effect.u.rumble.weak_magnitude); const int effect_upload_res = ioctl(fd, EVIOCSFF, ¤t_effect); if (effect_upload_res == 0) { @@ -590,8 +607,10 @@ static void input_udev( .value = 1, }; - const int effect_start_res = write(fd, (const void*) &rumble_play, sizeof(rumble_play)); - if (effect_start_res < 0) { + const int effect_start_res = write(fd, (const void*)&rumble_play, sizeof(rumble_play)); + if (effect_start_res == sizeof(rumble_play)) { + printf("Rumble effect play requested to driver\n"); + } else { fprintf(stderr, "Unable to write input event starting the rumble: %d\n", effect_start_res); } } else {