[英]UINPUT device program in C for Ubuntu 14.04 does not work. Why?
I am using Ubuntu 14.04, and I am setting up a virtual keyboard in c, which requires uinput to work. 我正在使用Ubuntu 14.04,并且正在c中设置虚拟键盘,这需要uinput才能工作。
I had this root privilege problem: 我有这个root特权问题:
ls -l /dev/uinput
crw-rw-rw- 1 root root 10, 223 Feb 25 11:11 /dev/uinput
But I solved it by creating a new rule for this. 但是我为此创建了一条新规则来解决它。 It now prints:
现在可以打印:
ls -l /dev/uinput
crw-rw---- 1 root uinput 10, 223 Feb 26 21:11 /dev/uinput
This however still doesn't solve my problem and my program still doesn't work, even with sudo. 但是,这仍然不能解决我的问题,即使使用sudo,我的程序仍然无法正常工作。
Here's my code: 这是我的代码:
int main()
{
int uinp_fd;
int i;
uinp_fd = open("/dev/uinput", O_WRONLY|O_NDELAY);
if(uinp_fd < 0)
{
printf("Unable to open /dev/uinput\n");
return -1;
}
else printf("Can open /dev/uinput\n");
/********* Setup input device structure section: ***********/
memset(&uinp,0,sizeof(uinp));
snprintf(uinp.name, UINPUT_MAX_NAME_SIZE, "The C Keyboard");
uinp.id.bustype = BUS_USB;
uinp.id.version = 1;
uinp.id.vendor = 0x1234;
uinp.id.product = 0xfedc;
write(uinp_fd, &uinp, sizeof(uinp));
/****** Setup the uinput keyboard device section: **********/
ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
ioctl(uinp_fd, UI_SET_EVBIT, EV_SYN);
ioctl(uinp_fd, UI_SET_EVBIT, EV_REP);
ioctl(uinp_fd, UI_SET_KEYBIT, KEY_A);
ioctl(uinp_fd, UI_DEV_CREATE, NULL);
if (ioctl(uinp_fd, UI_DEV_CREATE, NULL) < 0)
{
printf("Unable to create UINPUT device.\n");
return -1;
}
My source code continues on further from there. 我的源代码从此继续。
However, the program always prints "Unable to create UINPUT device.". 但是,程序始终打印“无法创建UINPUT设备”。
sudo command to run my program also doesn't work and the same error message is printed. 用于运行我的程序的sudo命令也无法正常工作,并且会显示相同的错误消息。 What should I do to get the program to work?
我应该怎么做才能使程序正常工作? Thanks.
谢谢。
ioctl(uinp_fd, UI_DEV_CREATE, NULL);
if (ioctl(uinp_fd, UI_DEV_CREATE, NULL) < 0)
{
printf("Unable to create UINPUT device.\n");
return -1;
}
You have invoked the UI_DEV_CREATE ioctl twice. 您已两次调用UI_DEV_CREATE ioctl。 The first call will succeed and the second will fail.
第一个电话将成功,第二个电话将失败。 So just delete the first call.
因此,只需删除第一个电话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.