diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-05-22 12:49:41 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-05-22 12:49:41 +0200 |
commit | c6819cfa0213a72be66c7e6c9808fd1b7ad9ce03 (patch) | |
tree | 16cece5699eebeeda67f58f825a8418824c17104 /proto-ft/mutex.cpp | |
parent | 78d34c3285bbdc8ba30bf07d06235f68f73346fb (diff) |
proto/freetrack: runonce check can be a static member
Now that we have -fuse-cxa-atexit static dtors for modules are called as
they should be.
Diffstat (limited to 'proto-ft/mutex.cpp')
-rw-r--r-- | proto-ft/mutex.cpp | 109 |
1 files changed, 51 insertions, 58 deletions
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())); +} |