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