#include #include #include #include "pui.h" #include "lv2pui.h" static char s_lv2ui_uri[128]; static void *lv2pui_thread(void *ptr) { LV2PUi *lv2pui = (LV2PUi *)ptr; while (!lv2pui->exit) { usleep(1000000 / 25); puglProcessEvents(lv2pui->view); } return NULL; } void LV2PUi::Instantiate( const LV2UI_Descriptor *descriptor, const char *bundle_path, LV2UI_Write_Function write_function, LV2UI_Controller controller, LV2UI_Widget *widget, const LV2_Feature *const *features) { PuglNativeWindow parent = 0; for (; features && *features; ++features) { if (!strcmp((*features)->URI, LV2_UI__parent)) { parent = (PuglNativeWindow)(*features)->data; } else if (!strcmp((*features)->URI, LV2_UI__resize)) { this->resize = (LV2UI_Resize *)(*features)->data; } } this->write = write_function; this->controller = controller; this->exit = false; this->InitWidgets(); int w = this->widget->w / this->scale; int h = this->widget->h / this->scale; this->view = puglCreate(parent, "Test", w, h, true); puglSetHandle(this->view, this); this->SetFunc(); if (this->resize) { this->resize->ui_resize(this->resize->handle, w, h); } pthread_create(&this->thread, NULL, &lv2pui_thread, this); *widget = (void *)puglGetNativeWindow(this->view); } static LV2UI_Handle lv2pui_instantiate( const LV2UI_Descriptor *descriptor, const char *plugin_uri, const char *bundle_path, LV2UI_Write_Function write_function, LV2UI_Controller controller, LV2UI_Widget *widget, const LV2_Feature *const *features) { LV2PUi *lv2pui = LV2PUiFactoryBase::CreateInstance(plugin_uri); if (!lv2pui) return NULL; lv2pui->Instantiate(descriptor, bundle_path, write_function, controller, widget, features); return lv2pui; } LV2PUi::~LV2PUi() { this->exit = true; pthread_join(this->thread, NULL); puglDestroy(this->view); } static void lv2pui_cleanup(LV2UI_Handle handle) { LV2PUi *lv2pui = (LV2PUi *)handle; delete lv2pui; } void LV2PUi::ParameterChanged(const Widget *w) { if (w->port == UINT_MAX) return; this->write(this->controller, w->port, sizeof w->value, 0, &w->value); if (!tooltip) return; char tmp[128]; snprintf(tmp, sizeof tmp, "(%s): %s: %0.4f", w->group, w->label, w->value); tooltip->SetLabel(tmp); } void LV2PUi::PortEvent(uint32_t port_index, uint32_t buffer_size, uint32_t format, const void *buffer) { Widget *w = this->widget->GetWidget(port_index); if (!w) return; w->SetValue(*((float *)buffer)); this->Repaint(); } static void lv2pui_port_event( LV2UI_Handle handle, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void *buffer) { LV2PUi *lv2pui = (LV2PUi *)handle; lv2pui->PortEvent(port_index, buffer_size, format, buffer); } static const void *lv2pui_extension_data(const char *uri) { return NULL; } static LV2UI_Descriptor s_lv2ui_descriptor = { s_lv2ui_uri, &lv2pui_instantiate, &lv2pui_cleanup, &lv2pui_port_event, &lv2pui_extension_data, }; const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index) { const char *uri = LV2PUiFactoryBase::GetUri(index); if (!uri) return NULL; snprintf(s_lv2ui_uri, sizeof s_lv2ui_uri, "%s#%s", uri, "X11UI"); return &s_lv2ui_descriptor; }