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