Skip to content

Instantly share code, notes, and snippets.

@TheBrokenRail
Created September 3, 2020 23:11
Show Gist options
  • Save TheBrokenRail/587f1b9ba74907e633bee1edda020cf4 to your computer and use it in GitHub Desktop.
Save TheBrokenRail/587f1b9ba74907e633bee1edda020cf4 to your computer and use it in GitHub Desktop.
#include <linux/input.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#define LENGTH 250
int msleep(long msec) {
struct timespec ts;
int res;
if (msec < 0) {
errno = EINVAL;
return -1;
}
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
do {
res = nanosleep(&ts, &ts);
} while (res && errno == EINTR);
return res;
}
int main() {
int fd = open("/dev/input/by-path/platform-vibrator-event", O_RDWR);
if (fd == -1) {
return 1;
}
int rc = 0;
struct input_event stop;
struct ff_effect effect;
effect.type = FF_RUMBLE;
effect.id = -1;
effect.u.rumble.strong_magnitude = 0;
effect.u.rumble.weak_magnitude = 0xFFFF;
effect.replay.length = LENGTH;
effect.replay.delay = 0;
if (ioctl(fd, EVIOCSFF, &effect) == -1) {
rc = 1;
goto exit;
}
struct input_event play;
/* Play effect */
play.type = EV_FF;
play.code = effect.id;
play.value = 1;
if (write(fd, (const void *) &play, sizeof (play)) == -1) {
rc = 1;
goto free;
}
msleep(LENGTH);
free:
if (ioctl(fd, EVIOCRMFF, effect.id) == -1) {
rc = 1;
goto exit;
}
exit:
close(fd);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment