use QLibrary to load external libs instead of direct OS calls

This commit is contained in:
itsmattkc
2019-03-23 02:24:28 +11:00
parent 8c8977f18d
commit 81a7b14816
8 changed files with 66 additions and 223 deletions
+36 -74
View File
@@ -34,7 +34,14 @@
#include "global/global.h"
#include "global/debug.h"
#ifdef __linux__
// Load libraries for retrieving the native window handle. Used for VST plugins that have a separate window
// dedicated to controls.
#if defined(_WIN32)
#include <Windows.h>
#elif defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
class NSWindow;
#elif defined(__linux__)
#include <X11/X.h>
#endif
@@ -105,95 +112,53 @@ typedef int32_t (*processEventsFuncPtr)(VstEvents *events);
typedef void (*processFuncPtr)(AEffect *effect, float **inputs, float **outputs, int32_t sampleFrames);
void VSTHost::loadPlugin() {
QString dll_fn = file_field->GetFileAt(0);
if (dll_fn.isEmpty()) {
return;
}
#if defined(__APPLE__)
bundle = BundleLoad(dll_fn);
// Try to load the plugin
modulePtr.setFileName(dll_fn);
if (!modulePtr.load()) {
if (bundle == NULL) {
QMessageBox::critical(nullptr, tr("Error loading VST plugin"), tr("Failed to create VST reference"));
// Show an error if the plugin fails to load
qCritical() << "Failed to load VST plugin" << dll_fn << "-" << modulePtr.errorString();
QMessageBox::critical(olive::MainWindow,
tr("Error loading VST plugin"),
tr("Failed to load VST plugin \"%1\": %2").arg(dll_fn, modulePtr.errorString()));
return;
}
vstPluginFuncPtr mainEntryPoint = NULL;
mainEntryPoint = (vstPluginFuncPtr)CFBundleGetFunctionPointerForName(bundle, CFSTR("VSTPluginMain"));
// VST plugins previous to the 2.4 SDK used main_macho for the entry point name
if(mainEntryPoint == NULL) {
mainEntryPoint = (vstPluginFuncPtr)CFBundleGetFunctionPointerForName(bundle, CFSTR("main_macho"));
// Try to find the VST entry point (first using VSTPluginMain() )
vstPluginFuncPtr mainEntryPoint = reinterpret_cast<vstPluginFuncPtr>(modulePtr.resolve("VSTPluginMain"));
if (mainEntryPoint == nullptr) {
// If there's no VSTPluginMain(), the plugin may use main() instead
mainEntryPoint = reinterpret_cast<vstPluginFuncPtr>(modulePtr.resolve("main"));
}
if(mainEntryPoint == NULL) {
qCritical() << "Couldn't get a pointer to VST plugin's main()";
BundleClose(bundle);
if (mainEntryPoint == nullptr) {
QMessageBox::critical(olive::MainWindow,
tr("Error loading VST plugin"),
tr("Failed to locate entry point for dynamic library."));
modulePtr.unload();
return;
}
// Instantiate the plugin
plugin = mainEntryPoint(hostCallback);
if(plugin == NULL) {
qCritical() << "Plugin's main() returns null";
BundleClose(bundle);
return;
}
#else
modulePtr = LibLoad(dll_fn);
if(modulePtr == nullptr) {
QString dll_error;
#ifdef _WIN32
DWORD dll_err = GetLastError();
dll_error = QString::number(dll_err);
#elif defined(__linux__) || defined(__HAIKU__)
dll_error = dlerror();
#endif
qCritical() << "Failed to load VST plugin" << dll_fn << "-" << dll_error;
QString msg_err = tr("Failed to load VST plugin \"%1\": %2").arg(dll_fn, dll_error);
#ifdef _WIN32
if (dll_err == 193) {
#ifdef _WIN64
msg_err += "\n\n" + tr("NOTE: You can't load 32-bit VST plugins into a 64-bit build of Olive. Please find a 64-bit version of this plugin or switch to a 32-bit build of Olive.");
#elif _WIN32
msg_err += "\n\n" + tr("NOTE: You can't load 64-bit VST plugins into a 32-bit build of Olive. Please find a 32-bit version of this plugin or switch to a 64-bit build of Olive.");
#endif
}
#endif
QMessageBox::critical(nullptr, tr("Error loading VST plugin"), msg_err);
return;
}
vstPluginFuncPtr mainEntryPoint = reinterpret_cast<vstPluginFuncPtr>(LibAddress(modulePtr, "VSTPluginMain"));
if (mainEntryPoint == nullptr) {
// if there's no VSTPluginMain(), fallback to main()
mainEntryPoint = reinterpret_cast<vstPluginFuncPtr>(LibAddress(modulePtr, "main"));
}
if (mainEntryPoint == nullptr) {
QMessageBox::critical(nullptr, tr("Error loading VST plugin"), tr("Failed to locate entry point for dynamic library."));
LibClose(modulePtr);
} else {
// Instantiate the plugin
plugin = mainEntryPoint(hostCallback);
}
#endif
}
void VSTHost::freePlugin() {
if (plugin != nullptr) {
stopPlugin();
data_cache.clear();
#if defined(__APPLE__)
CFBundleUnloadExecutable(bundle);
CFRelease(bundle);
#else
LibClose(modulePtr);
#endif
modulePtr.unload();
plugin = nullptr;
}
}
@@ -406,13 +371,10 @@ void VSTHost::change_plugin() {
dialog->setFixedSize(eRect->right - eRect->left, eRect->bottom - eRect->top);
} else {
#ifdef __APPLE__
CFBundleUnloadExecutable(bundle);
CFRelease(bundle);
#else
LibClose(modulePtr);
#endif
modulePtr.unload();
plugin = nullptr;
}
}
show_interface_btn->SetEnabled(plugin != nullptr);