Implement CLAP callback requests

This commit is contained in:
Robbert van der Helm
2022-09-11 18:40:46 +02:00
parent 7151544f99
commit 5d31191806
5 changed files with 54 additions and 21 deletions
@@ -65,5 +65,28 @@ clap_host_proxy::host_request_process(const struct clap_host* host) {
void CLAP_ABI
clap_host_proxy::host_request_callback(const struct clap_host* host) {
// TODO: Implement
assert(host && host->host_data);
auto self = static_cast<clap_host_proxy*>(host->host_data);
// TODO: Log
// Only schedule a `clap_plugin::on_main_thread()` call if we don't already
// have a pending one. This limits the number of unnecessarily stacked
// calls.
bool expected = false;
if (self->has_pending_host_callbacks_.compare_exchange_strong(expected,
true)) {
// We're acquiring a lock on the instance and then move it into the task
// to prevent this instance from being removed before this callback has
// been run
auto instance_lock =
self->bridge_.get_instance(self->owner_instance_id());
self->bridge_.main_context_.schedule_task(
[self, instance_lock = std::move(instance_lock)]() {
const auto& [instance, _] = instance_lock;
self->has_pending_host_callbacks_.store(false);
instance.plugin->on_main_thread(instance.plugin.get());
});
}
}
@@ -16,6 +16,8 @@
#pragma once
#include <atomic>
#include <clap/host.h>
#include "../../common/serialization/clap/plugin-factory.h"
@@ -78,4 +80,12 @@ class clap_host_proxy {
* the start of the struct and directly casting the `clap_host_t*`.
*/
const clap_host_t host_vtable_;
/**
* Keeps track of whether there are pending host callbacks. Used to prevent
* calling `clap_plugin::on_main_thread()` multiple times in a row when the
* plugin calls `clap_host::request_callback()` multiple times before
* `clap_plugin::on_main_thread()` is called.
*/
std::atomic_bool has_pending_host_callbacks_ = false;
};