From 2387f3ebaa8ef3b352f8a8d74eeab1de313bb134 Mon Sep 17 00:00:00 2001
From: WhiteMagic <white.magic@gmx.de>
Date: Sat, 6 Apr 2019 20:37:26 +1100
Subject: [PATCH] Relative axis mode fixes

- small deltas on vjoy axis entries result in no change
- early thread termination due to holding an axis input steady
---
 action_plugins/remap/__init__.py | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/action_plugins/remap/__init__.py b/action_plugins/remap/__init__.py
index f4534e1f..c69e270b 100644
--- a/action_plugins/remap/__init__.py
+++ b/action_plugins/remap/__init__.py
@@ -207,9 +207,11 @@ def __init__(self, action):
 
         self.needs_auto_release = self._check_for_auto_release(action)
         self.thread_running = False
+        self.should_stop_thread = False
         self.thread_last_update = time.time()
         self.thread = None
         self.axis_delta_value = 0.0
+        self.axis_value = 0.0
 
     def process_event(self, event, value):
         if self.input_type == InputType.JoystickAxis:
@@ -217,6 +219,7 @@ def process_event(self, event, value):
                 joystick_handling.VJoyProxy()[self.vjoy_device_id] \
                     .axis(self.vjoy_input_id).value = value.current
             else:
+                self.should_stop_thread = abs(event.value) < 0.05
                 self.axis_delta_value = \
                     value.current * (self.axis_scaling / 1000.0)
                 self.thread_last_update = time.time()
@@ -228,8 +231,6 @@ def process_event(self, event, value):
                     )
                     self.thread.start()
 
-
-
         elif self.input_type == InputType.JoystickButton:
             if event.event_type in [InputType.JoystickButton, InputType.Keyboard] \
                     and event.is_pressed \
@@ -241,6 +242,7 @@ def process_event(self, event, value):
 
             joystick_handling.VJoyProxy()[self.vjoy_device_id] \
                 .button(self.vjoy_input_id).is_pressed = value.current
+
         elif self.input_type == InputType.JoystickHat:
             joystick_handling.VJoyProxy()[self.vjoy_device_id] \
                 .hat(self.vjoy_input_id).direction = value.current
@@ -249,18 +251,22 @@ def process_event(self, event, value):
 
     def relative_axis_thread(self):
         self.thread_running = True
+        vjoy_dev = joystick_handling.VJoyProxy()[self.vjoy_device_id]
+        self.axis_value = vjoy_dev.axis(self.vjoy_input_id).value
         while self.thread_running:
-            cur_value = joystick_handling.VJoyProxy()[self.vjoy_device_id].axis(
-                self.vjoy_input_id).value
-            joystick_handling.VJoyProxy()[self.vjoy_device_id].axis(
-                self.vjoy_input_id).value = max(
-                -1.0,
-                min(1.0, cur_value+self.axis_delta_value)
-            )
+            try:
+                self.axis_value = max(
+                    -1.0,
+                    min(1.0, self.axis_value + self.axis_delta_value)
+                )
+                vjoy_dev.axis(self.vjoy_input_id).value = self.axis_value
 
-            if self.thread_last_update + 1.0 < time.time():
+                if self.should_stop_thread and \
+                        self.thread_last_update + 1.0 < time.time():
+                    self.thread_running = False
+                time.sleep(0.01)
+            except gremlin.error.VJoyError:
                 self.thread_running = False
-            time.sleep(0.01)
 
     def _check_for_auto_release(self, action):
         activation_condition = None