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