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