1

I'm working on a project that requires me to get a touchscreen working on Scientific Linux 6.4 (Linux kernel 2.6.32). Although the kernel does not support the touchscreen fully, I am able to see multi-touch events being generated in the /dev/input/eventX location for the touchscreen when I touch the screen.

I'm trying to write a simple C++ program to read the data from the /dev/input/eventX file and parse it so I can manually deal with the multi-touch events, since that seems the only way I'll get this working.

So I wrote the following program:

std::ifstream input("/dev/input/event10");
if(input.is_open()) {
    while(input.good()) {
        int header;
        input >> header;

        cout << std::hex << header << " ";

        int data[16] = {};
        for(int i = 0; i < 16; i++) {
            input >> data[i];
            cout << std::hex << data[i] << " ";
        }

        cout << endl;
    }

    input.close();
} else cout << "Unable to open event handler for input polling..." << endl;

Now, I don't exactly know if my method of reading and parsing the input itself is correct, but when I use the following command in bash:

sudo cat /dev/input/event10 | hexdump -C

I get the input data in the form of a number of lines beginning with an 8-digit hex value followed by 16 2-digit hex values (bytes).

The problem I'm having though is that I always get the message "Unable to open event handler for input polling..." suggesting an issue with opening the file. At first, I thought maybe that because nothing is in that file until an event is generated, it might not be able to be opened as an ifstream. I also tried running the program as sudo just in case it was a permissions issue and got the same message, so I believe it has to do with how I'm opening the file.

Does anyone know the proper way to open and read from these files?

EDIT: My question is regarding why the file is unable to be opened, not necessarily just how to parse the data. The suggested "duplicate" questions don't provide any helpful information in this regard.

Darin Beaudreau
  • 375
  • 7
  • 30
  • 1
    Possible duplicate of [Read from /dev/input](https://stackoverflow.com/questions/15949163/read-from-dev-input) – scohe001 May 28 '19 at 15:26
  • Also related: [How do you read the mouse button state from /dev/input/mice?](https://stackoverflow.com/a/23317086/2602718) – scohe001 May 28 '19 at 15:27
  • @scohe001 I also read both of those, but my question is about why the file is unable to be opened, and as both those questions deal with C, not C++, they don't help me here. – Darin Beaudreau May 28 '19 at 15:29
  • You'll need to read the /dev/input/event as a binary file if you want to stick with `ifstream`: https://stackoverflow.com/q/5420317/2602718. Though for something like this, those C answers will work just as well for you in C++... – scohe001 May 28 '19 at 15:33
  • @scohe001 Also tried using the ios::binary | ios::in flags to open it. Same result. – Darin Beaudreau May 28 '19 at 15:36
  • I recommend you to use plain `open` and `read`. `std` is not very suitable for doing such things. At least, with `open` and `read`, you can get the cause of the error by examining `errno`. You can try checking `errno` after calling `is_open`, it may contain the cause of the error. – geza May 28 '19 at 15:39
  • @geza Nevermind. It was something much dumber than that. Couldn't see just from inspecting the program output, but the filename had a trailing space. – Darin Beaudreau May 28 '19 at 15:41
  • 2
    @DarinBeaudreau: OK. If that's the case, I think this question should be closed, SO has a close reason for this very case (simple typographical error). Btw., it is a good thing that if some system call fails, we can print the error to the user. I'm not sure that ifstream can report the error reliably (in a documented way). – geza May 28 '19 at 15:44
  • 1
    Also means your [mcve] wasn't one! – Lightness Races in Orbit May 28 '19 at 16:08
  • Scientific Linux has shut down. – stark May 28 '19 at 16:42
  • @stark I'm aware. My company uses it for one of their products, so that won't change any time soon. – Darin Beaudreau May 28 '19 at 16:57

1 Answers1

0

Nevermind... there was a trailing space in my filename (which was detected rather than hard-coded). I added a trim function and now it opens just fine.

Darin Beaudreau
  • 375
  • 7
  • 30