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