pplugins/pui/lv2pui.cpp

277 lines
6.3 KiB
C++

#include <unistd.h>
#include <pthread.h>
#include <pugl/pugl.h>
#include "pui.h"
#include "lv2pui.h"
#include "lv2_external_ui.h"
static char s_lv2ui_uri[128];
/* X11UI support code */
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)
{
this->resize = NULL;
this->touch = NULL;
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;
} else if (!strcmp((*features)->URI, LV2_UI__touch)) {
this->touch = (LV2UI_Touch *)(*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()
{
}
void LV2PUi::WidgetGrabbed(const Widget *widget, bool grabbed)
{
if (this->touch) {
this->touch->touch(this->touch->handle, widget->port, grabbed);
}
}
static void lv2pui_cleanup(LV2UI_Handle handle)
{
LV2PUi *lv2pui = (LV2PUi *)handle;
lv2pui->exit = true;
pthread_join(lv2pui->thread, NULL);
puglDestroy(lv2pui->view);
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 const LV2UI_Descriptor s_lv2ui_descriptor = {
s_lv2ui_uri,
&lv2pui_instantiate,
&lv2pui_cleanup,
&lv2pui_port_event,
&lv2pui_extension_data,
};
/* External UI support code */
struct LV2PUi_Ext_Wrapper {
LV2_External_UI_Widget virt;
bool visible;
LV2PUi *lv2pui;
};
static void lv2pui_ext_run(LV2_External_UI_Widget *ptr)
{
LV2PUi_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)ptr;
LV2PUi *lv2pui = wrapper->lv2pui;
if (!wrapper->visible) return;
if (wrapper->lv2pui->close) {
wrapper->visible = false;
puglDestroy(lv2pui->view);
lv2pui->view = NULL;
} else {
puglProcessEvents(lv2pui->view);
}
}
static void lv2pui_ext_show(LV2_External_UI_Widget *ptr)
{
LV2PUi_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)ptr;
LV2PUi *lv2pui = wrapper->lv2pui;
if (wrapper->visible) return;
wrapper->visible = true;
wrapper->lv2pui->close = false;
wrapper->lv2pui->initialised = false;
int w = lv2pui->widget->w / lv2pui->scale;
int h = lv2pui->widget->h / lv2pui->scale;
lv2pui->view = puglCreate(0, "Test", w, h, true);
puglSetHandle(lv2pui->view, lv2pui);
lv2pui->SetFunc();
}
static void lv2pui_ext_hide(LV2_External_UI_Widget *ptr)
{
LV2PUi_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)ptr;
LV2PUi *lv2pui = wrapper->lv2pui;
if (!wrapper->visible) return;
wrapper->visible = false;
puglDestroy(lv2pui->view);
lv2pui->view = NULL;
}
static LV2UI_Handle lv2pui_ext_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_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)malloc(sizeof *wrapper);
wrapper->lv2pui = LV2PUiFactoryBase::CreateInstance(plugin_uri);
if (!wrapper->lv2pui) {
free(wrapper);
return NULL;
}
wrapper->virt.run = lv2pui_ext_run;
wrapper->virt.show = lv2pui_ext_show;
wrapper->virt.hide = lv2pui_ext_hide;
wrapper->visible = false;
wrapper->lv2pui->write = write_function;
wrapper->lv2pui->controller = controller;
wrapper->lv2pui->view = NULL;
wrapper->lv2pui->InitWidgets();
*widget = wrapper;
return wrapper;
}
static void lv2pui_ext_cleanup(LV2UI_Handle handle)
{
LV2PUi_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)handle;
delete wrapper->lv2pui;
free(wrapper);
}
static void lv2pui_ext_port_event(
LV2UI_Handle handle,
uint32_t port_index,
uint32_t buffer_size,
uint32_t format,
const void *buffer)
{
LV2PUi_Ext_Wrapper *wrapper = (LV2PUi_Ext_Wrapper *)handle;
if (wrapper->lv2pui->view) wrapper->lv2pui->PortEvent(port_index, buffer_size, format, buffer);
}
static const void *lv2pui_ext_extension_data(const char *uri)
{
return NULL;
}
static const LV2UI_Descriptor s_lv2ui_ext_descriptor = {
s_lv2ui_uri,
&lv2pui_ext_instantiate,
&lv2pui_ext_cleanup,
&lv2pui_ext_port_event,
&lv2pui_ext_extension_data,
};
const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index)
{
const char *uri = LV2PUiFactoryBase::GetUri(index / 2);
if (!uri) return NULL;
snprintf(s_lv2ui_uri, sizeof s_lv2ui_uri, "%s#%s", uri, !index ? "ExtUI" : "X11UI");
return !index ? &s_lv2ui_ext_descriptor : &s_lv2ui_descriptor;
}