1.genymobile的scrcpy使用反射的方法获取InputManager
https://github.com/Genymobile/scrcpy/blob/master/server/src/main/java/com/genymobile/scrcpy/Controller.java
private Method getInjectInputEventMethod() throws NoSuchMethodException {
if (injectInputEventMethod == null) {
injectInputEventMethod = manager.getClass().getMethod("injectInputEvent", InputEvent.class, int.class);
}
return injectInputEventMethod;
}
public boolean injectInputEvent(InputEvent inputEvent, int mode) {
try {
Method method = getInjectInputEventMethod();
return (boolean) method.invoke(manager, inputEvent, mode);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
Ln.e("Could not invoke method", e);
return false;
}
}
2.minitouch的方案是unistd方式写入监听触控输入设备
if ((fd = open(devpath, O_RDWR)) < 0) { //监听触控输入设备
perror("open");
fprintf(stderr, "Unable to open device %s for inspection", devpath);
goto mismatch;
}
#define WRITE_EVENT(state, type, code, value) _write_event(state, type, #type, code, #code, value)
static int _write_event(internal_state_touchpad_t *state,
uint16_t type, const char *type_name,
uint16_t code, const char *code_name,
int32_t value) {
// It seems that most devices do not require the event timestamps at all.
// Left here for reference should such a situation arise.
//
// timespec ts;
// clock_gettime(CLOCK_MONOTONIC, &ts);
// input_event event = {{ts.tv_sec, ts.tv_nsec / 1000}, type, code, value};
struct input_event event = {{0, 0}, type, code, value}; //对 input_event 进行赋值
ssize_t result;
ssize_t length = (ssize_t) sizeof(event);
if (g_verbose)
fprintf(stderr, "%-12s %-20s %08x\n", type_name, code_name, value); //输出日志
result = write(state->fd, &event, length); //写入事件
return result - length;
}