cdeb02aa166e65967d2bb10a6284c9d6069f10f7
[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 namespace kms
13 {
16 static const map<int, string> connector_names = {
17         { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
18         { DRM_MODE_CONNECTOR_VGA, "VGA" },
19         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
20         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
21         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
22         { DRM_MODE_CONNECTOR_Composite, "Composite" },
23         { DRM_MODE_CONNECTOR_SVIDEO, "S-Video" },
24         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
25         { DRM_MODE_CONNECTOR_Component, "Component" },
26         { DRM_MODE_CONNECTOR_9PinDIN, "9-Pin-DIN" },
27         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
28         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
29         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
30         { DRM_MODE_CONNECTOR_TV, "TV" },
31         { DRM_MODE_CONNECTOR_eDP, "eDP" },
32         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
33         { DRM_MODE_CONNECTOR_DSI, "DSI" },
34 };
36 static const map<int, string> connection_str = {
37         { 0, "<unknown>" },
38         { DRM_MODE_CONNECTED, "Connected" },
39         { DRM_MODE_DISCONNECTED, "Disconnected" },
40         { DRM_MODE_UNKNOWNCONNECTION, "Unknown" },
41 };
43 static const map<int, string> subpix_str = {
44 #define DEF_SUBPIX(c) { DRM_MODE_SUBPIXEL_##c, #c }
45         DEF_SUBPIX(UNKNOWN),
46         DEF_SUBPIX(HORIZONTAL_RGB),
47         DEF_SUBPIX(HORIZONTAL_BGR),
48         DEF_SUBPIX(VERTICAL_RGB),
49         DEF_SUBPIX(VERTICAL_BGR),
50         DEF_SUBPIX(NONE),
51 #undef DEF_SUBPIX
52 };
54 struct ConnectorPriv
55 {
56         drmModeConnectorPtr drm_connector;
57 };
59 Connector::Connector(Card &card, uint32_t id, uint32_t idx)
60         :DrmObject(card, id, DRM_MODE_OBJECT_CONNECTOR, idx)
61 {
62         m_priv = new ConnectorPriv();
64         m_priv->drm_connector = drmModeGetConnector(this->card().fd(), this->id());
65         assert(m_priv->drm_connector);
67         const auto& name = connector_names.at(m_priv->drm_connector->connector_type);
68         m_fullname = name + "-" + to_string(m_priv->drm_connector->connector_type_id);
69 }
72 Connector::~Connector()
73 {
74         drmModeFreeConnector(m_priv->drm_connector);
75         delete m_priv;
76 }
78 void Connector::setup()
79 {
80         if (m_priv->drm_connector->encoder_id != 0)
81                 m_current_encoder = card().get_encoder(m_priv->drm_connector->encoder_id);
82         else
83                 m_current_encoder = 0;
85         if (m_current_encoder)
86                 m_saved_crtc = m_current_encoder->get_crtc();
87         else
88                 m_saved_crtc = 0;
89 }
91 void Connector::restore_mode()
92 {
93         if (m_saved_crtc)
94                 m_saved_crtc->restore_mode(this);
95 }
97 Videomode Connector::get_default_mode() const
98 {
99         drmModeModeInfo drmmode = m_priv->drm_connector->modes[0];
101         return drm_mode_to_video_mode(drmmode);
104 Videomode Connector::get_mode(const string& mode) const
106         auto c = m_priv->drm_connector;
108         for (int i = 0; i < c->count_modes; i++)
109                 if (mode == c->modes[i].name)
110                         return drm_mode_to_video_mode(c->modes[i]);
112         throw invalid_argument(mode + ": mode not found");
115 Videomode Connector::get_mode(unsigned xres, unsigned yres, unsigned refresh) const
117         auto c = m_priv->drm_connector;
119         for (int i = 0; i < c->count_modes; i++) {
120                 drmModeModeInfo& m = c->modes[i];
122                 if (m.hdisplay != xres || m.vdisplay != yres)
123                         continue;
125                 if (refresh && m.vrefresh != refresh)
126                         continue;
128                 return drm_mode_to_video_mode(c->modes[i]);
129         }
131         throw invalid_argument("mode not found");
134 bool Connector::connected() const
136         return m_priv->drm_connector->connection == DRM_MODE_CONNECTED ||
137                         m_priv->drm_connector->connection == DRM_MODE_UNKNOWNCONNECTION;
140 vector<Crtc*> Connector::get_possible_crtcs() const
142         vector<Crtc*> crtcs;
144         for (int i = 0; i < m_priv->drm_connector->count_encoders; ++i) {
145                 auto enc = card().get_encoder(m_priv->drm_connector->encoders[i]);
147                 auto l = enc->get_possible_crtcs();
149                 crtcs.insert(crtcs.end(), l.begin(), l.end());
150         }
152         return crtcs;
155 Crtc* Connector::get_current_crtc() const
157         if (m_current_encoder)
158                 return m_current_encoder->get_crtc();
159         else
160                 return 0;
163 uint32_t Connector::connector_type() const
165         return m_priv->drm_connector->connector_type;
168 uint32_t Connector::connector_type_id() const
170         return m_priv->drm_connector->connector_type_id;
173 uint32_t Connector::mmWidth() const
175         return m_priv->drm_connector->mmWidth;
178 uint32_t Connector::mmHeight() const
180         return m_priv->drm_connector->mmHeight;
183 uint32_t Connector::subpixel() const
185         return m_priv->drm_connector->subpixel;
188 const string& Connector::subpixel_str() const
190         return subpix_str.at(subpixel());
193 std::vector<Videomode> Connector::get_modes() const
195         vector<Videomode> modes;
197         for (int i = 0; i < m_priv->drm_connector->count_modes; i++)
198                 modes.push_back(drm_mode_to_video_mode(
199                                         m_priv->drm_connector->modes[i]));
201         return modes;
204 std::vector<Encoder*> Connector::get_encoders() const
206         vector<Encoder*> encoders;
208         for (int i = 0; i < m_priv->drm_connector->count_encoders; i++) {
209                 auto enc = card().get_encoder(m_priv->drm_connector->encoders[i]);
210                 encoders.push_back(enc);
211         }
212         return encoders;