| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2019 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 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 17 | #include <gui/WindowInfo.h> | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 18 |  | 
 | 19 | #include "InputTarget.h" | 
 | 20 |  | 
 | 21 | #include "TouchState.h" | 
 | 22 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 23 | using android::gui::WindowInfo; | 
 | 24 | using android::gui::WindowInfoHandle; | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 25 |  | 
 | 26 | namespace android::inputdispatcher { | 
 | 27 |  | 
 | 28 | TouchState::TouchState() | 
 | 29 |       : down(false), split(false), deviceId(-1), source(0), displayId(ADISPLAY_ID_NONE) {} | 
 | 30 |  | 
 | 31 | TouchState::~TouchState() {} | 
 | 32 |  | 
 | 33 | void TouchState::reset() { | 
 | 34 |     down = false; | 
 | 35 |     split = false; | 
 | 36 |     deviceId = -1; | 
 | 37 |     source = 0; | 
 | 38 |     displayId = ADISPLAY_ID_NONE; | 
 | 39 |     windows.clear(); | 
 | 40 |     portalWindows.clear(); | 
 | 41 |     gestureMonitors.clear(); | 
 | 42 | } | 
 | 43 |  | 
 | 44 | void TouchState::copyFrom(const TouchState& other) { | 
 | 45 |     down = other.down; | 
 | 46 |     split = other.split; | 
 | 47 |     deviceId = other.deviceId; | 
 | 48 |     source = other.source; | 
 | 49 |     displayId = other.displayId; | 
 | 50 |     windows = other.windows; | 
 | 51 |     portalWindows = other.portalWindows; | 
 | 52 |     gestureMonitors = other.gestureMonitors; | 
 | 53 | } | 
 | 54 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 55 | void TouchState::addOrUpdateWindow(const sp<WindowInfoHandle>& windowHandle, int32_t targetFlags, | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 56 |                                    BitSet32 pointerIds) { | 
 | 57 |     if (targetFlags & InputTarget::FLAG_SPLIT) { | 
 | 58 |         split = true; | 
 | 59 |     } | 
 | 60 |  | 
 | 61 |     for (size_t i = 0; i < windows.size(); i++) { | 
 | 62 |         TouchedWindow& touchedWindow = windows[i]; | 
 | 63 |         if (touchedWindow.windowHandle == windowHandle) { | 
 | 64 |             touchedWindow.targetFlags |= targetFlags; | 
 | 65 |             if (targetFlags & InputTarget::FLAG_DISPATCH_AS_SLIPPERY_EXIT) { | 
 | 66 |                 touchedWindow.targetFlags &= ~InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 67 |             } | 
 | 68 |             touchedWindow.pointerIds.value |= pointerIds.value; | 
 | 69 |             return; | 
 | 70 |         } | 
 | 71 |     } | 
 | 72 |  | 
 | 73 |     TouchedWindow touchedWindow; | 
 | 74 |     touchedWindow.windowHandle = windowHandle; | 
 | 75 |     touchedWindow.targetFlags = targetFlags; | 
 | 76 |     touchedWindow.pointerIds = pointerIds; | 
 | 77 |     windows.push_back(touchedWindow); | 
 | 78 | } | 
 | 79 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 80 | void TouchState::addPortalWindow(const sp<android::gui::WindowInfoHandle>& windowHandle) { | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 81 |     size_t numWindows = portalWindows.size(); | 
 | 82 |     for (size_t i = 0; i < numWindows; i++) { | 
 | 83 |         if (portalWindows[i] == windowHandle) { | 
 | 84 |             return; | 
 | 85 |         } | 
 | 86 |     } | 
 | 87 |     portalWindows.push_back(windowHandle); | 
 | 88 | } | 
 | 89 |  | 
 | 90 | void TouchState::addGestureMonitors(const std::vector<TouchedMonitor>& newMonitors) { | 
 | 91 |     const size_t newSize = gestureMonitors.size() + newMonitors.size(); | 
 | 92 |     gestureMonitors.reserve(newSize); | 
 | 93 |     gestureMonitors.insert(std::end(gestureMonitors), std::begin(newMonitors), | 
 | 94 |                            std::end(newMonitors)); | 
 | 95 | } | 
 | 96 |  | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 97 | void TouchState::removeWindowByToken(const sp<IBinder>& token) { | 
 | 98 |     for (size_t i = 0; i < windows.size(); i++) { | 
 | 99 |         if (windows[i].windowHandle->getToken() == token) { | 
 | 100 |             windows.erase(windows.begin() + i); | 
 | 101 |             return; | 
 | 102 |         } | 
 | 103 |     } | 
 | 104 | } | 
 | 105 |  | 
 | 106 | void TouchState::filterNonAsIsTouchWindows() { | 
 | 107 |     for (size_t i = 0; i < windows.size();) { | 
 | 108 |         TouchedWindow& window = windows[i]; | 
 | 109 |         if (window.targetFlags & | 
 | 110 |             (InputTarget::FLAG_DISPATCH_AS_IS | InputTarget::FLAG_DISPATCH_AS_SLIPPERY_ENTER)) { | 
 | 111 |             window.targetFlags &= ~InputTarget::FLAG_DISPATCH_MASK; | 
 | 112 |             window.targetFlags |= InputTarget::FLAG_DISPATCH_AS_IS; | 
 | 113 |             i += 1; | 
 | 114 |         } else { | 
 | 115 |             windows.erase(windows.begin() + i); | 
 | 116 |         } | 
 | 117 |     } | 
 | 118 | } | 
 | 119 |  | 
 | 120 | void TouchState::filterNonMonitors() { | 
 | 121 |     windows.clear(); | 
 | 122 |     portalWindows.clear(); | 
 | 123 | } | 
 | 124 |  | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 125 | sp<WindowInfoHandle> TouchState::getFirstForegroundWindowHandle() const { | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 126 |     for (size_t i = 0; i < windows.size(); i++) { | 
 | 127 |         const TouchedWindow& window = windows[i]; | 
 | 128 |         if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
 | 129 |             return window.windowHandle; | 
 | 130 |         } | 
 | 131 |     } | 
 | 132 |     return nullptr; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | bool TouchState::isSlippery() const { | 
 | 136 |     // Must have exactly one foreground window. | 
 | 137 |     bool haveSlipperyForegroundWindow = false; | 
 | 138 |     for (const TouchedWindow& window : windows) { | 
 | 139 |         if (window.targetFlags & InputTarget::FLAG_FOREGROUND) { | 
 | 140 |             if (haveSlipperyForegroundWindow || | 
| chaviw | 3277faf | 2021-05-19 16:45:23 -0500 | [diff] [blame] | 141 |                 !window.windowHandle->getInfo()->flags.test(WindowInfo::Flag::SLIPPERY)) { | 
| Garfield Tan | e84e6f9 | 2019-08-29 17:28:41 -0700 | [diff] [blame] | 142 |                 return false; | 
 | 143 |             } | 
 | 144 |             haveSlipperyForegroundWindow = true; | 
 | 145 |         } | 
 | 146 |     } | 
 | 147 |     return haveSlipperyForegroundWindow; | 
 | 148 | } | 
 | 149 |  | 
 | 150 | } // namespace android::inputdispatcher |