There is no need to convert exception strings to .c_str().
[android/external-libkmsxx.git] / libkms++ / card.cpp
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <utility>
5 #include <stdexcept>
6 #include <string.h>
7 #include <algorithm>
9 #include <xf86drm.h>
10 #include <xf86drmMode.h>
12 #include "kms++.h"
14 #ifndef DRM_CLIENT_CAP_ATOMIC
15 #define DRM_CLIENT_CAP_ATOMIC 3
16 #endif
18 using namespace std;
20 namespace kms
21 {
23 Card::Card()
24 {
25         const char *card = "/dev/dri/card0";
27         int fd = open(card, O_RDWR | O_CLOEXEC);
28         if (fd < 0)
29                 throw invalid_argument(string(strerror(errno)) + " opening " +
30                                        card);
31         m_fd = fd;
33         int r;
35         r = drmSetMaster(fd);
36         m_master = r == 0;
38         r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
39         if (r)
40                 throw invalid_argument("Can't set universal planes");
42         r = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ATOMIC, 1);
43         m_has_atomic = r == 0;
45         uint64_t has_dumb;
46         r = drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb);
47         if (r || !has_dumb)
48                 throw invalid_argument("Dumb buffers not available");
50         auto res = drmModeGetResources(m_fd);
51         if (!res)
52                 throw invalid_argument("Can't get card resources");
54         for (int i = 0; i < res->count_connectors; ++i) {
55                 uint32_t id = res->connectors[i];
56                 m_obmap[id] = new Connector(*this, id, i);
57         }
59         for (int i = 0; i < res->count_crtcs; ++i) {
60                 uint32_t id = res->crtcs[i];
61                 m_obmap[id] = new Crtc(*this, id, i);
62         }
64         for (int i = 0; i < res->count_encoders; ++i) {
65                 uint32_t id = res->encoders[i];
66                 m_obmap[id] = new Encoder(*this, id);
67         }
69         drmModeFreeResources(res);
71         auto planeRes = drmModeGetPlaneResources(m_fd);
73         for (uint i = 0; i < planeRes->count_planes; ++i) {
74                 uint32_t id = planeRes->planes[i];
75                 m_obmap[id] = new Plane(*this, id);
76         }
78         drmModeFreePlaneResources(planeRes);
80         // collect all possible props
81         for (auto ob : get_objects()) {
82                 auto props = drmModeObjectGetProperties(m_fd, ob->id(), ob->object_type());
84                 if (props == nullptr)
85                         continue;
87                 for (unsigned i = 0; i < props->count_props; ++i) {
88                         uint32_t prop_id = props->props[i];
90                         if (m_obmap.find(prop_id) == m_obmap.end())
91                                 m_obmap[prop_id] = new Property(*this, prop_id);
92                 }
94                 drmModeFreeObjectProperties(props);
95         }
97         for (auto pair : m_obmap)
98                 pair.second->setup();
99 }
101 Card::~Card()
103         for (auto pair : m_obmap)
104                 delete pair.second;
106         close(m_fd);
109 template <class T> static void print_obs(const map<uint32_t, DrmObject*>& obmap)
111         for (auto pair : obmap) {
112                 auto ob = pair.second;
113                 if (dynamic_cast<T*>(ob)) {
114                         ob->print_short();
115                         //ob->print_props();
116                 }
117         }
120 void Card::print_short() const
122         print_obs<Connector>(m_obmap);
123         print_obs<Encoder>(m_obmap);
124         print_obs<Crtc>(m_obmap);
125         print_obs<Plane>(m_obmap);
128 Property* Card::get_prop(const char *name) const
130         for (auto pair : m_obmap) {
131                 auto prop = dynamic_cast<Property*>(pair.second);
132                 if (!prop)
133                         continue;
135                 if (strcmp(name, prop->name()) == 0)
136                         return prop;
137         }
139         throw invalid_argument(string("Card property ") + name + " not found");
142 Connector* Card::get_first_connected_connector() const
144         for(auto pair : m_obmap) {
145                 auto c = dynamic_cast<Connector*>(pair.second);
147                 if (c && c->connected())
148                         return c;
149         }
151         throw invalid_argument("no connected connectors");
154 DrmObject* Card::get_object(uint32_t id) const
156         return m_obmap.at(id);
159 vector<Connector*> Card::get_connectors() const
161         vector<Connector*> v;
162         for(auto pair : m_obmap) {
163                 auto p = dynamic_cast<Connector*>(pair.second);
164                 if (p)
165                         v.push_back(p);
166         }
167         return v;
170 vector<Plane*> Card::get_planes() const
172         vector<Plane*> v;
173         for(auto pair : m_obmap) {
174                 auto p = dynamic_cast<Plane*>(pair.second);
175                 if (p)
176                         v.push_back(p);
177         }
178         return v;
181 vector<DrmObject*> Card::get_objects() const
183         vector<DrmObject*> v;
184         for(auto pair : m_obmap)
185                 v.push_back(pair.second);
186         return v;
189 Crtc* Card::get_crtc_by_index(uint32_t idx) const
191         for(auto pair : m_obmap) {
192                 auto crtc = dynamic_cast<Crtc*>(pair.second);
193                 if (crtc && crtc->idx() == idx)
194                         return crtc;
195         }
196         throw invalid_argument(string("Crtc #") + to_string(idx) + "not found");
199 Crtc* Card::get_crtc(uint32_t id) const { return dynamic_cast<Crtc*>(get_object(id)); }
200 Encoder* Card::get_encoder(uint32_t id) const { return dynamic_cast<Encoder*>(get_object(id)); }
201 Property* Card::get_prop(uint32_t id) const { return dynamic_cast<Property*>(get_object(id)); }
203 std::vector<kms::Pipeline> Card::get_connected_pipelines()
205         vector<Pipeline> outputs;
207         for (auto conn : get_connectors())
208         {
209                 if (conn->connected() == false)
210                         continue;
212                 Crtc* crtc = conn->get_current_crtc();
214                 if (!crtc) {
215                         for (auto possible : conn->get_possible_crtcs()) {
216                                 if (find_if(outputs.begin(), outputs.end(), [possible](Pipeline out) { return out.crtc == possible; }) == outputs.end()) {
217                                         crtc = possible;
218                                         break;
219                                 }
220                         }
221                 }
223                 if (!crtc)
224                         throw invalid_argument(string("Connector #") +
225                                                to_string(conn->idx()) +
226                                                " has no possible crtcs");
228                 outputs.push_back(Pipeline { crtc, conn });
229         }
231         return outputs;