blob: 2450235ec17f36a4cde70c014a23e28dbd57ba56 [file] [log] [blame]
Prabir Pradhan29c95332018-11-14 20:14:11 -08001/*
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 Wrightdde67b82020-10-27 16:09:22 +000022#include "input/DisplayViewport.h"
23#include "input/Input.h"
Prabir Pradhan29c95332018-11-14 20:14:11 -080024
Prabir Pradhan29c95332018-11-14 20:14:11 -080025#include <android-base/stringprintf.h>
Dominik Laskowski75788452021-02-09 18:51:25 -080026#include <android/log.h>
27#include <ftl/enum.h>
Prabir Pradhan29c95332018-11-14 20:14:11 -080028
29#define INDENT " "
30#define INDENT2 " "
31#define INDENT3 " "
32#define INDENT4 " "
33#define INDENT5 " "
34
35using android::base::StringPrintf;
36
37namespace android {
38
Prabir Pradhan29c95332018-11-14 20:14:11 -080039// --- InputReaderConfiguration ---
40
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -070041std::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 }
Prabir Pradhan5cc1a692021-08-06 14:01:18 +000070 if (changes & CHANGE_POINTER_CAPTURE) {
71 result += "POINTER_CAPTURE | ";
72 }
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -070073 if (changes & CHANGE_ENABLED_STATE) {
74 result += "ENABLED_STATE | ";
75 }
Harry Cuttsa546ba82023-01-13 17:21:00 +000076 if (changes & CHANGE_TOUCHPAD_SETTINGS) {
77 result += "TOUCHPAD_SETTINGS | ";
78 }
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -070079 if (changes & CHANGE_MUST_REOPEN) {
80 result += "MUST_REOPEN | ";
81 }
82 return result;
83}
84
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070085std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId(
86 const std::string& uniqueDisplayId) const {
87 if (uniqueDisplayId.empty()) {
88 ALOGE("Empty string provided to %s", __func__);
89 return std::nullopt;
90 }
91 size_t count = 0;
92 std::optional<DisplayViewport> result = std::nullopt;
Prabir Pradhan29c95332018-11-14 20:14:11 -080093 for (const DisplayViewport& currentViewport : mDisplays) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070094 if (uniqueDisplayId == currentViewport.uniqueId) {
95 result = std::make_optional(currentViewport);
96 count++;
97 }
98 }
99 if (count > 1) {
100 ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most",
101 count, uniqueDisplayId.c_str());
102 }
103 return result;
104}
105
106std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type)
107 const {
108 size_t count = 0;
109 std::optional<DisplayViewport> result = std::nullopt;
110 for (const DisplayViewport& currentViewport : mDisplays) {
Michael Wrightdde67b82020-10-27 16:09:22 +0000111 // Return the first match, or the default display if we're looking for the internal viewport
Prabir Pradhan7d384732019-09-25 15:07:32 -0700112 if (currentViewport.type == type) {
Michael Wrightdde67b82020-10-27 16:09:22 +0000113 if (!result ||
114 (type == ViewportType::INTERNAL &&
115 currentViewport.displayId == ADISPLAY_ID_DEFAULT)) {
Prabir Pradhan7d384732019-09-25 15:07:32 -0700116 result = std::make_optional(currentViewport);
117 }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700118 count++;
119 }
120 }
121 if (count > 1) {
Michael Wrightdde67b82020-10-27 16:09:22 +0000122 ALOGW("Found %zu viewports with type %s, but expected 1 at most", count,
Dominik Laskowski75788452021-02-09 18:51:25 -0800123 ftl::enum_string(type).c_str());
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700124 }
125 return result;
126}
127
128std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
129 uint8_t displayPort) const {
130 for (const DisplayViewport& currentViewport : mDisplays) {
131 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
132 if (physicalPort && (*physicalPort == displayPort)) {
133 return std::make_optional(currentViewport);
Prabir Pradhan29c95332018-11-14 20:14:11 -0800134 }
135 }
136 return std::nullopt;
137}
138
Garfield Tan888a6a42020-01-09 11:39:16 -0800139std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById(
140 int32_t displayId) const {
141 for (const DisplayViewport& currentViewport : mDisplays) {
142 if (currentViewport.displayId == displayId) {
143 return std::make_optional(currentViewport);
144 }
145 }
146 return std::nullopt;
147}
148
Prabir Pradhan29c95332018-11-14 20:14:11 -0800149void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
150 mDisplays = viewports;
151}
152
153void InputReaderConfiguration::dump(std::string& dump) const {
154 for (const DisplayViewport& viewport : mDisplays) {
155 dumpViewport(dump, viewport);
156 }
157}
158
159void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
160 const {
161 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
162}
163
164
165// -- TouchAffineTransformation --
166void TouchAffineTransformation::applyTo(float& x, float& y) const {
167 float newX, newY;
168 newX = x * x_scale + y * x_ymix + x_offset;
169 newY = x * y_xmix + y * y_scale + y_offset;
170
171 x = newX;
172 y = newY;
173}
174
Garfield Tan888a6a42020-01-09 11:39:16 -0800175} // namespace android