Update to latest pybind11
[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("planes", [](Card* self) {
28                                 vector<unique_ptr<Plane, py::nodelete>> v;
29                                 for (Plane* p : self->get_planes())
30                                         v.push_back(unique_ptr<Plane, py::nodelete>(p));
31                                 return v;
32                         })
34                         .def_property_readonly("has_atomic", &Card::has_atomic)
35                         .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
36                         ;
38         py::class_<DrmObject, unique_ptr<DrmObject, py::nodelete>>(m, "DrmObject")
39                         .def_property_readonly("id", &DrmObject::id)
40                         .def_property_readonly("idx", &DrmObject::idx)
41                         .def_property_readonly("card", &DrmObject::card)
42                         ;
44         py::class_<DrmPropObject, DrmObject, unique_ptr<DrmPropObject, py::nodelete>>(m, "DrmPropObject")
45                         .def("refresh_props", &DrmPropObject::refresh_props)
46                         .def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
47                         .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value)
48                         .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value)
49                         .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
50                         .def("get_prop", &DrmPropObject::get_prop)
51                         ;
53         py::class_<Connector, DrmPropObject, unique_ptr<Connector, py::nodelete>>(m, "Connector")
54                         .def_property_readonly("fullname", &Connector::fullname)
55                         .def("get_default_mode", &Connector::get_default_mode)
56                         .def("get_current_crtc", &Connector::get_current_crtc)
57                         .def("get_possible_crtcs", &Connector::get_possible_crtcs)
58                         .def("get_modes", &Connector::get_modes)
59                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
60                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
61                         .def("connected", &Connector::connected)
62                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
63                         .def("refresh", &Connector::refresh)
64                         ;
66         py::class_<Crtc, DrmPropObject, unique_ptr<Crtc, py::nodelete>>(m, "Crtc")
67                         .def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
68                         .def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
69                         .def("disable_mode", &Crtc::disable_mode)
70                         .def("page_flip",
71                              [](Crtc* self, Framebuffer& fb, uint32_t data)
72                                 {
73                                         self->page_flip(fb, (void*)(intptr_t)data);
74                                 }, py::arg("fb"), py::arg("data") = 0)
75                         .def("set_plane", &Crtc::set_plane)
76                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
77                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
78                         .def_property_readonly("mode", &Crtc::mode)
79                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
80                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
81                         .def("refresh", &Crtc::refresh)
82                         ;
84         py::class_<Encoder, DrmPropObject, unique_ptr<Encoder, py::nodelete>>(m, "Encoder")
85                         .def("refresh", &Encoder::refresh)
86                         ;
88         py::class_<Plane, DrmPropObject, unique_ptr<Plane, py::nodelete>>(m, "Plane")
89                         .def("supports_crtc", &Plane::supports_crtc)
90                         .def_property_readonly("formats", &Plane::get_formats)
91                         .def_property_readonly("plane_type", &Plane::plane_type)
92                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
93                         ;
95         py::enum_<PlaneType>(m, "PlaneType")
96                         .value("Overlay", PlaneType::Overlay)
97                         .value("Primary", PlaneType::Primary)
98                         .value("Cursor", PlaneType::Cursor)
99                         ;
101         py::class_<Property, DrmObject, unique_ptr<Property, py::nodelete>>(m, "Property")
102                         .def_property_readonly("name", &Property::name)
103                         .def_property_readonly("enums", &Property::get_enums)
104                         ;
106         py::class_<Blob>(m, "Blob")
107                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
108                                 py::buffer_info info = buf.request();
109                                 if (info.ndim != 1)
110                                         throw std::runtime_error("Incompatible buffer dimension!");
112                                 new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
113                         })
114                         .def_property_readonly("data", &Blob::data)
116                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
117                         // and a subclass with standard holder-type.
118                         // So we just copy the DrmObject members here.
119                         // Note that this means that python thinks we don't derive from DrmObject
120                         .def_property_readonly("id", &DrmObject::id)
121                         .def_property_readonly("idx", &DrmObject::idx)
122                         .def_property_readonly("card", &DrmObject::card)
123                         ;
125         py::class_<Framebuffer>(m, "Framebuffer")
126                         .def_property_readonly("width", &Framebuffer::width)
127                         .def_property_readonly("height", &Framebuffer::height)
129                         // XXX pybind11 doesn't support a base object (DrmObject) with custom holder-type,
130                         // and a subclass with standard holder-type.
131                         // So we just copy the DrmObject members here.
132                         // Note that this means that python thinks we don't derive from DrmObject
133                         .def_property_readonly("id", &DrmObject::id)
134                         .def_property_readonly("idx", &DrmObject::idx)
135                         .def_property_readonly("card", &DrmObject::card)
136                         ;
138         py::class_<DumbFramebuffer, Framebuffer>(m, "DumbFramebuffer")
139                         .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
140                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
141                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
142                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
143                         .def_property_readonly("format", &DumbFramebuffer::format)
144                         .def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
145                         .def("fd", &DumbFramebuffer::prime_fd)
146                         .def("stride", &DumbFramebuffer::stride)
147                         .def("offset", &DumbFramebuffer::offset)
148                         ;
150         py::class_<ExtFramebuffer, Framebuffer>(m, "ExtFramebuffer")
151                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
152                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
153                         ;
155         py::enum_<PixelFormat>(m, "PixelFormat")
156                         .value("Undefined", PixelFormat::Undefined)
158                         .value("NV12", PixelFormat::NV12)
159                         .value("NV21", PixelFormat::NV21)
161                         .value("UYVY", PixelFormat::UYVY)
162                         .value("YUYV", PixelFormat::YUYV)
163                         .value("YVYU", PixelFormat::YVYU)
164                         .value("VYUY", PixelFormat::VYUY)
166                         .value("XRGB8888", PixelFormat::XRGB8888)
167                         .value("XBGR8888", PixelFormat::XBGR8888)
168                         .value("ARGB8888", PixelFormat::ARGB8888)
169                         .value("ABGR8888", PixelFormat::ABGR8888)
171                         .value("RGB888", PixelFormat::RGB888)
172                         .value("BGR888", PixelFormat::BGR888)
174                         .value("RGB565", PixelFormat::RGB565)
175                         .value("BGR565", PixelFormat::BGR565)
176                         ;
178         py::enum_<SyncPolarity>(m, "SyncPolarity")
179                         .value("Undefined", SyncPolarity::Undefined)
180                         .value("Positive", SyncPolarity::Positive)
181                         .value("Negative", SyncPolarity::Negative)
182                         ;
184         py::class_<Videomode>(m, "Videomode")
185                         .def(py::init<>())
187                         .def_readwrite("name", &Videomode::name)
189                         .def_readwrite("clock", &Videomode::clock)
191                         .def_readwrite("hdisplay", &Videomode::hdisplay)
192                         .def_readwrite("hsync_start", &Videomode::hsync_start)
193                         .def_readwrite("hsync_end", &Videomode::hsync_end)
194                         .def_readwrite("htotal", &Videomode::htotal)
196                         .def_readwrite("vdisplay", &Videomode::vdisplay)
197                         .def_readwrite("vsync_start", &Videomode::vsync_start)
198                         .def_readwrite("vsync_end", &Videomode::vsync_end)
199                         .def_readwrite("vtotal", &Videomode::vtotal)
201                         .def_readwrite("vrefresh", &Videomode::vrefresh)
203                         .def_readwrite("flags", &Videomode::flags)
204                         .def_readwrite("type", &Videomode::type)
206                         .def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
208                         .def("to_blob", &Videomode::to_blob)
210                         .def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
211                         .def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
212                         ;
215         m.def("videomode_from_timings", &videomode_from_timings);
217         py::class_<AtomicReq>(m, "AtomicReq")
218                         .def(py::init<Card&>(),
219                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
220                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
221                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
222                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
223                         .def("commit",
224                              [](AtomicReq* self, uint32_t data, bool allow)
225                                 {
226                                         return self->commit((void*)(intptr_t)data, allow);
227                                 }, py::arg("data") = 0, py::arg("allow_modeset") = false)
228                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
229                         ;