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