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