From 71856701c8bedad3d992cb63620e695df8727812 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 7 Jun 2015 07:24:06 +0200 Subject: plugin-support: make header-only, expose as public API Issue: #151 Some global namespace macros are now prefixed with "OPENTRACK_" to avoid namespace clashes. This header is now safe to include in third-party projects. --- facetracknoir/ui.h | 2 +- ftnoir_tracker_hatire/ftnoir_tracker_hat.h | 2 +- ftnoir_tracker_hatire/ftnoir_tracker_hat_dll.cpp | 2 +- opentrack/plugin-support.cpp | 223 -------------------- opentrack/plugin-support.h | 50 ----- opentrack/plugin-support.hpp | 247 +++++++++++++++++++++++ opentrack/selected-libraries.cpp | 42 ++++ opentrack/selected-libraries.hpp | 15 ++ opentrack/shortcuts.h | 2 +- opentrack/state.hpp | 28 +-- opentrack/tracker.h | 3 +- opentrack/work.hpp | 2 +- 12 files changed, 312 insertions(+), 306 deletions(-) delete mode 100644 opentrack/plugin-support.cpp delete mode 100644 opentrack/plugin-support.h create mode 100644 opentrack/plugin-support.hpp create mode 100644 opentrack/selected-libraries.cpp create mode 100644 opentrack/selected-libraries.hpp diff --git a/facetracknoir/ui.h b/facetracknoir/ui.h index 1705553b..f2758d81 100644 --- a/facetracknoir/ui.h +++ b/facetracknoir/ui.h @@ -48,7 +48,7 @@ #include "opentrack/options.hpp" #include "opentrack/main-settings.hpp" -#include "opentrack/plugin-support.h" +#include "opentrack/plugin-support.hpp" #include "opentrack/tracker.h" #include "opentrack/shortcuts.h" #include "opentrack/work.hpp" diff --git a/ftnoir_tracker_hatire/ftnoir_tracker_hat.h b/ftnoir_tracker_hatire/ftnoir_tracker_hat.h index 020b4132..d9dd0694 100644 --- a/ftnoir_tracker_hatire/ftnoir_tracker_hat.h +++ b/ftnoir_tracker_hatire/ftnoir_tracker_hat.h @@ -2,7 +2,7 @@ #define FTNOIR_TRACKER_HAT_H #ifdef OPENTRACK_API -# include "opentrack/plugin-support.h" +# include "opentrack/plugin-support.hpp" #else # include "..\ftnoir_tracker_base\ftnoir_tracker_base.h" #endif diff --git a/ftnoir_tracker_hatire/ftnoir_tracker_hat_dll.cpp b/ftnoir_tracker_hatire/ftnoir_tracker_hat_dll.cpp index 5f64fe4b..66fead4f 100644 --- a/ftnoir_tracker_hatire/ftnoir_tracker_hat_dll.cpp +++ b/ftnoir_tracker_hatire/ftnoir_tracker_hat_dll.cpp @@ -91,7 +91,7 @@ QIcon TrackerDll::icon() // _GetTrackerDll@0 - Common name decoration for __stdcall functions in C language. #ifdef OPENTRACK_API -# include "opentrack/plugin-support.h" +# include "opentrack/plugin-support.hpp" extern "C" OPENTRACK_EXPORT Metadata* GetMetadata() #else # pragma comment(linker, "/export:GetTrackerDll=_GetTrackerDll@0") diff --git a/opentrack/plugin-support.cpp b/opentrack/plugin-support.cpp deleted file mode 100644 index 403798de..00000000 --- a/opentrack/plugin-support.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include -#include -#include "plugin-support.h" -#include -#include -#include - -#include - -#ifndef _WIN32 -# include -#endif - -SelectedLibraries::~SelectedLibraries() -{ -} - -template -static mem make_instance(mem lib) -{ - mem ret; - if (lib != nullptr && lib->Constructor) - { - qDebug() << "dylib" << (lib ? lib->filename : "") << "ptr" << (intptr_t) lib->Constructor; - std::cout.flush(); - ret = mem(reinterpret_cast(reinterpret_cast(lib->Constructor)())); - } - return ret; -} - -SelectedLibraries::SelectedLibraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f) : - pTracker(nullptr), - pFilter(nullptr), - pProtocol(nullptr), - correct(false) -{ - pTracker = make_instance(t); - pProtocol = make_instance(p); - pFilter = make_instance(f); - - if (!pTracker || !pProtocol) - { - qDebug() << "dylib load failure"; - return; - } - - if(!pProtocol->correct()) - { - qDebug() << "protocol load failure"; - return; - } - - pTracker->start_tracker(frame); - - correct = true; -} - -#if defined(__APPLE__) -# define SONAME "dylib" -#elif defined(_WIN32) -# define SONAME "dll" -#else -# define SONAME "so" -#endif - -#include - -#ifdef _MSC_VER -# error "No support for MSVC anymore" -#else -# define LIB_PREFIX "lib" -#endif - -static bool get_metadata(mem lib, QString& name, QIcon& icon) -{ - Metadata* meta; - if (!lib->Meta || ((meta = lib->Meta()), !meta)) - return false; - name = meta->name(); - icon = meta->icon(); - delete meta; - return true; -} - -QList> dylib::enum_libraries() -{ -#define BASE "opentrack-" -#define SUFF "-*." - const char* filters_n[] = { BASE "filter" SUFF, - BASE "tracker" SUFF, - BASE "proto" SUFF }; - const Type filters_t[] = { Filter, Tracker, Protocol }; - - QDir settingsDir( QCoreApplication::applicationDirPath() ); - - QList> ret; - - for (int i = 0; i < 3; i++) - { - QString filter = filters_n[i]; - auto t = filters_t[i]; - QStringList filenames = settingsDir.entryList(QStringList { LIB_PREFIX + filter + SONAME }, - QDir::Files, - QDir::Name); - for (int i = 0; i < filenames.size(); i++) { - QIcon icon; - QString longName; - QString str = filenames.at(i); - auto lib = std::make_shared(str, t); - qDebug() << "Loading" << str; - std::cout.flush(); - if (!get_metadata(lib, longName, icon)) - continue; - ret.push_back(lib); - } - } - - return ret; -} - -dylib::dylib(const QString& filename, Type t) : - type(t), - filename(filename), - Dialog(nullptr), - Constructor(nullptr), - Meta(nullptr), - handle(nullptr) -{ - // otherwise dlopen opens the calling executable - if (filename.size() == 0) - return; - -#if defined(_WIN32) - QString fullPath = QCoreApplication::applicationDirPath() + "/" + this->filename; - handle = new QLibrary(fullPath); - - struct _foo { - static bool die(QLibrary*& l, bool failp) - { - if (failp) - { - qDebug() << "failed" << l->errorString(); - delete l; - l = nullptr; - } - return failp; - } - }; - - if (_foo::die(handle, !handle->load())) - return; - - Dialog = (CTOR_FUNPTR) handle->resolve("GetDialog"); - if (_foo::die(handle, !Dialog)) - return; - - Constructor = (CTOR_FUNPTR) handle->resolve("GetConstructor"); - if (_foo::die(handle, !Constructor)) - return; - - Meta = (METADATA_FUNPTR) handle->resolve("GetMetadata"); - if (_foo::die(handle, !Meta)) - return; -#else - QByteArray latin1 = QFile::encodeName(filename); - handle = dlopen(latin1.constData(), RTLD_NOW | -# if defined(__APPLE__) - RTLD_LOCAL|RTLD_FIRST|RTLD_NOW -# else - RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE -# endif - ); - - struct _foo { - static bool err(void*& handle) - { - const char* err = dlerror(); - if (err) - { - fprintf(stderr, "Error, ignoring: %s\n", err); - fflush(stderr); - dlclose(handle); - handle = nullptr; - return true; - } - return false; - } - }; - - if (handle) - { - if (_foo::err(handle)) - return; - Dialog = (CTOR_FUNPTR) dlsym(handle, "GetDialog"); - if (_foo::err(handle)) - return; - Constructor = (CTOR_FUNPTR) dlsym(handle, "GetConstructor"); - if (_foo::err(handle)) - return; - Meta = (METADATA_FUNPTR) dlsym(handle, "GetMetadata"); - if (_foo::err(handle)) - return; - } else { - (void) _foo::err(handle); - } -#endif - - auto m = mem(Meta()); - - icon = m->icon(); - name = m->name(); -} - -dylib::~dylib() -{ -#if defined(_WIN32) - if (handle) - delete handle; -#else - if (handle) - (void) dlclose(handle); -#endif -} diff --git a/opentrack/plugin-support.h b/opentrack/plugin-support.h deleted file mode 100644 index a2b6d403..00000000 --- a/opentrack/plugin-support.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once - -#include "plugin-api.hpp" -#include "options.hpp" - -#include -#include -#include -#include -#include -#include - - -extern "C" typedef void* (*CTOR_FUNPTR)(void); -extern "C" typedef Metadata* (*METADATA_FUNPTR)(void); - -struct dylib { - enum Type { Filter, Tracker, Protocol }; - - dylib(const QString& filename, Type t); - ~dylib(); - static QList> enum_libraries(); - - Type type; - QString filename; - - QIcon icon; - QString name; - - CTOR_FUNPTR Dialog; - CTOR_FUNPTR Constructor; - METADATA_FUNPTR Meta; -private: -#if defined(_WIN32) - QLibrary* handle; -#else - void* handle; -#endif -}; - -struct SelectedLibraries { - using dylibptr = mem; - mem pTracker; - mem pFilter; - mem pProtocol; - SelectedLibraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f); - SelectedLibraries() : pTracker(nullptr), pFilter(nullptr), pProtocol(nullptr), correct(false) {} - ~SelectedLibraries(); - bool correct; -}; diff --git a/opentrack/plugin-support.hpp b/opentrack/plugin-support.hpp new file mode 100644 index 00000000..bc07c106 --- /dev/null +++ b/opentrack/plugin-support.hpp @@ -0,0 +1,247 @@ +#pragma once + +/* Copyright (c) 2015 Stanislaw Halik + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ + +#include "plugin-api.hpp" +#include "options.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#ifndef _WIN32 +# include +#endif + +#if defined(__APPLE__) +# define OPENTRACK_SONAME "dylib" +#elif defined(_WIN32) +# define OPENTRACK_SONAME "dll" +#else +# define OPENTRACK_SONAME "so" +#endif + +#include + +#ifdef _MSC_VER +# error "No support for MSVC anymore" +#else +# define OPENTRACK_LIB_PREFIX "lib" +#endif + + +extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void); +extern "C" typedef Metadata* (*OPENTRACK_METADATA_FUNPTR)(void); + +struct dylib { + enum Type { Filter, Tracker, Protocol }; + + dylib(const QString& filename, Type t) : + type(t), + filename(filename), + Dialog(nullptr), + Constructor(nullptr), + Meta(nullptr), + handle(nullptr) + { + // otherwise dlopen opens the calling executable + if (filename.size() == 0) + return; + +#if defined(_WIN32) + QString fullPath = QCoreApplication::applicationDirPath() + "/" + this->filename; + handle = new QLibrary(fullPath); + + struct _foo { + static bool die(QLibrary*& l, bool failp) + { + if (failp) + { + qDebug() << "failed" << l->errorString(); + delete l; + l = nullptr; + } + return failp; + } + }; + + if (_foo::die(handle, !handle->load())) + return; + + Dialog = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetDialog"); + if (_foo::die(handle, !Dialog)) + return; + + Constructor = (OPENTRACK_CTOR_FUNPTR) handle->resolve("GetConstructor"); + if (_foo::die(handle, !Constructor)) + return; + + Meta = (OPENTRACK_METADATA_FUNPTR) handle->resolve("GetMetadata"); + if (_foo::die(handle, !Meta)) + return; +#else + QByteArray latin1 = QFile::encodeName(filename); + handle = dlopen(latin1.constData(), +# if defined(__APPLE__) + RTLD_LOCAL|RTLD_FIRST|RTLD_NOW +# else + RTLD_NOW|RTLD_GLOBAL|RTLD_NODELETE +# endif + ); + + struct _foo { + static bool err(void*& handle) + { + const char* err = dlerror(); + if (err) + { + fprintf(stderr, "Error, ignoring: %s\n", err); + fflush(stderr); + dlclose(handle); + handle = nullptr; + return true; + } + return false; + } + }; + + if (handle) + { + if (_foo::err(handle)) + return; + Dialog = (OPENTRACK_CTOR_FUNPTR) dlsym(handle, "GetDialog"); + if (_foo::err(handle)) + return; + Constructor = (OPENTRACK_CTOR_FUNPTR) dlsym(handle, "GetConstructor"); + if (_foo::err(handle)) + return; + Meta = (OPENTRACK_METADATA_FUNPTR) dlsym(handle, "GetMetadata"); + if (_foo::err(handle)) + return; + } else { + (void) _foo::err(handle); + } +#endif + + auto m = mem(Meta()); + + icon = m->icon(); + name = m->name(); + } + ~dylib() + { +#if defined(_WIN32) + if (handle) + delete handle; +#else + if (handle) + (void) dlclose(handle); +#endif + } + + static QList> enum_libraries() + { + const char* filters_n[] = { "opentrack-filter-*.", + "opentrack-tracker-*.", + "opentrack-proto-*." + }; + const Type filters_t[] = { Filter, Tracker, Protocol }; + + QDir settingsDir( QCoreApplication::applicationDirPath() ); + + QList> ret; + + for (int i = 0; i < 3; i++) + { + QString filter = filters_n[i]; + auto t = filters_t[i]; + QStringList filenames = settingsDir.entryList(QStringList { OPENTRACK_LIB_PREFIX + filter + OPENTRACK_SONAME }, + QDir::Files, + QDir::Name); + for (int i = 0; i < filenames.size(); i++) { + QIcon icon; + QString longName; + QString str = filenames.at(i); + auto lib = std::make_shared(str, t); + qDebug() << "Loading" << str; + std::cout.flush(); + if (!get_metadata(lib, longName, icon)) + continue; + ret.push_back(lib); + } + } + + return ret; + } + + Type type; + QString filename; + + QIcon icon; + QString name; + + OPENTRACK_CTOR_FUNPTR Dialog; + OPENTRACK_CTOR_FUNPTR Constructor; + OPENTRACK_METADATA_FUNPTR Meta; +private: +#if defined(_WIN32) + QLibrary* handle; +#else + void* handle; +#endif + + static bool get_metadata(mem lib, QString& name, QIcon& icon) + { + Metadata* meta; + if (!lib->Meta || ((meta = lib->Meta()), !meta)) + return false; + name = meta->name(); + icon = meta->icon(); + delete meta; + return true; + } +}; + +struct Modules { + Modules() : + module_list(dylib::enum_libraries()), + filter_modules(filter(dylib::Filter)), + tracker_modules(filter(dylib::Tracker)), + protocol_modules(filter(dylib::Protocol)) + {} + QList>& filters() { return filter_modules; } + QList>& trackers() { return tracker_modules; } + QList>& protocols() { return protocol_modules; } +private: + QList> module_list; + QList> filter_modules; + QList> tracker_modules; + QList> protocol_modules; + + QList> filter(dylib::Type t) + { + QList> ret; + for (auto x : module_list) + if (x->type == t) + ret.push_back(x); + return ret; + } +}; diff --git a/opentrack/selected-libraries.cpp b/opentrack/selected-libraries.cpp new file mode 100644 index 00000000..8cb226c8 --- /dev/null +++ b/opentrack/selected-libraries.cpp @@ -0,0 +1,42 @@ +#include "opentrack/selected-libraries.hpp" +#include + +SelectedLibraries::~SelectedLibraries() +{ +} + +template +static mem make_instance(mem lib) +{ + mem ret; + if (lib != nullptr && lib->Constructor) + ret = mem(reinterpret_cast(reinterpret_cast(lib->Constructor)())); + return ret; +} + +SelectedLibraries::SelectedLibraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f) : + pTracker(nullptr), + pFilter(nullptr), + pProtocol(nullptr), + correct(false) +{ + pTracker = make_instance(t); + pProtocol = make_instance(p); + pFilter = make_instance(f); + + if (!pTracker || !pProtocol) + { + qDebug() << "dylib load failure"; + return; + } + + if(!pProtocol->correct()) + { + qDebug() << "protocol load failure"; + return; + } + + pTracker->start_tracker(frame); + + correct = true; +} diff --git a/opentrack/selected-libraries.hpp b/opentrack/selected-libraries.hpp new file mode 100644 index 00000000..ffaf882c --- /dev/null +++ b/opentrack/selected-libraries.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "opentrack/plugin-support.hpp" +#include + +struct SelectedLibraries { + using dylibptr = mem; + mem pTracker; + mem pFilter; + mem pProtocol; + SelectedLibraries(QFrame* frame, dylibptr t, dylibptr p, dylibptr f); + SelectedLibraries() : pTracker(nullptr), pFilter(nullptr), pProtocol(nullptr), correct(false) {} + ~SelectedLibraries(); + bool correct; +}; diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index d0aedec0..ee0d9777 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -10,7 +10,7 @@ #include #include "qxt-mini/QxtGlobalShortcut" -#include "opentrack/plugin-support.h" +#include "opentrack/plugin-support.hpp" #include "opentrack/options.hpp" #include "opentrack/main-settings.hpp" diff --git a/opentrack/state.hpp b/opentrack/state.hpp index 2c37e5eb..a9f66241 100644 --- a/opentrack/state.hpp +++ b/opentrack/state.hpp @@ -1,36 +1,10 @@ #include #include "opentrack/options.hpp" using namespace options; -#include "opentrack/plugin-support.h" +#include "opentrack/plugin-support.hpp" #include "opentrack/main-settings.hpp" #include "opentrack/mappings.hpp" -struct Modules { - Modules() : - module_list(dylib::enum_libraries()), - filter_modules(filter(dylib::Filter)), - tracker_modules(filter(dylib::Tracker)), - protocol_modules(filter(dylib::Protocol)) - {} - QList>& filters() { return filter_modules; } - QList>& trackers() { return tracker_modules; } - QList>& protocols() { return protocol_modules; } -private: - QList> module_list; - QList> filter_modules; - QList> tracker_modules; - QList> protocol_modules; - - QList> filter(dylib::Type t) - { - QList> ret; - for (auto x : module_list) - if (x->type == t) - ret.push_back(x); - return ret; - } -}; - struct Work; struct State { diff --git a/opentrack/tracker.h b/opentrack/tracker.h index ace9fa3c..d4dc149f 100644 --- a/opentrack/tracker.h +++ b/opentrack/tracker.h @@ -3,10 +3,11 @@ #include #include "timer.hpp" -#include "plugin-support.h" +#include "plugin-support.hpp" #include "mappings.hpp" #include "pose.hpp" #include "simple-mat.hpp" +#include "selected-libraries.hpp" #include "../qfunctionconfigurator/functionconfig.h" #include "main-settings.hpp" diff --git a/opentrack/work.hpp b/opentrack/work.hpp index b93c71b6..eb5bd4ff 100644 --- a/opentrack/work.hpp +++ b/opentrack/work.hpp @@ -1,7 +1,7 @@ #pragma once #include "opentrack/main-settings.hpp" -#include "opentrack/plugin-support.h" +#include "opentrack/plugin-support.hpp" #include "opentrack/tracker.h" #include "opentrack/shortcuts.h" -- cgit v1.2.3