From 926b2b16c8df2e7c54de10780860176d2277e9b1 Mon Sep 17 00:00:00 2001
From: Stanislaw Halik <sthalik@misaki.pl>
Date: Wed, 6 Apr 2016 16:25:05 +0200
Subject: proto/ft: make clear registry path on quit optional

Make clearing the dll path optional, defaulting to false. See previous
commit.

It has a side effect of games started before opentrack not seeing any
dll path, and loading nothing. That is, opentrack only functions when
tracking is started before a game is started.

Issue: #332
---
 proto-ft/ftnoir_ftcontrols.ui          | 121 +++++++++++++++++++++++++--------
 proto-ft/ftnoir_protocol_ft.cpp        |   4 ++
 proto-ft/ftnoir_protocol_ft.h          |   6 +-
 proto-ft/ftnoir_protocol_ft_dialog.cpp |   1 +
 proto-ft/mutex.cpp                     |  14 +++-
 5 files changed, 111 insertions(+), 35 deletions(-)
 mode change 100644 => 100755 proto-ft/ftnoir_ftcontrols.ui
 mode change 100644 => 100755 proto-ft/ftnoir_protocol_ft_dialog.cpp

diff --git a/proto-ft/ftnoir_ftcontrols.ui b/proto-ft/ftnoir_ftcontrols.ui
old mode 100644
new mode 100755
index 5356d2e5..b63d92a1
--- a/proto-ft/ftnoir_ftcontrols.ui
+++ b/proto-ft/ftnoir_ftcontrols.ui
@@ -12,8 +12,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>422</width>
-    <height>305</height>
+    <width>511</width>
+    <height>413</height>
    </rect>
   </property>
   <property name="sizePolicy">
@@ -42,10 +42,47 @@
    <bool>false</bool>
   </property>
   <layout class="QGridLayout" name="gridLayout_2">
+   <item row="1" column="0">
+    <widget class="QGroupBox" name="groupBox_3">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="title">
+      <string>Select interface</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignJustify|Qt::AlignTop</set>
+     </property>
+     <property name="flat">
+      <bool>false</bool>
+     </property>
+     <layout class="QHBoxLayout" name="horizontalLayout_3">
+      <property name="spacing">
+       <number>9</number>
+      </property>
+      <item>
+       <widget class="QComboBox" name="cbxSelectInterface"/>
+      </item>
+      <item>
+       <widget class="QLabel" name="label_8">
+        <property name="text">
+         <string>Disable one of the protocols if game is confused by presence of both at the same time.</string>
+        </property>
+        <property name="wordWrap">
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
    <item row="0" column="0">
     <widget class="QGroupBox" name="groupBox">
      <property name="sizePolicy">
-      <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
+      <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
@@ -59,18 +96,24 @@
      <property name="flat">
       <bool>false</bool>
      </property>
-     <layout class="QFormLayout" name="formLayout_2">
-      <item row="0" column="0">
+     <layout class="QHBoxLayout" name="horizontalLayout_4">
+      <property name="spacing">
+       <number>9</number>
+      </property>
+      <item>
        <widget class="QCheckBox" name="chkTIRViews">
-        <property name="layoutDirection">
-         <enum>Qt::RightToLeft</enum>
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Maximum" vsizetype="Fixed">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
         </property>
         <property name="text">
          <string>Memory hacks</string>
         </property>
        </widget>
       </item>
-      <item row="0" column="1">
+      <item>
        <widget class="QLabel" name="label_2">
         <property name="text">
          <string>Only for very old and buggy old games such as CFS3.</string>
@@ -92,7 +135,7 @@
    <item row="2" column="0">
     <widget class="QGroupBox" name="groupBox_4">
      <property name="sizePolicy">
-      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+      <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
@@ -106,18 +149,29 @@
      <property name="flat">
       <bool>false</bool>
      </property>
-     <layout class="QGridLayout" name="gridLayout">
-      <item row="0" column="0">
+     <layout class="QHBoxLayout" name="horizontalLayout_2">
+      <property name="spacing">
+       <number>9</number>
+      </property>
+      <item>
        <widget class="QPushButton" name="bntLocateNPClient">
         <property name="text">
          <string>Locate DLL</string>
         </property>
        </widget>
       </item>
-      <item row="0" column="1">
+      <item>
        <widget class="QLabel" name="label_10">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
         <property name="text">
-         <string>Replace the registry entry if you want to use other software with the NPClient protocol and it doesn't work automatically.</string>
+         <string>Replace the registry entry if you want to use other software with the NPClient protocol and it doesn't work automatically.
+
+Starting tracking will again overwrite the DLL locations.</string>
         </property>
         <property name="wordWrap">
          <bool>true</bool>
@@ -127,38 +181,45 @@
      </layout>
     </widget>
    </item>
-   <item row="3" column="0">
+   <item row="4" column="0">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="standardButtons">
       <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
      </property>
     </widget>
    </item>
-   <item row="1" column="0">
-    <widget class="QGroupBox" name="groupBox_3">
+   <item row="3" column="0">
+    <widget class="QGroupBox" name="groupBox_2">
      <property name="sizePolicy">
-      <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
+      <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="title">
-      <string>Select interface</string>
-     </property>
-     <property name="alignment">
-      <set>Qt::AlignJustify|Qt::AlignTop</set>
-     </property>
-     <property name="flat">
-      <bool>false</bool>
+      <string>Disable tracking for games on exit</string>
      </property>
