From 292a26d6ad420f123332622aac6bbd4a721d817d Mon Sep 17 00:00:00 2001 From: Iwan Timmer Date: Sat, 16 Feb 2019 16:12:06 +0100 Subject: [PATCH] Initial attempt to implement rumble for evdev --- src/input/evdev.c | 39 +++++++++++++++++++++++++++++++++++++++ src/input/evdev.h | 1 + src/main.c | 1 + 3 files changed, 41 insertions(+) diff --git a/src/input/evdev.c b/src/input/evdev.c index 96f4c7c..51ba4f9 100644 --- a/src/input/evdev.c +++ b/src/input/evdev.c @@ -63,6 +63,7 @@ struct input_device { char modifiers; __s32 mouseDeltaX, mouseDeltaY, mouseScroll; short controllerId; + int haptic_effect_id; int buttonFlags; char leftTrigger, rightTrigger; short leftStickX, leftStickY; @@ -547,6 +548,7 @@ void evdev_create(const char* device, struct mapping* mappings, bool verbose) { } devices[dev].controllerId = -1; + devices[dev].haptic_effect_id = -1; if (devices[dev].map != NULL) { bool valid = evdev_init_parms(&devices[dev], &(devices[dev].xParms), devices[dev].map->abs_leftx); @@ -704,3 +706,40 @@ void evdev_stop() { void evdev_init() { handler = evdev_handle_event; } + +static struct input_device* evdev_get_input_device(unsigned short controller_id) { + for (int i=0; ihaptic_effect_id >= 0) { + ioctl(device->fd, EVIOCRMFF, device->haptic_effect_id); + device->haptic_effect_id = -1; + } + + if (low_freq_motor == 0 && high_freq_motor == 0) + return; + + struct ff_effect effect = {0}; + effect.type = FF_RUMBLE; + effect.id = -1; + effect.replay.length = USHRT_MAX; + effect.u.rumble.strong_magnitude = low_freq_motor; + effect.u.rumble.weak_magnitude = high_freq_motor; + if (ioctl(device->fd, EVIOCSFF, &effect) == -1) + return; + + struct input_event event = {0}; + event.type = EV_FF; + event.code = effect.id; + write(device->fd, (const void*) &event, sizeof(event)); + device->haptic_effect_id = effect.id; +} diff --git a/src/input/evdev.h b/src/input/evdev.h index 8583a27..7a2e453 100644 --- a/src/input/evdev.h +++ b/src/input/evdev.h @@ -28,3 +28,4 @@ void evdev_init(); void evdev_start(); void evdev_stop(); void evdev_map(char* device); +void evdev_rumble(unsigned short controller_id, unsigned short low_freq_motor, unsigned short high_freq_motor); diff --git a/src/main.c b/src/main.c index 5dc9fd9..8edfe84 100644 --- a/src/main.c +++ b/src/main.c @@ -311,6 +311,7 @@ int main(int argc, char* argv[]) { udev_init(!inputAdded, mappings, config.debug_level > 0); evdev_init(); + rumble_handler = evdev_rumble; #ifdef HAVE_LIBCEC cec_init(); #endif /* HAVE_LIBCEC */