summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2026-11-21 16:54:05 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2026-11-28 21:59:00 -0700
commit861cf929bc43105a522beb2db0b2b6bb2d56b890 (patch)
tree06059b5075f3a62f2388f6b2fa53e05d005c6645
parentswell-generic-wayland: add a native Wayland backend (diff)
downloadWDL-wayland-master.tar.xz
WDL-wayland-master.zip
swell-vst3-wayland: host native Wayland VST3 plugins [PoC]HEADmaster
VST3 3.8 lets a plugin open a Wayland connection to its host and make its editor a subsurface of the host window. Let's monkey patch it into REAPER. This code would be best implemented inside of REAPER proper, but REAPER is closed source. So this patch hijacks GetPluginFactory to carry out its mischief. Obviously much of this will be moved into REAPER proper; SWELL isn't supposed to depend on the VST3 SDK at all, for example. So this commit is mostly a proof of concept, and also has some of the compositor code that will be useful later. This works by giving plugins a private display and forwarding its requests to our compositor connection, which lets the compositor use the plugin's own buffers. We share the containing xdg surface and toplevel too, so the plugin can parent menus and dialogs and such. Unfortunately, since this is a hack, REAPER still asks for an X11 editor. When REAPER calls GetPluginFactory, we actually use the XID it passes to attached() to find the corresponding SWELL window. This means we still have Xwayland allocating these, but is unused on Wayland plugins. To build this, point VST3_SDK at the directory containing pluginterfaces from the 3.8 SDK. This also needs libwayland-server and libffi. make -C WDL/swell WAYLAND=1 VST3_SDK=/path/to/vst3sdk
-rw-r--r--WDL/swell/Makefile18
-rw-r--r--WDL/swell/swell-generic-wayland.cpp202
-rw-r--r--WDL/swell/swell-internal.h3
-rw-r--r--WDL/swell/swell-vst3-wayland.cpp688
-rw-r--r--WDL/swell/swell-vst3-wayland.h52
-rw-r--r--WDL/swell/swell-wayland-delegate.cpp606
-rw-r--r--WDL/swell/swell-xwayland-bridge.cpp63
-rw-r--r--WDL/swell/swell.cpp14
8 files changed, 1616 insertions, 30 deletions
diff --git a/WDL/swell/Makefile b/WDL/swell/Makefile
index a879f6b7..cb5ed336 100644
--- a/WDL/swell/Makefile
+++ b/WDL/swell/Makefile
@@ -125,12 +125,18 @@ ifndef NOGDK
cairo libdecor-0 epoxy egl xcb-keysyms xcb-randr xcb-damage \
xcb-shape xcb-shm xcb-dri3 xcb-sync xcb-composite xcb-ewmh xcb-xtest \
xcb-xfixes xcb-xkb xcb-image xcb-renderutil xcb-res libdrm
+ WAYLAND_PROTOCOLS := xdg-shell text-input-unstable-v3 xdg-activation-v1 xdg-toplevel-drag-v1 viewporter fractional-scale-v1 xdg-output-unstable-v1 cursor-shape-v1 pointer-warp-v1 tablet-unstable-v2
+ ifdef VST3_SDK
+ WAYLAND_PACKAGES += 'wayland-server >= 1.22' libffi
+ WAYLAND_PROTOCOLS += linux-dmabuf-v1 linux-drm-syncobj-v1
+ CFLAGS += -DSWELL_WAYLAND_VST3
+ SWELL_OBJS += swell-vst3-wayland.o swell-wayland-delegate.o
+ endif
CFLAGS += -DSWELL_TARGET_WAYLAND $(shell $(PKG_CONFIG) --cflags $(WAYLAND_PACKAGES))
LINKEXTRA += $(shell $(PKG_CONFIG) --libs $(WAYLAND_PACKAGES))
SWELL_OBJS += swell-generic-wayland.o swell-xwayland-bridge.o
WAYLAND_SCANNER ?= wayland-scanner
WAYLAND_PROTOCOLS_DIR := $(shell $(PKG_CONFIG) --variable=pkgdatadir wayland-protocols)
- WAYLAND_PROTOCOLS := xdg-shell text-input-unstable-v3 xdg-activation-v1 xdg-toplevel-drag-v1 viewporter fractional-scale-v1 xdg-output-unstable-v1 cursor-shape-v1 pointer-warp-v1 tablet-unstable-v2
SWELL_OBJS += $(addsuffix -protocol.o,$(WAYLAND_PROTOCOLS))
endif
ifndef SWELL_GDK_ONLY
@@ -205,8 +211,14 @@ clean:
ifdef WAYLAND
ifndef NOGDK
ifndef GDK2
-swell-generic-wayland.o swell-xwayland-bridge.o: CXXFLAGS += -std=c++17
-swell-generic-wayland.o swell-xwayland-bridge.o: $(SWELL_HEADERS) swell-wayland.h swell-xwayland-bridge.h $(addsuffix -client-protocol.h,$(WAYLAND_PROTOCOLS))
+ifdef VST3_SDK
+swell-vst3-wayland.o: CXXFLAGS += -isystem "$(VST3_SDK)"
+swell.o swell-generic-wayland.o swell-xwayland-bridge.o swell-vst3-wayland.o swell-wayland-delegate.o: swell-vst3-wayland.h
+endif
+swell-generic-wayland.o swell-xwayland-bridge.o swell-vst3-wayland.o swell-wayland-delegate.o: CXXFLAGS += -std=c++17
+swell-generic-wayland.o swell-xwayland-bridge.o swell-vst3-wayland.o swell-wayland-delegate.o: $(SWELL_HEADERS) swell-wayland.h swell-xwayland-bridge.h $(addsuffix -client-protocol.h,$(WAYLAND_PROTOCOLS))
+linux-dmabuf-v1-client-protocol.h linux-dmabuf-v1-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/stable/linux-dmabuf/linux-dmabuf-v1.xml
+linux-drm-syncobj-v1-client-protocol.h linux-drm-syncobj-v1-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/staging/linux-drm-syncobj/linux-drm-syncobj-v1.xml
xdg-shell-client-protocol.h xdg-shell-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/stable/xdg-shell/xdg-shell.xml
text-input-unstable-v3-client-protocol.h text-input-unstable-v3-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/unstable/text-input/text-input-unstable-v3.xml
xdg-activation-v1-client-protocol.h xdg-activation-v1-protocol.c: $(WAYLAND_PROTOCOLS_DIR)/staging/xdg-activation/xdg-activation-v1.xml
diff --git a/WDL/swell/swell-generic-wayland.cpp b/WDL/swell/swell-generic-wayland.cpp
index c5de629c..7496d54d 100644
--- a/WDL/swell/swell-generic-wayland.cpp
+++ b/WDL/swell/swell-generic-wayland.cpp
@@ -61,6 +61,9 @@
#include "swell-internal.h"
#include "swell-dlggen.h"
#include "swell-wayland.h"
+#ifdef SWELL_WAYLAND_VST3
+#include "swell-vst3-wayland.h"
+#endif
#include "swell-platform.h"
#include "swell-xwayland-bridge.h"
#include "../assocarray.h"
@@ -115,6 +118,9 @@ static wl_touch *touch;
static int touch_id = -1;
static SWELL_WaylandWindow *touch_window, *click_window;
static SWELL_WaylandWindow *pointer_window, *keyboard_window, *button_window;
+#ifdef SWELL_WAYLAND_VST3
+static wl_surface *foreign_pointer_surface;
+#endif
static uint32_t input_serial, popup_serial, button_serial;
static unsigned modifiers;
static double pointer_x, pointer_y;
@@ -1333,6 +1339,116 @@ static xdg_surface *role_surface(SWELL_WaylandWindow *w)
return w ? (w->frame ? libdecor_frame_get_xdg_surface(w->frame) : w->xdg) : NULL;
}
+#ifdef SWELL_WAYLAND_VST3
+wl_display *swell_vst3_display()
+{
+ cancel_read();
+ return disconnected ? NULL : display;
+}
+
+struct SWELL_VST3Parent
+{
+ wl_surface *surface;
+ SWELL_WaylandBuffer *buffer;
+ wl_subsurface *subsurface = NULL;
+ SWELL_WaylandWindow *top = NULL;
+ int x = 0, y = 0;
+};
+
+int swell_vst3_scale()
+{
+ return layout_scale;
+}
+
+SWELL_VST3Parent *swell_vst3_create_parent()
+{
+ if (!subcompositor || !compositor || !shm) return NULL;
+ SWELL_WaylandWindow temporary;
+ temporary.alpha = true;
+ SWELL_WaylandBuffer *buffer = create_buffer(&temporary);
+ if (!buffer) return NULL;
+ buffer->owner = NULL;
+ wl_surface *surface = wl_compositor_create_surface(compositor);
+ wl_region *empty = surface ? wl_compositor_create_region(compositor) : NULL;
+ if (!empty)
+ {
+ if (surface) wl_surface_destroy(surface);
+ delete buffer;
+ return NULL;
+ }
+ wl_surface_set_input_region(surface, empty);
+ wl_region_destroy(empty);
+ return new SWELL_VST3Parent{surface, buffer};
+}
+
+void swell_vst3_position(SWELL_VST3Parent *view, HWND hwnd)
+{
+ SWELL_WaylandWindow *top = hwnd && IsWindowVisible(hwnd) ? window_from_hwnd(hwnd) : NULL;
+ if (top && (top->dead || top->destroying)) top = NULL;
+ POINT point = {};
+ if (top)
+ {
+ SWELL_WaylandHWNDGuard guard(hwnd);
+ ClientToScreen(hwnd, &point);
+ if (!guard.valid() || swell_vst3_parent_view(hwnd) != view || window_from_hwnd(hwnd) != top) return;
+ }
+ const bool changed = top != view->top;
+ if (changed)
+ {
+ if (view->subsurface) wl_subsurface_destroy(view->subsurface);
+ view->subsurface = NULL;
+ view->top = top;
+ if (!top) return;
+ view->subsurface = wl_subcompositor_get_subsurface(subcompositor, view->surface, top->surface);
+ if (!view->subsurface)
+ {
+ view->top = NULL;
+ return;
+ }
+ wl_subsurface_set_desync(view->subsurface);
+ wl_surface_attach(view->surface, view->buffer->object, 0, 0);
+ wl_surface_damage(view->surface, 0, 0, 1, 1);
+ }
+ if (!top) return;
+ const POINT origin = window_origin(top);
+ const int x = floor_scale(point.x - origin.x, top->scale), y = floor_scale(point.y - origin.y, top->scale);
+ if (changed || x != view->x || y != view->y)
+ {
+ view->x = x;
+ view->y = y;
+ wl_subsurface_set_position(view->subsurface, x, y);
+ wl_surface_commit(view->surface);
+ commit_parent(top);
+ }
+}
+
+void swell_vst3_destroy_parent(SWELL_VST3Parent *view)
+{
+ if (!view) return;
+ swell_wayland_client_forget((wl_proxy *)view->surface);
+ if (view->subsurface) wl_subsurface_destroy(view->subsurface);
+ wl_surface_destroy(view->surface);
+ delete view->buffer;
+ delete view;
+}
+
+wl_proxy *swell_vst3_parent(SWELL_VST3Parent *view, const wl_interface *type, RECT *rect)
+{
+ if (!view) return NULL;
+ if (type == &wl_surface_interface) return (wl_proxy *)view->surface;
+ SWELL_WaylandWindow *w = view->top;
+ if (!w) return NULL;
+ if (rect)
+ {
+ int x = 0, y = 0;
+ if (w->frame) libdecor_frame_translate_coordinate(w->frame, 0, 0, &x, &y);
+ *rect = {-x - view->x, -y - view->y, w->width - view->x, w->height - view->y};
+ }
+ if (type == &xdg_surface_interface) return (wl_proxy *)role_surface(w);
+ return w->frame ? (wl_proxy *)libdecor_frame_get_xdg_toplevel(w->frame) : NULL;
+}
+#endif
+
static xdg_positioner *popup_positioner(SWELL_WaylandWindow *w)
{
RECT anchor = {w->x, w->y, w->x, w->y};
@@ -1517,6 +1633,11 @@ static void destroy_native(SWELL_WaylandWindow *w)
}
forget_gl_window(w);
reset_gl(w);
+#ifdef SWELL_WAYLAND_VST3
+ swell_wayland_client_forget((wl_proxy *)w->surface);
+ swell_wayland_client_forget((wl_proxy *)role_surface(w));
+ if (w->frame) swell_wayland_client_forget((wl_proxy *)libdecor_frame_get_xdg_toplevel(w->frame));
+#endif
w->dead = true;
w->mapped = false;
retire_buffers(w);
@@ -1606,7 +1727,7 @@ static void notify_view_focus(SWELL_WaylandWindow *w, bool focused)
}
}
-static bool app_inactive;
+static bool app_inactive, foreign_keyboard_focus;
static UINT_PTR app_deactivate_timer;
static void set_app_active(bool active)
@@ -1624,12 +1745,12 @@ static void set_app_active(bool active)
static void app_deactivate_cb(HWND, UINT, UINT_PTR, DWORD)
{
- set_app_active(keyboard_window != NULL);
+ set_app_active(keyboard_window || foreign_keyboard_focus);
}
static void update_app_focus()
{
- if (keyboard_window) set_app_active(true);
+ if (keyboard_window || foreign_keyboard_focus) set_app_active(true);
else if (!app_inactive && !app_deactivate_timer)
app_deactivate_timer = SetTimer(NULL, 0, 200, app_deactivate_cb);
}
@@ -2024,12 +2145,17 @@ static void pointer_button(SWELL_WaylandWindow *w, double x, double y, uint32_t
}
static void pointer_enter_cb(
- void *, wl_pointer *, uint32_t serial, wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
+ void *, wl_pointer *p, uint32_t serial, wl_surface *surface, wl_fixed_t x, wl_fixed_t y)
{
SWELL_WaylandWindowGuard target(window_from_surface(surface));
flush_pointer_motion();
+ if (p != pointer) return;
pointer_window = target.valid() ? target.window : NULL;
pointer_serial = serial;
+#ifdef SWELL_WAYLAND_VST3
+ foreign_pointer_surface = pointer_window ? NULL : surface;
+ if (foreign_pointer_surface) swell_wayland_client_pointer_enter(surface, seat_name, serial);
+#endif
if (!pointer_window) return;
pointer_x = pointer_window->pixel_coordinate(x);
pointer_y = pointer_window->pixel_coordinate(y);
@@ -2042,6 +2168,9 @@ static void pointer_enter_cb(
static void pointer_leave_cb(void *, wl_pointer *, uint32_t, wl_surface *surface)
{
+#ifdef SWELL_WAYLAND_VST3
+ foreign_pointer_surface = NULL;
+#endif
SWELL_WaylandWindowGuard target(window_from_surface(surface));
flush_pointer_motion();
SWELL_WaylandWindow *w = target.valid() ? target.window : NULL;
@@ -2079,6 +2208,10 @@ static void pointer_button_cb(
void *, wl_pointer *, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
flush_pointer_motion();
+#ifdef SWELL_WAYLAND_VST3
+ if (foreign_pointer_surface && state == WL_POINTER_BUTTON_STATE_PRESSED)
+ if (HWND hwnd = swell_wayland_client_window(foreign_pointer_surface)) SetFocus(hwnd);
+#endif
double x = pointer_x, y = pointer_y;
SWELL_WaylandWindow *w = pointer_target(pointer_window, x, y);
pointer_button(w, x, y, serial, time, button, state);
@@ -2191,7 +2324,9 @@ static void keyboard_enter_cb(void *, wl_keyboard *, uint32_t serial, wl_surface
flush_pointer_motion();
input_serial = serial;
keyboard_window = target.valid() ? target.window : NULL;
+ foreign_keyboard_focus = surface && !keyboard_window;
if (keyboard_window) set_focus(keyboard_window, true);
+ else update_app_focus();
}
static void keyboard_leave_cb(void *, wl_keyboard *, uint32_t, wl_surface *surface)
@@ -2201,8 +2336,10 @@ static void keyboard_leave_cb(void *, wl_keyboard *, uint32_t, wl_surface *surfa
stop_repeat();
SWELL_WaylandWindow *w = target.valid() ? target.window : NULL;
if (keyboard_window == w) keyboard_window = NULL;
+ foreign_keyboard_focus = false;
if (compose_state) xkb_compose_state_reset(compose_state);
if (w) set_focus(w, false);
+ else update_app_focus();
}
static void keyboard_key_cb(void *, wl_keyboard *, uint32_t serial, uint32_t, uint32_t code, uint32_t state)
@@ -2255,6 +2392,10 @@ static void touch_down_cb(void *, wl_touch *, uint32_t serial, uint32_t time, wl
{
if (touch_id >= 0) return;
touch_window = window_from_surface(surface);
+#ifdef SWELL_WAYLAND_VST3
+ if (!touch_window)
+ if (HWND hwnd = swell_wayland_client_window(surface)) SetFocus(hwnd);
+#endif
if (!touch_window) return;
touch_id = id;
touch_x = touch_window->pixel_coordinate(x);
@@ -2321,6 +2462,9 @@ static void seat_capabilities_cb(void *, wl_seat *s, uint32_t capabilities)
if (pointer_window) pointer_window->wheel[0] = pointer_window->wheel[1] = 0;
if (cursor_window == pointer_window) cursor_window = NULL;
pointer_window = NULL;
+#ifdef SWELL_WAYLAND_VST3
+ foreign_pointer_surface = NULL;
+#endif
mouse_buttons = 0;
pointer_serial = 0;
click_window = NULL;
@@ -2340,6 +2484,8 @@ static void seat_capabilities_cb(void *, wl_seat *s, uint32_t capabilities)
wl_keyboard_release(keyboard);
keyboard = NULL;
keyboard_window = NULL;
+ foreign_keyboard_focus = false;
+ update_app_focus();
modifiers = 0;
if (key_state) xkb_state_update_mask(key_state, 0, 0, 0, 0, 0, 0);
if (compose_state) xkb_compose_state_reset(compose_state);
@@ -2439,23 +2585,8 @@ static const struct { const char *name; uint32_t shape; } cursor_shapes[] = {
{"bottom_right_corner", WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE}
};
-static void update_cursor()
+static void set_pointer_cursor(SWELL_WaylandCursor *cursor, int window_scale, bool dragging)
{
- const bool dragging = transfer_drag_active();
- SWELL_WaylandWindow *window = pointer_window ? pointer_window :
- dragging ? button_window : NULL;
- if (!pointer || !window || !pointer_serial) return;
- if (cursor_visible < 0)
- {
- wl_pointer_set_cursor(pointer, pointer_serial, NULL, 0, 0);
- return;
- }
- SWELL_WaylandCursor *cursor = (SWELL_WaylandCursor *)current_cursor;
- if (!dragging && window->is_view)
- {
- SWELL_WaylandCursor *view_cursor = &((SWELL_WaylandView *)window)->cursor;
- if (view_cursor->buffer || view_cursor->name.GetLength()) cursor = view_cursor;
- }
if (!cursor && !dragging)
{
wl_pointer_set_cursor(pointer, pointer_serial, NULL, 0, 0);
@@ -2468,9 +2599,7 @@ static void update_cursor()
cursor_device, pointer_serial, cursor->shape);
return;
}
- SWELL_WaylandOutput *output = view_output(window);
- const int cursor_scale = output ? output_scale(output) : window->scale;
- const int scale = (cursor_scale + SWELL_WAYLAND_SCALE_ONE - 1) / SWELL_WAYLAND_SCALE_ONE;
+ const int scale = (window_scale + SWELL_WAYLAND_SCALE_ONE - 1) / SWELL_WAYLAND_SCALE_ONE;
if (!cursor_surface) cursor_surface = wl_compositor_create_surface(compositor);
wl_buffer *buffer = NULL;
int hotspot_x = 0, hotspot_y = 0, width = 0, height = 0, buffer_scale = scale;
@@ -2541,6 +2670,33 @@ static void update_cursor()
wl_surface_commit(cursor_surface);
}
+static void update_cursor()
+{
+ const bool dragging = transfer_drag_active();
+ SWELL_WaylandWindow *window = pointer_window ? pointer_window :
+ dragging ? button_window : NULL;
+ if (!pointer || !window || !pointer_serial) return;
+ SWELL_WaylandCursor *cursor = (SWELL_WaylandCursor *)current_cursor;
+ if (!dragging && window->is_view)
+ {
+ SWELL_WaylandCursor *view_cursor = &((SWELL_WaylandView *)window)->cursor;
+ if (view_cursor->buffer || view_cursor->name.GetLength()) cursor = view_cursor;
+ }
+ SWELL_WaylandOutput *output = view_output(window);
+ const int scale = output ? output_scale(output) : window->scale;
+ if (cursor_visible < 0) set_pointer_cursor(NULL, scale, false);
+ else set_pointer_cursor(cursor, scale, dragging);
+}
+
+#ifdef SWELL_WAYLAND_VST3
+void swell_vst3_default_cursor(HWND hwnd)
+{
+ SWELL_WaylandWindow *w = window_from_hwnd(hwnd);
+ set_pointer_cursor((SWELL_WaylandCursor *)LoadCursor(NULL, IDC_ARROW),
+ w ? w->scale : layout_scale, false);
+}
+#endif
+
bool swell_wayland_view_has_pointer(SWELL_WaylandView *view, POINT *point)
{
if (!view || view != pointer_window) return false;
diff --git a/WDL/swell/swell-internal.h b/WDL/swell/swell-internal.h
index 1bc4fd73..f184c876 100644
--- a/WDL/swell/swell-internal.h
+++ b/WDL/swell/swell-internal.h
@@ -1078,6 +1078,9 @@ typedef struct
BOOL (*dllMain)(HINSTANCE, DWORD, LPVOID);
#endif
void *lastSymbolRequested;
+#ifdef SWELL_WAYLAND_VST3
+ struct SWELL_VST3Factory *vst3_factory;
+#endif
} SWELL_HINSTANCE;
diff --git a/WDL/swell/swell-vst3-wayland.cpp b/WDL/swell/swell-vst3-wayland.cpp
new file mode 100644
index 00000000..c2d2465e
--- /dev/null
+++ b/WDL/swell/swell-vst3-wayland.cpp
@@ -0,0 +1,688 @@
+/* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
+ Copyright (C) 2006 and later, Cockos, Inc.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+
+ */
+
+
+#include "pluginterfaces/base/ipluginbase.h"
+#include "pluginterfaces/gui/iplugview.h"
+#include "pluginterfaces/gui/iplugviewcontentscalesupport.h"
+#include "pluginterfaces/gui/iwaylandframe.h"
+#include "pluginterfaces/vst/ivstcomponent.h"
+#include "pluginterfaces/vst/ivsteditcontroller.h"
+#include "pluginterfaces/vst/ivsthostapplication.h"
+#define WDL_WCHAR Steinberg::char16
+#include "../wdlutf8.h"
+#undef WDL_WCHAR
+#include <wayland-client.h>
+#include "xdg-shell-client-protocol.h"
+#include <ffi.h>
+#include <glib.h>
+#include <glib-unix.h>
+#include <poll.h>
+#include "swell-vst3-wayland.h"
+#include "swell-wayland.h"
+#include "swell-internal.h"
+#include "../ptrlist.h"
+
+struct SWELL_VST3Factory
+{
+ ffi_cif cif;
+ ffi_closure *closure;
+ void *entry;
+ Steinberg::IPluginFactory *(*original)();
+ int id;
+ bool native_host = false;
+ Steinberg::IPtr<Steinberg::FUnknown> host;
+};
+
+namespace {
+using namespace Steinberg;
+using namespace Steinberg::Vst;
+
+static bool equal(FIDString a, const TUID b)
+{
+ return a && !memcmp(a, b, sizeof(TUID));
+}
+
+template<class T> static IPtr<T> query(FUnknown *object, const TUID interface_id)
+{
+ T *ptr = NULL;
+ if (object) object->queryInterface(interface_id, (void **)&ptr);
+ return owned(ptr);
+}
+
+template<class T> static tresult give(T *ptr, void **out)
+{
+ *out = ptr;
+ if (!ptr) return kNoInterface;
+ ptr->addRef();
+ return kResultOk;
+}
+
+template<class T> class RefCounted : public T
+{
+ gint refs = 1;
+protected:
+ virtual ~RefCounted() {}
+public:
+ uint32 addRef() override { return g_atomic_int_add(&refs, 1) + 1; }
+ uint32 release() override
+ {
+ const uint32 count = g_atomic_int_add(&refs, -1) - 1;
+ if (!count) delete this;
+ return count;
+ }
+};
+
+class Host : public RefCounted<IWaylandHost>
+{
+public:
+ struct Owner
+ {
+ int module, instance;
+ };
+ struct Watch
+ {
+ IPtr<Linux::IEventHandler> handler;
+ GSource *source;
+ Watch *next;
+ Watch(Linux::IEventHandler *h, int fd, Watch *tail)
+ : handler(h), source(g_unix_fd_source_new(fd, G_IO_IN)), next(tail)
+ {
+ g_source_set_callback(source, G_SOURCE_FUNC(readable), h, NULL);
+ g_source_attach(source, NULL);
+ }
+ ~Watch()
+ {
+ g_source_destroy(source);
+ g_source_unref(source);
+ }
+ static gboolean readable(gint fd, GIOCondition condition, gpointer data)
+ {
+ struct pollfd pfd = {fd, POLLIN, 0};
+ if (poll(&pfd, 1, 0) > 0 && (pfd.revents & (POLLIN | POLLHUP | POLLERR)))
+ {
+ IPtr<Linux::IEventHandler> handler((Linux::IEventHandler *)data);
+ handler->onFDIsSet(fd);
+ }
+ return !(condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL));
+ }
+ };
+ struct Client
+ {
+ wl_display *display;
+ Owner owner;
+ bool early;
+ Watch *watches;
+ };
+ struct Scope
+ {
+ Owner owner;
+ wl_display *offer = NULL;
+ Scope *previous;
+ explicit Scope(Owner o) : owner(o), previous(current) { current = this; }
+ ~Scope() { current = previous; }
+ };
+ static thread_local Scope *current;
+ WDL_TypedBuf<Client> clients;
+ static Host &get()
+ {
+ static Host host;
+ return host;
+ }
+ static int nextId()
+ {
+ static gint last;
+ return g_atomic_int_add(&last, 1) + 1;
+ }
+
+ wl_display *connect(Owner owner, bool early)
+ {
+ wl_display *display = swell_wayland_client_open(swell_vst3_display());
+ if (display && !clients.Add({display, owner, early, NULL}))
+ {
+ swell_wayland_client_close(display);
+ return NULL;
+ }
+ return display;
+ }
+ tresult registerHandler(Linux::IRunLoop *loop, Linux::IEventHandler *handler, int fd)
+ {
+ if (!handler || fd < 0) return kInvalidArgument;
+ for (Client &c : clients)
+ if (wl_display_get_fd(c.display) == fd)
+ {
+ c.watches = new Watch(handler, fd, c.watches);
+ return kResultOk;
+ }
+ return loop->registerEventHandler(handler, fd);
+ }
+ tresult unregisterHandler(Linux::IRunLoop *loop, Linux::IEventHandler *handler)
+ {
+ for (Client &c : clients)
+ for (Watch **p = &c.watches; *p; p = &(*p)->next)
+ if ((*p)->handler == handler)
+ {
+ Watch *w = *p;
+ *p = w->next;
+ delete w;
+ return kResultOk;
+ }
+ return loop->unregisterEventHandler(handler);
+ }
+ wl_display *existing(Owner owner, bool &ambiguous)
+ {
+ wl_display *found = NULL;
+ int best = 0;
+ bool unscoped = false;
+ ambiguous = false;
+ for (const Client &c : clients)
+ {
+ if (!c.early) continue;
+ if (!c.owner.module)
+ {
+ unscoped = true;
+ continue;
+ }
+ if (c.owner.module != owner.module ||
+ (c.owner.instance && c.owner.instance != owner.instance)) continue;
+ const int rank = c.owner.instance ? 2 : 1;
+ if (rank < best) continue;
+ ambiguous = rank == best;
+ best = rank;
+ found = c.display;
+ }
+ if (!found && unscoped) ambiguous = true;
+ return ambiguous ? NULL : found;
+ }
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(interface_id, FUnknown_iid) || equal(interface_id, IWaylandHost_iid)) return give(this, out);
+ *out = NULL;
+ return kNoInterface;
+ }
+ wl_display *openWaylandConnection() override
+ {
+ if (current && current->offer)
+ {
+ wl_display *display = current->offer;
+ current->offer = NULL;
+ return display;
+ }
+ return connect(current ? current->owner : Owner{}, true);
+ }
+ tresult closeWaylandConnection(wl_display *display) override
+ {
+ for (int i = 0; i < clients.GetSize(); ++i)
+ if (clients.Get()[i].display == display)
+ {
+ Watch *w = clients.Get()[i].watches;
+ clients.Delete(i);
+ while (w)
+ {
+ Watch *next = w->next;
+ delete w;
+ w = next;
+ }
+ return swell_wayland_client_close(display) ? kResultOk : kResultFalse;
+ }
+ return kInvalidArgument;
+ }
+ ~Host()
+ {
+ while (clients.GetSize()) closeWaylandConnection(clients.Get()[clients.GetSize()-1].display);
+ }
+};
+
+thread_local Host::Scope *Host::current;
+
+template<class T> class WithRunLoop : public RefCounted<T>, public Linux::IRunLoop
+{
+ IPtr<Linux::IRunLoop> loop;
+protected:
+ tresult queryHost(FUnknown *ctx, const TUID id, void **out)
+ {
+ if (!equal(id, Linux::IRunLoop_iid)) return ctx->queryInterface(id, out);
+ if (!loop) loop = query<Linux::IRunLoop>(ctx, id);
+ return give(loop ? (Linux::IRunLoop *)this : NULL, out);
+ }
+public:
+ uint32 addRef() override { return RefCounted<T>::addRef(); }
+ uint32 release() override { return RefCounted<T>::release(); }
+ tresult registerEventHandler(Linux::IEventHandler *handler, int fd) override
+ {
+ return Host::get().registerHandler(loop, handler, fd);
+ }
+ tresult unregisterEventHandler(Linux::IEventHandler *handler) override
+ {
+ return Host::get().unregisterHandler(loop, handler);
+ }
+ tresult registerTimer(Linux::ITimerHandler *handler, Linux::TimerInterval interval) override
+ {
+ return loop->registerTimer(handler, interval);
+ }
+ tresult unregisterTimer(Linux::ITimerHandler *handler) override { return loop->unregisterTimer(handler); }
+};
+
+class Application : public WithRunLoop<IHostApplication>
+{
+ IPtr<FUnknown> original;
+ IPtr<IHostApplication> app;
+public:
+ explicit Application(FUnknown *ctx) : original(ctx), app(query<IHostApplication>(ctx, IHostApplication_iid)) {}
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(interface_id, IHostApplication_iid) || equal(interface_id, FUnknown_iid))
+ return give((IHostApplication *)this, out);
+ if (equal(interface_id, IWaylandHost_iid)) return give(&Host::get(), out);
+ return queryHost(original, interface_id, out);
+ }
+ tresult getName(String128 name) override
+ {
+ if (app) return app->getName(name);
+ WDL_MBtoWideStr(name, g_swell_appname ? g_swell_appname : "SWELL", sizeof(String128));
+ return kResultOk;
+ }
+ tresult createInstance(TUID cid, TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(cid, IWaylandHost_iid)) return Host::get().queryInterface(interface_id, out);
+ *out = NULL;
+ return app ? app->createInstance(cid, interface_id, out) : kNoInterface;
+ }
+};
+
+static IPtr<FUnknown> context(FUnknown *ctx)
+{
+ return owned<IHostApplication>(ctx ? new Application(ctx) : NULL);
+}
+
+static bool hosts_wayland(FUnknown *ctx)
+{
+ if (query<IWaylandHost>(ctx, IWaylandHost_iid)) return true;
+ IPtr<IHostApplication> app = query<IHostApplication>(ctx, IHostApplication_iid);
+ if (!app) return false;
+ TUID id;
+ memcpy(id, IWaylandHost_iid, sizeof(id));
+ IWaylandHost *host = NULL;
+ app->createInstance(id, id, (void **)&host);
+ if (!host) return false;
+ host->release();
+ return true;
+}
+
+static int32 scale_coord(int32 value, int from, int to)
+{
+ const int64_t n = (int64_t)value * to + (value < 0 ? -from/2 : from/2);
+ return (int32)wdl_max((int64_t)INT32_MIN, wdl_min(n / from, (int64_t)INT32_MAX));
+}
+
+static ViewRect scale_rect(const ViewRect &r, int from, int to)
+{
+ return ViewRect(scale_coord(r.left, from, to), scale_coord(r.top, from, to),
+ scale_coord(r.right, from, to), scale_coord(r.bottom, from, to));
+}
+
+class Frame : public WithRunLoop<IPlugFrame>, public IWaylandFrame
+{
+ IPtr<IPlugFrame> original;
+ IPlugView *outer, *inner;
+ HWND parent = NULL;
+ wl_display *implicit = NULL;
+public:
+ bool native = true;
+ int scale() const { return native ? swell_vst3_scale() : SWELL_WAYLAND_SCALE_ONE; }
+ Frame(IPlugFrame *f, IPlugView *v, IPlugView *wrapped) : original(f), outer(v), inner(wrapped) {}
+ uint32 addRef() override { return RefCounted::addRef(); }
+ uint32 release() override { return RefCounted::release(); }
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(interface_id, FUnknown_iid) || equal(interface_id, IPlugFrame_iid)) return give((IPlugFrame *)this, out);
+ if (equal(interface_id, IWaylandFrame_iid)) return give((IWaylandFrame *)this, out);
+ if (equal(interface_id, IWaylandHost_iid)) return give(&Host::get(), out);
+ return queryHost(original, interface_id, out);
+ }
+ tresult resizeView(IPlugView *view, ViewRect *size) override
+ {
+ if (!outer || view != inner || !size) return kInvalidArgument;
+ ViewRect pixels = scale_rect(*size, SWELL_WAYLAND_SCALE_ONE, scale());
+ return original->resizeView(outer, &pixels);
+ }
+ void setParent(HWND hwnd)
+ {
+ if (implicit) Host::get().closeWaylandConnection(implicit);
+ implicit = NULL;
+ if (hwnd) hwnd->Retain();
+ if (parent)
+ {
+ swell_vst3_unbind(parent);
+ parent->Release();
+ }
+ parent = hwnd;
+ }
+ void ownConnection(wl_display *display) { implicit = display; }
+ void detach() { outer = NULL; setParent(NULL); }
+ ~Frame() { setParent(NULL); }
+
+ wl_proxy *share(wl_display *display, const wl_interface *type, RECT *r = NULL)
+ {
+ if (!parent || parent->m_hashaddestroy) return NULL;
+ wl_proxy *object = swell_vst3_parent(swell_vst3_parent_view(parent), type, r);
+ return swell_wayland_client_share(display, object, type, parent);
+ }
+ wl_surface *getWaylandSurface(wl_display *display) override
+ {
+ return (wl_surface *)share(display, &wl_surface_interface);
+ }
+ xdg_surface *getParentSurface(ViewRect &size, wl_display *display) override
+ {
+ RECT r = {};
+ xdg_surface *surface = (xdg_surface *)share(display, &xdg_surface_interface, &r);
+ if (surface) size = ViewRect(r.left, r.top, r.right, r.bottom);
+ return surface;
+ }
+ xdg_toplevel *getParentToplevel(wl_display *display) override
+ {
+ return (xdg_toplevel *)share(display, &xdg_toplevel_interface);
+ }
+};
+
+class View : public RefCounted<IPlugView>, public IPlugViewContentScaleSupport
+{
+ IPtr<IPlugView> original;
+ IPtr<Frame> frame;
+ IPtr<IPlugViewContentScaleSupport> scaling;
+ Host::Owner owner;
+ float requested_scale = 0;
+ int scale() const { return frame ? frame->scale() : swell_vst3_scale(); }
+public:
+ uint32 addRef() override { return RefCounted::addRef(); }
+ uint32 release() override { return RefCounted::release(); }
+ View(IPlugView *v, Host::Owner o) : original(owned(v)),
+ scaling(query<IPlugViewContentScaleSupport>(v, IPlugViewContentScaleSupport_iid)), owner(o) {}
+ ~View() { if (frame) frame->detach(); }
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(interface_id, FUnknown_iid) || equal(interface_id, IPlugView_iid)) return give((IPlugView *)this, out);
+ if (scaling && equal(interface_id, IPlugViewContentScaleSupport_iid))
+ return give((IPlugViewContentScaleSupport *)this, out);
+ return original->queryInterface(interface_id, out);
+ }
+ tresult setFrame(IPlugFrame *f) override
+ {
+ Host::Scope scope(owner);
+ IPtr<Frame> next = owned(f ? new Frame(f, this, original) : NULL);
+ const tresult result = original->setFrame(next);
+ if (result == kResultOk || !f)
+ {
+ if (frame) frame->detach();
+ frame = next;
+ }
+ else if (next) next->detach();
+ return result;
+ }
+ tresult isPlatformTypeSupported(FIDString type) override
+ {
+ if (type && !strcmp(type, kPlatformTypeX11EmbedWindowID)) return kResultOk;
+ return original->isPlatformTypeSupported(type);
+ }
+ tresult attached(void *parent, FIDString type) override
+ {
+ if (frame && type && !strcmp(type, kPlatformTypeX11EmbedWindowID))
+ {
+ HWND hwnd = swell_vst3_find_parent(parent);
+ if (hwnd)
+ {
+ IPtr<Frame> f = frame;
+ f->setParent(hwnd);
+ f->native = true;
+ Host::Scope scope(owner);
+ bool ambiguous = false;
+ wl_display *client = Host::get().existing(owner, ambiguous);
+ if (!client && !ambiguous) client = scope.offer = Host::get().connect(owner, false);
+ wl_surface *surface = client ? f->getWaylandSurface(client) : NULL;
+ tresult result = surface ? original->attached(surface, kPlatformTypeWaylandSurfaceID) : kResultFalse;
+ if (scope.offer) f->ownConnection(scope.offer);
+ if (result == kResultOk) return result;
+ f->setParent(NULL);
+ }
+ if (original->isPlatformTypeSupported(type) != kResultOk) return kResultFalse;
+ if (frame) frame->native = false;
+ if (scaling && requested_scale > 0) scaling->setContentScaleFactor(requested_scale);
+ }
+ return original->attached(parent, type);
+ }
+ tresult removed() override
+ {
+ const tresult result = original->removed();
+ if (frame) frame->setParent(NULL);
+ return result;
+ }
+ tresult onWheel(float distance) override { return original->onWheel(distance); }
+ tresult onKeyDown(char16 ch, int16 key, int16 mods) override { return original->onKeyDown(ch, key, mods); }
+ tresult onKeyUp(char16 ch, int16 key, int16 mods) override { return original->onKeyUp(ch, key, mods); }
+ tresult setContentScaleFactor(ScaleFactor factor) override
+ {
+ requested_scale = factor;
+ return scaling->setContentScaleFactor(factor * SWELL_WAYLAND_SCALE_ONE / scale());
+ }
+ tresult getSize(ViewRect *size) override
+ {
+ tresult result = original->getSize(size);
+ if (result == kResultOk && size) *size = scale_rect(*size, SWELL_WAYLAND_SCALE_ONE, scale());
+ return result;
+ }
+ tresult onSize(ViewRect *size) override
+ {
+ if (!size) return kInvalidArgument;
+ ViewRect logical = scale_rect(*size, scale(), SWELL_WAYLAND_SCALE_ONE);
+ return original->onSize(&logical);
+ }
+ tresult onFocus(TBool state) override { return original->onFocus(state); }
+ tresult canResize() override { return original->canResize(); }
+ tresult checkSizeConstraint(ViewRect *size) override
+ {
+ if (!size) return kInvalidArgument;
+ const int s = scale();
+ ViewRect logical = scale_rect(*size, s, SWELL_WAYLAND_SCALE_ONE);
+ tresult result = original->checkSizeConstraint(&logical);
+ if (result == kResultOk) *size = scale_rect(logical, SWELL_WAYLAND_SCALE_ONE, s);
+ return result;
+ }
+};
+
+class Instance : public RefCounted<IComponent>
+{
+ class Controller : public IEditController
+ {
+ public:
+ Instance &owner;
+ IPtr<IEditController> ctl;
+ Controller(Instance &i, FUnknown *obj) : owner(i), ctl(query<IEditController>(obj, IEditController_iid)) {}
+ uint32 addRef() override { return owner.addRef(); }
+ uint32 release() override { return owner.release(); }
+ tresult queryInterface(const TUID interface_id, void **out) override { return owner.queryInterface(interface_id, out); }
+ tresult initialize(FUnknown *ctx) override
+ {
+ return owner.initialize_plugin(ctl, ctx, owner.controller_host);
+ }
+ tresult terminate() override { return ctl->terminate(); }
+ tresult setComponentState(IBStream *s) override { return ctl->setComponentState(s); }
+ tresult setState(IBStream *s) override { return ctl->setState(s); }
+ tresult getState(IBStream *s) override { return ctl->getState(s); }
+ int32 getParameterCount() override { return ctl->getParameterCount(); }
+ tresult getParameterInfo(int32 i, ParameterInfo &info) override { return ctl->getParameterInfo(i, info); }
+ tresult getParamStringByValue(ParamID id, ParamValue value, String128 s) override
+ {
+ return ctl->getParamStringByValue(id, value, s);
+ }
+ tresult getParamValueByString(ParamID id, TChar *s, ParamValue &value) override
+ {
+ return ctl->getParamValueByString(id, s, value);
+ }
+ ParamValue normalizedParamToPlain(ParamID id, ParamValue v) override { return ctl->normalizedParamToPlain(id, v); }
+ ParamValue plainParamToNormalized(ParamID id, ParamValue v) override { return ctl->plainParamToNormalized(id, v); }
+ ParamValue getParamNormalized(ParamID id) override { return ctl->getParamNormalized(id); }
+ tresult setParamNormalized(ParamID id, ParamValue v) override { return ctl->setParamNormalized(id, v); }
+ tresult setComponentHandler(IComponentHandler *h) override { return ctl->setComponentHandler(h); }
+ IPlugView *createView(FIDString name) override
+ {
+ Host::Scope scope(owner.id);
+ IPlugView *view = ctl->createView(name);
+ if (view && !owner.native_host && view->isPlatformTypeSupported(kPlatformTypeWaylandSurfaceID) == kResultOk &&
+ view->isPlatformTypeSupported("") != kResultOk)
+ return new View(view, owner.id);
+ return view;
+ }
+ };
+
+ IPtr<FUnknown> component_host, controller_host;
+ IPtr<FUnknown> original;
+ IPtr<IComponent> component;
+ Host::Owner id;
+ bool native_host = false;
+ Controller controller;
+
+ tresult initialize_plugin(IPluginBase *plugin, FUnknown *ctx, IPtr<FUnknown> &host)
+ {
+ native_host = hosts_wayland(ctx);
+ IPtr<FUnknown> next = context(native_host ? NULL : ctx);
+ Host::Scope scope(id);
+ const tresult result = plugin->initialize(native_host ? ctx : next.get());
+ if (result == kResultOk) host = next;
+ return result;
+ }
+public:
+ Instance(FUnknown *obj, int module) : original(obj), component(query<IComponent>(obj, IComponent_iid)),
+ id{module, Host::nextId()}, controller(*this, obj) {}
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (component && (equal(interface_id, FUnknown_iid) || equal(interface_id, IPluginBase_iid) || equal(interface_id, IComponent_iid)))
+ return give((IComponent *)this, out);
+ if (controller.ctl && (equal(interface_id, FUnknown_iid) || equal(interface_id, IPluginBase_iid) || equal(interface_id, IEditController_iid)))
+ return give((IEditController *)&controller, out);
+ return original->queryInterface(interface_id, out);
+ }
+ tresult initialize(FUnknown *ctx) override
+ {
+ return initialize_plugin(component, ctx, component_host);
+ }
+ tresult terminate() override { return component->terminate(); }
+ tresult getControllerClassId(TUID cid) override { return component->getControllerClassId(cid); }
+ tresult setIoMode(IoMode mode) override { return component->setIoMode(mode); }
+ int32 getBusCount(MediaType type, BusDirection dir) override { return component->getBusCount(type, dir); }
+ tresult getBusInfo(MediaType type, BusDirection dir, int32 i, BusInfo &info) override
+ {
+ return component->getBusInfo(type, dir, i, info);
+ }
+ tresult getRoutingInfo(RoutingInfo &in, RoutingInfo &out) override { return component->getRoutingInfo(in, out); }
+ tresult activateBus(MediaType type, BusDirection dir, int32 i, TBool state) override
+ {
+ return component->activateBus(type, dir, i, state);
+ }
+ tresult setActive(TBool state) override { return component->setActive(state); }
+ tresult setState(IBStream *s) override { return component->setState(s); }
+ tresult getState(IBStream *s) override { return component->getState(s); }
+};
+
+class Factory : public RefCounted<IPluginFactory3>
+{
+ IPtr<IPluginFactory> factory;
+ IPtr<IPluginFactory2> factory2;
+ IPtr<IPluginFactory3> factory3;
+ SWELL_VST3Factory &module;
+public:
+ Factory(IPluginFactory *f, SWELL_VST3Factory &m)
+ : factory(owned(f)), factory2(query<IPluginFactory2>(f, IPluginFactory2_iid)),
+ factory3(query<IPluginFactory3>(f, IPluginFactory3_iid)), module(m) {}
+ tresult queryInterface(const TUID interface_id, void **out) override
+ {
+ if (!out) return kInvalidArgument;
+ if (equal(interface_id, FUnknown_iid) || equal(interface_id, IPluginFactory_iid) ||
+ (factory2 && equal(interface_id, IPluginFactory2_iid)) || (factory3 && equal(interface_id, IPluginFactory3_iid)))
+ return give(this, out);
+ return factory->queryInterface(interface_id, out);
+ }
+ tresult getFactoryInfo(PFactoryInfo *p) override { return factory->getFactoryInfo(p); }
+ int32 countClasses() override { return factory->countClasses(); }
+ tresult getClassInfo(int32 i, PClassInfo *p) override { return factory->getClassInfo(i, p); }
+ tresult getClassInfo2(int32 i, PClassInfo2 *p) override { return factory2->getClassInfo2(i, p); }
+ tresult getClassInfoUnicode(int32 i, PClassInfoW *p) override { return factory3->getClassInfoUnicode(i, p); }
+ tresult setHostContext(FUnknown *ctx) override
+ {
+ module.native_host = hosts_wayland(ctx);
+ IPtr<FUnknown> next = context(module.native_host ? NULL : ctx);
+ Host::Scope scope({module.id, 0});
+ const tresult result = factory3->setHostContext(module.native_host ? ctx : next.get());
+ if (result == kResultOk) module.host = next;
+ return result;
+ }
+ tresult createInstance(FIDString cid, FIDString interface_id, void **out) override
+ {
+ tresult result = factory->createInstance(cid, interface_id, out);
+ if (!module.native_host && result == kResultOk && out && *out &&
+ (equal(interface_id, IComponent_iid) || equal(interface_id, IEditController_iid) ||
+ equal(interface_id, FUnknown_iid) || equal(interface_id, IPluginBase_iid)))
+ {
+ IPtr<FUnknown> obj = owned((FUnknown *)*out);
+ IPtr<Instance> instance = owned(new Instance(obj, module.id));
+ result = instance->queryInterface(interface_id, out);
+ }
+ return result;
+ }
+};
+}
+
+static void get_factory(ffi_cif *, void *result, void **, void *data)
+{
+ SWELL_VST3Factory *module = (SWELL_VST3Factory *)data;
+ Steinberg::IPluginFactory *factory = module->original();
+ *(Steinberg::IPluginFactory **)result = factory ? new Factory(factory, *module) : NULL;
+}
+
+void *swell_vst3_factory(SWELL_VST3Factory **slot, void *entry)
+{
+ if (*slot) return (*slot)->entry;
+ SWELL_VST3Factory *f = new SWELL_VST3Factory;
+ f->id = Host::nextId();
+ f->original = (Steinberg::IPluginFactory *(*)())entry;
+ f->closure = (ffi_closure *)ffi_closure_alloc(sizeof(ffi_closure), &f->entry);
+ if (f->closure && ffi_prep_cif(&f->cif, FFI_DEFAULT_ABI, 0, &ffi_type_pointer, NULL) == FFI_OK &&
+ ffi_prep_closure_loc(f->closure, &f->cif, get_factory, f, f->entry) == FFI_OK)
+ {
+ *slot = f;
+ return f->entry;
+ }
+ swell_vst3_free_factory(f);
+ return entry;
+}
+
+void swell_vst3_free_factory(SWELL_VST3Factory *factory)
+{
+ if (!factory) return;
+ if (factory->closure) ffi_closure_free(factory->closure);
+ delete factory;
+}
diff --git a/WDL/swell/swell-vst3-wayland.h b/WDL/swell/swell-vst3-wayland.h
new file mode 100644
index 00000000..13e2c872
--- /dev/null
+++ b/WDL/swell/swell-vst3-wayland.h
@@ -0,0 +1,52 @@
+/* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
+ Copyright (C) 2006 and later, Cockos, Inc.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+
+ */
+
+
+#pragma once
+
+#include "swell.h"
+
+struct wl_display;
+struct wl_proxy;
+struct wl_surface;
+struct wl_interface;
+struct SWELL_VST3Parent;
+struct SWELL_VST3Factory;
+
+void *swell_vst3_factory(SWELL_VST3Factory **slot, void *entry);
+void swell_vst3_free_factory(SWELL_VST3Factory *factory);
+HWND swell_vst3_find_parent(void *xid);
+void swell_vst3_unbind(HWND hwnd);
+SWELL_VST3Parent *swell_vst3_parent_view(HWND hwnd);
+SWELL_VST3Parent *swell_vst3_create_parent();
+void swell_vst3_destroy_parent(SWELL_VST3Parent *parent);
+void swell_vst3_position(SWELL_VST3Parent *parent, HWND hwnd);
+int swell_vst3_scale();
+void swell_vst3_default_cursor(HWND hwnd);
+wl_display *swell_vst3_display();
+wl_proxy *swell_vst3_parent(SWELL_VST3Parent *view, const wl_interface *type, RECT *rect);
+wl_display *swell_wayland_client_open(wl_display *session);
+bool swell_wayland_client_close(wl_display *client);
+wl_proxy *swell_wayland_client_share(wl_display *client, wl_proxy *object, const wl_interface *type, HWND hwnd);
+HWND swell_wayland_client_window(wl_surface *surface);
+void swell_wayland_client_pointer_enter(wl_surface *surface, uint32_t seat, uint32_t serial);
+void swell_wayland_client_forget(wl_proxy *object);
diff --git a/WDL/swell/swell-wayland-delegate.cpp b/WDL/swell/swell-wayland-delegate.cpp
new file mode 100644
index 00000000..bbd8d657
--- /dev/null
+++ b/WDL/swell/swell-wayland-delegate.cpp
@@ -0,0 +1,606 @@
+/* Cockos SWELL (Simple/Small Win32 Emulation Layer for Linux/OSX)
+ Copyright (C) 2006 and later, Cockos, Inc.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+
+ */
+
+
+#include <wayland-client.h>
+#include <wayland-server.h>
+#include "xdg-shell-client-protocol.h"
+#include "linux-dmabuf-v1-client-protocol.h"
+#include "linux-drm-syncobj-v1-client-protocol.h"
+#include "viewporter-client-protocol.h"
+#include "fractional-scale-v1-client-protocol.h"
+#include "cursor-shape-v1-client-protocol.h"
+#include <errno.h>
+#include <poll.h>
+#include <sys/eventfd.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <glib.h>
+#include "swell-vst3-wayland.h"
+#include "../assocarray.h"
+#include "../ptrlist.h"
+#include "../mutex.h"
+
+namespace {
+
+struct Connection;
+struct Global
+{
+ Connection *connection;
+ const wl_interface *type;
+ uint32_t name;
+ wl_global *global = NULL;
+ wl_proxy *parent = NULL, *borrowed = NULL;
+ HWND hwnd = NULL;
+ uint32_t pointer_serial = 0;
+ bool cursor_set = false;
+};
+
+struct Object
+{
+ Connection *connection;
+ const wl_interface *type;
+ wl_proxy *proxy;
+ wl_resource *resource;
+ bool borrowed, focused = false, pointer_frame_pending = false;
+ Global *global = NULL;
+ Object *child = NULL, *parent = NULL;
+ WDL_TypedBuf<int32_t> touches;
+ Object(Connection *c, const wl_interface *t, wl_proxy *p, wl_resource *r, bool b) :
+ connection(c), type(t), proxy(p), resource(r), borrowed(b) {}
+};
+
+struct Connection
+{
+ wl_display *upstream = NULL, *server = NULL, *client = NULL;
+ wl_client *peer = NULL;
+ wl_registry *registry = NULL, *client_registry = NULL;
+ wl_event_queue *queue = NULL;
+ int stop = -1;
+ GThread *thread = NULL;
+ WDL_Mutex mutex;
+ WDL_PtrList_DeleteOnDestroy<Global> globals;
+ WDL_PtrList<Object> objects;
+ WDL_PointerKeyedArray<wl_proxy *, Object *> by_proxy;
+ wl_protocol_logger *barrier = NULL;
+ struct PeerListener : wl_listener { Connection *connection; } gone = {};
+
+ void clear_objects()
+ {
+ while (objects.GetSize()) wl_resource_destroy(objects.GetLast()->resource);
+ }
+
+ bool start(wl_display *session);
+ void run();
+ ~Connection();
+ Object *add(wl_proxy *proxy, const wl_interface *type, uint32_t id, uint32_t version, bool borrowed);
+ wl_proxy *share(wl_proxy *parent, const wl_interface *type, HWND hwnd);
+ void forget(wl_proxy *parent);
+};
+
+static WDL_PtrList<Connection> clients;
+
+static int destructor_opcode(const wl_interface *type, int version)
+{
+ for (int i = 0; i < type->method_count; ++i)
+ {
+ const wl_message &m = type->methods[i];
+ if ((!strcmp(m.name, "destroy") || !strcmp(m.name, "release")) && atoi(m.signature) <= version)
+ return i;
+ }
+ return -1;
+}
+
+static HWND frame_window(Object *o)
+{
+ for (int depth = 0; o && depth < o->connection->objects.GetSize(); ++depth)
+ {
+ if (o->borrowed) return o->proxy ? o->global->hwnd : NULL;
+ Object *parent = NULL;
+ for (Object *role : o->connection->objects)
+ if (role->child == o)
+ {
+ parent = role->parent;
+ break;
+ }
+ o = parent;
+ }
+ return NULL;
+}
+
+static void dispose_object(wl_resource *resource)
+{
+ Object *o = (Object *)wl_resource_get_user_data(resource);
+ o->connection->objects.DeletePtr(o);
+ if (o->type == &wl_surface_interface)
+ for (int i = o->connection->objects.GetSize() - 1; i >= 0; --i)
+ {
+ Object *child = o->connection->objects.Get(i);
+ if (child->child == o) child->child = NULL;
+ if (child->parent != o) continue;
+ if (child->type == &wl_callback_interface) wl_resource_destroy(child->resource);
+ else child->parent = NULL;
+ }
+ if (o->proxy && !o->borrowed)
+ {
+ o->connection->by_proxy.Delete(o->proxy);
+ int opcode = destructor_opcode(o->type, wl_proxy_get_version(o->proxy));
+ if (opcode >= 0)
+ wl_proxy_marshal_flags(o->proxy, opcode, NULL, wl_proxy_get_version(o->proxy), WL_MARSHAL_FLAG_DESTROY);
+ else
+ wl_proxy_destroy(o->proxy);
+ }
+ delete o;
+}
+
+static int dispatch(const void *data, void *target, uint32_t opcode, const wl_message *m, wl_argument *in)
+{
+ Object *o = (Object *)data;
+ Connection *c = o->connection;
+ const bool request = target == o->resource;
+ const char *name = m->name;
+ if (request && o->borrowed)
+ {
+ if (!strcmp(name, "destroy"))
+ {
+ Global *g = o->global;
+ wl_resource_destroy(o->resource);
+ c->globals.DeletePtr(g, true);
+ }
+ else wl_resource_post_error(o->resource, WL_DISPLAY_ERROR_INVALID_METHOD, "the plug-in frame is read-only");
+ return 0;
+ }
+
+ wl_argument args[20];
+ int count = 0, new_id = -1;
+ bool deliver = true;
+ for (const char *s = m->signature; *s; ++s)
+ {
+ if ((*s >= '0' && *s <= '9') || *s == '?') continue;
+ if (count == (int)(sizeof(args)/sizeof(args[0]))) return -1;
+ const int i = count++;
+ args[i] = in[i];
+ if (*s == 'n') new_id = i;
+ else if (*s == 'o')
+ {
+ if (!in[i].o)
+ {
+ if (s == m->signature || s[-1] != '?') deliver = false;
+ }
+ else if (request)
+ {
+ Object *other = (Object *)wl_resource_get_user_data((wl_resource *)in[i].o);
+ if (!other || !other->proxy) deliver = false;
+ args[i].o = (wl_object *)(other ? other->proxy : NULL);
+ }
+ else
+ {
+ Object *other = c->by_proxy.Get((wl_proxy *)in[i].o);
+ if (!other) deliver = false;
+ args[i].o = (wl_object *)(other ? other->resource : NULL);
+ }
+ }
+ }
+
+ if (!request && (o->type == &wl_pointer_interface || o->type == &wl_keyboard_interface))
+ {
+ if (!strcmp(name, "enter"))
+ {
+ o->focused = deliver;
+ if (o->type == &wl_pointer_interface && o->global->pointer_serial != args[0].u)
+ {
+ o->global->pointer_serial = args[0].u;
+ o->global->cursor_set = false;
+ }
+ }
+ else if (!strcmp(name, "leave"))
+ {
+ deliver = deliver && o->focused;
+ o->focused = false;
+ }
+ else if (!o->focused && strcmp(name, "keymap") && strcmp(name, "repeat_info") && strcmp(name, "frame"))
+ deliver = false;
+ }
+ if (!request && o->type == &wl_pointer_interface &&
+ wl_resource_get_version(o->resource) >= WL_POINTER_FRAME_SINCE_VERSION)
+ {
+ if (opcode == WL_POINTER_FRAME)
+ {
+ deliver = o->pointer_frame_pending;
+ o->pointer_frame_pending = false;
+ }
+ else if (deliver)
+ {
+ if (opcode == WL_POINTER_ENTER && o->pointer_frame_pending) wl_pointer_send_frame(o->resource);
+ o->pointer_frame_pending = true;
+ }
+ }
+ if (!request && o->type == &wl_touch_interface)
+ {
+ if (!strcmp(name, "down"))
+ {
+ if (deliver) o->touches.Add(args[3].i);
+ }
+ else if (!strcmp(name, "up"))
+ {
+ int i = o->touches.Find(args[2].i);
+ deliver = i >= 0;
+ if (deliver) o->touches.Delete(i);
+ }
+ else if (!strcmp(name, "motion")) deliver = o->touches.Find(args[1].i) >= 0;
+ else if (!strcmp(name, "shape") || !strcmp(name, "orientation")) deliver = o->touches.Find(args[0].i) >= 0;
+ else if (!strcmp(name, "cancel"))
+ {
+ deliver = o->touches.GetSize() > 0;
+ o->touches.Resize(0, false);
+ }
+ }
+
+ const wl_interface *child = new_id < 0 ? NULL : m->types[new_id];
+ if (request)
+ {
+ const bool destroy = !strcmp(name, "destroy") || !strcmp(name, "release");
+ if (deliver && o->proxy)
+ {
+ if (((o->type == &wl_pointer_interface && opcode == WL_POINTER_SET_CURSOR) ||
+ (o->type == &wp_cursor_shape_device_v1_interface && opcode == WP_CURSOR_SHAPE_DEVICE_V1_SET_SHAPE)) &&
+ o->global && o->global->pointer_serial == args[0].u)
+ o->global->cursor_set = true;
+ const uint32_t version = wl_proxy_get_version(o->proxy);
+ const uint32_t id = child ? args[new_id].n : 0;
+ if (child) args[new_id].o = NULL;
+ wl_proxy *proxy = wl_proxy_marshal_array_flags(o->proxy, opcode, child, version,
+ destroy ? WL_MARSHAL_FLAG_DESTROY : 0, args);
+ if (child)
+ {
+ Object *obj = proxy ? c->add(proxy, child, id, version, false) : NULL;
+ if (!obj) wl_client_post_no_memory(c->peer);
+ else if (child == &wl_pointer_interface) obj->global = o->global;
+ else if (child == &wp_cursor_shape_device_v1_interface &&
+ opcode == WP_CURSOR_SHAPE_MANAGER_V1_GET_POINTER)
+ obj->global = ((Object *)wl_resource_get_user_data((wl_resource *)in[1].o))->global;
+ else if (o->type == &wl_surface_interface && opcode == WL_SURFACE_FRAME) obj->parent = o;
+ else if (child == &wl_subsurface_interface)
+ {
+ obj->child = (Object *)wl_resource_get_user_data((wl_resource *)in[1].o);
+ obj->parent = (Object *)wl_resource_get_user_data((wl_resource *)in[2].o);
+ }
+ }
+ }
+ else if (child && !c->add(NULL, child, args[new_id].n, wl_resource_get_version(o->resource), false))
+ wl_client_post_no_memory(c->peer);
+ if (destroy)
+ {
+ if (o->proxy) c->by_proxy.Delete(o->proxy);
+ o->proxy = NULL;
+ wl_resource_destroy(o->resource);
+ }
+ }
+ else if (deliver)
+ {
+ if (child)
+ {
+ wl_proxy *proxy = (wl_proxy *)in[new_id].o;
+ Object *obj = c->add(proxy, child, 0, wl_proxy_get_version(proxy), false);
+ if (!obj) wl_client_post_no_memory(c->peer);
+ else args[new_id].o = (wl_object *)obj->resource;
+ deliver = obj != NULL;
+ }
+ if (deliver)
+ {
+ wl_resource_post_event_array(o->resource, opcode, args);
+ if (o->type == &wl_callback_interface) wl_resource_destroy(o->resource);
+ }
+ }
+ for (const char *s = m->signature; *s; ++s)
+ if ((*s < '0' || *s > '9') && *s != '?')
+ {
+ if (*s == 'h') close(in->h);
+ ++in;
+ }
+ return 0;
+}
+
+Object *Connection::add(wl_proxy *proxy, const wl_interface *type, uint32_t id, uint32_t version, bool borrowed)
+{
+ wl_resource *resource = wl_resource_create(peer, type, version, id);
+ if (!resource) return NULL;
+ Object *o = new Object{this, type, proxy, resource, borrowed};
+ objects.Add(o);
+ wl_resource_set_dispatcher(resource, dispatch, o, o, dispose_object);
+ if (proxy && !borrowed)
+ {
+ by_proxy.Insert(proxy, o);
+ wl_proxy_add_dispatcher(proxy, dispatch, o, o);
+ }
+ return o;
+}
+
+static void bind_global(wl_client *client, void *data, uint32_t version, uint32_t id)
+{
+ Global *g = (Global *)data;
+ Connection *c = g->connection;
+ const bool borrowed = !g->name;
+ wl_proxy *proxy = borrowed ? g->parent : (wl_proxy *)wl_registry_bind(c->registry, g->name, g->type, version);
+ Object *o = c->add(proxy, g->type, id, version, borrowed);
+ if (!o)
+ {
+ wl_client_post_no_memory(client);
+ return;
+ }
+ o->global = g;
+ if (borrowed)
+ {
+ wl_global_destroy(g->global);
+ g->global = NULL;
+ }
+}
+
+static void registry_add(void *data, wl_registry *, uint32_t name, const char *iface, uint32_t version)
+{
+ static const wl_interface *const interfaces[] = {
+ &wl_compositor_interface, &wl_subcompositor_interface, &wl_shm_interface, &wl_seat_interface,
+ &wl_output_interface, &xdg_wm_base_interface, &zwp_linux_dmabuf_v1_interface, &wp_viewporter_interface,
+ &wp_fractional_scale_manager_v1_interface, &wp_cursor_shape_manager_v1_interface,
+ &wp_linux_drm_syncobj_manager_v1_interface,
+ };
+ Connection *c = (Connection *)data;
+ for (const wl_interface *type : interfaces)
+ if (!strcmp(iface, type->name))
+ {
+ Global *g = c->globals.Add(new Global{c, type, name});
+ g->global = wl_global_create(c->server, type, wdl_min(version, (uint32_t)type->version), g, bind_global);
+ break;
+ }
+}
+
+static void registry_remove(void *data, wl_registry *, uint32_t name)
+{
+ Connection *c = (Connection *)data;
+ for (Global *g : c->globals)
+ if (g->name == name && g->global)
+ {
+ wl_global_destroy(g->global);
+ g->global = NULL;
+ break;
+ }
+}
+
+static void sync_display(void *data, wl_protocol_logger_type direction, const wl_protocol_logger_message *msg)
+{
+ if (direction != WL_PROTOCOL_LOGGER_REQUEST || wl_resource_get_id(msg->resource) != 1 ||
+ msg->message_opcode != WL_DISPLAY_SYNC) return;
+ Connection *c = (Connection *)data;
+ if (wl_display_roundtrip_queue(c->upstream, c->queue) < 0)
+ wl_client_post_implementation_error(c->peer, "Wayland compositor disconnected");
+}
+
+static void peer_destroyed(wl_listener *listener, void *)
+{
+ Connection *c = static_cast<Connection::PeerListener *>(listener)->connection;
+ c->clear_objects();
+ c->peer = NULL;
+}
+
+static gpointer server_thread(gpointer data)
+{
+ ((Connection *)data)->run();
+ return NULL;
+}
+
+bool Connection::start(wl_display *session)
+{
+ upstream = session;
+ if (!upstream || !(server = wl_display_create()) || !(queue = wl_display_create_queue(upstream))) return false;
+ wl_display *wrapper = (wl_display *)wl_proxy_create_wrapper(upstream);
+ if (!wrapper) return false;
+ wl_proxy_set_queue((wl_proxy *)wrapper, queue);
+ registry = wl_display_get_registry(wrapper);
+ wl_proxy_wrapper_destroy(wrapper);
+ static const wl_registry_listener listener = {registry_add, registry_remove};
+ if (!registry || wl_registry_add_listener(registry, &listener, this) < 0 ||
+ wl_display_roundtrip_queue(upstream, queue) < 0) return false;
+
+ int fds[2];
+ if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds) < 0) return false;
+ peer = wl_client_create(server, fds[0]);
+ if (!peer)
+ {
+ close(fds[0]);
+ close(fds[1]);
+ return false;
+ }
+ gone.notify = peer_destroyed;
+ gone.connection = this;
+ wl_client_add_destroy_listener(peer, &gone);
+ barrier = wl_display_add_protocol_logger(server, sync_display, this);
+ if (!barrier)
+ {
+ close(fds[1]);
+ return false;
+ }
+ client = wl_display_connect_to_fd(fds[1]);
+ if (!client || !(client_registry = wl_display_get_registry(client)) ||
+ (stop = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) < 0) return false;
+ thread = g_thread_try_new("vst3-wayland", server_thread, this, NULL);
+ return thread != NULL;
+}
+
+void Connection::run()
+{
+ wl_event_loop *loop = wl_display_get_event_loop(server);
+ pollfd fds[] = {{wl_event_loop_get_fd(loop), POLLIN, 0}, {wl_display_get_fd(upstream), POLLIN, 0},
+ {stop, POLLIN, 0}};
+ for (;;)
+ {
+ {
+ WDL_MutexLock guard(&mutex);
+ if (wl_display_dispatch_queue_pending(upstream, queue) < 0) break;
+ wl_display_flush_clients(server);
+ }
+ if (wl_display_prepare_read_queue(upstream, queue) < 0) continue;
+ fds[1].events = POLLIN;
+ if (wl_display_flush(upstream) < 0 && errno == EAGAIN) fds[1].events |= POLLOUT;
+ int n;
+ do n = poll(fds, 3, -1); while (n < 0 && errno == EINTR);
+ if (n > 0 && (fds[1].revents & POLLIN) && !fds[2].revents)
+ {
+ if (wl_display_read_events(upstream) < 0) break;
+ }
+ else wl_display_cancel_read(upstream);
+ if (n < 0 || fds[2].revents || (fds[1].revents & (POLLERR | POLLHUP))) break;
+ if (fds[0].revents)
+ {
+ WDL_MutexLock guard(&mutex);
+ if (wl_event_loop_dispatch(loop, 0) < 0 || !peer) break;
+ }
+ }
+ WDL_MutexLock guard(&mutex);
+ if (peer) shutdown(wl_client_get_fd(peer), SHUT_RDWR);
+}
+
+wl_proxy *Connection::share(wl_proxy *parent, const wl_interface *type, HWND hwnd)
+{
+ WDL_MutexLock guard(&mutex);
+ if (!peer) return NULL;
+ for (Global *g : globals)
+ if (!g->name && g->parent == parent) return g->borrowed;
+ Global *g = globals.Add(new Global{this, type, 0});
+ g->parent = parent;
+ g->hwnd = hwnd;
+ uint32_t version = wl_proxy_get_version(parent);
+ g->global = wl_global_create(server, type, version, g, bind_global);
+ if (g->global)
+ g->borrowed = (wl_proxy *)wl_registry_bind(client_registry, wl_global_get_name(g->global, peer), type, version);
+ if (g->borrowed) return g->borrowed;
+ if (g->global) wl_global_destroy(g->global);
+ globals.DeletePtr(g, true);
+ return NULL;
+}
+
+void Connection::forget(wl_proxy *parent)
+{
+ WDL_MutexLock guard(&mutex);
+ for (Object *o : objects)
+ if (o->borrowed && o->proxy == parent) o->proxy = NULL;
+ for (Global *g : globals)
+ if (!g->name && g->parent == parent)
+ {
+ g->parent = NULL;
+ if (g->borrowed)
+ {
+ const uint32_t version = wl_proxy_get_version(g->borrowed);
+ wl_proxy_marshal_flags(g->borrowed, destructor_opcode(g->type, version), NULL, version, WL_MARSHAL_FLAG_DESTROY);
+ g->borrowed = NULL;
+ wl_display_flush(client);
+ }
+ }
+}
+
+Connection::~Connection()
+{
+ if (thread)
+ {
+ uint64_t one = 1;
+ while (write(stop, &one, sizeof(one)) < 0 && errno == EINTR) {}
+ g_thread_join(thread);
+ }
+ if (stop >= 0) close(stop);
+ for (Global *g : globals)
+ if (g->borrowed) wl_proxy_destroy(g->borrowed);
+ if (client_registry) wl_registry_destroy(client_registry);
+ if (client) wl_display_disconnect(client);
+ clear_objects();
+ if (barrier) wl_protocol_logger_destroy(barrier);
+ if (server)
+ {
+ wl_display_destroy_clients(server);
+ wl_display_destroy(server);
+ }
+ if (registry) wl_registry_destroy(registry);
+ if (queue) wl_event_queue_destroy(queue);
+ if (upstream) wl_display_flush(upstream);
+}
+
+}
+
+wl_display *swell_wayland_client_open(wl_display *session)
+{
+ Connection *c = new Connection;
+ if (c->start(session))
+ {
+ clients.Add(c);
+ return c->client;
+ }
+ delete c;
+ return NULL;
+}
+
+bool swell_wayland_client_close(wl_display *client)
+{
+ for (Connection *c : clients)
+ if (c->client == client)
+ {
+ clients.DeletePtr(c, true);
+ return true;
+ }
+ return false;
+}
+
+wl_proxy *swell_wayland_client_share(wl_display *client, wl_proxy *object, const wl_interface *type, HWND hwnd)
+{
+ if (object)
+ for (Connection *c : clients)
+ if (c->client == client) return c->share(object, type, hwnd);
+ return NULL;
+}
+
+HWND swell_wayland_client_window(wl_surface *surface)
+{
+ for (Connection *c : clients)
+ {
+ WDL_MutexLock guard(&c->mutex);
+ if (Object *o = c->by_proxy.Get((wl_proxy *)surface)) return frame_window(o);
+ }
+ return NULL;
+}
+
+void swell_wayland_client_pointer_enter(wl_surface *surface, uint32_t seat, uint32_t serial)
+{
+ for (Connection *c : clients)
+ {
+ WDL_MutexLock guard(&c->mutex);
+ if (Object *o = c->by_proxy.Get((wl_proxy *)surface))
+ {
+ for (Global *g : c->globals)
+ if (g->name == seat && g->cursor_set && g->pointer_serial == serial) return;
+ swell_vst3_default_cursor(frame_window(o));
+ return;
+ }
+ }
+}
+
+void swell_wayland_client_forget(wl_proxy *object)
+{
+ if (object)
+ for (Connection *c : clients) c->forget(object);
+}
diff --git a/WDL/swell/swell-xwayland-bridge.cpp b/WDL/swell/swell-xwayland-bridge.cpp
index 6afe0f9b..0b7bc8c6 100644
--- a/WDL/swell/swell-xwayland-bridge.cpp
+++ b/WDL/swell/swell-xwayland-bridge.cpp
@@ -70,6 +70,9 @@
#include <unistd.h>
#include "swell-xwayland-bridge.h"
#include "swell-wayland.h"
+#ifdef SWELL_WAYLAND_VST3
+#include "swell-vst3-wayland.h"
+#endif
#include "swell-internal.h"
#include "../assocarray.h"
#include "../wdlstring.h"
@@ -2232,6 +2235,7 @@ struct Capture
struct XWaylandBridge
{
+ struct SWELL_VST3Parent *native = NULL;
xcb_connection_t *connection = NULL;
xcb_window_t parent = 0;
xcb_atom_t monitor = XCB_NONE;
@@ -3738,8 +3742,7 @@ static void place_container(xcb_connection_t *connection, xcb_window_t container
static bool try_create_plugin(HWND hwnd)
{
XWaylandBridge *bridge = g_bridges.Get(hwnd);
- if (!bridge) return true;
- if (bridge->external_gl) return true;
+ if (!bridge || bridge->native || bridge->external_gl) return true;
if (!bridge->placed)
{
xw_size(hwnd);
@@ -4415,6 +4418,9 @@ void init_private_xwayland()
static void detach_view(XWaylandBridge *bridge)
{
+#ifdef SWELL_WAYLAND_VST3
+ if (bridge->native) swell_vst3_position(bridge->native, NULL);
+#endif
Capture *c = bridge->cap;
if (c)
{
@@ -4435,16 +4441,21 @@ static void detach_view(XWaylandBridge *bridge)
}
xcb_flush(c->connection);
}
- if (bridge->view) swell_wayland_view_destroy(bridge->view);
+ SWELL_WaylandView *view = bridge->view;
bridge->view = NULL;
bridge->placed = false;
if (c) c->view = NULL;
+ if (view) swell_wayland_view_destroy(view);
}
static void xw_destroy(HWND hwnd)
{
XWaylandBridge *bridge = g_bridges.Get(hwnd);
if (!bridge) return;
+#ifdef SWELL_WAYLAND_VST3
+ swell_vst3_destroy_parent(bridge->native);
+ bridge->native = NULL;
+#endif
g_source_destroy(bridge->event_source);
g_source_unref(bridge->event_source);
KillTimer(hwnd, 1);
@@ -4487,6 +4498,13 @@ static void xw_size(HWND hwnd)
{
XWaylandBridge *bridge = g_bridges.Get(hwnd);
if (!bridge) return;
+#ifdef SWELL_WAYLAND_VST3
+ if (bridge->native)
+ {
+ swell_vst3_position(bridge->native, hwnd);
+ return;
+ }
+#endif
if (bridge->external_gl) return;
if (!IsWindowVisible(hwnd))
{
@@ -4836,6 +4854,43 @@ HWND xw_bridge_create(HWND parent, void **wref, const RECT *rect, const char *br
return hwnd;
}
+#ifdef SWELL_WAYLAND_VST3
+HWND swell_vst3_find_parent(void *xid)
+{
+ for (const auto &entry : g_bridges)
+ if (entry.val->parent == (uintptr_t)xid && !entry.val->native && !entry.val->cap && !entry.val->external_gl)
+ {
+ HWND hwnd = entry.key;
+ XWaylandBridge *bridge = entry.val;
+ bridge->native = swell_vst3_create_parent();
+ if (!bridge->native) return NULL;
+ hwnd->Retain();
+ KillTimer(hwnd, 1);
+ detach_view(bridge);
+ if (!hwnd->m_hashaddestroy) xw_size(hwnd);
+ const bool valid = !hwnd->m_hashaddestroy && g_bridges.Get(hwnd) == bridge;
+ hwnd->Release();
+ return valid ? hwnd : NULL;
+ }
+ return NULL;
+}
+
+void swell_vst3_unbind(HWND hwnd)
+{
+ XWaylandBridge *bridge = g_bridges.Get(hwnd);
+ if (!bridge || !bridge->native) return;
+ swell_vst3_destroy_parent(bridge->native);
+ bridge->native = NULL;
+ if (!hwnd->m_hashaddestroy) SetTimer(hwnd, 1, 100, NULL);
+}
+
+SWELL_VST3Parent *swell_vst3_parent_view(HWND hwnd)
+{
+ XWaylandBridge *bridge = g_bridges.Get(hwnd);
+ return bridge ? bridge->native : NULL;
+}
+#endif
+
bool xw_bridge_key_wanted_by_host(HWND hwnd, uint32_t code, unsigned state)
{
XWaylandBridge *bridge = g_bridges.Get(hwnd);
@@ -4845,7 +4900,7 @@ bool xw_bridge_key_wanted_by_host(HWND hwnd, uint32_t code, unsigned state)
void *hook = GetProp(hwnd, "SWELL_XBRIDGE_KBHOOK_CHECK");
HWND parent = hook ? GetParent(hwnd) : NULL;
if (!parent) return false;
- if (input_focus_in(bridge->connection, bridge->parent)) return false;
+ if (!bridge->native && input_focus_in(bridge->connection, bridge->parent)) return false;
hwnd->Retain();
const bool handled = SendMessage(parent, (UINT)(INT_PTR)hook, code, state) != 0;
const bool valid = !hwnd->m_hashaddestroy && g_bridges.Get(hwnd) == bridge && GetFocus() == hwnd;
diff --git a/WDL/swell/swell.cpp b/WDL/swell/swell.cpp
index 9e2d2707..5c325df7 100644
--- a/WDL/swell/swell.cpp
+++ b/WDL/swell/swell.cpp
@@ -36,6 +36,9 @@
#include "swell-internal.h"
+#ifdef SWELL_WAYLAND_VST3
+#include "swell-vst3-wayland.h"
+#endif
#ifndef SWELL_TARGET_OSX
#include "swell-platform.h"
#endif
@@ -931,6 +934,14 @@ void *GetProcAddress(HINSTANCE hInst, const char *procName)
#endif
if (rec->instptr) ret=(void *)dlsym(rec->instptr, procName);
if (ret) rec->lastSymbolRequested=ret;
+#ifdef SWELL_WAYLAND_VST3
+ if (ret && !strcmp(procName, "GetPluginFactory") && swell_platform_is_wayland() &&
+ dlsym(rec->instptr, "ModuleEntry") && dlsym(rec->instptr, "ModuleExit"))
+ {
+ WDL_MutexLock lock(&s_libraryMutex);
+ return swell_vst3_factory(&rec->vst3_factory, ret);
+ }
+#endif
return ret;
}
@@ -966,6 +977,9 @@ BOOL FreeLibrary(HINSTANCE hInst)
CFRelease((CFBundleRef)rec->bundleinstptr);
}
#endif
+#ifdef SWELL_WAYLAND_VST3
+ if (dofree) swell_vst3_free_factory(rec->vst3_factory);
+#endif
if (rec->instptr) dlclose(rec->instptr);
if (dofree) free(rec);