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 | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 392 | /* 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] | 393 | * 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] | 394 | * |
| 395 | * These methods may be called on any thread (usually by the input manager). |
| 396 | */ |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 397 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor) = 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 398 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel) = 0; |
| 399 | }; |
| 400 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 401 | /* Dispatches events to input targets. Some functions of the input dispatcher, such as |
| 402 | * identifying input targets, are controlled by a separate policy object. |
| 403 | * |
| 404 | * IMPORTANT INVARIANT: |
| 405 | * Because the policy can potentially block or cause re-entrance into the input dispatcher, |
| 406 | * the input dispatcher never calls into the policy while holding its internal locks. |
| 407 | * The implementation is also carefully designed to recover from scenarios such as an |
| 408 | * input channel becoming unregistered while identifying input targets or processing timeouts. |
| 409 | * |
| 410 | * Methods marked 'Locked' must be called with the lock acquired. |
| 411 | * |
| 412 | * Methods marked 'LockedInterruptible' must be called with the lock acquired but |
| 413 | * may during the course of their execution release the lock, call into the policy, and |
| 414 | * then reacquire the lock. The caller is responsible for recovering gracefully. |
| 415 | * |
| 416 | * A 'LockedInterruptible' method may called a 'Locked' method, but NOT vice-versa. |
| 417 | */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 418 | class InputDispatcher : public InputDispatcherInterface { |
| 419 | protected: |
| 420 | virtual ~InputDispatcher(); |
| 421 | |
| 422 | public: |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 423 | explicit InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 424 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 425 | virtual void dump(String8& dump); |
| 426 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 427 | virtual void dispatchOnce(); |
| 428 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 429 | virtual void notifyConfigurationChanged(nsecs_t eventTime); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 430 | virtual void notifyKey(nsecs_t eventTime, int32_t deviceId, int32_t source, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 431 | uint32_t policyFlags, int32_t action, int32_t flags, int32_t keyCode, |
| 432 | int32_t scanCode, int32_t metaState, nsecs_t downTime); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 433 | virtual void notifyMotion(nsecs_t eventTime, int32_t deviceId, int32_t source, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 434 | uint32_t policyFlags, int32_t action, int32_t flags, |
| 435 | int32_t metaState, int32_t edgeFlags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 436 | uint32_t pointerCount, const int32_t* pointerIds, const PointerCoords* pointerCoords, |
| 437 | float xPrecision, float yPrecision, nsecs_t downTime); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 438 | virtual void notifySwitch(nsecs_t when, |
| 439 | int32_t switchCode, int32_t switchValue, uint32_t policyFlags) ; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 440 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 441 | virtual int32_t injectInputEvent(const InputEvent* event, |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 442 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 443 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 444 | virtual void setInputWindows(const Vector<InputWindow>& inputWindows); |
| 445 | virtual void setFocusedApplication(const InputApplication* inputApplication); |
| 446 | virtual void setInputDispatchMode(bool enabled, bool frozen); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 447 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 448 | virtual status_t registerInputChannel(const sp<InputChannel>& inputChannel, bool monitor); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 449 | virtual status_t unregisterInputChannel(const sp<InputChannel>& inputChannel); |
| 450 | |
| 451 | private: |
| 452 | template <typename T> |
| 453 | struct Link { |
| 454 | T* next; |
| 455 | T* prev; |
| 456 | }; |
| 457 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 458 | struct InjectionState { |
| 459 | mutable int32_t refCount; |
| 460 | |
| 461 | int32_t injectorPid; |
| 462 | int32_t injectorUid; |
| 463 | int32_t injectionResult; // initially INPUT_EVENT_INJECTION_PENDING |
| 464 | bool injectionIsAsync; // set to true if injection is not waiting for the result |
| 465 | int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress |
| 466 | }; |
| 467 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 468 | struct EventEntry : Link<EventEntry> { |
| 469 | enum { |
| 470 | TYPE_SENTINEL, |
| 471 | TYPE_CONFIGURATION_CHANGED, |
| 472 | TYPE_KEY, |
| 473 | TYPE_MOTION |
| 474 | }; |
| 475 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 476 | mutable int32_t refCount; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 477 | int32_t type; |
| 478 | nsecs_t eventTime; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 479 | uint32_t policyFlags; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 480 | InjectionState* injectionState; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 481 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 482 | bool dispatchInProgress; // initially false, set to true while dispatching |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 483 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 484 | inline bool isInjected() { return injectionState != NULL; } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | struct ConfigurationChangedEntry : EventEntry { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 488 | }; |
| 489 | |
| 490 | struct KeyEntry : EventEntry { |
| 491 | int32_t deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 492 | int32_t source; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 493 | int32_t action; |
| 494 | int32_t flags; |
| 495 | int32_t keyCode; |
| 496 | int32_t scanCode; |
| 497 | int32_t metaState; |
| 498 | int32_t repeatCount; |
| 499 | nsecs_t downTime; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 500 | |
| 501 | bool syntheticRepeat; // set to true for synthetic key repeats |
| 502 | |
| 503 | enum InterceptKeyResult { |
| 504 | INTERCEPT_KEY_RESULT_UNKNOWN, |
| 505 | INTERCEPT_KEY_RESULT_SKIP, |
| 506 | INTERCEPT_KEY_RESULT_CONTINUE, |
| 507 | }; |
| 508 | InterceptKeyResult interceptKeyResult; // set based on the interception result |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 509 | }; |
| 510 | |
| 511 | struct MotionSample { |
| 512 | MotionSample* next; |
| 513 | |
| 514 | nsecs_t eventTime; |
| 515 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 516 | }; |
| 517 | |
| 518 | struct MotionEntry : EventEntry { |
| 519 | int32_t deviceId; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 520 | int32_t source; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 521 | int32_t action; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 522 | int32_t flags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 523 | int32_t metaState; |
| 524 | int32_t edgeFlags; |
| 525 | float xPrecision; |
| 526 | float yPrecision; |
| 527 | nsecs_t downTime; |
| 528 | uint32_t pointerCount; |
| 529 | int32_t pointerIds[MAX_POINTERS]; |
| 530 | |
| 531 | // Linked list of motion samples associated with this motion event. |
| 532 | MotionSample firstSample; |
| 533 | MotionSample* lastSample; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 534 | |
| 535 | uint32_t countSamples() const; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 538 | // Tracks the progress of dispatching a particular event to a particular connection. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 539 | struct DispatchEntry : Link<DispatchEntry> { |
| 540 | EventEntry* eventEntry; // the event to dispatch |
| 541 | int32_t targetFlags; |
| 542 | float xOffset; |
| 543 | float yOffset; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 544 | |
| 545 | // True if dispatch has started. |
| 546 | bool inProgress; |
| 547 | |
| 548 | // For motion events: |
| 549 | // Pointer to the first motion sample to dispatch in this cycle. |
| 550 | // Usually NULL to indicate that the list of motion samples begins at |
| 551 | // MotionEntry::firstSample. Otherwise, some samples were dispatched in a previous |
| 552 | // cycle and this pointer indicates the location of the first remainining sample |
| 553 | // to dispatch during the current cycle. |
| 554 | MotionSample* headMotionSample; |
| 555 | // Pointer to a motion sample to dispatch in the next cycle if the dispatcher was |
| 556 | // unable to send all motion samples during this cycle. On the next cycle, |
| 557 | // headMotionSample will be initialized to tailMotionSample and tailMotionSample |
| 558 | // will be set to NULL. |
| 559 | MotionSample* tailMotionSample; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 560 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 561 | inline bool hasForegroundTarget() const { |
| 562 | return targetFlags & InputTarget::FLAG_FOREGROUND; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 563 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 564 | |
| 565 | inline bool isSplit() const { |
| 566 | return targetFlags & InputTarget::FLAG_SPLIT; |
| 567 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 568 | }; |
| 569 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 570 | // A command entry captures state and behavior for an action to be performed in the |
| 571 | // dispatch loop after the initial processing has taken place. It is essentially |
| 572 | // a kind of continuation used to postpone sensitive policy interactions to a point |
| 573 | // in the dispatch loop where it is safe to release the lock (generally after finishing |
| 574 | // the critical parts of the dispatch cycle). |
| 575 | // |
| 576 | // The special thing about commands is that they can voluntarily release and reacquire |
| 577 | // the dispatcher lock at will. Initially when the command starts running, the |
| 578 | // dispatcher lock is held. However, if the command needs to call into the policy to |
| 579 | // do some work, it can release the lock, do the work, then reacquire the lock again |
| 580 | // before returning. |
| 581 | // |
| 582 | // This mechanism is a bit clunky but it helps to preserve the invariant that the dispatch |
| 583 | // never calls into the policy while holding its lock. |
| 584 | // |
| 585 | // Commands are implicitly 'LockedInterruptible'. |
| 586 | struct CommandEntry; |
| 587 | typedef void (InputDispatcher::*Command)(CommandEntry* commandEntry); |
| 588 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 589 | class Connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 590 | struct CommandEntry : Link<CommandEntry> { |
| 591 | CommandEntry(); |
| 592 | ~CommandEntry(); |
| 593 | |
| 594 | Command command; |
| 595 | |
| 596 | // parameters for the command (usage varies by command) |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 597 | sp<Connection> connection; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 598 | nsecs_t eventTime; |
| 599 | KeyEntry* keyEntry; |
| 600 | sp<InputChannel> inputChannel; |
| 601 | sp<InputApplicationHandle> inputApplicationHandle; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 602 | int32_t userActivityEventType; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 603 | }; |
| 604 | |
| 605 | // Generic queue implementation. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 606 | template <typename T> |
| 607 | struct Queue { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 608 | T headSentinel; |
| 609 | T tailSentinel; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 610 | |
| 611 | inline Queue() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 612 | headSentinel.prev = NULL; |
| 613 | headSentinel.next = & tailSentinel; |
| 614 | tailSentinel.prev = & headSentinel; |
| 615 | tailSentinel.next = NULL; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 618 | inline bool isEmpty() const { |
| 619 | return headSentinel.next == & tailSentinel; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | inline void enqueueAtTail(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 623 | T* last = tailSentinel.prev; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 624 | last->next = entry; |
| 625 | entry->prev = last; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 626 | entry->next = & tailSentinel; |
| 627 | tailSentinel.prev = entry; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | inline void enqueueAtHead(T* entry) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 631 | T* first = headSentinel.next; |
| 632 | headSentinel.next = entry; |
| 633 | entry->prev = & headSentinel; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 634 | entry->next = first; |
| 635 | first->prev = entry; |
| 636 | } |
| 637 | |
| 638 | inline void dequeue(T* entry) { |
| 639 | entry->prev->next = entry->next; |
| 640 | entry->next->prev = entry->prev; |
| 641 | } |
| 642 | |
| 643 | inline T* dequeueAtHead() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 644 | T* first = headSentinel.next; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 645 | dequeue(first); |
| 646 | return first; |
| 647 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 648 | |
| 649 | uint32_t count() const; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 650 | }; |
| 651 | |
| 652 | /* Allocates queue entries and performs reference counting as needed. */ |
| 653 | class Allocator { |
| 654 | public: |
| 655 | Allocator(); |
| 656 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 657 | InjectionState* obtainInjectionState(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 658 | ConfigurationChangedEntry* obtainConfigurationChangedEntry(nsecs_t eventTime); |
| 659 | KeyEntry* obtainKeyEntry(nsecs_t eventTime, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 660 | int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 661 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
| 662 | int32_t repeatCount, nsecs_t downTime); |
| 663 | MotionEntry* obtainMotionEntry(nsecs_t eventTime, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 664 | int32_t deviceId, int32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 665 | int32_t flags, int32_t metaState, int32_t edgeFlags, |
| 666 | float xPrecision, float yPrecision, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 667 | nsecs_t downTime, uint32_t pointerCount, |
| 668 | const int32_t* pointerIds, const PointerCoords* pointerCoords); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 669 | DispatchEntry* obtainDispatchEntry(EventEntry* eventEntry, |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 670 | int32_t targetFlags, float xOffset, float yOffset); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 671 | CommandEntry* obtainCommandEntry(Command command); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 672 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 673 | void releaseInjectionState(InjectionState* injectionState); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 674 | void releaseEventEntry(EventEntry* entry); |
| 675 | void releaseConfigurationChangedEntry(ConfigurationChangedEntry* entry); |
| 676 | void releaseKeyEntry(KeyEntry* entry); |
| 677 | void releaseMotionEntry(MotionEntry* entry); |
| 678 | void releaseDispatchEntry(DispatchEntry* entry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 679 | void releaseCommandEntry(CommandEntry* entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 680 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 681 | void recycleKeyEntry(KeyEntry* entry); |
| 682 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 683 | void appendMotionSample(MotionEntry* motionEntry, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 684 | nsecs_t eventTime, const PointerCoords* pointerCoords); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 685 | |
| 686 | private: |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 687 | Pool<InjectionState> mInjectionStatePool; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 688 | Pool<ConfigurationChangedEntry> mConfigurationChangeEntryPool; |
| 689 | Pool<KeyEntry> mKeyEntryPool; |
| 690 | Pool<MotionEntry> mMotionEntryPool; |
| 691 | Pool<MotionSample> mMotionSamplePool; |
| 692 | Pool<DispatchEntry> mDispatchEntryPool; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 693 | Pool<CommandEntry> mCommandEntryPool; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 694 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 695 | void initializeEventEntry(EventEntry* entry, int32_t type, nsecs_t eventTime, |
| 696 | uint32_t policyFlags); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 697 | void releaseEventEntryInjectionState(EventEntry* entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 700 | /* Tracks dispatched key and motion event state so that cancelation events can be |
| 701 | * synthesized when events are dropped. */ |
| 702 | class InputState { |
| 703 | public: |
| 704 | // Specifies whether a given event will violate input state consistency. |
| 705 | enum Consistency { |
| 706 | // The event is consistent with the current input state. |
| 707 | CONSISTENT, |
| 708 | // The event is inconsistent with the current input state but applications |
| 709 | // will tolerate it. eg. Down followed by another down. |
| 710 | TOLERABLE, |
| 711 | // The event is inconsistent with the current input state and will probably |
| 712 | // cause applications to crash. eg. Up without prior down, move with |
| 713 | // unexpected number of pointers. |
| 714 | BROKEN |
| 715 | }; |
| 716 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 717 | // Specifies the sources to cancel. |
| 718 | enum CancelationOptions { |
| 719 | CANCEL_ALL_EVENTS = 0, |
| 720 | CANCEL_POINTER_EVENTS = 1, |
| 721 | CANCEL_NON_POINTER_EVENTS = 2, |
| 722 | }; |
| 723 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 724 | InputState(); |
| 725 | ~InputState(); |
| 726 | |
| 727 | // Returns true if there is no state to be canceled. |
| 728 | bool isNeutral() const; |
| 729 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 730 | // Records tracking information for an event that has just been published. |
| 731 | // Returns whether the event is consistent with the current input state. |
| 732 | Consistency trackEvent(const EventEntry* entry); |
| 733 | |
| 734 | // Records tracking information for a key event that has just been published. |
| 735 | // Returns whether the event is consistent with the current input state. |
| 736 | Consistency trackKey(const KeyEntry* entry); |
| 737 | |
| 738 | // Records tracking information for a motion event that has just been published. |
| 739 | // Returns whether the event is consistent with the current input state. |
| 740 | Consistency trackMotion(const MotionEntry* entry); |
| 741 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 742 | // Synthesizes cancelation events for the current state and resets the tracked state. |
| 743 | void synthesizeCancelationEvents(nsecs_t currentTime, Allocator* allocator, |
| 744 | Vector<EventEntry*>& outEvents, CancelationOptions options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 745 | |
| 746 | // Clears the current state. |
| 747 | void clear(); |
| 748 | |
| 749 | private: |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 750 | struct KeyMemento { |
| 751 | int32_t deviceId; |
| 752 | int32_t source; |
| 753 | int32_t keyCode; |
| 754 | int32_t scanCode; |
| 755 | nsecs_t downTime; |
| 756 | }; |
| 757 | |
| 758 | struct MotionMemento { |
| 759 | int32_t deviceId; |
| 760 | int32_t source; |
| 761 | float xPrecision; |
| 762 | float yPrecision; |
| 763 | nsecs_t downTime; |
| 764 | uint32_t pointerCount; |
| 765 | int32_t pointerIds[MAX_POINTERS]; |
| 766 | PointerCoords pointerCoords[MAX_POINTERS]; |
| 767 | |
| 768 | void setPointers(const MotionEntry* entry); |
| 769 | }; |
| 770 | |
| 771 | Vector<KeyMemento> mKeyMementos; |
| 772 | Vector<MotionMemento> mMotionMementos; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 773 | |
| 774 | static bool shouldCancelEvent(int32_t eventSource, CancelationOptions options); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 775 | }; |
| 776 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 777 | /* Manages the dispatch state associated with a single input channel. */ |
| 778 | class Connection : public RefBase { |
| 779 | protected: |
| 780 | virtual ~Connection(); |
| 781 | |
| 782 | public: |
| 783 | enum Status { |
| 784 | // Everything is peachy. |
| 785 | STATUS_NORMAL, |
| 786 | // An unrecoverable communication error has occurred. |
| 787 | STATUS_BROKEN, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 788 | // The input channel has been unregistered. |
| 789 | STATUS_ZOMBIE |
| 790 | }; |
| 791 | |
| 792 | Status status; |
| 793 | sp<InputChannel> inputChannel; |
| 794 | InputPublisher inputPublisher; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 795 | InputState inputState; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 796 | Queue<DispatchEntry> outboundQueue; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 797 | |
| 798 | nsecs_t lastEventTime; // the time when the event was originally captured |
| 799 | nsecs_t lastDispatchTime; // the time when the last event was dispatched |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 800 | |
| 801 | explicit Connection(const sp<InputChannel>& inputChannel); |
| 802 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 803 | inline const char* getInputChannelName() const { return inputChannel->getName().string(); } |
| 804 | |
| 805 | const char* getStatusLabel() const; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 806 | |
| 807 | // Finds a DispatchEntry in the outbound queue associated with the specified event. |
| 808 | // Returns NULL if not found. |
| 809 | DispatchEntry* findQueuedDispatchEntryForEvent(const EventEntry* eventEntry) const; |
| 810 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 811 | // 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] | 812 | inline double getEventLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 813 | return (currentTime - lastEventTime) / 1000000.0; |
| 814 | } |
| 815 | |
| 816 | // Gets the time since the current event entered the outbound dispatch queue. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 817 | inline double getDispatchLatencyMillis(nsecs_t currentTime) const { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 818 | return (currentTime - lastDispatchTime) / 1000000.0; |
| 819 | } |
| 820 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 821 | status_t initialize(); |
| 822 | }; |
| 823 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 824 | enum DropReason { |
| 825 | DROP_REASON_NOT_DROPPED = 0, |
| 826 | DROP_REASON_POLICY = 1, |
| 827 | DROP_REASON_APP_SWITCH = 2, |
| 828 | DROP_REASON_DISABLED = 3, |
| 829 | }; |
| 830 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 831 | sp<InputDispatcherPolicyInterface> mPolicy; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 832 | |
| 833 | Mutex mLock; |
| 834 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 835 | Allocator mAllocator; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 836 | sp<Looper> mLooper; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 837 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 838 | EventEntry* mPendingEvent; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 839 | Queue<EventEntry> mInboundQueue; |
| 840 | Queue<CommandEntry> mCommandQueue; |
| 841 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 842 | Vector<EventEntry*> mTempCancelationEvents; |
| 843 | |
| 844 | void dispatchOnceInnerLocked(nsecs_t keyRepeatTimeout, nsecs_t keyRepeatDelay, |
| 845 | nsecs_t* nextWakeupTime); |
| 846 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 847 | // Enqueues an inbound event. Returns true if mLooper->wake() should be called. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 848 | bool enqueueInboundEventLocked(EventEntry* entry); |
| 849 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 850 | // Cleans up input state when dropping an inbound event. |
| 851 | void dropInboundEventLocked(EventEntry* entry, DropReason dropReason); |
| 852 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 853 | // App switch latency optimization. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 854 | bool mAppSwitchSawKeyDown; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 855 | nsecs_t mAppSwitchDueTime; |
| 856 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 857 | static bool isAppSwitchKeyCode(int32_t keyCode); |
| 858 | bool isAppSwitchKeyEventLocked(KeyEntry* keyEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 859 | bool isAppSwitchPendingLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 860 | void resetPendingAppSwitchLocked(bool handled); |
| 861 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 862 | // All registered connections mapped by receive pipe file descriptor. |
| 863 | KeyedVector<int, sp<Connection> > mConnectionsByReceiveFd; |
| 864 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 865 | ssize_t getConnectionIndexLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 866 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 867 | // Active connections are connections that have a non-empty outbound queue. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 868 | // We don't use a ref-counted pointer here because we explicitly abort connections |
| 869 | // during unregistration which causes the connection's outbound queue to be cleared |
| 870 | // and the connection itself to be deactivated. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 871 | Vector<Connection*> mActiveConnections; |
| 872 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 873 | // Input channels that will receive a copy of all input events. |
| 874 | Vector<sp<InputChannel> > mMonitoringChannels; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 875 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 876 | // Preallocated key event object used for policy inquiries. |
| 877 | KeyEvent mReusableKeyEvent; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 878 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 879 | // Event injection and synchronization. |
| 880 | Condition mInjectionResultAvailableCondition; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 881 | bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 882 | void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult); |
| 883 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 884 | Condition mInjectionSyncFinishedCondition; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 885 | void incrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 886 | void decrementPendingForegroundDispatchesLocked(EventEntry* entry); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 887 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 888 | // Throttling state. |
| 889 | struct ThrottleState { |
| 890 | nsecs_t minTimeBetweenEvents; |
| 891 | |
| 892 | nsecs_t lastEventTime; |
| 893 | int32_t lastDeviceId; |
| 894 | uint32_t lastSource; |
| 895 | |
| 896 | uint32_t originalSampleCount; // only collected during debugging |
| 897 | } mThrottleState; |
| 898 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 899 | // Key repeat tracking. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 900 | struct KeyRepeatState { |
| 901 | KeyEntry* lastKeyEntry; // or null if no repeat |
| 902 | nsecs_t nextRepeatTime; |
| 903 | } mKeyRepeatState; |
| 904 | |
| 905 | void resetKeyRepeatLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 906 | KeyEntry* synthesizeKeyRepeatLocked(nsecs_t currentTime, nsecs_t keyRepeatTimeout); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 907 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 908 | // Deferred command processing. |
| 909 | bool runCommandsLockedInterruptible(); |
| 910 | CommandEntry* postCommandLocked(Command command); |
| 911 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 912 | // Inbound event processing. |
| 913 | void drainInboundQueueLocked(); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 914 | void releasePendingEventLocked(); |
| 915 | void releaseInboundEventLocked(EventEntry* entry); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 916 | bool isEventFromTrustedSourceLocked(EventEntry* entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 917 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 918 | // Dispatch state. |
| 919 | bool mDispatchEnabled; |
| 920 | bool mDispatchFrozen; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 921 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 922 | Vector<InputWindow> mWindows; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 923 | |
| 924 | const InputWindow* getWindowLocked(const sp<InputChannel>& inputChannel); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 925 | |
| 926 | // Focus tracking for keys, trackball, etc. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 927 | const InputWindow* mFocusedWindow; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 928 | |
| 929 | // Focus tracking for touch. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 930 | struct TouchedWindow { |
| 931 | const InputWindow* window; |
| 932 | int32_t targetFlags; |
| 933 | BitSet32 pointerIds; |
| 934 | sp<InputChannel> channel; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 935 | }; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 936 | struct TouchState { |
| 937 | bool down; |
| 938 | bool split; |
| 939 | Vector<TouchedWindow> windows; |
| 940 | |
| 941 | TouchState(); |
| 942 | ~TouchState(); |
| 943 | void reset(); |
| 944 | void copyFrom(const TouchState& other); |
| 945 | void addOrUpdateWindow(const InputWindow* window, int32_t targetFlags, BitSet32 pointerIds); |
| 946 | void removeOutsideTouchWindows(); |
| 947 | const InputWindow* getFirstForegroundWindow(); |
| 948 | }; |
| 949 | |
| 950 | TouchState mTouchState; |
| 951 | TouchState mTempTouchState; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 952 | |
| 953 | // Focused application. |
| 954 | InputApplication* mFocusedApplication; |
| 955 | InputApplication mFocusedApplicationStorage; // preallocated storage for mFocusedApplication |
| 956 | void releaseFocusedApplicationLocked(); |
| 957 | |
| 958 | // Dispatch inbound events. |
| 959 | bool dispatchConfigurationChangedLocked( |
| 960 | nsecs_t currentTime, ConfigurationChangedEntry* entry); |
| 961 | bool dispatchKeyLocked( |
| 962 | nsecs_t currentTime, KeyEntry* entry, nsecs_t keyRepeatTimeout, |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 963 | bool dropEvent, nsecs_t* nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 964 | bool dispatchMotionLocked( |
| 965 | nsecs_t currentTime, MotionEntry* entry, |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 966 | bool dropEvent, nsecs_t* nextWakeupTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 967 | void dispatchEventToCurrentInputTargetsLocked( |
| 968 | nsecs_t currentTime, EventEntry* entry, bool resumeWithAppendedMotionSample); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 969 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 970 | void logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry); |
| 971 | void logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry); |
| 972 | |
| 973 | // The input targets that were most recently identified for dispatch. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 974 | bool mCurrentInputTargetsValid; // false while targets are being recomputed |
| 975 | Vector<InputTarget> mCurrentInputTargets; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 976 | |
| 977 | enum InputTargetWaitCause { |
| 978 | INPUT_TARGET_WAIT_CAUSE_NONE, |
| 979 | INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY, |
| 980 | INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY, |
| 981 | }; |
| 982 | |
| 983 | InputTargetWaitCause mInputTargetWaitCause; |
| 984 | nsecs_t mInputTargetWaitStartTime; |
| 985 | nsecs_t mInputTargetWaitTimeoutTime; |
| 986 | bool mInputTargetWaitTimeoutExpired; |
| 987 | |
| 988 | // Finding targets for input events. |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 989 | void resetTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 990 | void commitTargetsLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 991 | int32_t handleTargetsNotReadyLocked(nsecs_t currentTime, const EventEntry* entry, |
| 992 | const InputApplication* application, const InputWindow* window, |
| 993 | nsecs_t* nextWakeupTime); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 994 | void resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| 995 | const sp<InputChannel>& inputChannel); |
| 996 | nsecs_t getTimeSpentWaitingForApplicationLocked(nsecs_t currentTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 997 | void resetANRTimeoutsLocked(); |
| 998 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 999 | int32_t findFocusedWindowTargetsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 1000 | nsecs_t* nextWakeupTime); |
| 1001 | int32_t findTouchedWindowTargetsLocked(nsecs_t currentTime, const MotionEntry* entry, |
| 1002 | nsecs_t* nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1003 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1004 | void addWindowTargetLocked(const InputWindow* window, int32_t targetFlags, |
| 1005 | BitSet32 pointerIds); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1006 | void addMonitoringTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1007 | bool shouldPokeUserActivityForCurrentInputTargetsLocked(); |
| 1008 | void pokeUserActivityLocked(nsecs_t eventTime, int32_t eventType); |
| 1009 | bool checkInjectionPermission(const InputWindow* window, const InjectionState* injectionState); |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1010 | bool isWindowObscuredAtPointLocked(const InputWindow* window, int32_t x, int32_t y) const; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1011 | bool isWindowFinishedWithPreviousInputLocked(const InputWindow* window); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1012 | String8 getApplicationWindowLabelLocked(const InputApplication* application, |
| 1013 | const InputWindow* window); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1014 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1015 | // Manage the dispatch cycle for a single connection. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1016 | // These methods are deliberately not Interruptible because doing all of the work |
| 1017 | // with the mutex held makes it easier to ensure that connection invariants are maintained. |
| 1018 | // If needed, the methods post commands to run later once the critical bits are done. |
| 1019 | void prepareDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1020 | EventEntry* eventEntry, const InputTarget* inputTarget, |
| 1021 | bool resumeWithAppendedMotionSample); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1022 | void startDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1023 | void finishDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1024 | void startNextDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 1025 | void abortBrokenDispatchCycleLocked(nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1026 | void drainOutboundQueueLocked(Connection* connection); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 1027 | static int handleReceiveCallback(int receiveFd, int events, void* data); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1028 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame^] | 1029 | void synthesizeCancelationEventsForAllConnectionsLocked( |
| 1030 | InputState::CancelationOptions options, const char* reason); |
| 1031 | void synthesizeCancelationEventsForInputChannelLocked(const sp<InputChannel>& channel, |
| 1032 | InputState::CancelationOptions options, const char* reason); |
| 1033 | void synthesizeCancelationEventsForConnectionLocked(const sp<Connection>& connection, |
| 1034 | InputState::CancelationOptions options, const char* reason); |
| 1035 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1036 | // Splitting motion events across windows. |
| 1037 | MotionEntry* splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds); |
| 1038 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1039 | // Dump state. |
| 1040 | void dumpDispatchStateLocked(String8& dump); |
| 1041 | void logDispatchStateLocked(); |
| 1042 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1043 | // Add or remove a connection to the mActiveConnections vector. |
| 1044 | void activateConnectionLocked(Connection* connection); |
| 1045 | void deactivateConnectionLocked(Connection* connection); |
| 1046 | |
| 1047 | // 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] | 1048 | void onDispatchCycleStartedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1049 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1050 | void onDispatchCycleFinishedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1051 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1052 | void onDispatchCycleBrokenLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1053 | nsecs_t currentTime, const sp<Connection>& connection); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1054 | void onANRLocked( |
| 1055 | nsecs_t currentTime, const InputApplication* application, const InputWindow* window, |
| 1056 | nsecs_t eventTime, nsecs_t waitStartTime); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1057 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1058 | // Outbound policy interactions. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1059 | void doNotifyConfigurationChangedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1060 | void doNotifyInputChannelBrokenLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1061 | void doNotifyANRLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1062 | void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry); |
| 1063 | void doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1064 | |
| 1065 | // Statistics gathering. |
| 1066 | void updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 1067 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | /* Enqueues and dispatches input events, endlessly. */ |
| 1071 | class InputDispatcherThread : public Thread { |
| 1072 | public: |
| 1073 | explicit InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher); |
| 1074 | ~InputDispatcherThread(); |
| 1075 | |
| 1076 | private: |
| 1077 | virtual bool threadLoop(); |
| 1078 | |
| 1079 | sp<InputDispatcherInterface> mDispatcher; |
| 1080 | }; |
| 1081 | |
| 1082 | } // namespace android |
| 1083 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1084 | #endif // _UI_INPUT_DISPATCHER_H |