blob: 0e2a2e7f9e13b5c2ca96dbcf867a9f41df79605c [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 Vishniakou8158e7e2018-10-15 14:28:20 -070052std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByUniqueId(
53 const std::string& uniqueDisplayId) const {
54 if (uniqueDisplayId.empty()) {
55 ALOGE("Empty string provided to %s", __func__);
56 return std::nullopt;
57 }
58 size_t count = 0;
59 std::optional<DisplayViewport> result = std::nullopt;
Prabir Pradhan29c95332018-11-14 20:14:11 -080060 for (const DisplayViewport& currentViewport : mDisplays) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070061 if (uniqueDisplayId == currentViewport.uniqueId) {
62 result = std::make_optional(currentViewport);
63 count++;
64 }
65 }
66 if (count > 1) {
67 ALOGE("Found %zu viewports with uniqueId %s, but expected 1 at most",
68 count, uniqueDisplayId.c_str());
69 }
70 return result;
71}
72
73std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByType(ViewportType type)
74 const {
75 size_t count = 0;
76 std::optional<DisplayViewport> result = std::nullopt;
77 for (const DisplayViewport& currentViewport : mDisplays) {
78 // Return the first match
79 if (currentViewport.type == type && !result) {
80 result = std::make_optional(currentViewport);
81 count++;
82 }
83 }
84 if (count > 1) {
85 ALOGE("Found %zu viewports with type %s, but expected 1 at most",
86 count, viewportTypeToString(type));
87 }
88 return result;
89}
90
91std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportByPort(
92 uint8_t displayPort) const {
93 for (const DisplayViewport& currentViewport : mDisplays) {
94 const std::optional<uint8_t>& physicalPort = currentViewport.physicalPort;
95 if (physicalPort && (*physicalPort == displayPort)) {
96 return std::make_optional(currentViewport);
Prabir Pradhan29c95332018-11-14 20:14:11 -080097 }
98 }
99 return std::nullopt;
100}
101
Garfield Tan0a080c32020-01-09 11:39:16 -0800102std::optional<DisplayViewport> InputReaderConfiguration::getDisplayViewportById(
103 int32_t displayId) const {
104 for (const DisplayViewport& currentViewport : mDisplays) {
105 if (currentViewport.displayId == displayId) {
106 return std::make_optional(currentViewport);
107 }
108 }
109 return std::nullopt;
110}
111
Prabir Pradhan29c95332018-11-14 20:14:11 -0800112void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
113 mDisplays = viewports;
114}
115
116void InputReaderConfiguration::dump(std::string& dump) const {
117 for (const DisplayViewport& viewport : mDisplays) {
118 dumpViewport(dump, viewport);
119 }
120}
121
122void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
123 const {
124 dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
125}
126
127
128// -- TouchAffineTransformation --
129void TouchAffineTransformation::applyTo(float& x, float& y) const {
130 float newX, newY;
131 newX = x * x_scale + y * x_ymix + x_offset;
132 newY = x * y_xmix + y * y_scale + y_offset;
133
134 x = newX;
135 y = newY;
136}
137
Garfield Tan0a080c32020-01-09 11:39:16 -0800138} // namespace android