-     <layout class="QGridLayout" name="gridLayout_3">
-      <item row="0" column="0">
-       <widget class="QComboBox" name="cbxSelectInterface"/>
+     <layout class="QHBoxLayout" name="horizontalLayout">
+      <property name="spacing">
+       <number>9</number>
+      </property>
+      <item>
+       <widget class="QCheckBox" name="close_on_exit">
+        <property name="sizePolicy">
+         <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
+          <horstretch>0</horstretch>
+          <verstretch>0</verstretch>
+         </sizepolicy>
+        </property>
+        <property name="text">
+         <string>Disable on protocol exit</string>
+        </property>
+       </widget>
       </item>
-      <item row="0" column="1">
-       <widget class="QLabel" name="label_8">
+      <item>
+       <widget class="QLabel" name="label">
         <property name="text">
-         <string>Disable one of the protocols if game is confused by presence of both at the same time.</string>
+         <string>Use this for games that disable mouselook when opentrack isn't running, and you're not running opentrack that much.</string>
         </property>
         <property name="wordWrap">
          <bool>true</bool>
diff --git a/proto-ft/ftnoir_protocol_ft.cpp b/proto-ft/ftnoir_protocol_ft.cpp
index 134ffc53..121da494 100755
--- a/proto-ft/ftnoir_protocol_ft.cpp
+++ b/proto-ft/ftnoir_protocol_ft.cpp
@@ -16,6 +16,10 @@ FTNoIR_Protocol::FTNoIR_Protocol() :
     viewsStop(nullptr),
     intGameID(0)
 {
+    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();
 }
 
diff --git a/proto-ft/ftnoir_protocol_ft.h b/proto-ft/ftnoir_protocol_ft.h
index 8940e5d8..b5b802bd 100755
--- a/proto-ft/ftnoir_protocol_ft.h
+++ b/proto-ft/ftnoir_protocol_ft.h
@@ -27,11 +27,12 @@ using namespace options;
 
 struct settings : opts {
     value<int> intUsedInterface;
-    value<bool> useTIRViews;
+    value<bool> useTIRViews, close_protocols_on_exit;
     settings() :
         opts("proto-freetrack"),
         intUsedInterface(b, "used-interfaces", 0),
-        useTIRViews(b, "use-memory-hacks", false)
+        useTIRViews(b, "use-memory-hacks", false),
+        close_protocols_on_exit(b, "close-protocols-on-exit", false)
     {}
 };
 
@@ -45,6 +46,7 @@ struct runonce
     virtual void try_exit() = 0;
     virtual bool is_first_run() = 0;
     virtual ~runonce() {}
+    virtual void set_enabled(bool flag) = 0;
 };
 
 class FTNoIR_Protocol : public IProtocol
diff --git a/proto-ft/ftnoir_protocol_ft_dialog.cpp b/proto-ft/ftnoir_protocol_ft_dialog.cpp
old mode 100644
new mode 100755
index 25f3a4cb..da00a03a
--- a/proto-ft/ftnoir_protocol_ft_dialog.cpp
+++ b/proto-ft/ftnoir_protocol_ft_dialog.cpp
@@ -21,6 +21,7 @@ FTControls::FTControls()
     connect(ui.bntLocateNPClient, SIGNAL(clicked()), this, SLOT(selectDLL()));
 
     tie_setting(s.useTIRViews, ui.chkTIRViews);
+    tie_setting(s.close_protocols_on_exit, ui.close_on_exit);
 
     ui.cbxSelectInterface->addItem("Enable both");
     ui.cbxSelectInterface->addItem("Use FreeTrack, hide TrackIR");
diff --git a/proto-ft/mutex.cpp b/proto-ft/mutex.cpp
index 0d50c9bd..34f5f518 100755
--- a/proto-ft/mutex.cpp
+++ b/proto-ft/mutex.cpp
@@ -5,6 +5,7 @@ class check_for_first_run : public runonce
 {
     bool checked_for_first_run;
     bool is_first_instance;
+    bool enabled;
 
 public:
     bool is_first_run() override
@@ -12,11 +13,17 @@ public:
         return checked_for_first_run && is_first_instance;
     }
 
+    void set_enabled(bool flag) override
+    {
+        enabled = flag;
+        qDebug() << "ft runonce:" << "enabled" << flag;
+    }
+
     void try_runonce() override
     {
         constexpr const char* name = "opentrack-freetrack-runonce";
 
-        if (checked_for_first_run)
+        if (checked_for_first_run || !enabled)
             return;
 
         // just leak it, no issue
@@ -44,7 +51,7 @@ public:
 
     void try_exit() override
     {
-        if (is_first_instance)
+        if (is_first_instance && enabled)
         {
             qDebug() << "ft runonce: removing registry keys";
             FTNoIR_Protocol::set_protocols(false, false);
@@ -52,9 +59,10 @@ public:
     }
 
 public:
-    check_for_first_run() : checked_for_first_run(false), is_first_instance(false)
+    check_for_first_run() : checked_for_first_run(false), is_first_instance(false), enabled(false)
     {
     }
+
     ~check_for_first_run()
     {
         try_exit();
-- 
cgit v1.2.3