#include "internal/mana_lib.hpp" #include extern "C" { #include "internal/rxi/log.h" } #include #include using namespace mesytec; using namespace mesytec::mnode; namespace py = pybind11; PYBIND11_EMBEDDED_MODULE(py_mana, m) {} namespace { struct Context { py::scoped_interpreter interp; py::module usercode; py::object userobject; py::object py_begin_run; py::object py_process_event; py::object py_process_system_event; py::object py_end_run; std::vector> eventBuffers; }; } // namespace MANA_DEFINE_PLUGIN_INIT(init) { log_set_level(LOG_INFO); static Context g_ctx; auto ctx = &g_ctx; log_debug("init: ctx=%p", ctx); // TODO: pass args to init() // TODO: catch exceptions ctx->usercode = py::module::import("mana_python_test_plugin"); return ctx; } MANA_DEFINE_PLUGIN_SHUTDOWN(shutdown) { log_info("shutdown: ctx=%p", context); } MANA_DEFINE_PLUGIN_BEGIN_RUN(begin_run) { log_info("begin_run: ctx=%p", context); auto jRun = nlohmann::json::parse(descriptor_json); auto ctx = reinterpret_cast(context); ctx->usercode.reload(); // TODO: check if the retrieved attributes are callable ctx->py_begin_run = ctx->usercode.attr("begin_run"); ctx->py_process_event = ctx->usercode.attr("process_event"); ctx->py_process_system_event = ctx->usercode.attr("process_system_event"); ctx->py_end_run = ctx->usercode.attr("end_run"); ctx->py_begin_run(descriptor_json); ctx->eventBuffers.clear(); ctx->eventBuffers.resize(jRun["events"].size()); for (size_t ei = 0; ei < jRun["events"].size(); ++ei) { auto &jEvent = jRun["events"][ei]; ctx->eventBuffers[ei].resize(jEvent["outputs"].size()); } } MANA_DEFINE_PLUGIN_END_RUN(end_run) { log_info("end: ctx=%p", context); auto ctx = reinterpret_cast(context); ctx->py_end_run(descriptor_json); } MANA_DEFINE_PLUGIN_EVENT_DATA(process_event) { log_trace("event: ctx=%p, eventIndex=%d, arrayCount=%zu, totalBytes=%zu", context, eventIndex, arrayCount, totalBytes); auto ctx = reinterpret_cast(context); auto &buffers = ctx->eventBuffers.at(eventIndex); assert(buffers.size() == arrayCount); for (size_t ai = 0; ai < arrayCount; ++ai) { auto arraySpan = mana::get_span(arrays[ai]); ctx->eventBuffers[eventIndex][ai] = py::memoryview::from_memory(arraySpan.data(), arraySpan.size() * sizeof(float)); } ctx->py_process_event(eventIndex, ctx->eventBuffers); } MANA_DEFINE_PLUGIN_SYSTEM_EVENT(process_system_event) { log_debug("system_event: ctx=%p, size=%zu", context, size); auto ctx = reinterpret_cast(context); auto view = py::memoryview::from_memory(data, size * sizeof(uint32_t)); ctx->py_process_system_event(view); } extern "C" MANA_C_SINK_PLUGIN() { mana_sink_plugin_t plugin; plugin.init = init; plugin.shutdown = shutdown; plugin.begin_run = begin_run; plugin.end_run = end_run; plugin.process_event = process_event; plugin.process_system_event = process_system_event; return plugin; }