1 #include <pybind11/pybind11.h>
2 #include <pybind11/stl.h>
3 #include <kms++/kms++.h>
4 
5 namespace py = pybind11;
6 
7 using namespace kms;
8 using namespace std;
9 
10 // Helper to convert vector<T*> to vector<unique_ptr<T, py::nodelete>>
11 template<typename T>
convert_vector(const vector<T * > & source)12 static vector<unique_ptr<T, py::nodelete>> convert_vector(const vector<T*>& source)
13 {
14 	vector<unique_ptr<T, py::nodelete>> v;
15 	for (T* p : source)
16 		v.push_back(unique_ptr<T, py::nodelete>(p));
17 	return v;
18 }
19 
init_pykmsbase(py::module & m)20 void init_pykmsbase(py::module& m)
21 {
22 	py::class_<Card>(m, "Card")
23 		.def(py::init<>())
24 		.def(py::init<const string&>())
25 		.def(py::init<const string&, uint32_t>())
26 		.def_property_readonly("fd", &Card::fd)
27 		.def_property_readonly("minor", &Card::dev_minor)
28 		.def_property_readonly("get_first_connected_connector", &Card::get_first_connected_connector)
29 
30 		// XXX pybind11 can't handle vector<T*> where T is non-copyable, and complains:
31 		// RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
32 		// So we do this manually.
33 		.def_property_readonly("connectors", [](Card* self) {
34 			return convert_vector(self->get_connectors());
35 		})
36 
37 		.def_property_readonly("crtcs", [](Card* self) {
38 			return convert_vector(self->get_crtcs());
39 		})
40 
41 		.def_property_readonly("encoders", [](Card* self) {
42 			return convert_vector(self->get_encoders());
43 		})
44 
45 		.def_property_readonly("planes", [](Card* self) {
46 			return convert_vector(self->get_planes());
47 		})
48 
49 		.def_property_readonly("has_atomic", &Card::has_atomic)
50 		.def("get_prop", (Property * (Card::*)(uint32_t) const) & Card::get_prop)
51 
52 		.def_property_readonly("version_name", &Card::version_name);
53 	;
54 
55 	py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
56 		.def_property_readonly("id", &DrmObject::id)
57 		.def_property_readonly("idx", &DrmObject::idx)
58 		.def_property_readonly("card", &DrmObject::card);
59 
60 	py::class_<DrmPropObject, DrmObject, unique_ptr<DrmPropObject, py::nodelete>>(m, "DrmPropObject")
61 		.def("refresh_props", &DrmPropObject::refresh_props)
62 		.def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
63 		.def("get_prop_value", (uint64_t(DrmPropObject::*)(const string&) const) & DrmPropObject::get_prop_value)
64 		.def("set_prop_value", (int (DrmPropObject::*)(const string&, uint64_t)) & DrmPropObject::set_prop_value)
65 		.def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
66 		.def("get_prop", &DrmPropObject::get_prop)
67 		.def("has_prop", &DrmPropObject::has_prop);
68 
69 	py::class_<Connector, DrmPropObject, unique_ptr<Connector, py::nodelete>>(m, "Connector")
70 		.def_property_readonly("fullname", &Connector::fullname)
71 		.def("get_default_mode", &Connector::get_default_mode)
72 		.def("get_current_crtc", &Connector::get_current_crtc)
73 		.def("get_possible_crtcs", [](Connector* self) {
74 			return convert_vector(self->get_possible_crtcs());
75 		})
76 		.def("get_modes", &Connector::get_modes)
77 		.def("get_mode", (Videomode(Connector::*)(const string& mode) const) & Connector::get_mode)
78 		.def("get_mode", (Videomode(Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const) & Connector::get_mode)
79 		.def("connected", &Connector::connected)
80 		.def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
81 		.def("refresh", &Connector::refresh);
82 
83 	py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
84 		.def("set_mode", (int (Crtc::*)(Connector*, const Videomode&)) & Crtc::set_mode)
85 		.def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&)) & Crtc::set_mode)
86 		.def("disable_mode", &Crtc::disable_mode)
87 		.def(
88 			"page_flip",
89 			[](Crtc* self, Framebuffer& fb, uint32_t data) {
90 				self->page_flip(fb, (void*)(intptr_t)data);
91 			},
92 			py::arg("fb"), py::arg("data") = 0)
93 		.def("set_plane", &Crtc::set_plane)
94 		.def_property_readonly("possible_planes", &Crtc::get_possible_planes)
95 		.def_property_readonly("primary_plane", &Crtc::get_primary_plane)
96 		.def_property_readonly("mode", &Crtc::mode)
97 		.def_property_readonly("mode_valid", &Crtc::mode_valid)
98 		.def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
99 		.def("refresh", &Crtc::refresh)
100 		.def("legacy_gamma_size", &Crtc::legacy_gamma_size)
101 		.def("legacy_gamma_set", &Crtc::legacy_gamma_set);
102 
103 	py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
104 		.def("refresh", &Encoder::refresh);
105 
106 	py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
107 		.def("supports_crtc", &Plane::supports_crtc)
108 		.def_property_readonly("formats", &Plane::get_formats)
109 		.def_property_readonly("plane_type", &Plane::plane_type)
110 		.def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; });
111 
112 	py::enum_<PlaneType>(m, "PlaneType")
113 		.value("Overlay", PlaneType::Overlay)
114 		.value("Primary", PlaneType::Primary)
115 		.value("Cursor", PlaneType::Cursor);
116 
117 	py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
118 		.def_property_readonly("name", &Property::name)
119 		.def_property_readonly("enums", &Property::get_enums);
120 
121 	py::class_<Blob>(m, "Blob")
122 		.def(py::init([](Card& card, py::buffer buf) {
123 			     py::buffer_info info = buf.request();
124 			     if (info.ndim != 1)
125 				     throw std::runtime_error("Incompatible buffer dimension!");
126 
127 			     return new Blob(card, info.ptr, info.size * info.itemsize);
128 		     }),
129 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
130 
131 		.def_property_readonly("data", &Blob::data)
132 
133 		// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
134 		// and a subclass with standard holder-type.
135 		// So we just copy the DrmObject members here.
136 		// Note that this means that python thinks we don't derive from DrmObject
137 		.def_property_readonly("id", &DrmObject::id)
138 		.def_property_readonly("idx", &DrmObject::idx)
139 		.def_property_readonly("card", &DrmObject::card);
140 
141 	py::class_<Framebuffer>(m, "Framebuffer")
142 		.def_property_readonly("width", &Framebuffer::width)
143 		.def_property_readonly("height", &Framebuffer::height)
144 		.def_property_readonly("format", &Framebuffer::format)
145 		.def_property_readonly("num_planes", &Framebuffer::num_planes)
146 		.def("stride", &Framebuffer::stride)
147 		.def("size", &Framebuffer::size)
148 		.def("offset", &Framebuffer::offset)
149 		.def("fd", &Framebuffer::prime_fd)
150 
151 		// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
152 		// and a subclass with standard holder-type.
153 		// So we just copy the DrmObject members here.
154 		// Note that this means that python thinks we don't derive from DrmObject
155 		.def_property_readonly("id", &DrmObject::id)
156 		.def_property_readonly("idx", &DrmObject::idx)
157 		.def_property_readonly("card", &DrmObject::card);
158 
159 	py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
160 		.def(py::init<Card&, uint32_t, uint32_t, const string&>(),
161 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
162 		.def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
163 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
164 		;
165 
166 	py::class_<DmabufFramebuffer, Framebuffer>(m, "DmabufFramebuffer")
167 		.def(py::init<Card&, uint32_t, uint32_t, const string&, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
168 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
169 		.def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
170 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
171 		;
172 
173 	py::enum_<PixelFormat>(m, "PixelFormat")
174 		.value("Undefined", PixelFormat::Undefined)
175 
176 		.value("NV12", PixelFormat::NV12)
177 		.value("NV21", PixelFormat::NV21)
178 		.value("NV16", PixelFormat::NV16)
179 		.value("NV61", PixelFormat::NV61)
180 
181 		.value("YUV420", PixelFormat::YUV420)
182 		.value("YVU420", PixelFormat::YVU420)
183 		.value("YUV422", PixelFormat::YUV422)
184 		.value("YVU422", PixelFormat::YVU422)
185 		.value("YUV444", PixelFormat::YUV444)
186 		.value("YVU444", PixelFormat::YVU444)
187 
188 		.value("UYVY", PixelFormat::UYVY)
189 		.value("YUYV", PixelFormat::YUYV)
190 		.value("YVYU", PixelFormat::YVYU)
191 		.value("VYUY", PixelFormat::VYUY)
192 
193 		.value("XRGB8888", PixelFormat::XRGB8888)
194 		.value("XBGR8888", PixelFormat::XBGR8888)
195 		.value("RGBX8888", PixelFormat::RGBX8888)
196 		.value("BGRX8888", PixelFormat::BGRX8888)
197 
198 		.value("ARGB8888", PixelFormat::ARGB8888)
199 		.value("ABGR8888", PixelFormat::ABGR8888)
200 		.value("RGBA8888", PixelFormat::RGBA8888)
201 		.value("BGRA8888", PixelFormat::BGRA8888)
202 
203 		.value("RGB888", PixelFormat::RGB888)
204 		.value("BGR888", PixelFormat::BGR888)
205 
206 		.value("RGB332", PixelFormat::RGB332)
207 
208 		.value("RGB565", PixelFormat::RGB565)
209 		.value("BGR565", PixelFormat::BGR565)
210 
211 		.value("XRGB4444", PixelFormat::XRGB4444)
212 		.value("XRGB1555", PixelFormat::XRGB1555)
213 
214 		.value("ARGB4444", PixelFormat::ARGB4444)
215 		.value("ARGB1555", PixelFormat::ARGB1555)
216 
217 		.value("XRGB2101010", PixelFormat::XRGB2101010)
218 		.value("XBGR2101010", PixelFormat::XBGR2101010)
219 		.value("RGBX1010102", PixelFormat::RGBX1010102)
220 		.value("BGRX1010102", PixelFormat::BGRX1010102)
221 
222 		.value("ARGB2101010", PixelFormat::ARGB2101010)
223 		.value("ABGR2101010", PixelFormat::ABGR2101010)
224 		.value("RGBA1010102", PixelFormat::RGBA1010102)
225 		.value("BGRA1010102", PixelFormat::BGRA1010102);
226 
227 	py::enum_<SyncPolarity>(m, "SyncPolarity")
228 		.value("Undefined", SyncPolarity::Undefined)
229 		.value("Positive", SyncPolarity::Positive)
230 		.value("Negative", SyncPolarity::Negative);
231 
232 	py::class_<Videomode>(m, "Videomode")
233 		.def(py::init<>())
234 
235 		.def_readwrite("name", &Videomode::name)
236 
237 		.def_readwrite("clock", &Videomode::clock)
238 
239 		.def_readwrite("hdisplay", &Videomode::hdisplay)
240 		.def_readwrite("hsync_start", &Videomode::hsync_start)
241 		.def_readwrite("hsync_end", &Videomode::hsync_end)
242 		.def_readwrite("htotal", &Videomode::htotal)
243 
244 		.def_readwrite("vdisplay", &Videomode::vdisplay)
245 		.def_readwrite("vsync_start", &Videomode::vsync_start)
246 		.def_readwrite("vsync_end", &Videomode::vsync_end)
247 		.def_readwrite("vtotal", &Videomode::vtotal)
248 
249 		.def_readwrite("vrefresh", &Videomode::vrefresh)
250 
251 		.def_readwrite("flags", &Videomode::flags)
252 		.def_readwrite("type", &Videomode::type)
253 
254 		.def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
255 
256 		.def("to_blob", &Videomode::to_blob)
257 
258 		.def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
259 		.def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
260 
261 		.def("to_string_short", &Videomode::to_string_short)
262 		.def("to_string_long", &Videomode::to_string_long);
263 
264 	m.def("videomode_from_timings", &videomode_from_timings);
265 
266 	py::class_<AtomicReq>(m, "AtomicReq")
267 		.def(py::init<Card&>(),
268 		     py::keep_alive<1, 2>()) // Keep Card alive until this is destructed
269 		.def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) & AtomicReq::add)
270 		.def("add", (void (AtomicReq::*)(DrmPropObject*, Property*, uint64_t)) & AtomicReq::add)
271 		.def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) & AtomicReq::add)
272 		.def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
273 		.def(
274 			"commit",
275 			[](AtomicReq* self, uint32_t data, bool allow) {
276 				return self->commit((void*)(intptr_t)data, allow);
277 			},
278 			py::arg("data") = 0, py::arg("allow_modeset") = false)
279 		.def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false);
280 }
281