Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _UI_INPUT_INPUTDISPATCHER_ENTRY_H |
| 18 | #define _UI_INPUT_INPUTDISPATCHER_ENTRY_H |
| 19 | |
| 20 | #include "InjectionState.h" |
| 21 | #include "InputTarget.h" |
| 22 | |
| 23 | #include <input/Input.h> |
| 24 | #include <input/InputApplication.h> |
| 25 | #include <stdint.h> |
| 26 | #include <utils/Timers.h> |
| 27 | #include <functional> |
| 28 | #include <string> |
| 29 | |
| 30 | namespace android::inputdispatcher { |
| 31 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 32 | // Sequence number for synthesized or injected events. |
| 33 | constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0; |
| 34 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 35 | struct EventEntry { |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 36 | enum class Type { CONFIGURATION_CHANGED, DEVICE_RESET, KEY, MOTION }; |
| 37 | |
| 38 | static const char* typeToString(Type type) { |
| 39 | switch (type) { |
| 40 | case Type::CONFIGURATION_CHANGED: |
| 41 | return "CONFIGURATION_CHANGED"; |
| 42 | case Type::DEVICE_RESET: |
| 43 | return "DEVICE_RESET"; |
| 44 | case Type::KEY: |
| 45 | return "KEY"; |
| 46 | case Type::MOTION: |
| 47 | return "MOTION"; |
| 48 | } |
| 49 | } |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 50 | |
| 51 | uint32_t sequenceNum; |
| 52 | mutable int32_t refCount; |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 53 | Type type; |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 54 | nsecs_t eventTime; |
| 55 | uint32_t policyFlags; |
| 56 | InjectionState* injectionState; |
| 57 | |
| 58 | bool dispatchInProgress; // initially false, set to true while dispatching |
| 59 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 60 | /** |
| 61 | * Injected keys are events from an external (probably untrusted) application |
| 62 | * and are not related to real hardware state. They come in via |
| 63 | * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED. |
| 64 | */ |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 65 | inline bool isInjected() const { return injectionState != nullptr; } |
| 66 | |
Siarhei Vishniakou | de4bf15 | 2019-08-16 11:12:52 -0500 | [diff] [blame] | 67 | /** |
| 68 | * Synthesized events are either injected events, or events that come |
| 69 | * from real hardware, but aren't directly attributable to a specific hardware event. |
| 70 | * Key repeat is a synthesized event, because it is related to an actual hardware state |
| 71 | * (a key is currently pressed), but the repeat itself is generated by the framework. |
| 72 | */ |
| 73 | inline bool isSynthesized() const { |
| 74 | return isInjected() || sequenceNum == SYNTHESIZED_EVENT_SEQUENCE_NUM; |
| 75 | } |
| 76 | |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 77 | void release(); |
| 78 | |
| 79 | virtual void appendDescription(std::string& msg) const = 0; |
| 80 | |
| 81 | protected: |
Siarhei Vishniakou | 4948327 | 2019-10-22 13:13:47 -0700 | [diff] [blame] | 82 | EventEntry(uint32_t sequenceNum, Type type, nsecs_t eventTime, uint32_t policyFlags); |
Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 83 | virtual ~EventEntry(); |
| 84 | void releaseInjectionState(); |
| 85 | }; |
| 86 | |
| 87 | struct ConfigurationChangedEntry : EventEntry { |
| 88 | explicit ConfigurationChangedEntry(uint32_t sequenceNum, nsecs_t eventTime); |
| 89 | virtual void appendDescription(std::string& msg) const; |
| 90 | |
| 91 | protected: |
| 92 | virtual ~ConfigurationChangedEntry(); |
| 93 | }; |
| 94 | |
| 95 | struct DeviceResetEntry : EventEntry { |
| 96 | int32_t deviceId; |
| 97 | |
| 98 | DeviceResetEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId); |
| 99 | virtual void appendDescription(std::string& msg) const; |
| 100 | |
| 101 | protected: |
| 102 | virtual ~DeviceResetEntry(); |
| 103 | }; |
| 104 | |
| 105 | struct KeyEntry : EventEntry { |
| 106 | int32_t deviceId; |
| 107 | uint32_t source; |
| 108 | int32_t displayId; |
| 109 | int32_t action; |
| 110 | int32_t flags; |
| 111 | int32_t keyCode; |
| 112 | int32_t scanCode; |
| 113 | int32_t metaState; |
| 114 | int32_t repeatCount; |
| 115 | nsecs_t downTime; |
| 116 | |
| 117 | bool syntheticRepeat; // set to true for synthetic key repeats |
| 118 | |
| 119 | enum InterceptKeyResult { |
| 120 | INTERCEPT_KEY_RESULT_UNKNOWN, |
| 121 | INTERCEPT_KEY_RESULT_SKIP, |
| 122 | INTERCEPT_KEY_RESULT_CONTINUE, |
| 123 | INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER, |
| 124 | }; |
| 125 | InterceptKeyResult interceptKeyResult; // set based on the interception result |
| 126 | nsecs_t interceptKeyWakeupTime; // used with INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER |
| 127 | |
| 128 | KeyEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 129 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t flags, |
| 130 | int32_t keyCode, int32_t scanCode, int32_t metaState, int32_t repeatCount, |
| 131 | nsecs_t downTime); |
| 132 | virtual void appendDescription(std::string& msg) const; |
| 133 | void recycle(); |
| 134 | |
| 135 | protected: |
| 136 | virtual ~KeyEntry(); |
| 137 | }; |
| 138 | |
| 139 | struct MotionEntry : EventEntry { |
| 140 | nsecs_t eventTime; |
| 141 | int32_t deviceId; |
| 142 | uint32_t source; |
| 143 | int32_t displayId; |
| 144 | int32_t action; |
| 145 | int32_t actionButton; |
| 146 | int32_t flags; |
| 147 | int32_t metaState; |
| 148 | int32_t buttonState; |
| 149 | MotionClassification classification; |
| 150 | int32_t edgeFlags; |
| 151 | float xPrecision; |
| 152 | float yPrecision; |
| 153 | float xCursorPosition; |
| 154 | float yCursorPosition; |
| 155 | nsecs_t downTime; |
| 156 | uint32_t pointerCount; |
| 157 | PointerProperties pointerProperties[MAX_POINTERS]; |
| 158 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 159 | |
| 160 | MotionEntry(uint32_t sequenceNum, nsecs_t eventTime, int32_t deviceId, uint32_t source, |
| 161 | int32_t displayId, uint32_t policyFlags, int32_t action, int32_t actionButton, |
| 162 | int32_t flags, int32_t metaState, int32_t buttonState, |
| 163 | MotionClassification classification, int32_t edgeFlags, float xPrecision, |
| 164 | float yPrecision, float xCursorPosition, float yCursorPosition, nsecs_t downTime, |
| 165 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 166 | const PointerCoords* pointerCoords, float xOffset, float yOffset); |
| 167 | virtual void appendDescription(std::string& msg) const; |
| 168 | |
| 169 | protected: |
| 170 | virtual ~MotionEntry(); |
| 171 | }; |
| 172 | |
| 173 | // Tracks the progress of dispatching a particular event to a particular connection. |
| 174 | struct DispatchEntry { |
| 175 | const uint32_t seq; // unique sequence number, never 0 |
| 176 | |
| 177 | EventEntry* eventEntry; // the event to dispatch |
| 178 | int32_t targetFlags; |
| 179 | float xOffset; |
| 180 | float yOffset; |
| 181 | float globalScaleFactor; |
| 182 | float windowXScale = 1.0f; |
| 183 | float windowYScale = 1.0f; |
| 184 | nsecs_t deliveryTime; // time when the event was actually delivered |
| 185 | |
| 186 | // Set to the resolved action and flags when the event is enqueued. |
| 187 | int32_t resolvedAction; |
| 188 | int32_t resolvedFlags; |
| 189 | |
| 190 | DispatchEntry(EventEntry* eventEntry, int32_t targetFlags, float xOffset, float yOffset, |
| 191 | float globalScaleFactor, float windowXScale, float windowYScale); |
| 192 | ~DispatchEntry(); |
| 193 | |
| 194 | inline bool hasForegroundTarget() const { return targetFlags & InputTarget::FLAG_FOREGROUND; } |
| 195 | |
| 196 | inline bool isSplit() const { return targetFlags & InputTarget::FLAG_SPLIT; } |
| 197 | |
| 198 | private: |
| 199 | static volatile int32_t sNextSeqAtomic; |
| 200 | |
| 201 | static uint32_t nextSeq(); |
| 202 | }; |
| 203 | |
| 204 | class InputDispatcher; |
| 205 | // A command entry captures state and behavior for an action to be performed in the |
| 206 | // dispatch loop after the initial processing has taken place. It is essentially |
| 207 | // a kind of continuation used to postpone sensitive policy interactions to a point |
| 208 | // in the dispatch loop where it is safe to release the lock (generally after finishing |
| 209 | // the critical parts of the dispatch cycle). |
| 210 | // |
| 211 | // The special thing about commands is that they can voluntarily release and reacquire |
| 212 | // the dispatcher lock at will. Initially when the command starts running, the |
| 213 | // dispatcher lock is held. However, if the command needs to call into the policy to |
| 214 | // do some work, it can release the lock, do the work, then reacquire the lock again |
| 215 | // before returning. |
| 216 | // |
| 217 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch |
| 218 | // never calls into the policy while holding its lock. |
| 219 | // |
| 220 | // Commands are implicitly 'LockedInterruptible'. |
| 221 | struct CommandEntry; |
| 222 | typedef std::function<void(InputDispatcher&, CommandEntry*)> Command; |
| 223 | |
| 224 | class Connection; |
| 225 | struct CommandEntry { |
| 226 | explicit CommandEntry(Command command); |
| 227 | ~CommandEntry(); |
| 228 | |
| 229 | Command command; |
| 230 | |
| 231 | // parameters for the command (usage varies by command) |
| 232 | sp<Connection> connection; |
| 233 | nsecs_t eventTime; |
| 234 | KeyEntry* keyEntry; |
| 235 | sp<InputApplicationHandle> inputApplicationHandle; |
| 236 | std::string reason; |
| 237 | int32_t userActivityEventType; |
| 238 | uint32_t seq; |
| 239 | bool handled; |
| 240 | sp<InputChannel> inputChannel; |
| 241 | sp<IBinder> oldToken; |
| 242 | sp<IBinder> newToken; |
| 243 | }; |
| 244 | |
| 245 | } // namespace android::inputdispatcher |
| 246 | |
| 247 | #endif // _UI_INPUT_INPUTDISPATCHER_ENTRY_H |