Last active
October 12, 2022 02:43
-
-
Save cfillion/335695dee9b7acf02fd03b8dfe673c6e to your computer and use it in GitHub Desktop.
Proof of concept: Add prevent UI refresh to REAPER native custom actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstring> | |
#define REAPERAPI_IMPLEMENT | |
#include <reaper_plugin_functions.h> | |
const char *(*__localizeFunc)(const char *str, const char *subctx, int flags); | |
static bool IsCustomAction(KbdSectionInfo *section, const int id) | |
{ | |
// logic from SWS's IsMacroOrScript | |
const char *prefix = __localizeFunc("Custom", "actions", 0); | |
const size_t prefixLen = strlen(prefix); | |
const char *actionName = kbd_getTextFromCmd(id, section); | |
return !strncmp(actionName, prefix, prefixLen) && actionName[prefixLen] == ':'; | |
} | |
static bool commandHook(KbdSectionInfo *section, const int command, int, int, int, HWND) | |
{ | |
static int runCustomAction = 0; | |
if(section->uniqueID != 0 || runCustomAction == command || !IsCustomAction(section, command)) | |
return false; | |
runCustomAction = command; | |
PreventUIRefresh(1); | |
Main_OnCommand(command, 0); // FIXME: non-main sections! | |
PreventUIRefresh(-1); | |
runCustomAction = 0; | |
return true; | |
} | |
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT( | |
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec) | |
{ | |
if(!rec) { | |
plugin_register("-hookcommand2", reinterpret_cast<void *>(commandHook)); | |
return 0; | |
} | |
if(rec->caller_version != REAPER_PLUGIN_VERSION || !rec->GetFunc) | |
return 0; | |
__localizeFunc = (decltype(__localizeFunc)) rec->GetFunc("__localizeFunc"); | |
kbd_getTextFromCmd = (decltype(kbd_getTextFromCmd))rec->GetFunc("kbd_getTextFromCmd"); | |
Main_OnCommand = (decltype(Main_OnCommand)) rec->GetFunc("Main_OnCommand"); | |
plugin_register = (decltype(plugin_register)) rec->GetFunc("plugin_register"); | |
PreventUIRefresh = (decltype(PreventUIRefresh)) rec->GetFunc("PreventUIRefresh"); | |
plugin_register("hookcommand2", reinterpret_cast<void *>(commandHook)); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment