1 /*
2  * Copyright (C) 2017 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 #ifndef _LIBINPUT_DISPLAY_VIEWPORT_H
18 #define _LIBINPUT_DISPLAY_VIEWPORT_H
19 
20 #include <android-base/stringprintf.h>
21 #include <ui/DisplayInfo.h>
22 #include <input/Input.h>
23 #include <inttypes.h>
24 #include <optional>
25 
26 using android::base::StringPrintf;
27 
28 namespace android {
29 
30 /**
31  * Describes the different type of viewports supported by input flinger.
32  * Keep in sync with values in InputManagerService.java.
33  */
34 enum class ViewportType : int32_t {
35     VIEWPORT_INTERNAL = 1,
36     VIEWPORT_EXTERNAL = 2,
37     VIEWPORT_VIRTUAL = 3,
38 };
39 
viewportTypeToString(ViewportType type)40 static const char* viewportTypeToString(ViewportType type) {
41     switch(type) {
42         case ViewportType::VIEWPORT_INTERNAL:
43             return "INTERNAL";
44         case ViewportType::VIEWPORT_EXTERNAL:
45             return "EXTERNAL";
46         case ViewportType::VIEWPORT_VIRTUAL:
47             return "VIRTUAL";
48         default:
49             return "UNKNOWN";
50     }
51 }
52 
53 /*
54  * Describes how coordinates are mapped on a physical display.
55  * See com.android.server.display.DisplayViewport.
56  */
57 struct DisplayViewport {
58     int32_t displayId; // -1 if invalid
59     int32_t orientation;
60     int32_t logicalLeft;
61     int32_t logicalTop;
62     int32_t logicalRight;
63     int32_t logicalBottom;
64     int32_t physicalLeft;
65     int32_t physicalTop;
66     int32_t physicalRight;
67     int32_t physicalBottom;
68     int32_t deviceWidth;
69     int32_t deviceHeight;
70     std::string uniqueId;
71     // The actual (hardware) port that the associated display is connected to.
72     // Not all viewports will have this specified.
73     std::optional<uint8_t> physicalPort;
74     ViewportType type;
75 
DisplayViewportDisplayViewport76     DisplayViewport() :
77             displayId(ADISPLAY_ID_NONE), orientation(DISPLAY_ORIENTATION_0),
78             logicalLeft(0), logicalTop(0), logicalRight(0), logicalBottom(0),
79             physicalLeft(0), physicalTop(0), physicalRight(0), physicalBottom(0),
80             deviceWidth(0), deviceHeight(0), uniqueId(), physicalPort(std::nullopt),
81             type(ViewportType::VIEWPORT_INTERNAL) {
82     }
83 
84     bool operator==(const DisplayViewport& other) const {
85         return displayId == other.displayId
86                 && orientation == other.orientation
87                 && logicalLeft == other.logicalLeft
88                 && logicalTop == other.logicalTop
89                 && logicalRight == other.logicalRight
90                 && logicalBottom == other.logicalBottom
91                 && physicalLeft == other.physicalLeft
92                 && physicalTop == other.physicalTop
93                 && physicalRight == other.physicalRight
94                 && physicalBottom == other.physicalBottom
95                 && deviceWidth == other.deviceWidth
96                 && deviceHeight == other.deviceHeight
97                 && uniqueId == other.uniqueId
98                 && physicalPort == other.physicalPort
99                 && type == other.type;
100     }
101 
102     bool operator!=(const DisplayViewport& other) const {
103         return !(*this == other);
104     }
105 
isValidDisplayViewport106     inline bool isValid() const {
107         return displayId >= 0;
108     }
109 
setNonDisplayViewportDisplayViewport110     void setNonDisplayViewport(int32_t width, int32_t height) {
111         displayId = ADISPLAY_ID_NONE;
112         orientation = DISPLAY_ORIENTATION_0;
113         logicalLeft = 0;
114         logicalTop = 0;
115         logicalRight = width;
116         logicalBottom = height;
117         physicalLeft = 0;
118         physicalTop = 0;
119         physicalRight = width;
120         physicalBottom = height;
121         deviceWidth = width;
122         deviceHeight = height;
123         uniqueId.clear();
124         physicalPort = std::nullopt;
125         type = ViewportType::VIEWPORT_INTERNAL;
126     }
127 
toStringDisplayViewport128     std::string toString() const {
129         return StringPrintf("Viewport %s: displayId=%d, uniqueId=%s, port=%s, orientation=%d, "
130             "logicalFrame=[%d, %d, %d, %d], "
131             "physicalFrame=[%d, %d, %d, %d], "
132             "deviceSize=[%d, %d]",
133             viewportTypeToString(type), displayId,
134             uniqueId.c_str(),
135             physicalPort ? StringPrintf("%" PRIu8, *physicalPort).c_str() : "<none>",
136             orientation,
137             logicalLeft, logicalTop,
138             logicalRight, logicalBottom,
139             physicalLeft, physicalTop,
140             physicalRight, physicalBottom,
141             deviceWidth, deviceHeight);
142     }
143 };
144 
145 } // namespace android
146 
147 #endif // _LIBINPUT_DISPLAY_VIEWPORT_H
148