blob: 0a38f9f5cdcc645c17d31ee59c484c6fd896b322 [file] [log] [blame]
Garfield Tane84e6f92019-08-29 17:28:41 -07001/*
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
Prabir Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Garfield Tane84e6f92019-08-29 17:28:41 -070018
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080019#include <gui/WindowInfo.h>
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000020#include <input/Input.h>
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080021#include <utils/BitSet.h>
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000022#include <bitset>
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070023#include <set>
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080024#include "InputTarget.h"
Garfield Tane84e6f92019-08-29 17:28:41 -070025
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080026namespace android {
Garfield Tane84e6f92019-08-29 17:28:41 -070027
28namespace inputdispatcher {
29
30// Focus tracking for touch.
31struct TouchedWindow {
chaviw98318de2021-05-19 16:45:23 -050032 sp<gui::WindowInfoHandle> windowHandle;
Siarhei Vishniakou253f4642022-11-09 13:42:06 -080033 ftl::Flags<InputTarget::Flags> targetFlags;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000034
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070035 // Hovering
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000036 bool hasHoveringPointers() const;
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070037 bool hasHoveringPointers(DeviceId deviceId) const;
38 bool hasHoveringPointer(DeviceId deviceId, int32_t pointerId) const;
39 void addHoveringPointer(DeviceId deviceId, int32_t pointerId);
40 void removeHoveringPointer(DeviceId deviceId, int32_t pointerId);
Siarhei Vishniakou0686f0c2023-05-02 11:56:15 -070041
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070042 // Touching
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070043 bool hasTouchingPointer(DeviceId deviceId, int32_t pointerId) const;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070044 bool hasTouchingPointers() const;
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070045 bool hasTouchingPointers(DeviceId deviceId) const;
46 std::bitset<MAX_POINTER_ID + 1> getTouchingPointers(DeviceId deviceId) const;
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070047 void addTouchingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointers);
48 void removeTouchingPointer(DeviceId deviceId, int32_t pointerId);
49 void removeTouchingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointers);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070050 /**
51 * Get the currently active touching device id. If there isn't exactly 1 touching device, return
52 * nullopt.
53 */
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070054 std::set<DeviceId> getTouchingDeviceIds() const;
Siarhei Vishniakou45504fe2023-05-05 16:05:10 -070055 /**
56 * The ids of devices that are currently touching or hovering.
57 */
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070058 std::set<DeviceId> getActiveDeviceIds() const;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070059
60 // Pilfering pointers
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070061 bool hasPilferingPointers(DeviceId deviceId) const;
62 void addPilferingPointers(DeviceId deviceId, std::bitset<MAX_POINTER_ID + 1> pointerIds);
63 void addPilferingPointer(DeviceId deviceId, int32_t pointerId);
64 std::bitset<MAX_POINTER_ID + 1> getPilferingPointers(DeviceId deviceId) const;
65 std::map<DeviceId, std::bitset<MAX_POINTER_ID + 1>> getPilferingPointers() const;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070066
67 // Down time
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070068 std::optional<nsecs_t> getDownTimeInTarget(DeviceId deviceId) const;
69 void trySetDownTimeInTarget(DeviceId deviceId, nsecs_t downTime);
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070070
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070071 void removeAllTouchingPointersForDevice(DeviceId deviceId);
72 void removeAllHoveringPointersForDevice(DeviceId deviceId);
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000073 void clearHoveringPointers();
Siarhei Vishniakou6e1e9872022-11-08 17:51:35 -080074 std::string dump() const;
Siarhei Vishniakoub581f7f2022-12-07 20:23:06 +000075
76private:
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070077 struct DeviceState {
78 std::bitset<MAX_POINTER_ID + 1> touchingPointerIds;
79 // The pointer ids of the pointers that this window is currently pilfering, by device
80 std::bitset<MAX_POINTER_ID + 1> pilferingPointerIds;
81 // Time at which the first action down occurred on this window, for each device
82 // NOTE: This is not initialized in case of HOVER entry/exit and DISPATCH_AS_OUTSIDE
83 // scenario.
84 std::optional<nsecs_t> downTimeInTarget;
85 std::bitset<MAX_POINTER_ID + 1> hoveringPointerIds;
86
87 bool hasPointers() const { return touchingPointerIds.any() || hoveringPointerIds.any(); };
88 };
89
Siarhei Vishniakoud38a1e02023-07-18 11:55:17 -070090 std::map<DeviceId, DeviceState> mDeviceStates;
Siarhei Vishniakou0836a302023-05-03 13:54:30 -070091
92 static std::string deviceStateToString(const TouchedWindow::DeviceState& state);
Garfield Tane84e6f92019-08-29 17:28:41 -070093};
94
95} // namespace inputdispatcher
96} // namespace android