0

I'm doing some research on the Linux kernel, particularly the input subsystem. I'm interested in reading /dev/input/eventX device(s) for different input events (mainly keyboard and mouse).

However the read() operation blocks. The only thing I can think of is creating a state of all the keyboard keys and mouse buttons, and then create a new thread for reading keyboard and mouse states (those threads might be blocked from time to time), and from my main process, access the state of the keyboard and mouse.

However, I'm not very experienced in non blocking programming under C++ and Linux and I think that a thread for each device might be an overkill.

I'd like to know if there are other ways to handle input in non blocking way, or using threads is fine?

Thanks, skwee.

Dmitry Kudryavtsev
  • 16,354
  • 4
  • 25
  • 32

2 Answers2

1

You can check out the poll system call for this. Is for handling I/O on multiple file descriptors. One possibility would be to spawn only one thread to poll for events on multiple file descriptors.

Here is some reading material : http://www.makelinux.net/ldd3/chp-6-sect-3

LarryPel
  • 286
  • 3
  • 9
1

You can set the file description to non blocking. You can also use select/poll to check to see if data is available to be read in which case you don't need non blocking. See this thread;

Non-blocking call for reading descriptor

Community
  • 1
  • 1
Mike Makuch
  • 1,788
  • 12
  • 15