fe4bc463ab699e899c05f1a49b8ff328b5ced4e9
[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("get_first_connected_connector", &Card::get_first_connected_connector)
16                         .def_property_readonly("connectors", &Card::get_connectors)
17                         .def_property_readonly("crtcs", &Card::get_crtcs)
18                         .def_property_readonly("encoders", &Card::get_encoders)
19                         .def_property_readonly("planes", &Card::get_planes)
20                         .def_property_readonly("has_atomic", &Card::has_atomic)
21                         .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
22                         ;
24         py::class_<DrmObject, DrmObject*>(m, "DrmObject")
25                         .def_property_readonly("id", &DrmObject::id)
26                         .def_property_readonly("idx", &DrmObject::idx)
27                         .def_property_readonly("card", &DrmObject::card)
28                         ;
30         py::class_<DrmPropObject, DrmPropObject*>(m, "DrmPropObject", py::base<DrmObject>())
31                         .def("refresh_props", &DrmPropObject::refresh_props)
32                         .def_property_readonly("prop_map", &DrmPropObject::get_prop_map)
33                         .def("get_prop_value", (uint64_t (DrmPropObject::*)(const string&) const)&DrmPropObject::get_prop_value)
34                         .def("set_prop_value",(int (DrmPropObject::*)(const string&, uint64_t)) &DrmPropObject::set_prop_value)
35                         .def("get_prop_value_as_blob", &DrmPropObject::get_prop_value_as_blob)
36                         .def("get_prop", &DrmPropObject::get_prop)
37                         ;
39         py::class_<Connector, Connector*>(m, "Connector",  py::base<DrmPropObject>())
40                         .def_property_readonly("fullname", &Connector::fullname)
41                         .def("get_default_mode", &Connector::get_default_mode)
42                         .def("get_current_crtc", &Connector::get_current_crtc)
43                         .def("get_possible_crtcs", &Connector::get_possible_crtcs)
44                         .def("get_modes", &Connector::get_modes)
45                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
46                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
47                         .def("connected", &Connector::connected)
48                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
49                         .def("refresh", &Connector::refresh)
50                         ;
52         py::class_<Crtc, Crtc*>(m, "Crtc",  py::base<DrmPropObject>())
53                         .def("set_mode", (int (Crtc::*)(Connector*, const Videomode&))&Crtc::set_mode)
54                         .def("set_mode", (int (Crtc::*)(Connector*, Framebuffer&, const Videomode&))&Crtc::set_mode)
55                         .def("disable_mode", &Crtc::disable_mode)
56                         .def("page_flip",
57                              [](Crtc* self, Framebuffer& fb, uint32_t data)
58                                 {
59                                         self->page_flip(fb, (void*)(intptr_t)data);
60                                 }, py::arg("fb"), py::arg("data") = 0)
61                         .def("set_plane", &Crtc::set_plane)
62                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
63                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
64                         .def_property_readonly("mode", &Crtc::mode)
65                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
66                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
67                         .def("refresh", &Crtc::refresh)
68                         ;
70         py::class_<Encoder, Encoder*>(m, "Encoder",  py::base<DrmPropObject>())
71                         .def("refresh", &Encoder::refresh)
72                         ;
74         py::class_<Plane, Plane*>(m, "Plane",  py::base<DrmPropObject>())
75                         .def("supports_crtc", &Plane::supports_crtc)
76                         .def_property_readonly("formats", &Plane::get_formats)
77                         .def_property_readonly("plane_type", &Plane::plane_type)
78                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
79                         ;
81         py::enum_<PlaneType>(m, "PlaneType")
82                         .value("Overlay", PlaneType::Overlay)
83                         .value("Primary", PlaneType::Primary)
84                         .value("Cursor", PlaneType::Cursor)
85                         ;
87         py::class_<Property, Property*>(m, "Property",  py::base<DrmObject>())
88                         .def_property_readonly("name", &Property::name)
89                         .def_property_readonly("enums", &Property::get_enums)
90                         ;
92         py::class_<Blob>(m, "Blob", py::base<DrmObject>())
93                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
94                                 py::buffer_info info = buf.request();
95                                 if (info.ndim != 1)
96                                         throw std::runtime_error("Incompatible buffer dimension!");
98                                 new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
99                         })
100                         .def_property_readonly("data", &Blob::data)
101                         ;
103         py::class_<Framebuffer>(m, "Framebuffer", py::base<DrmObject>())
104                         ;
106         py::class_<Framebuffer>(m, "Framebuffer", py::base<Framebuffer>())
107                         .def_property_readonly("width", &Framebuffer::width)
108                         .def_property_readonly("height", &Framebuffer::height)
109                         ;
111         py::class_<DumbFramebuffer>(m, "DumbFramebuffer", py::base<Framebuffer>())
112                         .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
113                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
114                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
115                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
116                         .def_property_readonly("format", &DumbFramebuffer::format)
117                         .def_property_readonly("num_planes", &DumbFramebuffer::num_planes)
118                         .def("fd", &DumbFramebuffer::prime_fd)
119                         .def("stride", &DumbFramebuffer::stride)
120                         .def("offset", &DumbFramebuffer::offset)
121                         ;
123         py::class_<ExtFramebuffer>(m, "ExtFramebuffer", py::base<Framebuffer>())
124                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat, vector<int>, vector<uint32_t>, vector<uint32_t>>(),
125                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
126                         ;
128         py::enum_<PixelFormat>(m, "PixelFormat")
129                         .value("Undefined", PixelFormat::Undefined)
131                         .value("NV12", PixelFormat::NV12)
132                         .value("NV21", PixelFormat::NV21)
134                         .value("UYVY", PixelFormat::UYVY)
135                         .value("YUYV", PixelFormat::YUYV)
136                         .value("YVYU", PixelFormat::YVYU)
137                         .value("VYUY", PixelFormat::VYUY)
139                         .value("XRGB8888", PixelFormat::XRGB8888)
140                         .value("XBGR8888", PixelFormat::XBGR8888)
141                         .value("ARGB8888", PixelFormat::ARGB8888)
142                         .value("ABGR8888", PixelFormat::ABGR8888)
144                         .value("RGB888", PixelFormat::RGB888)
145                         .value("BGR888", PixelFormat::BGR888)
147                         .value("RGB565", PixelFormat::RGB565)
148                         .value("BGR565", PixelFormat::BGR565)
149                         ;
151         py::enum_<SyncPolarity>(m, "SyncPolarity")
152                         .value("Undefined", SyncPolarity::Undefined)
153                         .value("Positive", SyncPolarity::Positive)
154                         .value("Negative", SyncPolarity::Negative)
155                         ;
157         py::class_<Videomode>(m, "Videomode")
158                         .def(py::init<>())
160                         .def_readwrite("name", &Videomode::name)
162                         .def_readwrite("clock", &Videomode::clock)
164                         .def_readwrite("hdisplay", &Videomode::hdisplay)
165                         .def_readwrite("hsync_start", &Videomode::hsync_start)
166                         .def_readwrite("hsync_end", &Videomode::hsync_end)
167                         .def_readwrite("htotal", &Videomode::htotal)
169                         .def_readwrite("vdisplay", &Videomode::vdisplay)
170                         .def_readwrite("vsync_start", &Videomode::vsync_start)
171                         .def_readwrite("vsync_end", &Videomode::vsync_end)
172                         .def_readwrite("vtotal", &Videomode::vtotal)
174                         .def_readwrite("vrefresh", &Videomode::vrefresh)
176                         .def_readwrite("flags", &Videomode::flags)
177                         .def_readwrite("type", &Videomode::type)
179                         .def("__repr__", [](const Videomode& vm) { return "<pykms.Videomode " + to_string(vm.hdisplay) + "x" + to_string(vm.vdisplay) + ">"; })
181                         .def("to_blob", &Videomode::to_blob)
183                         .def_property("hsync", &Videomode::hsync, &Videomode::set_hsync)
184                         .def_property("vsync", &Videomode::vsync, &Videomode::set_vsync)
185                         ;
188         m.def("videomode_from_timings", &videomode_from_timings);
190         py::class_<AtomicReq>(m, "AtomicReq")
191                         .def(py::init<Card&>(),
192                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
193                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
194                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const map<string, uint64_t>&)) &AtomicReq::add)
195                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
196                         .def("commit",
197                              [](AtomicReq* self, uint32_t data, bool allow)
198                                 {
199                                         return self->commit((void*)(intptr_t)data, allow);
200                                 }, py::arg("data") = 0, py::arg("allow_modeset") = false)
201                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
202                         ;