Android⼿机遥控器添加模拟⿏标功能
⼿机遥控器要求有模拟⿏标功能,之前别⼈做的模拟⿏标是⽤service加上⾃⼰画的图标来实现的,不能像真正⿏标⼀样⽅便,⽽且实现⽐较难。⽹上查资料发现可以通过UInput来实现这个功能。
Uinput是⼀个虚拟的设备,使得可以在⽤户控件处理input设备,⼀般来讲uinput设备⽂件存在于/dev/input或者/dev/input/uinput⽬录中。在Linux中⼀切都是⽂件,所以使⽤uinput也很简单只需要open这个设备就可以了
打开设备:
extern "C"
JNIEXPORT int JNICALL
Java_com_remote_uinput_UInput_open(JNIEnv *env, jclass clazz) {
struct uinput_user_dev uidev;
//以只读的⽅式打开字符设备
fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
android模拟点击//    if (fd < 0) die("error: open ");
LOGD( "fd == %d", fd);
//打印出打开设备失败的原因,可以看看是否因为权限导致
LOGD("open %s fail, %s\n", "/dev/uinput", strerror(errno));
if (fd<0) {
LOGD( "fd == %d", fd);
return -1;
}
//config uinput working mode,  mouse or touchscreen?  relative coordinates or absolute coordinate?
if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)        //support key button
die("error: ioctl UI_SET_EVBIT");
if (ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)  //support mouse left key
die("error: ioctl  UI_SET_KEYBIT");
if (ioctl(fd, UI_SET_KEYBIT, BTN_RIGHT) < 0)  //support mouse right key
die("error: ioctl UI_SET_KEYBIT");
if (ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)      //uinput use relative coordinates
die("error: ioctl UI_SET_EVBIT");
if (ioctl(fd, UI_SET_RELBIT, REL_X) < 0)        //uinput use x coordinates
die("error: ioctl UI_SET_RELBIT REL_X");
if (ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)        //uinput use y coordinates
die("error: ioctl REL_Y");
memset(&uidev, 0,
sizeof(uidev));                  //creat an virtul input device node in /dev/input/***