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 <ftl/NamedEnum.h>
22 #include <gui/constants.h>
23 #include <input/Input.h>
24 
25 #include <cinttypes>
26 #include <optional>
27 
28 using android::base::StringPrintf;
29 
30 namespace android {
31 
32 enum {
33     DISPLAY_ORIENTATION_0 = 0,
34     DISPLAY_ORIENTATION_90 = 1,
35     DISPLAY_ORIENTATION_180 = 2,
36     DISPLAY_ORIENTATION_270 = 3
37 };
38 
39 /**
40  * Describes the different type of viewports supported by input flinger.
41  * Keep in sync with values in InputManagerService.java.
42  */
43 enum class ViewportType : int32_t {
44     INTERNAL = 1,
45     EXTERNAL = 2,
46     VIRTUAL = 3,
47 };
48 
49 /*
50  * Describes how coordinates are mapped on a physical display.
51  * See com.android.server.display.DisplayViewport.
52  */
53 struct DisplayViewport {
54     int32_t displayId; // -1 if invalid
55     int32_t orientation;
56     int32_t logicalLeft;
57     int32_t logicalTop;
58     int32_t logicalRight;
59     int32_t logicalBottom;
60     int32_t physicalLeft;
61     int32_t physicalTop;
62     int32_t physicalRight;
63     int32_t physicalBottom;
64     int32_t deviceWidth;
65     int32_t deviceHeight;
66     bool isActive;
67     std::string uniqueId;
68     // The actual (hardware) port that the associated display is connected to.
69     // Not all viewports will have this specified.
70     std::optional<uint8_t> physicalPort;
71     ViewportType type;
72 
DisplayViewportDisplayViewport73     DisplayViewport()
74           : displayId(ADISPLAY_ID_NONE),
75             orientation(DISPLAY_ORIENTATION_0),
76             logicalLeft(0),
77             logicalTop(0),
78             logicalRight(0),
79             logicalBottom(0),
80             physicalLeft(0),
81             physicalTop(0),
82             physicalRight(0),
83             physicalBottom(0),
84             deviceWidth(0),
85             deviceHeight(0),
86             isActive(false),
87             uniqueId(),
88             physicalPort(std::nullopt),
89             type(ViewportType::INTERNAL) {}
90 
91     bool operator==(const DisplayViewport& other) const {
92         return displayId == other.displayId && orientation == other.orientation &&
93                 logicalLeft == other.logicalLeft && logicalTop == other.logicalTop &&
94                 logicalRight == other.logicalRight && logicalBottom == other.logicalBottom &&
95                 physicalLeft == other.physicalLeft && physicalTop == other.physicalTop &&
96                 physicalRight == other.physicalRight && physicalBottom == other.physicalBottom &&
97                 deviceWidth == other.deviceWidth && deviceHeight == other.deviceHeight &&
98                 isActive == other.isActive && uniqueId == other.uniqueId &&
99                 physicalPort == other.physicalPort && 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         isActive = true;
124         uniqueId.clear();
125         physicalPort = std::nullopt;
126         type = ViewportType::INTERNAL;
127     }
128 
toStringDisplayViewport129     std::string toString() const {
130         return StringPrintf("Viewport %s: displayId=%d, uniqueId=%s, port=%s, orientation=%d, "
131                             "logicalFrame=[%d, %d, %d, %d], "
132                             "physicalFrame=[%d, %d, %d, %d], "
133                             "deviceSize=[%d, %d], "
134                             "isActive=[%d]",
135                             NamedEnum::string(type).c_str(), displayId, uniqueId.c_str(),
136                             physicalPort ? StringPrintf("%" PRIu8, *physicalPort).c_str()
137                                          : "<none>",
138                             orientation, logicalLeft, logicalTop, logicalRight, logicalBottom,
139                             physicalLeft, physicalTop, physicalRight, physicalBottom, deviceWidth,
140                             deviceHeight, isActive);
141     }
142 };
143 
144 } // namespace android
145 
146 #endif // _LIBINPUT_DISPLAY_VIEWPORT_H
147