blob: bc53cf52cc65f2c21075fbdbe0fa171a380a024c [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"
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
32using android::base::StringPrintf;
33
34namespace android {
35
36// --- InputReaderThread ---
37
38InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
39 Thread(/*canCallJava*/ true), mReader(reader) {
40}
41
42InputReaderThread::~InputReaderThread() {
43}
44
45bool InputReaderThread::threadLoop() {
46 mReader->loopOnce();
47 return true;
48}
49
50// --- InputReaderConfiguration ---
51
Siarhei Vishniakouc5ae0dc2019-07-10 15:51:18 -070052std::string InputReaderConfiguration::changesToString(uint32_t changes) {
53 if (changes == 0) {
54 return "<none>";
55 }
56 std::string result;
57 if (changes & CHANGE_POINTER_SPEED) {
58 result += "POINTER_SPEED | ";
59 }
60 if (changes & CHANGE_POINTER_GESTURE_ENABLEMENT) {
61 result += "POINTER_GESTURE_ENABLEMENT | ";
62 }
63 if (changes & CHANGE_DISPLAY_INFO) {
64 result += "DISPLAY_INFO | ";
65 }
66 if (changes & CHANGE_SHOW_TOUCHES) {
67 result += "SHOW_TOUCHES | ";
68 }
69 if (changes & CHANGE_KEYBOARD_LAYOUTS) {
70 result += "KEYBOARD_LAYOUTS | ";
71 }
72 if (changes & CHANGE_DEVICE_ALIAS) {
73 result += "DEVICE_ALIAS | ";
74 }
75 if (changes & CHANGE_TOUCH_AFFINE_TRANSFORMATION) {
76 result += "TOUCH_AFFINE_TRANSFORMATION | ";
77 }
78 if (changes & CHANGE_EXTERNAL_STYLUS_PRESENCE) {
79 result += "EXTERNAL_STYLUS_PRESENCE | ";
80 }
81 if (changes & CHANGE_ENABLED_STATE) {
82 result += "ENABLED_STATE | ";
83 }
84 if (changes & CHANGE_MUST_REOPEN) {
85 result += "MUST_REOPEN | ";
86 }
87 return result;
88}
89
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070090std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId(
91 const std::string& uniqueDisplayId) const {
92 if (uniqueDisplayId.empty()) {
93 ALOGE("Empty string provided to %s", __func__);
94 return std::nullopt;
95 }
96 size_t count = 0;
97 std::optional<DisplayViewport> result = std::nullopt;
Prabir Pradhan29c95332018-11-14 20:14:11 -080098 for (const DisplayViewport& currentViewport : mDisplays) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070099 if (uniqueDisplayId == currentViewport.uniqueId) {
100 result = std::make_optional(currentViewport);
101 count++;
102 }
103 }
104 if (count > 1) {
105 ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most",
106 count, uniqueDisplayId.c_str());
107 }
108 return result;
109}
110
111std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type)
112 const {
113 size_t count = 0;
114 std::optional<DisplayViewport> result = std::nullopt;
115 for (const DisplayViewport& currentViewport : mDisplays) {
116 // Return the first match
117 if (currentViewport.type == type && !result) {
118 result = std::make_optional(currentViewport);
119 count++;
120 }
121 }
122 if (count > 1) {
123 ALOGE("Found %zu viewports with type %s, but expected 1 at most",
124 count, viewportTypeToString(type));
125 }
126 return result;
127}
128
129std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
130 uint8_t displayPort) const {
131 for (const DisplayViewport& currentViewport : mDisplays) {
132 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
133 if (physicalPort && (*physicalPort == displayPort)) {
134 return std::make_optional(currentViewport);
Prabir Pradhan29c95332018-11-14 20:14:11 -0800135 }
136 }
137 return std::nullopt;
138}
139
140void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
141 mDisplays = viewports;
142}
143
144void InputReaderConfiguration::dump(std::string& dump) const {
145 for (const DisplayViewport& viewport : mDisplays) {
146 dumpViewport(dump, viewport);
147 }
148}
149
150void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
151 const {
152 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
153}
154
155
156// -- TouchAffineTransformation --
157void TouchAffineTransformation::applyTo(float& x, float& y) const {
158 float newX, newY;
159 newX = x * x_scale + y * x_ymix + x_offset;
160 newY = x * y_xmix + y * y_scale + y_offset;
161
162 x = newX;
163 y = newY;
164}
165
166} // namespace android