uinput 在 wayland/weston 下模拟 LWIN + S

uinput emulate LWIN + S under wayland/weston

本文关键字:模拟 LWIN weston wayland uinput      更新时间:2023-10-16

我正在尝试模拟 wayland 下的左窗口键 (Super( + S 的组合键以自动执行屏幕截图(此组合键可以正确启动 weston-screenshooter,将屏幕截图保存到主目录,但无法从终端执行(。 我发现了使用 uinput 来做到这一点,我找到了一些例子并提出了这个:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>
#define die(str) do { 
perror(str); 
exit(EXIT_FAILURE); 
} while(0)
void emitEvent(int fd, uint16_t type, uint16_t code, int val)
{
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
/* timestamp values below are ignored */
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}
#define UI_DEV_SETUP _IOW(UINPUT_IOCTL_BASE, 3, struct uinput_setup)
int main(int argc, char *argv[])
{
int                    fd;
struct uinput_user_dev uidev;
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if(fd < 0)
die("error: open");
if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, KEY_S) < 0)
die("error: ioctl");
if(ioctl(fd, UI_SET_KEYBIT, 133) < 0)
die("error: ioctl");
memset(&uidev, 0, sizeof(uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
uidev.id.bustype = BUS_USB;
uidev.id.vendor  = 0x1234; // random stuff
uidev.id.product = 0x6433;
uidev.id.version = 1;
if(write(fd, &uidev, sizeof(uidev)) < 0)
die("error: write");
if(ioctl(fd, UI_DEV_CREATE) < 0)
die("error: ioctl");
sleep(1);
// srand(time(NULL));
emitEvent(fd, EV_KEY, 133, 1);
emitEvent(fd, EV_KEY, KEY_S, 1);
emitEvent(fd, EV_SYN, SYN_REPORT, 0);
usleep(300);
emitEvent(fd, EV_KEY, KEY_S, 0);
emitEvent(fd, EV_KEY, 133, 0);
emitEvent(fd, EV_SYN, SYN_REPORT, 0);
sleep(1);
if(ioctl(fd, UI_DEV_DESTROY) < 0)
die("error: ioctl");
close(fd);
return 0;
}

我最大的问题是我不知道哪个KEY_是左赢/左超级键,键定义根本没有向我显示任何与 win 或超级或mod_s相关的内容,在 x11 上使用 xev 我发现 LWIN 应该是 133,但我认为这是不正确的,因为代码中的 KEY_S = 31,但在 xev 中它是 39。

运行应用程序时,我可以看到设备正在系统中注册:

[ 6278.405013] input: uinput-sample as /devices/virtual/input/input23
[14:02:17.556] event2  - [14:02:17.556] uinput-sample: [14:02:17.556] is tagged by udev as: Keyboard
[14:02:17.556] event2  - [14:02:17.557] uinput-sample: [14:02:17.557] device is a keyboard
[14:02:19.549] event2  - [14:02:19.550] uinput-sample: [14:02:19.550] device removed

但除此之外什么都没有发生。

左窗口键的正确键定义是什么?上面的代码对于发送组合键(一次按下 2 个键(是否有效?甚至有可能实现我想要的吗?我在系统中具有完全的根访问权限。

我实际上在发布此内容后几分钟就发现了它!它有效!

密钥代码KEY_LEFTMETA,我使用 evtest 包找到了它。截图制作成功。