uinput 工作的填坑记录:
用uinput 键盘支持都不错,没废事。但鼠标滚轮事件死活不对。折腾半天,最终搞定,上关键代码
-
设备配置:
int create_uinput(void)
{
int i;
int ret = 0;uinput_fd = open("/dev/uinput", O_RDWR | O_NDELAY);
if(uinput_fd < 0){
printf("%s:%d\n", func, LINE);
return -1;//error process.
}//to set uinput dev
memset(&uinput_dev, 0, sizeof(struct uinput_user_dev));
snprintf(uinput_dev.name, UINPUT_MAX_NAME_SIZE, “uinput-custom-dev”);
uinput_dev.id.version = 1;
uinput_dev.id.bustype = BUS_VIRTUAL;ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN);
ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY); // kbd press
// ioctl(uinput_fd, UI_SET_EVBIT, EV_REP); // kbd repeat report
ioctl(uinput_fd, UI_SET_EVBIT, EV_REL); // kbd releasefor(i = 0; i < 256; i++){
ioctl(uinput_fd, UI_SET_KEYBIT, i);
}ioctl(uinput_fd, UI_SET_RELBIT, REL_X); // mouse x
ioctl(uinput_fd, UI_SET_RELBIT, REL_Y); // mouse y
ioctl(uinput_fd,UI_SET_RELBIT, REL_WHEEL);ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOUCH);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_FORWARD);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_BACK);ret = write(uinput_fd, &uinput_dev, sizeof(struct uinput_user_dev));
if(ret < 0){
printf("%s:%d\n", func, LINE);
return ret;//error process.
}ret = ioctl(uinput_fd, UI_DEV_CREATE);
if(ret < 0){
printf("%s:%d\n", func, LINE);
close(uinput_fd);
return ret;//error process.
}
} -
上报鼠标滚轮事件:
void mouse_wheel(short wheel_value){
struct input_event event;
/* Move pointer to (0,0) location */
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = EV_REL;
event.code = REL_WHEEL;
event.value = wheel_value;
write(uinput_fd, &event, sizeof(event));
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(uinput_fd, &event, sizeof(event));
}