0

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
ankebut
  • 21
  • 4
  • By the way, I didn't learn yet what the concept of ioctl with different input devices. I mean there are /dev/input/event0 and /dev/input/js0. When I want to use ioctl both of them are different and because of this ioctl cannot accepts the same struct to write input0 and js0. I think they aren't same concepts. and secondly my question is different from flush buffer of keyboard, terminal or else because the device is logitech gamewheel. Because of this there should be something diffent for clear buffer. It uses usb and game drivers. – ankebut Jan 10 '19 at 17:23
  • Why don't you just read it and throw it away? – that other guy Jan 10 '19 at 17:44
  • This just happens when I first plug usb to linux computer. If usb is already pluged so when I say to calibrate wheel to center it will not do anything because already configured so there is no any garbage data. My application is working as linux service. Just only one time the game wheel need to calibrate itself to center. So how can I say throw data away? This will not be logical because we don't know is the incoming data garbage or not. What I need is just say to input system clear buffer. – ankebut Jan 11 '19 at 05:03

0 Answers0