Skip to content

Commit

Permalink
Intial glue code for plugin undo
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed May 4, 2024
1 parent af2e1a8 commit bf1dfbc
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/clap/helpers/host-proxy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ namespace clap { namespace helpers {
bool requestDirectory(bool isShared) const noexcept;
void releaseDirectory(bool isShared) const noexcept;

////////////////////
// clap_host_undo //
////////////////////
bool canUseHostUndo() const noexcept;
void undoBeginChange() const noexcept;
void undoCancelChange() const noexcept;
void undoChangeMade(const char *name,
const void *redo_delta,
size_t redo_delta_size,
const void *undo_delta,
size_t undo_delta_size) const noexcept;
void undoUndo(const clap_host_t *host) const noexcept;
void undoRedo(const clap_host_t *host) const noexcept;
void undoSetWantsContextInfo(const clap_host_t *host, bool wants_info) const noexcept;

protected:
void ensureMainThread(const char *method) const noexcept;
void ensureAudioThread(const char *method) const noexcept;
Expand All @@ -211,5 +226,6 @@ namespace clap { namespace helpers {
const clap_host_tail *_hostTail = nullptr;
const clap_host_context_menu *_hostContextMenu = nullptr;
const clap_host_preset_load *_hostPresetLoad = nullptr;
const clap_host_undo *_hostUndo = nullptr;
};
}} // namespace clap::helpers
64 changes: 64 additions & 0 deletions include/clap/helpers/host-proxy.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace clap { namespace helpers {
getExtension(_hostPresetLoad, CLAP_EXT_PRESET_LOAD);
if (!_hostPresetLoad)
getExtension(_hostPresetLoad, CLAP_EXT_PRESET_LOAD);
getExtension(_hostUndo, CLAP_EXT_UNDO);
}

template <MisbehaviourHandler h, CheckingLevel l>
Expand Down Expand Up @@ -681,4 +682,67 @@ namespace clap { namespace helpers {
ensureMainThread("resource_directory.release_directory");
_hostResourceDirectory->release_directory(_host, isShared);
}

////////////////////
// clap_host_undo //
////////////////////
template <MisbehaviourHandler h, CheckingLevel l>
bool HostProxy<h, l>::canUseHostUndo() const noexcept {
if (!_hostUndo)
return false;

if (_hostUndo->begin_change && _hostUndo->cancel_change && _hostUndo->change_made &&
_hostUndo->undo && _hostUndo->redo && _hostUndo->set_wants_context_info)
return true;

return false;
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoBeginChange() const noexcept {
assert(canUseUndo());
ensureMainThread("undo.begin_change");
_hostUndo->begin_change(_host);
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoCancelChange() const noexcept {
assert(canUseUndo());
ensureMainThread("undo.cancel_change");
_hostUndo->cancel_change(_host);
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoChangeMade(const char *name,
const void *redo_delta,
size_t redo_delta_size,
const void *undo_delta,
size_t undo_delta_size) const noexcept {
assert(canUseUndo());
ensureMainThread("undo.change_made");
_hostUndo->change_made(_host, name, redo_delta, redo_delta_size, undo_delta, undo_delta_size);
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoUndo(const clap_host_t *host) const noexcept {
assert(canUseUndo());
ensureMainThread("undo.undo");
_hostUndo->undo(_host);
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoRedo(const clap_host_t *host) const noexcept {
assert(canUseUndo());
ensureMainThread("undo.redo");
_hostUndo->redo(_host);
}

template <MisbehaviourHandler h, CheckingLevel l>
void HostProxy<h, l>::undoSetWantsContextInfo(const clap_host_t *host,
bool wants_info) const noexcept {
assert(canUseUndo());
ensureMainThread("undo.set_wants_context_info");
_hostUndo->set_wants_context_info(_host, wants_info);
}

}} // namespace clap::helpers
26 changes: 26 additions & 0 deletions include/clap/helpers/plugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ namespace clap { namespace helpers {
virtual bool implementsVoiceInfo() const noexcept { return false; }
virtual bool voiceInfoGet(clap_voice_info *info) noexcept { return false; }

//------------------//
// clap_plugin_undo //
//------------------//
virtual bool implementsUndo() const noexcept { return false; }
virtual void undoGetDeltaProperties(clap_undo_delta_properties_t *properties) noexcept;
virtual bool undoCanUseDeltaFormatVersion(clap_id format_version) noexcept;
virtual bool
undoApplyDelta(clap_id format_version, const void *delta, size_t delta_size) noexcept;
virtual void
undoSetContextInfo(uint64_t flags, const char *undo_name, const char *redo_name) noexcept;

/////////////
// Logging //
/////////////
Expand Down Expand Up @@ -523,6 +534,20 @@ namespace clap { namespace helpers {
char *path,
uint32_t path_size) noexcept;

// clap_plugin_undo
static void clapUndoGetDeltaProperties(const clap_plugin_t *plugin,
clap_undo_delta_properties_t *properties) noexcept;
static bool clapUndoCanUseDeltaFormatVersion(const clap_plugin_t *plugin,
clap_id format_version) noexcept;
static bool clapUndoApplyDelta(const clap_plugin_t *plugin,
clap_id format_version,
const void *delta,
size_t delta_size) noexcept;
static void clapUndoSetContextInfo(const clap_plugin_t *plugin,
uint64_t flags,
const char *undo_name,
const char *redo_name) noexcept;

// interfaces
static const clap_plugin_audio_ports _pluginAudioPorts;
static const clap_plugin_audio_ports_config _pluginAudioPortsConfig;
Expand All @@ -546,6 +571,7 @@ namespace clap { namespace helpers {
static const clap_plugin_voice_info _pluginVoiceInfo;
static const clap_plugin_context_menu _pluginContextMenu;
static const clap_plugin_resource_directory _pluginResourceDirectory;
static const clap_plugin_undo _pluginUndo;

// state
bool _wasInitialized = false;
Expand Down
50 changes: 50 additions & 0 deletions include/clap/helpers/plugin.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ namespace clap { namespace helpers {
clapTailGet,
};

template <MisbehaviourHandler h, CheckingLevel l>
const clap_plugin_undo Plugin<h, l>::_pluginUndo = {
clapUndoGetDeltaProperties,
clapUndoCanUseDeltaFormatVersion,
clapUndoApplyDelta,
clapUndoSetContextInfo,
};

template <MisbehaviourHandler h, CheckingLevel l>
Plugin<h, l>::Plugin(const clap_plugin_descriptor *desc, const clap_host *host) : _host(host) {
_plugin.plugin_data = this;
Expand Down Expand Up @@ -496,6 +504,8 @@ namespace clap { namespace helpers {
if (self.enableDraftExtensions()) {
if (!strcmp(id, CLAP_EXT_RESOURCE_DIRECTORY) && self.implementsResourceDirectory())
return &_pluginResourceDirectory;
if (!strcmp(id, CLAP_EXT_UNDO) && self.implementsUndo())
return &_pluginUndo;
}

return self.extension(id);
Expand Down Expand Up @@ -1645,6 +1655,46 @@ namespace clap { namespace helpers {
return self.resourceDirectoryGetFilePath(index, path, path_size);
}

//------------------//
// clap_plugin_undo //
//------------------//
template <MisbehaviourHandler h, CheckingLevel l>
void
Plugin<h, l>::clapUndoGetDeltaProperties(const clap_plugin_t *plugin,
clap_undo_delta_properties_t *properties) noexcept {
auto &self = from(plugin);
self.ensureMainThread("clap_undo.get_delta_properties");
return self.undoGetDeltaProperties(properties);
}

template <MisbehaviourHandler h, CheckingLevel l>
bool Plugin<h, l>::clapUndoCanUseDeltaFormatVersion(const clap_plugin_t *plugin,
clap_id format_version) noexcept {
auto &self = from(plugin);
self.ensureMainThread("clap_undo.can_use_delta_format_version");
return self.undoCanUseDeltaFormatVersion(format_version);
}

template <MisbehaviourHandler h, CheckingLevel l>
bool Plugin<h, l>::clapUndoApplyDelta(const clap_plugin_t *plugin,
clap_id format_version,
const void *delta,
size_t delta_size) noexcept {
auto &self = from(plugin);
self.ensureMainThread("clap_undo.apply_delta");
return self.undoApplyDelta(format_version, delta, delta_size);
}

template <MisbehaviourHandler h, CheckingLevel l>
void Plugin<h, l>::clapUndoSetContextInfo(const clap_plugin_t *plugin,
uint64_t flags,
const char *undo_name,
const char *redo_name) noexcept {
auto &self = from(plugin);
self.ensureMainThread("clap_undo.set_context_info");
return self.undoSetContextInfo(flags, undo_name, redo_name);
}

/////////////
// Logging //
/////////////
Expand Down

0 comments on commit bf1dfbc

Please sign in to comment.