• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("get_first_connected_connector", &Card::get_first_connected_connector)
28 
29 			// XXX pybind11 can't handle vector<T*> where T is non-copyable, and complains:
30 			// RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
31 			// So we do this manually.
32 			.def_property_readonly("connectors", [](Card* self) {
33 				return convert_vector(self->get_connectors());
34 			})
35 
36 			.def_property_readonly("crtcs", [](Card* self) {
37 				return convert_vector(self->get_crtcs());
38 			})
39 
40 			.def_property_readonly("encoders", [](Card* self) {
41 				return convert_vector(self->get_encoders());
42 			})
43 
44 			.def_property_readonly("planes", [](Card* self) {
45 				return convert_vector(self->get_planes());
46 			})
47 
48 			.def_property_readonly("has_atomic", &Card::has_atomic)
49 			.def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
50 
51 			.def_property_readonly("version_name", &Card::version_name);
52 			;
53 
54 	py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
55 			.def_property_readonly("id", &DrmObject::id)
56 			.def_property_readonly("idx", &DrmObject::idx)
57 			.def_property_readonly("card", &DrmObject::card)
58 			;
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 			;
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 
84 	py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
85 			.def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
86 			.def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
87 			.def("disable_mode", &Crtc::disable_mode)
88 			.def("page_flip",
89 			     [](Crtc* self, Framebuffer& fb, uint32_t data)
90 				{
91 					self->page_flip(fb, (void*)(intptr_t)data);
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 			;
101 
102 	py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
103 			.def("refresh", &Encoder::refresh)
104 			;
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 
113 	py::enum_<PlaneType>(m, "PlaneType")
114 			.value("Overlay", PlaneType::Overlay)
115 			.value("Primary", PlaneType::Primary)
116 			.value("Cursor", PlaneType::Cursor)
117 			;
118 
119 	py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
120 			.def_property_readonly("name", &Property::name)
121 			.def_property_readonly("enums", &Property::get_enums)
122 			;
123 
124 	py::class_<Blob>(m, "Blob")
125 			.def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
126 					py::buffer_info info = buf.request();
127 					if (info.ndim != 1)
128 						throw std::runtime_error("Incompatible buffer dimension!");
129 
130 					new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
131 				},
132 				py::keep_alive<1, 3>())	// Keep Card alive until this is destructed
133 
134 			.def_property_readonly("data", &Blob::data)
135 
136 			// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
137 			// and a subclass with standard holder-type.
138 			// So we just copy the DrmObject members here.
139 			// Note that this means that python thinks we don't derive from DrmObject
140 			.def_property_readonly("id", &DrmObject::id)
141 			.def_property_readonly("idx", &DrmObject::idx)
142 			.def_property_readonly("card", &DrmObject::card)
143 			;
144 
145 	py::class_<Framebuffer>(m, "Framebuffer")
146 			.def_property_readonly("width", &Framebuffer::width)
147 			.def_property_readonly("height", &Framebuffer::height)
148 
149 			// XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
150 			// and a subclass with standard holder-type.
151 			// So we just copy the DrmObject members here.
152 			// Note that this means that python thinks we don't derive from DrmObject
153 			.def_property_readonly("id", &DrmObject::id)
154 			.def_property_readonly("idx", &DrmObject::idx)
155 			.def_property_readonly("card", &DrmObject::card)
156 			;
157 
158 	py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
159 			.def(py::init<Card&, uint32_t, uint32_t, const string&>(),
160 			     py::keep_alive<1, 2>())	// Keep Card alive until this is destructed
161 			.def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
162 			     py::keep_alive<1, 2>())	// Keep Card alive until this is destructed
163 			.def_property_readonly("format", &DumbFramebuffer::format)
164 			.def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
165 			.def("fd", &DumbFramebuffer::prime_fd)
166 			.def("stride", &DumbFramebuffer::stride)
167 			.def("offset", &DumbFramebuffer::offset)
168 			;
169 
170 	py::class_<ExtFramebuffer, Framebuffer>(m, "ExtFramebuffer")
171 			.def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
172 			     py::keep_alive<1, 2>())	// Keep Card alive until this is destructed
173 			;
174 
175 	py::enum_<PixelFormat>(m, "PixelFormat")
176 			.value("Undefined", PixelFormat::Undefined)
177 
178 			.value("NV12", PixelFormat::NV12)
179 			.value("NV21", PixelFormat::NV21)
180 
181 			.value("UYVY", PixelFormat::UYVY)
182 			.value("YUYV", PixelFormat::YUYV)
183 			.value("YVYU", PixelFormat::YVYU)
184 			.value("VYUY", PixelFormat::VYUY)
185 
186 			.value("XRGB8888", PixelFormat::XRGB8888)
187 			.value("XBGR8888", PixelFormat::XBGR8888)
188 			.value("ARGB8888", PixelFormat::ARGB8888)
189 			.value("ABGR8888", PixelFormat::ABGR8888)
190 
191 			.value("RGB888", PixelFormat::RGB888)
192 			.value("BGR888", PixelFormat::BGR888)
193 
194 			.value("RGB565", PixelFormat::RGB565)
195 			.value("BGR565", PixelFormat::BGR565)
196 			;
197 
198 	py::enum_<SyncPolarity>(m, "SyncPolarity")
199 			.value("Undefined", SyncPolarity::Undefined)
200 			.value("Positive", SyncPolarity::Positive)
201 			.value("Negative", SyncPolarity::Negative)
202 			;
203 
204 	py::class_<Videomode>(m, "Videomode")
205 			.def(py::init<>())
206 
207 			.def_readwrite("name", &Videomode::name)
208 
209 			.def_readwrite("clock", &Videomode::clock)
210 
211 			.def_readwrite("hdisplay", &Videomode::hdisplay)
212 			.def_readwrite("hsync_start", &Videomode::hsync_start)
213 			.def_readwrite("hsync_end", &Videomode::hsync_end)
214 			.def_readwrite("htotal", &Videomode::htotal)
215 
216 			.def_readwrite("vdisplay", &Videomode::vdisplay)
217 			.def_readwrite("vsync_start", &Videomode::vsync_start)
218 			.def_readwrite("vsync_end", &Videomode::vsync_end)
219 			.def_readwrite("vtotal", &Videomode::vtotal)
220 
221 			.def_readwrite("vrefresh", &Videomode::vrefresh)
222 
223 			.def_readwrite("flags", &Videomode::flags)
224 			.def_readwrite("type", &Videomode::type)
225 
226 			.def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
227 
228 			.def("to_blob", &Videomode::to_blob)
229 
230 			.def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
231 			.def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
232 			;
233 
234 
235 	m.def("videomode_from_timings", &videomode_from_timings);
236 
237 	py::class_<AtomicReq>(m, "AtomicReq")
238 			.def(py::init<Card&>(),
239 			     py::keep_alive<1, 2>())	// Keep Card alive until this is destructed
240 			.def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
241 			.def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
242 			.def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
243 			.def("commit",
244 			     [](AtomicReq* self, uint32_t data, bool allow)
245 				{
246 					return self->commit((void*)(intptr_t)data, allow);
247 				}, py::arg("data") = 0, py::arg("allow_modeset") = false)
248 			.def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
249 			;
250 }
251