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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cassert>
#include <cmath>
#include <kms++/kms++.h>
#include "helpers.h"
using namespace std;
namespace kms
{
#ifndef DRM_MODE_CONNECTOR_DPI
#define DRM_MODE_CONNECTOR_DPI 17
#endif
static const map<int, string> connector_names = {
{ DRM_MODE_CONNECTOR_Unknown, "Unknown" },
{ DRM_MODE_CONNECTOR_VGA, "VGA" },
{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
{ DRM_MODE_CONNECTOR_Composite, "Composite" },
{ DRM_MODE_CONNECTOR_SVIDEO, "S-Video" },
{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
{ DRM_MODE_CONNECTOR_Component, "Component" },
{ DRM_MODE_CONNECTOR_9PinDIN, "9-Pin-DIN" },
{ DRM_MODE_CONNECTOR_DisplayPort, "DP" },
{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
{ DRM_MODE_CONNECTOR_TV, "TV" },
{ DRM_MODE_CONNECTOR_eDP, "eDP" },
{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
{ DRM_MODE_CONNECTOR_DSI, "DSI" },
{ DRM_MODE_CONNECTOR_DPI, "DPI" },
{ DRM_MODE_CONNECTOR_WRITEBACK, "writeback" },
};
static const map<int, string> connection_str = {
{ 0, "<unknown>" },
{ DRM_MODE_CONNECTED, "Connected" },
{ DRM_MODE_DISCONNECTED, "Disconnected" },
{ DRM_MODE_UNKNOWNCONNECTION, "Unknown" },
};
static const map<int, string> subpix_str = {
#define DEF_SUBPIX(c) \
{ \
DRM_MODE_SUBPIXEL_##c, #c \
}
DEF_SUBPIX(UNKNOWN),
DEF_SUBPIX(HORIZONTAL_RGB),
DEF_SUBPIX(HORIZONTAL_BGR),
DEF_SUBPIX(VERTICAL_RGB),
DEF_SUBPIX(VERTICAL_BGR),
DEF_SUBPIX(NONE),
#undef DEF_SUBPIX
};
struct ConnectorPriv {
drmModeConnectorPtr drm_connector;
};
Connector::Connector(Card& card, uint32_t id, uint32_t idx)
: DrmPropObject(card, id, DRM_MODE_OBJECT_CONNECTOR, idx)
{
m_priv = new ConnectorPriv();
m_priv->drm_connector = drmModeGetConnector(this->card().fd(), this->id());
assert(m_priv->drm_connector);
// XXX drmModeGetConnector() does forced probe, which seems to change (at least) EDID blob id.
// XXX So refresh the props again here.
refresh_props();
const auto& name = connector_names.at(m_priv->drm_connector->connector_type);
m_fullname = name + "-" + to_string(m_priv->drm_connector->connector_type_id);
}
Connector::~Connector()
{
drmModeFreeConnector(m_priv->drm_connector);
delete m_priv;
}
void Connector::refresh()
{
drmModeFreeConnector(m_priv->drm_connector);
m_priv->drm_connector = drmModeGetConnector(this->card().fd(), this->id());
assert(m_priv->drm_connector);
// XXX drmModeGetConnector() does forced probe, which seems to change (at least) EDID blob id.
// XXX So refresh the props again here.
refresh_props();
const auto& name = connector_names.at(m_priv->drm_connector->connector_type);
m_fullname = name + "-" + to_string(m_priv->drm_connector->connector_type_id);
}
void Connector::setup()
{
if (m_priv->drm_connector->encoder_id != 0)
m_current_encoder = card().get_encoder(m_priv->drm_connector->encoder_id);
else
m_current_encoder = 0;
if (m_current_encoder)
m_saved_crtc = m_current_encoder->get_crtc();
else
m_saved_crtc = 0;
}
void Connector::restore_mode()
{
if (m_saved_crtc)
m_saved_crtc->restore_mode(this);
}
Videomode Connector::get_default_mode() const
{
if (m_priv->drm_connector->count_modes == 0)
return Videomode();
drmModeModeInfo drmmode = m_priv->drm_connector->modes[0];
return drm_mode_to_video_mode(drmmode);
}
Videomode Connector::get_mode(const string& mode) const
{
auto c = m_priv->drm_connector;
size_t idx = mode.find('@');
string name = idx == string::npos ? mode : mode.substr(0, idx);
float vrefresh = idx == string::npos ? 0.0 : stod(mode.substr(idx + 1));
for (int i = 0; i < c->count_modes; i++) {
Videomode m = drm_mode_to_video_mode(c->modes[i]);
if (m.name != name)
continue;
if (vrefresh && vrefresh != m.calculated_vrefresh())
continue;
return m;
}
throw invalid_argument(mode + ": mode not found");
}
Videomode Connector::get_mode(unsigned xres, unsigned yres, float vrefresh, bool ilace) const
{
auto c = m_priv->drm_connector;
for (int i = 0; i < c->count_modes; i++) {
Videomode m = drm_mode_to_video_mode(c->modes[i]);
if (m.hdisplay != xres || m.vdisplay != yres)
continue;
if (ilace != m.interlace())
continue;
if (vrefresh && vrefresh != m.calculated_vrefresh())
continue;
return m;
}
// If not found, do another round using rounded vrefresh
for (int i = 0; i < c->count_modes; i++) {
Videomode m = drm_mode_to_video_mode(c->modes[i]);
if (m.hdisplay != xres || m.vdisplay != yres)
continue;
if (ilace != m.interlace())
continue;
if (vrefresh && vrefresh != roundf(m.calculated_vrefresh()))
continue;
return m;
}
throw invalid_argument("mode not found");
}
bool Connector::connected() const
{
return m_priv->drm_connector->connection == DRM_MODE_CONNECTED ||
m_priv->drm_connector->connection == DRM_MODE_UNKNOWNCONNECTION;
}
ConnectorStatus Connector::connector_status() const
{
switch (m_priv->drm_connector->connection) {
case DRM_MODE_CONNECTED:
return ConnectorStatus::Connected;
case DRM_MODE_DISCONNECTED:
return ConnectorStatus::Disconnected;
default:
return ConnectorStatus::Unknown;
}
}
vector<Crtc*> Connector::get_possible_crtcs() const
{
vector<Crtc*> crtcs;
for (int i = 0; i < m_priv->drm_connector->count_encoders; ++i) {
auto enc = card().get_encoder(m_priv->drm_connector->encoders[i]);
auto l = enc->get_possible_crtcs();
crtcs.insert(crtcs.end(), l.begin(), l.end());
}
return crtcs;
}
Crtc* Connector::get_current_crtc() const
{
if (m_current_encoder)
return m_current_encoder->get_crtc();
else
return 0;
}
uint32_t Connector::connector_type() const
{
return m_priv->drm_connector->connector_type;
}
uint32_t Connector::connector_type_id() const
{
return m_priv->drm_connector->connector_type_id;
}
uint32_t Connector::mmWidth() const
{
return m_priv->drm_connector->mmWidth;
}
uint32_t Connector::mmHeight() const
{
return m_priv->drm_connector->mmHeight;
}
uint32_t Connector::subpixel() const
{
return m_priv->drm_connector->subpixel;
}
const string& Connector::subpixel_str() const
{
return subpix_str.at(subpixel());
}
std::vector<Videomode> Connector::get_modes() const
{
vector<Videomode> modes;
for (int i = 0; i < m_priv->drm_connector->count_modes; i++)
modes.push_back(drm_mode_to_video_mode(
m_priv->drm_connector->modes[i]));
return modes;
}
std::vector<Encoder*> Connector::get_encoders() const
{
vector<Encoder*> encoders;
for (int i = 0; i < m_priv->drm_connector->count_encoders; i++) {
auto enc = card().get_encoder(m_priv->drm_connector->encoders[i]);
encoders.push_back(enc);
}
return encoders;
}
} // namespace kms
|