gpt4 book ai didi

Linux uinput : simple example?

转载 作者:IT王子 更新时间:2023-10-29 01:14:23 27 4
gpt4 key购买 nike

我在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com