我在网上几乎没有找到关于使用 libevdev 发送绝对事件 (EV_ABS) 的示例。
我能够创建一个设备并发送一些击键,但绝对事件没有运气。
该设备显示,sudo libinput list-devices
但当我尝试记录事件时,sudo evemu-record
没有显示任何内容。
这是我的测试代码:
#include <iostream>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libevdev-1.0/libevdev/libevdev-uinput.h>
#include <unistd.h>
int main() {
int err;
struct libevdev *dev;
struct libevdev_uinput *uidev;
struct input_absinfo absinfo{
.value = 0,
.minimum = 0,
.maximum = 12372,
.fuzz = 0,
.flat = 0,
.resolution = 40
};
dev = libevdev_new();
libevdev_set_name(dev, "eye tracker pointer");
libevdev_enable_event_type(dev, EV_ABS);
libevdev_enable_event_code(dev, EV_ABS, ABS_X, &absinfo);
libevdev_enable_event_code(dev, EV_ABS, ABS_Y, &absinfo);
err = libevdev_uinput_create_from_device(dev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev);
if (err != 0) return err;
usleep(10000000UL);//s
libevdev_uinput_write_event(uidev, EV_ABS, ABS_X, 100);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_Y, 100);
libevdev_uinput_write_event(uidev, EV_ABS, SYN_REPORT, 0);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_X, 200);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_Y, 200);
libevdev_uinput_write_event(uidev, EV_ABS, SYN_REPORT, 0);
usleep(2000000UL);
libevdev_uinput_destroy(uidev);
return 0;
也许我在 input_absinfo 初始化时做错了什么?
我使用的是 Fedora 36
答案1
错误只是因为我没有以正确的方式调用同步(EV_SYN)。我花了几个小时才意识到。
代码应该是:
libevdev_uinput_write_event(uidev, EV_ABS, ABS_X, 100);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_Y, 100);
libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_X, 200);
libevdev_uinput_write_event(uidev, EV_ABS, ABS_Y, 200);
libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);