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"
22
23 #include <android/log.h>
24 #include <android-base/stringprintf.h>
25
26 #define INDENT " "
27 #define INDENT2 " "
28 #define INDENT3 " "
29 #define INDENT4 " "
30 #define INDENT5 " "
31
32 using android::base::StringPrintf;
33
34 namespace android {
35
36 // --- InputReaderConfiguration ---
37
changesToString(uint32_t changes)38 std::string InputReaderConfiguration::changesToString(uint32_t changes) {
39 if (changes == 0) {
40 return "<none>";
41 }
42 std::string result;
43 if (changes & CHANGE_POINTER_SPEED) {
44 result += "POINTER_SPEED | ";
45 }
46 if (changes & CHANGE_POINTER_GESTURE_ENABLEMENT) {
47 result += "POINTER_GESTURE_ENABLEMENT | ";
48 }
49 if (changes & CHANGE_DISPLAY_INFO) {
50 result += "DISPLAY_INFO | ";
51 }
52 if (changes & CHANGE_SHOW_TOUCHES) {
53 result += "SHOW_TOUCHES | ";
54 }
55 if (changes & CHANGE_KEYBOARD_LAYOUTS) {
56 result += "KEYBOARD_LAYOUTS | ";
57 }
58 if (changes & CHANGE_DEVICE_ALIAS) {
59 result += "DEVICE_ALIAS | ";
60 }
61 if (changes & CHANGE_TOUCH_AFFINE_TRANSFORMATION) {
62 result += "TOUCH_AFFINE_TRANSFORMATION | ";
63 }
64 if (changes & CHANGE_EXTERNAL_STYLUS_PRESENCE) {
65 result += "EXTERNAL_STYLUS_PRESENCE | ";
66 }
67 if (changes & CHANGE_ENABLED_STATE) {
68 result += "ENABLED_STATE | ";
69 }
70 if (changes & CHANGE_MUST_REOPEN) {
71 result += "MUST_REOPEN | ";
72 }
73 return result;
74 }
75
getDisplayViewportByUniqueId(const std::string & uniqueDisplayId) const76 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId(
77 const std::string& uniqueDisplayId) const {
78 if (uniqueDisplayId.empty()) {
79 ALOGE("Empty string provided to %s", __func__);
80 return std::nullopt;
81 }
82 size_t count = 0;
83 std::optional<DisplayViewport> result = std::nullopt;
84 for (const DisplayViewport& currentViewport : mDisplays) {
85 if (uniqueDisplayId == currentViewport.uniqueId) {
86 result = std::make_optional(currentViewport);
87 count++;
88 }
89 }
90 if (count > 1) {
91 ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most",
92 count, uniqueDisplayId.c_str());
93 }
94 return result;
95 }
96
getDisplayViewportByType(ViewportType type) const97 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type)
98 const {
99 size_t count = 0;
100 std::optional<DisplayViewport> result = std::nullopt;
101 for (const DisplayViewport& currentViewport : mDisplays) {
102 // Return the first match
103 if (currentViewport.type == type) {
104 if (!result) {
105 result = std::make_optional(currentViewport);
106 }
107 count++;
108 }
109 }
110 if (count > 1) {
111 ALOGE("Found %zu viewports with type %s, but expected 1 at most",
112 count, viewportTypeToString(type));
113 }
114 return result;
115 }
116
getDisplayViewportByPort(uint8_t displayPort) const117 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
118 uint8_t displayPort) const {
119 for (const DisplayViewport& currentViewport : mDisplays) {
120 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
121 if (physicalPort && (*physicalPort == displayPort)) {
122 return std::make_optional(currentViewport);
123 }
124 }
125 return std::nullopt;
126 }
127
getDisplayViewportById(int32_t displayId) const128 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById(
129 int32_t displayId) const {
130 for (const DisplayViewport& currentViewport : mDisplays) {
131 if (currentViewport.displayId == displayId) {
132 return std::make_optional(currentViewport);
133 }
134 }
135 return std::nullopt;
136 }
137
setDisplayViewports(const std::vector<DisplayViewport> & viewports)138 void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
139 mDisplays = viewports;
140 }
141
dump(std::string & dump) const142 void InputReaderConfiguration::dump(std::string& dump) const {
143 for (const DisplayViewport& viewport : mDisplays) {
144 dumpViewport(dump, viewport);
145 }
146 }
147
dumpViewport(std::string & dump,const DisplayViewport & viewport) const148 void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
149 const {
150 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
151 }
152
153
154 // -- TouchAffineTransformation --
applyTo(float & x,float & y) const155 void TouchAffineTransformation::applyTo(float& x, float& y) const {
156 float newX, newY;
157 newX = x * x_scale + y * x_ymix + x_offset;
158 newY = x * y_xmix + y * y_scale + y_offset;
159
160 x = newX;
161 y = newY;
162 }
163
164 } // namespace android
165