I need to detect device of certain type (e.g. mouse) and catch its events in linux with daemon.
Usage of Xlib
seems useless because Xlib
can only catch an event with window created by application but not all system events (but if I am wrong, please correct me).
One of solutions was using of /dev/input/eventX
files (where X
is number). In command line with ls -l by-id
I can detect which event file handles certain device.
Further googling led me to libevdev. It is actually wrapper around system calls (you even have to obtain file descriptor of event file manually before handling this file with libevdev) but it can also find out types and codes of events that this device can emit before these events are emitted. But I can't use it because there is some difficulties with embedding it onto destination computers.
So the question is: can I do this with C/C++ without parsing of ls -l by-id
output? I mean can I detect type of device binded to certain /dev/input/eventX
file or at least get types of events that it can emit with only system calls?
And if there any solutions for detecting device events in linux else?
Thanks in advance.
=======
[UPD]: Another solution in addition to one that given by @AlejandroDiaz in comments to this post (by the way, did not found how to paste reference to certain comment) without using libevdev
is reading event bits with ioctl and parsing them like this:
int c_iHd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK);
if(c_iHd == -1)
{
exit(1);
}
unsigned long c_uEventBits;
ioctl(c_iHd, EVIOCGBIT(0, EV_MAX), &c_uEventBits);
int hasButtonsEvent = (c_uEventBits >> EV_KEY) & 1;
close(c_iHd);
Verbose solution is described in this thread.
The core of libevdev is relatively simple and not much more than a read() that reads in a set of struct input_event. Those are defined in linux/input.h and you can get type, code and value from those.
Most of what libevdev provides is type-safety so you can't screw up the ioctls and some of the corner cases of event handling (most of them to do with multitouch).
If your need is fairly simple and straightforward, writing the code directly will work just as well, especially if even static linking isn't an option. All you need then is open(), read() and ioctl().