Skip to content

Commit

Permalink
Initial attempt to implement rumble for evdev
Browse files Browse the repository at this point in the history
  • Loading branch information
irtimmer committed Feb 16, 2019
1 parent 0868f02 commit 292a26d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/input/evdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; i<numDevices; i++)
if (devices[i].controllerId == controller_id)
return &devices[i];

return NULL;
}

void evdev_rumble(unsigned short controller_id, unsigned short low_freq_motor, unsigned short high_freq_motor) {
struct input_device* device = evdev_get_input_device(controller_id);
if (!device)
return;

if (device->haptic_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;
}
1 change: 1 addition & 0 deletions src/input/evdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 292a26d

Please sign in to comment.