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 
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 
32 using android::base::StringPrintf;
33 
34 namespace android {
35 
36 // --- InputReaderThread ---
37 
InputReaderThread(const sp<InputReaderInterface> & reader)38 InputReaderThread::InputReaderThread(const sp<InputReaderInterface>& reader) :
39         Thread(/*canCallJava*/ true), mReader(reader) {
40 }
41 
~InputReaderThread()42 InputReaderThread::~InputReaderThread() {
43 }
44 
threadLoop()45 bool InputReaderThread::threadLoop() {
46     mReader->loopOnce();
47     return true;
48 }
49 
50 // --- InputReaderConfiguration ---
51 
getDisplayViewportByUniqueId(const std::string & uniqueDisplayId) const52 std::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;
60     for (const DisplayViewport& currentViewport : mDisplays) {
61         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 
getDisplayViewportByType(ViewportType type) const73 std::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 
getDisplayViewportByPort(uint8_t displayPort) const91 std::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);
97         }
98     }
99     return std::nullopt;
100 }
101 
getDisplayViewportById(int32_t displayId) const102 std::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 
setDisplayViewports(const std::vector<DisplayViewport> & viewports)112 void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
113     mDisplays = viewports;
114 }
115 
dump(std::string & dump) const116 void InputReaderConfiguration::dump(std::string& dump) const {
117     for (const DisplayViewport& viewport : mDisplays) {
118         dumpViewport(dump, viewport);
119     }
120 }
121 
dumpViewport(std::string & dump,const DisplayViewport & viewport) const122 void 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 --
applyTo(float & x,float & y) const129 void 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 
138 } // namespace android
139