lynx_test_lv2ui.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* An LV2 effect (a 0.5 gain) with an X11 UI that lists the UI resize extension in its .ttl but whose extension_data()
  2. * gives NULL for it, as DrumGizmo 0.9.20's UI does (round 190rf: crash reports 0.1.29 x5 / 0.1.30, "making the editor of
  3. * DrumGizmo": JUCE's LV2 host read the extension's struct through the null pointer). Linux:
  4. * cc -shared -fPIC -O2 -I<JUCE>/modules/juce_audio_processors/format_types/LV2_SDK/lv2 tools/testplugin/lynx_test_lv2ui.c \
  5. * -o tools/testplugin/lynx_test_lv2ui.lv2/lynx_test_lv2ui.so -ldl
  6. * then LYNX --plugwintest tools/testplugin/lynx_test_lv2ui.lv2 report.txt (LV2_PATH not needed: the bundle is named).
  7. * Its UI is a plain 240x120 X11 child window of the host's parent (Xlib looked up at run time).
  8. */
  9. #include <lv2/core/lv2.h>
  10. #include <lv2/ui/ui.h>
  11. #include <dlfcn.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #define PLUGIN_URI "urn:lynx:test-lv2ui"
  16. #define UI_URI "urn:lynx:test-lv2ui#ui"
  17. typedef struct { const float* in; float* out; } Gain;
  18. static LV2_Handle instantiate (const LV2_Descriptor* d, double rate, const char* path, const LV2_Feature* const* f)
  19. {
  20. (void) d; (void) rate; (void) path; (void) f;
  21. return calloc (1, sizeof (Gain));
  22. }
  23. static void connect (LV2_Handle h, uint32_t port, void* data)
  24. {
  25. Gain* g = (Gain*) h;
  26. if (port == 0) g->in = (const float*) data;
  27. else g->out = (float*) data;
  28. }
  29. static void run (LV2_Handle h, uint32_t n)
  30. {
  31. Gain* g = (Gain*) h;
  32. if (g->in == NULL || g->out == NULL) return;
  33. for (uint32_t i = 0; i < n; ++i) g->out[i] = 0.5f * g->in[i];
  34. }
  35. static void cleanup (LV2_Handle h) { free (h); }
  36. static const void* plugin_extension (const char* uri) { (void) uri; return NULL; }
  37. static const LV2_Descriptor descriptor = { PLUGIN_URI, instantiate, connect, NULL, run, NULL, cleanup, plugin_extension };
  38. LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index) { return index == 0 ? &descriptor : NULL; }
  39. /* ---- the UI ---- */
  40. typedef void* (*OpenDisplay) (const char*);
  41. typedef unsigned long (*CreateSimpleWindow) (void*, unsigned long, int, int, unsigned, unsigned, unsigned, unsigned long, unsigned long);
  42. typedef int (*MapWindow) (void*, unsigned long);
  43. typedef int (*DestroyWindow) (void*, unsigned long);
  44. typedef int (*CloseDisplay) (void*);
  45. typedef int (*Flush) (void*);
  46. typedef struct { void* xlib; void* display; unsigned long window; } Ui;
  47. static LV2UI_Handle ui_instantiate (const LV2UI_Descriptor* d, const char* plugin, const char* path, LV2UI_Write_Function write,
  48. LV2UI_Controller controller, LV2UI_Widget* widget, const LV2_Feature* const* features)
  49. {
  50. (void) d; (void) plugin; (void) path; (void) write; (void) controller;
  51. unsigned long parent = 0;
  52. for (int i = 0; features != NULL && features[i] != NULL; ++i)
  53. if (strcmp (features[i]->URI, LV2_UI__parent) == 0)
  54. parent = (unsigned long) (uintptr_t) features[i]->data;
  55. Ui* ui = (Ui*) calloc (1, sizeof (Ui));
  56. ui->xlib = dlopen ("libX11.so.6", RTLD_LAZY | RTLD_LOCAL);
  57. if (ui->xlib != NULL && parent != 0)
  58. {
  59. OpenDisplay open = (OpenDisplay) dlsym (ui->xlib, "XOpenDisplay");
  60. CreateSimpleWindow create = (CreateSimpleWindow) dlsym (ui->xlib, "XCreateSimpleWindow");
  61. MapWindow map = (MapWindow) dlsym (ui->xlib, "XMapWindow");
  62. Flush flush = (Flush) dlsym (ui->xlib, "XFlush");
  63. ui->display = open != NULL ? open (NULL) : NULL;
  64. if (ui->display != NULL && create != NULL && map != NULL)
  65. {
  66. ui->window = create (ui->display, parent, 0, 0, 240, 120, 0, 0, 0x808080);
  67. map (ui->display, ui->window);
  68. if (flush != NULL) flush (ui->display);
  69. }
  70. }
  71. *widget = (LV2UI_Widget) (uintptr_t) ui->window;
  72. return ui;
  73. }
  74. static void ui_cleanup (LV2UI_Handle h)
  75. {
  76. Ui* ui = (Ui*) h;
  77. if (ui->display != NULL)
  78. {
  79. DestroyWindow destroy = (DestroyWindow) dlsym (ui->xlib, "XDestroyWindow");
  80. CloseDisplay close = (CloseDisplay) dlsym (ui->xlib, "XCloseDisplay");
  81. if (destroy != NULL && ui->window != 0) destroy (ui->display, ui->window);
  82. if (close != NULL) close (ui->display);
  83. }
  84. free (ui);
  85. }
  86. static void ui_port_event (LV2UI_Handle h, uint32_t port, uint32_t size, uint32_t format, const void* buffer)
  87. {
  88. (void) h; (void) port; (void) size; (void) format; (void) buffer;
  89. }
  90. /* The .ttl lists ui:resize and ui:idleInterface as extension data; like DrumGizmo's UI, this one gives NULL for both */
  91. static const void* ui_extension (const char* uri) { (void) uri; return NULL; }
  92. static const LV2UI_Descriptor ui_descriptor = { UI_URI, ui_instantiate, ui_cleanup, ui_port_event, ui_extension };
  93. LV2_SYMBOL_EXPORT const LV2UI_Descriptor* lv2ui_descriptor (uint32_t index) { return index == 0 ? &ui_descriptor : NULL; }