Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | #define LOG_TAG "InputReaderBase" |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "InputReaderBase.h" |
Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 22 | #include "input/DisplayViewport.h" |
| 23 | #include "input/Input.h" |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 24 | |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 26 | #include <android/log.h> |
| 27 | #include <ftl/enum.h> |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 28 | |
| 29 | #define INDENT " " |
| 30 | #define INDENT2 " " |
| 31 | #define INDENT3 " " |
| 32 | #define INDENT4 " " |
| 33 | #define INDENT5 " " |
| 34 | |
| 35 | using android::base::StringPrintf; |
| 36 | |
| 37 | namespace android { |
| 38 | |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 39 | // --- InputReaderConfiguration --- |
| 40 | |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 41 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId( |
| 42 | const std::string& uniqueDisplayId) const { |
| 43 | if (uniqueDisplayId.empty()) { |
| 44 | ALOGE("Empty string provided to %s", __func__); |
| 45 | return std::nullopt; |
| 46 | } |
| 47 | size_t count = 0; |
| 48 | std::optional<DisplayViewport> result = std::nullopt; |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 49 | for (const DisplayViewport& currentViewport : mDisplays) { |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 50 | if (uniqueDisplayId == currentViewport.uniqueId) { |
| 51 | result = std::make_optional(currentViewport); |
| 52 | count++; |
| 53 | } |
| 54 | } |
| 55 | if (count > 1) { |
| 56 | ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most", |
| 57 | count, uniqueDisplayId.c_str()); |
| 58 | } |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type) |
| 63 | const { |
| 64 | size_t count = 0; |
| 65 | std::optional<DisplayViewport> result = std::nullopt; |
| 66 | for (const DisplayViewport& currentViewport : mDisplays) { |
Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 67 | // Return the first match, or the default display if we're looking for the internal viewport |
Prabir Pradhan | 7d38473 | 2019-09-25 15:07:32 -0700 | [diff] [blame] | 68 | if (currentViewport.type == type) { |
Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 69 | if (!result || |
| 70 | (type == ViewportType::INTERNAL && |
Siarhei Vishniakou | cfbee53 | 2024-05-10 13:41:35 -0700 | [diff] [blame] | 71 | currentViewport.displayId == ui::LogicalDisplayId::DEFAULT)) { |
Prabir Pradhan | 7d38473 | 2019-09-25 15:07:32 -0700 | [diff] [blame] | 72 | result = std::make_optional(currentViewport); |
| 73 | } |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 74 | count++; |
| 75 | } |
| 76 | } |
| 77 | if (count > 1) { |
Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 78 | ALOGW("Found %zu viewports with type %s, but expected 1 at most", count, |
Dominik Laskowski | 7578845 | 2021-02-09 18:51:25 -0800 | [diff] [blame] | 79 | ftl::enum_string(type).c_str()); |
Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort( |
| 85 | uint8_t displayPort) const { |
| 86 | for (const DisplayViewport& currentViewport : mDisplays) { |
| 87 | const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort; |
| 88 | if (physicalPort && (*physicalPort == displayPort)) { |
| 89 | return std::make_optional(currentViewport); |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | return std::nullopt; |
| 93 | } |
| 94 | |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 95 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById( |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 96 | ui::LogicalDisplayId displayId) const { |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 97 | for (const DisplayViewport& currentViewport : mDisplays) { |
| 98 | if (currentViewport.displayId == displayId) { |
| 99 | return std::make_optional(currentViewport); |
| 100 | } |
| 101 | } |
| 102 | return std::nullopt; |
| 103 | } |
| 104 | |
Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 105 | void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { |
| 106 | mDisplays = viewports; |
| 107 | } |
| 108 | |
| 109 | void InputReaderConfiguration::dump(std::string& dump) const { |
| 110 | for (const DisplayViewport& viewport : mDisplays) { |
| 111 | dumpViewport(dump, viewport); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport) |
| 116 | const { |
| 117 | dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str()); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // -- TouchAffineTransformation -- |
| 122 | void TouchAffineTransformation::applyTo(float& x, float& y) const { |
| 123 | float newX, newY; |
| 124 | newX = x * x_scale + y * x_ymix + x_offset; |
| 125 | newY = x * y_xmix + y * y_scale + y_offset; |
| 126 | |
| 127 | x = newX; |
| 128 | y = newY; |
| 129 | } |
| 130 | |
Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 131 | } // namespace android |