Skip to content

Commit

Permalink
Make sure we run the gui callback on the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Sep 17, 2024
1 parent 724a3c5 commit 3f2e77d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
14 changes: 11 additions & 3 deletions plugins/core-plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,14 @@ namespace clap {

bool CorePlugin::guiCreate(const char *api, bool isFloating) noexcept {
_guiFactory = LocalGuiFactory::getInstance();
if (!_guiFactory)
if (_guiFactory) {
_guiHandle = _guiFactory->createGui(*this);
} else {
_guiFactory = ThreadedGuiFactory::getInstance();

_guiHandle = _guiFactory->createGui(*this);
if (!_threadedGuiListenerProxy)
_threadedGuiListenerProxy = std::make_unique<ThreadedGuiListenerProxy>(*this);
_guiHandle = _guiFactory->createGui(*_threadedGuiListenerProxy);
}

if (!_guiHandle)
return false;
Expand Down Expand Up @@ -282,6 +286,10 @@ namespace clap {
//---------------------//
// AbstractGuiListener //
//---------------------//
void CorePlugin::onGuiRunOnMainThread(std::function<void()> callback) {
runOnMainThread(std::move(callback));
}

void CorePlugin::onGuiPoll() {
_pluginToGuiQueue.consume([this](clap_id paramId, const CorePlugin::PluginToGuiValue &value) {
_guiHandle->gui().updateParameter(paramId, value.value, value.mod);
Expand Down
4 changes: 4 additions & 0 deletions plugins/core-plugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "context.hh"
#include "gui/abstract-gui-factory.hh"
#include "gui/abstract-gui-listener.hh"
#include "gui/threaded-gui-listener-proxy.hh"
#include "intrusive-list.hh"
#include "parameters.hh"
#include "path-provider.hh"
Expand Down Expand Up @@ -171,6 +172,7 @@ namespace clap {
//---------------------//
// AbstractGuiListener //
//---------------------//
void onGuiRunOnMainThread(std::function<void()> callback) override;
void onGuiPoll() override;
void pollVoiceInfo();
void onGuiParamBeginAdjust(clap_id paramId) override;
Expand Down Expand Up @@ -314,5 +316,7 @@ namespace clap {
bool _canRedo{false};
std::optional<std::string> _undoName;
std::optional<std::string> _redoName;

std::unique_ptr<ThreadedGuiListenerProxy> _threadedGuiListenerProxy;
};
} // namespace clap
2 changes: 2 additions & 0 deletions plugins/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ add_library(
threaded-gui-factory.cc
threaded-gui-proxy.hh
threaded-gui-proxy.cc
threaded-gui-listener-proxy.hh
threaded-gui-listener-proxy.cc
timer.hh
timer.cc
cf-timer.hh
Expand Down
3 changes: 3 additions & 0 deletions plugins/gui/abstract-gui-listener.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <variant>
#include <vector>
#include <functional>

#include <clap/clap.h>

Expand All @@ -17,6 +18,8 @@ namespace clap {
public:
virtual ~AbstractGuiListener();

virtual void onGuiRunOnMainThread(std::function<void()> callback) = 0;

// timer based polling of new parameter changes, transport value, ...
// the plugin shall transmit them via
virtual void onGuiPoll() = 0;
Expand Down
48 changes: 48 additions & 0 deletions plugins/gui/threaded-gui-listener-proxy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "threaded-gui-listener-proxy.hh"

namespace clap {
ThreadedGuiListenerProxy::ThreadedGuiListenerProxy(AbstractGuiListener &guiListener)
: _guiListener(guiListener) {}

void ThreadedGuiListenerProxy::onGuiRunOnMainThread(std::function<void()> callback) {
_guiListener.onGuiRunOnMainThread(std::move(callback));
}

void ThreadedGuiListenerProxy::onGuiPoll() {
onGuiRunOnMainThread([this] { _guiListener.onGuiPoll(); });
}

void ThreadedGuiListenerProxy::onGuiParamBeginAdjust(clap_id paramId) {
onGuiRunOnMainThread([this, paramId] { _guiListener.onGuiParamBeginAdjust(paramId); });
}

void ThreadedGuiListenerProxy::onGuiParamAdjust(clap_id paramId, double value) {
onGuiRunOnMainThread([this, paramId, value] { _guiListener.onGuiParamAdjust(paramId, value); });
}

void ThreadedGuiListenerProxy::onGuiParamEndAdjust(clap_id paramId) {
onGuiRunOnMainThread([this, paramId] { _guiListener.onGuiParamEndAdjust(paramId); });
}

void ThreadedGuiListenerProxy::onGuiSetTransportIsSubscribed(bool isSubscribed) {
onGuiRunOnMainThread(
[this, isSubscribed] { _guiListener.onGuiSetTransportIsSubscribed(isSubscribed); });
}

void ThreadedGuiListenerProxy::onGuiWindowClosed(bool wasDestroyed) {
onGuiRunOnMainThread([this, wasDestroyed] { _guiListener.onGuiWindowClosed(wasDestroyed); });
}

void ThreadedGuiListenerProxy::onGuiUndo() {
onGuiRunOnMainThread([this] { _guiListener.onGuiUndo(); });
}

void ThreadedGuiListenerProxy::onGuiRedo() {
onGuiRunOnMainThread([this] { _guiListener.onGuiRedo(); });
}

void ThreadedGuiListenerProxy::onGuiInvoke(const std::string &method,
const InvocationArgumentsType &args) {
onGuiRunOnMainThread([this, method, args] { _guiListener.onGuiInvoke(method, args); });
}
} // namespace clap
30 changes: 30 additions & 0 deletions plugins/gui/threaded-gui-listener-proxy.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "abstract-gui-listener.hh"

namespace clap {
class ThreadedGuiListenerProxy final : public AbstractGuiListener {
public:
ThreadedGuiListenerProxy(AbstractGuiListener &guiListener);

void onGuiRunOnMainThread(std::function<void()> callback) override;

void onGuiPoll() override;

void onGuiParamBeginAdjust(clap_id paramId) override;
void onGuiParamAdjust(clap_id paramId, double value) override;
void onGuiParamEndAdjust(clap_id paramId) override;

void onGuiSetTransportIsSubscribed(bool isSubscribed) override;

void onGuiWindowClosed(bool wasDestroyed) override;

void onGuiUndo() override;
void onGuiRedo() override;

void onGuiInvoke(const std::string &method, const InvocationArgumentsType &args) override;

private:
AbstractGuiListener &_guiListener;
};
} // namespace clap

0 comments on commit 3f2e77d

Please sign in to comment.