diff options
| author | 2019-10-22 13:13:47 -0700 | |
|---|---|---|
| committer | 2019-11-21 18:21:28 -0800 | |
| commit | 49483274051a25e6fde2a995dc2aee048be02dac (patch) | |
| tree | 691e4d84a92619b12e3e68dd73108275df84b651 | |
| parent | 5073d389b4463d4f011d082c9c524d37ceed3096 (diff) | |
Use enum class for EventEntry
Switch EventEntry to use enum class. This improves type safety and
catches missing switch/case statements at compile time.
Bug: 70668286
Test: presubmit
Change-Id: Ib5f52cb357c87fcac8c7045ae13632814ab518b3
| -rw-r--r-- | services/inputflinger/dispatcher/Entry.cpp | 10 | ||||
| -rw-r--r-- | services/inputflinger/dispatcher/Entry.h | 19 | ||||
| -rw-r--r-- | services/inputflinger/dispatcher/InputDispatcher.cpp | 83 | ||||
| -rw-r--r-- | services/inputflinger/reader/mapper/SwitchInputMapper.cpp | 4 |
4 files changed, 78 insertions, 38 deletions
diff --git a/services/inputflinger/dispatcher/Entry.cpp b/services/inputflinger/dispatcher/Entry.cpp index 930c7c77e7..e925f5b6ba 100644 --- a/services/inputflinger/dispatcher/Entry.cpp +++ b/services/inputflinger/dispatcher/Entry.cpp @@ -60,7 +60,7 @@ static std::string keyActionToString(int32_t action) { // --- EventEntry --- -EventEntry::EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags) +EventEntry::EventEntry(uint32_t sequenceNum, Type type, nsecs_t eventTime, uint32_t policyFlags) : sequenceNum(sequenceNum), refCount(1), type(type), @@ -92,7 +92,7 @@ void EventEntry::releaseInjectionState() { // --- ConfigurationChangedEntry --- ConfigurationChangedEntry::ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime) - : EventEntry(sequenceNum, TYPE_CONFIGURATION_CHANGED, eventTime, 0) {} + : EventEntry(sequenceNum, Type::CONFIGURATION_CHANGED, eventTime, 0) {} ConfigurationChangedEntry::~ConfigurationChangedEntry() {} @@ -103,7 +103,7 @@ void ConfigurationChangedEntry::appendDescription(std::string& msg) const { // --- DeviceResetEntry --- DeviceResetEntry::DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId) - : EventEntry(sequenceNum, TYPE_DEVICE_RESET, eventTime, 0), deviceId(deviceId) {} + : EventEntry(sequenceNum, Type::DEVICE_RESET, eventTime, 0), deviceId(deviceId) {} DeviceResetEntry::~DeviceResetEntry() {} @@ -117,7 +117,7 @@ KeyEntry::KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, ui int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, nsecs_t downTime) - : EventEntry(sequenceNum, TYPE_KEY, eventTime, policyFlags), + : EventEntry(sequenceNum, Type::KEY, eventTime, policyFlags), deviceId(deviceId), source(source), displayId(displayId), @@ -165,7 +165,7 @@ MotionEntry::MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t device float xCursorPosition, float yCursorPosition, nsecs_t downTime, uint32_t pointerCount, const PointerProperties* pointerProperties, const PointerCoords* pointerCoords, float xOffset, float yOffset) - : EventEntry(sequenceNum, TYPE_MOTION, eventTime, policyFlags), + : EventEntry(sequenceNum, Type::MOTION, eventTime, policyFlags), eventTime(eventTime), deviceId(deviceId), source(source), diff --git a/services/inputflinger/dispatcher/Entry.h b/services/inputflinger/dispatcher/Entry.h index 28c2799477..9dcaadc6ee 100644 --- a/services/inputflinger/dispatcher/Entry.h +++ b/services/inputflinger/dispatcher/Entry.h @@ -33,11 +33,24 @@ namespace android::inputdispatcher { constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0; struct EventEntry { - enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION }; + enum class Type { CONFIGURATION_CHANGED, DEVICE_RESET, KEY, MOTION }; + + static const char* typeToString(Type type) { + switch (type) { + case Type::CONFIGURATION_CHANGED: + return "CONFIGURATION_CHANGED"; + case Type::DEVICE_RESET: + return "DEVICE_RESET"; + case Type::KEY: + return "KEY"; + case Type::MOTION: + return "MOTION"; + } + } uint32_t sequenceNum; mutable int32_t refCount; - int32_t type; + Type type; nsecs_t eventTime; uint32_t policyFlags; InjectionState* injectionState; @@ -66,7 +79,7 @@ struct EventEntry { virtual void appendDescription(std::string& msg) const = 0; protected: - EventEntry(uint32_t sequenceNum, int32_t type, nsecs_t eventTime, uint32_t policyFlags); + EventEntry(uint32_t sequenceNum, Type type, nsecs_t eventTime, uint32_t policyFlags); virtual ~EventEntry(); void releaseInjectionState(); }; diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp index 58a5b3c8d3..c219941129 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.cpp +++ b/services/inputflinger/dispatcher/InputDispatcher.cpp @@ -386,7 +386,7 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { } switch (mPendingEvent->type) { - case EventEntry::TYPE_CONFIGURATION_CHANGED: { + case EventEntry::Type::CONFIGURATION_CHANGED: { ConfigurationChangedEntry* typedEntry = static_cast<ConfigurationChangedEntry*>(mPendingEvent); done = dispatchConfigurationChangedLocked(currentTime, typedEntry); @@ -394,14 +394,14 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { break; } - case EventEntry::TYPE_DEVICE_RESET: { + case EventEntry::Type::DEVICE_RESET: { DeviceResetEntry* typedEntry = static_cast<DeviceResetEntry*>(mPendingEvent); done = dispatchDeviceResetLocked(currentTime, typedEntry); dropReason = DropReason::NOT_DROPPED; // device resets are never dropped break; } - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); if (isAppSwitchDue) { if (isAppSwitchKeyEvent(*typedEntry)) { @@ -421,7 +421,7 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); if (dropReason == DropReason::NOT_DROPPED && isAppSwitchDue) { dropReason = DropReason::APP_SWITCH; @@ -435,10 +435,6 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { done = dispatchMotionLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); break; } - - default: - ALOG_ASSERT(false); - break; } if (done) { @@ -458,7 +454,7 @@ bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { traceInboundQueueLengthLocked(); switch (entry->type) { - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { // Optimize app switch latency. // If the application takes too long to catch up then we drop all events preceding // the app switch key. @@ -480,7 +476,7 @@ bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { // Optimize case where the current application is unresponsive and the user // decides to touch a window in a different application. // If the application takes too long to catch up then we drop all events preceding @@ -508,6 +504,11 @@ bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { } break; } + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + // nothing to do + break; + } } return needWake; @@ -627,12 +628,12 @@ void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason } switch (entry.type) { - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); synthesizeCancelationEventsForAllConnectionsLocked(options); break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); if (motionEntry.source & AINPUT_SOURCE_CLASS_POINTER) { CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); @@ -643,6 +644,11 @@ void InputDispatcher::dropInboundEventLocked(const EventEntry& entry, DropReason } break; } + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + LOG_ALWAYS_FATAL("Should not drop %s events", EventEntry::typeToString(entry.type)); + break; + } } } @@ -1174,18 +1180,19 @@ void InputDispatcher::resetANRTimeoutsLocked() { int32_t InputDispatcher::getTargetDisplayId(const EventEntry& entry) { int32_t displayId; switch (entry.type) { - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { const KeyEntry& keyEntry = static_cast<const KeyEntry&>(entry); displayId = keyEntry.displayId; break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { const MotionEntry& motionEntry = static_cast<const MotionEntry&>(entry); displayId = motionEntry.displayId; break; } - default: { - ALOGE("Unsupported event type '%" PRId32 "' for target display.", entry.type); + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + ALOGE("%s events do not have a target display", EventEntry::typeToString(entry.type)); return ADISPLAY_ID_NONE; } } @@ -1849,7 +1856,7 @@ std::string InputDispatcher::checkWindowReadyForMoreInputLocked( } // Ensure that the dispatch queues aren't too far backed up for this event. - if (eventEntry.type == EventEntry::TYPE_KEY) { + if (eventEntry.type == EventEntry::Type::KEY) { // If the event is a key event, then we must wait for all previous events to // complete before delivering it because previous events may have the // side-effect of transferring focus to a different window and we want to @@ -1937,7 +1944,7 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { int32_t eventType = USER_ACTIVITY_EVENT_OTHER; switch (eventEntry.type) { - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { const MotionEntry& motionEntry = static_cast<const MotionEntry&>(eventEntry); if (motionEntry.action == AMOTION_EVENT_ACTION_CANCEL) { return; @@ -1948,7 +1955,7 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { } break; } - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { const KeyEntry& keyEntry = static_cast<const KeyEntry&>(eventEntry); if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) { return; @@ -1956,6 +1963,12 @@ void InputDispatcher::pokeUserActivityLocked(const EventEntry& eventEntry) { eventType = USER_ACTIVITY_EVENT_BUTTON; break; } + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + LOG_ALWAYS_FATAL("%s events are not user activity", + EventEntry::typeToString(eventEntry.type)); + break; + } } std::unique_ptr<CommandEntry> commandEntry = @@ -1996,7 +2009,7 @@ void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, // Split a motion event if needed. if (inputTarget->flags & InputTarget::FLAG_SPLIT) { - ALOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION); + ALOG_ASSERT(eventEntry->type == EventEntry::Type::MOTION); const MotionEntry& originalMotionEntry = static_cast<const MotionEntry&>(*eventEntry); if (inputTarget->pointerIds.count() != originalMotionEntry.pointerCount) { @@ -2080,7 +2093,7 @@ void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connectio // Apply target flags and update the connection's input state. switch (eventEntry->type) { - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { const KeyEntry& keyEntry = static_cast<const KeyEntry&>(*eventEntry); dispatchEntry->resolvedAction = keyEntry.action; dispatchEntry->resolvedFlags = keyEntry.flags; @@ -2097,7 +2110,7 @@ void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connectio break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { const MotionEntry& motionEntry = static_cast<const MotionEntry&>(*eventEntry); if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; @@ -2147,6 +2160,12 @@ void InputDispatcher::enqueueDispatchEntryLocked(const sp<Connection>& connectio break; } + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + LOG_ALWAYS_FATAL("%s events should not go to apps", + EventEntry::typeToString(eventEntry->type)); + break; + } } // Remember that we are waiting for this dispatch to complete. @@ -2206,7 +2225,7 @@ void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, status_t status; EventEntry* eventEntry = dispatchEntry->eventEntry; switch (eventEntry->type) { - case EventEntry::TYPE_KEY: { + case EventEntry::Type::KEY: { KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); // Publish the key event. @@ -2221,7 +2240,7 @@ void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, break; } - case EventEntry::TYPE_MOTION: { + case EventEntry::Type::MOTION: { MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); PointerCoords scaledCoords[MAX_POINTERS]; @@ -2502,15 +2521,23 @@ void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( for (size_t i = 0; i < cancelationEvents.size(); i++) { EventEntry* cancelationEventEntry = cancelationEvents[i]; switch (cancelationEventEntry->type) { - case EventEntry::TYPE_KEY: + case EventEntry::Type::KEY: { logOutboundKeyDetails("cancel - ", static_cast<const KeyEntry&>(*cancelationEventEntry)); break; - case EventEntry::TYPE_MOTION: + } + case EventEntry::Type::MOTION: { logOutboundMotionDetails("cancel - ", static_cast<const MotionEntry&>( *cancelationEventEntry)); break; + } + case EventEntry::Type::CONFIGURATION_CHANGED: + case EventEntry::Type::DEVICE_RESET: { + LOG_ALWAYS_FATAL("%s event should not be found inside Connections's queue", + EventEntry::typeToString(cancelationEventEntry->type)); + break; + } } InputTarget target; @@ -4237,11 +4264,11 @@ void InputDispatcher::doDispatchCycleFinishedLockedInterruptible(CommandEntry* c } bool restartEvent; - if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) { + if (dispatchEntry->eventEntry->type == EventEntry::Type::KEY) { KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); restartEvent = afterKeyEventLockedInterruptible(connection, dispatchEntry, keyEntry, handled); - } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) { + } else if (dispatchEntry->eventEntry->type == EventEntry::Type::MOTION) { MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); restartEvent = afterMotionEventLockedInterruptible(connection, dispatchEntry, motionEntry, handled); diff --git a/services/inputflinger/reader/mapper/SwitchInputMapper.cpp b/services/inputflinger/reader/mapper/SwitchInputMapper.cpp index 4ff941f5cf..16095b9eb1 100644 --- a/services/inputflinger/reader/mapper/SwitchInputMapper.cpp +++ b/services/inputflinger/reader/mapper/SwitchInputMapper.cpp @@ -56,8 +56,8 @@ void SwitchInputMapper::processSwitch(int32_t switchCode, int32_t switchValue) { void SwitchInputMapper::sync(nsecs_t when) { if (mUpdatedSwitchMask) { uint32_t updatedSwitchValues = mSwitchValues & mUpdatedSwitchMask; - NotifySwitchArgs args(mContext->getNextSequenceNum(), when, 0, updatedSwitchValues, - mUpdatedSwitchMask); + NotifySwitchArgs args(mContext->getNextSequenceNum(), when, 0 /*policyFlags*/, + updatedSwitchValues, mUpdatedSwitchMask); getListener()->notifySwitch(&args); mUpdatedSwitchMask = 0; |