admin管理员组文章数量:1576941
IMS:injectInputEvent注入Input事件
android11-release
IMS:InputManagerService启动简要
IMS:InputReader线程获取输入事件
IMS:InputDispatcher线程分发事件
adb命令输入KEYCODE_VOLUME_UP
adb shell input keyevent KEYCODE_VOLUME_UP
frameworks\base\core\java\android\view\KeyEvent.java
frameworks\base\cmds\input\src\com\android\commands\input\Input.java
injectKeyEvent(event);
调用IMS.injectInputEvent
,最终走到通过JNI调用InputDispatcher::injectInputEvent
public class Input extends BaseCommand {
// ... ... ... ...
Input() {
COMMANDS.put("text", new InputText());
COMMANDS.put("keyevent", new InputKeyEvent());
COMMANDS.put("tap", new InputTap());
COMMANDS.put("swipe", new InputSwipe());
COMMANDS.put("draganddrop", new InputDragAndDrop());
COMMANDS.put("press", new InputPress());
COMMANDS.put("roll", new InputRoll());
COMMANDS.put("motionevent", new InputMotionEvent());
}
@Override
public void onRun() throws Exception {
String arg = nextArgRequired();
int inputSource = InputDevice.SOURCE_UNKNOWN;
// Get source (optional).
if (SOURCES.containsKey(arg)) {
inputSource = SOURCES.get(arg);
arg = nextArgRequired();
}
// Get displayId (optional).
int displayId = INVALID_DISPLAY;
if ("-d".equals(arg)) {
displayId = getDisplayId();
arg = nextArgRequired();
}
// Get command and run.
InputCmd cmd = COMMANDS.get(arg);
if (cmd != null) {
try {
cmd.run(inputSource, displayId);
return;
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(INVALID_ARGUMENTS + arg);
}
}
throw new IllegalArgumentException("Error: Unknown command: " + arg);
}
// ... ... ... ...
class InputKeyEvent implements InputCmd {
@Override
public void run(int inputSource, int displayId) {
String arg = nextArgRequired();
final boolean longpress = "--longpress".equals(arg);
if (longpress) {
arg = nextArgRequired();
}
do {
final int keycode = KeyEvent.keyCodeFromString(arg);
sendKeyEvent(inputSource, keycode, longpress, displayId);
} while ((arg = nextArg()) != null);
}
private void sendKeyEvent(int inputSource, int keyCode, boolean longpress, int displayId) {
final long now = SystemClock.uptimeMillis();
int repeatCount = 0;
KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, repeatCount,
0 /*metaState*/, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /*scancode*/, 0 /*flags*/,
inputSource);
event.setDisplayId(displayId);
injectKeyEvent(event);
if (longpress) {
repeatCount++;
injectKeyEvent(KeyEvent.changeTimeRepeat(event, now, repeatCount,
KeyEvent.FLAG_LONG_PRESS));
}
injectKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
}
}
InputDispatcher::injectInputEvent注入Input事件
frameworks\base\services\core\java\com\android\server\input\InputManagerService.java
frameworks\base\services\core\jni\com_android_server_input_InputManagerService.cpp
frameworks\native\services\inputflinger\dispatcher\InputDispatcher.cpp
adb注入的是KeyEvent,InputDispatcher::injectInputEvent
将 injectedEntries(EventEntry队列)通过InputDispatcher::enqueueInboundEventLocked
添加到mInboundQueue
,mLooper->wake()
唤醒 InputDispatcher线程
,最后返回注入结果injectionResult
int32_t InputDispatcher::injectInputEvent(const InputEvent* event, int32_t injectorPid,
int32_t injectorUid, int32_t syncMode,
std::chrono::milliseconds timeout, uint32_t policyFlags) {
#if DEBUG_INBOUND_EVENT_DETAILS
ALOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, "
"syncMode=%d, timeout=%lld, policyFlags=0x%08x",
event->getType(), injectorPid, injectorUid, syncMode, timeout.count(), policyFlags);
#endif
// ... ... ... ...
std::queue<EventEntry*> injectedEntries;
switch (event->getType()) {
case AINPUT_EVENT_TYPE_KEY: {
const KeyEvent& incomingKey = static_cast<const KeyEvent&>(*event);
int32_t action = incomingKey.getAction();
if (!validateKeyEvent(action)) {
return INPUT_EVENT_INJECTION_FAILED;
}
int32_t flags = incomingKey.getFlags();
int32_t keyCode = incomingKey.getKeyCode();
int32_t metaState = incomingKey.getMetaState();
accelerateMetaShortcuts(VIRTUAL_KEYBOARD_ID, action,
/*byref*/ keyCode, /*byref*/ metaState);
KeyEvent keyEvent;
keyEvent.initialize(incomingKey.getId(), VIRTUAL_KEYBOARD_ID, incomingKey.getSource(),
incomingKey.getDisplayId(), INVALID_HMAC, action, flags, keyCode,
incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
incomingKey.getDownTime(), incomingKey.getEventTime());
if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) {
policyFlags |= POLICY_FLAG_VIRTUAL;
}
if (!(policyFlags & POLICY_FLAG_FILTERED)) {
android::base::Timer t;
mPolicy->interceptKeyBeforeQueueing(&keyEvent, /*byref*/ policyFlags);
if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
ALOGW("Excessive delay in interceptKeyBeforeQueueing; took %s ms",
std::to_string(t.duration().count()).c_str());
}
}
mLock.lock();
KeyEntry* injectedEntry =
new KeyEntry(incomingKey.getId(), incomingKey.getEventTime(),
VIRTUAL_KEYBOARD_ID, incomingKey.getSource(),
incomingKey.getDisplayId(), policyFlags, action, flags, keyCode,
incomingKey.getScanCode(), metaState, incomingKey.getRepeatCount(),
incomingKey.getDownTime());
injectedEntries.push(injectedEntry);
break;
}
// ... ... ... ...
}
// ... ... ... ...
bool needWake = false;
while (!injectedEntries.empty()) {
needWake |= enqueueInboundEventLocked(injectedEntries.front());
injectedEntries.pop();
}
mLock.unlock();
if (needWake) {
mLooper->wake();
}
// ... ... ... ...
#if DEBUG_INJECTION
ALOGD("injectInputEvent - Finished with result %d. injectorPid=%d, injectorUid=%d",
injectionResult, injectorPid, injectorUid);
#endif
return injectionResult;
}
mInboundQueue
分发查看IMS:InputDispatcher线程分发事件,这里可以看到注入 Input 事件直接加入到mInboundQueue由InputDispatcher
分发,没有经过InputReader
IMS:injectInputEvent注入反馈结果
本文标签: 事件IMSinjectInputEventInput
版权声明:本文标题:IMS:injectInputEvent注入Input事件 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727797925a1130476.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论