| 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" | 
|  | 24 | #include "input/NamedEnum.h" | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | #include <android/log.h> | 
|  | 27 | #include <android-base/stringprintf.h> | 
|  | 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 | c5ae0dc | 2019-07-10 15:51:18 -0700 | [diff] [blame] | 41 | std::string InputReaderConfiguration::changesToString(uint32_t changes) { | 
|  | 42 | if (changes == 0) { | 
|  | 43 | return "<none>"; | 
|  | 44 | } | 
|  | 45 | std::string result; | 
|  | 46 | if (changes & CHANGE_POINTER_SPEED) { | 
|  | 47 | result += "POINTER_SPEED | "; | 
|  | 48 | } | 
|  | 49 | if (changes & CHANGE_POINTER_GESTURE_ENABLEMENT) { | 
|  | 50 | result += "POINTER_GESTURE_ENABLEMENT | "; | 
|  | 51 | } | 
|  | 52 | if (changes & CHANGE_DISPLAY_INFO) { | 
|  | 53 | result += "DISPLAY_INFO | "; | 
|  | 54 | } | 
|  | 55 | if (changes & CHANGE_SHOW_TOUCHES) { | 
|  | 56 | result += "SHOW_TOUCHES | "; | 
|  | 57 | } | 
|  | 58 | if (changes & CHANGE_KEYBOARD_LAYOUTS) { | 
|  | 59 | result += "KEYBOARD_LAYOUTS | "; | 
|  | 60 | } | 
|  | 61 | if (changes & CHANGE_DEVICE_ALIAS) { | 
|  | 62 | result += "DEVICE_ALIAS | "; | 
|  | 63 | } | 
|  | 64 | if (changes & CHANGE_TOUCH_AFFINE_TRANSFORMATION) { | 
|  | 65 | result += "TOUCH_AFFINE_TRANSFORMATION | "; | 
|  | 66 | } | 
|  | 67 | if (changes & CHANGE_EXTERNAL_STYLUS_PRESENCE) { | 
|  | 68 | result += "EXTERNAL_STYLUS_PRESENCE | "; | 
|  | 69 | } | 
|  | 70 | if (changes & CHANGE_ENABLED_STATE) { | 
|  | 71 | result += "ENABLED_STATE | "; | 
|  | 72 | } | 
|  | 73 | if (changes & CHANGE_MUST_REOPEN) { | 
|  | 74 | result += "MUST_REOPEN | "; | 
|  | 75 | } | 
|  | 76 | return result; | 
|  | 77 | } | 
|  | 78 |  | 
| Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 79 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId( | 
|  | 80 | const std::string& uniqueDisplayId) const { | 
|  | 81 | if (uniqueDisplayId.empty()) { | 
|  | 82 | ALOGE("Empty string provided to %s", __func__); | 
|  | 83 | return std::nullopt; | 
|  | 84 | } | 
|  | 85 | size_t count = 0; | 
|  | 86 | std::optional<DisplayViewport> result = std::nullopt; | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 87 | for (const DisplayViewport& currentViewport : mDisplays) { | 
| Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 88 | if (uniqueDisplayId == currentViewport.uniqueId) { | 
|  | 89 | result = std::make_optional(currentViewport); | 
|  | 90 | count++; | 
|  | 91 | } | 
|  | 92 | } | 
|  | 93 | if (count > 1) { | 
|  | 94 | ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most", | 
|  | 95 | count, uniqueDisplayId.c_str()); | 
|  | 96 | } | 
|  | 97 | return result; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type) | 
|  | 101 | const { | 
|  | 102 | size_t count = 0; | 
|  | 103 | std::optional<DisplayViewport> result = std::nullopt; | 
|  | 104 | for (const DisplayViewport& currentViewport : mDisplays) { | 
| Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 105 | // 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] | 106 | if (currentViewport.type == type) { | 
| Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 107 | if (!result || | 
|  | 108 | (type == ViewportType::INTERNAL && | 
|  | 109 | currentViewport.displayId == ADISPLAY_ID_DEFAULT)) { | 
| Prabir Pradhan | 7d38473 | 2019-09-25 15:07:32 -0700 | [diff] [blame] | 110 | result = std::make_optional(currentViewport); | 
|  | 111 | } | 
| Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 112 | count++; | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 | if (count > 1) { | 
| Michael Wright | dde67b8 | 2020-10-27 16:09:22 +0000 | [diff] [blame] | 116 | ALOGW("Found %zu viewports with type %s, but expected 1 at most", count, | 
|  | 117 | NamedEnum::string(type).c_str()); | 
| Siarhei Vishniakou | 8158e7e | 2018-10-15 14:28:20 -0700 | [diff] [blame] | 118 | } | 
|  | 119 | return result; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort( | 
|  | 123 | uint8_t displayPort) const { | 
|  | 124 | for (const DisplayViewport& currentViewport : mDisplays) { | 
|  | 125 | const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort; | 
|  | 126 | if (physicalPort && (*physicalPort == displayPort)) { | 
|  | 127 | return std::make_optional(currentViewport); | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 128 | } | 
|  | 129 | } | 
|  | 130 | return std::nullopt; | 
|  | 131 | } | 
|  | 132 |  | 
| Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 133 | std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById( | 
|  | 134 | int32_t displayId) const { | 
|  | 135 | for (const DisplayViewport& currentViewport : mDisplays) { | 
|  | 136 | if (currentViewport.displayId == displayId) { | 
|  | 137 | return std::make_optional(currentViewport); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 | return std::nullopt; | 
|  | 141 | } | 
|  | 142 |  | 
| Prabir Pradhan | 29c9533 | 2018-11-14 20:14:11 -0800 | [diff] [blame] | 143 | void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) { | 
|  | 144 | mDisplays = viewports; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | void InputReaderConfiguration::dump(std::string& dump) const { | 
|  | 148 | for (const DisplayViewport& viewport : mDisplays) { | 
|  | 149 | dumpViewport(dump, viewport); | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport) | 
|  | 154 | const { | 
|  | 155 | dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str()); | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 |  | 
|  | 159 | // -- TouchAffineTransformation -- | 
|  | 160 | void TouchAffineTransformation::applyTo(float& x, float& y) const { | 
|  | 161 | float newX, newY; | 
|  | 162 | newX = x * x_scale + y * x_ymix + x_offset; | 
|  | 163 | newY = x * y_xmix + y * y_scale + y_offset; | 
|  | 164 |  | 
|  | 165 | x = newX; | 
|  | 166 | y = newY; | 
|  | 167 | } | 
|  | 168 |  | 
| Garfield Tan | 888a6a4 | 2020-01-09 11:39:16 -0800 | [diff] [blame] | 169 | } // namespace android |