5247158d7bd4d794733b28136024dca6aa665610
[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("call_page_flip_handlers", &Card::call_page_flip_handlers)
22                         .def("get_prop", (Property* (Card::*)(uint32_t) const)&Card::get_prop)
23                         ;
25         py::class_<DrmObject, DrmObject*>(m, "DrmObject")
26                         .def_property_readonly("id", &DrmObject::id)
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                         ;
38         py::class_<Connector, Connector*>(m, "Connector",  py::base<DrmPropObject>())
39                         .def_property_readonly("fullname", &Connector::fullname)
40                         .def("get_default_mode", &Connector::get_default_mode)
41                         .def("get_current_crtc", &Connector::get_current_crtc)
42                         .def("get_possible_crtcs", &Connector::get_possible_crtcs)
43                         .def("get_modes", &Connector::get_modes)
44                         .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)
45                         .def("get_mode", (Videomode (Connector::*)(unsigned xres, unsigned yres, float refresh, bool ilace) const)&Connector::get_mode)
46                         .def("__repr__", [](const Connector& o) { return "<pykms.Connector " + to_string(o.id()) + ">"; })
47                         ;
49         py::class_<Crtc, Crtc*>(m, "Crtc",  py::base<DrmPropObject>())
50                         .def("set_mode", &Crtc::set_mode)
51                         .def("page_flip", &Crtc::page_flip)
52                         .def("set_plane", &Crtc::set_plane)
53                         .def_property_readonly("possible_planes", &Crtc::get_possible_planes)
54                         .def_property_readonly("primary_plane", &Crtc::get_primary_plane)
55                         .def_property_readonly("mode", &Crtc::mode)
56                         .def_property_readonly("mode_valid", &Crtc::mode_valid)
57                         .def("__repr__", [](const Crtc& o) { return "<pykms.Crtc " + to_string(o.id()) + ">"; })
58                         ;
60         py::class_<Encoder, Encoder*>(m, "Encoder",  py::base<DrmPropObject>())
61                         ;
63         py::class_<Plane, Plane*>(m, "Plane",  py::base<DrmPropObject>())
64                         .def("supports_crtc", &Plane::supports_crtc)
65                         .def_property_readonly("plane_type", &Plane::plane_type)
66                         .def("__repr__", [](const Plane& o) { return "<pykms.Plane " + to_string(o.id()) + ">"; })
67                         ;
69         py::enum_<PlaneType>(m, "PlaneType")
70                         .value("Overlay", PlaneType::Overlay)
71                         .value("Primary", PlaneType::Primary)
72                         .value("Cursor", PlaneType::Cursor)
73                         ;
75         py::class_<Property, Property*>(m, "Property",  py::base<DrmObject>())
76                         .def_property_readonly("name", &Property::name)
77                         ;
79         py::class_<Blob>(m, "Blob", py::base<DrmObject>())
80                         .def("__init__", [](Blob& instance, Card& card, py::buffer buf) {
81                                 py::buffer_info info = buf.request();
82                                 if (info.ndim != 1)
83                                         throw std::runtime_error("Incompatible buffer dimension!");
85                                 new (&instance) Blob(card, info.ptr, info.size * info.itemsize);
86                         })
87                         .def_property_readonly("data", &Blob::data)
88                         ;
90         py::class_<Framebuffer>(m, "Framebuffer", py::base<DrmObject>())
91                         ;
93         py::class_<MappedFramebuffer>(m, "MappedFramebuffer", py::base<Framebuffer>())
94                         .def_property_readonly("width", &MappedFramebuffer::width)
95                         .def_property_readonly("height", &MappedFramebuffer::height)
96                         ;
98         py::class_<DumbFramebuffer>(m, "DumbFramebuffer", py::base<MappedFramebuffer>())
99                         .def(py::init<Card&, uint32_t, uint32_t, const string&>(),
100                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
101                         .def(py::init<Card&, uint32_t, uint32_t, PixelFormat>(),
102                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
103                         ;
105         py::enum_<PixelFormat>(m, "PixelFormat")
106                         .value("Undefined", PixelFormat::Undefined)
108                         .value("NV12", PixelFormat::NV12)
109                         .value("NV21", PixelFormat::NV21)
111                         .value("UYVY", PixelFormat::UYVY)
112                         .value("YUYV", PixelFormat::YUYV)
113                         .value("YVYU", PixelFormat::YVYU)
114                         .value("VYUY", PixelFormat::VYUY)
116                         .value("XRGB8888", PixelFormat::XRGB8888)
117                         .value("XBGR8888", PixelFormat::XBGR8888)
118                         .value("ARGB8888", PixelFormat::ARGB8888)
119                         .value("ABGR8888", PixelFormat::ABGR8888)
121                         .value("RGB888", PixelFormat::RGB888)
123                         .value("RGB565", PixelFormat::RGB565)
124                         ;
126         py::class_<Videomode>(m, "Videomode")
127                         .def(py::init<>())
129                         .def_readwrite("name", &Videomode::name)
131                         .def_readwrite("clock", &Videomode::clock)
133                         .def_readwrite("hdisplay", &Videomode::hdisplay)
134                         .def_readwrite("hsync_start", &Videomode::hsync_start)
135                         .def_readwrite("hsync_end", &Videomode::hsync_end)
136                         .def_readwrite("htotal", &Videomode::htotal)
138                         .def_readwrite("vdisplay", &Videomode::vdisplay)
139                         .def_readwrite("vsync_start", &Videomode::vsync_start)
140                         .def_readwrite("vsync_end", &Videomode::vsync_end)
141                         .def_readwrite("vtotal", &Videomode::vtotal)
143                         .def_readwrite("vrefresh", &Videomode::vrefresh)
145                         .def_readwrite("flags", &Videomode::flags)
146                         .def_readwrite("type", &Videomode::type)
147                         ;
149         py::class_<AtomicReq>(m, "AtomicReq")
150                         .def(py::init<Card&>(),
151                              py::keep_alive<1, 2>())    // Keep Card alive until this is destructed
152                         .def("add", (void (AtomicReq::*)(DrmPropObject*, const string&, uint64_t)) &AtomicReq::add)
153                         .def("test", &AtomicReq::test, py::arg("allow_modeset") = false)
154                         .def("commit", &AtomicReq::commit, py::arg("data"), py::arg("allow_modeset") = false)
155                         .def("commit_sync", &AtomicReq::commit_sync, py::arg("allow_modeset") = false)
156                         ;