Linux uinput : simple example?

标签 linux uinput

我在使用 uinput 使代码的两侧 工作时遇到了一些问题。

基于 Getting started with uinput: the user level input subsystem [死链接; archived ] 我整理了以下编写器(减去错误处理):

int main(int ac, char **av)
{
    int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    int ret = ioctl(fd, UI_SET_EVBIT, EV_ABS);
    ret = ioctl(fd, UI_SET_ABSBIT, ABS_X);

    struct uinput_user_dev uidev = {0};
    snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-rotary");
    uidev.absmin[ABS_X] = 0;
    uidev.absmax[ABS_X] = 255;
    ret = write(fd, &uidev, sizeof(uidev));
    ret = ioctl(fd, UI_DEV_CREATE);

    struct input_event ev = {0};
    ev.type = EV_ABS;
    ev.code = ABS_X;
    ev.value = 42;

    ret = write(fd, &ev, sizeof(ev));

    getchar();

    ret = ioctl(fd, UI_DEV_DESTROY);
    return EXIT_SUCCESS;
}

这似乎可行,至少完整的 input_event 结构似乎已被写入。

然后我写了我能想到的最天真的读者事件:

int main(int ac, char **av)
{
    int fd = open(av[1], O_RDONLY);

    char name[256] = "unknown";
    ioctl(fd, EVIOCGNAME(sizeof(name)), name);
    printf("reading from %s\n", name);

    struct input_event ev = {0};
    int ret = read(fd, &ev, sizeof(ev));
    printf("Read an event! %i\n", ret);
    printf("ev.time.tv_sec: %li\n", ev.time.tv_sec);
    printf("ev.time.tv_usec: %li\n", ev.time.tv_usec);
    printf("ev.type: %hi\n", ev.type);
    printf("ev.code: %hi\n", ev.code);
    printf("ev.value: %li\n", ev.value);

    return EXIT_SUCCESS;
}

不幸的是,阅读器端根本不起作用;每次只能读取 8 个字节,这几乎不是一个完整的 input_event 结构。

我犯了什么愚蠢的错误?

最佳答案

您还应该在实际事件之后编写一个同步事件。在您的作者端代码中:

struct input_event ev = {0};
ev.type = EV_ABS;
ev.code = ABS_X;
ev.value = 42;

usleep(1500);

memset(&ev, 0, sizeof(ev));
ev.type = EV_SYN;
ev.code = 0;
ev.value = 0;

ret = write(fd, &ev, sizeof(ev));

getchar();

关于Linux uinput : simple example?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693280/

相关文章:

c - evdev如何判断是否移动x11光标?

python - 使用与 Libinput 接口(interface)的 Python 创建宏

c++ - 为什么我的析构函数仅在其父析构函数调用后才使我的代码崩溃?

linux - 为什么 Linux 内核的 sysfs API 中的存储函数需要返回使用的字节?

linux - find 没有考虑变量

c - 在 linux 中使用 C 和 uinput 库模拟击键

c++ - uinput在wayland/weston下模拟LWIN + S

php - Cron Job - 用主文件递归替换文件实例(timthumb 漏洞利用预防)

c - 无法理解 select() 系统调用

linux - uinput虚拟设备和/dev/input/mice