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