summaryrefslogtreecommitdiffhomepage
path: root/proto-ft/mutex.cpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2018-02-21 10:25:10 +0100
committerStanislaw Halik <sthalik@misaki.pl>2018-02-21 11:12:52 +0100
commit8354713abd533c3a48282f5cdce8961df5ffa178 (patch)
tree3ad16a88525d5209536708149de948f94e1e99e3 /proto-ft/mutex.cpp
parent7ffec3899cf9c2fe14350d72cfd0d1858481b43c (diff)
[STABLE] contrib/npclient, proto/ft: revert
Diffstat (limited to 'proto-ft/mutex.cpp')
-rw-r--r--proto-ft/mutex.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/proto-ft/mutex.cpp b/proto-ft/mutex.cpp
new file mode 100644
index 00000000..a012ba90
--- /dev/null
+++ b/proto-ft/mutex.cpp
@@ -0,0 +1,60 @@
+#include "ftnoir_protocol_ft.h"
+#include "mutex.hpp"
+#include <windows.h>
+#include <QDebug>
+
+check_for_first_run::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;
+}
+
+void check_for_first_run::set_enabled(bool flag)
+{
+ enabled = flag;
+}
+
+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))
+ {
+ 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);
+}
+
+check_for_first_run::~check_for_first_run()
+{
+ try_exit();
+}
+
+void check_for_first_run::try_exit()
+{
+ if (is_first_instance && enabled)
+ {
+ qDebug() << "ft runonce: removing registry keys";
+ freetrack::set_protocols(false, false);
+ }
+}