summaryrefslogtreecommitdiff
path: root/include/input/DisplayViewport.h
blob: 2427a075a17da22f24a73a9ec32ad3d431a226dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _LIBINPUT_DISPLAY_VIEWPORT_H
#define _LIBINPUT_DISPLAY_VIEWPORT_H

#include <cinttypes>
#include <optional>

#include <android-base/stringprintf.h>
#include <input/Input.h>

using android::base::StringPrintf;

namespace android {

enum {
    DISPLAY_ORIENTATION_0 = 0,
    DISPLAY_ORIENTATION_90 = 1,
    DISPLAY_ORIENTATION_180 = 2,
    DISPLAY_ORIENTATION_270 = 3
};

/**
 * Describes the different type of viewports supported by input flinger.
 * Keep in sync with values in InputManagerService.java.
 */
enum class ViewportType : int32_t {
    VIEWPORT_INTERNAL = 1,
    VIEWPORT_EXTERNAL = 2,
    VIEWPORT_VIRTUAL = 3,
};

static const char* viewportTypeToString(ViewportType type) {
    switch(type) {
        case ViewportType::VIEWPORT_INTERNAL:
            return "INTERNAL";
        case ViewportType::VIEWPORT_EXTERNAL:
            return "EXTERNAL";
        case ViewportType::VIEWPORT_VIRTUAL:
            return "VIRTUAL";
        default:
            return "UNKNOWN";
    }
}

/*
 * Describes how coordinates are mapped on a physical display.
 * See com.android.server.display.DisplayViewport.
 */
struct DisplayViewport {
    int32_t displayId; // -1 if invalid
    int32_t orientation;
    int32_t logicalLeft;
    int32_t logicalTop;
    int32_t logicalRight;
    int32_t logicalBottom;
    int32_t physicalLeft;
    int32_t physicalTop;
    int32_t physicalRight;
    int32_t physicalBottom;
    int32_t deviceWidth;
    int32_t deviceHeight;
    bool isActive;
    std::string uniqueId;
    // The actual (hardware) port that the associated display is connected to.
    // Not all viewports will have this specified.
    std::optional<uint8_t> physicalPort;
    ViewportType type;

    DisplayViewport()
          : displayId(ADISPLAY_ID_NONE),
            orientation(DISPLAY_ORIENTATION_0),
            logicalLeft(0),
            logicalTop(0),
            logicalRight(0),
            logicalBottom(0),
            physicalLeft(0),
            physicalTop(0),
            physicalRight(0),
            physicalBottom(0),
            deviceWidth(0),
            deviceHeight(0),
            isActive(false),
            uniqueId(),
            physicalPort(std::nullopt),
            type(ViewportType::VIEWPORT_INTERNAL) {}

    bool operator==(const DisplayViewport& other) const {
        return displayId == other.displayId && orientation == other.orientation &&
                logicalLeft == other.logicalLeft && logicalTop == other.logicalTop &&
                logicalRight == other.logicalRight && logicalBottom == other.logicalBottom &&
                physicalLeft == other.physicalLeft && physicalTop == other.physicalTop &&
                physicalRight == other.physicalRight && physicalBottom == other.physicalBottom &&
                deviceWidth == other.deviceWidth && deviceHeight == other.deviceHeight &&
                isActive == other.isActive && uniqueId == other.uniqueId &&
                physicalPort == other.physicalPort && type == other.type;
    }

    bool operator!=(const DisplayViewport& other) const {
        return !(*this == other);
    }

    inline bool isValid() const {
        return displayId >= 0;
    }

    void setNonDisplayViewport(int32_t width, int32_t height) {
        displayId = ADISPLAY_ID_NONE;
        orientation = DISPLAY_ORIENTATION_0;
        logicalLeft = 0;
        logicalTop = 0;
        logicalRight = width;
        logicalBottom = height;
        physicalLeft = 0;
        physicalTop = 0;
        physicalRight = width;
        physicalBottom = height;
        deviceWidth = width;
        deviceHeight = height;
        isActive = false;
        uniqueId.clear();
        physicalPort = std::nullopt;
        type = ViewportType::VIEWPORT_INTERNAL;
    }

    std::string toString() const {
        return StringPrintf("Viewport %s: displayId=%d, uniqueId=%s, port=%s, orientation=%d, "
                            "logicalFrame=[%d, %d, %d, %d], "
                            "physicalFrame=[%d, %d, %d, %d], "
                            "deviceSize=[%d, %d], "
                            "isActive=[%d]",
                            viewportTypeToString(type), displayId, uniqueId.c_str(),
                            physicalPort ? StringPrintf("%" PRIu8, *physicalPort).c_str()
                                         : "<none>",
                            orientation, logicalLeft, logicalTop, logicalRight, logicalBottom,
                            physicalLeft, physicalTop, physicalRight, physicalBottom, deviceWidth,
                            deviceHeight, isActive);
    }
};

} // namespace android

#endif // _LIBINPUT_DISPLAY_VIEWPORT_H