diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/plugin-api.cpp | 70 | ||||
-rw-r--r-- | api/plugin-api.hpp | 27 | ||||
-rw-r--r-- | api/plugin-support.hpp | 103 |
3 files changed, 103 insertions, 97 deletions
diff --git a/api/plugin-api.cpp b/api/plugin-api.cpp index 5a485d62..b2329f4b 100644 --- a/api/plugin-api.cpp +++ b/api/plugin-api.cpp @@ -2,21 +2,9 @@ #include <utility> -using namespace plugin_api::detail; - -// these exist so that vtable is emitted in a single compilation unit, not all of them. - -Metadata_::~Metadata_() = default; -IFilter::~IFilter() = default; -IProtocol::~IProtocol() = default; -ITracker::~ITracker() = default; -IExtension::~IExtension() = default; - -void ITrackerDialog::register_tracker(ITracker*) {} -void ITrackerDialog::unregister_tracker() {} +namespace plugin_api::detail { BaseDialog::BaseDialog() = default; - void BaseDialog::closeEvent(QCloseEvent*) { if (isVisible()) @@ -26,35 +14,40 @@ void BaseDialog::closeEvent(QCloseEvent*) } } -bool ITracker::center() { return false; } - -module_status ITracker::status_ok() +void BaseDialog::done(int) { - return module_status(); + if (isVisible()) + { + hide(); + close(); + } } -module_status ITracker::error(const QString& error) -{ - return module_status(error); -} +} // ns plugin_api::detail + +// these exist so that vtable is emitted in a single compilation unit, not all of them. Metadata_::Metadata_() = default; +Metadata_::~Metadata_() = default; +Metadata::Metadata() = default; +Metadata::~Metadata() = default; + IFilter::IFilter() = default; +IFilter::~IFilter() = default; IFilterDialog::IFilterDialog() = default; +IFilterDialog::~IFilterDialog() = default; IProtocol::IProtocol() = default; +IProtocol::~IProtocol() = default; IProtocolDialog::IProtocolDialog() = default; +IProtocolDialog::~IProtocolDialog() = default; ITracker::ITracker() = default; +ITracker::~ITracker() = default; +bool ITracker::center() { return false; } ITrackerDialog::ITrackerDialog() = default; - -void BaseDialog::done(int) -{ - if (isVisible()) - { - hide(); - close(); - } -} - +ITrackerDialog::~ITrackerDialog() = default; +void ITrackerDialog::register_tracker(ITracker*) {} +void ITrackerDialog::unregister_tracker() {} +IExtension::~IExtension() = default; IExtensionDialog::~IExtensionDialog() = default; bool module_status::is_ok() const @@ -62,8 +55,9 @@ bool module_status::is_ok() const return error.isNull(); } -module_status::module_status(QString error) : error(std::move(error)) {} - +module_status_mixin::~module_status_mixin() = default; +module_status::module_status(const QString& error) : error(error) {} +module_status::module_status() = default; module_status module_status_mixin::status_ok() { return {}; } module_status module_status_mixin::error(const QString& error) @@ -71,6 +65,12 @@ module_status module_status_mixin::error(const QString& error) return module_status(error.isEmpty() ? "Unknown error" : error); } +module_status ITracker::status_ok() +{ + return module_status(); +} -Metadata::Metadata() = default; -Metadata::~Metadata() = default; +module_status ITracker::error(const QString& error) +{ + return module_status(error); +} diff --git a/api/plugin-api.hpp b/api/plugin-api.hpp index 52776986..3fd605b2 100644 --- a/api/plugin-api.hpp +++ b/api/plugin-api.hpp @@ -21,10 +21,15 @@ using Pose = Mat<double, 6, 1>; -enum Axis { - TX, TY, TZ, Yaw, Pitch, Roll, - +enum Axis : int +{ NonAxis = -1, + TX = 0, TY = 1, TZ = 2, + + Yaw = 3, Pitch = 4, Roll = 5, + Axis_MIN = TX, Axis_MAX = 5, + + Axis_COUNT = 6, }; namespace plugin_api::detail { @@ -84,7 +89,7 @@ class OTR_API_EXPORT Metadata : public TR, public Metadata_ public: Metadata(); - ~Metadata(); + ~Metadata() override; }; struct OTR_API_EXPORT module_status final @@ -92,7 +97,8 @@ struct OTR_API_EXPORT module_status final QString error; bool is_ok() const; - module_status(QString error = {}); + module_status(); + explicit module_status(const QString& error); }; /* @@ -104,6 +110,7 @@ struct OTR_API_EXPORT module_status_mixin static module_status error(const QString& error); // return error message on init failure virtual module_status initialize() = 0; // where to return from + virtual ~module_status_mixin(); }; // implement this in filters @@ -114,7 +121,7 @@ struct OTR_API_EXPORT IFilter : module_status_mixin IFilter(); // optional destructor - virtual ~IFilter(); + ~IFilter() override; // perform filtering step. // you have to take care of dt on your own, try "opentrack-compat/timer.hpp" virtual void filter(const double *input, double *output) = 0; @@ -125,6 +132,7 @@ struct OTR_API_EXPORT IFilter : module_status_mixin struct OTR_API_EXPORT IFilterDialog : public plugin_api::detail::BaseDialog { IFilterDialog(); + ~IFilterDialog() override; // optional destructor //~IFilterDialog() override; @@ -142,12 +150,11 @@ struct OTR_API_EXPORT IFilterDialog : public plugin_api::detail::BaseDialog struct OTR_API_EXPORT IProtocol : module_status_mixin { IProtocol(); + ~IProtocol() override; IProtocol(const IProtocol&) = delete; IProtocol& operator=(const IProtocol&) = delete; - // optional destructor - virtual ~IProtocol(); // called 250 times a second with XYZ yaw pitch roll pose // try not to perform intense computation here. use a thread. virtual void pose(const double* headpose) = 0; @@ -165,6 +172,7 @@ struct OTR_API_EXPORT IProtocolDialog : public plugin_api::detail::BaseDialog virtual void unregister_protocol() = 0; IProtocolDialog(); + ~IProtocolDialog() override; }; // call once with your chosen class names in the plugin @@ -203,6 +211,7 @@ struct OTR_API_EXPORT ITrackerDialog : public plugin_api::detail::BaseDialog virtual void unregister_tracker(); ITrackerDialog(); + ~ITrackerDialog() override; }; // call once with your chosen class names in the plugin @@ -231,7 +240,7 @@ struct OTR_API_EXPORT IExtension : module_status_mixin }; IExtension() = default; - virtual ~IExtension(); + ~IExtension() override; virtual event_mask hook_types() = 0; diff --git a/api/plugin-support.hpp b/api/plugin-support.hpp index 5062a688..b9400e88 100644 --- a/api/plugin-support.hpp +++ b/api/plugin-support.hpp @@ -8,6 +8,7 @@ #pragma once #include "plugin-api.hpp" +#include "compat/library-path.hpp" #include <memory> #include <algorithm> @@ -21,69 +22,64 @@ #include <QList> #include <QIcon> -#if defined(__APPLE__) -# define OPENTRACK_SOLIB_EXT "dylib" -#elif defined(_WIN32) -# define OPENTRACK_SOLIB_EXT "dll" -#else -# define OPENTRACK_SOLIB_EXT "so" -#endif - -#define OPENTRACK_SOLIB_PREFIX "" - -extern "C" typedef void* (*OPENTRACK_CTOR_FUNPTR)(void); -extern "C" typedef Metadata_* (*OPENTRACK_METADATA_FUNPTR)(void); +extern "C" { + using module_ctor_t = void* (*)(void); + using module_metadata_t = Metadata_* (*)(void); +} struct dylib final { enum Type : unsigned { - Filter = 0xdeadbabeu, - Tracker = 0xcafebeefu, - Protocol = 0xdeadf00du, - Extension = 0xdeadf001u, - Invalid = 0xcafebabeu, + Filter = 0xdeadbabe, + Tracker = 0xcafebeef, + Protocol = 0xdeadf00d, + Extension = 0xcafebabe, + Invalid = (unsigned)-1, }; dylib(const QString& filename_, Type t) : - type(Invalid), full_filename(filename_), - module_name(trim_filename(filename_)), - Dialog(nullptr), - Constructor(nullptr), - Meta(nullptr) + module_name(trim_filename(filename_)) { // otherwise dlopen opens the calling executable - if (filename_.size() == 0 || module_name.size() == 0) + if (filename_.isEmpty() || module_name.isEmpty()) return; handle.setFileName(filename_); handle.setLoadHints(QLibrary::DeepBindHint | QLibrary::PreventUnloadHint | QLibrary::ResolveAllSymbolsHint); +#ifdef __clang__ +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wcomma" +#endif + if (check(!handle.load())) return; - if (check((Dialog = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetDialog"), !Dialog))) + if (check((Dialog = (module_ctor_t) handle.resolve("GetDialog"), !Dialog))) return; - if (check((Constructor = (OPENTRACK_CTOR_FUNPTR) handle.resolve("GetConstructor"), !Constructor))) + if (check((Constructor = (module_ctor_t) handle.resolve("GetConstructor"), !Constructor))) return; - if (check((Meta = (OPENTRACK_METADATA_FUNPTR) handle.resolve("GetMetadata"), !Meta))) + if (check((Meta = (module_metadata_t) handle.resolve("GetMetadata"), !Meta))) return; - auto m = std::unique_ptr<Metadata_>(Meta()); + std::unique_ptr<Metadata_> m{Meta()}; icon = m->icon(); name = m->name(); type = t; - } - ~dylib() - { - // QLibrary refcounts the .dll's so don't forcefully unload +#ifdef __clang__ +# pragma clang diagnostic pop +#endif } + // QLibrary refcounts the .dll's so don't forcefully unload + ~dylib() = default; + static QList<std::shared_ptr<dylib>> enum_libraries(const QString& library_path) { QDir module_directory(library_path); @@ -91,14 +87,14 @@ struct dylib final using str = QLatin1String; - static const struct filter_ { + const struct filter_ { Type type; QLatin1String glob; } filters[] = { - { Filter, str(OPENTRACK_SOLIB_PREFIX "opentrack-filter-*." OPENTRACK_SOLIB_EXT), }, - { Tracker, str(OPENTRACK_SOLIB_PREFIX "opentrack-tracker-*." OPENTRACK_SOLIB_EXT), }, - { Protocol, str(OPENTRACK_SOLIB_PREFIX "opentrack-proto-*." OPENTRACK_SOLIB_EXT), }, - { Extension, str(OPENTRACK_SOLIB_PREFIX "opentrack-ext-*." OPENTRACK_SOLIB_EXT), }, + { Filter, str(OPENTRACK_LIBRARY_PREFIX "opentrack-filter-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Tracker, str(OPENTRACK_LIBRARY_PREFIX "opentrack-tracker-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Protocol, str(OPENTRACK_LIBRARY_PREFIX "opentrack-proto-*." OPENTRACK_LIBRARY_EXTENSION), }, + { Extension, str(OPENTRACK_LIBRARY_PREFIX "opentrack-ext-*." OPENTRACK_LIBRARY_EXTENSION), }, }; for (const filter_& filter : filters) @@ -127,16 +123,16 @@ struct dylib final return ret; } - Type type; + Type type{Invalid}; QString full_filename; QString module_name; QIcon icon; QString name; - OPENTRACK_CTOR_FUNPTR Dialog; - OPENTRACK_CTOR_FUNPTR Constructor; - OPENTRACK_METADATA_FUNPTR Meta; + module_ctor_t Dialog{nullptr}; + module_ctor_t Constructor{nullptr}; + module_metadata_t Meta{nullptr}; private: QLibrary handle; @@ -150,21 +146,21 @@ private: { in = in.mid(idx + 1); - if (in.startsWith(OPENTRACK_SOLIB_PREFIX) && - in.endsWith("." OPENTRACK_SOLIB_EXT)) + if (in.startsWith(OPENTRACK_LIBRARY_PREFIX) && + in.endsWith("." OPENTRACK_LIBRARY_EXTENSION)) { - constexpr unsigned pfx_len = sizeof(OPENTRACK_SOLIB_PREFIX) - 1; - constexpr unsigned rst_len = sizeof("." OPENTRACK_SOLIB_EXT) - 1; + constexpr unsigned pfx_len = sizeof(OPENTRACK_LIBRARY_PREFIX) - 1; + constexpr unsigned rst_len = sizeof("." OPENTRACK_LIBRARY_EXTENSION) - 1; in = in.mid(pfx_len); in = in.left(in.size() - rst_len); - static constexpr const char* const names[] = + const char* const names[] = { - "opentrack-tracker-", - "opentrack-proto-", - "opentrack-filter-", - "opentrack-ext-", + OPENTRACK_LIBRARY_PREFIX "opentrack-tracker-", + OPENTRACK_LIBRARY_PREFIX "opentrack-proto-", + OPENTRACK_LIBRARY_PREFIX "opentrack-filter-", + OPENTRACK_LIBRARY_PREFIX "opentrack-ext-", }; for (auto name : names) @@ -174,7 +170,7 @@ private: } } } - return QString(); + return {""}; } bool check(bool fail) @@ -213,6 +209,7 @@ struct Modules final dylib_list& trackers() { return tracker_modules; } dylib_list& protocols() { return protocol_modules; } dylib_list& extensions() { return extension_modules; } + private: dylib_list module_list; dylib_list filter_modules; @@ -229,7 +226,7 @@ private: dylib_list filter(dylib::Type t) { QList<std::shared_ptr<dylib>> ret; - for (auto x : module_list) + for (const auto& x : module_list) if (x->type == t) ret.push_back(x); @@ -238,10 +235,10 @@ private: }; template<typename t> -static inline std::shared_ptr<t> make_dylib_instance(const std::shared_ptr<dylib>& lib) +std::shared_ptr<t> make_dylib_instance(const std::shared_ptr<dylib>& lib) { std::shared_ptr<t> ret; if (lib != nullptr && lib->Constructor) - ret = std::shared_ptr<t>(reinterpret_cast<t*>(reinterpret_cast<OPENTRACK_CTOR_FUNPTR>(lib->Constructor)())); + ret = std::shared_ptr<t>(reinterpret_cast<t*>(reinterpret_cast<module_ctor_t>(lib->Constructor)())); return ret; } |