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