0

I want to be notified when an USB mouse is disconnected (not just having the read fail). I use epoll with the flags

   EPOLLIN | EPOLLERR | EPOLLRDHUP | EPOLLET

I used

read(fd, struct input_event, sizeof input_event)

I wait for events from the mouse. All is good and working fine, until I click on a mouse button. This generates two events at the same time. One is the EV_MSC/MSC_SCAN event and the other is EV_KEY/BTN_LEFT. If I read only one event (ie. read with buffer of len 24) I get another EPOLL notication and the the read gets the EV_MSC event again. If I read with a buffer of size 48 I get both events.

What is the correct way to handle this case. Should I not keep on reading until I get EAGAIN in the read event handler?

2 Answers2

0

Oops. My bad. Turned out I was reading from a descriptor that had no data (uinput device)

0

The sole difference between level-triggered and edge-triggered is that edge-triggered will only notify you when new data is queued while level-triggered will keep notifying you until you read all the data.

If you're going to use edge triggering, you should make sure to read all the data after you get a notification because you are not guaranteed to get a new notification unless new data arrives. (There are some circumstances where you will get a notification, but it is not guaranteed and so it is an error to rely on it.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278