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