Let InputFlinger generate event IDs.
Also send event IDs via InputMessage and add some atrace calls to form a
complete chain of input event processing.
Bug: 144889238
Test: systrace shows correct event IDs.
Test: atest inputflinger_tests
Change-Id: I3c561b03b0ba75c22115ae020e6b41855686ab64
diff --git a/services/inputflinger/dispatcher/Connection.cpp b/services/inputflinger/dispatcher/Connection.cpp
index 6f82f4f..188212b 100644
--- a/services/inputflinger/dispatcher/Connection.cpp
+++ b/services/inputflinger/dispatcher/Connection.cpp
@@ -20,11 +20,13 @@
namespace android::inputdispatcher {
-Connection::Connection(const sp<InputChannel>& inputChannel, bool monitor)
+Connection::Connection(const sp<InputChannel>& inputChannel, bool monitor,
+ const IdGenerator& idGenerator)
: status(STATUS_NORMAL),
inputChannel(inputChannel),
monitor(monitor),
inputPublisher(inputChannel),
+ inputState(idGenerator),
inputPublisherBlocked(false) {}
Connection::~Connection() {}
diff --git a/services/inputflinger/dispatcher/Connection.h b/services/inputflinger/dispatcher/Connection.h
index 8423010..bb3f2fe 100644
--- a/services/inputflinger/dispatcher/Connection.h
+++ b/services/inputflinger/dispatcher/Connection.h
@@ -58,7 +58,7 @@
// yet received a "finished" response from the application.
std::deque<DispatchEntry*> waitQueue;
- explicit Connection(const sp<InputChannel>& inputChannel, bool monitor);
+ Connection(const sp<InputChannel>& inputChannel, bool monitor, const IdGenerator& idGenerator);
inline const std::string getInputChannelName() const { return inputChannel->getName(); }
diff --git a/services/inputflinger/dispatcher/Entry.h b/services/inputflinger/dispatcher/Entry.h
index c58ae23..ab481bd 100644
--- a/services/inputflinger/dispatcher/Entry.h
+++ b/services/inputflinger/dispatcher/Entry.h
@@ -29,9 +29,6 @@
namespace android::inputdispatcher {
-// Sequence number for synthesized or injected events.
-constexpr int32_t SYNTHESIZED_EVENT_ID = 0;
-
struct EventEntry {
enum class Type {
CONFIGURATION_CHANGED,
@@ -78,7 +75,9 @@
* Key repeat is a synthesized event, because it is related to an actual hardware state
* (a key is currently pressed), but the repeat itself is generated by the framework.
*/
- inline bool isSynthesized() const { return isInjected() || id == SYNTHESIZED_EVENT_ID; }
+ inline bool isSynthesized() const {
+ return isInjected() || IdGenerator::getSource(id) != IdGenerator::Source::INPUT_READER;
+ }
void release();
@@ -199,7 +198,8 @@
float windowYScale = 1.0f;
nsecs_t deliveryTime; // time when the event was actually delivered
- // Set to the resolved action and flags when the event is enqueued.
+ // Set to the resolved ID, action and flags when the event is enqueued.
+ int32_t resolvedEventId;
int32_t resolvedAction;
int32_t resolvedFlags;
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 2f7b5ad..308d19b 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -383,6 +383,7 @@
: mPolicy(policy),
mPendingEvent(nullptr),
mLastDropReason(DropReason::NOT_DROPPED),
+ mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
mAppSwitchSawKeyDown(false),
mAppSwitchDueTime(LONG_LONG_MAX),
mNextUnblockedEvent(nullptr),
@@ -925,12 +926,13 @@
(POLICY_FLAG_RAW_MASK | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED);
if (entry->refCount == 1) {
entry->recycle();
+ entry->id = mIdGenerator.nextId();
entry->eventTime = currentTime;
entry->policyFlags = policyFlags;
entry->repeatCount += 1;
} else {
KeyEntry* newEntry =
- new KeyEntry(SYNTHESIZED_EVENT_ID, currentTime, entry->deviceId, entry->source,
+ new KeyEntry(mIdGenerator.nextId(), currentTime, entry->deviceId, entry->source,
entry->displayId, policyFlags, entry->action, entry->flags,
entry->keyCode, entry->scanCode, entry->metaState,
entry->repeatCount + 1, entry->downTime);
@@ -981,7 +983,7 @@
void InputDispatcher::enqueueFocusEventLocked(const InputWindowHandle& window, bool hasFocus) {
FocusEntry* focusEntry =
- new FocusEntry(SYNTHESIZED_EVENT_ID, now(), window.getToken(), hasFocus);
+ new FocusEntry(mIdGenerator.nextId(), now(), window.getToken(), hasFocus);
enqueueInboundEventLocked(focusEntry);
}
@@ -2188,7 +2190,7 @@
const InputTarget& inputTarget) {
if (ATRACE_ENABLED()) {
std::string message =
- StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=%" PRIx32 ")",
+ StringPrintf("prepareDispatchCycleLocked(inputChannel=%s, id=0x%" PRIx32 ")",
connection->getInputChannelName().c_str(), eventEntry->id);
ATRACE_NAME(message.c_str());
}
@@ -2244,7 +2246,7 @@
const InputTarget& inputTarget) {
if (ATRACE_ENABLED()) {
std::string message =
- StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=%" PRIx32 ")",
+ StringPrintf("enqueueDispatchEntriesLocked(inputChannel=%s, id=0x%" PRIx32 ")",
connection->getInputChannelName().c_str(), eventEntry->id);
ATRACE_NAME(message.c_str());
}
@@ -2299,6 +2301,7 @@
switch (newEntry->type) {
case EventEntry::Type::KEY: {
const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*newEntry);
+ dispatchEntry->resolvedEventId = keyEntry.id;
dispatchEntry->resolvedAction = keyEntry.action;
dispatchEntry->resolvedFlags = keyEntry.flags;
@@ -2315,6 +2318,11 @@
case EventEntry::Type::MOTION: {
const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*newEntry);
+ // Assign a default value to dispatchEntry that will never be generated by InputReader,
+ // and assign a InputDispatcher value if it doesn't change in the if-else chain below.
+ constexpr int32_t DEFAULT_RESOLVED_EVENT_ID =
+ static_cast<int32_t>(IdGenerator::Source::OTHER);
+ dispatchEntry->resolvedEventId = DEFAULT_RESOLVED_EVENT_ID;
if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) {
dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE;
} else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) {
@@ -2327,6 +2335,7 @@
dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN;
} else {
dispatchEntry->resolvedAction = motionEntry.action;
+ dispatchEntry->resolvedEventId = motionEntry.id;
}
if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE &&
!connection->inputState.isHovering(motionEntry.deviceId, motionEntry.source,
@@ -2357,6 +2366,17 @@
return; // skip the inconsistent event
}
+ dispatchEntry->resolvedEventId =
+ dispatchEntry->resolvedEventId == DEFAULT_RESOLVED_EVENT_ID
+ ? mIdGenerator.nextId()
+ : motionEntry.id;
+ if (ATRACE_ENABLED() && dispatchEntry->resolvedEventId != motionEntry.id) {
+ std::string message = StringPrintf("Transmute MotionEvent(id=0x%" PRIx32
+ ") to MotionEvent(id=0x%" PRIx32 ").",
+ motionEntry.id, dispatchEntry->resolvedEventId);
+ ATRACE_NAME(message.c_str());
+ }
+
dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction,
inputTarget.inputChannel->getConnectionToken());
@@ -2438,14 +2458,16 @@
std::array<uint8_t, 32> hmac = mHmacKeyManager.sign(verifiedEvent);
// Publish the key event.
- status = connection->inputPublisher
- .publishKeyEvent(dispatchEntry->seq, keyEntry->deviceId,
- keyEntry->source, keyEntry->displayId,
- std::move(hmac), dispatchEntry->resolvedAction,
- dispatchEntry->resolvedFlags, keyEntry->keyCode,
- keyEntry->scanCode, keyEntry->metaState,
- keyEntry->repeatCount, keyEntry->downTime,
- keyEntry->eventTime);
+ status =
+ connection->inputPublisher
+ .publishKeyEvent(dispatchEntry->seq, dispatchEntry->resolvedEventId,
+ keyEntry->deviceId, keyEntry->source,
+ keyEntry->displayId, std::move(hmac),
+ dispatchEntry->resolvedAction,
+ dispatchEntry->resolvedFlags, keyEntry->keyCode,
+ keyEntry->scanCode, keyEntry->metaState,
+ keyEntry->repeatCount, keyEntry->downTime,
+ keyEntry->eventTime);
break;
}
@@ -2494,9 +2516,11 @@
// Publish the motion event.
status = connection->inputPublisher
- .publishMotionEvent(dispatchEntry->seq, motionEntry->deviceId,
- motionEntry->source, motionEntry->displayId,
- std::move(hmac), dispatchEntry->resolvedAction,
+ .publishMotionEvent(dispatchEntry->seq,
+ dispatchEntry->resolvedEventId,
+ motionEntry->deviceId, motionEntry->source,
+ motionEntry->displayId, std::move(hmac),
+ dispatchEntry->resolvedAction,
motionEntry->actionButton,
dispatchEntry->resolvedFlags,
motionEntry->edgeFlags, motionEntry->metaState,
@@ -2515,6 +2539,7 @@
case EventEntry::Type::FOCUS: {
FocusEntry* focusEntry = static_cast<FocusEntry*>(eventEntry);
status = connection->inputPublisher.publishFocusEvent(dispatchEntry->seq,
+ focusEntry->id,
focusEntry->hasFocus,
mInTouchMode);
break;
@@ -2920,10 +2945,17 @@
}
}
+ int32_t newId = mIdGenerator.nextId();
+ if (ATRACE_ENABLED()) {
+ std::string message = StringPrintf("Split MotionEvent(id=0x%" PRIx32
+ ") to MotionEvent(id=0x%" PRIx32 ").",
+ originalMotionEntry.id, newId);
+ ATRACE_NAME(message.c_str());
+ }
MotionEntry* splitMotionEntry =
- new MotionEntry(originalMotionEntry.id, originalMotionEntry.eventTime,
- originalMotionEntry.deviceId, originalMotionEntry.source,
- originalMotionEntry.displayId, originalMotionEntry.policyFlags, action,
+ new MotionEntry(newId, originalMotionEntry.eventTime, originalMotionEntry.deviceId,
+ originalMotionEntry.source, originalMotionEntry.displayId,
+ originalMotionEntry.policyFlags, action,
originalMotionEntry.actionButton, originalMotionEntry.flags,
originalMotionEntry.metaState, originalMotionEntry.buttonState,
originalMotionEntry.classification, originalMotionEntry.edgeFlags,
@@ -4225,7 +4257,7 @@
return BAD_VALUE;
}
- sp<Connection> connection = new Connection(inputChannel, false /*monitor*/);
+ sp<Connection> connection = new Connection(inputChannel, false /*monitor*/, mIdGenerator);
int fd = inputChannel->getFd();
mConnectionsByFd[fd] = connection;
@@ -4254,7 +4286,7 @@
return BAD_VALUE;
}
- sp<Connection> connection = new Connection(inputChannel, true /*monitor*/);
+ sp<Connection> connection = new Connection(inputChannel, true /*monitor*/, mIdGenerator);
const int fd = inputChannel->getFd();
mConnectionsByFd[fd] = connection;
diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h
index 482133e..4aa47f8 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.h
+++ b/services/inputflinger/dispatcher/InputDispatcher.h
@@ -156,6 +156,8 @@
DropReason mLastDropReason GUARDED_BY(mLock);
+ const IdGenerator mIdGenerator;
+
// With each iteration, InputDispatcher nominally processes one queued event,
// a timeout, or a response from an input consumer.
// This method should only be called on the input dispatcher's own thread.
diff --git a/services/inputflinger/dispatcher/InputState.cpp b/services/inputflinger/dispatcher/InputState.cpp
index 7fa9e09..386056d 100644
--- a/services/inputflinger/dispatcher/InputState.cpp
+++ b/services/inputflinger/dispatcher/InputState.cpp
@@ -16,9 +16,11 @@
#include "InputState.h"
+#include "InputDispatcher.h"
+
namespace android::inputdispatcher {
-InputState::InputState() {}
+InputState::InputState(const IdGenerator& idGenerator) : mIdGenerator(idGenerator) {}
InputState::~InputState() {}
@@ -268,7 +270,7 @@
std::vector<EventEntry*> events;
for (KeyMemento& memento : mKeyMementos) {
if (shouldCancelKey(memento, options)) {
- events.push_back(new KeyEntry(SYNTHESIZED_EVENT_ID, currentTime, memento.deviceId,
+ events.push_back(new KeyEntry(mIdGenerator.nextId(), currentTime, memento.deviceId,
memento.source, memento.displayId, memento.policyFlags,
AKEY_EVENT_ACTION_UP,
memento.flags | AKEY_EVENT_FLAG_CANCELED, memento.keyCode,
@@ -281,7 +283,7 @@
if (shouldCancelMotion(memento, options)) {
const int32_t action = memento.hovering ? AMOTION_EVENT_ACTION_HOVER_EXIT
: AMOTION_EVENT_ACTION_CANCEL;
- events.push_back(new MotionEntry(SYNTHESIZED_EVENT_ID, currentTime, memento.deviceId,
+ events.push_back(new MotionEntry(mIdGenerator.nextId(), currentTime, memento.deviceId,
memento.source, memento.displayId, memento.policyFlags,
action, 0 /*actionButton*/, memento.flags, AMETA_NONE,
0 /*buttonState*/, MotionClassification::NONE,
@@ -331,7 +333,7 @@
: AMOTION_EVENT_ACTION_POINTER_DOWN
| (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
- events.push_back(new MotionEntry(SYNTHESIZED_EVENT_ID, currentTime, memento.deviceId,
+ events.push_back(new MotionEntry(mIdGenerator.nextId(), currentTime, memento.deviceId,
memento.source, memento.displayId, memento.policyFlags,
action, 0 /*actionButton*/, memento.flags, AMETA_NONE,
0 /*buttonState*/, MotionClassification::NONE,
diff --git a/services/inputflinger/dispatcher/InputState.h b/services/inputflinger/dispatcher/InputState.h
index 08266ae..d97a664 100644
--- a/services/inputflinger/dispatcher/InputState.h
+++ b/services/inputflinger/dispatcher/InputState.h
@@ -30,7 +30,7 @@
* synthesized when events are dropped. */
class InputState {
public:
- InputState();
+ explicit InputState(const IdGenerator& idGenerator);
~InputState();
// Returns true if there is no state to be canceled.
@@ -111,6 +111,8 @@
void mergePointerStateTo(MotionMemento& other) const;
};
+ const IdGenerator& mIdGenerator; // InputDispatcher owns it so we won't have dangling reference.
+
std::vector<KeyMemento> mKeyMementos;
std::vector<MotionMemento> mMotionMementos;
KeyedVector<int32_t, int32_t> mFallbackKeys;