blob: 334fe348e7fda292bcc3249f9f07849e0b6e084a [file] [log] [blame]
Santos Cordonfa5cf462017-04-05 10:37:00 -07001/*
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
Dominik Laskowski718f9602019-11-09 20:01:35 -080020#include <android-base/stringprintf.h>
21#include <input/Input.h>
22
Michael Wrightfe3de7d2020-07-02 19:05:30 +010023#include <cinttypes>
24#include <optional>
25
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010026using android::base::StringPrintf;
27
Santos Cordonfa5cf462017-04-05 10:37:00 -070028namespace android {
29
Dominik Laskowski718f9602019-11-09 20:01:35 -080030enum {
31 DISPLAY_ORIENTATION_0 = 0,
32 DISPLAY_ORIENTATION_90 = 1,
33 DISPLAY_ORIENTATION_180 = 2,
34 DISPLAY_ORIENTATION_270 = 3
35};
36
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010037/**
38 * Describes the different type of viewports supported by input flinger.
39 * Keep in sync with values in InputManagerService.java.
40 */
41enum class ViewportType : int32_t {
Michael Wrightfe3de7d2020-07-02 19:05:30 +010042 INTERNAL = 1,
43 EXTERNAL = 2,
44 VIRTUAL = 3,
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010045};
46
47static const char* viewportTypeToString(ViewportType type) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +010048 switch (type) {
49 case ViewportType::INTERNAL:
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010050 return "INTERNAL";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010051 case ViewportType::EXTERNAL:
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010052 return "EXTERNAL";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010053 case ViewportType::VIRTUAL:
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010054 return "VIRTUAL";
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010055 }
Michael Wrightfe3de7d2020-07-02 19:05:30 +010056 return "UNKNOWN";
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010057}
58
Santos Cordonfa5cf462017-04-05 10:37:00 -070059/*
60 * Describes how coordinates are mapped on a physical display.
61 * See com.android.server.display.DisplayViewport.
62 */
63struct DisplayViewport {
64 int32_t displayId; // -1 if invalid
65 int32_t orientation;
66 int32_t logicalLeft;
67 int32_t logicalTop;
68 int32_t logicalRight;
69 int32_t logicalBottom;
70 int32_t physicalLeft;
71 int32_t physicalTop;
72 int32_t physicalRight;
73 int32_t physicalBottom;
74 int32_t deviceWidth;
75 int32_t deviceHeight;
Chris Ye13b16852020-04-25 10:58:45 -070076 bool isActive;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +010077 std::string uniqueId;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070078 // The actual (hardware) port that the associated display is connected to.
79 // Not all viewports will have this specified.
80 std::optional<uint8_t> physicalPort;
Siarhei Vishniakoud6343922018-07-06 23:33:37 +010081 ViewportType type;
Santos Cordonfa5cf462017-04-05 10:37:00 -070082
Chris Ye13b16852020-04-25 10:58:45 -070083 DisplayViewport()
84 : displayId(ADISPLAY_ID_NONE),
85 orientation(DISPLAY_ORIENTATION_0),
86 logicalLeft(0),
87 logicalTop(0),
88 logicalRight(0),
89 logicalBottom(0),
90 physicalLeft(0),
91 physicalTop(0),
92 physicalRight(0),
93 physicalBottom(0),
94 deviceWidth(0),
95 deviceHeight(0),
96 isActive(false),
97 uniqueId(),
98 physicalPort(std::nullopt),
Michael Wrightfe3de7d2020-07-02 19:05:30 +010099 type(ViewportType::INTERNAL) {}
Santos Cordonfa5cf462017-04-05 10:37:00 -0700100
101 bool operator==(const DisplayViewport& other) const {
Chris Ye13b16852020-04-25 10:58:45 -0700102 return displayId == other.displayId && orientation == other.orientation &&
103 logicalLeft == other.logicalLeft && logicalTop == other.logicalTop &&
104 logicalRight == other.logicalRight && logicalBottom == other.logicalBottom &&
105 physicalLeft == other.physicalLeft && physicalTop == other.physicalTop &&
106 physicalRight == other.physicalRight && physicalBottom == other.physicalBottom &&
107 deviceWidth == other.deviceWidth && deviceHeight == other.deviceHeight &&
108 isActive == other.isActive && uniqueId == other.uniqueId &&
109 physicalPort == other.physicalPort && type == other.type;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700110 }
111
112 bool operator!=(const DisplayViewport& other) const {
113 return !(*this == other);
114 }
115
116 inline bool isValid() const {
117 return displayId >= 0;
118 }
119
120 void setNonDisplayViewport(int32_t width, int32_t height) {
121 displayId = ADISPLAY_ID_NONE;
122 orientation = DISPLAY_ORIENTATION_0;
123 logicalLeft = 0;
124 logicalTop = 0;
125 logicalRight = width;
126 logicalBottom = height;
127 physicalLeft = 0;
128 physicalTop = 0;
129 physicalRight = width;
130 physicalBottom = height;
131 deviceWidth = width;
132 deviceHeight = height;
Chris Ye13b16852020-04-25 10:58:45 -0700133 isActive = false;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700134 uniqueId.clear();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700135 physicalPort = std::nullopt;
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100136 type = ViewportType::INTERNAL;
Santos Cordonfa5cf462017-04-05 10:37:00 -0700137 }
Santos Cordonfa5cf462017-04-05 10:37:00 -0700138
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100139 std::string toString() const {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700140 return StringPrintf("Viewport %s: displayId=%d, uniqueId=%s, port=%s, orientation=%d, "
Chris Ye13b16852020-04-25 10:58:45 -0700141 "logicalFrame=[%d, %d, %d, %d], "
142 "physicalFrame=[%d, %d, %d, %d], "
143 "deviceSize=[%d, %d], "
144 "isActive=[%d]",
145 viewportTypeToString(type), displayId, uniqueId.c_str(),
146 physicalPort ? StringPrintf("%" PRIu8, *physicalPort).c_str()
147 : "<none>",
148 orientation, logicalLeft, logicalTop, logicalRight, logicalBottom,
149 physicalLeft, physicalTop, physicalRight, physicalBottom, deviceWidth,
150 deviceHeight, isActive);
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100151 }
Santos Cordonfa5cf462017-04-05 10:37:00 -0700152};
153
154} // namespace android
155
156#endif // _LIBINPUT_DISPLAY_VIEWPORT_H