This repository has no description
2.0 kB
83 lines
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <linux/input.h>
7#include <sys/ioctl.h>
8
9int main(int argc, char *argv[]) {
10 int fd;
11 struct ff_effect effect;
12 struct input_event play, stop;
13 int duration_ms = 500;
14 int strength = 0xFFFF;
15 const char *device = "/dev/input/event2";
16
17 // Parse arguments
18 if (argc > 1) {
19 duration_ms = atoi(argv[1]);
20 }
21 if (argc > 2) {
22 strength = (atoi(argv[2]) * 0xFFFF) / 100;
23 if (strength > 0xFFFF) strength = 0xFFFF;
24 }
25 if (argc > 3) {
26 device = argv[3];
27 }
28
29 // Open device
30 fd = open(device, O_RDWR);
31 if (fd < 0) {
32 perror("Failed to open device");
33 fprintf(stderr, "Usage: %s [duration_ms] [strength_percent] [device]\n", argv[0]);
34 return 1;
35 }
36
37 // Setup rumble effect
38 memset(&effect, 0, sizeof(effect));
39 effect.type = FF_RUMBLE;
40 effect.id = -1;
41 effect.u.rumble.strong_magnitude = strength;
42 effect.u.rumble.weak_magnitude = strength / 2;
43 effect.replay.length = duration_ms;
44 effect.replay.delay = 0;
45
46 // Upload effect
47 if (ioctl(fd, EVIOCSFF, &effect) < 0) {
48 perror("Failed to upload effect");
49 close(fd);
50 return 1;
51 }
52
53 // Play effect
54 memset(&play, 0, sizeof(play));
55 play.type = EV_FF;
56 play.code = effect.id;
57 play.value = 1;
58
59 if (write(fd, &play, sizeof(play)) < 0) {
60 perror("Failed to play effect");
61 ioctl(fd, EVIOCRMFF, effect.id);
62 close(fd);
63 return 1;
64 }
65
66 printf("Rumble playing for %d ms at %d%% strength\n", duration_ms, (strength * 100) / 0xFFFF);
67
68 // Wait for effect to finish
69 usleep(duration_ms * 1000);
70
71 // Stop effect
72 memset(&stop, 0, sizeof(stop));
73 stop.type = EV_FF;
74 stop.code = effect.id;
75 stop.value = 0;
76 write(fd, &stop, sizeof(stop));
77
78 // Cleanup
79 ioctl(fd, EVIOCRMFF, effect.id);
80 close(fd);
81
82 return 0;
83}