• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
setDisplayViewports(const std::vector<DisplayViewport> & viewports)102 void InputReaderConfiguration::setDisplayViewports(const std::vector<DisplayViewport>& viewports) {
103     mDisplays = viewports;
104 }
105 
dump(std::string & dump) const106 void InputReaderConfiguration::dump(std::string& dump) const {
107     for (const DisplayViewport& viewport : mDisplays) {
108         dumpViewport(dump, viewport);
109     }
110 }
111 
dumpViewport(std::string & dump,const DisplayViewport & viewport) const112 void InputReaderConfiguration::dumpViewport(std::string& dump, const DisplayViewport& viewport)
113         const {
114     dump += StringPrintf(INDENT4 "%s\n", viewport.toString().c_str());
115 }
116 
117 
118 // -- TouchAffineTransformation --
applyTo(float & x,float & y) const119 void TouchAffineTransformation::applyTo(float& x, float& y) const {
120     float newX, newY;
121     newX = x * x_scale + y * x_ymix + x_offset;
122     newY = x * y_xmix + y * y_scale + y_offset;
123 
124     x = newX;
125     y = newY;
126 }
127 
128 } // namespace android