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