diff --git a/pui/lv2_external_ui.h b/pui/lv2_external_ui.h new file mode 100644 index 0000000..e5c75bd --- /dev/null +++ b/pui/lv2_external_ui.h @@ -0,0 +1,107 @@ +/* + LV2 External UI extension + This work is in public domain. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + If you have questions, contact Filipe Coelho (aka falkTX) + or ask in #lad channel, FreeNode IRC network. +*/ + +/** + @file lv2_external_ui.h + C header for the LV2 External UI extension . +*/ + +#ifndef LV2_EXTERNAL_UI_H +#define LV2_EXTERNAL_UI_H + +#include "lv2/lv2plug.in/ns/extensions/ui/ui.h" + +#define LV2_EXTERNAL_UI_URI "http://kxstudio.sf.net/ns/lv2ext/external-ui" +#define LV2_EXTERNAL_UI_PREFIX LV2_EXTERNAL_UI_URI "#" + +#define LV2_EXTERNAL_UI__Host LV2_EXTERNAL_UI_PREFIX "Host" +#define LV2_EXTERNAL_UI__Widget LV2_EXTERNAL_UI_PREFIX "Widget" + +/** This extension used to be defined by a lv2plug.in URI */ +#define LV2_EXTERNAL_UI_DEPRECATED_URI "http://lv2plug.in/ns/extensions/ui#external" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * When LV2_EXTERNAL_UI__Widget UI is instantiated, the returned + * LV2UI_Widget handle must be cast to pointer to LV2_External_UI_Widget. + * UI is created in invisible state. + */ +typedef struct _LV2_External_UI_Widget { + /** + * Host calls this function regulary. UI library implementing the + * callback may do IPC or redraw the UI. + * + * @param _this_ the UI context + */ + void (*run)(struct _LV2_External_UI_Widget * _this_); + + /** + * Host calls this function to make the plugin UI visible. + * + * @param _this_ the UI context + */ + void (*show)(struct _LV2_External_UI_Widget * _this_); + + /** + * Host calls this function to make the plugin UI invisible again. + * + * @param _this_ the UI context + */ + void (*hide)(struct _LV2_External_UI_Widget * _this_); + +} LV2_External_UI_Widget; + +#define LV2_EXTERNAL_UI_RUN(ptr) (ptr)->run(ptr) +#define LV2_EXTERNAL_UI_SHOW(ptr) (ptr)->show(ptr) +#define LV2_EXTERNAL_UI_HIDE(ptr) (ptr)->hide(ptr) + +/** + * On UI instantiation, host must supply LV2_EXTERNAL_UI__Host feature. + * LV2_Feature::data must be pointer to LV2_External_UI_Host. + */ +typedef struct _LV2_External_UI_Host { + /** + * Callback that plugin UI will call + * when UI (GUI window) is closed by user. + * This callback will be called during execution of LV2_External_UI_Widget::run() + * (i.e. not from background thread). + * + * After this callback is called, UI is defunct. Host must call + * LV2UI_Descriptor::cleanup(). If host wants to make the UI visible + * again UI must be reinstantiated. + * + * @param controller Host context associated with plugin UI, as + * supplied to LV2UI_Descriptor::instantiate() + */ + void (*ui_closed)(LV2UI_Controller controller); + + /** + * Optional (may be NULL) "user friendly" identifier which the UI + * may display to allow a user to easily associate this particular + * UI instance with the correct plugin instance as it is represented + * by the host (e.g. "track 1" or "channel 4"). + * + * If supplied by host, the string will be referenced only during + * LV2UI_Descriptor::instantiate() + */ + const char * plugin_human_id; + +} LV2_External_UI_Host; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* LV2_EXTERNAL_UI_H */ diff --git a/pui/lv2pui.cpp b/pui/lv2pui.cpp index cfa84cd..1649301 100644 --- a/pui/lv2pui.cpp +++ b/pui/lv2pui.cpp @@ -3,9 +3,12 @@ #include #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; @@ -73,14 +76,16 @@ static LV2UI_Handle lv2pui_instantiate( LV2PUi::~LV2PUi() { - this->exit = true; - pthread_join(this->thread, NULL); - puglDestroy(this->view); } static void lv2pui_cleanup(LV2UI_Handle handle) { LV2PUi *lv2pui = (LV2PUi *)handle; + + lv2pui->exit = true; + pthread_join(lv2pui->thread, NULL); + puglDestroy(lv2pui->view); + delete lv2pui; } @@ -122,7 +127,7 @@ static const void *lv2pui_extension_data(const char *uri) return NULL; } -static LV2UI_Descriptor s_lv2ui_descriptor = { +static const LV2UI_Descriptor s_lv2ui_descriptor = { s_lv2ui_uri, &lv2pui_instantiate, &lv2pui_cleanup, @@ -130,11 +135,130 @@ static LV2UI_Descriptor s_lv2ui_descriptor = { &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); + const char *uri = LV2PUiFactoryBase::GetUri(index / 2); if (!uri) return NULL; - snprintf(s_lv2ui_uri, sizeof s_lv2ui_uri, "%s#%s", uri, "X11UI"); - return &s_lv2ui_descriptor; + snprintf(s_lv2ui_uri, sizeof s_lv2ui_uri, "%s#%s", uri, !index ? "ExtUI" : "X11UI"); + return !index ? &s_lv2ui_ext_descriptor : &s_lv2ui_descriptor; } diff --git a/pui/pui.cpp b/pui/pui.cpp index 724b331..175baaf 100644 --- a/pui/pui.cpp +++ b/pui/pui.cpp @@ -157,6 +157,11 @@ void PUi::OnScroll(float dx, float dy) } } +void PUi::OnClose() +{ + this->close = true; +} + static void onReshape(PuglView *view, int width, int height) { PUi *pui = (PUi *)puglGetHandle(view); @@ -199,6 +204,12 @@ static void onScroll(PuglView *view, float dx, float dy) pui->OnScroll(dx, dy); } +static void onClose(PuglView *view) +{ + PUi *pui = (PUi *)puglGetHandle(view); + pui->OnClose(); +} + void PUi::SetFunc() { puglSetDisplayFunc(this->view, onDisplay); @@ -208,4 +219,5 @@ void PUi::SetFunc() puglSetMotionFunc(this->view, onMotion); puglSetMouseFunc(this->view, onMouse); puglSetScrollFunc(this->view, onScroll); + puglSetCloseFunc(this->view, onClose); } diff --git a/pui/pui.h b/pui/pui.h index b6a0441..63d2303 100644 --- a/pui/pui.h +++ b/pui/pui.h @@ -34,8 +34,9 @@ struct PUi : PTextures { Widget *active; int mx, my, mb, mm; float scale; + bool close; - PUi() : view(NULL), widget(NULL), tooltip(NULL), active(NULL), mx(0), my(0), mb(0), mm(0), scale(1.f) {} + PUi() : view(NULL), widget(NULL), tooltip(NULL), active(NULL), mx(0), my(0), mb(0), mm(0), scale(1.f), close(false) {} virtual ~PUi(); @@ -57,6 +58,8 @@ struct PUi : PTextures { void OnScroll(float dx, float dy); + void OnClose(); + virtual void ParameterChanged(const Widget *widget) = 0; void SetFunc();