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