我需要在程序中检索 evdev 设备的抓取状态。更具体地说,我需要检索此处看到的结构grab
中指针的状态evdev
:https://elixir.bootlin.com/linux/v4.20/source/drivers/input/evdev.c#L42(如果它是 NULL 或非 NULL)。这是否可以从用户空间实现,例如通过使用 /dev/(k)mem 读取内核内存,或者我是否需要编写内核模块?我应该从哪里开始?
答案1
判断某个设备当前是否被抓取,从用户空间,可以尝试自己抓取;要么使用EVIOCGRAB
ioctl
你自己,要么libevdev_grab
在libevdev
:
if (!ioctl(evdevfd, EVIOCGRAB, (void *) 1)) {
// We grabbed the device, no one else had it; release it
ioctl(evdevfd, EVIOCGRAB, (void *) 0);
} else if (errno == EBUSY) {
// The device is busy, someone else has it
} else {
// Something else went wrong
}
或者
struct libevdev *dev;
libevdev_new_from_fd(evdevfd, &dev);
if (!libevdev_grab(dev, LIBEVDEV_GRAB)) {
// We grabbed the device, no one else had it; release it
libevdev_grab(dev, LIBEVDEV_UNGRAB);
} else if (errno == EBUSY) {
// The device is busy, someone else has it
} else {
// Something else went wrong
}
(大量地进行错误处理;evdevfd
是您感兴趣的 evdev 设备的打开文件描述符。)