Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [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 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "InputDispatcher" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | // Log detailed debug messages about each inbound event notification to the dispatcher. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 22 | #define DEBUG_INBOUND_EVENT_DETAILS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 23 | |
| 24 | // Log detailed debug messages about each outbound event processed by the dispatcher. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 25 | #define DEBUG_OUTBOUND_EVENT_DETAILS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 26 | |
| 27 | // Log debug messages about batching. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 28 | #define DEBUG_BATCHING 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | |
| 30 | // Log debug messages about the dispatch cycle. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 31 | #define DEBUG_DISPATCH_CYCLE 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 33 | // Log debug messages about registrations. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 34 | #define DEBUG_REGISTRATION 0 |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 35 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 36 | // Log debug messages about performance statistics. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 37 | #define DEBUG_PERFORMANCE_STATISTICS 0 |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 38 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 39 | // Log debug messages about input event injection. |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 40 | #define DEBUG_INJECTION 0 |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 41 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 42 | // Log debug messages about input event throttling. |
| 43 | #define DEBUG_THROTTLING 0 |
| 44 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 45 | // Log debug messages about input focus tracking. |
| 46 | #define DEBUG_FOCUS 0 |
| 47 | |
| 48 | // Log debug messages about the app switch latency optimization. |
| 49 | #define DEBUG_APP_SWITCH 0 |
| 50 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 51 | // Log debug messages about hover events. |
| 52 | #define DEBUG_HOVER 0 |
| 53 | |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 54 | #include "InputDispatcher.h" |
| 55 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 56 | #include <cutils/log.h> |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 57 | #include <ui/PowerManager.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | |
| 59 | #include <stddef.h> |
| 60 | #include <unistd.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 61 | #include <errno.h> |
| 62 | #include <limits.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 63 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 64 | #define INDENT " " |
| 65 | #define INDENT2 " " |
| 66 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 67 | namespace android { |
| 68 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 69 | // Default input dispatching timeout if there is no focused application or paused window |
| 70 | // from which to determine an appropriate dispatching timeout. |
| 71 | const nsecs_t DEFAULT_INPUT_DISPATCHING_TIMEOUT = 5000 * 1000000LL; // 5 sec |
| 72 | |
| 73 | // Amount of time to allow for all pending events to be processed when an app switch |
| 74 | // key is on the way. This is used to preempt input dispatch and drop input events |
| 75 | // when an application takes too long to respond and the user has pressed an app switch key. |
| 76 | const nsecs_t APP_SWITCH_TIMEOUT = 500 * 1000000LL; // 0.5sec |
| 77 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 78 | // Amount of time to allow for an event to be dispatched (measured since its eventTime) |
| 79 | // before considering it stale and dropping it. |
| 80 | const nsecs_t STALE_EVENT_TIMEOUT = 10000 * 1000000LL; // 10sec |
| 81 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 82 | // Motion samples that are received within this amount of time are simply coalesced |
| 83 | // when batched instead of being appended. This is done because some drivers update |
| 84 | // the location of pointers one at a time instead of all at once. |
| 85 | // For example, when there are 10 fingers down, the input dispatcher may receive 10 |
| 86 | // samples in quick succession with only one finger's location changed in each sample. |
| 87 | // |
| 88 | // This value effectively imposes an upper bound on the touch sampling rate. |
| 89 | // Touch sensors typically have a 50Hz - 200Hz sampling rate, so we expect distinct |
| 90 | // samples to become available 5-20ms apart but individual finger reports can trickle |
| 91 | // in over a period of 2-4ms or so. |
| 92 | // |
| 93 | // Empirical testing shows that a 2ms coalescing interval (500Hz) is not enough, |
| 94 | // a 3ms coalescing interval (333Hz) works well most of the time and doesn't introduce |
| 95 | // significant quantization noise on current hardware. |
| 96 | const nsecs_t MOTION_SAMPLE_COALESCE_INTERVAL = 3 * 1000000LL; // 3ms, 333Hz |
| 97 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 98 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 99 | static inline nsecs_t now() { |
| 100 | return systemTime(SYSTEM_TIME_MONOTONIC); |
| 101 | } |
| 102 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 103 | static inline const char* toString(bool value) { |
| 104 | return value ? "true" : "false"; |
| 105 | } |
| 106 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 107 | static inline int32_t getMotionEventActionPointerIndex(int32_t action) { |
| 108 | return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) |
| 109 | >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; |
| 110 | } |
| 111 | |
| 112 | static bool isValidKeyAction(int32_t action) { |
| 113 | switch (action) { |
| 114 | case AKEY_EVENT_ACTION_DOWN: |
| 115 | case AKEY_EVENT_ACTION_UP: |
| 116 | return true; |
| 117 | default: |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static bool validateKeyEvent(int32_t action) { |
| 123 | if (! isValidKeyAction(action)) { |
| 124 | LOGE("Key event has invalid action code 0x%x", action); |
| 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 130 | static bool isValidMotionAction(int32_t action, size_t pointerCount) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 131 | switch (action & AMOTION_EVENT_ACTION_MASK) { |
| 132 | case AMOTION_EVENT_ACTION_DOWN: |
| 133 | case AMOTION_EVENT_ACTION_UP: |
| 134 | case AMOTION_EVENT_ACTION_CANCEL: |
| 135 | case AMOTION_EVENT_ACTION_MOVE: |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 136 | case AMOTION_EVENT_ACTION_OUTSIDE: |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 137 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 138 | case AMOTION_EVENT_ACTION_HOVER_MOVE: |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 139 | case AMOTION_EVENT_ACTION_HOVER_EXIT: |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 140 | case AMOTION_EVENT_ACTION_SCROLL: |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 141 | return true; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 142 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 143 | case AMOTION_EVENT_ACTION_POINTER_UP: { |
| 144 | int32_t index = getMotionEventActionPointerIndex(action); |
| 145 | return index >= 0 && size_t(index) < pointerCount; |
| 146 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 147 | default: |
| 148 | return false; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static bool validateMotionEvent(int32_t action, size_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 153 | const PointerProperties* pointerProperties) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 154 | if (! isValidMotionAction(action, pointerCount)) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 155 | LOGE("Motion event has invalid action code 0x%x", action); |
| 156 | return false; |
| 157 | } |
| 158 | if (pointerCount < 1 || pointerCount > MAX_POINTERS) { |
| 159 | LOGE("Motion event has invalid pointer count %d; value must be between 1 and %d.", |
| 160 | pointerCount, MAX_POINTERS); |
| 161 | return false; |
| 162 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 163 | BitSet32 pointerIdBits; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 164 | for (size_t i = 0; i < pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 165 | int32_t id = pointerProperties[i].id; |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 166 | if (id < 0 || id > MAX_POINTER_ID) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 167 | LOGE("Motion event has invalid pointer id %d; value must be between 0 and %d", |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 168 | id, MAX_POINTER_ID); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
Jeff Brown | c3db858 | 2010-10-20 15:33:38 -0700 | [diff] [blame] | 171 | if (pointerIdBits.hasBit(id)) { |
| 172 | LOGE("Motion event has duplicate pointer id %d", id); |
| 173 | return false; |
| 174 | } |
| 175 | pointerIdBits.markBit(id); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 180 | static void scalePointerCoords(const PointerCoords* inCoords, size_t count, float scaleFactor, |
| 181 | PointerCoords* outCoords) { |
| 182 | for (size_t i = 0; i < count; i++) { |
| 183 | outCoords[i] = inCoords[i]; |
| 184 | outCoords[i].scale(scaleFactor); |
| 185 | } |
| 186 | } |
| 187 | |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 188 | static void dumpRegion(String8& dump, const SkRegion& region) { |
| 189 | if (region.isEmpty()) { |
| 190 | dump.append("<empty>"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | bool first = true; |
| 195 | for (SkRegion::Iterator it(region); !it.done(); it.next()) { |
| 196 | if (first) { |
| 197 | first = false; |
| 198 | } else { |
| 199 | dump.append("|"); |
| 200 | } |
| 201 | const SkIRect& rect = it.rect(); |
| 202 | dump.appendFormat("[%d,%d][%d,%d]", rect.fLeft, rect.fTop, rect.fRight, rect.fBottom); |
| 203 | } |
| 204 | } |
| 205 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 206 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 207 | // --- InputDispatcher --- |
| 208 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 209 | InputDispatcher::InputDispatcher(const sp<InputDispatcherPolicyInterface>& policy) : |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 210 | mPolicy(policy), |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 211 | mPendingEvent(NULL), mAppSwitchSawKeyDown(false), mAppSwitchDueTime(LONG_LONG_MAX), |
| 212 | mNextUnblockedEvent(NULL), |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 213 | mDispatchEnabled(true), mDispatchFrozen(false), mInputFilterEnabled(false), |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 214 | mCurrentInputTargetsValid(false), |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 215 | mInputTargetWaitCause(INPUT_TARGET_WAIT_CAUSE_NONE) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 216 | mLooper = new Looper(false); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 218 | mKeyRepeatState.lastKeyEntry = NULL; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 219 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 220 | policy->getDispatcherConfiguration(&mConfig); |
| 221 | |
| 222 | mThrottleState.minTimeBetweenEvents = 1000000000LL / mConfig.maxEventsPerSecond; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 223 | mThrottleState.lastDeviceId = -1; |
| 224 | |
| 225 | #if DEBUG_THROTTLING |
| 226 | mThrottleState.originalSampleCount = 0; |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 227 | LOGD("Throttling - Max events per second = %d", mConfig.maxEventsPerSecond); |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 228 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | InputDispatcher::~InputDispatcher() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 232 | { // acquire lock |
| 233 | AutoMutex _l(mLock); |
| 234 | |
| 235 | resetKeyRepeatLocked(); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 236 | releasePendingEventLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 237 | drainInboundQueueLocked(); |
| 238 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 239 | |
| 240 | while (mConnectionsByReceiveFd.size() != 0) { |
| 241 | unregisterInputChannel(mConnectionsByReceiveFd.valueAt(0)->inputChannel); |
| 242 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | void InputDispatcher::dispatchOnce() { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 246 | nsecs_t nextWakeupTime = LONG_LONG_MAX; |
| 247 | { // acquire lock |
| 248 | AutoMutex _l(mLock); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 249 | dispatchOnceInnerLocked(&nextWakeupTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 250 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 251 | if (runCommandsLockedInterruptible()) { |
| 252 | nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 253 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 254 | } // release lock |
| 255 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 256 | // Wait for callback or timeout or wake. (make sure we round up, not down) |
| 257 | nsecs_t currentTime = now(); |
Jeff Brown | aa3855d | 2011-03-17 01:34:19 -0700 | [diff] [blame] | 258 | int timeoutMillis = toMillisecondTimeoutDelay(currentTime, nextWakeupTime); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 259 | mLooper->pollOnce(timeoutMillis); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 262 | void InputDispatcher::dispatchOnceInnerLocked(nsecs_t* nextWakeupTime) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 263 | nsecs_t currentTime = now(); |
| 264 | |
| 265 | // Reset the key repeat timer whenever we disallow key events, even if the next event |
| 266 | // is not a key. This is to ensure that we abort a key repeat if the device is just coming |
| 267 | // out of sleep. |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 268 | if (!mPolicy->isKeyRepeatEnabled()) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 269 | resetKeyRepeatLocked(); |
| 270 | } |
| 271 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 272 | // If dispatching is frozen, do not process timeouts or try to deliver any new events. |
| 273 | if (mDispatchFrozen) { |
| 274 | #if DEBUG_FOCUS |
| 275 | LOGD("Dispatch frozen. Waiting some more."); |
| 276 | #endif |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | // Optimize latency of app switches. |
| 281 | // Essentially we start a short timeout when an app switch key (HOME / ENDCALL) has |
| 282 | // been pressed. When it expires, we preempt dispatch and drop all other pending events. |
| 283 | bool isAppSwitchDue = mAppSwitchDueTime <= currentTime; |
| 284 | if (mAppSwitchDueTime < *nextWakeupTime) { |
| 285 | *nextWakeupTime = mAppSwitchDueTime; |
| 286 | } |
| 287 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 288 | // Ready to start a new event. |
| 289 | // If we don't already have a pending event, go grab one. |
| 290 | if (! mPendingEvent) { |
| 291 | if (mInboundQueue.isEmpty()) { |
| 292 | if (isAppSwitchDue) { |
| 293 | // The inbound queue is empty so the app switch key we were waiting |
| 294 | // for will never arrive. Stop waiting for it. |
| 295 | resetPendingAppSwitchLocked(false); |
| 296 | isAppSwitchDue = false; |
| 297 | } |
| 298 | |
| 299 | // Synthesize a key repeat if appropriate. |
| 300 | if (mKeyRepeatState.lastKeyEntry) { |
| 301 | if (currentTime >= mKeyRepeatState.nextRepeatTime) { |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 302 | mPendingEvent = synthesizeKeyRepeatLocked(currentTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 303 | } else { |
| 304 | if (mKeyRepeatState.nextRepeatTime < *nextWakeupTime) { |
| 305 | *nextWakeupTime = mKeyRepeatState.nextRepeatTime; |
| 306 | } |
| 307 | } |
| 308 | } |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 309 | |
| 310 | // Nothing to do if there is no pending event. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 311 | if (! mPendingEvent) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 312 | if (mActiveConnections.isEmpty()) { |
| 313 | dispatchIdleLocked(); |
| 314 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | } else { |
| 318 | // Inbound queue has at least one entry. |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 319 | EventEntry* entry = mInboundQueue.head; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 320 | |
| 321 | // Throttle the entry if it is a move event and there are no |
| 322 | // other events behind it in the queue. Due to movement batching, additional |
| 323 | // samples may be appended to this event by the time the throttling timeout |
| 324 | // expires. |
| 325 | // TODO Make this smarter and consider throttling per device independently. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 326 | if (entry->type == EventEntry::TYPE_MOTION |
| 327 | && !isAppSwitchDue |
| 328 | && mDispatchEnabled |
| 329 | && (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) |
| 330 | && !entry->isInjected()) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 331 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 332 | int32_t deviceId = motionEntry->deviceId; |
| 333 | uint32_t source = motionEntry->source; |
| 334 | if (! isAppSwitchDue |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 335 | && !motionEntry->next // exactly one event, no successors |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 336 | && (motionEntry->action == AMOTION_EVENT_ACTION_MOVE |
| 337 | || motionEntry->action == AMOTION_EVENT_ACTION_HOVER_MOVE) |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 338 | && deviceId == mThrottleState.lastDeviceId |
| 339 | && source == mThrottleState.lastSource) { |
| 340 | nsecs_t nextTime = mThrottleState.lastEventTime |
| 341 | + mThrottleState.minTimeBetweenEvents; |
| 342 | if (currentTime < nextTime) { |
| 343 | // Throttle it! |
| 344 | #if DEBUG_THROTTLING |
| 345 | LOGD("Throttling - Delaying motion event for " |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 346 | "device %d, source 0x%08x by up to %0.3fms.", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 347 | deviceId, source, (nextTime - currentTime) * 0.000001); |
| 348 | #endif |
| 349 | if (nextTime < *nextWakeupTime) { |
| 350 | *nextWakeupTime = nextTime; |
| 351 | } |
| 352 | if (mThrottleState.originalSampleCount == 0) { |
| 353 | mThrottleState.originalSampleCount = |
| 354 | motionEntry->countSamples(); |
| 355 | } |
| 356 | return; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | #if DEBUG_THROTTLING |
| 361 | if (mThrottleState.originalSampleCount != 0) { |
| 362 | uint32_t count = motionEntry->countSamples(); |
| 363 | LOGD("Throttling - Motion event sample count grew by %d from %d to %d.", |
| 364 | count - mThrottleState.originalSampleCount, |
| 365 | mThrottleState.originalSampleCount, count); |
| 366 | mThrottleState.originalSampleCount = 0; |
| 367 | } |
| 368 | #endif |
| 369 | |
makarand.karvekar | f634ded | 2011-03-02 15:41:03 -0600 | [diff] [blame] | 370 | mThrottleState.lastEventTime = currentTime; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 371 | mThrottleState.lastDeviceId = deviceId; |
| 372 | mThrottleState.lastSource = source; |
| 373 | } |
| 374 | |
| 375 | mInboundQueue.dequeue(entry); |
| 376 | mPendingEvent = entry; |
| 377 | } |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 378 | |
| 379 | // Poke user activity for this event. |
| 380 | if (mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
| 381 | pokeUserActivityLocked(mPendingEvent); |
| 382 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // Now we have an event to dispatch. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 386 | // All events are eventually dequeued and processed this way, even if we intend to drop them. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 387 | LOG_ASSERT(mPendingEvent != NULL); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 388 | bool done = false; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 389 | DropReason dropReason = DROP_REASON_NOT_DROPPED; |
| 390 | if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) { |
| 391 | dropReason = DROP_REASON_POLICY; |
| 392 | } else if (!mDispatchEnabled) { |
| 393 | dropReason = DROP_REASON_DISABLED; |
| 394 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 395 | |
| 396 | if (mNextUnblockedEvent == mPendingEvent) { |
| 397 | mNextUnblockedEvent = NULL; |
| 398 | } |
| 399 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 400 | switch (mPendingEvent->type) { |
| 401 | case EventEntry::TYPE_CONFIGURATION_CHANGED: { |
| 402 | ConfigurationChangedEntry* typedEntry = |
| 403 | static_cast<ConfigurationChangedEntry*>(mPendingEvent); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 404 | done = dispatchConfigurationChangedLocked(currentTime, typedEntry); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 405 | dropReason = DROP_REASON_NOT_DROPPED; // configuration changes are never dropped |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 409 | case EventEntry::TYPE_DEVICE_RESET: { |
| 410 | DeviceResetEntry* typedEntry = |
| 411 | static_cast<DeviceResetEntry*>(mPendingEvent); |
| 412 | done = dispatchDeviceResetLocked(currentTime, typedEntry); |
| 413 | dropReason = DROP_REASON_NOT_DROPPED; // device resets are never dropped |
| 414 | break; |
| 415 | } |
| 416 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 417 | case EventEntry::TYPE_KEY: { |
| 418 | KeyEntry* typedEntry = static_cast<KeyEntry*>(mPendingEvent); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 419 | if (isAppSwitchDue) { |
| 420 | if (isAppSwitchKeyEventLocked(typedEntry)) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 421 | resetPendingAppSwitchLocked(true); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 422 | isAppSwitchDue = false; |
| 423 | } else if (dropReason == DROP_REASON_NOT_DROPPED) { |
| 424 | dropReason = DROP_REASON_APP_SWITCH; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 427 | if (dropReason == DROP_REASON_NOT_DROPPED |
| 428 | && isStaleEventLocked(currentTime, typedEntry)) { |
| 429 | dropReason = DROP_REASON_STALE; |
| 430 | } |
| 431 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| 432 | dropReason = DROP_REASON_BLOCKED; |
| 433 | } |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 434 | done = dispatchKeyLocked(currentTime, typedEntry, &dropReason, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | |
| 438 | case EventEntry::TYPE_MOTION: { |
| 439 | MotionEntry* typedEntry = static_cast<MotionEntry*>(mPendingEvent); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 440 | if (dropReason == DROP_REASON_NOT_DROPPED && isAppSwitchDue) { |
| 441 | dropReason = DROP_REASON_APP_SWITCH; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 442 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 443 | if (dropReason == DROP_REASON_NOT_DROPPED |
| 444 | && isStaleEventLocked(currentTime, typedEntry)) { |
| 445 | dropReason = DROP_REASON_STALE; |
| 446 | } |
| 447 | if (dropReason == DROP_REASON_NOT_DROPPED && mNextUnblockedEvent) { |
| 448 | dropReason = DROP_REASON_BLOCKED; |
| 449 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 450 | done = dispatchMotionLocked(currentTime, typedEntry, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 451 | &dropReason, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | |
| 455 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 456 | LOG_ASSERT(false); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 460 | if (done) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 461 | if (dropReason != DROP_REASON_NOT_DROPPED) { |
| 462 | dropInboundEventLocked(mPendingEvent, dropReason); |
| 463 | } |
| 464 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 465 | releasePendingEventLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 466 | *nextWakeupTime = LONG_LONG_MIN; // force next poll to wake up immediately |
| 467 | } |
| 468 | } |
| 469 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 470 | void InputDispatcher::dispatchIdleLocked() { |
| 471 | #if DEBUG_FOCUS |
| 472 | LOGD("Dispatcher idle. There are no pending events or active connections."); |
| 473 | #endif |
| 474 | |
| 475 | // Reset targets when idle, to release input channels and other resources |
| 476 | // they are holding onto. |
| 477 | resetTargetsLocked(); |
| 478 | } |
| 479 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 480 | bool InputDispatcher::enqueueInboundEventLocked(EventEntry* entry) { |
| 481 | bool needWake = mInboundQueue.isEmpty(); |
| 482 | mInboundQueue.enqueueAtTail(entry); |
| 483 | |
| 484 | switch (entry->type) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 485 | case EventEntry::TYPE_KEY: { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 486 | // Optimize app switch latency. |
| 487 | // If the application takes too long to catch up then we drop all events preceding |
| 488 | // the app switch key. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 489 | KeyEntry* keyEntry = static_cast<KeyEntry*>(entry); |
| 490 | if (isAppSwitchKeyEventLocked(keyEntry)) { |
| 491 | if (keyEntry->action == AKEY_EVENT_ACTION_DOWN) { |
| 492 | mAppSwitchSawKeyDown = true; |
| 493 | } else if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 494 | if (mAppSwitchSawKeyDown) { |
| 495 | #if DEBUG_APP_SWITCH |
| 496 | LOGD("App switch is pending!"); |
| 497 | #endif |
| 498 | mAppSwitchDueTime = keyEntry->eventTime + APP_SWITCH_TIMEOUT; |
| 499 | mAppSwitchSawKeyDown = false; |
| 500 | needWake = true; |
| 501 | } |
| 502 | } |
| 503 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 504 | break; |
| 505 | } |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 506 | |
| 507 | case EventEntry::TYPE_MOTION: { |
| 508 | // Optimize case where the current application is unresponsive and the user |
| 509 | // decides to touch a window in a different application. |
| 510 | // If the application takes too long to catch up then we drop all events preceding |
| 511 | // the touch into the other window. |
| 512 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 513 | if (motionEntry->action == AMOTION_EVENT_ACTION_DOWN |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 514 | && (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) |
| 515 | && mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 516 | && mInputTargetWaitApplicationHandle != NULL) { |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 517 | int32_t x = int32_t(motionEntry->firstSample.pointerCoords[0]. |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 518 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 519 | int32_t y = int32_t(motionEntry->firstSample.pointerCoords[0]. |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 520 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 521 | sp<InputWindowHandle> touchedWindowHandle = findTouchedWindowAtLocked(x, y); |
| 522 | if (touchedWindowHandle != NULL |
| 523 | && touchedWindowHandle->inputApplicationHandle |
| 524 | != mInputTargetWaitApplicationHandle) { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 525 | // User touched a different application than the one we are waiting on. |
| 526 | // Flag the event, and start pruning the input queue. |
| 527 | mNextUnblockedEvent = motionEntry; |
| 528 | needWake = true; |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 533 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 534 | |
| 535 | return needWake; |
| 536 | } |
| 537 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 538 | sp<InputWindowHandle> InputDispatcher::findTouchedWindowAtLocked(int32_t x, int32_t y) { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 539 | // Traverse windows from front to back to find touched window. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 540 | size_t numWindows = mWindowHandles.size(); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 541 | for (size_t i = 0; i < numWindows; i++) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 542 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 543 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 544 | int32_t flags = windowInfo->layoutParamsFlags; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 545 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 546 | if (windowInfo->visible) { |
| 547 | if (!(flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
| 548 | bool isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
| 549 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
| 550 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 551 | // Found window. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 552 | return windowHandle; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 557 | if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 558 | // Error window is on top but not visible, so touch is dropped. |
| 559 | return NULL; |
| 560 | } |
| 561 | } |
| 562 | return NULL; |
| 563 | } |
| 564 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 565 | void InputDispatcher::dropInboundEventLocked(EventEntry* entry, DropReason dropReason) { |
| 566 | const char* reason; |
| 567 | switch (dropReason) { |
| 568 | case DROP_REASON_POLICY: |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 569 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 570 | LOGD("Dropped event because policy consumed it."); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 571 | #endif |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 572 | reason = "inbound event was dropped because the policy consumed it"; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 573 | break; |
| 574 | case DROP_REASON_DISABLED: |
| 575 | LOGI("Dropped event because input dispatch is disabled."); |
| 576 | reason = "inbound event was dropped because input dispatch is disabled"; |
| 577 | break; |
| 578 | case DROP_REASON_APP_SWITCH: |
| 579 | LOGI("Dropped event because of pending overdue app switch."); |
| 580 | reason = "inbound event was dropped because of pending overdue app switch"; |
| 581 | break; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 582 | case DROP_REASON_BLOCKED: |
| 583 | LOGI("Dropped event because the current application is not responding and the user " |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 584 | "has started interacting with a different application."); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 585 | reason = "inbound event was dropped because the current application is not responding " |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 586 | "and the user has started interacting with a different application"; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 587 | break; |
| 588 | case DROP_REASON_STALE: |
| 589 | LOGI("Dropped event because it is stale."); |
| 590 | reason = "inbound event was dropped because it is stale"; |
| 591 | break; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 592 | default: |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 593 | LOG_ASSERT(false); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | |
| 597 | switch (entry->type) { |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 598 | case EventEntry::TYPE_KEY: { |
| 599 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 600 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 601 | break; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 602 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 603 | case EventEntry::TYPE_MOTION: { |
| 604 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
| 605 | if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER) { |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 606 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, reason); |
| 607 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 608 | } else { |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 609 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, reason); |
| 610 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 611 | } |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | bool InputDispatcher::isAppSwitchKeyCode(int32_t keyCode) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 618 | return keyCode == AKEYCODE_HOME || keyCode == AKEYCODE_ENDCALL; |
| 619 | } |
| 620 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 621 | bool InputDispatcher::isAppSwitchKeyEventLocked(KeyEntry* keyEntry) { |
| 622 | return ! (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) |
| 623 | && isAppSwitchKeyCode(keyEntry->keyCode) |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 624 | && (keyEntry->policyFlags & POLICY_FLAG_TRUSTED) |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 625 | && (keyEntry->policyFlags & POLICY_FLAG_PASS_TO_USER); |
| 626 | } |
| 627 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 628 | bool InputDispatcher::isAppSwitchPendingLocked() { |
| 629 | return mAppSwitchDueTime != LONG_LONG_MAX; |
| 630 | } |
| 631 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 632 | void InputDispatcher::resetPendingAppSwitchLocked(bool handled) { |
| 633 | mAppSwitchDueTime = LONG_LONG_MAX; |
| 634 | |
| 635 | #if DEBUG_APP_SWITCH |
| 636 | if (handled) { |
| 637 | LOGD("App switch has arrived."); |
| 638 | } else { |
| 639 | LOGD("App switch was abandoned."); |
| 640 | } |
| 641 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 644 | bool InputDispatcher::isStaleEventLocked(nsecs_t currentTime, EventEntry* entry) { |
| 645 | return currentTime - entry->eventTime >= STALE_EVENT_TIMEOUT; |
| 646 | } |
| 647 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 648 | bool InputDispatcher::runCommandsLockedInterruptible() { |
| 649 | if (mCommandQueue.isEmpty()) { |
| 650 | return false; |
| 651 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 652 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 653 | do { |
| 654 | CommandEntry* commandEntry = mCommandQueue.dequeueAtHead(); |
| 655 | |
| 656 | Command command = commandEntry->command; |
| 657 | (this->*command)(commandEntry); // commands are implicitly 'LockedInterruptible' |
| 658 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 659 | commandEntry->connection.clear(); |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 660 | delete commandEntry; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 661 | } while (! mCommandQueue.isEmpty()); |
| 662 | return true; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 665 | InputDispatcher::CommandEntry* InputDispatcher::postCommandLocked(Command command) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 666 | CommandEntry* commandEntry = new CommandEntry(command); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 667 | mCommandQueue.enqueueAtTail(commandEntry); |
| 668 | return commandEntry; |
| 669 | } |
| 670 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 671 | void InputDispatcher::drainInboundQueueLocked() { |
| 672 | while (! mInboundQueue.isEmpty()) { |
| 673 | EventEntry* entry = mInboundQueue.dequeueAtHead(); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 674 | releaseInboundEventLocked(entry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 675 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 678 | void InputDispatcher::releasePendingEventLocked() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 679 | if (mPendingEvent) { |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 680 | releaseInboundEventLocked(mPendingEvent); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 681 | mPendingEvent = NULL; |
| 682 | } |
| 683 | } |
| 684 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 685 | void InputDispatcher::releaseInboundEventLocked(EventEntry* entry) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 686 | InjectionState* injectionState = entry->injectionState; |
| 687 | if (injectionState && injectionState->injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 688 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 689 | LOGD("Injected inbound event was dropped."); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 690 | #endif |
| 691 | setInjectionResultLocked(entry, INPUT_EVENT_INJECTION_FAILED); |
| 692 | } |
Jeff Brown | abb4d44 | 2011-08-15 12:55:32 -0700 | [diff] [blame] | 693 | if (entry == mNextUnblockedEvent) { |
| 694 | mNextUnblockedEvent = NULL; |
| 695 | } |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 696 | entry->release(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 699 | void InputDispatcher::resetKeyRepeatLocked() { |
| 700 | if (mKeyRepeatState.lastKeyEntry) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 701 | mKeyRepeatState.lastKeyEntry->release(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 702 | mKeyRepeatState.lastKeyEntry = NULL; |
| 703 | } |
| 704 | } |
| 705 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 706 | InputDispatcher::KeyEntry* InputDispatcher::synthesizeKeyRepeatLocked(nsecs_t currentTime) { |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 707 | KeyEntry* entry = mKeyRepeatState.lastKeyEntry; |
| 708 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 709 | // Reuse the repeated key entry if it is otherwise unreferenced. |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 710 | uint32_t policyFlags = (entry->policyFlags & POLICY_FLAG_RAW_MASK) |
| 711 | | POLICY_FLAG_PASS_TO_USER | POLICY_FLAG_TRUSTED; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 712 | if (entry->refCount == 1) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 713 | entry->recycle(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 714 | entry->eventTime = currentTime; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 715 | entry->policyFlags = policyFlags; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 716 | entry->repeatCount += 1; |
| 717 | } else { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 718 | KeyEntry* newEntry = new KeyEntry(currentTime, |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 719 | entry->deviceId, entry->source, policyFlags, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 720 | entry->action, entry->flags, entry->keyCode, entry->scanCode, |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 721 | entry->metaState, entry->repeatCount + 1, entry->downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 722 | |
| 723 | mKeyRepeatState.lastKeyEntry = newEntry; |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 724 | entry->release(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 725 | |
| 726 | entry = newEntry; |
| 727 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 728 | entry->syntheticRepeat = true; |
| 729 | |
| 730 | // Increment reference count since we keep a reference to the event in |
| 731 | // mKeyRepeatState.lastKeyEntry in addition to the one we return. |
| 732 | entry->refCount += 1; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 733 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 734 | mKeyRepeatState.nextRepeatTime = currentTime + mConfig.keyRepeatDelay; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 735 | return entry; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 738 | bool InputDispatcher::dispatchConfigurationChangedLocked( |
| 739 | nsecs_t currentTime, ConfigurationChangedEntry* entry) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 740 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 741 | LOGD("dispatchConfigurationChanged - eventTime=%lld", entry->eventTime); |
| 742 | #endif |
| 743 | |
| 744 | // Reset key repeating in case a keyboard device was added or removed or something. |
| 745 | resetKeyRepeatLocked(); |
| 746 | |
| 747 | // Enqueue a command to run outside the lock to tell the policy that the configuration changed. |
| 748 | CommandEntry* commandEntry = postCommandLocked( |
| 749 | & InputDispatcher::doNotifyConfigurationChangedInterruptible); |
| 750 | commandEntry->eventTime = entry->eventTime; |
| 751 | return true; |
| 752 | } |
| 753 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 754 | bool InputDispatcher::dispatchDeviceResetLocked( |
| 755 | nsecs_t currentTime, DeviceResetEntry* entry) { |
| 756 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 757 | LOGD("dispatchDeviceReset - eventTime=%lld, deviceId=%d", entry->eventTime, entry->deviceId); |
| 758 | #endif |
| 759 | |
| 760 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
| 761 | "device was reset"); |
| 762 | options.deviceId = entry->deviceId; |
| 763 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
| 764 | return true; |
| 765 | } |
| 766 | |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 767 | bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, KeyEntry* entry, |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 768 | DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 769 | // Preprocessing. |
| 770 | if (! entry->dispatchInProgress) { |
| 771 | if (entry->repeatCount == 0 |
| 772 | && entry->action == AKEY_EVENT_ACTION_DOWN |
| 773 | && (entry->policyFlags & POLICY_FLAG_TRUSTED) |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 774 | && (!(entry->policyFlags & POLICY_FLAG_DISABLE_KEY_REPEAT))) { |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 775 | if (mKeyRepeatState.lastKeyEntry |
| 776 | && mKeyRepeatState.lastKeyEntry->keyCode == entry->keyCode) { |
| 777 | // We have seen two identical key downs in a row which indicates that the device |
| 778 | // driver is automatically generating key repeats itself. We take note of the |
| 779 | // repeat here, but we disable our own next key repeat timer since it is clear that |
| 780 | // we will not need to synthesize key repeats ourselves. |
| 781 | entry->repeatCount = mKeyRepeatState.lastKeyEntry->repeatCount + 1; |
| 782 | resetKeyRepeatLocked(); |
| 783 | mKeyRepeatState.nextRepeatTime = LONG_LONG_MAX; // don't generate repeats ourselves |
| 784 | } else { |
| 785 | // Not a repeat. Save key down state in case we do see a repeat later. |
| 786 | resetKeyRepeatLocked(); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 787 | mKeyRepeatState.nextRepeatTime = entry->eventTime + mConfig.keyRepeatTimeout; |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 788 | } |
| 789 | mKeyRepeatState.lastKeyEntry = entry; |
| 790 | entry->refCount += 1; |
| 791 | } else if (! entry->syntheticRepeat) { |
| 792 | resetKeyRepeatLocked(); |
| 793 | } |
| 794 | |
Jeff Brown | e2e0126 | 2011-03-02 20:34:30 -0800 | [diff] [blame] | 795 | if (entry->repeatCount == 1) { |
| 796 | entry->flags |= AKEY_EVENT_FLAG_LONG_PRESS; |
| 797 | } else { |
| 798 | entry->flags &= ~AKEY_EVENT_FLAG_LONG_PRESS; |
| 799 | } |
| 800 | |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 801 | entry->dispatchInProgress = true; |
| 802 | resetTargetsLocked(); |
| 803 | |
| 804 | logOutboundKeyDetailsLocked("dispatchKey - ", entry); |
| 805 | } |
| 806 | |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 807 | // Handle case where the policy asked us to try again later last time. |
| 808 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER) { |
| 809 | if (currentTime < entry->interceptKeyWakeupTime) { |
| 810 | if (entry->interceptKeyWakeupTime < *nextWakeupTime) { |
| 811 | *nextWakeupTime = entry->interceptKeyWakeupTime; |
| 812 | } |
| 813 | return false; // wait until next wakeup |
| 814 | } |
| 815 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
| 816 | entry->interceptKeyWakeupTime = 0; |
| 817 | } |
| 818 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 819 | // Give the policy a chance to intercept the key. |
| 820 | if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN) { |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 821 | if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) { |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 822 | CommandEntry* commandEntry = postCommandLocked( |
| 823 | & InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 824 | if (mFocusedWindowHandle != NULL) { |
| 825 | commandEntry->inputWindowHandle = mFocusedWindowHandle; |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 826 | } |
| 827 | commandEntry->keyEntry = entry; |
| 828 | entry->refCount += 1; |
| 829 | return false; // wait for the command to run |
| 830 | } else { |
| 831 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 832 | } |
| 833 | } else if (entry->interceptKeyResult == KeyEntry::INTERCEPT_KEY_RESULT_SKIP) { |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 834 | if (*dropReason == DROP_REASON_NOT_DROPPED) { |
| 835 | *dropReason = DROP_REASON_POLICY; |
| 836 | } |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | // Clean up if dropping the event. |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 840 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 841 | resetTargetsLocked(); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 842 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
| 843 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 844 | return true; |
| 845 | } |
| 846 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 847 | // Identify targets. |
| 848 | if (! mCurrentInputTargetsValid) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 849 | int32_t injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| 850 | entry, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 851 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | setInjectionResultLocked(entry, injectionResult); |
| 856 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 857 | return true; |
| 858 | } |
| 859 | |
| 860 | addMonitoringTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 861 | commitTargetsLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // Dispatch the key. |
| 865 | dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 866 | return true; |
| 867 | } |
| 868 | |
| 869 | void InputDispatcher::logOutboundKeyDetailsLocked(const char* prefix, const KeyEntry* entry) { |
| 870 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 871 | LOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 872 | "action=0x%x, flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, " |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 873 | "repeatCount=%d, downTime=%lld", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 874 | prefix, |
| 875 | entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, |
| 876 | entry->action, entry->flags, entry->keyCode, entry->scanCode, entry->metaState, |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 877 | entry->repeatCount, entry->downTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 878 | #endif |
| 879 | } |
| 880 | |
| 881 | bool InputDispatcher::dispatchMotionLocked( |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 882 | nsecs_t currentTime, MotionEntry* entry, DropReason* dropReason, nsecs_t* nextWakeupTime) { |
Jeff Brown | e46a0a4 | 2010-11-02 17:58:22 -0700 | [diff] [blame] | 883 | // Preprocessing. |
| 884 | if (! entry->dispatchInProgress) { |
| 885 | entry->dispatchInProgress = true; |
| 886 | resetTargetsLocked(); |
| 887 | |
| 888 | logOutboundMotionDetailsLocked("dispatchMotion - ", entry); |
| 889 | } |
| 890 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 891 | // Clean up if dropping the event. |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 892 | if (*dropReason != DROP_REASON_NOT_DROPPED) { |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 893 | resetTargetsLocked(); |
Jeff Brown | 3122e44 | 2010-10-11 23:32:49 -0700 | [diff] [blame] | 894 | setInjectionResultLocked(entry, *dropReason == DROP_REASON_POLICY |
| 895 | ? INPUT_EVENT_INJECTION_SUCCEEDED : INPUT_EVENT_INJECTION_FAILED); |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 896 | return true; |
| 897 | } |
| 898 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 899 | bool isPointerEvent = entry->source & AINPUT_SOURCE_CLASS_POINTER; |
| 900 | |
| 901 | // Identify targets. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 902 | bool conflictingPointerActions = false; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 903 | if (! mCurrentInputTargetsValid) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 904 | int32_t injectionResult; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 905 | const MotionSample* splitBatchAfterSample = NULL; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 906 | if (isPointerEvent) { |
| 907 | // Pointer event. (eg. touchscreen) |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 908 | injectionResult = findTouchedWindowTargetsLocked(currentTime, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 909 | entry, nextWakeupTime, &conflictingPointerActions, &splitBatchAfterSample); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 910 | } else { |
| 911 | // Non touch event. (eg. trackball) |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 912 | injectionResult = findFocusedWindowTargetsLocked(currentTime, |
| 913 | entry, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 914 | } |
| 915 | if (injectionResult == INPUT_EVENT_INJECTION_PENDING) { |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | setInjectionResultLocked(entry, injectionResult); |
| 920 | if (injectionResult != INPUT_EVENT_INJECTION_SUCCEEDED) { |
| 921 | return true; |
| 922 | } |
| 923 | |
| 924 | addMonitoringTargetsLocked(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 925 | commitTargetsLocked(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 926 | |
| 927 | // Unbatch the event if necessary by splitting it into two parts after the |
| 928 | // motion sample indicated by splitBatchAfterSample. |
| 929 | if (splitBatchAfterSample && splitBatchAfterSample->next) { |
| 930 | #if DEBUG_BATCHING |
| 931 | uint32_t originalSampleCount = entry->countSamples(); |
| 932 | #endif |
| 933 | MotionSample* nextSample = splitBatchAfterSample->next; |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 934 | MotionEntry* nextEntry = new MotionEntry(nextSample->eventTime, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 935 | entry->deviceId, entry->source, entry->policyFlags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 936 | entry->action, entry->flags, |
| 937 | entry->metaState, entry->buttonState, entry->edgeFlags, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 938 | entry->xPrecision, entry->yPrecision, entry->downTime, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 939 | entry->pointerCount, entry->pointerProperties, nextSample->pointerCoords); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 940 | if (nextSample != entry->lastSample) { |
| 941 | nextEntry->firstSample.next = nextSample->next; |
| 942 | nextEntry->lastSample = entry->lastSample; |
| 943 | } |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 944 | delete nextSample; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 945 | |
| 946 | entry->lastSample = const_cast<MotionSample*>(splitBatchAfterSample); |
| 947 | entry->lastSample->next = NULL; |
| 948 | |
| 949 | if (entry->injectionState) { |
| 950 | nextEntry->injectionState = entry->injectionState; |
| 951 | entry->injectionState->refCount += 1; |
| 952 | } |
| 953 | |
| 954 | #if DEBUG_BATCHING |
| 955 | LOGD("Split batch of %d samples into two parts, first part has %d samples, " |
| 956 | "second part has %d samples.", originalSampleCount, |
| 957 | entry->countSamples(), nextEntry->countSamples()); |
| 958 | #endif |
| 959 | |
| 960 | mInboundQueue.enqueueAtHead(nextEntry); |
| 961 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | // Dispatch the motion. |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 965 | if (conflictingPointerActions) { |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 966 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 967 | "conflicting pointer actions"); |
| 968 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 969 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 970 | dispatchEventToCurrentInputTargetsLocked(currentTime, entry, false); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 971 | return true; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | void InputDispatcher::logOutboundMotionDetailsLocked(const char* prefix, const MotionEntry* entry) { |
| 976 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 977 | LOGD("%seventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 978 | "action=0x%x, flags=0x%x, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 979 | "metaState=0x%x, buttonState=0x%x, " |
| 980 | "edgeFlags=0x%x, xPrecision=%f, yPrecision=%f, downTime=%lld", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 981 | prefix, |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 982 | entry->eventTime, entry->deviceId, entry->source, entry->policyFlags, |
| 983 | entry->action, entry->flags, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 984 | entry->metaState, entry->buttonState, |
| 985 | entry->edgeFlags, entry->xPrecision, entry->yPrecision, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 986 | entry->downTime); |
| 987 | |
| 988 | // Print the most recent sample that we have available, this may change due to batching. |
| 989 | size_t sampleCount = 1; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 990 | const MotionSample* sample = & entry->firstSample; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 991 | for (; sample->next != NULL; sample = sample->next) { |
| 992 | sampleCount += 1; |
| 993 | } |
| 994 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 995 | LOGD(" Pointer %d: id=%d, toolType=%d, " |
| 996 | "x=%f, y=%f, pressure=%f, size=%f, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 997 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 998 | "orientation=%f", |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 999 | i, entry->pointerProperties[i].id, |
| 1000 | entry->pointerProperties[i].toolType, |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1001 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1002 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 1003 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 1004 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 1005 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 1006 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 1007 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 1008 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 1009 | sample->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | // Keep in mind that due to batching, it is possible for the number of samples actually |
| 1013 | // dispatched to change before the application finally consumed them. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1014 | if (entry->action == AMOTION_EVENT_ACTION_MOVE) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1015 | LOGD(" ... Total movement samples currently batched %d ...", sampleCount); |
| 1016 | } |
| 1017 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | void InputDispatcher::dispatchEventToCurrentInputTargetsLocked(nsecs_t currentTime, |
| 1021 | EventEntry* eventEntry, bool resumeWithAppendedMotionSample) { |
| 1022 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1023 | LOGD("dispatchEventToCurrentInputTargets - " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1024 | "resumeWithAppendedMotionSample=%s", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1025 | toString(resumeWithAppendedMotionSample)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1026 | #endif |
| 1027 | |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1028 | LOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 1029 | |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 1030 | pokeUserActivityLocked(eventEntry); |
| 1031 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1032 | for (size_t i = 0; i < mCurrentInputTargets.size(); i++) { |
| 1033 | const InputTarget& inputTarget = mCurrentInputTargets.itemAt(i); |
| 1034 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1035 | ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1036 | if (connectionIndex >= 0) { |
| 1037 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1038 | prepareDispatchCycleLocked(currentTime, connection, eventEntry, & inputTarget, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1039 | resumeWithAppendedMotionSample); |
| 1040 | } else { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1041 | #if DEBUG_FOCUS |
| 1042 | LOGD("Dropping event delivery to target with channel '%s' because it " |
| 1043 | "is no longer registered with the input dispatcher.", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1044 | inputTarget.inputChannel->getName().string()); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1045 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
Jeff Brown | 54a1825 | 2010-09-16 14:07:33 -0700 | [diff] [blame] | 1050 | void InputDispatcher::resetTargetsLocked() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1051 | mCurrentInputTargetsValid = false; |
| 1052 | mCurrentInputTargets.clear(); |
Jeff Brown | 5ea29ab | 2011-07-27 11:50:51 -0700 | [diff] [blame] | 1053 | resetANRTimeoutsLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1056 | void InputDispatcher::commitTargetsLocked() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1057 | mCurrentInputTargetsValid = true; |
| 1058 | } |
| 1059 | |
| 1060 | int32_t InputDispatcher::handleTargetsNotReadyLocked(nsecs_t currentTime, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1061 | const EventEntry* entry, |
| 1062 | const sp<InputApplicationHandle>& applicationHandle, |
| 1063 | const sp<InputWindowHandle>& windowHandle, |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1064 | nsecs_t* nextWakeupTime) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1065 | if (applicationHandle == NULL && windowHandle == NULL) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1066 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY) { |
| 1067 | #if DEBUG_FOCUS |
| 1068 | LOGD("Waiting for system to become ready for input."); |
| 1069 | #endif |
| 1070 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_SYSTEM_NOT_READY; |
| 1071 | mInputTargetWaitStartTime = currentTime; |
| 1072 | mInputTargetWaitTimeoutTime = LONG_LONG_MAX; |
| 1073 | mInputTargetWaitTimeoutExpired = false; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1074 | mInputTargetWaitApplicationHandle.clear(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } else { |
| 1077 | if (mInputTargetWaitCause != INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| 1078 | #if DEBUG_FOCUS |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1079 | LOGD("Waiting for application to become ready for input: %s", |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1080 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).string()); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1081 | #endif |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1082 | nsecs_t timeout; |
| 1083 | if (windowHandle != NULL) { |
| 1084 | timeout = windowHandle->getDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1085 | } else if (applicationHandle != NULL) { |
| 1086 | timeout = applicationHandle->getDispatchingTimeout( |
| 1087 | DEFAULT_INPUT_DISPATCHING_TIMEOUT); |
| 1088 | } else { |
| 1089 | timeout = DEFAULT_INPUT_DISPATCHING_TIMEOUT; |
| 1090 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1091 | |
| 1092 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY; |
| 1093 | mInputTargetWaitStartTime = currentTime; |
| 1094 | mInputTargetWaitTimeoutTime = currentTime + timeout; |
| 1095 | mInputTargetWaitTimeoutExpired = false; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1096 | mInputTargetWaitApplicationHandle.clear(); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1097 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1098 | if (windowHandle != NULL) { |
| 1099 | mInputTargetWaitApplicationHandle = windowHandle->inputApplicationHandle; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1100 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1101 | if (mInputTargetWaitApplicationHandle == NULL && applicationHandle != NULL) { |
| 1102 | mInputTargetWaitApplicationHandle = applicationHandle; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 1103 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | if (mInputTargetWaitTimeoutExpired) { |
| 1108 | return INPUT_EVENT_INJECTION_TIMED_OUT; |
| 1109 | } |
| 1110 | |
| 1111 | if (currentTime >= mInputTargetWaitTimeoutTime) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1112 | onANRLocked(currentTime, applicationHandle, windowHandle, |
| 1113 | entry->eventTime, mInputTargetWaitStartTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1114 | |
| 1115 | // Force poll loop to wake up immediately on next iteration once we get the |
| 1116 | // ANR response back from the policy. |
| 1117 | *nextWakeupTime = LONG_LONG_MIN; |
| 1118 | return INPUT_EVENT_INJECTION_PENDING; |
| 1119 | } else { |
| 1120 | // Force poll loop to wake up when timeout is due. |
| 1121 | if (mInputTargetWaitTimeoutTime < *nextWakeupTime) { |
| 1122 | *nextWakeupTime = mInputTargetWaitTimeoutTime; |
| 1123 | } |
| 1124 | return INPUT_EVENT_INJECTION_PENDING; |
| 1125 | } |
| 1126 | } |
| 1127 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1128 | void InputDispatcher::resumeAfterTargetsNotReadyTimeoutLocked(nsecs_t newTimeout, |
| 1129 | const sp<InputChannel>& inputChannel) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1130 | if (newTimeout > 0) { |
| 1131 | // Extend the timeout. |
| 1132 | mInputTargetWaitTimeoutTime = now() + newTimeout; |
| 1133 | } else { |
| 1134 | // Give up. |
| 1135 | mInputTargetWaitTimeoutExpired = true; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1136 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1137 | // Release the touch targets. |
| 1138 | mTouchState.reset(); |
Jeff Brown | 2a95c2a | 2010-09-16 12:31:46 -0700 | [diff] [blame] | 1139 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1140 | // Input state will not be realistic. Mark it out of sync. |
Jeff Brown | dc3e005 | 2010-09-16 11:02:16 -0700 | [diff] [blame] | 1141 | if (inputChannel.get()) { |
| 1142 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
| 1143 | if (connectionIndex >= 0) { |
| 1144 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
Jeff Brown | 00045a7 | 2010-12-09 18:10:30 -0800 | [diff] [blame] | 1145 | if (connection->status == Connection::STATUS_NORMAL) { |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 1146 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, |
Jeff Brown | 00045a7 | 2010-12-09 18:10:30 -0800 | [diff] [blame] | 1147 | "application not responding"); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 1148 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
Jeff Brown | 00045a7 | 2010-12-09 18:10:30 -0800 | [diff] [blame] | 1149 | } |
Jeff Brown | dc3e005 | 2010-09-16 11:02:16 -0700 | [diff] [blame] | 1150 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1151 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1155 | nsecs_t InputDispatcher::getTimeSpentWaitingForApplicationLocked( |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1156 | nsecs_t currentTime) { |
| 1157 | if (mInputTargetWaitCause == INPUT_TARGET_WAIT_CAUSE_APPLICATION_NOT_READY) { |
| 1158 | return currentTime - mInputTargetWaitStartTime; |
| 1159 | } |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | void InputDispatcher::resetANRTimeoutsLocked() { |
| 1164 | #if DEBUG_FOCUS |
| 1165 | LOGD("Resetting ANR timeouts."); |
| 1166 | #endif |
| 1167 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1168 | // Reset input target wait timeout. |
| 1169 | mInputTargetWaitCause = INPUT_TARGET_WAIT_CAUSE_NONE; |
Jeff Brown | 5ea29ab | 2011-07-27 11:50:51 -0700 | [diff] [blame] | 1170 | mInputTargetWaitApplicationHandle.clear(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1173 | int32_t InputDispatcher::findFocusedWindowTargetsLocked(nsecs_t currentTime, |
| 1174 | const EventEntry* entry, nsecs_t* nextWakeupTime) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1175 | mCurrentInputTargets.clear(); |
| 1176 | |
| 1177 | int32_t injectionResult; |
| 1178 | |
| 1179 | // If there is no currently focused window and no focused application |
| 1180 | // then drop the event. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1181 | if (mFocusedWindowHandle == NULL) { |
| 1182 | if (mFocusedApplicationHandle != NULL) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1183 | #if DEBUG_FOCUS |
| 1184 | LOGD("Waiting because there is no focused window but there is a " |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1185 | "focused application that may eventually add a window: %s.", |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1186 | getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string()); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1187 | #endif |
| 1188 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1189 | mFocusedApplicationHandle, NULL, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1190 | goto Unresponsive; |
| 1191 | } |
| 1192 | |
| 1193 | LOGI("Dropping event because there is no focused window or focused application."); |
| 1194 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1195 | goto Failed; |
| 1196 | } |
| 1197 | |
| 1198 | // Check permissions. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1199 | if (! checkInjectionPermission(mFocusedWindowHandle, entry->injectionState)) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1200 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1201 | goto Failed; |
| 1202 | } |
| 1203 | |
| 1204 | // If the currently focused window is paused then keep waiting. |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1205 | if (mFocusedWindowHandle->getInfo()->paused) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1206 | #if DEBUG_FOCUS |
| 1207 | LOGD("Waiting because focused window is paused."); |
| 1208 | #endif |
| 1209 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1210 | mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1211 | goto Unresponsive; |
| 1212 | } |
| 1213 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1214 | // If the currently focused window is still working on previous events then keep waiting. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1215 | if (! isWindowFinishedWithPreviousInputLocked(mFocusedWindowHandle)) { |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1216 | #if DEBUG_FOCUS |
| 1217 | LOGD("Waiting because focused window still processing previous input."); |
| 1218 | #endif |
| 1219 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1220 | mFocusedApplicationHandle, mFocusedWindowHandle, nextWakeupTime); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1221 | goto Unresponsive; |
| 1222 | } |
| 1223 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1224 | // Success! Output targets. |
| 1225 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1226 | addWindowTargetLocked(mFocusedWindowHandle, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1227 | InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS, BitSet32(0)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1228 | |
| 1229 | // Done. |
| 1230 | Failed: |
| 1231 | Unresponsive: |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1232 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| 1233 | updateDispatchStatisticsLocked(currentTime, entry, |
| 1234 | injectionResult, timeSpentWaitingForApplication); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1235 | #if DEBUG_FOCUS |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1236 | LOGD("findFocusedWindow finished: injectionResult=%d, " |
| 1237 | "timeSpendWaitingForApplication=%0.1fms", |
| 1238 | injectionResult, timeSpentWaitingForApplication / 1000000.0); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1239 | #endif |
| 1240 | return injectionResult; |
| 1241 | } |
| 1242 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1243 | int32_t InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1244 | const MotionEntry* entry, nsecs_t* nextWakeupTime, bool* outConflictingPointerActions, |
| 1245 | const MotionSample** outSplitBatchAfterSample) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1246 | enum InjectionPermission { |
| 1247 | INJECTION_PERMISSION_UNKNOWN, |
| 1248 | INJECTION_PERMISSION_GRANTED, |
| 1249 | INJECTION_PERMISSION_DENIED |
| 1250 | }; |
| 1251 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1252 | mCurrentInputTargets.clear(); |
| 1253 | |
| 1254 | nsecs_t startTime = now(); |
| 1255 | |
| 1256 | // For security reasons, we defer updating the touch state until we are sure that |
| 1257 | // event injection will be allowed. |
| 1258 | // |
| 1259 | // FIXME In the original code, screenWasOff could never be set to true. |
| 1260 | // The reason is that the POLICY_FLAG_WOKE_HERE |
| 1261 | // and POLICY_FLAG_BRIGHT_HERE flags were set only when preprocessing raw |
| 1262 | // EV_KEY, EV_REL and EV_ABS events. As it happens, the touch event was |
| 1263 | // actually enqueued using the policyFlags that appeared in the final EV_SYN |
| 1264 | // events upon which no preprocessing took place. So policyFlags was always 0. |
| 1265 | // In the new native input dispatcher we're a bit more careful about event |
| 1266 | // preprocessing so the touches we receive can actually have non-zero policyFlags. |
| 1267 | // Unfortunately we obtain undesirable behavior. |
| 1268 | // |
| 1269 | // Here's what happens: |
| 1270 | // |
| 1271 | // When the device dims in anticipation of going to sleep, touches |
| 1272 | // in windows which have FLAG_TOUCHABLE_WHEN_WAKING cause |
| 1273 | // the device to brighten and reset the user activity timer. |
| 1274 | // Touches on other windows (such as the launcher window) |
| 1275 | // are dropped. Then after a moment, the device goes to sleep. Oops. |
| 1276 | // |
| 1277 | // Also notice how screenWasOff was being initialized using POLICY_FLAG_BRIGHT_HERE |
| 1278 | // instead of POLICY_FLAG_WOKE_HERE... |
| 1279 | // |
| 1280 | bool screenWasOff = false; // original policy: policyFlags & POLICY_FLAG_BRIGHT_HERE; |
| 1281 | |
| 1282 | int32_t action = entry->action; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1283 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1284 | |
| 1285 | // Update the touch state as needed based on the properties of the touch event. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1286 | int32_t injectionResult = INPUT_EVENT_INJECTION_PENDING; |
| 1287 | InjectionPermission injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1288 | sp<InputWindowHandle> newHoverWindowHandle; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1289 | |
| 1290 | bool isSplit = mTouchState.split; |
Jeff Brown | 2717eff | 2011-06-30 23:53:07 -0700 | [diff] [blame] | 1291 | bool switchedDevice = mTouchState.deviceId >= 0 |
| 1292 | && (mTouchState.deviceId != entry->deviceId |
| 1293 | || mTouchState.source != entry->source); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1294 | bool isHoverAction = (maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| 1295 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| 1296 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_EXIT); |
| 1297 | bool newGesture = (maskedAction == AMOTION_EVENT_ACTION_DOWN |
| 1298 | || maskedAction == AMOTION_EVENT_ACTION_SCROLL |
| 1299 | || isHoverAction); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1300 | bool wrongDevice = false; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1301 | if (newGesture) { |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1302 | bool down = maskedAction == AMOTION_EVENT_ACTION_DOWN; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1303 | if (switchedDevice && mTouchState.down && !down) { |
| 1304 | #if DEBUG_FOCUS |
| 1305 | LOGD("Dropping event because a pointer for a different device is already down."); |
| 1306 | #endif |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1307 | mTempTouchState.copyFrom(mTouchState); |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1308 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
| 1309 | switchedDevice = false; |
| 1310 | wrongDevice = true; |
| 1311 | goto Failed; |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1312 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1313 | mTempTouchState.reset(); |
| 1314 | mTempTouchState.down = down; |
| 1315 | mTempTouchState.deviceId = entry->deviceId; |
| 1316 | mTempTouchState.source = entry->source; |
| 1317 | isSplit = false; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1318 | } else { |
| 1319 | mTempTouchState.copyFrom(mTouchState); |
Jeff Brown | cc0c159 | 2011-02-19 05:07:28 -0800 | [diff] [blame] | 1320 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1321 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1322 | if (newGesture || (isSplit && maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN)) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1323 | /* Case 1: New splittable pointer going down, or need target for hover or scroll. */ |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1324 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1325 | const MotionSample* sample = &entry->firstSample; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1326 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1327 | int32_t x = int32_t(sample->pointerCoords[pointerIndex]. |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1328 | getAxisValue(AMOTION_EVENT_AXIS_X)); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1329 | int32_t y = int32_t(sample->pointerCoords[pointerIndex]. |
Jeff Brown | ebbd5d1 | 2011-02-17 13:01:34 -0800 | [diff] [blame] | 1330 | getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1331 | sp<InputWindowHandle> newTouchedWindowHandle; |
| 1332 | sp<InputWindowHandle> topErrorWindowHandle; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1333 | bool isTouchModal = false; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1334 | |
| 1335 | // Traverse windows from front to back to find touched window and outside targets. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1336 | size_t numWindows = mWindowHandles.size(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1337 | for (size_t i = 0; i < numWindows; i++) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1338 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1339 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 1340 | int32_t flags = windowInfo->layoutParamsFlags; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1341 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1342 | if (flags & InputWindowInfo::FLAG_SYSTEM_ERROR) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1343 | if (topErrorWindowHandle == NULL) { |
| 1344 | topErrorWindowHandle = windowHandle; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1345 | } |
| 1346 | } |
| 1347 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1348 | if (windowInfo->visible) { |
| 1349 | if (! (flags & InputWindowInfo::FLAG_NOT_TOUCHABLE)) { |
| 1350 | isTouchModal = (flags & (InputWindowInfo::FLAG_NOT_FOCUSABLE |
| 1351 | | InputWindowInfo::FLAG_NOT_TOUCH_MODAL)) == 0; |
| 1352 | if (isTouchModal || windowInfo->touchableRegionContainsPoint(x, y)) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1353 | if (! screenWasOff |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1354 | || (flags & InputWindowInfo::FLAG_TOUCHABLE_WHEN_WAKING)) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1355 | newTouchedWindowHandle = windowHandle; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1356 | } |
| 1357 | break; // found touched window, exit window loop |
| 1358 | } |
| 1359 | } |
| 1360 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1361 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1362 | && (flags & InputWindowInfo::FLAG_WATCH_OUTSIDE_TOUCH)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1363 | int32_t outsideTargetFlags = InputTarget::FLAG_DISPATCH_AS_OUTSIDE; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1364 | if (isWindowObscuredAtPointLocked(windowHandle, x, y)) { |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1365 | outsideTargetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1366 | } |
| 1367 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1368 | mTempTouchState.addOrUpdateWindow( |
| 1369 | windowHandle, outsideTargetFlags, BitSet32(0)); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | // If there is an error window but it is not taking focus (typically because |
| 1375 | // it is invisible) then wait for it. Any other focused window may in |
| 1376 | // fact be in ANR state. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1377 | if (topErrorWindowHandle != NULL && newTouchedWindowHandle != topErrorWindowHandle) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1378 | #if DEBUG_FOCUS |
| 1379 | LOGD("Waiting because system error window is pending."); |
| 1380 | #endif |
| 1381 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
| 1382 | NULL, NULL, nextWakeupTime); |
| 1383 | injectionPermission = INJECTION_PERMISSION_UNKNOWN; |
| 1384 | goto Unresponsive; |
| 1385 | } |
| 1386 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1387 | // Figure out whether splitting will be allowed for this window. |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1388 | if (newTouchedWindowHandle != NULL |
| 1389 | && newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1390 | // New window supports splitting. |
| 1391 | isSplit = true; |
| 1392 | } else if (isSplit) { |
| 1393 | // New window does not support splitting but we have already split events. |
| 1394 | // Assign the pointer to the first foreground window we find. |
| 1395 | // (May be NULL which is why we put this code block before the next check.) |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1396 | newTouchedWindowHandle = mTempTouchState.getFirstForegroundWindowHandle(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1397 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1398 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1399 | // If we did not find a touched window then fail. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1400 | if (newTouchedWindowHandle == NULL) { |
| 1401 | if (mFocusedApplicationHandle != NULL) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1402 | #if DEBUG_FOCUS |
| 1403 | LOGD("Waiting because there is no touched window but there is a " |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1404 | "focused application that may eventually add a new window: %s.", |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1405 | getApplicationWindowLabelLocked(mFocusedApplicationHandle, NULL).string()); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1406 | #endif |
| 1407 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1408 | mFocusedApplicationHandle, NULL, nextWakeupTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1409 | goto Unresponsive; |
| 1410 | } |
| 1411 | |
| 1412 | LOGI("Dropping event because there is no touched window or focused application."); |
| 1413 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1414 | goto Failed; |
| 1415 | } |
| 1416 | |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1417 | // Set target flags. |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1418 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND | InputTarget::FLAG_DISPATCH_AS_IS; |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1419 | if (isSplit) { |
| 1420 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1421 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1422 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1423 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1424 | } |
| 1425 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1426 | // Update hover state. |
| 1427 | if (isHoverAction) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1428 | newHoverWindowHandle = newTouchedWindowHandle; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1429 | |
| 1430 | // Ensure all subsequent motion samples are also within the touched window. |
| 1431 | // Set *outSplitBatchAfterSample to the sample before the first one that is not |
| 1432 | // within the touched window. |
| 1433 | if (!isTouchModal) { |
| 1434 | while (sample->next) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1435 | if (!newHoverWindowHandle->getInfo()->touchableRegionContainsPoint( |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1436 | sample->next->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 1437 | sample->next->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y))) { |
| 1438 | *outSplitBatchAfterSample = sample; |
| 1439 | break; |
| 1440 | } |
| 1441 | sample = sample->next; |
| 1442 | } |
| 1443 | } |
| 1444 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1445 | newHoverWindowHandle = mLastHoverWindowHandle; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1448 | // Update the temporary touch state. |
| 1449 | BitSet32 pointerIds; |
| 1450 | if (isSplit) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1451 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1452 | pointerIds.markBit(pointerId); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1453 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1454 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1455 | } else { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1456 | /* Case 2: Pointer move, up, cancel or non-splittable pointer down. */ |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1457 | |
| 1458 | // If the pointer is not currently down, then ignore the event. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1459 | if (! mTempTouchState.down) { |
Jeff Brown | a2cc28d | 2011-03-25 11:58:46 -0700 | [diff] [blame] | 1460 | #if DEBUG_FOCUS |
Jeff Brown | 76860e3 | 2010-10-25 17:37:46 -0700 | [diff] [blame] | 1461 | LOGD("Dropping event because the pointer is not down or we previously " |
| 1462 | "dropped the pointer down event."); |
| 1463 | #endif |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1464 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1465 | goto Failed; |
| 1466 | } |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1467 | |
| 1468 | // Check whether touches should slip outside of the current foreground window. |
| 1469 | if (maskedAction == AMOTION_EVENT_ACTION_MOVE |
| 1470 | && entry->pointerCount == 1 |
| 1471 | && mTempTouchState.isSlippery()) { |
| 1472 | const MotionSample* sample = &entry->firstSample; |
| 1473 | int32_t x = int32_t(sample->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X)); |
| 1474 | int32_t y = int32_t(sample->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
| 1475 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1476 | sp<InputWindowHandle> oldTouchedWindowHandle = |
| 1477 | mTempTouchState.getFirstForegroundWindowHandle(); |
| 1478 | sp<InputWindowHandle> newTouchedWindowHandle = findTouchedWindowAtLocked(x, y); |
| 1479 | if (oldTouchedWindowHandle != newTouchedWindowHandle |
| 1480 | && newTouchedWindowHandle != NULL) { |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1481 | #if DEBUG_FOCUS |
| 1482 | LOGD("Touch is slipping out of window %s into window %s.", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1483 | oldTouchedWindowHandle->getName().string(), |
| 1484 | newTouchedWindowHandle->getName().string()); |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1485 | #endif |
| 1486 | // Make a slippery exit from the old window. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1487 | mTempTouchState.addOrUpdateWindow(oldTouchedWindowHandle, |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1488 | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT, BitSet32(0)); |
| 1489 | |
| 1490 | // Make a slippery entrance into the new window. |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1491 | if (newTouchedWindowHandle->getInfo()->supportsSplitTouch()) { |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1492 | isSplit = true; |
| 1493 | } |
| 1494 | |
| 1495 | int32_t targetFlags = InputTarget::FLAG_FOREGROUND |
| 1496 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER; |
| 1497 | if (isSplit) { |
| 1498 | targetFlags |= InputTarget::FLAG_SPLIT; |
| 1499 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1500 | if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) { |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1501 | targetFlags |= InputTarget::FLAG_WINDOW_IS_OBSCURED; |
| 1502 | } |
| 1503 | |
| 1504 | BitSet32 pointerIds; |
| 1505 | if (isSplit) { |
| 1506 | pointerIds.markBit(entry->pointerProperties[0].id); |
| 1507 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1508 | mTempTouchState.addOrUpdateWindow(newTouchedWindowHandle, targetFlags, pointerIds); |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 1509 | |
| 1510 | // Split the batch here so we send exactly one sample. |
| 1511 | *outSplitBatchAfterSample = &entry->firstSample; |
| 1512 | } |
| 1513 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1514 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1515 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1516 | if (newHoverWindowHandle != mLastHoverWindowHandle) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1517 | // Split the batch here so we send exactly one sample as part of ENTER or EXIT. |
| 1518 | *outSplitBatchAfterSample = &entry->firstSample; |
| 1519 | |
| 1520 | // Let the previous window know that the hover sequence is over. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1521 | if (mLastHoverWindowHandle != NULL) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1522 | #if DEBUG_HOVER |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1523 | LOGD("Sending hover exit event to window %s.", |
| 1524 | mLastHoverWindowHandle->getName().string()); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1525 | #endif |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1526 | mTempTouchState.addOrUpdateWindow(mLastHoverWindowHandle, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1527 | InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT, BitSet32(0)); |
| 1528 | } |
| 1529 | |
| 1530 | // Let the new window know that the hover sequence is starting. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1531 | if (newHoverWindowHandle != NULL) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1532 | #if DEBUG_HOVER |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1533 | LOGD("Sending hover enter event to window %s.", |
| 1534 | newHoverWindowHandle->getName().string()); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1535 | #endif |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1536 | mTempTouchState.addOrUpdateWindow(newHoverWindowHandle, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1537 | InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER, BitSet32(0)); |
| 1538 | } |
| 1539 | } |
| 1540 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1541 | // Check permission to inject into all touched foreground windows and ensure there |
| 1542 | // is at least one touched foreground window. |
| 1543 | { |
| 1544 | bool haveForegroundWindow = false; |
| 1545 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1546 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1547 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1548 | haveForegroundWindow = true; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1549 | if (! checkInjectionPermission(touchedWindow.windowHandle, |
| 1550 | entry->injectionState)) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1551 | injectionResult = INPUT_EVENT_INJECTION_PERMISSION_DENIED; |
| 1552 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1553 | goto Failed; |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | if (! haveForegroundWindow) { |
Jeff Brown | a2cc28d | 2011-03-25 11:58:46 -0700 | [diff] [blame] | 1558 | #if DEBUG_FOCUS |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1559 | LOGD("Dropping event because there is no touched foreground window to receive it."); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1560 | #endif |
| 1561 | injectionResult = INPUT_EVENT_INJECTION_FAILED; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1562 | goto Failed; |
| 1563 | } |
| 1564 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1565 | // Permission granted to injection into all touched foreground windows. |
| 1566 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1567 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1568 | |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 1569 | // Check whether windows listening for outside touches are owned by the same UID. If it is |
| 1570 | // set the policy flag that we will not reveal coordinate information to this window. |
| 1571 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1572 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1573 | mTempTouchState.getFirstForegroundWindowHandle(); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1574 | const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 1575 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1576 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1577 | if (touchedWindow.targetFlags & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1578 | sp<InputWindowHandle> inputWindowHandle = touchedWindow.windowHandle; |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1579 | if (inputWindowHandle->getInfo()->ownerUid != foregroundWindowUid) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1580 | mTempTouchState.addOrUpdateWindow(inputWindowHandle, |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 1581 | InputTarget::FLAG_ZERO_COORDS, BitSet32(0)); |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1587 | // Ensure all touched foreground windows are ready for new input. |
| 1588 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1589 | const TouchedWindow& touchedWindow = mTempTouchState.windows[i]; |
| 1590 | if (touchedWindow.targetFlags & InputTarget::FLAG_FOREGROUND) { |
| 1591 | // If the touched window is paused then keep waiting. |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1592 | if (touchedWindow.windowHandle->getInfo()->paused) { |
Jeff Brown | a2cc28d | 2011-03-25 11:58:46 -0700 | [diff] [blame] | 1593 | #if DEBUG_FOCUS |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1594 | LOGD("Waiting because touched window is paused."); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1595 | #endif |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1596 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1597 | NULL, touchedWindow.windowHandle, nextWakeupTime); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1598 | goto Unresponsive; |
| 1599 | } |
| 1600 | |
| 1601 | // If the touched window is still working on previous events then keep waiting. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1602 | if (! isWindowFinishedWithPreviousInputLocked(touchedWindow.windowHandle)) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1603 | #if DEBUG_FOCUS |
| 1604 | LOGD("Waiting because touched window still processing previous input."); |
| 1605 | #endif |
| 1606 | injectionResult = handleTargetsNotReadyLocked(currentTime, entry, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1607 | NULL, touchedWindow.windowHandle, nextWakeupTime); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1608 | goto Unresponsive; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | // If this is the first pointer going down and the touched window has a wallpaper |
| 1614 | // then also add the touched wallpaper windows so they are locked in for the duration |
| 1615 | // of the touch gesture. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1616 | // We do not collect wallpapers during HOVER_MOVE or SCROLL because the wallpaper |
| 1617 | // engine only supports touch events. We would need to add a mechanism similar |
| 1618 | // to View.onGenericMotionEvent to enable wallpapers to handle these events. |
| 1619 | if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1620 | sp<InputWindowHandle> foregroundWindowHandle = |
| 1621 | mTempTouchState.getFirstForegroundWindowHandle(); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1622 | if (foregroundWindowHandle->getInfo()->hasWallpaper) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1623 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
| 1624 | sp<InputWindowHandle> windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1625 | if (windowHandle->getInfo()->layoutParamsType |
| 1626 | == InputWindowInfo::TYPE_WALLPAPER) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1627 | mTempTouchState.addOrUpdateWindow(windowHandle, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1628 | InputTarget::FLAG_WINDOW_IS_OBSCURED |
| 1629 | | InputTarget::FLAG_DISPATCH_AS_IS, |
| 1630 | BitSet32(0)); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1636 | // Success! Output targets. |
| 1637 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1638 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1639 | for (size_t i = 0; i < mTempTouchState.windows.size(); i++) { |
| 1640 | const TouchedWindow& touchedWindow = mTempTouchState.windows.itemAt(i); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1641 | addWindowTargetLocked(touchedWindow.windowHandle, touchedWindow.targetFlags, |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1642 | touchedWindow.pointerIds); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1645 | // Drop the outside or hover touch windows since we will not care about them |
| 1646 | // in the next iteration. |
| 1647 | mTempTouchState.filterNonAsIsTouchWindows(); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1648 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1649 | Failed: |
| 1650 | // Check injection permission once and for all. |
| 1651 | if (injectionPermission == INJECTION_PERMISSION_UNKNOWN) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1652 | if (checkInjectionPermission(NULL, entry->injectionState)) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1653 | injectionPermission = INJECTION_PERMISSION_GRANTED; |
| 1654 | } else { |
| 1655 | injectionPermission = INJECTION_PERMISSION_DENIED; |
| 1656 | } |
| 1657 | } |
| 1658 | |
| 1659 | // Update final pieces of touch state if the injector had permission. |
| 1660 | if (injectionPermission == INJECTION_PERMISSION_GRANTED) { |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1661 | if (!wrongDevice) { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1662 | if (switchedDevice) { |
| 1663 | #if DEBUG_FOCUS |
| 1664 | LOGD("Conflicting pointer actions: Switched to a different device."); |
| 1665 | #endif |
| 1666 | *outConflictingPointerActions = true; |
| 1667 | } |
| 1668 | |
| 1669 | if (isHoverAction) { |
| 1670 | // Started hovering, therefore no longer down. |
| 1671 | if (mTouchState.down) { |
| 1672 | #if DEBUG_FOCUS |
| 1673 | LOGD("Conflicting pointer actions: Hover received while pointer was down."); |
| 1674 | #endif |
| 1675 | *outConflictingPointerActions = true; |
| 1676 | } |
| 1677 | mTouchState.reset(); |
| 1678 | if (maskedAction == AMOTION_EVENT_ACTION_HOVER_ENTER |
| 1679 | || maskedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
| 1680 | mTouchState.deviceId = entry->deviceId; |
| 1681 | mTouchState.source = entry->source; |
| 1682 | } |
| 1683 | } else if (maskedAction == AMOTION_EVENT_ACTION_UP |
| 1684 | || maskedAction == AMOTION_EVENT_ACTION_CANCEL) { |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1685 | // All pointers up or canceled. |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1686 | mTouchState.reset(); |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1687 | } else if (maskedAction == AMOTION_EVENT_ACTION_DOWN) { |
| 1688 | // First pointer went down. |
| 1689 | if (mTouchState.down) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1690 | #if DEBUG_FOCUS |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1691 | LOGD("Conflicting pointer actions: Down received while already down."); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1692 | #endif |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1693 | *outConflictingPointerActions = true; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1694 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1695 | mTouchState.copyFrom(mTempTouchState); |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1696 | } else if (maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 1697 | // One pointer went up. |
| 1698 | if (isSplit) { |
| 1699 | int32_t pointerIndex = getMotionEventActionPointerIndex(action); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 1700 | uint32_t pointerId = entry->pointerProperties[pointerIndex].id; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1701 | |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1702 | for (size_t i = 0; i < mTempTouchState.windows.size(); ) { |
| 1703 | TouchedWindow& touchedWindow = mTempTouchState.windows.editItemAt(i); |
| 1704 | if (touchedWindow.targetFlags & InputTarget::FLAG_SPLIT) { |
| 1705 | touchedWindow.pointerIds.clearBit(pointerId); |
| 1706 | if (touchedWindow.pointerIds.isEmpty()) { |
| 1707 | mTempTouchState.windows.removeAt(i); |
| 1708 | continue; |
| 1709 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1710 | } |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1711 | i += 1; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1712 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1713 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1714 | mTouchState.copyFrom(mTempTouchState); |
| 1715 | } else if (maskedAction == AMOTION_EVENT_ACTION_SCROLL) { |
| 1716 | // Discard temporary touch state since it was only valid for this action. |
| 1717 | } else { |
| 1718 | // Save changes to touch state as-is for all other actions. |
| 1719 | mTouchState.copyFrom(mTempTouchState); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1720 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1721 | |
| 1722 | // Update hover state. |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1723 | mLastHoverWindowHandle = newHoverWindowHandle; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 1724 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1725 | } else { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1726 | #if DEBUG_FOCUS |
| 1727 | LOGD("Not updating touch focus because injection was denied."); |
| 1728 | #endif |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | Unresponsive: |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 1732 | // Reset temporary touch state to ensure we release unnecessary references to input channels. |
| 1733 | mTempTouchState.reset(); |
| 1734 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1735 | nsecs_t timeSpentWaitingForApplication = getTimeSpentWaitingForApplicationLocked(currentTime); |
| 1736 | updateDispatchStatisticsLocked(currentTime, entry, |
| 1737 | injectionResult, timeSpentWaitingForApplication); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1738 | #if DEBUG_FOCUS |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1739 | LOGD("findTouchedWindow finished: injectionResult=%d, injectionPermission=%d, " |
| 1740 | "timeSpentWaitingForApplication=%0.1fms", |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1741 | injectionResult, injectionPermission, timeSpentWaitingForApplication / 1000000.0); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1742 | #endif |
| 1743 | return injectionResult; |
| 1744 | } |
| 1745 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1746 | void InputDispatcher::addWindowTargetLocked(const sp<InputWindowHandle>& windowHandle, |
| 1747 | int32_t targetFlags, BitSet32 pointerIds) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1748 | mCurrentInputTargets.push(); |
| 1749 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1750 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1751 | InputTarget& target = mCurrentInputTargets.editTop(); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1752 | target.inputChannel = windowInfo->inputChannel; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1753 | target.flags = targetFlags; |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1754 | target.xOffset = - windowInfo->frameLeft; |
| 1755 | target.yOffset = - windowInfo->frameTop; |
| 1756 | target.scaleFactor = windowInfo->scaleFactor; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1757 | target.pointerIds = pointerIds; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1758 | } |
| 1759 | |
| 1760 | void InputDispatcher::addMonitoringTargetsLocked() { |
| 1761 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
| 1762 | mCurrentInputTargets.push(); |
| 1763 | |
| 1764 | InputTarget& target = mCurrentInputTargets.editTop(); |
| 1765 | target.inputChannel = mMonitoringChannels[i]; |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1766 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1767 | target.xOffset = 0; |
| 1768 | target.yOffset = 0; |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1769 | target.pointerIds.clear(); |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1770 | target.scaleFactor = 1.0f; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1774 | bool InputDispatcher::checkInjectionPermission(const sp<InputWindowHandle>& windowHandle, |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1775 | const InjectionState* injectionState) { |
| 1776 | if (injectionState |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1777 | && (windowHandle == NULL |
| 1778 | || windowHandle->getInfo()->ownerUid != injectionState->injectorUid) |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1779 | && !hasInjectionPermission(injectionState->injectorPid, injectionState->injectorUid)) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1780 | if (windowHandle != NULL) { |
| 1781 | LOGW("Permission denied: injecting event from pid %d uid %d to window %s " |
| 1782 | "owned by uid %d", |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1783 | injectionState->injectorPid, injectionState->injectorUid, |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1784 | windowHandle->getName().string(), |
| 1785 | windowHandle->getInfo()->ownerUid); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1786 | } else { |
| 1787 | LOGW("Permission denied: injecting event from pid %d uid %d", |
| 1788 | injectionState->injectorPid, injectionState->injectorUid); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1789 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1790 | return false; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1791 | } |
| 1792 | return true; |
| 1793 | } |
| 1794 | |
Jeff Brown | 19dfc83 | 2010-10-05 12:26:23 -0700 | [diff] [blame] | 1795 | bool InputDispatcher::isWindowObscuredAtPointLocked( |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1796 | const sp<InputWindowHandle>& windowHandle, int32_t x, int32_t y) const { |
| 1797 | size_t numWindows = mWindowHandles.size(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1798 | for (size_t i = 0; i < numWindows; i++) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1799 | sp<InputWindowHandle> otherHandle = mWindowHandles.itemAt(i); |
| 1800 | if (otherHandle == windowHandle) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1801 | break; |
| 1802 | } |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1803 | |
| 1804 | const InputWindowInfo* otherInfo = otherHandle->getInfo(); |
| 1805 | if (otherInfo->visible && ! otherInfo->isTrustedOverlay() |
| 1806 | && otherInfo->frameContainsPoint(x, y)) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1807 | return true; |
| 1808 | } |
| 1809 | } |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1813 | bool InputDispatcher::isWindowFinishedWithPreviousInputLocked( |
| 1814 | const sp<InputWindowHandle>& windowHandle) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1815 | ssize_t connectionIndex = getConnectionIndexLocked(windowHandle->getInputChannel()); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1816 | if (connectionIndex >= 0) { |
| 1817 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 1818 | return connection->outboundQueue.isEmpty(); |
| 1819 | } else { |
| 1820 | return true; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1824 | String8 InputDispatcher::getApplicationWindowLabelLocked( |
| 1825 | const sp<InputApplicationHandle>& applicationHandle, |
| 1826 | const sp<InputWindowHandle>& windowHandle) { |
| 1827 | if (applicationHandle != NULL) { |
| 1828 | if (windowHandle != NULL) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1829 | String8 label(applicationHandle->getName()); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1830 | label.append(" - "); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1831 | label.append(windowHandle->getName()); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1832 | return label; |
| 1833 | } else { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1834 | return applicationHandle->getName(); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1835 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1836 | } else if (windowHandle != NULL) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 1837 | return windowHandle->getName(); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1838 | } else { |
| 1839 | return String8("<unknown application or window>"); |
| 1840 | } |
| 1841 | } |
| 1842 | |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 1843 | void InputDispatcher::pokeUserActivityLocked(const EventEntry* eventEntry) { |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1844 | int32_t eventType = POWER_MANAGER_OTHER_EVENT; |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 1845 | switch (eventEntry->type) { |
| 1846 | case EventEntry::TYPE_MOTION: { |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 1847 | const MotionEntry* motionEntry = static_cast<const MotionEntry*>(eventEntry); |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 1848 | if (motionEntry->action == AMOTION_EVENT_ACTION_CANCEL) { |
| 1849 | return; |
| 1850 | } |
| 1851 | |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1852 | if (MotionEvent::isTouchEvent(motionEntry->source, motionEntry->action)) { |
Joe Onorato | 1a542c7 | 2010-11-08 09:48:20 -0800 | [diff] [blame] | 1853 | eventType = POWER_MANAGER_TOUCH_EVENT; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1854 | } |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | case EventEntry::TYPE_KEY: { |
| 1858 | const KeyEntry* keyEntry = static_cast<const KeyEntry*>(eventEntry); |
| 1859 | if (keyEntry->flags & AKEY_EVENT_FLAG_CANCELED) { |
| 1860 | return; |
| 1861 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 1862 | eventType = POWER_MANAGER_BUTTON_EVENT; |
Jeff Brown | 4d39605 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 1863 | break; |
| 1864 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1865 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1866 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1867 | CommandEntry* commandEntry = postCommandLocked( |
| 1868 | & InputDispatcher::doPokeUserActivityLockedInterruptible); |
Jeff Brown | e2fe69e | 2010-10-18 13:21:23 -0700 | [diff] [blame] | 1869 | commandEntry->eventTime = eventEntry->eventTime; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1870 | commandEntry->userActivityEventType = eventType; |
| 1871 | } |
| 1872 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 1873 | void InputDispatcher::prepareDispatchCycleLocked(nsecs_t currentTime, |
| 1874 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1875 | bool resumeWithAppendedMotionSample) { |
| 1876 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9cc695c | 2011-08-23 18:35:04 -0700 | [diff] [blame] | 1877 | LOGD("channel '%s' ~ prepareDispatchCycle - flags=0x%08x, " |
| 1878 | "xOffset=%f, yOffset=%f, scaleFactor=%f, " |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 1879 | "pointerIds=0x%x, " |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1880 | "resumeWithAppendedMotionSample=%s", |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1881 | connection->getInputChannelName(), inputTarget->flags, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1882 | inputTarget->xOffset, inputTarget->yOffset, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1883 | inputTarget->scaleFactor, inputTarget->pointerIds.value, |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1884 | toString(resumeWithAppendedMotionSample)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1885 | #endif |
| 1886 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1887 | // Make sure we are never called for streaming when splitting across multiple windows. |
| 1888 | bool isSplit = inputTarget->flags & InputTarget::FLAG_SPLIT; |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1889 | LOG_ASSERT(! (resumeWithAppendedMotionSample && isSplit)); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1890 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1891 | // Skip this event if the connection status is not normal. |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 1892 | // We don't want to enqueue additional outbound events if the connection is broken. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1893 | if (connection->status != Connection::STATUS_NORMAL) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1894 | #if DEBUG_DISPATCH_CYCLE |
| 1895 | LOGD("channel '%s' ~ Dropping event because the channel status is %s", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 1896 | connection->getInputChannelName(), connection->getStatusLabel()); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 1897 | #endif |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1898 | return; |
| 1899 | } |
| 1900 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1901 | // Split a motion event if needed. |
| 1902 | if (isSplit) { |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 1903 | LOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1904 | |
| 1905 | MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry); |
| 1906 | if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) { |
| 1907 | MotionEntry* splitMotionEntry = splitMotionEvent( |
| 1908 | originalMotionEntry, inputTarget->pointerIds); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 1909 | if (!splitMotionEntry) { |
| 1910 | return; // split event was dropped |
| 1911 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1912 | #if DEBUG_FOCUS |
| 1913 | LOGD("channel '%s' ~ Split motion event.", |
| 1914 | connection->getInputChannelName()); |
| 1915 | logOutboundMotionDetailsLocked(" ", splitMotionEntry); |
| 1916 | #endif |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 1917 | enqueueDispatchEntriesLocked(currentTime, connection, |
| 1918 | splitMotionEntry, inputTarget, resumeWithAppendedMotionSample); |
| 1919 | splitMotionEntry->release(); |
| 1920 | return; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 1924 | // Not splitting. Enqueue dispatch entries for the event as is. |
| 1925 | enqueueDispatchEntriesLocked(currentTime, connection, eventEntry, inputTarget, |
| 1926 | resumeWithAppendedMotionSample); |
| 1927 | } |
| 1928 | |
| 1929 | void InputDispatcher::enqueueDispatchEntriesLocked(nsecs_t currentTime, |
| 1930 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
| 1931 | bool resumeWithAppendedMotionSample) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1932 | // Resume the dispatch cycle with a freshly appended motion sample. |
| 1933 | // First we check that the last dispatch entry in the outbound queue is for the same |
| 1934 | // motion event to which we appended the motion sample. If we find such a dispatch |
| 1935 | // entry, and if it is currently in progress then we try to stream the new sample. |
| 1936 | bool wasEmpty = connection->outboundQueue.isEmpty(); |
| 1937 | |
| 1938 | if (! wasEmpty && resumeWithAppendedMotionSample) { |
| 1939 | DispatchEntry* motionEventDispatchEntry = |
| 1940 | connection->findQueuedDispatchEntryForEvent(eventEntry); |
| 1941 | if (motionEventDispatchEntry) { |
| 1942 | // If the dispatch entry is not in progress, then we must be busy dispatching an |
| 1943 | // earlier event. Not a problem, the motion event is on the outbound queue and will |
| 1944 | // be dispatched later. |
| 1945 | if (! motionEventDispatchEntry->inProgress) { |
| 1946 | #if DEBUG_BATCHING |
| 1947 | LOGD("channel '%s' ~ Not streaming because the motion event has " |
| 1948 | "not yet been dispatched. " |
| 1949 | "(Waiting for earlier events to be consumed.)", |
| 1950 | connection->getInputChannelName()); |
| 1951 | #endif |
| 1952 | return; |
| 1953 | } |
| 1954 | |
| 1955 | // If the dispatch entry is in progress but it already has a tail of pending |
| 1956 | // motion samples, then it must mean that the shared memory buffer filled up. |
| 1957 | // Not a problem, when this dispatch cycle is finished, we will eventually start |
| 1958 | // a new dispatch cycle to process the tail and that tail includes the newly |
| 1959 | // appended motion sample. |
| 1960 | if (motionEventDispatchEntry->tailMotionSample) { |
| 1961 | #if DEBUG_BATCHING |
| 1962 | LOGD("channel '%s' ~ Not streaming because no new samples can " |
| 1963 | "be appended to the motion event in this dispatch cycle. " |
| 1964 | "(Waiting for next dispatch cycle to start.)", |
| 1965 | connection->getInputChannelName()); |
| 1966 | #endif |
| 1967 | return; |
| 1968 | } |
| 1969 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 1970 | // If the motion event was modified in flight, then we cannot stream the sample. |
| 1971 | if ((motionEventDispatchEntry->targetFlags & InputTarget::FLAG_DISPATCH_MASK) |
| 1972 | != InputTarget::FLAG_DISPATCH_AS_IS) { |
| 1973 | #if DEBUG_BATCHING |
| 1974 | LOGD("channel '%s' ~ Not streaming because the motion event was not " |
| 1975 | "being dispatched as-is. " |
| 1976 | "(Waiting for next dispatch cycle to start.)", |
| 1977 | connection->getInputChannelName()); |
| 1978 | #endif |
| 1979 | return; |
| 1980 | } |
| 1981 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1982 | // The dispatch entry is in progress and is still potentially open for streaming. |
| 1983 | // Try to stream the new motion sample. This might fail if the consumer has already |
| 1984 | // consumed the motion event (or if the channel is broken). |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 1985 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| 1986 | MotionSample* appendedMotionSample = motionEntry->lastSample; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1987 | status_t status; |
| 1988 | if (motionEventDispatchEntry->scaleFactor == 1.0f) { |
| 1989 | status = connection->inputPublisher.appendMotionSample( |
| 1990 | appendedMotionSample->eventTime, appendedMotionSample->pointerCoords); |
| 1991 | } else { |
| 1992 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 1993 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
| 1994 | scaledCoords[i] = appendedMotionSample->pointerCoords[i]; |
| 1995 | scaledCoords[i].scale(motionEventDispatchEntry->scaleFactor); |
| 1996 | } |
| 1997 | status = connection->inputPublisher.appendMotionSample( |
| 1998 | appendedMotionSample->eventTime, scaledCoords); |
| 1999 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2000 | if (status == OK) { |
| 2001 | #if DEBUG_BATCHING |
| 2002 | LOGD("channel '%s' ~ Successfully streamed new motion sample.", |
| 2003 | connection->getInputChannelName()); |
| 2004 | #endif |
| 2005 | return; |
| 2006 | } |
| 2007 | |
| 2008 | #if DEBUG_BATCHING |
| 2009 | if (status == NO_MEMORY) { |
| 2010 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
| 2011 | "dispatched move event because the shared memory buffer is full. " |
| 2012 | "(Waiting for next dispatch cycle to start.)", |
| 2013 | connection->getInputChannelName()); |
| 2014 | } else if (status == status_t(FAILED_TRANSACTION)) { |
| 2015 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 2016 | "dispatched move event because the event has already been consumed. " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2017 | "(Waiting for next dispatch cycle to start.)", |
| 2018 | connection->getInputChannelName()); |
| 2019 | } else { |
| 2020 | LOGD("channel '%s' ~ Could not append motion sample to currently " |
| 2021 | "dispatched move event due to an error, status=%d. " |
| 2022 | "(Waiting for next dispatch cycle to start.)", |
| 2023 | connection->getInputChannelName(), status); |
| 2024 | } |
| 2025 | #endif |
| 2026 | // Failed to stream. Start a new tail of pending motion samples to dispatch |
| 2027 | // in the next cycle. |
| 2028 | motionEventDispatchEntry->tailMotionSample = appendedMotionSample; |
| 2029 | return; |
| 2030 | } |
| 2031 | } |
| 2032 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2033 | // Enqueue dispatch entries for the requested modes. |
| 2034 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2035 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT); |
| 2036 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2037 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_OUTSIDE); |
| 2038 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2039 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER); |
| 2040 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2041 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 2042 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2043 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT); |
| 2044 | enqueueDispatchEntryLocked(connection, eventEntry, inputTarget, |
| 2045 | resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2046 | |
| 2047 | // If the outbound queue was previously empty, start the dispatch cycle going. |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2048 | if (wasEmpty && !connection->outboundQueue.isEmpty()) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2049 | activateConnectionLocked(connection.get()); |
| 2050 | startDispatchCycleLocked(currentTime, connection); |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | void InputDispatcher::enqueueDispatchEntryLocked( |
| 2055 | const sp<Connection>& connection, EventEntry* eventEntry, const InputTarget* inputTarget, |
| 2056 | bool resumeWithAppendedMotionSample, int32_t dispatchMode) { |
| 2057 | int32_t inputTargetFlags = inputTarget->flags; |
| 2058 | if (!(inputTargetFlags & dispatchMode)) { |
| 2059 | return; |
| 2060 | } |
| 2061 | inputTargetFlags = (inputTargetFlags & ~InputTarget::FLAG_DISPATCH_MASK) | dispatchMode; |
| 2062 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2063 | // This is a new event. |
| 2064 | // Enqueue a new dispatch entry onto the outbound queue for this connection. |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2065 | DispatchEntry* dispatchEntry = new DispatchEntry(eventEntry, // increments ref |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 2066 | inputTargetFlags, inputTarget->xOffset, inputTarget->yOffset, |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2067 | inputTarget->scaleFactor); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 2068 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2069 | // Handle the case where we could not stream a new motion sample because the consumer has |
| 2070 | // already consumed the motion event (otherwise the corresponding dispatch entry would |
| 2071 | // still be in the outbound queue for this connection). We set the head motion sample |
| 2072 | // to the list starting with the newly appended motion sample. |
| 2073 | if (resumeWithAppendedMotionSample) { |
| 2074 | #if DEBUG_BATCHING |
| 2075 | LOGD("channel '%s' ~ Preparing a new dispatch cycle for additional motion samples " |
| 2076 | "that cannot be streamed because the motion event has already been consumed.", |
| 2077 | connection->getInputChannelName()); |
| 2078 | #endif |
| 2079 | MotionSample* appendedMotionSample = static_cast<MotionEntry*>(eventEntry)->lastSample; |
| 2080 | dispatchEntry->headMotionSample = appendedMotionSample; |
| 2081 | } |
| 2082 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2083 | // Apply target flags and update the connection's input state. |
| 2084 | switch (eventEntry->type) { |
| 2085 | case EventEntry::TYPE_KEY: { |
| 2086 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
| 2087 | dispatchEntry->resolvedAction = keyEntry->action; |
| 2088 | dispatchEntry->resolvedFlags = keyEntry->flags; |
| 2089 | |
| 2090 | if (!connection->inputState.trackKey(keyEntry, |
| 2091 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| 2092 | #if DEBUG_DISPATCH_CYCLE |
| 2093 | LOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent key event", |
| 2094 | connection->getInputChannelName()); |
| 2095 | #endif |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 2096 | delete dispatchEntry; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2097 | return; // skip the inconsistent event |
| 2098 | } |
| 2099 | break; |
| 2100 | } |
| 2101 | |
| 2102 | case EventEntry::TYPE_MOTION: { |
| 2103 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
| 2104 | if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_OUTSIDE) { |
| 2105 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_OUTSIDE; |
| 2106 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_EXIT) { |
| 2107 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_EXIT; |
| 2108 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_HOVER_ENTER) { |
| 2109 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2110 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 2111 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_CANCEL; |
| 2112 | } else if (dispatchMode & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER) { |
| 2113 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_DOWN; |
| 2114 | } else { |
| 2115 | dispatchEntry->resolvedAction = motionEntry->action; |
| 2116 | } |
| 2117 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE |
| 2118 | && !connection->inputState.isHovering( |
| 2119 | motionEntry->deviceId, motionEntry->source)) { |
| 2120 | #if DEBUG_DISPATCH_CYCLE |
| 2121 | LOGD("channel '%s' ~ enqueueDispatchEntryLocked: filling in missing hover enter event", |
| 2122 | connection->getInputChannelName()); |
| 2123 | #endif |
| 2124 | dispatchEntry->resolvedAction = AMOTION_EVENT_ACTION_HOVER_ENTER; |
| 2125 | } |
| 2126 | |
| 2127 | dispatchEntry->resolvedFlags = motionEntry->flags; |
| 2128 | if (dispatchEntry->targetFlags & InputTarget::FLAG_WINDOW_IS_OBSCURED) { |
| 2129 | dispatchEntry->resolvedFlags |= AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED; |
| 2130 | } |
| 2131 | |
| 2132 | if (!connection->inputState.trackMotion(motionEntry, |
| 2133 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags)) { |
| 2134 | #if DEBUG_DISPATCH_CYCLE |
| 2135 | LOGD("channel '%s' ~ enqueueDispatchEntryLocked: skipping inconsistent motion event", |
| 2136 | connection->getInputChannelName()); |
| 2137 | #endif |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 2138 | delete dispatchEntry; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2139 | return; // skip the inconsistent event |
| 2140 | } |
| 2141 | break; |
| 2142 | } |
| 2143 | } |
| 2144 | |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 2145 | // Remember that we are waiting for this dispatch to complete. |
| 2146 | if (dispatchEntry->hasForegroundTarget()) { |
| 2147 | incrementPendingForegroundDispatchesLocked(eventEntry); |
| 2148 | } |
| 2149 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2150 | // Enqueue the dispatch entry. |
| 2151 | connection->outboundQueue.enqueueAtTail(dispatchEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 2154 | void InputDispatcher::startDispatchCycleLocked(nsecs_t currentTime, |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2155 | const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2156 | #if DEBUG_DISPATCH_CYCLE |
| 2157 | LOGD("channel '%s' ~ startDispatchCycle", |
| 2158 | connection->getInputChannelName()); |
| 2159 | #endif |
| 2160 | |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2161 | LOG_ASSERT(connection->status == Connection::STATUS_NORMAL); |
| 2162 | LOG_ASSERT(! connection->outboundQueue.isEmpty()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2163 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2164 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2165 | LOG_ASSERT(! dispatchEntry->inProgress); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2166 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2167 | // Mark the dispatch entry as in progress. |
| 2168 | dispatchEntry->inProgress = true; |
| 2169 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2170 | // Publish the event. |
| 2171 | status_t status; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2172 | EventEntry* eventEntry = dispatchEntry->eventEntry; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2173 | switch (eventEntry->type) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2174 | case EventEntry::TYPE_KEY: { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2175 | KeyEntry* keyEntry = static_cast<KeyEntry*>(eventEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2176 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2177 | // Publish the key event. |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2178 | status = connection->inputPublisher.publishKeyEvent( |
| 2179 | keyEntry->deviceId, keyEntry->source, |
| 2180 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
| 2181 | keyEntry->keyCode, keyEntry->scanCode, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2182 | keyEntry->metaState, keyEntry->repeatCount, keyEntry->downTime, |
| 2183 | keyEntry->eventTime); |
| 2184 | |
| 2185 | if (status) { |
| 2186 | LOGE("channel '%s' ~ Could not publish key event, " |
| 2187 | "status=%d", connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2188 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2189 | return; |
| 2190 | } |
| 2191 | break; |
| 2192 | } |
| 2193 | |
| 2194 | case EventEntry::TYPE_MOTION: { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2195 | MotionEntry* motionEntry = static_cast<MotionEntry*>(eventEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2196 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2197 | // If headMotionSample is non-NULL, then it points to the first new sample that we |
| 2198 | // were unable to dispatch during the previous cycle so we resume dispatching from |
| 2199 | // that point in the list of motion samples. |
| 2200 | // Otherwise, we just start from the first sample of the motion event. |
| 2201 | MotionSample* firstMotionSample = dispatchEntry->headMotionSample; |
| 2202 | if (! firstMotionSample) { |
| 2203 | firstMotionSample = & motionEntry->firstSample; |
| 2204 | } |
| 2205 | |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2206 | PointerCoords scaledCoords[MAX_POINTERS]; |
| 2207 | const PointerCoords* usingCoords = firstMotionSample->pointerCoords; |
| 2208 | |
Jeff Brown | d361659 | 2010-07-16 17:21:06 -0700 | [diff] [blame] | 2209 | // Set the X and Y offset depending on the input source. |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2210 | float xOffset, yOffset, scaleFactor; |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 2211 | if (motionEntry->source & AINPUT_SOURCE_CLASS_POINTER |
| 2212 | && !(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2213 | scaleFactor = dispatchEntry->scaleFactor; |
| 2214 | xOffset = dispatchEntry->xOffset * scaleFactor; |
| 2215 | yOffset = dispatchEntry->yOffset * scaleFactor; |
| 2216 | if (scaleFactor != 1.0f) { |
| 2217 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2218 | scaledCoords[i] = firstMotionSample->pointerCoords[i]; |
| 2219 | scaledCoords[i].scale(scaleFactor); |
| 2220 | } |
| 2221 | usingCoords = scaledCoords; |
| 2222 | } |
Jeff Brown | d361659 | 2010-07-16 17:21:06 -0700 | [diff] [blame] | 2223 | } else { |
| 2224 | xOffset = 0.0f; |
| 2225 | yOffset = 0.0f; |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 2226 | scaleFactor = 1.0f; |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 2227 | |
| 2228 | // We don't want the dispatch target to know. |
| 2229 | if (dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS) { |
| 2230 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2231 | scaledCoords[i].clear(); |
| 2232 | } |
| 2233 | usingCoords = scaledCoords; |
| 2234 | } |
Jeff Brown | d361659 | 2010-07-16 17:21:06 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2237 | // Publish the motion event and the first motion sample. |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2238 | status = connection->inputPublisher.publishMotionEvent( |
| 2239 | motionEntry->deviceId, motionEntry->source, |
| 2240 | dispatchEntry->resolvedAction, dispatchEntry->resolvedFlags, |
| 2241 | motionEntry->edgeFlags, motionEntry->metaState, motionEntry->buttonState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2242 | xOffset, yOffset, |
| 2243 | motionEntry->xPrecision, motionEntry->yPrecision, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2244 | motionEntry->downTime, firstMotionSample->eventTime, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2245 | motionEntry->pointerCount, motionEntry->pointerProperties, |
| 2246 | usingCoords); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2247 | |
| 2248 | if (status) { |
| 2249 | LOGE("channel '%s' ~ Could not publish motion event, " |
| 2250 | "status=%d", connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2251 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2252 | return; |
| 2253 | } |
| 2254 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2255 | if (dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_MOVE |
| 2256 | || dispatchEntry->resolvedAction == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2257 | // Append additional motion samples. |
| 2258 | MotionSample* nextMotionSample = firstMotionSample->next; |
| 2259 | for (; nextMotionSample != NULL; nextMotionSample = nextMotionSample->next) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2260 | if (usingCoords == scaledCoords) { |
Kenny Root | 7a9db18 | 2011-06-02 15:16:05 -0700 | [diff] [blame] | 2261 | if (!(dispatchEntry->targetFlags & InputTarget::FLAG_ZERO_COORDS)) { |
| 2262 | for (size_t i = 0; i < motionEntry->pointerCount; i++) { |
| 2263 | scaledCoords[i] = nextMotionSample->pointerCoords[i]; |
| 2264 | scaledCoords[i].scale(scaleFactor); |
| 2265 | } |
Dianne Hackborn | 2ba3e80 | 2011-05-11 10:59:54 -0700 | [diff] [blame] | 2266 | } |
| 2267 | } else { |
| 2268 | usingCoords = nextMotionSample->pointerCoords; |
Dianne Hackborn | e7d25b7 | 2011-05-09 21:19:26 -0700 | [diff] [blame] | 2269 | } |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2270 | status = connection->inputPublisher.appendMotionSample( |
Dianne Hackborn | aa9d84c | 2011-05-09 19:00:59 -0700 | [diff] [blame] | 2271 | nextMotionSample->eventTime, usingCoords); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2272 | if (status == NO_MEMORY) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2273 | #if DEBUG_DISPATCH_CYCLE |
| 2274 | LOGD("channel '%s' ~ Shared memory buffer full. Some motion samples will " |
| 2275 | "be sent in the next dispatch cycle.", |
| 2276 | connection->getInputChannelName()); |
| 2277 | #endif |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2278 | break; |
| 2279 | } |
| 2280 | if (status != OK) { |
| 2281 | LOGE("channel '%s' ~ Could not append motion sample " |
| 2282 | "for a reason other than out of memory, status=%d", |
| 2283 | connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2284 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2285 | return; |
| 2286 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2287 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2288 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2289 | // Remember the next motion sample that we could not dispatch, in case we ran out |
| 2290 | // of space in the shared memory buffer. |
| 2291 | dispatchEntry->tailMotionSample = nextMotionSample; |
| 2292 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2293 | break; |
| 2294 | } |
| 2295 | |
| 2296 | default: { |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2297 | LOG_ASSERT(false); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | // Send the dispatch signal. |
| 2302 | status = connection->inputPublisher.sendDispatchSignal(); |
| 2303 | if (status) { |
| 2304 | LOGE("channel '%s' ~ Could not send dispatch signal, status=%d", |
| 2305 | connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2306 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2307 | return; |
| 2308 | } |
| 2309 | |
| 2310 | // Record information about the newly started dispatch cycle. |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2311 | connection->lastEventTime = eventEntry->eventTime; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2312 | connection->lastDispatchTime = currentTime; |
| 2313 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2314 | // Notify other system components. |
| 2315 | onDispatchCycleStartedLocked(currentTime, connection); |
| 2316 | } |
| 2317 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 2318 | void InputDispatcher::finishDispatchCycleLocked(nsecs_t currentTime, |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2319 | const sp<Connection>& connection, bool handled) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2320 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2321 | LOGD("channel '%s' ~ finishDispatchCycle - %01.1fms since event, " |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2322 | "%01.1fms since dispatch, handled=%s", |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2323 | connection->getInputChannelName(), |
| 2324 | connection->getEventLatencyMillis(currentTime), |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2325 | connection->getDispatchLatencyMillis(currentTime), |
| 2326 | toString(handled)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2327 | #endif |
| 2328 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2329 | if (connection->status == Connection::STATUS_BROKEN |
| 2330 | || connection->status == Connection::STATUS_ZOMBIE) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2331 | return; |
| 2332 | } |
| 2333 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2334 | // Reset the publisher since the event has been consumed. |
| 2335 | // We do this now so that the publisher can release some of its internal resources |
| 2336 | // while waiting for the next dispatch cycle to begin. |
| 2337 | status_t status = connection->inputPublisher.reset(); |
| 2338 | if (status) { |
| 2339 | LOGE("channel '%s' ~ Could not reset publisher, status=%d", |
| 2340 | connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2341 | abortBrokenDispatchCycleLocked(currentTime, connection, true /*notify*/); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2342 | return; |
| 2343 | } |
| 2344 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2345 | // Notify other system components and prepare to start the next dispatch cycle. |
| 2346 | onDispatchCycleFinishedLocked(currentTime, connection, handled); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | void InputDispatcher::startNextDispatchCycleLocked(nsecs_t currentTime, |
| 2350 | const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2351 | // Start the next dispatch cycle for this connection. |
| 2352 | while (! connection->outboundQueue.isEmpty()) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2353 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2354 | if (dispatchEntry->inProgress) { |
| 2355 | // Finish or resume current event in progress. |
| 2356 | if (dispatchEntry->tailMotionSample) { |
| 2357 | // We have a tail of undispatched motion samples. |
| 2358 | // Reuse the same DispatchEntry and start a new cycle. |
| 2359 | dispatchEntry->inProgress = false; |
| 2360 | dispatchEntry->headMotionSample = dispatchEntry->tailMotionSample; |
| 2361 | dispatchEntry->tailMotionSample = NULL; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2362 | startDispatchCycleLocked(currentTime, connection); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2363 | return; |
| 2364 | } |
| 2365 | // Finished. |
| 2366 | connection->outboundQueue.dequeueAtHead(); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2367 | if (dispatchEntry->hasForegroundTarget()) { |
| 2368 | decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 2369 | } |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2370 | delete dispatchEntry; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2371 | } else { |
| 2372 | // If the head is not in progress, then we must have already dequeued the in |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2373 | // progress event, which means we actually aborted it. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2374 | // So just start the next event for this connection. |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2375 | startDispatchCycleLocked(currentTime, connection); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2376 | return; |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | // Outbound queue is empty, deactivate the connection. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 2381 | deactivateConnectionLocked(connection.get()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2384 | void InputDispatcher::abortBrokenDispatchCycleLocked(nsecs_t currentTime, |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2385 | const sp<Connection>& connection, bool notify) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2386 | #if DEBUG_DISPATCH_CYCLE |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2387 | LOGD("channel '%s' ~ abortBrokenDispatchCycle - notify=%s", |
| 2388 | connection->getInputChannelName(), toString(notify)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2389 | #endif |
| 2390 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2391 | // Clear the outbound queue. |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2392 | drainOutboundQueueLocked(connection.get()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2393 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2394 | // The connection appears to be unrecoverably broken. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2395 | // Ignore already broken or zombie connections. |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2396 | if (connection->status == Connection::STATUS_NORMAL) { |
| 2397 | connection->status = Connection::STATUS_BROKEN; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2398 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2399 | if (notify) { |
| 2400 | // Notify other system components. |
| 2401 | onDispatchCycleBrokenLocked(currentTime, connection); |
| 2402 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2403 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2406 | void InputDispatcher::drainOutboundQueueLocked(Connection* connection) { |
| 2407 | while (! connection->outboundQueue.isEmpty()) { |
| 2408 | DispatchEntry* dispatchEntry = connection->outboundQueue.dequeueAtHead(); |
| 2409 | if (dispatchEntry->hasForegroundTarget()) { |
| 2410 | decrementPendingForegroundDispatchesLocked(dispatchEntry->eventEntry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2411 | } |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2412 | delete dispatchEntry; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2413 | } |
| 2414 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2415 | deactivateConnectionLocked(connection); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 2418 | int InputDispatcher::handleReceiveCallback(int receiveFd, int events, void* data) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2419 | InputDispatcher* d = static_cast<InputDispatcher*>(data); |
| 2420 | |
| 2421 | { // acquire lock |
| 2422 | AutoMutex _l(d->mLock); |
| 2423 | |
| 2424 | ssize_t connectionIndex = d->mConnectionsByReceiveFd.indexOfKey(receiveFd); |
| 2425 | if (connectionIndex < 0) { |
| 2426 | LOGE("Received spurious receive callback for unknown input channel. " |
| 2427 | "fd=%d, events=0x%x", receiveFd, events); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 2428 | return 0; // remove the callback |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2431 | bool notify; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2432 | sp<Connection> connection = d->mConnectionsByReceiveFd.valueAt(connectionIndex); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2433 | if (!(events & (ALOOPER_EVENT_ERROR | ALOOPER_EVENT_HANGUP))) { |
| 2434 | if (!(events & ALOOPER_EVENT_INPUT)) { |
| 2435 | LOGW("channel '%s' ~ Received spurious callback for unhandled poll event. " |
| 2436 | "events=0x%x", connection->getInputChannelName(), events); |
| 2437 | return 1; |
| 2438 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2439 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2440 | bool handled = false; |
| 2441 | status_t status = connection->inputPublisher.receiveFinishedSignal(&handled); |
| 2442 | if (!status) { |
| 2443 | nsecs_t currentTime = now(); |
| 2444 | d->finishDispatchCycleLocked(currentTime, connection, handled); |
| 2445 | d->runCommandsLockedInterruptible(); |
| 2446 | return 1; |
| 2447 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2448 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2449 | LOGE("channel '%s' ~ Failed to receive finished signal. status=%d", |
| 2450 | connection->getInputChannelName(), status); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2451 | notify = true; |
| 2452 | } else { |
| 2453 | // Monitor channels are never explicitly unregistered. |
| 2454 | // We do it automatically when the remote endpoint is closed so don't warn |
| 2455 | // about them. |
| 2456 | notify = !connection->monitor; |
| 2457 | if (notify) { |
| 2458 | LOGW("channel '%s' ~ Consumer closed input channel or an error occurred. " |
| 2459 | "events=0x%x", connection->getInputChannelName(), events); |
| 2460 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2461 | } |
| 2462 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2463 | // Unregister the channel. |
| 2464 | d->unregisterInputChannelLocked(connection->inputChannel, notify); |
| 2465 | return 0; // remove the callback |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2466 | } // release lock |
| 2467 | } |
| 2468 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2469 | void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2470 | const CancelationOptions& options) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2471 | for (size_t i = 0; i < mConnectionsByReceiveFd.size(); i++) { |
| 2472 | synthesizeCancelationEventsForConnectionLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2473 | mConnectionsByReceiveFd.valueAt(i), options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | void InputDispatcher::synthesizeCancelationEventsForInputChannelLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2478 | const sp<InputChannel>& channel, const CancelationOptions& options) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2479 | ssize_t index = getConnectionIndexLocked(channel); |
| 2480 | if (index >= 0) { |
| 2481 | synthesizeCancelationEventsForConnectionLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2482 | mConnectionsByReceiveFd.valueAt(index), options); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | void InputDispatcher::synthesizeCancelationEventsForConnectionLocked( |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2487 | const sp<Connection>& connection, const CancelationOptions& options) { |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 2488 | if (connection->status == Connection::STATUS_BROKEN) { |
| 2489 | return; |
| 2490 | } |
| 2491 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2492 | nsecs_t currentTime = now(); |
| 2493 | |
| 2494 | mTempCancelationEvents.clear(); |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2495 | connection->inputState.synthesizeCancelationEvents(currentTime, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2496 | mTempCancelationEvents, options); |
| 2497 | |
Jeff Brown | 9f63f12 | 2012-01-12 18:30:12 -0800 | [diff] [blame] | 2498 | if (!mTempCancelationEvents.isEmpty()) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2499 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 2500 | LOGD("channel '%s' ~ Synthesized %d cancelation events to bring channel back in sync " |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 2501 | "with reality: %s, mode=%d.", |
| 2502 | connection->getInputChannelName(), mTempCancelationEvents.size(), |
| 2503 | options.reason, options.mode); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2504 | #endif |
| 2505 | for (size_t i = 0; i < mTempCancelationEvents.size(); i++) { |
| 2506 | EventEntry* cancelationEventEntry = mTempCancelationEvents.itemAt(i); |
| 2507 | switch (cancelationEventEntry->type) { |
| 2508 | case EventEntry::TYPE_KEY: |
| 2509 | logOutboundKeyDetailsLocked("cancel - ", |
| 2510 | static_cast<KeyEntry*>(cancelationEventEntry)); |
| 2511 | break; |
| 2512 | case EventEntry::TYPE_MOTION: |
| 2513 | logOutboundMotionDetailsLocked("cancel - ", |
| 2514 | static_cast<MotionEntry*>(cancelationEventEntry)); |
| 2515 | break; |
| 2516 | } |
| 2517 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2518 | InputTarget target; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 2519 | sp<InputWindowHandle> windowHandle = getWindowHandleLocked(connection->inputChannel); |
| 2520 | if (windowHandle != NULL) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2521 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 2522 | target.xOffset = -windowInfo->frameLeft; |
| 2523 | target.yOffset = -windowInfo->frameTop; |
| 2524 | target.scaleFactor = windowInfo->scaleFactor; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2525 | } else { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2526 | target.xOffset = 0; |
| 2527 | target.yOffset = 0; |
| 2528 | target.scaleFactor = 1.0f; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2529 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2530 | target.inputChannel = connection->inputChannel; |
| 2531 | target.flags = InputTarget::FLAG_DISPATCH_AS_IS; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2532 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 2533 | enqueueDispatchEntryLocked(connection, cancelationEventEntry, // increments ref |
| 2534 | &target, false, InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2535 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2536 | cancelationEventEntry->release(); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2539 | if (!connection->outboundQueue.head->inProgress) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2540 | startDispatchCycleLocked(currentTime, connection); |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2545 | InputDispatcher::MotionEntry* |
| 2546 | InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) { |
Jeff Brown | b6110c2 | 2011-04-01 16:15:13 -0700 | [diff] [blame] | 2547 | LOG_ASSERT(pointerIds.value != 0); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2548 | |
| 2549 | uint32_t splitPointerIndexMap[MAX_POINTERS]; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2550 | PointerProperties splitPointerProperties[MAX_POINTERS]; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2551 | PointerCoords splitPointerCoords[MAX_POINTERS]; |
| 2552 | |
| 2553 | uint32_t originalPointerCount = originalMotionEntry->pointerCount; |
| 2554 | uint32_t splitPointerCount = 0; |
| 2555 | |
| 2556 | for (uint32_t originalPointerIndex = 0; originalPointerIndex < originalPointerCount; |
| 2557 | originalPointerIndex++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2558 | const PointerProperties& pointerProperties = |
| 2559 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
| 2560 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2561 | if (pointerIds.hasBit(pointerId)) { |
| 2562 | splitPointerIndexMap[splitPointerCount] = originalPointerIndex; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2563 | splitPointerProperties[splitPointerCount].copyFrom(pointerProperties); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2564 | splitPointerCoords[splitPointerCount].copyFrom( |
| 2565 | originalMotionEntry->firstSample.pointerCoords[originalPointerIndex]); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2566 | splitPointerCount += 1; |
| 2567 | } |
| 2568 | } |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 2569 | |
| 2570 | if (splitPointerCount != pointerIds.count()) { |
| 2571 | // This is bad. We are missing some of the pointers that we expected to deliver. |
| 2572 | // Most likely this indicates that we received an ACTION_MOVE events that has |
| 2573 | // different pointer ids than we expected based on the previous ACTION_DOWN |
| 2574 | // or ACTION_POINTER_DOWN events that caused us to decide to split the pointers |
| 2575 | // in this way. |
| 2576 | LOGW("Dropping split motion event because the pointer count is %d but " |
| 2577 | "we expected there to be %d pointers. This probably means we received " |
| 2578 | "a broken sequence of pointer ids from the input device.", |
| 2579 | splitPointerCount, pointerIds.count()); |
| 2580 | return NULL; |
| 2581 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2582 | |
| 2583 | int32_t action = originalMotionEntry->action; |
| 2584 | int32_t maskedAction = action & AMOTION_EVENT_ACTION_MASK; |
| 2585 | if (maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| 2586 | || maskedAction == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 2587 | int32_t originalPointerIndex = getMotionEventActionPointerIndex(action); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2588 | const PointerProperties& pointerProperties = |
| 2589 | originalMotionEntry->pointerProperties[originalPointerIndex]; |
| 2590 | uint32_t pointerId = uint32_t(pointerProperties.id); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2591 | if (pointerIds.hasBit(pointerId)) { |
| 2592 | if (pointerIds.count() == 1) { |
| 2593 | // The first/last pointer went down/up. |
| 2594 | action = maskedAction == AMOTION_EVENT_ACTION_POINTER_DOWN |
| 2595 | ? AMOTION_EVENT_ACTION_DOWN : AMOTION_EVENT_ACTION_UP; |
Jeff Brown | 9a01d05 | 2010-09-27 16:35:11 -0700 | [diff] [blame] | 2596 | } else { |
| 2597 | // A secondary pointer went down/up. |
| 2598 | uint32_t splitPointerIndex = 0; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2599 | while (pointerId != uint32_t(splitPointerProperties[splitPointerIndex].id)) { |
Jeff Brown | 9a01d05 | 2010-09-27 16:35:11 -0700 | [diff] [blame] | 2600 | splitPointerIndex += 1; |
| 2601 | } |
| 2602 | action = maskedAction | (splitPointerIndex |
| 2603 | << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2604 | } |
| 2605 | } else { |
| 2606 | // An unrelated pointer changed. |
| 2607 | action = AMOTION_EVENT_ACTION_MOVE; |
| 2608 | } |
| 2609 | } |
| 2610 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2611 | MotionEntry* splitMotionEntry = new MotionEntry( |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2612 | originalMotionEntry->eventTime, |
| 2613 | originalMotionEntry->deviceId, |
| 2614 | originalMotionEntry->source, |
| 2615 | originalMotionEntry->policyFlags, |
| 2616 | action, |
| 2617 | originalMotionEntry->flags, |
| 2618 | originalMotionEntry->metaState, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2619 | originalMotionEntry->buttonState, |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2620 | originalMotionEntry->edgeFlags, |
| 2621 | originalMotionEntry->xPrecision, |
| 2622 | originalMotionEntry->yPrecision, |
| 2623 | originalMotionEntry->downTime, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2624 | splitPointerCount, splitPointerProperties, splitPointerCoords); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2625 | |
| 2626 | for (MotionSample* originalMotionSample = originalMotionEntry->firstSample.next; |
| 2627 | originalMotionSample != NULL; originalMotionSample = originalMotionSample->next) { |
| 2628 | for (uint32_t splitPointerIndex = 0; splitPointerIndex < splitPointerCount; |
| 2629 | splitPointerIndex++) { |
| 2630 | uint32_t originalPointerIndex = splitPointerIndexMap[splitPointerIndex]; |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 2631 | splitPointerCoords[splitPointerIndex].copyFrom( |
| 2632 | originalMotionSample->pointerCoords[originalPointerIndex]); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2635 | splitMotionEntry->appendSample(originalMotionSample->eventTime, splitPointerCoords); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2638 | if (originalMotionEntry->injectionState) { |
| 2639 | splitMotionEntry->injectionState = originalMotionEntry->injectionState; |
| 2640 | splitMotionEntry->injectionState->refCount += 1; |
| 2641 | } |
| 2642 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2643 | return splitMotionEntry; |
| 2644 | } |
| 2645 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2646 | void InputDispatcher::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2647 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2648 | LOGD("notifyConfigurationChanged - eventTime=%lld", args->eventTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2649 | #endif |
| 2650 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2651 | bool needWake; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2652 | { // acquire lock |
| 2653 | AutoMutex _l(mLock); |
| 2654 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2655 | ConfigurationChangedEntry* newEntry = new ConfigurationChangedEntry(args->eventTime); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2656 | needWake = enqueueInboundEventLocked(newEntry); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2657 | } // release lock |
| 2658 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2659 | if (needWake) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 2660 | mLooper->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2664 | void InputDispatcher::notifyKey(const NotifyKeyArgs* args) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2665 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2666 | LOGD("notifyKey - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, action=0x%x, " |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2667 | "flags=0x%x, keyCode=0x%x, scanCode=0x%x, metaState=0x%x, downTime=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2668 | args->eventTime, args->deviceId, args->source, args->policyFlags, |
| 2669 | args->action, args->flags, args->keyCode, args->scanCode, |
| 2670 | args->metaState, args->downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2671 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2672 | if (!validateKeyEvent(args->action)) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2673 | return; |
| 2674 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2675 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2676 | uint32_t policyFlags = args->policyFlags; |
| 2677 | int32_t flags = args->flags; |
| 2678 | int32_t metaState = args->metaState; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2679 | if ((policyFlags & POLICY_FLAG_VIRTUAL) || (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY)) { |
| 2680 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 2681 | flags |= AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY; |
| 2682 | } |
Jeff Brown | 924c4d4 | 2011-03-07 16:40:47 -0800 | [diff] [blame] | 2683 | if (policyFlags & POLICY_FLAG_ALT) { |
| 2684 | metaState |= AMETA_ALT_ON | AMETA_ALT_LEFT_ON; |
| 2685 | } |
| 2686 | if (policyFlags & POLICY_FLAG_ALT_GR) { |
| 2687 | metaState |= AMETA_ALT_ON | AMETA_ALT_RIGHT_ON; |
| 2688 | } |
| 2689 | if (policyFlags & POLICY_FLAG_SHIFT) { |
| 2690 | metaState |= AMETA_SHIFT_ON | AMETA_SHIFT_LEFT_ON; |
| 2691 | } |
| 2692 | if (policyFlags & POLICY_FLAG_CAPS_LOCK) { |
| 2693 | metaState |= AMETA_CAPS_LOCK_ON; |
| 2694 | } |
| 2695 | if (policyFlags & POLICY_FLAG_FUNCTION) { |
| 2696 | metaState |= AMETA_FUNCTION_ON; |
| 2697 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2698 | |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 2699 | policyFlags |= POLICY_FLAG_TRUSTED; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2700 | |
| 2701 | KeyEvent event; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2702 | event.initialize(args->deviceId, args->source, args->action, |
| 2703 | flags, args->keyCode, args->scanCode, metaState, 0, |
| 2704 | args->downTime, args->eventTime); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 2705 | |
| 2706 | mPolicy->interceptKeyBeforeQueueing(&event, /*byref*/ policyFlags); |
| 2707 | |
| 2708 | if (policyFlags & POLICY_FLAG_WOKE_HERE) { |
| 2709 | flags |= AKEY_EVENT_FLAG_WOKE_HERE; |
| 2710 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2711 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2712 | bool needWake; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2713 | { // acquire lock |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2714 | mLock.lock(); |
| 2715 | |
| 2716 | if (mInputFilterEnabled) { |
| 2717 | mLock.unlock(); |
| 2718 | |
| 2719 | policyFlags |= POLICY_FLAG_FILTERED; |
| 2720 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 2721 | return; // event was consumed by the filter |
| 2722 | } |
| 2723 | |
| 2724 | mLock.lock(); |
| 2725 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2726 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 2727 | int32_t repeatCount = 0; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2728 | KeyEntry* newEntry = new KeyEntry(args->eventTime, |
| 2729 | args->deviceId, args->source, policyFlags, |
| 2730 | args->action, flags, args->keyCode, args->scanCode, |
| 2731 | metaState, repeatCount, args->downTime); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2732 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2733 | needWake = enqueueInboundEventLocked(newEntry); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2734 | mLock.unlock(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2735 | } // release lock |
| 2736 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2737 | if (needWake) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 2738 | mLooper->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2739 | } |
| 2740 | } |
| 2741 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2742 | void InputDispatcher::notifyMotion(const NotifyMotionArgs* args) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2743 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2744 | LOGD("notifyMotion - eventTime=%lld, deviceId=%d, source=0x%x, policyFlags=0x%x, " |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2745 | "action=0x%x, flags=0x%x, metaState=0x%x, buttonState=0x%x, edgeFlags=0x%x, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2746 | "xPrecision=%f, yPrecision=%f, downTime=%lld", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2747 | args->eventTime, args->deviceId, args->source, args->policyFlags, |
| 2748 | args->action, args->flags, args->metaState, args->buttonState, |
| 2749 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime); |
| 2750 | for (uint32_t i = 0; i < args->pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2751 | LOGD(" Pointer %d: id=%d, toolType=%d, " |
| 2752 | "x=%f, y=%f, pressure=%f, size=%f, " |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2753 | "touchMajor=%f, touchMinor=%f, toolMajor=%f, toolMinor=%f, " |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 2754 | "orientation=%f", |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2755 | i, args->pointerProperties[i].id, |
| 2756 | args->pointerProperties[i].toolType, |
| 2757 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2758 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y), |
| 2759 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), |
| 2760 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_SIZE), |
| 2761 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR), |
| 2762 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), |
| 2763 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), |
| 2764 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), |
| 2765 | args->pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2766 | } |
| 2767 | #endif |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2768 | if (!validateMotionEvent(args->action, args->pointerCount, args->pointerProperties)) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2769 | return; |
| 2770 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2771 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2772 | uint32_t policyFlags = args->policyFlags; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 2773 | policyFlags |= POLICY_FLAG_TRUSTED; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2774 | mPolicy->interceptMotionBeforeQueueing(args->eventTime, /*byref*/ policyFlags); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 2775 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2776 | bool needWake; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2777 | { // acquire lock |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2778 | mLock.lock(); |
| 2779 | |
| 2780 | if (mInputFilterEnabled) { |
| 2781 | mLock.unlock(); |
| 2782 | |
| 2783 | MotionEvent event; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2784 | event.initialize(args->deviceId, args->source, args->action, args->flags, |
| 2785 | args->edgeFlags, args->metaState, args->buttonState, 0, 0, |
| 2786 | args->xPrecision, args->yPrecision, |
| 2787 | args->downTime, args->eventTime, |
| 2788 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2789 | |
| 2790 | policyFlags |= POLICY_FLAG_FILTERED; |
| 2791 | if (!mPolicy->filterInputEvent(&event, policyFlags)) { |
| 2792 | return; // event was consumed by the filter |
| 2793 | } |
| 2794 | |
| 2795 | mLock.lock(); |
| 2796 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2797 | |
| 2798 | // Attempt batching and streaming of move events. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2799 | if (args->action == AMOTION_EVENT_ACTION_MOVE |
| 2800 | || args->action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2801 | // BATCHING CASE |
| 2802 | // |
| 2803 | // Try to append a move sample to the tail of the inbound queue for this device. |
| 2804 | // Give up if we encounter a non-move motion event for this device since that |
| 2805 | // means we cannot append any new samples until a new motion event has started. |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2806 | for (EventEntry* entry = mInboundQueue.tail; entry; entry = entry->prev) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2807 | if (entry->type != EventEntry::TYPE_MOTION) { |
| 2808 | // Keep looking for motion events. |
| 2809 | continue; |
| 2810 | } |
| 2811 | |
| 2812 | MotionEntry* motionEntry = static_cast<MotionEntry*>(entry); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2813 | if (motionEntry->deviceId != args->deviceId |
| 2814 | || motionEntry->source != args->source) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2815 | // Keep looking for this device and source. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2816 | continue; |
| 2817 | } |
| 2818 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2819 | if (!motionEntry->canAppendSamples(args->action, |
| 2820 | args->pointerCount, args->pointerProperties)) { |
Jeff Brown | efd3266 | 2011-03-08 15:13:06 -0800 | [diff] [blame] | 2821 | // Last motion event in the queue for this device and source is |
| 2822 | // not compatible for appending new samples. Stop here. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2823 | goto NoBatchingOrStreaming; |
| 2824 | } |
| 2825 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2826 | // Do the batching magic. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2827 | batchMotionLocked(motionEntry, args->eventTime, |
| 2828 | args->metaState, args->pointerCoords, |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2829 | "most recent motion event for this device and source in the inbound queue"); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2830 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2831 | return; // done! |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Jeff Brown | f6989da | 2011-04-06 17:19:48 -0700 | [diff] [blame] | 2834 | // BATCHING ONTO PENDING EVENT CASE |
| 2835 | // |
| 2836 | // Try to append a move sample to the currently pending event, if there is one. |
| 2837 | // We can do this as long as we are still waiting to find the targets for the |
| 2838 | // event. Once the targets are locked-in we can only do streaming. |
| 2839 | if (mPendingEvent |
| 2840 | && (!mPendingEvent->dispatchInProgress || !mCurrentInputTargetsValid) |
| 2841 | && mPendingEvent->type == EventEntry::TYPE_MOTION) { |
| 2842 | MotionEntry* motionEntry = static_cast<MotionEntry*>(mPendingEvent); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2843 | if (motionEntry->deviceId == args->deviceId |
| 2844 | && motionEntry->source == args->source) { |
| 2845 | if (!motionEntry->canAppendSamples(args->action, |
| 2846 | args->pointerCount, args->pointerProperties)) { |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2847 | // Pending motion event is for this device and source but it is |
| 2848 | // not compatible for appending new samples. Stop here. |
Jeff Brown | f6989da | 2011-04-06 17:19:48 -0700 | [diff] [blame] | 2849 | goto NoBatchingOrStreaming; |
| 2850 | } |
| 2851 | |
Jeff Brown | f6989da | 2011-04-06 17:19:48 -0700 | [diff] [blame] | 2852 | // Do the batching magic. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2853 | batchMotionLocked(motionEntry, args->eventTime, |
| 2854 | args->metaState, args->pointerCoords, |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2855 | "pending motion event"); |
Jeff Brown | f6989da | 2011-04-06 17:19:48 -0700 | [diff] [blame] | 2856 | mLock.unlock(); |
| 2857 | return; // done! |
| 2858 | } |
| 2859 | } |
| 2860 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2861 | // STREAMING CASE |
| 2862 | // |
| 2863 | // There is no pending motion event (of any kind) for this device in the inbound queue. |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2864 | // Search the outbound queue for the current foreground targets to find a dispatched |
| 2865 | // motion event that is still in progress. If found, then, appen the new sample to |
| 2866 | // that event and push it out to all current targets. The logic in |
| 2867 | // prepareDispatchCycleLocked takes care of the case where some targets may |
| 2868 | // already have consumed the motion event by starting a new dispatch cycle if needed. |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 2869 | if (mCurrentInputTargetsValid) { |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2870 | for (size_t i = 0; i < mCurrentInputTargets.size(); i++) { |
| 2871 | const InputTarget& inputTarget = mCurrentInputTargets[i]; |
| 2872 | if ((inputTarget.flags & InputTarget::FLAG_FOREGROUND) == 0) { |
| 2873 | // Skip non-foreground targets. We only want to stream if there is at |
| 2874 | // least one foreground target whose dispatch is still in progress. |
| 2875 | continue; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2876 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2877 | |
| 2878 | ssize_t connectionIndex = getConnectionIndexLocked(inputTarget.inputChannel); |
| 2879 | if (connectionIndex < 0) { |
| 2880 | // Connection must no longer be valid. |
| 2881 | continue; |
| 2882 | } |
| 2883 | |
| 2884 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 2885 | if (connection->outboundQueue.isEmpty()) { |
| 2886 | // This foreground target has an empty outbound queue. |
| 2887 | continue; |
| 2888 | } |
| 2889 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2890 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2891 | if (! dispatchEntry->inProgress |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 2892 | || dispatchEntry->eventEntry->type != EventEntry::TYPE_MOTION |
| 2893 | || dispatchEntry->isSplit()) { |
| 2894 | // No motion event is being dispatched, or it is being split across |
| 2895 | // windows in which case we cannot stream. |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2896 | continue; |
| 2897 | } |
| 2898 | |
| 2899 | MotionEntry* motionEntry = static_cast<MotionEntry*>( |
| 2900 | dispatchEntry->eventEntry); |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2901 | if (motionEntry->action != args->action |
| 2902 | || motionEntry->deviceId != args->deviceId |
| 2903 | || motionEntry->source != args->source |
| 2904 | || motionEntry->pointerCount != args->pointerCount |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2905 | || motionEntry->isInjected()) { |
| 2906 | // The motion event is not compatible with this move. |
| 2907 | continue; |
| 2908 | } |
| 2909 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2910 | if (args->action == AMOTION_EVENT_ACTION_HOVER_MOVE) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 2911 | if (mLastHoverWindowHandle == NULL) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2912 | #if DEBUG_BATCHING |
| 2913 | LOGD("Not streaming hover move because there is no " |
| 2914 | "last hovered window."); |
| 2915 | #endif |
| 2916 | goto NoBatchingOrStreaming; |
| 2917 | } |
| 2918 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 2919 | sp<InputWindowHandle> hoverWindowHandle = findTouchedWindowAtLocked( |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2920 | args->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), |
| 2921 | args->pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y)); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 2922 | if (mLastHoverWindowHandle != hoverWindowHandle) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2923 | #if DEBUG_BATCHING |
| 2924 | LOGD("Not streaming hover move because the last hovered window " |
| 2925 | "is '%s' but the currently hovered window is '%s'.", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2926 | mLastHoverWindowHandle->getName().string(), |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 2927 | hoverWindowHandle != NULL |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 2928 | ? hoverWindowHandle->getName().string() : "<null>"); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2929 | #endif |
| 2930 | goto NoBatchingOrStreaming; |
| 2931 | } |
| 2932 | } |
| 2933 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2934 | // Hurray! This foreground target is currently dispatching a move event |
| 2935 | // that we can stream onto. Append the motion sample and resume dispatch. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2936 | motionEntry->appendSample(args->eventTime, args->pointerCoords); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2937 | #if DEBUG_BATCHING |
| 2938 | LOGD("Appended motion sample onto batch for most recently dispatched " |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2939 | "motion event for this device and source in the outbound queues. " |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2940 | "Attempting to stream the motion sample."); |
| 2941 | #endif |
| 2942 | nsecs_t currentTime = now(); |
| 2943 | dispatchEventToCurrentInputTargetsLocked(currentTime, motionEntry, |
| 2944 | true /*resumeWithAppendedMotionSample*/); |
| 2945 | |
| 2946 | runCommandsLockedInterruptible(); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2947 | mLock.unlock(); |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 2948 | return; // done! |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | NoBatchingOrStreaming:; |
| 2953 | } |
| 2954 | |
| 2955 | // Just enqueue a new motion event. |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 2956 | MotionEntry* newEntry = new MotionEntry(args->eventTime, |
| 2957 | args->deviceId, args->source, policyFlags, |
| 2958 | args->action, args->flags, args->metaState, args->buttonState, |
| 2959 | args->edgeFlags, args->xPrecision, args->yPrecision, args->downTime, |
| 2960 | args->pointerCount, args->pointerProperties, args->pointerCoords); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2961 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2962 | needWake = enqueueInboundEventLocked(newEntry); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 2963 | mLock.unlock(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2964 | } // release lock |
| 2965 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 2966 | if (needWake) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 2967 | mLooper->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2968 | } |
| 2969 | } |
| 2970 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2971 | void InputDispatcher::batchMotionLocked(MotionEntry* entry, nsecs_t eventTime, |
| 2972 | int32_t metaState, const PointerCoords* pointerCoords, const char* eventDescription) { |
| 2973 | // Combine meta states. |
| 2974 | entry->metaState |= metaState; |
| 2975 | |
| 2976 | // Coalesce this sample if not enough time has elapsed since the last sample was |
| 2977 | // initially appended to the batch. |
| 2978 | MotionSample* lastSample = entry->lastSample; |
| 2979 | long interval = eventTime - lastSample->eventTimeBeforeCoalescing; |
| 2980 | if (interval <= MOTION_SAMPLE_COALESCE_INTERVAL) { |
| 2981 | uint32_t pointerCount = entry->pointerCount; |
| 2982 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 2983 | lastSample->pointerCoords[i].copyFrom(pointerCoords[i]); |
| 2984 | } |
| 2985 | lastSample->eventTime = eventTime; |
| 2986 | #if DEBUG_BATCHING |
| 2987 | LOGD("Coalesced motion into last sample of batch for %s, events were %0.3f ms apart", |
| 2988 | eventDescription, interval * 0.000001f); |
| 2989 | #endif |
| 2990 | return; |
| 2991 | } |
| 2992 | |
| 2993 | // Append the sample. |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 2994 | entry->appendSample(eventTime, pointerCoords); |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 2995 | #if DEBUG_BATCHING |
| 2996 | LOGD("Appended motion sample onto batch for %s, events were %0.3f ms apart", |
| 2997 | eventDescription, interval * 0.000001f); |
| 2998 | #endif |
| 2999 | } |
| 3000 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3001 | void InputDispatcher::notifySwitch(const NotifySwitchArgs* args) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3002 | #if DEBUG_INBOUND_EVENT_DETAILS |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3003 | LOGD("notifySwitch - eventTime=%lld, policyFlags=0x%x, switchCode=%d, switchValue=%d", |
| 3004 | args->eventTime, args->policyFlags, |
| 3005 | args->switchCode, args->switchValue); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3006 | #endif |
| 3007 | |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3008 | uint32_t policyFlags = args->policyFlags; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 3009 | policyFlags |= POLICY_FLAG_TRUSTED; |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 3010 | mPolicy->notifySwitch(args->eventTime, |
| 3011 | args->switchCode, args->switchValue, policyFlags); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 3014 | void InputDispatcher::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
| 3015 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3016 | LOGD("notifyDeviceReset - eventTime=%lld, deviceId=%d", |
| 3017 | args->eventTime, args->deviceId); |
| 3018 | #endif |
| 3019 | |
| 3020 | bool needWake; |
| 3021 | { // acquire lock |
| 3022 | AutoMutex _l(mLock); |
| 3023 | |
| 3024 | DeviceResetEntry* newEntry = new DeviceResetEntry(args->eventTime, args->deviceId); |
| 3025 | needWake = enqueueInboundEventLocked(newEntry); |
| 3026 | } // release lock |
| 3027 | |
| 3028 | if (needWake) { |
| 3029 | mLooper->wake(); |
| 3030 | } |
| 3031 | } |
| 3032 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3033 | int32_t InputDispatcher::injectInputEvent(const InputEvent* event, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3034 | int32_t injectorPid, int32_t injectorUid, int32_t syncMode, int32_t timeoutMillis, |
| 3035 | uint32_t policyFlags) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3036 | #if DEBUG_INBOUND_EVENT_DETAILS |
| 3037 | LOGD("injectInputEvent - eventType=%d, injectorPid=%d, injectorUid=%d, " |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3038 | "syncMode=%d, timeoutMillis=%d, policyFlags=0x%08x", |
| 3039 | event->getType(), injectorPid, injectorUid, syncMode, timeoutMillis, policyFlags); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3040 | #endif |
| 3041 | |
| 3042 | nsecs_t endTime = now() + milliseconds_to_nanoseconds(timeoutMillis); |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 3043 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3044 | policyFlags |= POLICY_FLAG_INJECTED; |
Jeff Brown | e20c9e0 | 2010-10-11 14:20:19 -0700 | [diff] [blame] | 3045 | if (hasInjectionPermission(injectorPid, injectorUid)) { |
| 3046 | policyFlags |= POLICY_FLAG_TRUSTED; |
| 3047 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3048 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3049 | EventEntry* injectedEntry; |
| 3050 | switch (event->getType()) { |
| 3051 | case AINPUT_EVENT_TYPE_KEY: { |
| 3052 | const KeyEvent* keyEvent = static_cast<const KeyEvent*>(event); |
| 3053 | int32_t action = keyEvent->getAction(); |
| 3054 | if (! validateKeyEvent(action)) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3055 | return INPUT_EVENT_INJECTION_FAILED; |
| 3056 | } |
| 3057 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3058 | int32_t flags = keyEvent->getFlags(); |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 3059 | if (flags & AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY) { |
| 3060 | policyFlags |= POLICY_FLAG_VIRTUAL; |
| 3061 | } |
| 3062 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3063 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3064 | mPolicy->interceptKeyBeforeQueueing(keyEvent, /*byref*/ policyFlags); |
| 3065 | } |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 3066 | |
| 3067 | if (policyFlags & POLICY_FLAG_WOKE_HERE) { |
| 3068 | flags |= AKEY_EVENT_FLAG_WOKE_HERE; |
| 3069 | } |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3070 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3071 | mLock.lock(); |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3072 | injectedEntry = new KeyEntry(keyEvent->getEventTime(), |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 3073 | keyEvent->getDeviceId(), keyEvent->getSource(), |
| 3074 | policyFlags, action, flags, |
| 3075 | keyEvent->getKeyCode(), keyEvent->getScanCode(), keyEvent->getMetaState(), |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3076 | keyEvent->getRepeatCount(), keyEvent->getDownTime()); |
| 3077 | break; |
| 3078 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3079 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3080 | case AINPUT_EVENT_TYPE_MOTION: { |
| 3081 | const MotionEvent* motionEvent = static_cast<const MotionEvent*>(event); |
| 3082 | int32_t action = motionEvent->getAction(); |
| 3083 | size_t pointerCount = motionEvent->getPointerCount(); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3084 | const PointerProperties* pointerProperties = motionEvent->getPointerProperties(); |
| 3085 | if (! validateMotionEvent(action, pointerCount, pointerProperties)) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3086 | return INPUT_EVENT_INJECTION_FAILED; |
| 3087 | } |
| 3088 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3089 | if (!(policyFlags & POLICY_FLAG_FILTERED)) { |
| 3090 | nsecs_t eventTime = motionEvent->getEventTime(); |
| 3091 | mPolicy->interceptMotionBeforeQueueing(eventTime, /*byref*/ policyFlags); |
| 3092 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3093 | |
| 3094 | mLock.lock(); |
| 3095 | const nsecs_t* sampleEventTimes = motionEvent->getSampleEventTimes(); |
| 3096 | const PointerCoords* samplePointerCoords = motionEvent->getSamplePointerCoords(); |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3097 | MotionEntry* motionEntry = new MotionEntry(*sampleEventTimes, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3098 | motionEvent->getDeviceId(), motionEvent->getSource(), policyFlags, |
| 3099 | action, motionEvent->getFlags(), |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3100 | motionEvent->getMetaState(), motionEvent->getButtonState(), |
| 3101 | motionEvent->getEdgeFlags(), |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3102 | motionEvent->getXPrecision(), motionEvent->getYPrecision(), |
| 3103 | motionEvent->getDownTime(), uint32_t(pointerCount), |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3104 | pointerProperties, samplePointerCoords); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3105 | for (size_t i = motionEvent->getHistorySize(); i > 0; i--) { |
| 3106 | sampleEventTimes += 1; |
| 3107 | samplePointerCoords += pointerCount; |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3108 | motionEntry->appendSample(*sampleEventTimes, samplePointerCoords); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3109 | } |
| 3110 | injectedEntry = motionEntry; |
| 3111 | break; |
| 3112 | } |
| 3113 | |
| 3114 | default: |
| 3115 | LOGW("Cannot inject event of type %d", event->getType()); |
| 3116 | return INPUT_EVENT_INJECTION_FAILED; |
| 3117 | } |
| 3118 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3119 | InjectionState* injectionState = new InjectionState(injectorPid, injectorUid); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3120 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3121 | injectionState->injectionIsAsync = true; |
| 3122 | } |
| 3123 | |
| 3124 | injectionState->refCount += 1; |
| 3125 | injectedEntry->injectionState = injectionState; |
| 3126 | |
| 3127 | bool needWake = enqueueInboundEventLocked(injectedEntry); |
| 3128 | mLock.unlock(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3129 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3130 | if (needWake) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3131 | mLooper->wake(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | int32_t injectionResult; |
| 3135 | { // acquire lock |
| 3136 | AutoMutex _l(mLock); |
| 3137 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3138 | if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) { |
| 3139 | injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED; |
| 3140 | } else { |
| 3141 | for (;;) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3142 | injectionResult = injectionState->injectionResult; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3143 | if (injectionResult != INPUT_EVENT_INJECTION_PENDING) { |
| 3144 | break; |
| 3145 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3146 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3147 | nsecs_t remainingTimeout = endTime - now(); |
| 3148 | if (remainingTimeout <= 0) { |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3149 | #if DEBUG_INJECTION |
| 3150 | LOGD("injectInputEvent - Timed out waiting for injection result " |
| 3151 | "to become available."); |
| 3152 | #endif |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3153 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3154 | break; |
| 3155 | } |
| 3156 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3157 | mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout); |
| 3158 | } |
| 3159 | |
| 3160 | if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED |
| 3161 | && syncMode == INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISHED) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3162 | while (injectionState->pendingForegroundDispatches != 0) { |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3163 | #if DEBUG_INJECTION |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3164 | LOGD("injectInputEvent - Waiting for %d pending foreground dispatches.", |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3165 | injectionState->pendingForegroundDispatches); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3166 | #endif |
| 3167 | nsecs_t remainingTimeout = endTime - now(); |
| 3168 | if (remainingTimeout <= 0) { |
| 3169 | #if DEBUG_INJECTION |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3170 | LOGD("injectInputEvent - Timed out waiting for pending foreground " |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3171 | "dispatches to finish."); |
| 3172 | #endif |
| 3173 | injectionResult = INPUT_EVENT_INJECTION_TIMED_OUT; |
| 3174 | break; |
| 3175 | } |
| 3176 | |
| 3177 | mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout); |
| 3178 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3179 | } |
| 3180 | } |
| 3181 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3182 | injectionState->release(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3183 | } // release lock |
| 3184 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3185 | #if DEBUG_INJECTION |
| 3186 | LOGD("injectInputEvent - Finished with result %d. " |
| 3187 | "injectorPid=%d, injectorUid=%d", |
| 3188 | injectionResult, injectorPid, injectorUid); |
| 3189 | #endif |
| 3190 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3191 | return injectionResult; |
| 3192 | } |
| 3193 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3194 | bool InputDispatcher::hasInjectionPermission(int32_t injectorPid, int32_t injectorUid) { |
| 3195 | return injectorUid == 0 |
| 3196 | || mPolicy->checkInjectEventsPermissionNonReentrant(injectorPid, injectorUid); |
| 3197 | } |
| 3198 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3199 | void InputDispatcher::setInjectionResultLocked(EventEntry* entry, int32_t injectionResult) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3200 | InjectionState* injectionState = entry->injectionState; |
| 3201 | if (injectionState) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3202 | #if DEBUG_INJECTION |
| 3203 | LOGD("Setting input event injection result to %d. " |
| 3204 | "injectorPid=%d, injectorUid=%d", |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3205 | injectionResult, injectionState->injectorPid, injectionState->injectorUid); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3206 | #endif |
| 3207 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3208 | if (injectionState->injectionIsAsync |
| 3209 | && !(entry->policyFlags & POLICY_FLAG_FILTERED)) { |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3210 | // Log the outcome since the injector did not wait for the injection result. |
| 3211 | switch (injectionResult) { |
| 3212 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 3213 | LOGV("Asynchronous input event injection succeeded."); |
| 3214 | break; |
| 3215 | case INPUT_EVENT_INJECTION_FAILED: |
| 3216 | LOGW("Asynchronous input event injection failed."); |
| 3217 | break; |
| 3218 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 3219 | LOGW("Asynchronous input event injection permission denied."); |
| 3220 | break; |
| 3221 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 3222 | LOGW("Asynchronous input event injection timed out."); |
| 3223 | break; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3227 | injectionState->injectionResult = injectionResult; |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3228 | mInjectionResultAvailableCondition.broadcast(); |
| 3229 | } |
| 3230 | } |
| 3231 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3232 | void InputDispatcher::incrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
| 3233 | InjectionState* injectionState = entry->injectionState; |
| 3234 | if (injectionState) { |
| 3235 | injectionState->pendingForegroundDispatches += 1; |
| 3236 | } |
| 3237 | } |
| 3238 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3239 | void InputDispatcher::decrementPendingForegroundDispatchesLocked(EventEntry* entry) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3240 | InjectionState* injectionState = entry->injectionState; |
| 3241 | if (injectionState) { |
| 3242 | injectionState->pendingForegroundDispatches -= 1; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 3243 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3244 | if (injectionState->pendingForegroundDispatches == 0) { |
| 3245 | mInjectionSyncFinishedCondition.broadcast(); |
| 3246 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3247 | } |
| 3248 | } |
| 3249 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3250 | sp<InputWindowHandle> InputDispatcher::getWindowHandleLocked( |
| 3251 | const sp<InputChannel>& inputChannel) const { |
| 3252 | size_t numWindows = mWindowHandles.size(); |
| 3253 | for (size_t i = 0; i < numWindows; i++) { |
| 3254 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3255 | if (windowHandle->getInputChannel() == inputChannel) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3256 | return windowHandle; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3257 | } |
| 3258 | } |
| 3259 | return NULL; |
| 3260 | } |
| 3261 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3262 | bool InputDispatcher::hasWindowHandleLocked( |
| 3263 | const sp<InputWindowHandle>& windowHandle) const { |
| 3264 | size_t numWindows = mWindowHandles.size(); |
| 3265 | for (size_t i = 0; i < numWindows; i++) { |
| 3266 | if (mWindowHandles.itemAt(i) == windowHandle) { |
| 3267 | return true; |
| 3268 | } |
| 3269 | } |
| 3270 | return false; |
| 3271 | } |
| 3272 | |
| 3273 | void InputDispatcher::setInputWindows(const Vector<sp<InputWindowHandle> >& inputWindowHandles) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3274 | #if DEBUG_FOCUS |
| 3275 | LOGD("setInputWindows"); |
| 3276 | #endif |
| 3277 | { // acquire lock |
| 3278 | AutoMutex _l(mLock); |
| 3279 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3280 | Vector<sp<InputWindowHandle> > oldWindowHandles = mWindowHandles; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3281 | mWindowHandles = inputWindowHandles; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3282 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3283 | sp<InputWindowHandle> newFocusedWindowHandle; |
| 3284 | bool foundHoveredWindow = false; |
| 3285 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
| 3286 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3287 | if (!windowHandle->updateInfo() || windowHandle->getInputChannel() == NULL) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3288 | mWindowHandles.removeAt(i--); |
| 3289 | continue; |
| 3290 | } |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3291 | if (windowHandle->getInfo()->hasFocus) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3292 | newFocusedWindowHandle = windowHandle; |
| 3293 | } |
| 3294 | if (windowHandle == mLastHoverWindowHandle) { |
| 3295 | foundHoveredWindow = true; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3296 | } |
| 3297 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3298 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3299 | if (!foundHoveredWindow) { |
| 3300 | mLastHoverWindowHandle = NULL; |
| 3301 | } |
| 3302 | |
| 3303 | if (mFocusedWindowHandle != newFocusedWindowHandle) { |
| 3304 | if (mFocusedWindowHandle != NULL) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3305 | #if DEBUG_FOCUS |
| 3306 | LOGD("Focus left window: %s", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3307 | mFocusedWindowHandle->getName().string()); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3308 | #endif |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3309 | sp<InputChannel> focusedInputChannel = mFocusedWindowHandle->getInputChannel(); |
| 3310 | if (focusedInputChannel != NULL) { |
Christopher Tate | d9be36c | 2011-08-16 16:09:33 -0700 | [diff] [blame] | 3311 | CancelationOptions options(CancelationOptions::CANCEL_NON_POINTER_EVENTS, |
| 3312 | "focus left window"); |
| 3313 | synthesizeCancelationEventsForInputChannelLocked( |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3314 | focusedInputChannel, options); |
Christopher Tate | d9be36c | 2011-08-16 16:09:33 -0700 | [diff] [blame] | 3315 | } |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3316 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3317 | if (newFocusedWindowHandle != NULL) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3318 | #if DEBUG_FOCUS |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3319 | LOGD("Focus entered window: %s", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3320 | newFocusedWindowHandle->getName().string()); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3321 | #endif |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3322 | } |
| 3323 | mFocusedWindowHandle = newFocusedWindowHandle; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3324 | } |
| 3325 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3326 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3327 | TouchedWindow& touchedWindow = mTouchState.windows.editItemAt(i); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3328 | if (!hasWindowHandleLocked(touchedWindow.windowHandle)) { |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3329 | #if DEBUG_FOCUS |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3330 | LOGD("Touched window was removed: %s", |
| 3331 | touchedWindow.windowHandle->getName().string()); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3332 | #endif |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3333 | sp<InputChannel> touchedInputChannel = |
| 3334 | touchedWindow.windowHandle->getInputChannel(); |
| 3335 | if (touchedInputChannel != NULL) { |
Christopher Tate | d9be36c | 2011-08-16 16:09:33 -0700 | [diff] [blame] | 3336 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
| 3337 | "touched window was removed"); |
| 3338 | synthesizeCancelationEventsForInputChannelLocked( |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3339 | touchedInputChannel, options); |
Christopher Tate | d9be36c | 2011-08-16 16:09:33 -0700 | [diff] [blame] | 3340 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3341 | mTouchState.windows.removeAt(i--); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 3342 | } |
| 3343 | } |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3344 | |
| 3345 | // Release information for windows that are no longer present. |
| 3346 | // This ensures that unused input channels are released promptly. |
| 3347 | // Otherwise, they might stick around until the window handle is destroyed |
| 3348 | // which might not happen until the next GC. |
| 3349 | for (size_t i = 0; i < oldWindowHandles.size(); i++) { |
| 3350 | const sp<InputWindowHandle>& oldWindowHandle = oldWindowHandles.itemAt(i); |
| 3351 | if (!hasWindowHandleLocked(oldWindowHandle)) { |
| 3352 | #if DEBUG_FOCUS |
| 3353 | LOGD("Window went away: %s", oldWindowHandle->getName().string()); |
| 3354 | #endif |
| 3355 | oldWindowHandle->releaseInfo(); |
| 3356 | } |
| 3357 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3358 | } // release lock |
| 3359 | |
| 3360 | // Wake up poll loop since it may need to make new input dispatching choices. |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3361 | mLooper->wake(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3364 | void InputDispatcher::setFocusedApplication( |
| 3365 | const sp<InputApplicationHandle>& inputApplicationHandle) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3366 | #if DEBUG_FOCUS |
| 3367 | LOGD("setFocusedApplication"); |
| 3368 | #endif |
| 3369 | { // acquire lock |
| 3370 | AutoMutex _l(mLock); |
| 3371 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3372 | if (inputApplicationHandle != NULL && inputApplicationHandle->updateInfo()) { |
Jeff Brown | 5ea29ab | 2011-07-27 11:50:51 -0700 | [diff] [blame] | 3373 | if (mFocusedApplicationHandle != inputApplicationHandle) { |
| 3374 | if (mFocusedApplicationHandle != NULL) { |
| 3375 | resetTargetsLocked(); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3376 | mFocusedApplicationHandle->releaseInfo(); |
Jeff Brown | 5ea29ab | 2011-07-27 11:50:51 -0700 | [diff] [blame] | 3377 | } |
| 3378 | mFocusedApplicationHandle = inputApplicationHandle; |
| 3379 | } |
| 3380 | } else if (mFocusedApplicationHandle != NULL) { |
| 3381 | resetTargetsLocked(); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3382 | mFocusedApplicationHandle->releaseInfo(); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3383 | mFocusedApplicationHandle.clear(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | #if DEBUG_FOCUS |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3387 | //logDispatchStateLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3388 | #endif |
| 3389 | } // release lock |
| 3390 | |
| 3391 | // Wake up poll loop since it may need to make new input dispatching choices. |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3392 | mLooper->wake(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3395 | void InputDispatcher::setInputDispatchMode(bool enabled, bool frozen) { |
| 3396 | #if DEBUG_FOCUS |
| 3397 | LOGD("setInputDispatchMode: enabled=%d, frozen=%d", enabled, frozen); |
| 3398 | #endif |
| 3399 | |
| 3400 | bool changed; |
| 3401 | { // acquire lock |
| 3402 | AutoMutex _l(mLock); |
| 3403 | |
| 3404 | if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) { |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 3405 | if (mDispatchFrozen && !frozen) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3406 | resetANRTimeoutsLocked(); |
| 3407 | } |
| 3408 | |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 3409 | if (mDispatchEnabled && !enabled) { |
| 3410 | resetAndDropEverythingLocked("dispatcher is being disabled"); |
| 3411 | } |
| 3412 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3413 | mDispatchEnabled = enabled; |
| 3414 | mDispatchFrozen = frozen; |
| 3415 | changed = true; |
| 3416 | } else { |
| 3417 | changed = false; |
| 3418 | } |
| 3419 | |
| 3420 | #if DEBUG_FOCUS |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3421 | //logDispatchStateLocked(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3422 | #endif |
| 3423 | } // release lock |
| 3424 | |
| 3425 | if (changed) { |
| 3426 | // Wake up poll loop since it may need to make new input dispatching choices. |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3427 | mLooper->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3428 | } |
| 3429 | } |
| 3430 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 3431 | void InputDispatcher::setInputFilterEnabled(bool enabled) { |
| 3432 | #if DEBUG_FOCUS |
| 3433 | LOGD("setInputFilterEnabled: enabled=%d", enabled); |
| 3434 | #endif |
| 3435 | |
| 3436 | { // acquire lock |
| 3437 | AutoMutex _l(mLock); |
| 3438 | |
| 3439 | if (mInputFilterEnabled == enabled) { |
| 3440 | return; |
| 3441 | } |
| 3442 | |
| 3443 | mInputFilterEnabled = enabled; |
| 3444 | resetAndDropEverythingLocked("input filter is being enabled or disabled"); |
| 3445 | } // release lock |
| 3446 | |
| 3447 | // Wake up poll loop since there might be work to do to drop everything. |
| 3448 | mLooper->wake(); |
| 3449 | } |
| 3450 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3451 | bool InputDispatcher::transferTouchFocus(const sp<InputChannel>& fromChannel, |
| 3452 | const sp<InputChannel>& toChannel) { |
| 3453 | #if DEBUG_FOCUS |
| 3454 | LOGD("transferTouchFocus: fromChannel=%s, toChannel=%s", |
| 3455 | fromChannel->getName().string(), toChannel->getName().string()); |
| 3456 | #endif |
| 3457 | { // acquire lock |
| 3458 | AutoMutex _l(mLock); |
| 3459 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3460 | sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromChannel); |
| 3461 | sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toChannel); |
| 3462 | if (fromWindowHandle == NULL || toWindowHandle == NULL) { |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3463 | #if DEBUG_FOCUS |
| 3464 | LOGD("Cannot transfer focus because from or to window not found."); |
| 3465 | #endif |
| 3466 | return false; |
| 3467 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3468 | if (fromWindowHandle == toWindowHandle) { |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3469 | #if DEBUG_FOCUS |
| 3470 | LOGD("Trivial transfer to same window."); |
| 3471 | #endif |
| 3472 | return true; |
| 3473 | } |
| 3474 | |
| 3475 | bool found = false; |
| 3476 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
| 3477 | const TouchedWindow& touchedWindow = mTouchState.windows[i]; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3478 | if (touchedWindow.windowHandle == fromWindowHandle) { |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3479 | int32_t oldTargetFlags = touchedWindow.targetFlags; |
| 3480 | BitSet32 pointerIds = touchedWindow.pointerIds; |
| 3481 | |
| 3482 | mTouchState.windows.removeAt(i); |
| 3483 | |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 3484 | int32_t newTargetFlags = oldTargetFlags |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 3485 | & (InputTarget::FLAG_FOREGROUND |
| 3486 | | InputTarget::FLAG_SPLIT | InputTarget::FLAG_DISPATCH_AS_IS); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3487 | mTouchState.addOrUpdateWindow(toWindowHandle, newTargetFlags, pointerIds); |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3488 | |
| 3489 | found = true; |
| 3490 | break; |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | if (! found) { |
| 3495 | #if DEBUG_FOCUS |
| 3496 | LOGD("Focus transfer failed because from window did not have focus."); |
| 3497 | #endif |
| 3498 | return false; |
| 3499 | } |
| 3500 | |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 3501 | ssize_t fromConnectionIndex = getConnectionIndexLocked(fromChannel); |
| 3502 | ssize_t toConnectionIndex = getConnectionIndexLocked(toChannel); |
| 3503 | if (fromConnectionIndex >= 0 && toConnectionIndex >= 0) { |
| 3504 | sp<Connection> fromConnection = mConnectionsByReceiveFd.valueAt(fromConnectionIndex); |
| 3505 | sp<Connection> toConnection = mConnectionsByReceiveFd.valueAt(toConnectionIndex); |
| 3506 | |
| 3507 | fromConnection->inputState.copyPointerStateTo(toConnection->inputState); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 3508 | CancelationOptions options(CancelationOptions::CANCEL_POINTER_EVENTS, |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 3509 | "transferring touch focus from this window to another window"); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 3510 | synthesizeCancelationEventsForConnectionLocked(fromConnection, options); |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 3513 | #if DEBUG_FOCUS |
| 3514 | logDispatchStateLocked(); |
| 3515 | #endif |
| 3516 | } // release lock |
| 3517 | |
| 3518 | // Wake up poll loop since it may need to make new input dispatching choices. |
| 3519 | mLooper->wake(); |
| 3520 | return true; |
| 3521 | } |
| 3522 | |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 3523 | void InputDispatcher::resetAndDropEverythingLocked(const char* reason) { |
| 3524 | #if DEBUG_FOCUS |
| 3525 | LOGD("Resetting and dropping all events (%s).", reason); |
| 3526 | #endif |
| 3527 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 3528 | CancelationOptions options(CancelationOptions::CANCEL_ALL_EVENTS, reason); |
| 3529 | synthesizeCancelationEventsForAllConnectionsLocked(options); |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 3530 | |
| 3531 | resetKeyRepeatLocked(); |
| 3532 | releasePendingEventLocked(); |
| 3533 | drainInboundQueueLocked(); |
| 3534 | resetTargetsLocked(); |
| 3535 | |
| 3536 | mTouchState.reset(); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3537 | mLastHoverWindowHandle.clear(); |
Jeff Brown | 120a459 | 2010-10-27 18:43:51 -0700 | [diff] [blame] | 3538 | } |
| 3539 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3540 | void InputDispatcher::logDispatchStateLocked() { |
| 3541 | String8 dump; |
| 3542 | dumpDispatchStateLocked(dump); |
Jeff Brown | 2a95c2a | 2010-09-16 12:31:46 -0700 | [diff] [blame] | 3543 | |
| 3544 | char* text = dump.lockBuffer(dump.size()); |
| 3545 | char* start = text; |
| 3546 | while (*start != '\0') { |
| 3547 | char* end = strchr(start, '\n'); |
| 3548 | if (*end == '\n') { |
| 3549 | *(end++) = '\0'; |
| 3550 | } |
| 3551 | LOGD("%s", start); |
| 3552 | start = end; |
| 3553 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | void InputDispatcher::dumpDispatchStateLocked(String8& dump) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3557 | dump.appendFormat(INDENT "DispatchEnabled: %d\n", mDispatchEnabled); |
| 3558 | dump.appendFormat(INDENT "DispatchFrozen: %d\n", mDispatchFrozen); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3559 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3560 | if (mFocusedApplicationHandle != NULL) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3561 | dump.appendFormat(INDENT "FocusedApplication: name='%s', dispatchingTimeout=%0.3fms\n", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3562 | mFocusedApplicationHandle->getName().string(), |
| 3563 | mFocusedApplicationHandle->getDispatchingTimeout( |
| 3564 | DEFAULT_INPUT_DISPATCHING_TIMEOUT) / 1000000.0); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3565 | } else { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3566 | dump.append(INDENT "FocusedApplication: <null>\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3567 | } |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3568 | dump.appendFormat(INDENT "FocusedWindow: name='%s'\n", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3569 | mFocusedWindowHandle != NULL ? mFocusedWindowHandle->getName().string() : "<null>"); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3570 | |
| 3571 | dump.appendFormat(INDENT "TouchDown: %s\n", toString(mTouchState.down)); |
| 3572 | dump.appendFormat(INDENT "TouchSplit: %s\n", toString(mTouchState.split)); |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 3573 | dump.appendFormat(INDENT "TouchDeviceId: %d\n", mTouchState.deviceId); |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 3574 | dump.appendFormat(INDENT "TouchSource: 0x%08x\n", mTouchState.source); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3575 | if (!mTouchState.windows.isEmpty()) { |
| 3576 | dump.append(INDENT "TouchedWindows:\n"); |
| 3577 | for (size_t i = 0; i < mTouchState.windows.size(); i++) { |
| 3578 | const TouchedWindow& touchedWindow = mTouchState.windows[i]; |
| 3579 | dump.appendFormat(INDENT2 "%d: name='%s', pointerIds=0x%0x, targetFlags=0x%x\n", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3580 | i, touchedWindow.windowHandle->getName().string(), |
| 3581 | touchedWindow.pointerIds.value, |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3582 | touchedWindow.targetFlags); |
| 3583 | } |
| 3584 | } else { |
| 3585 | dump.append(INDENT "TouchedWindows: <none>\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3588 | if (!mWindowHandles.isEmpty()) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3589 | dump.append(INDENT "Windows:\n"); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3590 | for (size_t i = 0; i < mWindowHandles.size(); i++) { |
| 3591 | const sp<InputWindowHandle>& windowHandle = mWindowHandles.itemAt(i); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3592 | const InputWindowInfo* windowInfo = windowHandle->getInfo(); |
| 3593 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3594 | dump.appendFormat(INDENT2 "%d: name='%s', paused=%s, hasFocus=%s, hasWallpaper=%s, " |
| 3595 | "visible=%s, canReceiveKeys=%s, flags=0x%08x, type=0x%08x, layer=%d, " |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 3596 | "frame=[%d,%d][%d,%d], scale=%f, " |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 3597 | "touchableRegion=", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3598 | i, windowInfo->name.string(), |
| 3599 | toString(windowInfo->paused), |
| 3600 | toString(windowInfo->hasFocus), |
| 3601 | toString(windowInfo->hasWallpaper), |
| 3602 | toString(windowInfo->visible), |
| 3603 | toString(windowInfo->canReceiveKeys), |
| 3604 | windowInfo->layoutParamsFlags, windowInfo->layoutParamsType, |
| 3605 | windowInfo->layer, |
| 3606 | windowInfo->frameLeft, windowInfo->frameTop, |
| 3607 | windowInfo->frameRight, windowInfo->frameBottom, |
| 3608 | windowInfo->scaleFactor); |
| 3609 | dumpRegion(dump, windowInfo->touchableRegion); |
| 3610 | dump.appendFormat(", inputFeatures=0x%08x", windowInfo->inputFeatures); |
Jeff Brown | fbf0977 | 2011-01-16 14:06:57 -0800 | [diff] [blame] | 3611 | dump.appendFormat(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%0.3fms\n", |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3612 | windowInfo->ownerPid, windowInfo->ownerUid, |
| 3613 | windowInfo->dispatchingTimeout / 1000000.0); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3614 | } |
| 3615 | } else { |
| 3616 | dump.append(INDENT "Windows: <none>\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3619 | if (!mMonitoringChannels.isEmpty()) { |
| 3620 | dump.append(INDENT "MonitoringChannels:\n"); |
| 3621 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
| 3622 | const sp<InputChannel>& channel = mMonitoringChannels[i]; |
| 3623 | dump.appendFormat(INDENT2 "%d: '%s'\n", i, channel->getName().string()); |
| 3624 | } |
| 3625 | } else { |
| 3626 | dump.append(INDENT "MonitoringChannels: <none>\n"); |
| 3627 | } |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3628 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3629 | dump.appendFormat(INDENT "InboundQueue: length=%u\n", mInboundQueue.count()); |
| 3630 | |
| 3631 | if (!mActiveConnections.isEmpty()) { |
| 3632 | dump.append(INDENT "ActiveConnections:\n"); |
| 3633 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 3634 | const Connection* connection = mActiveConnections[i]; |
Jeff Brown | 76860e3 | 2010-10-25 17:37:46 -0700 | [diff] [blame] | 3635 | dump.appendFormat(INDENT2 "%d: '%s', status=%s, outboundQueueLength=%u, " |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3636 | "inputState.isNeutral=%s\n", |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3637 | i, connection->getInputChannelName(), connection->getStatusLabel(), |
| 3638 | connection->outboundQueue.count(), |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 3639 | toString(connection->inputState.isNeutral())); |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3640 | } |
| 3641 | } else { |
| 3642 | dump.append(INDENT "ActiveConnections: <none>\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | if (isAppSwitchPendingLocked()) { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3646 | dump.appendFormat(INDENT "AppSwitch: pending, due in %01.1fms\n", |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3647 | (mAppSwitchDueTime - now()) / 1000000.0); |
| 3648 | } else { |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 3649 | dump.append(INDENT "AppSwitch: not pending\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3650 | } |
| 3651 | } |
| 3652 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 3653 | status_t InputDispatcher::registerInputChannel(const sp<InputChannel>& inputChannel, |
| 3654 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3655 | #if DEBUG_REGISTRATION |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3656 | LOGD("channel '%s' ~ registerInputChannel - monitor=%s", inputChannel->getName().string(), |
| 3657 | toString(monitor)); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3658 | #endif |
| 3659 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3660 | { // acquire lock |
| 3661 | AutoMutex _l(mLock); |
| 3662 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3663 | if (getConnectionIndexLocked(inputChannel) >= 0) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3664 | LOGW("Attempted to register already registered input channel '%s'", |
| 3665 | inputChannel->getName().string()); |
| 3666 | return BAD_VALUE; |
| 3667 | } |
| 3668 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3669 | sp<Connection> connection = new Connection(inputChannel, inputWindowHandle, monitor); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3670 | status_t status = connection->initialize(); |
| 3671 | if (status) { |
| 3672 | LOGE("Failed to initialize input publisher for input channel '%s', status=%d", |
| 3673 | inputChannel->getName().string(), status); |
| 3674 | return status; |
| 3675 | } |
| 3676 | |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 3677 | int32_t receiveFd = inputChannel->getReceivePipeFd(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3678 | mConnectionsByReceiveFd.add(receiveFd, connection); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3679 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3680 | if (monitor) { |
| 3681 | mMonitoringChannels.push(inputChannel); |
| 3682 | } |
| 3683 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3684 | mLooper->addFd(receiveFd, 0, ALOOPER_EVENT_INPUT, handleReceiveCallback, this); |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 3685 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3686 | runCommandsLockedInterruptible(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3687 | } // release lock |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3688 | return OK; |
| 3689 | } |
| 3690 | |
| 3691 | status_t InputDispatcher::unregisterInputChannel(const sp<InputChannel>& inputChannel) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3692 | #if DEBUG_REGISTRATION |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 3693 | LOGD("channel '%s' ~ unregisterInputChannel", inputChannel->getName().string()); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3694 | #endif |
| 3695 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3696 | { // acquire lock |
| 3697 | AutoMutex _l(mLock); |
| 3698 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3699 | status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/); |
| 3700 | if (status) { |
| 3701 | return status; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3702 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3703 | } // release lock |
| 3704 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3705 | // Wake the poll loop because removing the connection may have changed the current |
| 3706 | // synchronization state. |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 3707 | mLooper->wake(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3708 | return OK; |
| 3709 | } |
| 3710 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3711 | status_t InputDispatcher::unregisterInputChannelLocked(const sp<InputChannel>& inputChannel, |
| 3712 | bool notify) { |
| 3713 | ssize_t connectionIndex = getConnectionIndexLocked(inputChannel); |
| 3714 | if (connectionIndex < 0) { |
| 3715 | LOGW("Attempted to unregister already unregistered input channel '%s'", |
| 3716 | inputChannel->getName().string()); |
| 3717 | return BAD_VALUE; |
| 3718 | } |
| 3719 | |
| 3720 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 3721 | mConnectionsByReceiveFd.removeItemsAt(connectionIndex); |
| 3722 | |
| 3723 | if (connection->monitor) { |
| 3724 | removeMonitorChannelLocked(inputChannel); |
| 3725 | } |
| 3726 | |
| 3727 | mLooper->removeFd(inputChannel->getReceivePipeFd()); |
| 3728 | |
| 3729 | nsecs_t currentTime = now(); |
| 3730 | abortBrokenDispatchCycleLocked(currentTime, connection, notify); |
| 3731 | |
| 3732 | runCommandsLockedInterruptible(); |
| 3733 | |
| 3734 | connection->status = Connection::STATUS_ZOMBIE; |
| 3735 | return OK; |
| 3736 | } |
| 3737 | |
| 3738 | void InputDispatcher::removeMonitorChannelLocked(const sp<InputChannel>& inputChannel) { |
| 3739 | for (size_t i = 0; i < mMonitoringChannels.size(); i++) { |
| 3740 | if (mMonitoringChannels[i] == inputChannel) { |
| 3741 | mMonitoringChannels.removeAt(i); |
| 3742 | break; |
| 3743 | } |
| 3744 | } |
| 3745 | } |
| 3746 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3747 | ssize_t InputDispatcher::getConnectionIndexLocked(const sp<InputChannel>& inputChannel) { |
Jeff Brown | 2cbecea | 2010-08-17 15:59:26 -0700 | [diff] [blame] | 3748 | ssize_t connectionIndex = mConnectionsByReceiveFd.indexOfKey(inputChannel->getReceivePipeFd()); |
| 3749 | if (connectionIndex >= 0) { |
| 3750 | sp<Connection> connection = mConnectionsByReceiveFd.valueAt(connectionIndex); |
| 3751 | if (connection->inputChannel.get() == inputChannel.get()) { |
| 3752 | return connectionIndex; |
| 3753 | } |
| 3754 | } |
| 3755 | |
| 3756 | return -1; |
| 3757 | } |
| 3758 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3759 | void InputDispatcher::activateConnectionLocked(Connection* connection) { |
| 3760 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 3761 | if (mActiveConnections.itemAt(i) == connection) { |
| 3762 | return; |
| 3763 | } |
| 3764 | } |
| 3765 | mActiveConnections.add(connection); |
| 3766 | } |
| 3767 | |
| 3768 | void InputDispatcher::deactivateConnectionLocked(Connection* connection) { |
| 3769 | for (size_t i = 0; i < mActiveConnections.size(); i++) { |
| 3770 | if (mActiveConnections.itemAt(i) == connection) { |
| 3771 | mActiveConnections.removeAt(i); |
| 3772 | return; |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3777 | void InputDispatcher::onDispatchCycleStartedLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3778 | nsecs_t currentTime, const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3781 | void InputDispatcher::onDispatchCycleFinishedLocked( |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3782 | nsecs_t currentTime, const sp<Connection>& connection, bool handled) { |
| 3783 | CommandEntry* commandEntry = postCommandLocked( |
| 3784 | & InputDispatcher::doDispatchCycleFinishedLockedInterruptible); |
| 3785 | commandEntry->connection = connection; |
| 3786 | commandEntry->handled = handled; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3787 | } |
| 3788 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3789 | void InputDispatcher::onDispatchCycleBrokenLocked( |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3790 | nsecs_t currentTime, const sp<Connection>& connection) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3791 | LOGE("channel '%s' ~ Channel is unrecoverably broken and will be disposed!", |
| 3792 | connection->getInputChannelName()); |
| 3793 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3794 | CommandEntry* commandEntry = postCommandLocked( |
| 3795 | & InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3796 | commandEntry->connection = connection; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3799 | void InputDispatcher::onANRLocked( |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3800 | nsecs_t currentTime, const sp<InputApplicationHandle>& applicationHandle, |
| 3801 | const sp<InputWindowHandle>& windowHandle, |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3802 | nsecs_t eventTime, nsecs_t waitStartTime) { |
| 3803 | LOGI("Application is not responding: %s. " |
| 3804 | "%01.1fms since event, %01.1fms since wait started", |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3805 | getApplicationWindowLabelLocked(applicationHandle, windowHandle).string(), |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3806 | (currentTime - eventTime) / 1000000.0, |
| 3807 | (currentTime - waitStartTime) / 1000000.0); |
| 3808 | |
| 3809 | CommandEntry* commandEntry = postCommandLocked( |
| 3810 | & InputDispatcher::doNotifyANRLockedInterruptible); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3811 | commandEntry->inputApplicationHandle = applicationHandle; |
| 3812 | commandEntry->inputWindowHandle = windowHandle; |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3813 | } |
| 3814 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3815 | void InputDispatcher::doNotifyConfigurationChangedInterruptible( |
| 3816 | CommandEntry* commandEntry) { |
| 3817 | mLock.unlock(); |
| 3818 | |
| 3819 | mPolicy->notifyConfigurationChanged(commandEntry->eventTime); |
| 3820 | |
| 3821 | mLock.lock(); |
| 3822 | } |
| 3823 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3824 | void InputDispatcher::doNotifyInputChannelBrokenLockedInterruptible( |
| 3825 | CommandEntry* commandEntry) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3826 | sp<Connection> connection = commandEntry->connection; |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3827 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3828 | if (connection->status != Connection::STATUS_ZOMBIE) { |
| 3829 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3830 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 3831 | mPolicy->notifyInputChannelBroken(connection->inputWindowHandle); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3832 | |
| 3833 | mLock.lock(); |
| 3834 | } |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3835 | } |
| 3836 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3837 | void InputDispatcher::doNotifyANRLockedInterruptible( |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3838 | CommandEntry* commandEntry) { |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3839 | mLock.unlock(); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3840 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3841 | nsecs_t newTimeout = mPolicy->notifyANR( |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 3842 | commandEntry->inputApplicationHandle, commandEntry->inputWindowHandle); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3843 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 3844 | mLock.lock(); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 3845 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 3846 | resumeAfterTargetsNotReadyTimeoutLocked(newTimeout, |
| 3847 | commandEntry->inputWindowHandle != NULL |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 3848 | ? commandEntry->inputWindowHandle->getInputChannel() : NULL); |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3851 | void InputDispatcher::doInterceptKeyBeforeDispatchingLockedInterruptible( |
| 3852 | CommandEntry* commandEntry) { |
| 3853 | KeyEntry* entry = commandEntry->keyEntry; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 3854 | |
| 3855 | KeyEvent event; |
| 3856 | initializeKeyEvent(&event, entry); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3857 | |
| 3858 | mLock.unlock(); |
| 3859 | |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 3860 | nsecs_t delay = mPolicy->interceptKeyBeforeDispatching(commandEntry->inputWindowHandle, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 3861 | &event, entry->policyFlags); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3862 | |
| 3863 | mLock.lock(); |
| 3864 | |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 3865 | if (delay < 0) { |
| 3866 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_SKIP; |
| 3867 | } else if (!delay) { |
| 3868 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_CONTINUE; |
| 3869 | } else { |
| 3870 | entry->interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_TRY_AGAIN_LATER; |
| 3871 | entry->interceptKeyWakeupTime = now() + delay; |
| 3872 | } |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3873 | entry->release(); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 3874 | } |
| 3875 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3876 | void InputDispatcher::doDispatchCycleFinishedLockedInterruptible( |
| 3877 | CommandEntry* commandEntry) { |
| 3878 | sp<Connection> connection = commandEntry->connection; |
| 3879 | bool handled = commandEntry->handled; |
| 3880 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3881 | bool skipNext = false; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 3882 | if (!connection->outboundQueue.isEmpty()) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3883 | DispatchEntry* dispatchEntry = connection->outboundQueue.head; |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3884 | if (dispatchEntry->inProgress) { |
| 3885 | if (dispatchEntry->eventEntry->type == EventEntry::TYPE_KEY) { |
| 3886 | KeyEntry* keyEntry = static_cast<KeyEntry*>(dispatchEntry->eventEntry); |
| 3887 | skipNext = afterKeyEventLockedInterruptible(connection, |
| 3888 | dispatchEntry, keyEntry, handled); |
| 3889 | } else if (dispatchEntry->eventEntry->type == EventEntry::TYPE_MOTION) { |
| 3890 | MotionEntry* motionEntry = static_cast<MotionEntry*>(dispatchEntry->eventEntry); |
| 3891 | skipNext = afterMotionEventLockedInterruptible(connection, |
| 3892 | dispatchEntry, motionEntry, handled); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 3893 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3897 | if (!skipNext) { |
| 3898 | startNextDispatchCycleLocked(now(), connection); |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | bool InputDispatcher::afterKeyEventLockedInterruptible(const sp<Connection>& connection, |
| 3903 | DispatchEntry* dispatchEntry, KeyEntry* keyEntry, bool handled) { |
| 3904 | if (!(keyEntry->flags & AKEY_EVENT_FLAG_FALLBACK)) { |
| 3905 | // Get the fallback key state. |
| 3906 | // Clear it out after dispatching the UP. |
| 3907 | int32_t originalKeyCode = keyEntry->keyCode; |
| 3908 | int32_t fallbackKeyCode = connection->inputState.getFallbackKey(originalKeyCode); |
| 3909 | if (keyEntry->action == AKEY_EVENT_ACTION_UP) { |
| 3910 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 3911 | } |
| 3912 | |
| 3913 | if (handled || !dispatchEntry->hasForegroundTarget()) { |
| 3914 | // If the application handles the original key for which we previously |
| 3915 | // generated a fallback or if the window is not a foreground window, |
| 3916 | // then cancel the associated fallback key, if any. |
| 3917 | if (fallbackKeyCode != -1) { |
| 3918 | if (fallbackKeyCode != AKEYCODE_UNKNOWN) { |
| 3919 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 3920 | "application handled the original non-fallback key " |
| 3921 | "or is no longer a foreground target, " |
| 3922 | "canceling previously dispatched fallback key"); |
| 3923 | options.keyCode = fallbackKeyCode; |
| 3924 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 3925 | } |
| 3926 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 3927 | } |
| 3928 | } else { |
| 3929 | // If the application did not handle a non-fallback key, first check |
| 3930 | // that we are in a good state to perform unhandled key event processing |
| 3931 | // Then ask the policy what to do with it. |
| 3932 | bool initialDown = keyEntry->action == AKEY_EVENT_ACTION_DOWN |
| 3933 | && keyEntry->repeatCount == 0; |
| 3934 | if (fallbackKeyCode == -1 && !initialDown) { |
| 3935 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3936 | LOGD("Unhandled key event: Skipping unhandled key event processing " |
| 3937 | "since this is not an initial down. " |
| 3938 | "keyCode=%d, action=%d, repeatCount=%d", |
| 3939 | originalKeyCode, keyEntry->action, keyEntry->repeatCount); |
| 3940 | #endif |
| 3941 | return false; |
| 3942 | } |
| 3943 | |
| 3944 | // Dispatch the unhandled key to the policy. |
| 3945 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3946 | LOGD("Unhandled key event: Asking policy to perform fallback action. " |
| 3947 | "keyCode=%d, action=%d, repeatCount=%d", |
| 3948 | keyEntry->keyCode, keyEntry->action, keyEntry->repeatCount); |
| 3949 | #endif |
| 3950 | KeyEvent event; |
| 3951 | initializeKeyEvent(&event, keyEntry); |
| 3952 | |
| 3953 | mLock.unlock(); |
| 3954 | |
| 3955 | bool fallback = mPolicy->dispatchUnhandledKey(connection->inputWindowHandle, |
| 3956 | &event, keyEntry->policyFlags, &event); |
| 3957 | |
| 3958 | mLock.lock(); |
| 3959 | |
| 3960 | if (connection->status != Connection::STATUS_NORMAL) { |
| 3961 | connection->inputState.removeFallbackKey(originalKeyCode); |
| 3962 | return true; // skip next cycle |
| 3963 | } |
| 3964 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 3965 | LOG_ASSERT(connection->outboundQueue.head == dispatchEntry); |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 3966 | |
| 3967 | // Latch the fallback keycode for this key on an initial down. |
| 3968 | // The fallback keycode cannot change at any other point in the lifecycle. |
| 3969 | if (initialDown) { |
| 3970 | if (fallback) { |
| 3971 | fallbackKeyCode = event.getKeyCode(); |
| 3972 | } else { |
| 3973 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 3974 | } |
| 3975 | connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode); |
| 3976 | } |
| 3977 | |
| 3978 | LOG_ASSERT(fallbackKeyCode != -1); |
| 3979 | |
| 3980 | // Cancel the fallback key if the policy decides not to send it anymore. |
| 3981 | // We will continue to dispatch the key to the policy but we will no |
| 3982 | // longer dispatch a fallback key to the application. |
| 3983 | if (fallbackKeyCode != AKEYCODE_UNKNOWN |
| 3984 | && (!fallback || fallbackKeyCode != event.getKeyCode())) { |
| 3985 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 3986 | if (fallback) { |
| 3987 | LOGD("Unhandled key event: Policy requested to send key %d" |
| 3988 | "as a fallback for %d, but on the DOWN it had requested " |
| 3989 | "to send %d instead. Fallback canceled.", |
| 3990 | event.getKeyCode(), originalKeyCode, fallbackKeyCode); |
| 3991 | } else { |
| 3992 | LOGD("Unhandled key event: Policy did not request fallback for %d," |
| 3993 | "but on the DOWN it had requested to send %d. " |
| 3994 | "Fallback canceled.", |
| 3995 | originalKeyCode, fallbackKeyCode); |
| 3996 | } |
| 3997 | #endif |
| 3998 | |
| 3999 | CancelationOptions options(CancelationOptions::CANCEL_FALLBACK_EVENTS, |
| 4000 | "canceling fallback, policy no longer desires it"); |
| 4001 | options.keyCode = fallbackKeyCode; |
| 4002 | synthesizeCancelationEventsForConnectionLocked(connection, options); |
| 4003 | |
| 4004 | fallback = false; |
| 4005 | fallbackKeyCode = AKEYCODE_UNKNOWN; |
| 4006 | if (keyEntry->action != AKEY_EVENT_ACTION_UP) { |
| 4007 | connection->inputState.setFallbackKey(originalKeyCode, |
| 4008 | fallbackKeyCode); |
| 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4013 | { |
| 4014 | String8 msg; |
| 4015 | const KeyedVector<int32_t, int32_t>& fallbackKeys = |
| 4016 | connection->inputState.getFallbackKeys(); |
| 4017 | for (size_t i = 0; i < fallbackKeys.size(); i++) { |
| 4018 | msg.appendFormat(", %d->%d", fallbackKeys.keyAt(i), |
| 4019 | fallbackKeys.valueAt(i)); |
| 4020 | } |
| 4021 | LOGD("Unhandled key event: %d currently tracked fallback keys%s.", |
| 4022 | fallbackKeys.size(), msg.string()); |
| 4023 | } |
| 4024 | #endif |
| 4025 | |
| 4026 | if (fallback) { |
| 4027 | // Restart the dispatch cycle using the fallback key. |
| 4028 | keyEntry->eventTime = event.getEventTime(); |
| 4029 | keyEntry->deviceId = event.getDeviceId(); |
| 4030 | keyEntry->source = event.getSource(); |
| 4031 | keyEntry->flags = event.getFlags() | AKEY_EVENT_FLAG_FALLBACK; |
| 4032 | keyEntry->keyCode = fallbackKeyCode; |
| 4033 | keyEntry->scanCode = event.getScanCode(); |
| 4034 | keyEntry->metaState = event.getMetaState(); |
| 4035 | keyEntry->repeatCount = event.getRepeatCount(); |
| 4036 | keyEntry->downTime = event.getDownTime(); |
| 4037 | keyEntry->syntheticRepeat = false; |
| 4038 | |
| 4039 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4040 | LOGD("Unhandled key event: Dispatching fallback key. " |
| 4041 | "originalKeyCode=%d, fallbackKeyCode=%d, fallbackMetaState=%08x", |
| 4042 | originalKeyCode, fallbackKeyCode, keyEntry->metaState); |
| 4043 | #endif |
| 4044 | |
| 4045 | dispatchEntry->inProgress = false; |
| 4046 | startDispatchCycleLocked(now(), connection); |
| 4047 | return true; // already started next cycle |
| 4048 | } else { |
| 4049 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4050 | LOGD("Unhandled key event: No fallback key."); |
| 4051 | #endif |
| 4052 | } |
| 4053 | } |
| 4054 | } |
| 4055 | return false; |
| 4056 | } |
| 4057 | |
| 4058 | bool InputDispatcher::afterMotionEventLockedInterruptible(const sp<Connection>& connection, |
| 4059 | DispatchEntry* dispatchEntry, MotionEntry* motionEntry, bool handled) { |
| 4060 | return false; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4063 | void InputDispatcher::doPokeUserActivityLockedInterruptible(CommandEntry* commandEntry) { |
| 4064 | mLock.unlock(); |
| 4065 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4066 | mPolicy->pokeUserActivity(commandEntry->eventTime, commandEntry->userActivityEventType); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4067 | |
| 4068 | mLock.lock(); |
| 4069 | } |
| 4070 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 4071 | void InputDispatcher::initializeKeyEvent(KeyEvent* event, const KeyEntry* entry) { |
| 4072 | event->initialize(entry->deviceId, entry->source, entry->action, entry->flags, |
| 4073 | entry->keyCode, entry->scanCode, entry->metaState, entry->repeatCount, |
| 4074 | entry->downTime, entry->eventTime); |
| 4075 | } |
| 4076 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 4077 | void InputDispatcher::updateDispatchStatisticsLocked(nsecs_t currentTime, const EventEntry* entry, |
| 4078 | int32_t injectionResult, nsecs_t timeSpentWaitingForApplication) { |
| 4079 | // TODO Write some statistics about how long we spend waiting. |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | void InputDispatcher::dump(String8& dump) { |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 4083 | AutoMutex _l(mLock); |
| 4084 | |
Jeff Brown | f2f4871 | 2010-10-01 17:46:21 -0700 | [diff] [blame] | 4085 | dump.append("Input Dispatcher State:\n"); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4086 | dumpDispatchStateLocked(dump); |
Jeff Brown | 214eaf4 | 2011-05-26 19:17:02 -0700 | [diff] [blame] | 4087 | |
| 4088 | dump.append(INDENT "Configuration:\n"); |
| 4089 | dump.appendFormat(INDENT2 "MaxEventsPerSecond: %d\n", mConfig.maxEventsPerSecond); |
| 4090 | dump.appendFormat(INDENT2 "KeyRepeatDelay: %0.1fms\n", mConfig.keyRepeatDelay * 0.000001f); |
| 4091 | dump.appendFormat(INDENT2 "KeyRepeatTimeout: %0.1fms\n", mConfig.keyRepeatTimeout * 0.000001f); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4092 | } |
| 4093 | |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 4094 | void InputDispatcher::monitor() { |
| 4095 | // Acquire and release the lock to ensure that the dispatcher has not deadlocked. |
| 4096 | mLock.lock(); |
| 4097 | mLock.unlock(); |
| 4098 | } |
| 4099 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 4100 | |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 4101 | // --- InputDispatcher::Queue --- |
| 4102 | |
| 4103 | template <typename T> |
| 4104 | uint32_t InputDispatcher::Queue<T>::count() const { |
| 4105 | uint32_t result = 0; |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4106 | for (const T* entry = head; entry; entry = entry->next) { |
Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 4107 | result += 1; |
| 4108 | } |
| 4109 | return result; |
| 4110 | } |
| 4111 | |
| 4112 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4113 | // --- InputDispatcher::InjectionState --- |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4114 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4115 | InputDispatcher::InjectionState::InjectionState(int32_t injectorPid, int32_t injectorUid) : |
| 4116 | refCount(1), |
| 4117 | injectorPid(injectorPid), injectorUid(injectorUid), |
| 4118 | injectionResult(INPUT_EVENT_INJECTION_PENDING), injectionIsAsync(false), |
| 4119 | pendingForegroundDispatches(0) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4120 | } |
| 4121 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4122 | InputDispatcher::InjectionState::~InjectionState() { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4125 | void InputDispatcher::InjectionState::release() { |
| 4126 | refCount -= 1; |
| 4127 | if (refCount == 0) { |
| 4128 | delete this; |
| 4129 | } else { |
| 4130 | LOG_ASSERT(refCount > 0); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4131 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 4132 | } |
| 4133 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4134 | |
| 4135 | // --- InputDispatcher::EventEntry --- |
| 4136 | |
| 4137 | InputDispatcher::EventEntry::EventEntry(int32_t type, nsecs_t eventTime, uint32_t policyFlags) : |
| 4138 | refCount(1), type(type), eventTime(eventTime), policyFlags(policyFlags), |
| 4139 | injectionState(NULL), dispatchInProgress(false) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4142 | InputDispatcher::EventEntry::~EventEntry() { |
| 4143 | releaseInjectionState(); |
| 4144 | } |
| 4145 | |
| 4146 | void InputDispatcher::EventEntry::release() { |
| 4147 | refCount -= 1; |
| 4148 | if (refCount == 0) { |
| 4149 | delete this; |
| 4150 | } else { |
| 4151 | LOG_ASSERT(refCount > 0); |
| 4152 | } |
| 4153 | } |
| 4154 | |
| 4155 | void InputDispatcher::EventEntry::releaseInjectionState() { |
| 4156 | if (injectionState) { |
| 4157 | injectionState->release(); |
| 4158 | injectionState = NULL; |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | |
| 4163 | // --- InputDispatcher::ConfigurationChangedEntry --- |
| 4164 | |
| 4165 | InputDispatcher::ConfigurationChangedEntry::ConfigurationChangedEntry(nsecs_t eventTime) : |
| 4166 | EventEntry(TYPE_CONFIGURATION_CHANGED, eventTime, 0) { |
| 4167 | } |
| 4168 | |
| 4169 | InputDispatcher::ConfigurationChangedEntry::~ConfigurationChangedEntry() { |
| 4170 | } |
| 4171 | |
| 4172 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4173 | // --- InputDispatcher::DeviceResetEntry --- |
| 4174 | |
| 4175 | InputDispatcher::DeviceResetEntry::DeviceResetEntry(nsecs_t eventTime, int32_t deviceId) : |
| 4176 | EventEntry(TYPE_DEVICE_RESET, eventTime, 0), |
| 4177 | deviceId(deviceId) { |
| 4178 | } |
| 4179 | |
| 4180 | InputDispatcher::DeviceResetEntry::~DeviceResetEntry() { |
| 4181 | } |
| 4182 | |
| 4183 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4184 | // --- InputDispatcher::KeyEntry --- |
| 4185 | |
| 4186 | InputDispatcher::KeyEntry::KeyEntry(nsecs_t eventTime, |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 4187 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 4188 | int32_t flags, int32_t keyCode, int32_t scanCode, int32_t metaState, |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4189 | int32_t repeatCount, nsecs_t downTime) : |
| 4190 | EventEntry(TYPE_KEY, eventTime, policyFlags), |
| 4191 | deviceId(deviceId), source(source), action(action), flags(flags), |
| 4192 | keyCode(keyCode), scanCode(scanCode), metaState(metaState), |
| 4193 | repeatCount(repeatCount), downTime(downTime), |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 4194 | syntheticRepeat(false), interceptKeyResult(KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN), |
| 4195 | interceptKeyWakeupTime(0) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4198 | InputDispatcher::KeyEntry::~KeyEntry() { |
| 4199 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 4200 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4201 | void InputDispatcher::KeyEntry::recycle() { |
| 4202 | releaseInjectionState(); |
| 4203 | |
| 4204 | dispatchInProgress = false; |
| 4205 | syntheticRepeat = false; |
| 4206 | interceptKeyResult = KeyEntry::INTERCEPT_KEY_RESULT_UNKNOWN; |
Jeff Brown | 905805a | 2011-10-12 13:57:59 -0700 | [diff] [blame] | 4207 | interceptKeyWakeupTime = 0; |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | |
| 4211 | // --- InputDispatcher::MotionSample --- |
| 4212 | |
| 4213 | InputDispatcher::MotionSample::MotionSample(nsecs_t eventTime, |
| 4214 | const PointerCoords* pointerCoords, uint32_t pointerCount) : |
| 4215 | next(NULL), eventTime(eventTime), eventTimeBeforeCoalescing(eventTime) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 4216 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4217 | this->pointerCoords[i].copyFrom(pointerCoords[i]); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 4218 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 4222 | // --- InputDispatcher::MotionEntry --- |
| 4223 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4224 | InputDispatcher::MotionEntry::MotionEntry(nsecs_t eventTime, |
| 4225 | int32_t deviceId, uint32_t source, uint32_t policyFlags, int32_t action, int32_t flags, |
| 4226 | int32_t metaState, int32_t buttonState, |
| 4227 | int32_t edgeFlags, float xPrecision, float yPrecision, |
| 4228 | nsecs_t downTime, uint32_t pointerCount, |
| 4229 | const PointerProperties* pointerProperties, const PointerCoords* pointerCoords) : |
| 4230 | EventEntry(TYPE_MOTION, eventTime, policyFlags), |
| 4231 | deviceId(deviceId), source(source), action(action), flags(flags), |
| 4232 | metaState(metaState), buttonState(buttonState), edgeFlags(edgeFlags), |
| 4233 | xPrecision(xPrecision), yPrecision(yPrecision), |
| 4234 | downTime(downTime), pointerCount(pointerCount), |
| 4235 | firstSample(eventTime, pointerCoords, pointerCount), |
| 4236 | lastSample(&firstSample) { |
| 4237 | for (uint32_t i = 0; i < pointerCount; i++) { |
| 4238 | this->pointerProperties[i].copyFrom(pointerProperties[i]); |
| 4239 | } |
| 4240 | } |
| 4241 | |
| 4242 | InputDispatcher::MotionEntry::~MotionEntry() { |
| 4243 | for (MotionSample* sample = firstSample.next; sample != NULL; ) { |
| 4244 | MotionSample* next = sample->next; |
| 4245 | delete sample; |
| 4246 | sample = next; |
| 4247 | } |
| 4248 | } |
| 4249 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 4250 | uint32_t InputDispatcher::MotionEntry::countSamples() const { |
| 4251 | uint32_t count = 1; |
| 4252 | for (MotionSample* sample = firstSample.next; sample != NULL; sample = sample->next) { |
| 4253 | count += 1; |
| 4254 | } |
| 4255 | return count; |
| 4256 | } |
| 4257 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 4258 | bool InputDispatcher::MotionEntry::canAppendSamples(int32_t action, uint32_t pointerCount, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4259 | const PointerProperties* pointerProperties) const { |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 4260 | if (this->action != action |
| 4261 | || this->pointerCount != pointerCount |
| 4262 | || this->isInjected()) { |
| 4263 | return false; |
| 4264 | } |
| 4265 | for (uint32_t i = 0; i < pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4266 | if (this->pointerProperties[i] != pointerProperties[i]) { |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 4267 | return false; |
| 4268 | } |
| 4269 | } |
| 4270 | return true; |
| 4271 | } |
| 4272 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4273 | void InputDispatcher::MotionEntry::appendSample( |
| 4274 | nsecs_t eventTime, const PointerCoords* pointerCoords) { |
| 4275 | MotionSample* sample = new MotionSample(eventTime, pointerCoords, pointerCount); |
| 4276 | |
| 4277 | lastSample->next = sample; |
| 4278 | lastSample = sample; |
| 4279 | } |
| 4280 | |
| 4281 | |
| 4282 | // --- InputDispatcher::DispatchEntry --- |
| 4283 | |
| 4284 | InputDispatcher::DispatchEntry::DispatchEntry(EventEntry* eventEntry, |
| 4285 | int32_t targetFlags, float xOffset, float yOffset, float scaleFactor) : |
| 4286 | eventEntry(eventEntry), targetFlags(targetFlags), |
| 4287 | xOffset(xOffset), yOffset(yOffset), scaleFactor(scaleFactor), |
| 4288 | inProgress(false), |
| 4289 | resolvedAction(0), resolvedFlags(0), |
| 4290 | headMotionSample(NULL), tailMotionSample(NULL) { |
| 4291 | eventEntry->refCount += 1; |
| 4292 | } |
| 4293 | |
| 4294 | InputDispatcher::DispatchEntry::~DispatchEntry() { |
| 4295 | eventEntry->release(); |
| 4296 | } |
| 4297 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4298 | |
| 4299 | // --- InputDispatcher::InputState --- |
| 4300 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4301 | InputDispatcher::InputState::InputState() { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | InputDispatcher::InputState::~InputState() { |
| 4305 | } |
| 4306 | |
| 4307 | bool InputDispatcher::InputState::isNeutral() const { |
| 4308 | return mKeyMementos.isEmpty() && mMotionMementos.isEmpty(); |
| 4309 | } |
| 4310 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4311 | bool InputDispatcher::InputState::isHovering(int32_t deviceId, uint32_t source) const { |
| 4312 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4313 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4314 | if (memento.deviceId == deviceId |
| 4315 | && memento.source == source |
| 4316 | && memento.hovering) { |
| 4317 | return true; |
| 4318 | } |
| 4319 | } |
| 4320 | return false; |
| 4321 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4322 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4323 | bool InputDispatcher::InputState::trackKey(const KeyEntry* entry, |
| 4324 | int32_t action, int32_t flags) { |
| 4325 | switch (action) { |
| 4326 | case AKEY_EVENT_ACTION_UP: { |
| 4327 | if (entry->flags & AKEY_EVENT_FLAG_FALLBACK) { |
| 4328 | for (size_t i = 0; i < mFallbackKeys.size(); ) { |
| 4329 | if (mFallbackKeys.valueAt(i) == entry->keyCode) { |
| 4330 | mFallbackKeys.removeItemsAt(i); |
| 4331 | } else { |
| 4332 | i += 1; |
| 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | ssize_t index = findKeyMemento(entry); |
| 4337 | if (index >= 0) { |
| 4338 | mKeyMementos.removeAt(index); |
| 4339 | return true; |
| 4340 | } |
Jeff Brown | 68b909d | 2011-12-07 16:36:01 -0800 | [diff] [blame] | 4341 | /* FIXME: We can't just drop the key up event because that prevents creating |
| 4342 | * popup windows that are automatically shown when a key is held and then |
| 4343 | * dismissed when the key is released. The problem is that the popup will |
| 4344 | * not have received the original key down, so the key up will be considered |
| 4345 | * to be inconsistent with its observed state. We could perhaps handle this |
| 4346 | * by synthesizing a key down but that will cause other problems. |
| 4347 | * |
| 4348 | * So for now, allow inconsistent key up events to be dispatched. |
| 4349 | * |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4350 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4351 | LOGD("Dropping inconsistent key up event: deviceId=%d, source=%08x, " |
| 4352 | "keyCode=%d, scanCode=%d", |
| 4353 | entry->deviceId, entry->source, entry->keyCode, entry->scanCode); |
| 4354 | #endif |
| 4355 | return false; |
Jeff Brown | 68b909d | 2011-12-07 16:36:01 -0800 | [diff] [blame] | 4356 | */ |
| 4357 | return true; |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | case AKEY_EVENT_ACTION_DOWN: { |
| 4361 | ssize_t index = findKeyMemento(entry); |
| 4362 | if (index >= 0) { |
| 4363 | mKeyMementos.removeAt(index); |
| 4364 | } |
| 4365 | addKeyMemento(entry, flags); |
| 4366 | return true; |
| 4367 | } |
| 4368 | |
| 4369 | default: |
| 4370 | return true; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4371 | } |
| 4372 | } |
| 4373 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4374 | bool InputDispatcher::InputState::trackMotion(const MotionEntry* entry, |
| 4375 | int32_t action, int32_t flags) { |
| 4376 | int32_t actionMasked = action & AMOTION_EVENT_ACTION_MASK; |
| 4377 | switch (actionMasked) { |
| 4378 | case AMOTION_EVENT_ACTION_UP: |
| 4379 | case AMOTION_EVENT_ACTION_CANCEL: { |
| 4380 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 4381 | if (index >= 0) { |
| 4382 | mMotionMementos.removeAt(index); |
| 4383 | return true; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4384 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4385 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4386 | LOGD("Dropping inconsistent motion up or cancel event: deviceId=%d, source=%08x, " |
| 4387 | "actionMasked=%d", |
| 4388 | entry->deviceId, entry->source, actionMasked); |
| 4389 | #endif |
| 4390 | return false; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4391 | } |
| 4392 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4393 | case AMOTION_EVENT_ACTION_DOWN: { |
| 4394 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 4395 | if (index >= 0) { |
| 4396 | mMotionMementos.removeAt(index); |
| 4397 | } |
| 4398 | addMotionMemento(entry, flags, false /*hovering*/); |
| 4399 | return true; |
| 4400 | } |
| 4401 | |
| 4402 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 4403 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 4404 | case AMOTION_EVENT_ACTION_MOVE: { |
| 4405 | ssize_t index = findMotionMemento(entry, false /*hovering*/); |
| 4406 | if (index >= 0) { |
| 4407 | MotionMemento& memento = mMotionMementos.editItemAt(index); |
| 4408 | memento.setPointers(entry); |
| 4409 | return true; |
| 4410 | } |
Jeff Brown | 2e45fb6 | 2011-06-29 21:19:05 -0700 | [diff] [blame] | 4411 | if (actionMasked == AMOTION_EVENT_ACTION_MOVE |
| 4412 | && (entry->source & (AINPUT_SOURCE_CLASS_JOYSTICK |
| 4413 | | AINPUT_SOURCE_CLASS_NAVIGATION))) { |
| 4414 | // Joysticks and trackballs can send MOVE events without corresponding DOWN or UP. |
| 4415 | return true; |
| 4416 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4417 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4418 | LOGD("Dropping inconsistent motion pointer up/down or move event: " |
| 4419 | "deviceId=%d, source=%08x, actionMasked=%d", |
| 4420 | entry->deviceId, entry->source, actionMasked); |
| 4421 | #endif |
| 4422 | return false; |
| 4423 | } |
| 4424 | |
| 4425 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 4426 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 4427 | if (index >= 0) { |
| 4428 | mMotionMementos.removeAt(index); |
| 4429 | return true; |
| 4430 | } |
| 4431 | #if DEBUG_OUTBOUND_EVENT_DETAILS |
| 4432 | LOGD("Dropping inconsistent motion hover exit event: deviceId=%d, source=%08x", |
| 4433 | entry->deviceId, entry->source); |
| 4434 | #endif |
| 4435 | return false; |
| 4436 | } |
| 4437 | |
| 4438 | case AMOTION_EVENT_ACTION_HOVER_ENTER: |
| 4439 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
| 4440 | ssize_t index = findMotionMemento(entry, true /*hovering*/); |
| 4441 | if (index >= 0) { |
| 4442 | mMotionMementos.removeAt(index); |
| 4443 | } |
| 4444 | addMotionMemento(entry, flags, true /*hovering*/); |
| 4445 | return true; |
| 4446 | } |
| 4447 | |
| 4448 | default: |
| 4449 | return true; |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | ssize_t InputDispatcher::InputState::findKeyMemento(const KeyEntry* entry) const { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4454 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4455 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4456 | if (memento.deviceId == entry->deviceId |
| 4457 | && memento.source == entry->source |
| 4458 | && memento.keyCode == entry->keyCode |
| 4459 | && memento.scanCode == entry->scanCode) { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4460 | return i; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4461 | } |
| 4462 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4463 | return -1; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4466 | ssize_t InputDispatcher::InputState::findMotionMemento(const MotionEntry* entry, |
| 4467 | bool hovering) const { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4468 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4469 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4470 | if (memento.deviceId == entry->deviceId |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4471 | && memento.source == entry->source |
| 4472 | && memento.hovering == hovering) { |
| 4473 | return i; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4474 | } |
| 4475 | } |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4476 | return -1; |
| 4477 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4478 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4479 | void InputDispatcher::InputState::addKeyMemento(const KeyEntry* entry, int32_t flags) { |
| 4480 | mKeyMementos.push(); |
| 4481 | KeyMemento& memento = mKeyMementos.editTop(); |
| 4482 | memento.deviceId = entry->deviceId; |
| 4483 | memento.source = entry->source; |
| 4484 | memento.keyCode = entry->keyCode; |
| 4485 | memento.scanCode = entry->scanCode; |
| 4486 | memento.flags = flags; |
| 4487 | memento.downTime = entry->downTime; |
| 4488 | } |
| 4489 | |
| 4490 | void InputDispatcher::InputState::addMotionMemento(const MotionEntry* entry, |
| 4491 | int32_t flags, bool hovering) { |
| 4492 | mMotionMementos.push(); |
| 4493 | MotionMemento& memento = mMotionMementos.editTop(); |
| 4494 | memento.deviceId = entry->deviceId; |
| 4495 | memento.source = entry->source; |
| 4496 | memento.flags = flags; |
| 4497 | memento.xPrecision = entry->xPrecision; |
| 4498 | memento.yPrecision = entry->yPrecision; |
| 4499 | memento.downTime = entry->downTime; |
| 4500 | memento.setPointers(entry); |
| 4501 | memento.hovering = hovering; |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | void InputDispatcher::InputState::MotionMemento::setPointers(const MotionEntry* entry) { |
| 4505 | pointerCount = entry->pointerCount; |
| 4506 | for (uint32_t i = 0; i < entry->pointerCount; i++) { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4507 | pointerProperties[i].copyFrom(entry->pointerProperties[i]); |
Jeff Brown | ace13b1 | 2011-03-09 17:39:48 -0800 | [diff] [blame] | 4508 | pointerCoords[i].copyFrom(entry->lastSample->pointerCoords[i]); |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4512 | void InputDispatcher::InputState::synthesizeCancelationEvents(nsecs_t currentTime, |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4513 | Vector<EventEntry*>& outEvents, const CancelationOptions& options) { |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4514 | for (size_t i = 0; i < mKeyMementos.size(); i++) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4515 | const KeyMemento& memento = mKeyMementos.itemAt(i); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4516 | if (shouldCancelKey(memento, options)) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4517 | outEvents.push(new KeyEntry(currentTime, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4518 | memento.deviceId, memento.source, 0, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4519 | AKEY_EVENT_ACTION_UP, memento.flags | AKEY_EVENT_FLAG_CANCELED, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4520 | memento.keyCode, memento.scanCode, 0, 0, memento.downTime)); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4521 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4522 | } |
| 4523 | |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4524 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4525 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4526 | if (shouldCancelMotion(memento, options)) { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4527 | outEvents.push(new MotionEntry(currentTime, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4528 | memento.deviceId, memento.source, 0, |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 4529 | memento.hovering |
| 4530 | ? AMOTION_EVENT_ACTION_HOVER_EXIT |
| 4531 | : AMOTION_EVENT_ACTION_CANCEL, |
Jeff Brown | 8134681 | 2011-06-28 20:08:48 -0700 | [diff] [blame] | 4532 | memento.flags, 0, 0, 0, |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4533 | memento.xPrecision, memento.yPrecision, memento.downTime, |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 4534 | memento.pointerCount, memento.pointerProperties, memento.pointerCoords)); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4535 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | void InputDispatcher::InputState::clear() { |
| 4540 | mKeyMementos.clear(); |
| 4541 | mMotionMementos.clear(); |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4542 | mFallbackKeys.clear(); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Jeff Brown | 9c9f1a3 | 2010-10-11 18:32:20 -0700 | [diff] [blame] | 4545 | void InputDispatcher::InputState::copyPointerStateTo(InputState& other) const { |
| 4546 | for (size_t i = 0; i < mMotionMementos.size(); i++) { |
| 4547 | const MotionMemento& memento = mMotionMementos.itemAt(i); |
| 4548 | if (memento.source & AINPUT_SOURCE_CLASS_POINTER) { |
| 4549 | for (size_t j = 0; j < other.mMotionMementos.size(); ) { |
| 4550 | const MotionMemento& otherMemento = other.mMotionMementos.itemAt(j); |
| 4551 | if (memento.deviceId == otherMemento.deviceId |
| 4552 | && memento.source == otherMemento.source) { |
| 4553 | other.mMotionMementos.removeAt(j); |
| 4554 | } else { |
| 4555 | j += 1; |
| 4556 | } |
| 4557 | } |
| 4558 | other.mMotionMementos.push(memento); |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4563 | int32_t InputDispatcher::InputState::getFallbackKey(int32_t originalKeyCode) { |
| 4564 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 4565 | return index >= 0 ? mFallbackKeys.valueAt(index) : -1; |
| 4566 | } |
| 4567 | |
| 4568 | void InputDispatcher::InputState::setFallbackKey(int32_t originalKeyCode, |
| 4569 | int32_t fallbackKeyCode) { |
| 4570 | ssize_t index = mFallbackKeys.indexOfKey(originalKeyCode); |
| 4571 | if (index >= 0) { |
| 4572 | mFallbackKeys.replaceValueAt(index, fallbackKeyCode); |
| 4573 | } else { |
| 4574 | mFallbackKeys.add(originalKeyCode, fallbackKeyCode); |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | void InputDispatcher::InputState::removeFallbackKey(int32_t originalKeyCode) { |
| 4579 | mFallbackKeys.removeItem(originalKeyCode); |
| 4580 | } |
| 4581 | |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4582 | bool InputDispatcher::InputState::shouldCancelKey(const KeyMemento& memento, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4583 | const CancelationOptions& options) { |
| 4584 | if (options.keyCode != -1 && memento.keyCode != options.keyCode) { |
| 4585 | return false; |
| 4586 | } |
| 4587 | |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4588 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
| 4589 | return false; |
| 4590 | } |
| 4591 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4592 | switch (options.mode) { |
| 4593 | case CancelationOptions::CANCEL_ALL_EVENTS: |
| 4594 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4595 | return true; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4596 | case CancelationOptions::CANCEL_FALLBACK_EVENTS: |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4597 | return memento.flags & AKEY_EVENT_FLAG_FALLBACK; |
| 4598 | default: |
| 4599 | return false; |
| 4600 | } |
| 4601 | } |
| 4602 | |
| 4603 | bool InputDispatcher::InputState::shouldCancelMotion(const MotionMemento& memento, |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4604 | const CancelationOptions& options) { |
Jeff Brown | 65fd251 | 2011-08-18 11:20:58 -0700 | [diff] [blame] | 4605 | if (options.deviceId != -1 && memento.deviceId != options.deviceId) { |
| 4606 | return false; |
| 4607 | } |
| 4608 | |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4609 | switch (options.mode) { |
| 4610 | case CancelationOptions::CANCEL_ALL_EVENTS: |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4611 | return true; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4612 | case CancelationOptions::CANCEL_POINTER_EVENTS: |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4613 | return memento.source & AINPUT_SOURCE_CLASS_POINTER; |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4614 | case CancelationOptions::CANCEL_NON_POINTER_EVENTS: |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 4615 | return !(memento.source & AINPUT_SOURCE_CLASS_POINTER); |
| 4616 | default: |
| 4617 | return false; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 4618 | } |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4619 | } |
| 4620 | |
| 4621 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4622 | // --- InputDispatcher::Connection --- |
| 4623 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 4624 | InputDispatcher::Connection::Connection(const sp<InputChannel>& inputChannel, |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 4625 | const sp<InputWindowHandle>& inputWindowHandle, bool monitor) : |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 4626 | status(STATUS_NORMAL), inputChannel(inputChannel), inputWindowHandle(inputWindowHandle), |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 4627 | monitor(monitor), |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 4628 | inputPublisher(inputChannel), |
Jeff Brown | da3d5a9 | 2011-03-29 15:11:34 -0700 | [diff] [blame] | 4629 | lastEventTime(LONG_LONG_MAX), lastDispatchTime(LONG_LONG_MAX) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | InputDispatcher::Connection::~Connection() { |
| 4633 | } |
| 4634 | |
| 4635 | status_t InputDispatcher::Connection::initialize() { |
| 4636 | return inputPublisher.initialize(); |
| 4637 | } |
| 4638 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 4639 | const char* InputDispatcher::Connection::getStatusLabel() const { |
| 4640 | switch (status) { |
| 4641 | case STATUS_NORMAL: |
| 4642 | return "NORMAL"; |
| 4643 | |
| 4644 | case STATUS_BROKEN: |
| 4645 | return "BROKEN"; |
| 4646 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 4647 | case STATUS_ZOMBIE: |
| 4648 | return "ZOMBIE"; |
| 4649 | |
| 4650 | default: |
| 4651 | return "UNKNOWN"; |
| 4652 | } |
| 4653 | } |
| 4654 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4655 | InputDispatcher::DispatchEntry* InputDispatcher::Connection::findQueuedDispatchEntryForEvent( |
| 4656 | const EventEntry* eventEntry) const { |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4657 | for (DispatchEntry* dispatchEntry = outboundQueue.tail; dispatchEntry; |
| 4658 | dispatchEntry = dispatchEntry->prev) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4659 | if (dispatchEntry->eventEntry == eventEntry) { |
| 4660 | return dispatchEntry; |
| 4661 | } |
| 4662 | } |
| 4663 | return NULL; |
| 4664 | } |
| 4665 | |
Jeff Brown | b88102f | 2010-09-08 11:49:43 -0700 | [diff] [blame] | 4666 | |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 4667 | // --- InputDispatcher::CommandEntry --- |
| 4668 | |
Jeff Brown | ac38607 | 2011-07-20 15:19:50 -0700 | [diff] [blame] | 4669 | InputDispatcher::CommandEntry::CommandEntry(Command command) : |
| 4670 | command(command), eventTime(0), keyEntry(NULL), userActivityEventType(0), handled(false) { |
Jeff Brown | 9c3cda0 | 2010-06-15 01:31:58 -0700 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | InputDispatcher::CommandEntry::~CommandEntry() { |
| 4674 | } |
| 4675 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4676 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4677 | // --- InputDispatcher::TouchState --- |
| 4678 | |
| 4679 | InputDispatcher::TouchState::TouchState() : |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 4680 | down(false), split(false), deviceId(-1), source(0) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | InputDispatcher::TouchState::~TouchState() { |
| 4684 | } |
| 4685 | |
| 4686 | void InputDispatcher::TouchState::reset() { |
| 4687 | down = false; |
| 4688 | split = false; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 4689 | deviceId = -1; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 4690 | source = 0; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4691 | windows.clear(); |
| 4692 | } |
| 4693 | |
| 4694 | void InputDispatcher::TouchState::copyFrom(const TouchState& other) { |
| 4695 | down = other.down; |
| 4696 | split = other.split; |
Jeff Brown | 9571285 | 2011-01-04 19:41:59 -0800 | [diff] [blame] | 4697 | deviceId = other.deviceId; |
Jeff Brown | 58a2da8 | 2011-01-25 16:02:22 -0800 | [diff] [blame] | 4698 | source = other.source; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4699 | windows = other.windows; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4702 | void InputDispatcher::TouchState::addOrUpdateWindow(const sp<InputWindowHandle>& windowHandle, |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4703 | int32_t targetFlags, BitSet32 pointerIds) { |
| 4704 | if (targetFlags & InputTarget::FLAG_SPLIT) { |
| 4705 | split = true; |
| 4706 | } |
| 4707 | |
| 4708 | for (size_t i = 0; i < windows.size(); i++) { |
| 4709 | TouchedWindow& touchedWindow = windows.editItemAt(i); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4710 | if (touchedWindow.windowHandle == windowHandle) { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4711 | touchedWindow.targetFlags |= targetFlags; |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 4712 | if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { |
| 4713 | touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS; |
| 4714 | } |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4715 | touchedWindow.pointerIds.value |= pointerIds.value; |
| 4716 | return; |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | windows.push(); |
| 4721 | |
| 4722 | TouchedWindow& touchedWindow = windows.editTop(); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4723 | touchedWindow.windowHandle = windowHandle; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4724 | touchedWindow.targetFlags = targetFlags; |
| 4725 | touchedWindow.pointerIds = pointerIds; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 4728 | void InputDispatcher::TouchState::filterNonAsIsTouchWindows() { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4729 | for (size_t i = 0 ; i < windows.size(); ) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 4730 | TouchedWindow& window = windows.editItemAt(i); |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 4731 | if (window.targetFlags & (InputTarget::FLAG_DISPATCH_AS_IS |
| 4732 | | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 4733 | window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK; |
| 4734 | window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4735 | i += 1; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 4736 | } else { |
| 4737 | windows.removeAt(i); |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4738 | } |
| 4739 | } |
| 4740 | } |
| 4741 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4742 | sp<InputWindowHandle> InputDispatcher::TouchState::getFirstForegroundWindowHandle() const { |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4743 | for (size_t i = 0; i < windows.size(); i++) { |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 4744 | const TouchedWindow& window = windows.itemAt(i); |
| 4745 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 4746 | return window.windowHandle; |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4747 | } |
| 4748 | } |
| 4749 | return NULL; |
| 4750 | } |
| 4751 | |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 4752 | bool InputDispatcher::TouchState::isSlippery() const { |
| 4753 | // Must have exactly one foreground window. |
| 4754 | bool haveSlipperyForegroundWindow = false; |
| 4755 | for (size_t i = 0; i < windows.size(); i++) { |
| 4756 | const TouchedWindow& window = windows.itemAt(i); |
| 4757 | if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 4758 | if (haveSlipperyForegroundWindow |
| 4759 | || !(window.windowHandle->getInfo()->layoutParamsFlags |
| 4760 | & InputWindowInfo::FLAG_SLIPPERY)) { |
Jeff Brown | 98db5fa | 2011-06-08 15:37:10 -0700 | [diff] [blame] | 4761 | return false; |
| 4762 | } |
| 4763 | haveSlipperyForegroundWindow = true; |
| 4764 | } |
| 4765 | } |
| 4766 | return haveSlipperyForegroundWindow; |
| 4767 | } |
| 4768 | |
Jeff Brown | 01ce2e9 | 2010-09-26 22:20:12 -0700 | [diff] [blame] | 4769 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 4770 | // --- InputDispatcherThread --- |
| 4771 | |
| 4772 | InputDispatcherThread::InputDispatcherThread(const sp<InputDispatcherInterface>& dispatcher) : |
| 4773 | Thread(/*canCallJava*/ true), mDispatcher(dispatcher) { |
| 4774 | } |
| 4775 | |
| 4776 | InputDispatcherThread::~InputDispatcherThread() { |
| 4777 | } |
| 4778 | |
| 4779 | bool InputDispatcherThread::threadLoop() { |
| 4780 | mDispatcher->dispatchOnce(); |
| 4781 | return true; |
| 4782 | } |
| 4783 | |
| 4784 | } // namespace android |