Kernel

訪問evdev設備的抓取狀態

  • January 7, 2019

我需要在程序中檢索 evdev 設備的抓取狀態。更具體地說,我需要在此處看到grab的結構中檢索指針的狀態: https://elixir.bootlin.com/linux/v4.20/source/drivers/input/evdev.c#L42(如果它是 NULL或不為空)。這在使用者空間是否完全有可能,例如通過使用 /dev/(k)mem 讀取核心記憶體,還是我需要編寫核心模組?我應該從哪裡開始?evdev

判斷一個設備目前是否被抓取,從使用者空間,可以自己嘗試抓取;要麼使用EVIOCGRAB ioctl你自己,要麼libevdev_grablibevdev

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 設備的打開文件描述符。)

引用自:https://unix.stackexchange.com/questions/492909