使用/ dev / uinput模拟Android中的触摸事件无法正常工作

时间:2016-03-14 07:38:43

标签: android touch simulate uinput

我尝试使用/ dev / uinput来模拟Android中的触摸事件,有些代码如下:

首先打开" / dev / uinput"文件,并创建一个udev:

static int open_uinput_device(){
uinp_fd = open(uinput_deivce_path, O_WRONLY | O_NDELAY);
if (uinp_fd <= 0) {
    debug("could not open %s, %s", uinput_deivce_path, strerror(errno));
    return -1;
}

memset(&ui_dev, 0, sizeof(ui_dev));
strncpy(ui_dev.name, "RemoteTV Event", UINPUT_MAX_NAME_SIZE);
ui_dev.id.bustype = BUS_USB;
ui_dev.id.vendor = 0x1341;
ui_dev.id.product = 0x0001;
ui_dev.id.version = 4;

ui_dev.absmin[ABS_MT_SLOT] = 0;
ui_dev.absmax[ABS_MT_SLOT] = 9;
ui_dev.absmin[ABS_MT_TOUCH_MAJOR] = 0;
ui_dev.absmax[ABS_MT_TOUCH_MAJOR] = 255;
ui_dev.absmin[ABS_MT_POSITION_X] = 0;
ui_dev.absmax[ABS_MT_POSITION_X] = screen_width - 1;
ui_dev.absmin[ABS_MT_POSITION_Y] = 0;
ui_dev.absmax[ABS_MT_POSITION_Y] = screen_Height - 1;
ui_dev.absmin[ABS_MT_TRACKING_ID] = 0;
ui_dev.absmax[ABS_MT_TRACKING_ID] = 65535;
ui_dev.absmin[ABS_MT_PRESSURE] = 0;
ui_dev.absmax[ABS_MT_PRESSURE] = 30;
ui_dev.absmin[ABS_MT_TOOL_TYPE] = 0;
ui_dev.absmax[ABS_MT_TOOL_TYPE] = 1;

//enable direct
ioctl(uinp_fd, UI_SET_PROPBIT, INPUT_PROP_DIRECT);
//enable mouse event
ioctl(uinp_fd, UI_SET_EVBIT, EV_REL);
ioctl(uinp_fd, UI_SET_RELBIT, REL_X);
ioctl(uinp_fd, UI_SET_RELBIT, REL_Y);
//enable touch event
ioctl(uinp_fd, UI_SET_EVBIT, EV_ABS);
for (i = 0; i <= ABS_CNT; i++) {
    ioctl(uinp_fd, UI_SET_ABSBIT, i);
}

ioctl(uinp_fd, UI_SET_EVBIT, EV_SYN);
ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
for (i = 0; i < 256; i++) {
    ioctl(uinp_fd, UI_SET_KEYBIT, i);
}
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);
ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);

write(uinp_fd, &ui_dev, sizeof(ui_dev));
if (ioctl(uinp_fd, UI_DEV_CREATE)) {
    debug("Unable to create UINPUT device.");
    return -1;
}
return 0;

}

然后在uinput_fd中编写一些事件,代码段如下:

 int len;
// Move pointer to (100,100) location
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
event.type = type;
event.code = code;
event.value = value;
len = write(uinp_fd, &event, sizeof(event));
debug("SendEventToUinput done:%d %d %d",type, code, value);

我可以通过输入命令&#34; getevent -l&#34;看到终端上/ dev / input / event7上写的事件,如下所示:

/dev/input/event7: EV_KEY       BTN_TOUCH            DOWN                
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   00000027            
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   0000005d            
/dev/input/event7: EV_ABS       ABS_MT_POSITION_X    00000251            
/dev/input/event7: EV_ABS       ABS_MT_POSITION_Y    0000048f            
/dev/input/event7: EV_SYN       SYN_REPORT           00000000            
/dev/input/event7: EV_ABS       ABS_MT_TOUCH_MAJOR   00000045            
/dev/input/event7: EV_SYN       SYN_REPORT           00000000            
/dev/input/event7: EV_ABS       ABS_MT_TRACKING_ID   ffffffff            
/dev/input/event7: EV_SYN       SYN_REPORT           00000000            
/dev/input/event7: EV_KEY       BTN_TOUCH            UP                  
/dev/input/event7: EV_SYN       SYN_REPORT           00000000 

但屏幕上没有显示,如果我在&#34; dev / input / event4&#34;上发送相同的事件它可以工作吗?为什么&#34; dev / uinput&#34;是无效的?

有人帮助我。

5 个答案:

答案 0 :(得分:1)

执行此部分时:

Automatically gzip the file. This will set options.metadata.contentEncoding to gzip.

您还可以设置ABS_X和ABS_Y位。 ABS_X和ABS_Y位不能与ABS_MT_POSITION_X和ABS_MT_POSITION_Y位一起设置。

我建议只设置你将使用的ABS位。

答案 1 :(得分:0)

在我的推荐中,你应该使用这样的adb命令行:

adb shell sendevent /dev/input/event[event_number_here] input_value_here

前:

adb shell sendevent /dev/input/event2 1 315 1
adb shell sendevent /dev/input/event2 0 0 0

或者你可以尝试这个命令:

adb shell input tap x y

ex:adb shell输入tap 150 150

答案 2 :(得分:0)

你的ui_dev.name已经存在,请使用另一个,它会正常工作。

答案 3 :(得分:0)

查看adb shell输入的实现: https://android.googlesource.com/platform/frameworks/base/+/HEAD/cmds/input/src/com/android/commands/input/Input.java 有坐标的类型转换。可能就是这样的原因,当使用adb shell getevent捕获输入时,发送给输入事件的完全相同的坐标会转换为不同的坐标。我仍然没有解决方案,但是您可能会发现此信息有用-要使坐标不转换,需要以浮点形式在AOSP级别注入它。 -batcilla

答案 4 :(得分:-1)

尝试更改分配参数的顺序:

strncpy(ui_dev.name, "RemoteTV Event", UINPUT_MAX_NAME_SIZE);

制作:

strncpy(ui_dev.name,  UINPUT_MAX_NAME_SIZE, "RemoteTV Event");