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 17 #pragma once 18 19 #include <gui/WindowInfo.h> 20 #include <input/Input.h> 21 #include <utils/BitSet.h> 22 #include <bitset> 23 #include <ostream> 24 #include <set> 25 #include "InputTarget.h" 26 27 namespace android { 28 29 namespace inputdispatcher { 30 31 // Focus tracking for touch. 32 struct TouchedWindow { 33 sp<gui::WindowInfoHandle> windowHandle; 34 InputTarget::DispatchMode dispatchMode = InputTarget::DispatchMode::AS_IS; 35 ftl::Flags<InputTarget::Flags> targetFlags; 36 37 // If another window has transferred touches to this window, and wants to continue sending the 38 // rest of the gesture to this window, store the token of the originating (transferred-from) 39 // window here. 40 sp<IBinder> forwardingWindowToken; 41 42 // Hovering 43 bool hasHoveringPointers() const; 44 bool hasHoveringPointers(DeviceId deviceId) const; 45 bool hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const; 46 void addHoveringPointer(DeviceId deviceId, const PointerProperties& pointer, float x, float y); 47 void removeHoveringPointer(DeviceId deviceId, int32_t pointerId); 48 49 // Touching 50 bool hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const; 51 bool hasTouchingPointers() const; 52 bool hasTouchingPointers(DeviceId deviceId) const; 53 std::vector<PointerProperties> getTouchingPointers(DeviceId deviceId) const; 54 android::base::Result<void> addTouchingPointers(DeviceId deviceId, 55 const std::vector<PointerProperties>& pointers); 56 void removeTouchingPointer(DeviceId deviceId, int32_t pointerId); 57 void removeTouchingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointers); 58 bool hasActiveStylus() const; 59 std::set<DeviceId> getTouchingDeviceIds() const; 60 61 // Pilfering pointers 62 bool hasPilferingPointers(DeviceId deviceId) const; 63 void addPilferingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointerIds); 64 void addPilferingPointer(DeviceId deviceId, int32_t pointerId); 65 std::bitset<MAX_POINTER_ID + 1> getPilferingPointers(DeviceId deviceId) const; 66 std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> getPilferingPointers() const; 67 68 // Down time 69 std::optional<nsecs_t> getDownTimeInTarget(DeviceId deviceId) const; 70 void trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime); 71 72 void removeAllTouchingPointersForDevice(DeviceId deviceId); 73 void removeAllHoveringPointersForDevice(DeviceId deviceId); 74 void clearHoveringPointers(DeviceId deviceId); 75 std::string dump() const; 76 77 struct HoveringPointer { 78 PointerProperties properties; 79 float x; 80 float y; 81 }; 82 83 std::vector<DeviceId> eraseHoveringPointersIf( 84 std::function<bool(const PointerProperties&, float x, float y)> condition); 85 86 private: 87 struct DeviceState { 88 std::vector<PointerProperties> touchingPointers; 89 // The pointer ids of the pointers that this window is currently pilfering, by device 90 std::bitset<MAX_POINTER_ID + 1> pilferingPointerIds; 91 // Time at which the first action down occurred on this window, for each device 92 // NOTE: This is not initialized in case of HOVER entry/exit and DISPATCH_AS_OUTSIDE 93 // scenario. 94 std::optional<nsecs_t> downTimeInTarget; 95 std::vector<HoveringPointer> hoveringPointers; 96 hasPointersTouchedWindow::DeviceState97 bool hasPointers() const { return !touchingPointers.empty() || !hoveringPointers.empty(); }; 98 }; 99 100 std::map<DeviceId, DeviceState> mDeviceStates; 101 102 static std::string deviceStateToString(const TouchedWindow::DeviceState& state); 103 }; 104 105 std::ostream& operator<<(std::ostream& out, const TouchedWindow& window); 106 107 } // namespace inputdispatcher 108 } // namespace android 109