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