summaryrefslogtreecommitdiff
path: root/libkms++/connector.cpp
blob: 0d6d4d753c49885df61d1d0559628e0502eeb9cf (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
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <cassert>

#include "kms++.h"
#include "helpers.h"

using namespace std;

namespace kms
{


static const map<int, string> connector_names = {
#define DEF_CONN(c) { DRM_MODE_CONNECTOR_##c, #c }
	DEF_CONN(Unknown),
	DEF_CONN(VGA),
	DEF_CONN(DVII),
	DEF_CONN(DVID),
	DEF_CONN(DVIA),
	DEF_CONN(Composite),
	DEF_CONN(SVIDEO),
	DEF_CONN(LVDS),
	DEF_CONN(Component),
	DEF_CONN(9PinDIN),
	DEF_CONN(DisplayPort),
	DEF_CONN(HDMIA),
	DEF_CONN(HDMIB),
	DEF_CONN(TV),
	DEF_CONN(eDP),
	DEF_CONN(VIRTUAL),
	DEF_CONN(DSI),
#undef DEF_CONN
};

static const map<int, string> connection_str = {
	{ 0, "<unknown>" },
	{ DRM_MODE_CONNECTED, "Connected" },
	{ DRM_MODE_DISCONNECTED, "Disconnected" },
	{ DRM_MODE_UNKNOWNCONNECTION, "Unknown" },
};

struct ConnectorPriv
{
	drmModeConnectorPtr drm_connector;
};

Connector::Connector(Card &card, uint32_t id, uint32_t idx)
	:DrmObject(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);

	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::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);
}

void Connector::print_short() const
{
	auto c = m_priv->drm_connector;

	printf("Connector %d, %s, %dx%dmm, %s\n", id(), m_fullname.c_str(),
	       c->mmWidth, c->mmHeight, connection_str.at(c->connection).c_str());
}

Videomode Connector::get_default_mode() const
{
	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;

	for (int i = 0; i < c->count_modes; i++)
                if (mode == c->modes[i].name)
                        return drm_mode_to_video_mode(c->modes[i]);

        throw invalid_argument(mode + ": mode not found");
}

bool Connector::connected() const
{
	return m_priv->drm_connector->connection == DRM_MODE_CONNECTED;
}

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;
}

}