diff options
-rw-r--r-- | proto-ft/ftnoir_protocol_ft.cpp | 8 | ||||
-rw-r--r-- | proto-ft/ftnoir_protocol_ft.h | 13 | ||||
-rw-r--r-- | proto-ft/mutex.cpp | 109 | ||||
-rw-r--r-- | proto-ft/mutex.hpp | 16 |
4 files changed, 74 insertions, 72 deletions
diff --git a/proto-ft/ftnoir_protocol_ft.cpp b/proto-ft/ftnoir_protocol_ft.cpp index 42671b8f..9dba2bff 100644 --- a/proto-ft/ftnoir_protocol_ft.cpp +++ b/proto-ft/ftnoir_protocol_ft.cpp @@ -9,6 +9,8 @@ #include "ftnoir_protocol_ft.h" #include "csv/csv.h" +check_for_first_run FTNoIR_Protocol::runonce_check = check_for_first_run(); + FTNoIR_Protocol::FTNoIR_Protocol() : shm(FREETRACK_HEAP, FREETRACK_MUTEX, sizeof(FTHeap)), pMemData((FTHeap*) shm.ptr()), @@ -16,11 +18,11 @@ FTNoIR_Protocol::FTNoIR_Protocol() : viewsStop(nullptr), intGameID(0) { - runonce_check->set_enabled(s.close_protocols_on_exit); + runonce_check.set_enabled(s.close_protocols_on_exit); QObject::connect(&s.close_protocols_on_exit, static_cast<void (base_value::*)(bool)>(&value<bool>::valueChanged), - [] (bool flag) -> void { runonce_check->set_enabled(flag); }); - runonce_check->try_runonce(); + [] (bool flag) -> void { runonce_check.set_enabled(flag); }); + runonce_check.try_runonce(); } FTNoIR_Protocol::~FTNoIR_Protocol() diff --git a/proto-ft/ftnoir_protocol_ft.h b/proto-ft/ftnoir_protocol_ft.h index 061504d9..467fc9cc 100644 --- a/proto-ft/ftnoir_protocol_ft.h +++ b/proto-ft/ftnoir_protocol_ft.h @@ -22,6 +22,7 @@ #include "opentrack-compat/options.hpp" #include "freetrackclient/fttypes.h" #include <memory> +#include "mutex.hpp" using namespace options; @@ -39,15 +40,6 @@ struct settings : opts { typedef void (__stdcall *importTIRViewsStart)(void); typedef void (__stdcall *importTIRViewsStop)(void); -// for runonce, see mutex.cpp -struct runonce -{ - virtual void try_runonce() = 0; - virtual bool is_first_run() = 0; - virtual ~runonce() {} - virtual void set_enabled(bool flag) = 0; -}; - class FTNoIR_Protocol : public IProtocol { public: @@ -72,8 +64,7 @@ private: int intGameID; QString connected_game; QMutex game_name_mutex; - - static std::unique_ptr<runonce> runonce_check; + static check_for_first_run runonce_check; static inline double getRadsFromDegrees(double degrees) { return degrees * 0.017453; } void start_tirviews(); diff --git a/proto-ft/mutex.cpp b/proto-ft/mutex.cpp index 88021810..63cf4334 100644 --- a/proto-ft/mutex.cpp +++ b/proto-ft/mutex.cpp @@ -1,71 +1,64 @@ #include "ftnoir_protocol_ft.h" +#include "mutex.hpp" #include <windows.h> +#include <QDebug> -class check_for_first_run : public runonce +check_for_first_run::check_for_first_run() : checked_for_first_run(false), is_first_instance(false), enabled(false) { - bool checked_for_first_run; - bool is_first_instance; - bool enabled; +} - void try_exit() - { - if (is_first_instance && enabled) - { - qDebug() << "ft runonce: removing registry keys"; - FTNoIR_Protocol::set_protocols(false, false); - } - } - -public: - check_for_first_run() : checked_for_first_run(false), is_first_instance(false), enabled(false) - { - } +bool check_for_first_run::is_first_run() +{ + return checked_for_first_run && is_first_instance; +} - bool is_first_run() override - { - return checked_for_first_run && is_first_instance; - } +void check_for_first_run::set_enabled(bool flag) +{ + enabled = flag; + qDebug() << "ft runonce:" << "enabled" << flag; +} - void set_enabled(bool flag) override +void check_for_first_run::try_runonce() +{ + constexpr const char* name = "opentrack-freetrack-runonce"; + + if (checked_for_first_run) + return; + + // just leak it, no issue + HANDLE h = CreateMutexA(nullptr, false, name); + + switch (WaitForSingleObject(h, 0)) { - enabled = flag; - qDebug() << "ft runonce:" << "enabled" << flag; + case WAIT_OBJECT_0: + is_first_instance = true; + checked_for_first_run = true; + break; + case WAIT_TIMEOUT: + checked_for_first_run = true; + break; + default: + checked_for_first_run = false; + break; } + + if (checked_for_first_run && !is_first_instance) + CloseHandle(h); + + qDebug() << "ft runonce:" << "enabled" << enabled << "first-run" << is_first_instance << "checked" << checked_for_first_run; +} - void try_runonce() override - { - constexpr const char* name = "opentrack-freetrack-runonce"; - - if (checked_for_first_run) - return; - - // just leak it, no issue - HANDLE h = CreateMutexA(nullptr, false, name); - - switch (WaitForSingleObject(h, 0)) - { - case WAIT_OBJECT_0: - is_first_instance = true; - checked_for_first_run = true; - break; - case WAIT_TIMEOUT: - checked_for_first_run = true; - break; - default: - checked_for_first_run = false; - break; - } - - if (checked_for_first_run && !is_first_instance) - CloseHandle(h); - - qDebug() << "ft runonce:" << "first-run" << is_first_instance << "checked" << checked_for_first_run; - } +check_for_first_run::~check_for_first_run() +{ + try_exit(); +} - ~check_for_first_run() +void check_for_first_run::try_exit() +{ + qDebug() << "ft runonce enabled:" << enabled; + if (is_first_instance && enabled) { - try_exit(); + qDebug() << "ft runonce: removing registry keys"; + FTNoIR_Protocol::set_protocols(false, false); } -}; - -std::unique_ptr<runonce> FTNoIR_Protocol::runonce_check = std::unique_ptr<runonce>(static_cast<runonce*>(new check_for_first_run())); +} diff --git a/proto-ft/mutex.hpp b/proto-ft/mutex.hpp new file mode 100644 index 00000000..f92a14bf --- /dev/null +++ b/proto-ft/mutex.hpp @@ -0,0 +1,16 @@ +#pragma once + +class check_for_first_run final +{ + bool checked_for_first_run; + bool is_first_instance; + bool enabled; + + void try_exit(); +public: + check_for_first_run(); + bool is_first_run(); + void set_enabled(bool flag); + void try_runonce(); + ~check_for_first_run(); +}; |