1 #include <stdio.h>
2 #include <iostream>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <cassert>
6 #include <xf86drm.h>
7 #include <xf86drmMode.h>
8 
9 #include <kms++/kms++.h>
10 
11 using namespace std;
12 
13 namespace kms
14 {
15 
16 struct EncoderPriv
17 {
18 	drmModeEncoderPtr drm_encoder;
19 };
20 
21 static const map<int, string> encoder_types = {
22 #define DEF_ENC(c) { DRM_MODE_ENCODER_##c, #c }
23 	DEF_ENC(NONE),
24 	DEF_ENC(DAC),
25 	DEF_ENC(TMDS),
26 	DEF_ENC(LVDS),
27 	DEF_ENC(TVDAC),
28 	DEF_ENC(VIRTUAL),
29 	DEF_ENC(DSI),
30 	{ 7, "DPMST" },
31 #undef DEF_ENC
32 };
33 
Encoder(Card & card,uint32_t id,uint32_t idx)34 Encoder::Encoder(Card &card, uint32_t id, uint32_t idx)
35 	:DrmPropObject(card, id, DRM_MODE_OBJECT_ENCODER, idx)
36 {
37 	m_priv = new EncoderPriv();
38 	m_priv->drm_encoder = drmModeGetEncoder(this->card().fd(), this->id());
39 	assert(m_priv->drm_encoder);
40 }
41 
~Encoder()42 Encoder::~Encoder()
43 {
44 	drmModeFreeEncoder(m_priv->drm_encoder);
45 	delete m_priv;
46 }
47 
refresh()48 void Encoder::refresh()
49 {
50 	drmModeFreeEncoder(m_priv->drm_encoder);
51 
52 	m_priv->drm_encoder = drmModeGetEncoder(this->card().fd(), this->id());
53 	assert(m_priv->drm_encoder);
54 }
55 
get_crtc() const56 Crtc* Encoder::get_crtc() const
57 {
58 	if (m_priv->drm_encoder->crtc_id)
59 		return card().get_crtc(m_priv->drm_encoder->crtc_id);
60 	else
61 		return 0;
62 }
63 
get_possible_crtcs() const64 vector<Crtc*> Encoder::get_possible_crtcs() const
65 {
66 	unsigned bits = m_priv->drm_encoder->possible_crtcs;
67 	vector<Crtc*> crtcs;
68 
69 	for (int idx = 0; bits; idx++, bits >>= 1) {
70 		if ((bits & 1) == 0)
71 			continue;
72 
73 		auto crtc = card().get_crtcs()[idx];
74 		crtcs.push_back(crtc);
75 	}
76 
77 	return crtcs;
78 }
79 
get_encoder_type() const80 const string& Encoder::get_encoder_type() const
81 {
82 	return encoder_types.at(m_priv->drm_encoder->encoder_type);
83 }
84 
85 }
86