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

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

using namespace std;

#define DEF_CONN(c) [DRM_MODE_CONNECTOR_##c] = #c

namespace kms
{

static const char * connector_names[] = {
	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),
};

static const char *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);

	auto name = connector_names[m_priv->drm_connector->connector_type];
	m_fullname = std::string(string(name) + std::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) {
		auto enc = card().get_encoder(m_priv->drm_connector->encoder_id);
		if (enc)
			m_current_crtc = enc->get_crtc();
	}
}

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[c->connection]);
}

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 char *mode) const
{
	auto c = m_priv->drm_connector;
	string sMode(mode);

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

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

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