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 <set> 24 #include "InputTarget.h" 25 26 namespace android { 27 28 namespace inputdispatcher { 29 30 // Focus tracking for touch. 31 struct TouchedWindow { 32 sp<gui::WindowInfoHandle> windowHandle; 33 ftl::Flags<InputTarget::Flags> targetFlags; 34 35 // Hovering 36 bool hasHoveringPointers() const; 37 bool hasHoveringPointers(int32_t deviceId) const; 38 bool hasHoveringPointer(int32_t deviceId, int32_t pointerId) const; 39 void addHoveringPointer(int32_t deviceId, int32_t pointerId); 40 void removeHoveringPointer(int32_t deviceId, int32_t pointerId); 41 42 // Touching 43 bool hasTouchingPointer(int32_t deviceId, int32_t pointerId) const; 44 bool hasTouchingPointers() const; 45 bool hasTouchingPointers(int32_t deviceId) const; 46 std::bitset<MAX_POINTER_ID + 1> getTouchingPointers(int32_t deviceId) const; 47 void addTouchingPointer(int32_t deviceId, int32_t pointerId); 48 void addTouchingPointers(int32_t deviceId, std::bitset<MAX_POINTER_ID + 1> pointers); 49 void removeTouchingPointer(int32_t deviceId, int32_t pointerId); 50 void removeTouchingPointers(int32_t deviceId, std::bitset<MAX_POINTER_ID + 1> pointers); 51 /** 52 * Get the currently active touching device id. If there isn't exactly 1 touching device, return 53 * nullopt. 54 */ 55 std::set<int32_t> getTouchingDeviceIds() const; 56 /** 57 * The ids of devices that are currently touching or hovering. 58 */ 59 std::set<int32_t> getActiveDeviceIds() const; 60 61 // Pilfering pointers 62 bool hasPilferingPointers(int32_t deviceId) const; 63 void addPilferingPointers(int32_t deviceId, std::bitset<MAX_POINTER_ID + 1> pointerIds); 64 void addPilferingPointer(int32_t deviceId, int32_t pointerId); 65 std::bitset<MAX_POINTER_ID + 1> getPilferingPointers(int32_t deviceId) const; 66 std::map<int32_t, std::bitset<MAX_POINTER_ID + 1>> getPilferingPointers() const; 67 68 // Down time 69 std::optional<nsecs_t> getDownTimeInTarget(int32_t deviceId) const; 70 void trySetDownTimeInTarget(int32_t deviceId, nsecs_t downTime); 71 72 void removeAllTouchingPointersForDevice(int32_t deviceId); 73 void removeAllHoveringPointersForDevice(int32_t deviceId); 74 void clearHoveringPointers(); 75 std::string dump() const; 76 77 private: 78 struct DeviceState { 79 std::bitset<MAX_POINTER_ID + 1> touchingPointerIds; 80 // The pointer ids of the pointers that this window is currently pilfering, by device 81 std::bitset<MAX_POINTER_ID + 1> pilferingPointerIds; 82 // Time at which the first action down occurred on this window, for each device 83 // NOTE: This is not initialized in case of HOVER entry/exit and DISPATCH_AS_OUTSIDE 84 // scenario. 85 std::optional<nsecs_t> downTimeInTarget; 86 std::bitset<MAX_POINTER_ID + 1> hoveringPointerIds; 87 hasPointersTouchedWindow::DeviceState88 bool hasPointers() const { return touchingPointerIds.any() || hoveringPointerIds.any(); }; 89 }; 90 91 std::map<int32_t /*deviceId*/, DeviceState> mDeviceStates; 92 93 static std::string deviceStateToString(const TouchedWindow::DeviceState& state); 94 }; 95 96 } // namespace inputdispatcher 97 } // namespace android 98