Allow GUIs to optionally update on a timer

Otherwise plugins can't update their editors when the GUI is being blocked.
This commit is contained in:
Robbert van der Helm
2020-03-28 18:03:20 +01:00
parent d52989acc5
commit 8ec0ed4c3c
3 changed files with 83 additions and 10 deletions
+71 -6
View File
@@ -1,5 +1,8 @@
#include "editor.h"
// The Win32 API requires you to hardcode identifiers for tiemrs
constexpr size_t idle_timer_id = 1337;
constexpr char xembed_proeprty[] = "_XEMBED";
constexpr char xembed_info_proeprty[] = "_XEMBED_INFO";
@@ -33,7 +36,7 @@ Editor::Editor(std::string window_class_name)
->atom;
}
HWND Editor::open() {
HWND Editor::open(AEffect* effect) {
// Create a window without any decoratiosn for easy embedding. The
// combination of `WS_EX_TOOLWINDOW` and `WS_POPUP` causes the window to be
// drawn without any decorations (making resizes behave as you'd expect) and
@@ -44,14 +47,17 @@ HWND Editor::open() {
reinterpret_cast<LPCSTR>(window_class),
"yabridge plugin", WS_POPUP, CW_USEDEFAULT,
CW_USEDEFAULT, 2048, 2048, nullptr, nullptr,
GetModuleHandle(nullptr), nullptr),
GetModuleHandle(nullptr), this),
&DestroyWindow);
// Needed to send update messages on a timer
plugin = effect;
return win32_handle->get();
}
void Editor::close() {
// RAII will destroy the window for us
// RAII will destroy the window and tiemrs for us
win32_handle = std::nullopt;
// TODO: Do we need to do something on the X11 side or does the host do
@@ -98,9 +104,14 @@ bool Editor::embed_into(const size_t parent_window_handle) {
xcb_map_window(x11_connection.get(), child_window_handle);
xcb_flush(x11_connection.get());
// TODO: Add a timer after adding a seperate thread for midi events so that
// the GUI can redraw even while dropdowns are open.
ShowWindow(win32_handle->get(), SW_SHOWNORMAL);
// The Win32 API will block the `DispatchMessage` call when opening e.g. a
// dropdown, but it will still allow timers to be run so the GUI can still
// update in the background. Because of this we send `effEditIdle` to the
// plugin on a timer. The refresh rate is purposely fairly low since we
// we'll also trigger this manually in `Editor::handle_events()` whenever
// the plugin is not busy.
SetTimer(win32_handle->get(), idle_timer_id, 100, nullptr);
return true;
}
@@ -109,13 +120,26 @@ void Editor::handle_events() {
// Process any remaining events, otherwise we won't be able to interact with
// the window
if (win32_handle.has_value()) {
bool gui_was_updated = false;
MSG msg;
// The second argument has to be null since we not only want to handle
// events for this window but also for all child windows (i.e.
// dropdowns). I spent way longer debugging this than I want to admit.
while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_TIMER && msg.wParam == idle_timer_id) {
gui_was_updated = true;
}
}
// Make sure that the GUI always gets updated at least once for every
// `effEditIdle` call the host has sent to improve responsiveness when
// the GUI isn't being blocked.
if (!gui_was_updated) {
SendMessage(win32_handle->get(), WM_TIMER, idle_timer_id, 0);
}
}
}
@@ -149,12 +173,53 @@ void Editor::send_xembed_event(const xcb_window_t& window,
reinterpret_cast<char*>(&event));
}
LRESULT CALLBACK window_proc(HWND handle,
UINT message,
WPARAM wParam,
LPARAM lParam) {
switch (message) {
case WM_CREATE: {
const auto window_parameters =
reinterpret_cast<CREATESTRUCT*>(lParam);
const auto editor =
static_cast<Editor*>(window_parameters->lpCreateParams);
if (editor == nullptr) {
break;
}
// Sent when the window is first being created. `lParam` here
// contains the last argument of `CreateWindowEx`, which was a
// pointer to the `Editor` object. We need to attach this to the
// window handle so we can access our VST plugin instance later.
SetWindowLongPtr(handle, GWLP_USERDATA,
reinterpret_cast<size_t>(editor));
} break;
case WM_TIMER: {
auto editor = reinterpret_cast<Editor*>(
GetWindowLongPtr(handle, GWLP_USERDATA));
if (editor == nullptr || wParam != idle_timer_id) {
break;
}
// We'll send idle messages on a timer. This way the plugin will get
// these either when the host sends `effEditIdle` themself, or
// periodically when the GUI is being blocked by a dropdown or a
// message box.
editor->plugin->dispatcher(editor->plugin, effEditIdle, 0, 0,
nullptr, 0);
return 0;
} break;
}
return DefWindowProc(handle, message, wParam, lParam);
}
ATOM register_window_class(std::string window_class_name) {
WNDCLASSEX window_class{};
window_class.cbSize = sizeof(WNDCLASSEX);
window_class.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
window_class.lpfnWndProc = DefWindowProc;
window_class.lpfnWndProc = window_proc;
window_class.hInstance = GetModuleHandle(nullptr);
window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
window_class.hbrBackground = CreateHatchBrush(HS_CROSS, RGB(255, 0, 255));
+7 -1
View File
@@ -32,8 +32,11 @@ class Editor {
/**
* Open a window and return a handle to the new Win32 window that can be
* used by the hosted VST plugin.
*
* @param effect The plugin this window is being created for. Used to send
* `effEditIdle` messages on a timer.
*/
HWND open();
HWND open(AEffect* effect);
void close();
/**
@@ -64,6 +67,9 @@ class Editor {
*/
bool embed_into(const size_t parent_window_handle);
// Needed to handle idle updates through a timer
AEffect* plugin;
private:
/**
* Return the X11 window handle for the window if it's currently open.
+5 -3
View File
@@ -222,10 +222,12 @@ intptr_t PluginBridge::dispatch_wrapper(AEffect* plugin,
// Because of the way the Win32 API works we have to process events
// on the same thread the window was created, and that thread is the
// thread that's handling dispatcher calls
// To allow the GUI to update even when this thread gets blocked
// (e.g. when a dropdown is open), the actual `effEditIdle` event
// gets sent to the plugin on a timer.
editor.handle_events();
return plugin->dispatcher(plugin, opcode, index, value, data,
option);
return 1;
break;
case effClose: {
// Closing the editor will also shut down the thread that's
@@ -236,7 +238,7 @@ intptr_t PluginBridge::dispatch_wrapper(AEffect* plugin,
option);
} break;
case effEditOpen: {
const auto win32_handle = editor.open();
const auto win32_handle = editor.open(plugin);
// The plugin will return 0 if it can not open its
// editor window (or if it does not support it, but in