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 | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 378 | inline bool isInjected() { 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 | |
| 408 | nsecs_t eventTime; |
| 409 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 410 | }; |
| 411 | |
| 412 | struct MotionEntry : EventEntry { |
| 413 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 414 | uint32_t source; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 415 | int32_t action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 416 | int32_t flags; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 417 | int32_t metaState; |
| 418 | int32_t edgeFlags; |
| 419 | float xPrecision; |
| 420 | float yPrecision; |
| 421 | nsecs_t downTime; |
| 422 | uint32_t pointerCount; |
| 423 | int32_t pointerIds[MAX_POINTERS]; |
| 424 | |
| 425 | // Linked list of motion samples associated with this motion event. |
| 426 | MotionSample firstSample; |
| 427 | MotionSample* lastSample; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 428 | |
| 429 | uint32_t countSamples() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 430 | }; |
| 431 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 432 | // Tracks the progress of dispatching a particular event to a particular connection. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 433 | struct DispatchEntry : Link<DispatchEntry> { |
| 434 | EventEntry* eventEntry; // the event to dispatch |
| 435 | int32_t targetFlags; |
| 436 | float xOffset; |
| 437 | float yOffset; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame^] | 438 | float scaleFactor; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 439 | |
| 440 | // True if dispatch has started. |
| 441 | bool inProgress; |
| 442 | |
| 443 | // For motion events: |
| 444 | // Pointer to the first motion sample to dispatch in this cycle. |
| 445 | // Usually NULL to indicate that the list of motion samples begins at |
| 446 | // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous |
| 447 | // cycle and this pointer indicates the location of the first remainining sample |
| 448 | // to dispatch during the current cycle. |
| 449 | MotionSample* headMotionSample; |
| 450 | // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was |
| 451 | // unable to send all motion samples during this cycle. On the next cycle, |
| 452 | // headMotionSample will be initialized to tailMotionSample and tailMotionSample |
| 453 | // will be set to NULL. |
| 454 | MotionSample* tailMotionSample; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 455 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 456 | inline bool hasForegroundTarget() const { |
| 457 | return targetFlags & InputTarget::FLAG_FOREGROUND; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 458 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 459 | |
| 460 | inline bool isSplit() const { |
| 461 | return targetFlags & InputTarget::FLAG_SPLIT; |
| 462 | } |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 463 | }; |
| 464 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 465 | // A command entry captures state and behavior for an action to be performed in the |
| 466 | // dispatch loop after the initial processing has taken place. It is essentially |
| 467 | // a kind of continuation used to postpone sensitive policy interactions to a point |
| 468 | // in the dispatch loop where it is safe to release the lock (generally after finishing |
| 469 | // the critical parts of the dispatch cycle). |
| 470 | // |
| 471 | // The special thing about commands is that they can voluntarily release and reacquire |
| 472 | // the dispatcher lock at will. Initially when the command starts running, the |
| 473 | // dispatcher lock is held. However, if the command needs to call into the policy to |
| 474 | // do some work, it can release the lock, do the work, then reacquire the lock again |
| 475 | // before returning. |
| 476 | // |
| 477 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch |
| 478 | // never calls into the policy while holding its lock. |
| 479 | // |
| 480 | // Commands are implicitly 'LockedInterruptible'. |
| 481 | struct CommandEntry; |
| 482 | typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry); |
| 483 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 484 | class Connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 485 | struct CommandEntry : Link<CommandEntry> { |
| 486 | CommandEntry(); |
| 487 | ~CommandEntry(); |
| 488 | |
| 489 | Command command; |
| 490 | |
| 491 | // parameters for the command (usage varies by command) |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 492 | sp<Connection> connection; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 493 | nsecs_t eventTime; |
| 494 | KeyEntry* keyEntry; |
| 495 | sp<InputChannel> inputChannel; |
| 496 | sp<InputApplicationHandle> inputApplicationHandle; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 497 | sp<InputWindowHandle> inputWindowHandle; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 498 | int32_t userActivityEventType; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 499 | bool handled; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | // Generic queue implementation. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 503 | template <typename T> |
| 504 | struct Queue { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 505 | T headSentinel; |
| 506 | T tailSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 507 | |
| 508 | inline Queue() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 509 | headSentinel.prev = NULL; |
| 510 | headSentinel.next = & tailSentinel; |
| 511 | tailSentinel.prev = & headSentinel; |
| 512 | tailSentinel.next = NULL; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 515 | inline bool isEmpty() const { |
| 516 | return headSentinel.next == & tailSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | inline void enqueueAtTail(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 520 | T* last = tailSentinel.prev; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 521 | last->next = entry; |
| 522 | entry->prev = last; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 523 | entry->next = & tailSentinel; |
| 524 | tailSentinel.prev = entry; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | inline void enqueueAtHead(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 528 | T* first = headSentinel.next; |
| 529 | headSentinel.next = entry; |
| 530 | entry->prev = & headSentinel; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 531 | entry->next = first; |
| 532 | first->prev = entry; |
| 533 | } |
| 534 | |
| 535 | inline void dequeue(T* entry) { |
| 536 | entry->prev->next = entry->next; |
| 537 | entry->next->prev = entry->prev; |
| 538 | } |
| 539 | |
| 540 | inline T* dequeueAtHead() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 541 | T* first = headSentinel.next; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 542 | dequeue(first); |
| 543 | return first; |
| 544 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 545 | |
| 546 | uint32_t count() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | /* Allocates queue entries and performs reference counting as needed. */ |
| 550 | class Allocator { |
| 551 | public: |
| 552 | Allocator(); |
| 553 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 554 | InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 555 | ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime); |
| 556 | KeyEntry* obtainKeyEntry(nsecs_t eventTime, |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 557 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 558 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
| 559 | int32_t repeatCount, nsecs_t downTime); |
| 560 | MotionEntry* obtainMotionEntry(nsecs_t eventTime, |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 561 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 562 | int32_t flags, int32_t metaState, int32_t edgeFlags, |
| 563 | float xPrecision, float yPrecision, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 564 | nsecs_t downTime, uint32_t pointerCount, |
| 565 | const int32_t* pointerIds, const PointerCoords* pointerCoords); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 566 | DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame^] | 567 | int32_t targetFlags, float xOffset, float yOffset, float scaleFactor); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 568 | CommandEntry* obtainCommandEntry(Command command); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 569 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 570 | void releaseInjectionState(InjectionState* injectionState); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 571 | void releaseEventEntry(EventEntry* entry); |
| 572 | void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry); |
| 573 | void releaseKeyEntry(KeyEntry* entry); |
| 574 | void releaseMotionEntry(MotionEntry* entry); |
| 575 | void releaseDispatchEntry(DispatchEntry* entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 576 | void releaseCommandEntry(CommandEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 577 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 578 | void recycleKeyEntry(KeyEntry* entry); |
| 579 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 580 | void appendMotionSample(MotionEntry* motionEntry, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 581 | nsecs_t eventTime, const PointerCoords* pointerCoords); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 582 | |
| 583 | private: |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 584 | Pool<InjectionState> mInjectionStatePool; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 585 | Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool; |
| 586 | Pool<KeyEntry> mKeyEntryPool; |
| 587 | Pool<MotionEntry> mMotionEntryPool; |
| 588 | Pool<MotionSample> mMotionSamplePool; |
| 589 | Pool<DispatchEntry> mDispatchEntryPool; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 590 | Pool<CommandEntry> mCommandEntryPool; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 591 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 592 | void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime, |
| 593 | uint32_t policyFlags); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 594 | void releaseEventEntryInjectionState(EventEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 595 | }; |
| 596 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 597 | /* Tracks dispatched key and motion event state so that cancelation events can be |
| 598 | * synthesized when events are dropped. */ |
| 599 | class InputState { |
| 600 | public: |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 601 | // Specifies the sources to cancel. |
| 602 | enum CancelationOptions { |
| 603 | CANCEL_ALL_EVENTS = 0, |
| 604 | CANCEL_POINTER_EVENTS = 1, |
| 605 | CANCEL_NON_POINTER_EVENTS = 2, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 606 | CANCEL_FALLBACK_EVENTS = 3, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 607 | }; |
| 608 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 609 | InputState(); |
| 610 | ~InputState(); |
| 611 | |
| 612 | // Returns true if there is no state to be canceled. |
| 613 | bool isNeutral() const; |
| 614 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 615 | // Records tracking information for an event that has just been published. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 616 | void trackEvent(const EventEntry* entry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 617 | |
| 618 | // Records tracking information for a key event that has just been published. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 619 | void trackKey(const KeyEntry* entry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 620 | |
| 621 | // Records tracking information for a motion event that has just been published. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 622 | void trackMotion(const MotionEntry* entry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 623 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 624 | // Synthesizes cancelation events for the current state and resets the tracked state. |
| 625 | void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator, |
| 626 | Vector<EventEntry*>& outEvents, CancelationOptions options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 627 | |
| 628 | // Clears the current state. |
| 629 | void clear(); |
| 630 | |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 631 | // Copies pointer-related parts of the input state to another instance. |
| 632 | void copyPointerStateTo(InputState& other) const; |
| 633 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 634 | private: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 635 | struct KeyMemento { |
| 636 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 637 | uint32_t source; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 638 | int32_t keyCode; |
| 639 | int32_t scanCode; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 640 | int32_t flags; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 641 | nsecs_t downTime; |
| 642 | }; |
| 643 | |
| 644 | struct MotionMemento { |
| 645 | int32_t deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 646 | uint32_t source; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 647 | float xPrecision; |
| 648 | float yPrecision; |
| 649 | nsecs_t downTime; |
| 650 | uint32_t pointerCount; |
| 651 | int32_t pointerIds[MAX_POINTERS]; |
| 652 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 653 | |
| 654 | void setPointers(const MotionEntry* entry); |
| 655 | }; |
| 656 | |
| 657 | Vector<KeyMemento> mKeyMementos; |
| 658 | Vector<MotionMemento> mMotionMementos; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 659 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 660 | static bool shouldCancelKey(const KeyMemento& memento, |
| 661 | CancelationOptions options); |
| 662 | static bool shouldCancelMotion(const MotionMemento& memento, |
| 663 | CancelationOptions options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 664 | }; |
| 665 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 666 | /* Manages the dispatch state associated with a single input channel. */ |
| 667 | class Connection : public RefBase { |
| 668 | protected: |
| 669 | virtual ~Connection(); |
| 670 | |
| 671 | public: |
| 672 | enum Status { |
| 673 | // Everything is peachy. |
| 674 | STATUS_NORMAL, |
| 675 | // An unrecoverable communication error has occurred. |
| 676 | STATUS_BROKEN, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 677 | // The input channel has been unregistered. |
| 678 | STATUS_ZOMBIE |
| 679 | }; |
| 680 | |
| 681 | Status status; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 682 | sp<InputChannel> inputChannel; // never null |
| 683 | sp<InputWindowHandle> inputWindowHandle; // may be null |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 684 | InputPublisher inputPublisher; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 685 | InputState inputState; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 686 | Queue<DispatchEntry> outboundQueue; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 687 | |
| 688 | nsecs_t lastEventTime; // the time when the event was originally captured |
| 689 | nsecs_t lastDispatchTime; // the time when the last event was dispatched |
Jeff Brown | bfaf3b9 | 2011-02-22 15:00:50 -0800 | [diff] [blame] | 690 | int32_t originalKeyCodeForFallback; // original keycode for fallback in progress, -1 if none |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 691 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 692 | explicit Connection(const sp<InputChannel>& inputChannel, |
| 693 | const sp<InputWindowHandle>& inputWindowHandle); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 694 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 695 | inline const char* getInputChannelName() const { return inputChannel->getName().string(); } |
| 696 | |
| 697 | const char* getStatusLabel() const; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 698 | |
| 699 | // Finds a DispatchEntry in the outbound queue associated with the specified event. |
| 700 | // Returns NULL if not found. |
| 701 | DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const; |
| 702 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 703 | // 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] | 704 | inline double getEventLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 705 | return (currentTime - lastEventTime) / 1000000.0; |
| 706 | } |
| 707 | |
| 708 | // Gets the time since the current event entered the outbound dispatch queue. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 709 | inline double getDispatchLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 710 | return (currentTime - lastDispatchTime) / 1000000.0; |
| 711 | } |
| 712 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 713 | status_t initialize(); |
| 714 | }; |
| 715 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 716 | enum DropReason { |
| 717 | DROP_REASON_NOT_DROPPED = 0, |
| 718 | DROP_REASON_POLICY = 1, |
| 719 | DROP_REASON_APP_SWITCH = 2, |
| 720 | DROP_REASON_DISABLED = 3, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 721 | DROP_REASON_BLOCKED = 4, |
| 722 | DROP_REASON_STALE = 5, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 723 | }; |
| 724 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 725 | sp<InputDispatcherPolicyInterface> mPolicy; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 726 | |
| 727 | Mutex mLock; |
| 728 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 729 | Allocator mAllocator; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 730 | sp<Looper> mLooper; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 731 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 732 | EventEntry* mPendingEvent; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 733 | Queue<EventEntry> mInboundQueue; |
| 734 | Queue<CommandEntry> mCommandQueue; |
| 735 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 736 | Vector<EventEntry*> mTempCancelationEvents; |
| 737 | |
| 738 | void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay, |
| 739 | nsecs_t* nextWakeupTime); |
| 740 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 741 | // Enqueues an inbound event. Returns true if mLooper->wake() should be called. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 742 | bool enqueueInboundEventLocked(EventEntry* entry); |
| 743 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 744 | // Cleans up input state when dropping an inbound event. |
| 745 | void dropInboundEventLocked(EventEntry* entry, DropReason dropReason); |
| 746 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 747 | // App switch latency optimization. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 748 | bool mAppSwitchSawKeyDown; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 749 | nsecs_t mAppSwitchDueTime; |
| 750 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 751 | static bool isAppSwitchKeyCode(int32_t keyCode); |
| 752 | bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 753 | bool isAppSwitchPendingLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 754 | void resetPendingAppSwitchLocked(bool handled); |
| 755 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 756 | // Stale event latency optimization. |
| 757 | static bool isStaleEventLocked(nsecs_t currentTime, EventEntry* entry); |
| 758 | |
| 759 | // Blocked event latency optimization. Drops old events when the user intends |
| 760 | // to transfer focus to a new application. |
| 761 | EventEntry* mNextUnblockedEvent; |
| 762 | |
| 763 | const InputWindow* findTouchedWindowAtLocked(int32_t x, int32_t y); |
| 764 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 765 | // All registered connections mapped by receive pipe file descriptor. |
| 766 | KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd; |
| 767 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 768 | ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 769 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 770 | // Active connections are connections that have a non-empty outbound queue. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 771 | // We don't use a ref-counted pointer here because we explicitly abort connections |
| 772 | // during unregistration which causes the connection's outbound queue to be cleared |
| 773 | // and the connection itself to be deactivated. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 774 | Vector<Connection*> mActiveConnections; |
| 775 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 776 | // Input channels that will receive a copy of all input events. |
| 777 | Vector<sp<InputChannel> > mMonitoringChannels; |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 778 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 779 | // Event injection and synchronization. |
| 780 | Condition mInjectionResultAvailableCondition; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 781 | bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 782 | void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult); |
| 783 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 784 | Condition mInjectionSyncFinishedCondition; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 785 | void incrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 786 | void decrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 787 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 788 | // Throttling state. |
| 789 | struct ThrottleState { |
| 790 | nsecs_t minTimeBetweenEvents; |
| 791 | |
| 792 | nsecs_t lastEventTime; |
| 793 | int32_t lastDeviceId; |
| 794 | uint32_t lastSource; |
| 795 | |
| 796 | uint32_t originalSampleCount; // only collected during debugging |
| 797 | } mThrottleState; |
| 798 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 799 | // Key repeat tracking. |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 800 | struct KeyRepeatState { |
| 801 | KeyEntry* lastKeyEntry; // or null if no repeat |
| 802 | nsecs_t nextRepeatTime; |
| 803 | } mKeyRepeatState; |
| 804 | |
| 805 | void resetKeyRepeatLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 806 | KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 807 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 808 | // Deferred command processing. |
| 809 | bool runCommandsLockedInterruptible(); |
| 810 | CommandEntry* postCommandLocked(Command command); |
| 811 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 812 | // Inbound event processing. |
| 813 | void drainInboundQueueLocked(); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 814 | void releasePendingEventLocked(); |
| 815 | void releaseInboundEventLocked(EventEntry* entry); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 816 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 817 | // Dispatch state. |
| 818 | bool mDispatchEnabled; |
| 819 | bool mDispatchFrozen; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 820 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 821 | Vector<InputWindow> mWindows; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 822 | |
| 823 | const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 824 | |
| 825 | // Focus tracking for keys, trackball, etc. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 826 | const InputWindow* mFocusedWindow; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 827 | |
| 828 | // Focus tracking for touch. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 829 | struct TouchedWindow { |
| 830 | const InputWindow* window; |
| 831 | int32_t targetFlags; |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 832 | BitSet32 pointerIds; // zero unless target flag FLAG_SPLIT is set |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 833 | sp<InputChannel> channel; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 834 | }; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 835 | struct TouchState { |
| 836 | bool down; |
| 837 | bool split; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 838 | 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] | 839 | 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] | 840 | Vector<TouchedWindow> windows; |
| 841 | |
| 842 | TouchState(); |
| 843 | ~TouchState(); |
| 844 | void reset(); |
| 845 | void copyFrom(const TouchState& other); |
| 846 | void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds); |
| 847 | void removeOutsideTouchWindows(); |
| 848 | const InputWindow* getFirstForegroundWindow(); |
| 849 | }; |
| 850 | |
| 851 | TouchState mTouchState; |
| 852 | TouchState mTempTouchState; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 853 | |
| 854 | // Focused application. |
| 855 | InputApplication* mFocusedApplication; |
| 856 | InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication |
| 857 | void releaseFocusedApplicationLocked(); |
| 858 | |
| 859 | // Dispatch inbound events. |
| 860 | bool dispatchConfigurationChangedLocked( |
| 861 | nsecs_t currentTime, ConfigurationChangedEntry* entry); |
| 862 | bool dispatchKeyLocked( |
| 863 | nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 864 | DropReason* dropReason, nsecs_t* nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 865 | bool dispatchMotionLocked( |
| 866 | nsecs_t currentTime, MotionEntry* entry, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 867 | DropReason* dropReason, nsecs_t* nextWakeupTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 868 | void dispatchEventToCurrentInputTargetsLocked( |
| 869 | nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 870 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 871 | void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry); |
| 872 | void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry); |
| 873 | |
| 874 | // The input targets that were most recently identified for dispatch. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 875 | bool mCurrentInputTargetsValid; // false while targets are being recomputed |
| 876 | Vector<InputTarget> mCurrentInputTargets; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 877 | |
| 878 | enum InputTargetWaitCause { |
| 879 | INPUT_TARGET_WAIT_CAUSE_NONE, |
| 880 | INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY, |
| 881 | INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY, |
| 882 | }; |
| 883 | |
| 884 | InputTargetWaitCause mInputTargetWaitCause; |
| 885 | nsecs_t mInputTargetWaitStartTime; |
| 886 | nsecs_t mInputTargetWaitTimeoutTime; |
| 887 | bool mInputTargetWaitTimeoutExpired; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 888 | sp<InputApplicationHandle> mInputTargetWaitApplication; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 889 | |
| 890 | // Finding targets for input events. |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 891 | void resetTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 892 | void commitTargetsLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 893 | int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry, |
| 894 | const InputApplication* application, const InputWindow* window, |
| 895 | nsecs_t* nextWakeupTime); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 896 | void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| 897 | const sp<InputChannel>& inputChannel); |
| 898 | nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 899 | void resetANRTimeoutsLocked(); |
| 900 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 901 | int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 902 | nsecs_t* nextWakeupTime); |
| 903 | int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry, |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 904 | nsecs_t* nextWakeupTime, bool* outConflictingPointerActions); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 905 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 906 | void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags, |
| 907 | BitSet32 pointerIds); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 908 | void addMonitoringTargetsLocked(); |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 909 | void pokeUserActivityLocked(const EventEntry* eventEntry); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 910 | bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState); |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 911 | bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 912 | bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 913 | String8 getApplicationWindowLabelLocked(const InputApplication* application, |
| 914 | const InputWindow* window); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 915 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 916 | // Manage the dispatch cycle for a single connection. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 917 | // These methods are deliberately not Interruptible because doing all of the work |
| 918 | // with the mutex held makes it easier to ensure that connection invariants are maintained. |
| 919 | // If needed, the methods post commands to run later once the critical bits are done. |
| 920 | void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 921 | EventEntry* eventEntry, const InputTarget* inputTarget, |
| 922 | bool resumeWithAppendedMotionSample); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 923 | void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 924 | void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
| 925 | bool handled); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 926 | void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 927 | void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 928 | void drainOutboundQueueLocked(Connection* connection); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 929 | static int handleReceiveCallback(int receiveFd, int events, void* data); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 930 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 931 | void synthesizeCancelationEventsForAllConnectionsLocked( |
| 932 | InputState::CancelationOptions options, const char* reason); |
| 933 | void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel, |
| 934 | InputState::CancelationOptions options, const char* reason); |
| 935 | void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection, |
| 936 | InputState::CancelationOptions options, const char* reason); |
| 937 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 938 | // Splitting motion events across windows. |
| 939 | MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds); |
| 940 | |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 941 | // Reset and drop everything the dispatcher is doing. |
| 942 | void resetAndDropEverythingLocked(const char* reason); |
| 943 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 944 | // Dump state. |
| 945 | void dumpDispatchStateLocked(String8& dump); |
| 946 | void logDispatchStateLocked(); |
| 947 | |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 948 | // Add or remove a connection to the mActiveConnections vector. |
| 949 | void activateConnectionLocked(Connection* connection); |
| 950 | void deactivateConnectionLocked(Connection* connection); |
| 951 | |
| 952 | // 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] | 953 | void onDispatchCycleStartedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 954 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 955 | void onDispatchCycleFinishedLocked( |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 956 | nsecs_t currentTime, const sp<Connection>& connection, bool handled); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 957 | void onDispatchCycleBrokenLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 958 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 959 | void onANRLocked( |
| 960 | nsecs_t currentTime, const InputApplication* application, const InputWindow* window, |
| 961 | nsecs_t eventTime, nsecs_t waitStartTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 962 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 963 | // Outbound policy interactions. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 964 | void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 965 | void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 966 | void doNotifyANRLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 967 | void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 968 | void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 969 | void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 970 | void initializeKeyEvent(KeyEvent* event, const KeyEntry* entry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 971 | |
| 972 | // Statistics gathering. |
| 973 | void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 974 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication); |
Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 975 | }; |
| 976 | |
| 977 | /* Enqueues and dispatches input events, endlessly. */ |
| 978 | class InputDispatcherThread : public Thread { |
| 979 | public: |
| 980 | explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher); |
| 981 | ~InputDispatcherThread(); |
| 982 | |
| 983 | private: |
| 984 | virtual bool threadLoop(); |
| 985 | |
| 986 | sp<InputDispatcherInterface> mDispatcher; |
| 987 | }; |
| 988 | |
| 989 | } // namespace android |
| 990 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 991 | #endif // _UI_INPUT_DISPATCHER_H |