blob: 0422d8342bab5659ed6ade421b0cfdd0369fb2c2 [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
Prabir Pradhan7d384732019-09-25 15:07:32 -0700117 if (currentViewport.type == type) {
118 if (!result) {
119 result = std::make_optional(currentViewport);
120 }
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700121 count++;
122 }
123 }
124 if (count > 1) {
125 ALOGE("Found %zu viewports with type %s, but expected 1 at most",
126 count, viewportTypeToString(type));
127 }
128 return result;
129}
130
131std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
132 uint8_t displayPort) const {
133 for (const DisplayViewport& currentViewport : mDisplays) {
134 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
135 if (physicalPort && (*physicalPort == displayPort)) {
136 return std::make_optional(currentViewport);
Prabir Pradhan29c95332018-11-14 20:14:11 -0800137 }
138 }
139 return std::nullopt;
140}
141
142void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
143 mDisplays = viewports;
144}
145
146void InputReaderConfiguration::dump(std::string& dump) const {
147 for (const DisplayViewport& viewport : mDisplays) {
148 dumpViewport(dump, viewport);
149 }
150}
151
152void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
153 const {
154 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
155}
156
157
158// -- TouchAffineTransformation --
159void TouchAffineTransformation::applyTo(float& x, float& y) const {
160 float newX, newY;
161 newX = x * x_scale + y * x_ymix + x_offset;
162 newY = x * y_xmix + y * y_scale + y_offset;
163
164 x = newX;
165 y = newY;
166}
167
168} // namespace android