py: Add Card crtcs and encoders properties back to the Python bindings
[android/external-libkmsxx.git] / py / pykms / pykmsbase.cpp
1 #include <pybind11/pybind11.h>
2 #include <pybind11/stl.h>
3 #include <kms++/kms++.h>
5 namespace py = pybind11;
7 using namespace kms;
8 using namespace std;
10 void init_pykmsbase(py::module &m)
11 {
12         py::class_<Card>(m, "Card")
13                         .def(py::init<>())
14                         .def_property_readonly("fd", &Card::fd)
15                         .def_property_readonly("get_first_connected_connector", &Card::get_first_connected_connector)
17                         // XXX pybind11 can't handle vector<T*> where T is non-copyable, and complains:
18                         // RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
19                         // So we do this manually.
20                         .def_property_readonly("connectors", [](Card* self) {
21                                 vector<unique_ptr<Connector, py::nodelete>> v;
22                                 for (Connector* p : self->get_connectors())
23                                         v.push_back(unique_ptr<Connector, py::nodelete>(p));
24                                 return v;
25                         })
27                         .def_property_readonly("crtcs", [](Card* self) {
28                                 vector<unique_ptr<Crtc, py::nodelete>> v;
29                                 for (Crtc* p : self->get_crtcs())
30                                         v.push_back(unique_ptr<Crtc, py::nodelete>(p));
31                                 return v;
32                         })
34                         .def_property_readonly("encoders", [](Card* self) {
35                                 vector<unique_ptr<Encoder, py::nodelete>> v;
36                                 for (Encoder* p : self->get_encoders())
37                                         v.push_back(unique_ptr<Encoder, py::nodelete>(p));
38                                 return v;
39                         })
41                         .def_property_readonly("planes", [](Card* self) {
42                                 vector<unique_ptr<Plane, py::nodelete>> v;
43                                 for (Plane* p : self->get_planes())
44                                         v.push_back(unique_ptr<Plane, py::nodelete>(p));
45                                 return v;
46                         })
48                         .def_property_readonly("has_atomic", &Card::has_atomic)
49                         .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
50                         ;
52         py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
53                         .def_property_readonly("id", &DrmObject::id)
54                         .def_property_readonly("idx", &DrmObject::idx)
55                         .def_property_readonly("card", &DrmObject::card)
56                         ;
58         py::class_<DrmPropObject, DrmObject, unique_ptr<DrmPropObject, py::nodelete>>(m, "DrmPropObject")
59                         .def("refresh_props", &DrmPropObject::refresh_props)
60                         .def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
61                         .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value)
62                         .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value)
63                         .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
64                         .def("get_prop", &DrmPropObject::get_prop)
65                         ;
67         py::class_<Connector, DrmPropObject, unique_ptr<Connector, py::nodelete>>(m, "Connector")
68                         .def_property_readonly("fullname", &Connector::fullname)
69                         .def("get_default_mode", &Connector::get_default_mode)
70                         .def("get_current_crtc", &Connector::get_current_crtc)
71                         .def("get_possible_crtcs", [](Connector* self) {
72                                 vector<unique_ptr<Crtc, py::nodelete>> v;
73                                 for (Crtc* p : self->get_possible_crtcs())
74                                         v.push_back(unique_ptr<Crtc, py::nodelete>(p));
75                                 return v;
76                         })
77                         .def("get_modes", &Connector::get_modes)
78                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
79                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
80                         .def("connected", &Connector::connected)
81                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
82                         .def("refresh", &Connector::refresh)
83                         ;
85         py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
86                         .def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
87                         .def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
88                         .def("disable_mode", &Crtc::disable_mode)
89                         .def("page_flip",
90                              [](Crtc* self, Framebuffer& fb, uint32_t data)
91                                 {
92                                         self->page_flip(fb, (void*)(intptr_t)data);
93                                 }, py::arg("fb"), py::arg("data") = 0)
94                         .def("set_plane", &Crtc::set_plane)
95                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
96                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
97                         .def_property_readonly("mode", &Crtc::mode)
98                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
99                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
100                         .def("refresh", &Crtc::refresh)
101                         ;
103         py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
104                         .def("refresh", &Encoder::refresh)
105                         ;
107         py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
108                         .def("supports_crtc", &Plane::supports_crtc)
109                         .def_property_readonly("formats", &Plane::get_formats)
110                         .def_property_readonly("plane_type", &Plane::plane_type)
111                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
112                         ;
114         py::enum_<PlaneType>(m, "PlaneType")
115                         .value("Overlay", PlaneType::Overlay)
116                         .value("Primary", PlaneType::Primary)
117                         .value("Cursor", PlaneType::Cursor)
118                         ;
120         py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
121                         .def_property_readonly("name", &Property::name)
122                         .def_property_readonly("enums", &Property::get_enums)
123                         ;
125         py::class_<Blob>(m, "Blob")
126                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
127                                         py::buffer_info info = buf.request();
128                                         if (info.ndim != 1)
129                                                 throw std::runtime_error("Incompatible buffer dimension!");
131                                         new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
132                                 },
133                                 py::keep_alive<1, 3>()) // Keep Card alive until this is destructed
135                         .def_property_readonly("data", &Blob::data)
137                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
138                         // and a subclass with standard holder-type.
139                         // So we just copy the DrmObject members here.
140                         // Note that this means that python thinks we don't derive from DrmObject
141                         .def_property_readonly("id", &DrmObject::id)
142                         .def_property_readonly("idx", &DrmObject::idx)
143                         .def_property_readonly("card", &DrmObject::card)
144                         ;
146         py::class_<Framebuffer>(m, "Framebuffer")
147                         .def_property_readonly("width", &Framebuffer::width)
148                         .def_property_readonly("height", &Framebuffer::height)
150                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
151                         // and a subclass with standard holder-type.
152                         // So we just copy the DrmObject members here.
153                         // Note that this means that python thinks we don't derive from DrmObject
154                         .def_property_readonly("id", &DrmObject::id)
155                         .def_property_readonly("idx", &DrmObject::idx)
156                         .def_property_readonly("card", &DrmObject::card)
157                         ;
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                         .def_property_readonly("format", &DumbFramebuffer::format)
165                         .def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
166                         .def("fd", &DumbFramebuffer::prime_fd)
167                         .def("stride", &DumbFramebuffer::stride)
168                         .def("offset", &DumbFramebuffer::offset)
169                         ;
171         py::class_<ExtFramebuffer, Framebuffer>(m, "ExtFramebuffer")
172                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
173                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
174                         ;
176         py::enum_<PixelFormat>(m, "PixelFormat")
177                         .value("Undefined", PixelFormat::Undefined)
179                         .value("NV12", PixelFormat::NV12)
180                         .value("NV21", PixelFormat::NV21)
182                         .value("UYVY", PixelFormat::UYVY)
183                         .value("YUYV", PixelFormat::YUYV)
184                         .value("YVYU", PixelFormat::YVYU)
185                         .value("VYUY", PixelFormat::VYUY)
187                         .value("XRGB8888", PixelFormat::XRGB8888)
188                         .value("XBGR8888", PixelFormat::XBGR8888)
189                         .value("ARGB8888", PixelFormat::ARGB8888)
190                         .value("ABGR8888", PixelFormat::ABGR8888)
192                         .value("RGB888", PixelFormat::RGB888)
193                         .value("BGR888", PixelFormat::BGR888)
195                         .value("RGB565", PixelFormat::RGB565)
196                         .value("BGR565", PixelFormat::BGR565)
197                         ;
199         py::enum_<SyncPolarity>(m, "SyncPolarity")
200                         .value("Undefined", SyncPolarity::Undefined)
201                         .value("Positive", SyncPolarity::Positive)
202                         .value("Negative", SyncPolarity::Negative)
203                         ;
205         py::class_<Videomode>(m, "Videomode")
206                         .def(py::init<>())
208                         .def_readwrite("name", &Videomode::name)
210                         .def_readwrite("clock", &Videomode::clock)
212                         .def_readwrite("hdisplay", &Videomode::hdisplay)
213                         .def_readwrite("hsync_start", &Videomode::hsync_start)
214                         .def_readwrite("hsync_end", &Videomode::hsync_end)
215                         .def_readwrite("htotal", &Videomode::htotal)
217                         .def_readwrite("vdisplay", &Videomode::vdisplay)
218                         .def_readwrite("vsync_start", &Videomode::vsync_start)
219                         .def_readwrite("vsync_end", &Videomode::vsync_end)
220                         .def_readwrite("vtotal", &Videomode::vtotal)
222                         .def_readwrite("vrefresh", &Videomode::vrefresh)
224                         .def_readwrite("flags", &Videomode::flags)
225                         .def_readwrite("type", &Videomode::type)
227                         .def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
229                         .def("to_blob", &Videomode::to_blob)
231                         .def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
232                         .def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
233                         ;
236         m.def("videomode_from_timings", &videomode_from_timings);
238         py::class_<AtomicReq>(m, "AtomicReq")
239                         .def(py::init<Card&>(),
240                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
241                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
242                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
243                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
244                         .def("commit",
245                              [](AtomicReq* self, uint32_t data, bool allow)
246                                 {
247                                         return self->commit((void*)(intptr_t)data, allow);
248                                 }, py::arg("data") = 0, py::arg("allow_modeset") = false)
249                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
250                         ;