From 62c5a4a14e97c853a3cd749b0f01a0f0ced5d385 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 1 Feb 2013 21:47:59 +0000 Subject: [PATCH] Start of UI --- ptap/Makefile | 6 +-- ptap/manifest.ttl | 5 ++ ptap/ptap.ttl | 3 ++ ptap/ptapui.c | 129 +++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 138 insertions(+), 5 deletions(-) diff --git a/ptap/Makefile b/ptap/Makefile index 7ab69ce..4e5e5b8 100644 --- a/ptap/Makefile +++ b/ptap/Makefile @@ -1,8 +1,8 @@ CFLAGS := -Wall -O3 -g -D_GNU_SOURCE LDFLAGS := -lm -CFLAGS += `pkg-config lv2core --cflags` -LDFLAGS += `pkg-config lv2core --libs` +CFLAGS += `pkg-config lv2core pugl-0 --cflags` +LDFLAGS += `pkg-config lv2core pugl-0 --libs` PTAPSRC := ptap.c PTAPSRC += ptapui.c @@ -18,7 +18,7 @@ depend: makedepend $(PTAPSRC) $(PTAPO): $(PTAPOBJ) - $(CC) -shared -fPIC -Wl,-soname,$(PTAPO) $(PTAPOBJ) -o $@ + $(CC) $(LDFLAGS) -shared -fPIC -Wl,-soname,$(PTAPO) $(PTAPOBJ) -o $@ .c.o: $(CC) -c -fPIC $(CFLAGS) $< -o $@ diff --git a/ptap/manifest.ttl b/ptap/manifest.ttl index 96a2ffd..b8b2465 100644 --- a/ptap/manifest.ttl +++ b/ptap/manifest.ttl @@ -1,7 +1,12 @@ @prefix lv2: . @prefix rdfs: . +@prefix ui: . a lv2:Plugin, lv2:DelayPlugin; lv2:binary ; rdfs:seeAlso . + + + a ui:X11UI; + ui:binary . diff --git a/ptap/ptap.ttl b/ptap/ptap.ttl index de2488d..99952f0 100644 --- a/ptap/ptap.ttl +++ b/ptap/ptap.ttl @@ -1,6 +1,7 @@ @prefix lv2: . @prefix foaf: . @prefix doap: . +@prefix ui: . a lv2:Plugin; @@ -12,6 +13,8 @@ doap:name "PTap"; doap:license ; + ui:ui ; + lv2:port [ a lv2:InputPort, lv2:AudioPort; lv2:datatype lv2:float; diff --git a/ptap/ptapui.c b/ptap/ptapui.c index 9a2c6fe..e953d04 100644 --- a/ptap/ptapui.c +++ b/ptap/ptapui.c @@ -1,9 +1,86 @@ +#define _BSD_SOURCE 1 +#include /* for usleep */ + +#include #include #include #include #include #include #include +#include + +struct ptapui { + PuglView *view; + pthread_t thread; + LV2UI_Write_Function write; + LV2UI_Controller controller; + int width; + int height; + bool exit; +}; + +static void *ui_thread(void *ptr) +{ + struct ptapui *pui = (struct ptapui *)ptr; + while (!pui->exit) { + usleep(1000000 / 25); + puglProcessEvents(pui->view); + } + return NULL; +} + +static void parameterChanged(struct ptapui *pui, uint32_t index, float value) +{ + pui->write(pui->controller, index, sizeof value, 0, &value); +} + +static void onReshape(PuglView *view, int width, int height) +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glViewport(0, 0, width, height); + glOrtho(0, width, height, 0, 0, 1); + glMatrixMode(GL_MODELVIEW); + glDisable(GL_DEPTH_TEST); + glLoadIdentity(); +} + +static void onDisplay(PuglView *view) +{ + struct ptapui *pui = (struct ptapui *)puglGetHandle(view); + + glClear(GL_COLOR_BUFFER_BIT); + glBegin(GL_QUADS); + + GLdouble x2 = 2.0; + GLdouble y2 = 2.0; + GLdouble x1 = pui->width - 2.0; + GLdouble y1 = pui->height - 2.0; + glColor3f(1.0f, 0.2f, 0.0f); + glVertex2f(x1, y1); + glVertex2f(x1, y2); + glColor3f(0.0f, 1.0f, 0.0f); + glVertex2f(x2, x2); + glVertex2f(x2, y1); + glEnd(); +} + +static void onKeyboard(PuglView *view, bool press, uint32_t key) +{ +} + +static void onMotion(PuglView *view, int x, int y) +{ +} + +static void onMouse(PuglView *view, int button, bool press, int x, int y) +{ +} + +static void onScroll(PuglView *view, float dx, float dy) +{ +} static LV2UI_Handle ptapui_instantiate( const LV2UI_Descriptor *descriptor, @@ -14,11 +91,54 @@ static LV2UI_Handle ptapui_instantiate( LV2UI_Widget *widget, const LV2_Feature *const *host_features) { - return NULL; + PuglNativeWindow parent = 0; + LV2UI_Resize *resize = NULL; + for (; host_features && *host_features; host_features++) { + if (!strcmp((*host_features)->URI, LV2_UI__parent)) { + parent = (PuglNativeWindow)(*host_features)->data; + } else if (!strcmp((*host_features)->URI, LV2_UI__resize)) { + resize = (LV2UI_Resize *)(*host_features)->data; + } + } + + if (!parent) { + fprintf(stderr, "error: ptapui: No parent window provided.\n"); + return NULL; + } + + struct ptapui *pui = malloc(sizeof *pui); + pui->write = write_function; + pui->controller = controller; + pui->width = 320; + pui->height = 240; + pui->exit = false; + + pui->view = puglCreate(parent, "PTap", pui->width, pui->height, true); + puglSetHandle(pui->view, pui); + puglSetDisplayFunc(pui->view, onDisplay); + puglSetReshapeFunc(pui->view, onReshape); + puglSetKeyboardFunc(pui->view, onKeyboard); + puglSetMotionFunc(pui->view, onMotion); + puglSetMouseFunc(pui->view, onMouse); + puglSetScrollFunc(pui->view, onScroll); + + if (resize) { + resize->ui_resize(resize->handle, pui->width, pui->height); + } + + pthread_create(&pui->thread, NULL, ui_thread, pui); + + *widget = (void*)puglGetNativeWindow(pui->view); + return pui; } static void ptapui_cleanup(LV2UI_Handle ui) { + struct ptapui *pui = (struct ptapui *)ui; + pui->exit = true; + pthread_join(pui->thread, NULL); + puglDestroy(pui->view); + free(ui); } static void ptapui_port_event( @@ -30,13 +150,18 @@ static void ptapui_port_event( { } +static const void *ptapui_extension_data(const char *uri) +{ + return NULL; +} + static const LV2UI_Descriptor s_lv2uidescriptor = { .URI = "urn:fuzzle:ptap#X11UI", .instantiate = &ptapui_instantiate, .cleanup = &ptapui_cleanup, .port_event = &ptapui_port_event, - .extension_data = NULL, + .extension_data = &ptapui_extension_data, }; const LV2UI_Descriptor *lv2ui_descriptor(uint32_t index)