• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/enum.h>
22 #include <ftl/string.h>
23 #include <gui/constants.h>
24 #include <input/Input.h>
25 
26 #include <cinttypes>
27 #include <optional>
28 
29 using android::base::StringPrintf;
30 
31 namespace android {
32 
33 enum {
34     DISPLAY_ORIENTATION_0 = 0,
35     DISPLAY_ORIENTATION_90 = 1,
36     DISPLAY_ORIENTATION_180 = 2,
37     DISPLAY_ORIENTATION_270 = 3
38 };
39 
40 /**
41  * Describes the different type of viewports supported by input flinger.
42  * Keep in sync with values in InputManagerService.java.
43  */
44 enum class ViewportType : int32_t {
45     INTERNAL = 1,
46     EXTERNAL = 2,
47     VIRTUAL = 3,
48 
49     ftl_last = VIRTUAL
50 };
51 
52 /*
53  * Describes how coordinates are mapped on a physical display.
54  * See com.android.server.display.DisplayViewport.
55  */
56 struct DisplayViewport {
57     int32_t displayId; // -1 if invalid
58     int32_t orientation;
59     int32_t logicalLeft;
60     int32_t logicalTop;
61     int32_t logicalRight;
62     int32_t logicalBottom;
63     int32_t physicalLeft;
64     int32_t physicalTop;
65     int32_t physicalRight;
66     int32_t physicalBottom;
67     int32_t deviceWidth;
68     int32_t deviceHeight;
69     bool isActive;
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),
78             orientation(DISPLAY_ORIENTATION_0),
79             logicalLeft(0),
80             logicalTop(0),
81             logicalRight(0),
82             logicalBottom(0),
83             physicalLeft(0),
84             physicalTop(0),
85             physicalRight(0),
86             physicalBottom(0),
87             deviceWidth(0),
88             deviceHeight(0),
89             isActive(false),
90             uniqueId(),
91             physicalPort(std::nullopt),
92             type(ViewportType::INTERNAL) {}
93 
94     bool operator==(const DisplayViewport& other) const {
95         return displayId == other.displayId && orientation == other.orientation &&
96                 logicalLeft == other.logicalLeft && logicalTop == other.logicalTop &&
97                 logicalRight == other.logicalRight && logicalBottom == other.logicalBottom &&
98                 physicalLeft == other.physicalLeft && physicalTop == other.physicalTop &&
99                 physicalRight == other.physicalRight && physicalBottom == other.physicalBottom &&
100                 deviceWidth == other.deviceWidth && deviceHeight == other.deviceHeight &&
101                 isActive == other.isActive && uniqueId == other.uniqueId &&
102                 physicalPort == other.physicalPort && type == other.type;
103     }
104 
105     bool operator!=(const DisplayViewport& other) const {
106         return !(*this == other);
107     }
108 
isValidDisplayViewport109     inline bool isValid() const {
110         return displayId >= 0;
111     }
112 
setNonDisplayViewportDisplayViewport113     void setNonDisplayViewport(int32_t width, int32_t height) {
114         displayId = ADISPLAY_ID_NONE;
115         orientation = DISPLAY_ORIENTATION_0;
116         logicalLeft = 0;
117         logicalTop = 0;
118         logicalRight = width;
119         logicalBottom = height;
120         physicalLeft = 0;
121         physicalTop = 0;
122         physicalRight = width;
123         physicalBottom = height;
124         deviceWidth = width;
125         deviceHeight = height;
126         isActive = true;
127         uniqueId.clear();
128         physicalPort = std::nullopt;
129         type = ViewportType::INTERNAL;
130     }
131 
toStringDisplayViewport132     std::string toString() const {
133         return StringPrintf("Viewport %s: displayId=%d, uniqueId=%s, port=%s, orientation=%d, "
134                             "logicalFrame=[%d, %d, %d, %d], "
135                             "physicalFrame=[%d, %d, %d, %d], "
136                             "deviceSize=[%d, %d], "
137                             "isActive=[%d]",
138                             ftl::enum_string(type).c_str(), displayId, uniqueId.c_str(),
139                             physicalPort ? ftl::to_string(*physicalPort).c_str() : "<none>",
140                             orientation, logicalLeft, logicalTop, logicalRight, logicalBottom,
141                             physicalLeft, physicalTop, physicalRight, physicalBottom, deviceWidth,
142                             deviceHeight, isActive);
143     }
144 };
145 
146 } // namespace android
147 
148 #endif // _LIBINPUT_DISPLAY_VIEWPORT_H
149