| Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2022 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 | #include "TouchedWindow.h" | 
|  | 18 |  | 
|  | 19 | #include <android-base/stringprintf.h> | 
|  | 20 | #include <input/PrintTools.h> | 
|  | 21 |  | 
|  | 22 | using android::base::StringPrintf; | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 |  | 
|  | 26 | namespace inputdispatcher { | 
|  | 27 |  | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 28 | bool TouchedWindow::hasHoveringPointers() const { | 
|  | 29 | return !mHoveringPointerIdsByDevice.empty(); | 
|  | 30 | } | 
|  | 31 |  | 
| Siarhei Vishniakou | e0431e4 | 2023-01-28 17:01:39 -0800 | [diff] [blame] | 32 | bool TouchedWindow::hasHoveringPointers(int32_t deviceId) const { | 
|  | 33 | return mHoveringPointerIdsByDevice.find(deviceId) != mHoveringPointerIdsByDevice.end(); | 
|  | 34 | } | 
|  | 35 |  | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 36 | void TouchedWindow::clearHoveringPointers() { | 
|  | 37 | mHoveringPointerIdsByDevice.clear(); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | bool TouchedWindow::hasHoveringPointer(int32_t deviceId, int32_t pointerId) const { | 
|  | 41 | auto it = mHoveringPointerIdsByDevice.find(deviceId); | 
|  | 42 | if (it == mHoveringPointerIdsByDevice.end()) { | 
|  | 43 | return false; | 
|  | 44 | } | 
|  | 45 | return it->second.test(pointerId); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | void TouchedWindow::addHoveringPointer(int32_t deviceId, int32_t pointerId) { | 
|  | 49 | const auto [it, _] = mHoveringPointerIdsByDevice.insert({deviceId, {}}); | 
|  | 50 | it->second.set(pointerId); | 
|  | 51 | } | 
|  | 52 |  | 
| Siarhei Vishniakou | 6464e46 | 2023-02-06 18:57:59 -0800 | [diff] [blame] | 53 | void TouchedWindow::removeTouchingPointer(int32_t pointerId) { | 
|  | 54 | pointerIds.reset(pointerId); | 
|  | 55 | pilferedPointerIds.reset(pointerId); | 
|  | 56 | if (pointerIds.none()) { | 
|  | 57 | firstDownTimeInTarget.reset(); | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 |  | 
| Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 61 | void TouchedWindow::removeAllTouchingPointers() { | 
|  | 62 | pointerIds.reset(); | 
|  | 63 | } | 
|  | 64 |  | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 65 | void TouchedWindow::removeHoveringPointer(int32_t deviceId, int32_t pointerId) { | 
|  | 66 | const auto it = mHoveringPointerIdsByDevice.find(deviceId); | 
|  | 67 | if (it == mHoveringPointerIdsByDevice.end()) { | 
|  | 68 | return; | 
|  | 69 | } | 
|  | 70 | it->second.set(pointerId, false); | 
|  | 71 |  | 
|  | 72 | if (it->second.none()) { | 
|  | 73 | mHoveringPointerIdsByDevice.erase(deviceId); | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
| Siarhei Vishniakou | 0686f0c | 2023-05-02 11:56:15 -0700 | [diff] [blame] | 77 | void TouchedWindow::removeAllHoveringPointersForDevice(int32_t deviceId) { | 
|  | 78 | mHoveringPointerIdsByDevice.erase(deviceId); | 
|  | 79 | } | 
|  | 80 |  | 
| Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 81 | std::string TouchedWindow::dump() const { | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 82 | std::string out; | 
|  | 83 | std::string hoveringPointers = | 
|  | 84 | dumpMap(mHoveringPointerIdsByDevice, constToString, bitsetToString); | 
| Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 85 | out += StringPrintf("name='%s', pointerIds=%s, targetFlags=%s, firstDownTimeInTarget=%s, " | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 86 | "mHoveringPointerIdsByDevice=%s, pilferedPointerIds=%s\n", | 
| Siarhei Vishniakou | 8a87835 | 2023-01-30 14:05:01 -0800 | [diff] [blame] | 87 | windowHandle->getName().c_str(), bitsetToString(pointerIds).c_str(), | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 88 | targetFlags.string().c_str(), toString(firstDownTimeInTarget).c_str(), | 
| Siarhei Vishniakou | 060f82b | 2023-01-27 06:39:14 -0800 | [diff] [blame] | 89 | hoveringPointers.c_str(), bitsetToString(pilferedPointerIds).c_str()); | 
| Siarhei Vishniakou | b581f7f | 2022-12-07 20:23:06 +0000 | [diff] [blame] | 90 | return out; | 
| Siarhei Vishniakou | 6e1e987 | 2022-11-08 17:51:35 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | } // namespace inputdispatcher | 
|  | 94 | } // namespace android |