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