Please execuse me for my english I am not native.
Linux input device: /dev/input/event0 or /dev/input/by-id/usb-Logitech_G29_Driving_Force_Racing_Wheel-event-joystick
Problem: I don’t want to read buffered data.
Dear Stackoverflow,
1 - I opened an input device on linux.
2 - I Wrote some code to turn the device to left or back to center. (It is game wheel so when I open the application I have to be sure wheel is positioned on center)
3 - I closed file descriptors and open exactly the same device for not read old buffered data.
4 - When I want to get data with read function, I am getting old buffered data which it got when I turn device to center. (First position is 0 when I want to turn wheel to center it changes to 35000 from 0)
So how can I clear buffered data without writing second application? Because if I wrote two application which first one is set device wheel to center and second one is for getting data, so everyhing is okey. I mean if I close file descriptor from different application linux clears data but if I try this on same application there are buffers.
What I tried to clear buffer:
- Firstly I tried to search on google, stackoverflow and read almost everthing.
- fsync()
- fflush(stdin) and fflush(stdout)
- ioctl(fd, I_FLUSH, FLUSHRW)
- tcflush(fd, TCIOFLUSH)
- close(fd) before read. (I think it is because of the same application, linux gives me same file descriptor. If i exit from program and run again so there is no buffer)
$ gcc -o test_prog test.c && ./test_prog
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <strings.h>
#include <stropts.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
// set the wheel position to center of it
void set_autocenter(int fd, int autocenter) {
struct input_event ie;
ie.type = EV_FF;
ie.code = FF_AUTOCENTER;
ie.value = 0xFFFFUL * autocenter / 100;
if (write(fd, &ie, sizeof(ie)) == -1) {
perror("error set_auto_center");
}
}
int main(int argc, char *argv[]) {
int fd_joystick;
struct input_event ev = {0};
const char *device_wheel = "/dev/input/by-id/usb-Logitech_G29_Driving_Force_Racing_Wheel-event-joystick";
if ((fd_joystick = open(device_wheel, O_RDWR)) < 0) {
perror("device could't opened.");
return 0;
}
set_autocenter(fd_joystick, 100);
while (1) {
/* Problem starts from here
* When I first call read function it gives me garbage data which gets from
* function of set_autocenter. Because this function changes the direction of wheel
*/
if ( read(fd_joystick, &ev, sizeof(struct input_event)) == sizeof(struct input_event)) {
printf("Event Type: %d Event Code: %d Event Value: %d\n", ev.type, ev.code, ev.value);
}
}
return 1;
}
$ cat /proc/bus/input/devices
I: Bus=0003 Vendor=046d Product=c24f Version=0111
N: Name="Logitech G29 Driving Force Racing Wheel"
P: Phys=usb-0000:00:14.0-11/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb1/1-11/1-11:1.0/0003:046D:C24F.0040/input/input61
U: Uniq=
H: Handlers=event0 js0
B: PROP=0
B: EV=20001b
B: KEY=1ff 0 0 0 0 0 0 ffff00000000 0 0 0 0
B: ABS=30027
B: MSC=10
B: FF=300040000 0