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