Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_DISPATCHER_H |
| 18 | #define _UI_INPUT_DISPATCHER_H |
| 19 | |
| 20 | #include <ui/Input.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 21 | #include <ui/InputTransport.h> |
| 22 | #include <utils/KeyedVector.h> |
| 23 | #include <utils/Vector.h> |
| 24 | #include <utils/threads.h> |
| 25 | #include <utils/Timers.h> |
| 26 | #include <utils/RefBase.h> |
| 27 | #include <utils/String8.h> |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 28 | #include <utils/Looper.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | #include <utils/Pool.h> |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 30 | #include <utils/BitSet.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 31 | |
| 32 | #include <stddef.h> |
| 33 | #include <unistd.h> |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 34 | #include <limits.h> |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 35 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 36 | #include "InputWindow.h" |
| 37 | #include "InputApplication.h" |
| 38 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 39 | |
| 40 | namespace android { |
| 41 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 42 | /* |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 43 | * Constants used to report the outcome of input event injection. |
| 44 | */ |
| 45 | enum { |
| 46 | /* (INTERNAL USE ONLY) Specifies that injection is pending and its outcome is unknown. */ |
| 47 | INPUT_EVENT_INJECTION_PENDING = -1, |
| 48 | |
| 49 | /* Injection succeeded. */ |
| 50 | INPUT_EVENT_INJECTION_SUCCEEDED = 0, |
| 51 | |
| 52 | /* Injection failed because the injector did not have permission to inject |
| 53 | * into the application with input focus. */ |
| 54 | INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1, |
| 55 | |
| 56 | /* Injection failed because there were no available input targets. */ |
| 57 | INPUT_EVENT_INJECTION_FAILED = 2, |
| 58 | |
| 59 | /* Injection failed due to a timeout. */ |
| 60 | INPUT_EVENT_INJECTION_TIMED_OUT = 3 |
| 61 | }; |
| 62 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Constants used to determine the input event injection synchronization mode. |
| 65 | */ |
| 66 | enum { |
| 67 | /* Injection is asynchronous and is assumed always to be successful. */ |
| 68 | INPUT_EVENT_INJECTION_SYNC_NONE = 0, |
| 69 | |
| 70 | /* Waits for previous events to be dispatched so that the input dispatcher can determine |
| 71 | * whether input event injection willbe permitted based on the current input focus. |
| 72 | * Does not wait for the input event to finish processing. */ |
| 73 | INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1, |
| 74 | |
| 75 | /* Waits for the input event to be completely processed. */ |
| 76 | INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED = 2, |
| 77 | }; |
| 78 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 81 | * An input target specifies how an input event is to be dispatched to a particular window |
| 82 | * including the window's input channel, control flags, a timeout, and an X / Y offset to |
| 83 | * be added to input event coordinates to compensate for the absolute position of the |
| 84 | * window area. |
| 85 | */ |
| 86 | struct InputTarget { |
| 87 | enum { |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 88 | /* This flag indicates that the event is being delivered to a foreground application. */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 89 | FLAG_FOREGROUND = 1 << 0, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 90 | |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 91 | /* This flag indicates that the target of a MotionEvent is partly or wholly |
| 92 | * obscured by another visible window above it. The motion event should be |
| 93 | * delivered with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED. */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 94 | FLAG_WINDOW_IS_OBSCURED = 1 << 1, |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 95 | |
| 96 | /* This flag indicates that a motion event is being split across multiple windows. */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 97 | FLAG_SPLIT = 1 << 2, |
| 98 | |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame^] | 99 | /* This flag indicates that the pointer coordinates dispatched to the application |
| 100 | * will be zeroed out to avoid revealing information to an application. This is |
| 101 | * used in conjunction with FLAG_DISPATCH_AS_OUTSIDE to prevent apps not sharing |
| 102 | * the same UID from watching all touches. */ |
| 103 | FLAG_ZERO_COORDS = 1 << 3, |
| 104 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 105 | /* This flag indicates that the event should be sent as is. |
| 106 | * Should always be set unless the event is to be transmuted. */ |
| 107 | FLAG_DISPATCH_AS_IS = 1 << 8, |
| 108 | |
| 109 | /* This flag indicates that a MotionEvent with AMOTION_EVENT_ACTION_DOWN falls outside |
| 110 | * of the area of this target and so should instead be delivered as an |
| 111 | * AMOTION_EVENT_ACTION_OUTSIDE to this target. */ |
| 112 | FLAG_DISPATCH_AS_OUTSIDE = 1 << 9, |
| 113 | |
| 114 | /* This flag indicates that a hover sequence is starting in the given window. |
| 115 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 116 | FLAG_DISPATCH_AS_HOVER_ENTER = 1 << 10, |
| 117 | |
| 118 | /* This flag indicates that a hover event happened outside of a window which handled |
| 119 | * previous hover events, signifying the end of the current hover sequence for that |
| 120 | * window. |
| 121 | * The event is transmuted into ACTION_HOVER_ENTER. */ |
| 122 | FLAG_DISPATCH_AS_HOVER_EXIT = 1 << 11, |
| 123 | |
| 124 | /* Mask for all dispatch modes. */ |
| 125 | FLAG_DISPATCH_MASK = FLAG_DISPATCH_AS_IS |
| 126 | | FLAG_DISPATCH_AS_OUTSIDE |
| 127 | | FLAG_DISPATCH_AS_HOVER_ENTER |
| 128 | | FLAG_DISPATCH_AS_HOVER_EXIT, |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // The input channel to be targeted. |
| 132 | sp<InputChannel> inputChannel; |
| 133 | |
| 134 | // Flags for the input target. |
| 135 | int32_t flags; |
| 136 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 137 | // The x and y offset to add to a MotionEvent as it is delivered. |
| 138 | // (ignored for KeyEvents) |
| 139 | float xOffset, yOffset; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 140 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 141 | // Scaling factor to apply to MotionEvent as it is delivered. |
| 142 | // (ignored for KeyEvents) |
| 143 | float scaleFactor; |
| 144 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 145 | // The subset of pointer ids to include in motion events dispatched to this input target |
| 146 | // if FLAG_SPLIT is set. |
| 147 | BitSet32 pointerIds; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 150 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 151 | /* |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 152 | * Input dispatcher configuration. |
| 153 | * |
| 154 | * Specifies various options that modify the behavior of the input dispatcher. |
| 155 | */ |
| 156 | struct InputDispatcherConfiguration { |
| 157 | // The key repeat initial timeout. |
| 158 | nsecs_t keyRepeatTimeout; |
| 159 | |
| 160 | // The key repeat inter-key delay. |
| 161 | nsecs_t keyRepeatDelay; |
| 162 | |
| 163 | // The maximum suggested event delivery rate per second. |
| 164 | // This value is used to throttle motion event movement actions on a per-device |
| 165 | // basis. It is not intended to be a hard limit. |
| 166 | int32_t maxEventsPerSecond; |
| 167 | |
| 168 | InputDispatcherConfiguration() : |
| 169 | keyRepeatTimeout(500 * 1000000LL), |
| 170 | keyRepeatDelay(50 * 1000000LL), |
| 171 | maxEventsPerSecond(60) { } |
| 172 | }; |
| 173 | |
| 174 | |
| 175 | /* |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 176 | * Input dispatcher policy interface. |
| 177 | * |
| 178 | * The input reader policy is used by the input reader to interact with the Window Manager |
| 179 | * and other system components. |
| 180 | * |
| 181 | * The actual implementation is partially supported by callbacks into the DVM |
| 182 | * via JNI. This interface is also mocked in the unit tests. |
| 183 | */ |
| 184 | class InputDispatcherPolicyInterface : public virtual RefBase { |
| 185 | protected: |
| 186 | InputDispatcherPolicyInterface() { } |
| 187 | virtual ~InputDispatcherPolicyInterface() { } |
| 188 | |
| 189 | public: |
| 190 | /* Notifies the system that a configuration change has occurred. */ |
| 191 | virtual void notifyConfigurationChanged(nsecs_t when) = 0; |
| 192 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 193 | /* Notifies the system that an application is not responding. |
| 194 | * Returns a new timeout to continue waiting, or 0 to abort dispatch. */ |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 195 | virtual nsecs_t notifyANR(const sp<InputApplicationHandle>& inputApplicationHandle, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 196 | const sp<InputWindowHandle>& inputWindowHandle) = 0; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 197 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 198 | /* Notifies the system that an input channel is unrecoverably broken. */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 199 | virtual void notifyInputChannelBroken(const sp<InputWindowHandle>& inputWindowHandle) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 200 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 201 | /* Gets the input dispatcher configuration. */ |
| 202 | virtual void getDispatcherConfiguration(InputDispatcherConfiguration* outConfig) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 203 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 204 | /* Returns true if automatic key repeating is enabled. */ |
| 205 | virtual bool isKeyRepeatEnabled() = 0; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 206 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 207 | /* Filters an input event. |
| 208 | * Return true to dispatch the event unmodified, false to consume the event. |
| 209 | * A filter can also transform and inject events later by passing POLICY_FLAG_FILTERED |
| 210 | * to injectInputEvent. |
| 211 | */ |
| 212 | virtual bool filterInputEvent(const InputEvent* inputEvent, uint32_t policyFlags) = 0; |
| 213 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 214 | /* Intercepts a key event immediately before queueing it. |
| 215 | * The policy can use this method as an opportunity to perform power management functions |
| 216 | * and early event preprocessing such as updating policy flags. |
| 217 | * |
| 218 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 219 | * should be dispatched to applications. |
| 220 | */ |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 221 | virtual void interceptKeyBeforeQueueing(const KeyEvent* keyEvent, uint32_t& policyFlags) = 0; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 222 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 223 | /* Intercepts a touch, trackball or other motion event before queueing it. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 224 | * The policy can use this method as an opportunity to perform power management functions |
| 225 | * and early event preprocessing such as updating policy flags. |
| 226 | * |
| 227 | * This method is expected to set the POLICY_FLAG_PASS_TO_USER policy flag if the event |
| 228 | * should be dispatched to applications. |
| 229 | */ |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 230 | virtual void interceptMotionBeforeQueueing(nsecs_t when, uint32_t& policyFlags) = 0; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 231 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 232 | /* Allows the policy a chance to intercept a key before dispatching. */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 233 | virtual bool interceptKeyBeforeDispatching(const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 234 | const KeyEvent* keyEvent, uint32_t policyFlags) = 0; |
| 235 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 236 | /* Allows the policy a chance to perform default processing for an unhandled key. |
| 237 | * Returns an alternate keycode to redispatch as a fallback, or 0 to give up. */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 238 | virtual bool dispatchUnhandledKey(const sp<InputWindowHandle>& inputWindowHandle, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 239 | const KeyEvent* keyEvent, uint32_t policyFlags, KeyEvent* outFallbackKeyEvent) = 0; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 240 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 241 | /* Notifies the policy about switch events. |
| 242 | */ |
| 243 | virtual void notifySwitch(nsecs_t when, |
| 244 | int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0; |
| 245 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 246 | /* Poke user activity for an event dispatched to a window. */ |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 247 | virtual void pokeUserActivity(nsecs_t eventTime, int32_t eventType) = 0; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 248 | |
| 249 | /* Checks whether a given application pid/uid has permission to inject input events |
| 250 | * into other applications. |
| 251 | * |
| 252 | * This method is special in that its implementation promises to be non-reentrant and |
| 253 | * is safe to call while holding other locks. (Most other methods make no such guarantees!) |
| 254 | */ |
| 255 | virtual bool checkInjectEventsPermissionNonReentrant( |
| 256 | int32_t injectorPid, int32_t injectorUid) = 0; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 260 | /* Notifies the system about input events generated by the input reader. |
| 261 | * The dispatcher is expected to be mostly asynchronous. */ |
| 262 | class InputDispatcherInterface : public virtual RefBase { |
| 263 | protected: |
| 264 | InputDispatcherInterface() { } |
| 265 | virtual ~InputDispatcherInterface() { } |
| 266 | |
| 267 | public: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 268 | /* Dumps the state of the input dispatcher. |
| 269 | * |
| 270 | * This method may be called on any thread (usually by the input manager). */ |
| 271 | virtual void dump(String8& dump) = 0; |
| 272 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 273 | /* Runs a single iteration of the dispatch loop. |
| 274 | * Nominally processes one queued event, a timeout, or a response from an input consumer. |
| 275 | * |
| 276 | * This method should only be called on the input dispatcher thread. |
| 277 | */ |
| 278 | virtual void dispatchOnce() = 0; |
| 279 | |
| 280 | /* Notifies the dispatcher about new events. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 281 | * |
| 282 | * These methods should only be called on the input reader thread. |
| 283 | */ |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 284 | virtual void notifyConfigurationChanged(nsecs_t eventTime) = 0; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 285 | virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 286 | uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, |
| 287 | int32_t scanCode, int32_t metaState, nsecs_t downTime) = 0; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 288 | virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 289 | uint32_t policyFlags, int32_t action, int32_t flags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 290 | int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 291 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 292 | const PointerCoords* pointerCoords, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 293 | float xPrecision, float yPrecision, nsecs_t downTime) = 0; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 294 | virtual void notifySwitch(nsecs_t when, |
| 295 | int32_t switchCode, int32_t switchValue, uint32_t policyFlags) = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 296 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 297 | /* Injects an input event and optionally waits for sync. |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 298 | * The synchronization mode determines whether the method blocks while waiting for |
| 299 | * input injection to proceed. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 300 | * Returns one of the INPUT_EVENT_INJECTION_XXX constants. |
| 301 | * |
| 302 | * This method may be called on any thread (usually by the input manager). |
| 303 | */ |
| 304 | virtual int32_t injectInputEvent(const InputEvent* event, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 305 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 306 | uint32_t policyFlags) = 0; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 307 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 308 | /* Sets the list of input windows. |
| 309 | * |
| 310 | * This method may be called on any thread (usually by the input manager). |
| 311 | */ |
| 312 | virtual void setInputWindows(const Vector<InputWindow>& inputWindows) = 0; |
| 313 | |
| 314 | /* Sets the focused application. |
| 315 | * |
| 316 | * This method may be called on any thread (usually by the input manager). |
| 317 | */ |
| 318 | virtual void setFocusedApplication(const InputApplication* inputApplication) = 0; |
| 319 | |
| 320 | /* Sets the input dispatching mode. |
| 321 | * |
| 322 | * This method may be called on any thread (usually by the input manager). |
| 323 | */ |
| 324 | virtual void setInputDispatchMode(bool enabled, bool frozen) = 0; |
| 325 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 326 | /* Sets whether input event filtering is enabled. |
| 327 | * When enabled, incoming input events are sent to the policy's filterInputEvent |
| 328 | * method instead of being dispatched. The filter is expected to use |
| 329 | * injectInputEvent to inject the events it would like to have dispatched. |
| 330 | * It should include POLICY_FLAG_FILTERED in the policy flags during injection. |
| 331 | */ |
| 332 | virtual void setInputFilterEnabled(bool enabled) = 0; |
| 333 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 334 | /* Transfers touch focus from the window associated with one channel to the |
| 335 | * window associated with the other channel. |
| 336 | * |
| 337 | * Returns true on success. False if the window did not actually have touch focus. |
| 338 | */ |
| 339 | virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel, |
| 340 | const sp<InputChannel>& toChannel) = 0; |
| 341 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 342 | /* Registers or unregister input channels that may be used as targets for input events. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 343 | * If monitor is true, the channel will receive a copy of all input events. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 344 | * |
| 345 | * These methods may be called on any thread (usually by the input manager). |
| 346 | */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 347 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, |
| 348 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) = 0; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 349 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0; |
| 350 | }; |
| 351 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 352 | /* Dispatches events to input targets. Some functions of the input dispatcher, such as |
| 353 | * identifying input targets, are controlled by a separate policy object. |
| 354 | * |
| 355 | * IMPORTANT INVARIANT: |
| 356 | * Because the policy can potentially block or cause re-entrance into the input dispatcher, |
| 357 | * the input dispatcher never calls into the policy while holding its internal locks. |
| 358 | * The implementation is also carefully designed to recover from scenarios such as an |
| 359 | * input channel becoming unregistered while identifying input targets or processing timeouts. |
| 360 | * |
| 361 | * Methods marked 'Locked' must be called with the lock acquired. |
| 362 | * |
| 363 | * Methods marked 'LockedInterruptible' must be called with the lock acquired but |
| 364 | * may during the course of their execution release the lock, call into the policy, and |
| 365 | * then reacquire the lock. The caller is responsible for recovering gracefully. |
| 366 | * |
| 367 | * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa. |
| 368 | */ |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | class InputDispatcher : public InputDispatcherInterface { |
| 370 | protected: |
| 371 | virtual ~InputDispatcher(); |
| 372 | |
| 373 | public: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 374 | explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 375 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 376 | virtual void dump(String8& dump); |
| 377 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 378 | virtual void dispatchOnce(); |
| 379 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 380 | virtual void notifyConfigurationChanged(nsecs_t eventTime); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 381 | virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 382 | uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, |
| 383 | int32_t scanCode, int32_t metaState, nsecs_t downTime); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 384 | virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, uint32_t source, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 385 | uint32_t policyFlags, int32_t action, int32_t flags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 386 | int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
| 387 | uint32_t pointerCount, const PointerProperties* pointerProperties, |
| 388 | const PointerCoords* pointerCoords, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 389 | float xPrecision, float yPrecision, nsecs_t downTime); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 390 | virtual void notifySwitch(nsecs_t when, |
| 391 | int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 392 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 393 | virtual int32_t injectInputEvent(const InputEvent* event, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 394 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 395 | uint32_t policyFlags); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 396 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 397 | virtual void setInputWindows(const Vector<InputWindow>& inputWindows); |
| 398 | virtual void setFocusedApplication(const InputApplication* inputApplication); |
| 399 | virtual void setInputDispatchMode(bool enabled, bool frozen); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 400 | virtual void setInputFilterEnabled(bool enabled); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 401 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 402 | virtual bool transferTouchFocus(const sp<InputChannel>& fromChannel, |
| 403 | const sp<InputChannel>& toChannel); |
| 404 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 405 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, |
| 406 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 407 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel); |
| 408 | |
| 409 | private: |
| 410 | template <typename T> |
| 411 | struct Link { |
| 412 | T* next; |
| 413 | T* prev; |
| 414 | }; |
| 415 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 416 | struct InjectionState { |
| 417 | mutable int32_t refCount; |
| 418 | |
| 419 | int32_t injectorPid; |
| 420 | int32_t injectorUid; |
| 421 | int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING |
| 422 | bool injectionIsAsync; // set to true if injection is not waiting for the result |
| 423 | int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress |
| 424 | }; |
| 425 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 426 | struct EventEntry : Link<EventEntry> { |
| 427 | enum { |
| 428 | TYPE_SENTINEL, |
| 429 | TYPE_CONFIGURATION_CHANGED, |
| 430 | TYPE_KEY, |
| 431 | TYPE_MOTION |
| 432 | }; |
| 433 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 434 | mutable int32_t refCount; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 435 | int32_t type; |
| 436 | nsecs_t eventTime; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 437 | uint32_t policyFlags; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 438 | InjectionState* injectionState; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 439 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 440 | bool dispatchInProgress; // initially false, set to true while dispatching |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 441 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 442 | inline bool isInjected() const { return injectionState != NULL; } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | struct ConfigurationChangedEntry : EventEntry { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | struct KeyEntry : EventEntry { |
| 449 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 450 | uint32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 451 | int32_t action; |
| 452 | int32_t flags; |
| 453 | int32_t keyCode; |
| 454 | int32_t scanCode; |
| 455 | int32_t metaState; |
| 456 | int32_t repeatCount; |
| 457 | nsecs_t downTime; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 458 | |
| 459 | bool syntheticRepeat; // set to true for synthetic key repeats |
| 460 | |
| 461 | enum InterceptKeyResult { |
| 462 | INTERCEPT_KEY_RESULT_UNKNOWN, |
| 463 | INTERCEPT_KEY_RESULT_SKIP, |
| 464 | INTERCEPT_KEY_RESULT_CONTINUE, |
| 465 | }; |
| 466 | InterceptKeyResult interceptKeyResult; // set based on the interception result |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | struct MotionSample { |
| 470 | MotionSample* next; |
| 471 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 472 | nsecs_t eventTime; // may be updated during coalescing |
| 473 | nsecs_t eventTimeBeforeCoalescing; // not updated during coalescing |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 474 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 475 | }; |
| 476 | |
| 477 | struct MotionEntry : EventEntry { |
| 478 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 479 | uint32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 480 | int32_t action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 481 | int32_t flags; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 482 | int32_t metaState; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 483 | int32_t buttonState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 484 | int32_t edgeFlags; |
| 485 | float xPrecision; |
| 486 | float yPrecision; |
| 487 | nsecs_t downTime; |
| 488 | uint32_t pointerCount; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 489 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 490 | |
| 491 | // Linked list of motion samples associated with this motion event. |
| 492 | MotionSample firstSample; |
| 493 | MotionSample* lastSample; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 494 | |
| 495 | uint32_t countSamples() const; |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 496 | |
| 497 | // Checks whether we can append samples, assuming the device id and source are the same. |
| 498 | bool canAppendSamples(int32_t action, uint32_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 499 | const PointerProperties* pointerProperties) const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 502 | // Tracks the progress of dispatching a particular event to a particular connection. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 503 | struct DispatchEntry : Link<DispatchEntry> { |
| 504 | EventEntry* eventEntry; // the event to dispatch |
| 505 | int32_t targetFlags; |
| 506 | float xOffset; |
| 507 | float yOffset; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 508 | float scaleFactor; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 509 | |
| 510 | // True if dispatch has started. |
| 511 | bool inProgress; |
| 512 | |
| 513 | // For motion events: |
| 514 | // Pointer to the first motion sample to dispatch in this cycle. |
| 515 | // Usually NULL to indicate that the list of motion samples begins at |
| 516 | // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous |
| 517 | // cycle and this pointer indicates the location of the first remainining sample |
| 518 | // to dispatch during the current cycle. |
| 519 | MotionSample* headMotionSample; |
| 520 | // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was |
| 521 | // unable to send all motion samples during this cycle. On the next cycle, |
| 522 | // headMotionSample will be initialized to tailMotionSample and tailMotionSample |
| 523 | // will be set to NULL. |
| 524 | MotionSample* tailMotionSample; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 525 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 526 | inline bool hasForegroundTarget() const { |
| 527 | return targetFlags & InputTarget::FLAG_FOREGROUND; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 528 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 529 | |
| 530 | inline bool isSplit() const { |
| 531 | return targetFlags & InputTarget::FLAG_SPLIT; |
| 532 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 533 | }; |
| 534 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 535 | // A command entry captures state and behavior for an action to be performed in the |
| 536 | // dispatch loop after the initial processing has taken place. It is essentially |
| 537 | // a kind of continuation used to postpone sensitive policy interactions to a point |
| 538 | // in the dispatch loop where it is safe to release the lock (generally after finishing |
| 539 | // the critical parts of the dispatch cycle). |
| 540 | // |
| 541 | // The special thing about commands is that they can voluntarily release and reacquire |
| 542 | // the dispatcher lock at will. Initially when the command starts running, the |
| 543 | // dispatcher lock is held. However, if the command needs to call into the policy to |
| 544 | // do some work, it can release the lock, do the work, then reacquire the lock again |
| 545 | // before returning. |
| 546 | // |
| 547 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch |
| 548 | // never calls into the policy while holding its lock. |
| 549 | // |
| 550 | // Commands are implicitly 'LockedInterruptible'. |
| 551 | struct CommandEntry; |
| 552 | typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry); |
| 553 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 554 | class Connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 555 | struct CommandEntry : Link<CommandEntry> { |
| 556 | CommandEntry(); |
| 557 | ~CommandEntry(); |
| 558 | |
| 559 | Command command; |
| 560 | |
| 561 | // parameters for the command (usage varies by command) |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 562 | sp<Connection> connection; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 563 | nsecs_t eventTime; |
| 564 | KeyEntry* keyEntry; |
| 565 | sp<InputChannel> inputChannel; |
| 566 | sp<InputApplicationHandle> inputApplicationHandle; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 567 | sp<InputWindowHandle> inputWindowHandle; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 568 | int32_t userActivityEventType; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 569 | bool handled; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | // Generic queue implementation. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 573 | template <typename T> |
| 574 | struct Queue { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 575 | T headSentinel; |
| 576 | T tailSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 577 | |
| 578 | inline Queue() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 579 | headSentinel.prev = NULL; |
| 580 | headSentinel.next = & tailSentinel; |
| 581 | tailSentinel.prev = & headSentinel; |
| 582 | tailSentinel.next = NULL; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 585 | inline bool isEmpty() const { |
| 586 | return headSentinel.next == & tailSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | inline void enqueueAtTail(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 590 | T* last = tailSentinel.prev; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 591 | last->next = entry; |
| 592 | entry->prev = last; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 593 | entry->next = & tailSentinel; |
| 594 | tailSentinel.prev = entry; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | inline void enqueueAtHead(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 598 | T* first = headSentinel.next; |
| 599 | headSentinel.next = entry; |
| 600 | entry->prev = & headSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 601 | entry->next = first; |
| 602 | first->prev = entry; |
| 603 | } |
| 604 | |
| 605 | inline void dequeue(T* entry) { |
| 606 | entry->prev->next = entry->next; |
| 607 | entry->next->prev = entry->prev; |
| 608 | } |
| 609 | |
| 610 | inline T* dequeueAtHead() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 611 | T* first = headSentinel.next; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 612 | dequeue(first); |
| 613 | return first; |
| 614 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 615 | |
| 616 | uint32_t count() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | /* Allocates queue entries and performs reference counting as needed. */ |
| 620 | class Allocator { |
| 621 | public: |
| 622 | Allocator(); |
| 623 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 624 | InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 625 | ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime); |
| 626 | KeyEntry* obtainKeyEntry(nsecs_t eventTime, |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 627 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 628 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
| 629 | int32_t repeatCount, nsecs_t downTime); |
| 630 | MotionEntry* obtainMotionEntry(nsecs_t eventTime, |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 631 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 632 | int32_t flags, int32_t metaState, int32_t buttonState, int32_t edgeFlags, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 633 | float xPrecision, float yPrecision, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 634 | nsecs_t downTime, uint32_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 635 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 636 | DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 637 | int32_t targetFlags, float xOffset, float yOffset, float scaleFactor); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 638 | CommandEntry* obtainCommandEntry(Command command); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 639 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 640 | void releaseInjectionState(InjectionState* injectionState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 641 | void releaseEventEntry(EventEntry* entry); |
| 642 | void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry); |
| 643 | void releaseKeyEntry(KeyEntry* entry); |
| 644 | void releaseMotionEntry(MotionEntry* entry); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 645 | void freeMotionSample(MotionSample* sample); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 646 | void releaseDispatchEntry(DispatchEntry* entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 647 | void releaseCommandEntry(CommandEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 648 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 649 | void recycleKeyEntry(KeyEntry* entry); |
| 650 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 651 | void appendMotionSample(MotionEntry* motionEntry, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 652 | nsecs_t eventTime, const PointerCoords* pointerCoords); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 653 | |
| 654 | private: |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 655 | Pool<InjectionState> mInjectionStatePool; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 656 | Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool; |
| 657 | Pool<KeyEntry> mKeyEntryPool; |
| 658 | Pool<MotionEntry> mMotionEntryPool; |
| 659 | Pool<MotionSample> mMotionSamplePool; |
| 660 | Pool<DispatchEntry> mDispatchEntryPool; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 661 | Pool<CommandEntry> mCommandEntryPool; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 662 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 663 | void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime, |
| 664 | uint32_t policyFlags); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 665 | void releaseEventEntryInjectionState(EventEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 666 | }; |
| 667 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 668 | /* Specifies which events are to be canceled and why. */ |
| 669 | struct CancelationOptions { |
| 670 | enum Mode { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 671 | CANCEL_ALL_EVENTS = 0, |
| 672 | CANCEL_POINTER_EVENTS = 1, |
| 673 | CANCEL_NON_POINTER_EVENTS = 2, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 674 | CANCEL_FALLBACK_EVENTS = 3, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 675 | }; |
| 676 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 677 | // The criterion to use to determine which events should be canceled. |
| 678 | Mode mode; |
| 679 | |
| 680 | // Descriptive reason for the cancelation. |
| 681 | const char* reason; |
| 682 | |
| 683 | // The specific keycode of the key event to cancel, or -1 to cancel any key event. |
| 684 | int32_t keyCode; |
| 685 | |
| 686 | CancelationOptions(Mode mode, const char* reason) : |
| 687 | mode(mode), reason(reason), keyCode(-1) { } |
| 688 | }; |
| 689 | |
| 690 | /* Tracks dispatched key and motion event state so that cancelation events can be |
| 691 | * synthesized when events are dropped. */ |
| 692 | class InputState { |
| 693 | public: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 694 | InputState(); |
| 695 | ~InputState(); |
| 696 | |
| 697 | // Returns true if there is no state to be canceled. |
| 698 | bool isNeutral() const; |
| 699 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 700 | // Records tracking information for an event that has just been published. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 701 | void trackEvent(const EventEntry* entry, int32_t action); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 702 | |
| 703 | // Records tracking information for a key event that has just been published. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 704 | void trackKey(const KeyEntry* entry, int32_t action); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 705 | |
| 706 | // Records tracking information for a motion event that has just been published. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 707 | void trackMotion(const MotionEntry* entry, int32_t action); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 708 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 709 | // Synthesizes cancelation events for the current state and resets the tracked state. |
| 710 | void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 711 | Vector<EventEntry*>& outEvents, const CancelationOptions& options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 712 | |
| 713 | // Clears the current state. |
| 714 | void clear(); |
| 715 | |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 716 | // Copies pointer-related parts of the input state to another instance. |
| 717 | void copyPointerStateTo(InputState& other) const; |
| 718 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 719 | // Gets the fallback key associated with a keycode. |
| 720 | // Returns -1 if none. |
| 721 | // Returns AKEYCODE_UNKNOWN if we are only dispatching the unhandled key to the policy. |
| 722 | int32_t getFallbackKey(int32_t originalKeyCode); |
| 723 | |
| 724 | // Sets the fallback key for a particular keycode. |
| 725 | void setFallbackKey(int32_t originalKeyCode, int32_t fallbackKeyCode); |
| 726 | |
| 727 | // Removes the fallback key for a particular keycode. |
| 728 | void removeFallbackKey(int32_t originalKeyCode); |
| 729 | |
| 730 | inline const KeyedVector<int32_t, int32_t>& getFallbackKeys() const { |
| 731 | return mFallbackKeys; |
| 732 | } |
| 733 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 734 | private: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 735 | struct KeyMemento { |
| 736 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 737 | uint32_t source; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 738 | int32_t keyCode; |
| 739 | int32_t scanCode; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 740 | int32_t flags; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 741 | nsecs_t downTime; |
| 742 | }; |
| 743 | |
| 744 | struct MotionMemento { |
| 745 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 746 | uint32_t source; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 747 | float xPrecision; |
| 748 | float yPrecision; |
| 749 | nsecs_t downTime; |
| 750 | uint32_t pointerCount; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 751 | PointerProperties pointerProperties[MAX_POINTERS]; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 752 | PointerCoords pointerCoords[MAX_POINTERS]; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 753 | bool hovering; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 754 | |
| 755 | void setPointers(const MotionEntry* entry); |
| 756 | }; |
| 757 | |
| 758 | Vector<KeyMemento> mKeyMementos; |
| 759 | Vector<MotionMemento> mMotionMementos; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 760 | KeyedVector<int32_t, int32_t> mFallbackKeys; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 761 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 762 | static bool shouldCancelKey(const KeyMemento& memento, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 763 | const CancelationOptions& options); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 764 | static bool shouldCancelMotion(const MotionMemento& memento, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 765 | const CancelationOptions& options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 766 | }; |
| 767 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 768 | /* Manages the dispatch state associated with a single input channel. */ |
| 769 | class Connection : public RefBase { |
| 770 | protected: |
| 771 | virtual ~Connection(); |
| 772 | |
| 773 | public: |
| 774 | enum Status { |
| 775 | // Everything is peachy. |
| 776 | STATUS_NORMAL, |
| 777 | // An unrecoverable communication error has occurred. |
| 778 | STATUS_BROKEN, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 779 | // The input channel has been unregistered. |
| 780 | STATUS_ZOMBIE |
| 781 | }; |
| 782 | |
| 783 | Status status; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 784 | sp<InputChannel> inputChannel; // never null |
| 785 | sp<InputWindowHandle> inputWindowHandle; // may be null |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 786 | InputPublisher inputPublisher; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 787 | InputState inputState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 788 | Queue<DispatchEntry> outboundQueue; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 789 | |
| 790 | nsecs_t lastEventTime; // the time when the event was originally captured |
| 791 | nsecs_t lastDispatchTime; // the time when the last event was dispatched |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 792 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 793 | explicit Connection(const sp<InputChannel>& inputChannel, |
| 794 | const sp<InputWindowHandle>& inputWindowHandle); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 795 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 796 | inline const char* getInputChannelName() const { return inputChannel->getName().string(); } |
| 797 | |
| 798 | const char* getStatusLabel() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 799 | |
| 800 | // Finds a DispatchEntry in the outbound queue associated with the specified event. |
| 801 | // Returns NULL if not found. |
| 802 | DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const; |
| 803 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 804 | // Gets the time since the current event was originally obtained from the input driver. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 805 | inline double getEventLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 806 | return (currentTime - lastEventTime) / 1000000.0; |
| 807 | } |
| 808 | |
| 809 | // Gets the time since the current event entered the outbound dispatch queue. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 810 | inline double getDispatchLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 811 | return (currentTime - lastDispatchTime) / 1000000.0; |
| 812 | } |
| 813 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 814 | status_t initialize(); |
| 815 | }; |
| 816 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 817 | enum DropReason { |
| 818 | DROP_REASON_NOT_DROPPED = 0, |
| 819 | DROP_REASON_POLICY = 1, |
| 820 | DROP_REASON_APP_SWITCH = 2, |
| 821 | DROP_REASON_DISABLED = 3, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 822 | DROP_REASON_BLOCKED = 4, |
| 823 | DROP_REASON_STALE = 5, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 824 | }; |
| 825 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 826 | sp<InputDispatcherPolicyInterface> mPolicy; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 827 | InputDispatcherConfiguration mConfig; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 828 | |
| 829 | Mutex mLock; |
| 830 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 831 | Allocator mAllocator; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 832 | sp<Looper> mLooper; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 833 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 834 | EventEntry* mPendingEvent; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 835 | Queue<EventEntry> mInboundQueue; |
| 836 | Queue<CommandEntry> mCommandQueue; |
| 837 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 838 | Vector<EventEntry*> mTempCancelationEvents; |
| 839 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 840 | void dispatchOnceInnerLocked(nsecs_t* nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 841 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 842 | // Batches a new sample onto a motion entry. |
| 843 | // Assumes that the we have already checked that we can append samples. |
| 844 | void batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, int32_t metaState, |
| 845 | const PointerCoords* pointerCoords, const char* eventDescription); |
| 846 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 847 | // Enqueues an inbound event. Returns true if mLooper->wake() should be called. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 848 | bool enqueueInboundEventLocked(EventEntry* entry); |
| 849 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 850 | // Cleans up input state when dropping an inbound event. |
| 851 | void dropInboundEventLocked(EventEntry* entry, DropReason dropReason); |
| 852 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 853 | // App switch latency optimization. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 854 | bool mAppSwitchSawKeyDown; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 855 | nsecs_t mAppSwitchDueTime; |
| 856 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 857 | static bool isAppSwitchKeyCode(int32_t keyCode); |
| 858 | bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 859 | bool isAppSwitchPendingLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 860 | void resetPendingAppSwitchLocked(bool handled); |
| 861 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 862 | // Stale event latency optimization. |
| 863 | static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry); |
| 864 | |
| 865 | // Blocked event latency optimization. Drops old events when the user intends |
| 866 | // to transfer focus to a new application. |
| 867 | EventEntry* mNextUnblockedEvent; |
| 868 | |
| 869 | const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y); |
| 870 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 871 | // All registered connections mapped by receive pipe file descriptor. |
| 872 | KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd; |
| 873 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 874 | ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 875 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 876 | // Active connections are connections that have a non-empty outbound queue. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 877 | // We don't use a ref-counted pointer here because we explicitly abort connections |
| 878 | // during unregistration which causes the connection's outbound queue to be cleared |
| 879 | // and the connection itself to be deactivated. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 880 | Vector<Connection*> mActiveConnections; |
| 881 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 882 | // Input channels that will receive a copy of all input events. |
| 883 | Vector<sp<InputChannel> > mMonitoringChannels; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 884 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 885 | // Event injection and synchronization. |
| 886 | Condition mInjectionResultAvailableCondition; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 887 | bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 888 | void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult); |
| 889 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 890 | Condition mInjectionSyncFinishedCondition; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 891 | void incrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 892 | void decrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 893 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 894 | // Throttling state. |
| 895 | struct ThrottleState { |
| 896 | nsecs_t minTimeBetweenEvents; |
| 897 | |
| 898 | nsecs_t lastEventTime; |
| 899 | int32_t lastDeviceId; |
| 900 | uint32_t lastSource; |
| 901 | |
| 902 | uint32_t originalSampleCount; // only collected during debugging |
| 903 | } mThrottleState; |
| 904 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 905 | // Key repeat tracking. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 906 | struct KeyRepeatState { |
| 907 | KeyEntry* lastKeyEntry; // or null if no repeat |
| 908 | nsecs_t nextRepeatTime; |
| 909 | } mKeyRepeatState; |
| 910 | |
| 911 | void resetKeyRepeatLocked(); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 912 | KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 913 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 914 | // Deferred command processing. |
| 915 | bool runCommandsLockedInterruptible(); |
| 916 | CommandEntry* postCommandLocked(Command command); |
| 917 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 918 | // Inbound event processing. |
| 919 | void drainInboundQueueLocked(); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 920 | void releasePendingEventLocked(); |
| 921 | void releaseInboundEventLocked(EventEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 922 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 923 | // Dispatch state. |
| 924 | bool mDispatchEnabled; |
| 925 | bool mDispatchFrozen; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 926 | bool mInputFilterEnabled; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 927 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 928 | Vector<InputWindow> mWindows; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 929 | |
| 930 | const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 931 | |
| 932 | // Focus tracking for keys, trackball, etc. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 933 | const InputWindow* mFocusedWindow; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 934 | |
| 935 | // Focus tracking for touch. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 936 | struct TouchedWindow { |
| 937 | const InputWindow* window; |
| 938 | int32_t targetFlags; |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 939 | BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 940 | sp<InputChannel> channel; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 941 | }; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 942 | struct TouchState { |
| 943 | bool down; |
| 944 | bool split; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 945 | int32_t deviceId; // id of the device that is currently down, others are rejected |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 946 | uint32_t source; // source of the device that is current down, others are rejected |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 947 | Vector<TouchedWindow> windows; |
| 948 | |
| 949 | TouchState(); |
| 950 | ~TouchState(); |
| 951 | void reset(); |
| 952 | void copyFrom(const TouchState& other); |
| 953 | void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 954 | void filterNonAsIsTouchWindows(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 955 | const InputWindow* getFirstForegroundWindow(); |
| 956 | }; |
| 957 | |
| 958 | TouchState mTouchState; |
| 959 | TouchState mTempTouchState; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 960 | |
| 961 | // Focused application. |
| 962 | InputApplication* mFocusedApplication; |
| 963 | InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication |
| 964 | void releaseFocusedApplicationLocked(); |
| 965 | |
| 966 | // Dispatch inbound events. |
| 967 | bool dispatchConfigurationChangedLocked( |
| 968 | nsecs_t currentTime, ConfigurationChangedEntry* entry); |
| 969 | bool dispatchKeyLocked( |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 970 | nsecs_t currentTime, KeyEntry* entry, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 971 | DropReason* dropReason, nsecs_t* nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 972 | bool dispatchMotionLocked( |
| 973 | nsecs_t currentTime, MotionEntry* entry, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 974 | DropReason* dropReason, nsecs_t* nextWakeupTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 975 | void dispatchEventToCurrentInputTargetsLocked( |
| 976 | nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 977 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 978 | void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry); |
| 979 | void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry); |
| 980 | |
| 981 | // The input targets that were most recently identified for dispatch. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 982 | bool mCurrentInputTargetsValid; // false while targets are being recomputed |
| 983 | Vector<InputTarget> mCurrentInputTargets; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 984 | |
| 985 | enum InputTargetWaitCause { |
| 986 | INPUT_TARGET_WAIT_CAUSE_NONE, |
| 987 | INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY, |
| 988 | INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY, |
| 989 | }; |
| 990 | |
| 991 | InputTargetWaitCause mInputTargetWaitCause; |
| 992 | nsecs_t mInputTargetWaitStartTime; |
| 993 | nsecs_t mInputTargetWaitTimeoutTime; |
| 994 | bool mInputTargetWaitTimeoutExpired; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 995 | sp<InputApplicationHandle> mInputTargetWaitApplication; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 996 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 997 | // Contains the last window which received a hover event. |
| 998 | const InputWindow* mLastHoverWindow; |
| 999 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1000 | // Finding targets for input events. |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 1001 | void resetTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1002 | void commitTargetsLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1003 | int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry, |
| 1004 | const InputApplication* application, const InputWindow* window, |
| 1005 | nsecs_t* nextWakeupTime); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1006 | void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| 1007 | const sp<InputChannel>& inputChannel); |
| 1008 | nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1009 | void resetANRTimeoutsLocked(); |
| 1010 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1011 | int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 1012 | nsecs_t* nextWakeupTime); |
| 1013 | int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1014 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions, |
| 1015 | const MotionSample** outSplitBatchAfterSample); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1016 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1017 | void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags, |
| 1018 | BitSet32 pointerIds); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1019 | void addMonitoringTargetsLocked(); |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 1020 | void pokeUserActivityLocked(const EventEntry* eventEntry); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1021 | bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState); |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1022 | bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1023 | bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1024 | String8 getApplicationWindowLabelLocked(const InputApplication* application, |
| 1025 | const InputWindow* window); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1026 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1027 | // Manage the dispatch cycle for a single connection. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1028 | // These methods are deliberately not Interruptible because doing all of the work |
| 1029 | // with the mutex held makes it easier to ensure that connection invariants are maintained. |
| 1030 | // If needed, the methods post commands to run later once the critical bits are done. |
| 1031 | void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1032 | EventEntry* eventEntry, const InputTarget* inputTarget, |
| 1033 | bool resumeWithAppendedMotionSample); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1034 | void enqueueDispatchEntryLocked(const sp<Connection>& connection, |
| 1035 | EventEntry* eventEntry, const InputTarget* inputTarget, |
| 1036 | bool resumeWithAppendedMotionSample, int32_t dispatchMode); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1037 | void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1038 | void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
| 1039 | bool handled); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1040 | void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1041 | void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1042 | void drainOutboundQueueLocked(Connection* connection); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 1043 | static int handleReceiveCallback(int receiveFd, int events, void* data); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1044 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1045 | void synthesizeCancelationEventsForAllConnectionsLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 1046 | const CancelationOptions& options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1047 | void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 1048 | const CancelationOptions& options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1049 | void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 1050 | const CancelationOptions& options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1051 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1052 | // Splitting motion events across windows. |
| 1053 | MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds); |
| 1054 | |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 1055 | // Reset and drop everything the dispatcher is doing. |
| 1056 | void resetAndDropEverythingLocked(const char* reason); |
| 1057 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1058 | // Dump state. |
| 1059 | void dumpDispatchStateLocked(String8& dump); |
| 1060 | void logDispatchStateLocked(); |
| 1061 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1062 | // Add or remove a connection to the mActiveConnections vector. |
| 1063 | void activateConnectionLocked(Connection* connection); |
| 1064 | void deactivateConnectionLocked(Connection* connection); |
| 1065 | |
| 1066 | // Interesting events that we might like to log or tell the framework about. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1067 | void onDispatchCycleStartedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1068 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1069 | void onDispatchCycleFinishedLocked( |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1070 | nsecs_t currentTime, const sp<Connection>& connection, bool handled); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1071 | void onDispatchCycleBrokenLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1072 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1073 | void onANRLocked( |
| 1074 | nsecs_t currentTime, const InputApplication* application, const InputWindow* window, |
| 1075 | nsecs_t eventTime, nsecs_t waitStartTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1076 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1077 | // Outbound policy interactions. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1078 | void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1079 | void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1080 | void doNotifyANRLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1081 | void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1082 | void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1083 | bool afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
| 1084 | DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled); |
| 1085 | bool afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
| 1086 | DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1087 | void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1088 | void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1089 | |
| 1090 | // Statistics gathering. |
| 1091 | void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 1092 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1093 | }; |
| 1094 | |
| 1095 | /* Enqueues and dispatches input events, endlessly. */ |
| 1096 | class InputDispatcherThread : public Thread { |
| 1097 | public: |
| 1098 | explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher); |
| 1099 | ~InputDispatcherThread(); |
| 1100 | |
| 1101 | private: |
| 1102 | virtual bool threadLoop(); |
| 1103 | |
| 1104 | sp<InputDispatcherInterface> mDispatcher; |
| 1105 | }; |
| 1106 | |
| 1107 | } // namespace android |
| 1108 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1109 | #endif // _UI_INPUT_DISPATCHER_H |