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