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