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