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