Add Connector::get_mode(const std::string &mode), remove const char * variant
[android/external-libkmsxx.git] / libkms++ / connector.cpp
1 #include <stdio.h>
2 #include <iostream>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <cassert>
7 #include "kms++.h"
8 #include "helpers.h"
10 using namespace std;
12 #define DEF_CONN(c) [DRM_MODE_CONNECTOR_##c] = #c
14 namespace kms
15 {
17 static const char * connector_names[] = {
18         DEF_CONN(Unknown),
19         DEF_CONN(VGA),
20         DEF_CONN(DVII),
21         DEF_CONN(DVID),
22         DEF_CONN(DVIA),
23         DEF_CONN(Composite),
24         DEF_CONN(SVIDEO),
25         DEF_CONN(LVDS),
26         DEF_CONN(Component),
27         DEF_CONN(9PinDIN),
28         DEF_CONN(DisplayPort),
29         DEF_CONN(HDMIA),
30         DEF_CONN(HDMIB),
31         DEF_CONN(TV),
32         DEF_CONN(eDP),
33         DEF_CONN(VIRTUAL),
34         DEF_CONN(DSI),
35 };
37 static const char *connection_str[] = {
38         [0] = "<unknown>",
39         [DRM_MODE_CONNECTED] = "Connected",
40         [DRM_MODE_DISCONNECTED] = "Disconnected",
41         [DRM_MODE_UNKNOWNCONNECTION] = "Unknown",
42 };
44 struct ConnectorPriv
45 {
46         drmModeConnectorPtr drm_connector;
47 };
49 Connector::Connector(Card &card, uint32_t id, uint32_t idx)
50         :DrmObject(card, id, DRM_MODE_OBJECT_CONNECTOR, idx)
51 {
52         m_priv = new ConnectorPriv();
54         m_priv->drm_connector = drmModeGetConnector(this->card().fd(), this->id());
55         assert(m_priv->drm_connector);
57         auto name = connector_names[m_priv->drm_connector->connector_type];
58         m_fullname = std::string(string(name) + std::to_string(m_priv->drm_connector->connector_type_id));
59 }
62 Connector::~Connector()
63 {
64         drmModeFreeConnector(m_priv->drm_connector);
65         delete m_priv;
66 }
68 void Connector::setup()
69 {
70         if (m_priv->drm_connector->encoder_id != 0) {
71                 auto enc = card().get_encoder(m_priv->drm_connector->encoder_id);
72                 if (enc)
73                         m_current_crtc = enc->get_crtc();
74         }
75 }
77 void Connector::print_short() const
78 {
79         auto c = m_priv->drm_connector;
81         printf("Connector %d, %s, %dx%dmm, %s\n", id(), m_fullname.c_str(),
82                c->mmWidth, c->mmHeight, connection_str[c->connection]);
83 }
85 Videomode Connector::get_default_mode() const
86 {
87         drmModeModeInfo drmmode = m_priv->drm_connector->modes[0];
89         return drm_mode_to_video_mode(drmmode);
90 }
92 Videomode Connector::get_mode(const string& mode) const
93 {
94         auto c = m_priv->drm_connector;
96         for (int i = 0; i < c->count_modes; i++)
97                 if (mode == c->modes[i].name)
98                         return drm_mode_to_video_mode(c->modes[i]);
100         throw invalid_argument(mode + ": mode not found");
103 bool Connector::connected() const
105         return m_priv->drm_connector->connection == DRM_MODE_CONNECTED;
108 vector<Crtc*> Connector::get_possible_crtcs() const
110         vector<Crtc*> crtcs;
112         for (int i = 0; i < m_priv->drm_connector->count_encoders; ++i) {
113                 auto enc = card().get_encoder(m_priv->drm_connector->encoders[i]);
115                 auto l = enc->get_possible_crtcs();
117                 crtcs.insert(crtcs.end(), l.begin(), l.end());
118         }
120         return crtcs;