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 #include <ftl/NamedEnum.h>
23 #include "input/DisplayViewport.h"
24 #include "input/Input.h"
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
39 // --- InputReaderConfiguration ---
40
changesToString(uint32_t changes)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_POINTER_CAPTURE) {
71 result += "POINTER_CAPTURE | ";
72 }
73 if (changes & CHANGE_ENABLED_STATE) {
74 result += "ENABLED_STATE | ";
75 }
76 if (changes & CHANGE_MUST_REOPEN) {
77 result += "MUST_REOPEN | ";
78 }
79 return result;
80 }
81
getDisplayViewportByUniqueId(const std::string & uniqueDisplayId) const82 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId(
83 const std::string& uniqueDisplayId) const {
84 if (uniqueDisplayId.empty()) {
85 ALOGE("Empty string provided to %s", __func__);
86 return std::nullopt;
87 }
88 size_t count = 0;
89 std::optional<DisplayViewport> result = std::nullopt;
90 for (const DisplayViewport& currentViewport : mDisplays) {
91 if (uniqueDisplayId == currentViewport.uniqueId) {
92 result = std::make_optional(currentViewport);
93 count++;
94 }
95 }
96 if (count > 1) {
97 ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most",
98 count, uniqueDisplayId.c_str());
99 }
100 return result;
101 }
102
getDisplayViewportByType(ViewportType type) const103 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type)
104 const {
105 size_t count = 0;
106 std::optional<DisplayViewport> result = std::nullopt;
107 for (const DisplayViewport& currentViewport : mDisplays) {
108 // Return the first match, or the default display if we're looking for the internal viewport
109 if (currentViewport.type == type) {
110 if (!result ||
111 (type == ViewportType::INTERNAL &&
112 currentViewport.displayId == ADISPLAY_ID_DEFAULT)) {
113 result = std::make_optional(currentViewport);
114 }
115 count++;
116 }
117 }
118 if (count > 1) {
119 ALOGW("Found %zu viewports with type %s, but expected 1 at most", count,
120 NamedEnum::string(type).c_str());
121 }
122 return result;
123 }
124
getDisplayViewportByPort(uint8_t displayPort) const125 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
126 uint8_t displayPort) const {
127 for (const DisplayViewport& currentViewport : mDisplays) {
128 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
129 if (physicalPort && (*physicalPort == displayPort)) {
130 return std::make_optional(currentViewport);
131 }
132 }
133 return std::nullopt;
134 }
135
getDisplayViewportById(int32_t displayId) const136 std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById(
137 int32_t displayId) const {
138 for (const DisplayViewport& currentViewport : mDisplays) {
139 if (currentViewport.displayId == displayId) {
140 return std::make_optional(currentViewport);
141 }
142 }
143 return std::nullopt;
144 }
145
setDisplayViewports(const std::vector<DisplayViewport> & viewports)146 void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
147 mDisplays = viewports;
148 }
149
dump(std::string & dump) const150 void InputReaderConfiguration::dump(std::string& dump) const {
151 for (const DisplayViewport& viewport : mDisplays) {
152 dumpViewport(dump, viewport);
153 }
154 }
155
dumpViewport(std::string & dump,const DisplayViewport & viewport) const156 void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
157 const {
158 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
159 }
160
161
162 // -- TouchAffineTransformation --
applyTo(float & x,float & y) const163 void TouchAffineTransformation::applyTo(float& x, float& y) const {
164 float newX, newY;
165 newX = x * x_scale + y * x_ymix + x_offset;
166 newY = x * y_xmix + y * y_scale + y_offset;
167
168 x = newX;
169 y = newY;
170 }
171
172 } // namespace android
173