summaryrefslogtreecommitdiffhomepage
path: root/logic
diff options
context:
space:
mode:
Diffstat (limited to 'logic')
-rw-r--r--logic/main-settings.hpp2
-rw-r--r--logic/tracker.cpp24
-rw-r--r--logic/tracker.h73
-rw-r--r--logic/work.cpp5
4 files changed, 78 insertions, 26 deletions
diff --git a/logic/main-settings.hpp b/logic/main-settings.hpp
index 0922ed37..04a5c6fd 100644
--- a/logic/main-settings.hpp
+++ b/logic/main-settings.hpp
@@ -76,6 +76,7 @@ struct main_settings
key_opts key_start_tracking, key_stop_tracking, key_toggle_tracking, key_restart_tracking;
key_opts key_center, key_toggle, key_zero;
key_opts key_toggle_press, key_zero_press;
+ key_opts key_disable_tcomp_press;
value<bool> tracklogging_enabled;
value<QString> tracklogging_filename;
main_settings() :
@@ -105,6 +106,7 @@ struct main_settings
key_zero(b, "zero"),
key_toggle_press(b, "toggle-press"),
key_zero_press(b, "zero-press"),
+ key_disable_tcomp_press(b, "disable-translation-compensation-while-held"),
tracklogging_enabled(b, "tracklogging-enabled", false),
tracklogging_filename(b, "tracklogging-filename", QString())
{
diff --git a/logic/tracker.cpp b/logic/tracker.cpp
index c5db5239..3a8df56e 100644
--- a/logic/tracker.cpp
+++ b/logic/tracker.cpp
@@ -15,6 +15,7 @@
#include "compat/sleep.hpp"
#include "tracker.h"
+
#include <cmath>
#include <algorithm>
#include <cstdio>
@@ -26,17 +27,14 @@
Tracker::Tracker(Mappings &m, SelectedLibraries &libs, TrackLogger &logger) :
m(m),
libs(libs),
- logger(logger),
- centerp(s.center_at_startup),
- enabledp(true),
- zero_(false),
- should_quit(false)
+ logger(logger)
{
+ set(f_center, s.center_at_startup);
}
Tracker::~Tracker()
{
- should_quit = true;
+ set(f_should_quit, true);
wait();
}
@@ -58,7 +56,7 @@ double Tracker::map(double pos, Map& axis)
axis.spline_main.setTrackingActive( !altp );
axis.spline_alt.setTrackingActive( altp );
auto& fc = altp ? axis.spline_alt : axis.spline_main;
- return fc.getValue(pos);
+ return double(fc.getValue(pos));
}
void Tracker::t_compensate(const rmat& rmat, const euler_t& xyz_, euler_t& output, bool rz)
@@ -146,7 +144,7 @@ void Tracker::logic()
{
bool can_center = false;
- if (centerp && !nanp)
+ if (get(f_center) && !nanp)
{
using std::fabs;
@@ -160,7 +158,7 @@ void Tracker::logic()
if (can_center)
{
- centerp = false;
+ set(f_center, false);
if (libs.pFilter)
libs.pFilter->center();
@@ -264,7 +262,7 @@ void Tracker::logic()
for (int i = 0; i < 6; i++)
value(i) += m(i).opts.zero;
- if (zero_)
+ if (get(f_zero))
for (int i = 0; i < 6; i++)
value(i) = 0;
@@ -272,7 +270,7 @@ void Tracker::logic()
nanp = true;
}
- if (s.tcomp_p)
+ if (s.tcomp_p && !get(f_tcomp_disabled))
{
euler_t value_(value(TX), value(TY), value(TZ));
t_compensate(euler_to_rmat(euler_t(value(Yaw) * d2r, value(Pitch) * d2r, value(Roll) * d2r)),
@@ -343,12 +341,12 @@ void Tracker::run()
t.start();
logger.reset_dt();
- while (!should_quit)
+ while (!get(f_should_quit))
{
Pose tmp;
libs.pTracker->data(tmp);
- if (enabledp)
+ if (get(f_enabled))
for (int i = 0; i < 6; i++)
newpose[i] = elide_nan(tmp(i), newpose(i));
diff --git a/logic/tracker.h b/logic/tracker.h
index 873e0936..9c100ea7 100644
--- a/logic/tracker.h
+++ b/logic/tracker.h
@@ -25,11 +25,63 @@
#include <QMutex>
#include <QThread>
+#include <atomic>
+
#include "export.hpp"
using Pose = Mat<double, 6, 1>;
-class OPENTRACK_LOGIC_EXPORT Tracker : private QThread
+struct bits
+{
+ enum flags {
+ f_center = 1 << 0,
+ f_enabled = 1 << 1,
+ f_zero = 1 << 2,
+ f_tcomp_disabled = 1 << 3,
+ f_should_quit = 1 << 4,
+ };
+
+ std::atomic<unsigned> b;
+
+ void set(flags flag_, bool val_)
+ {
+ unsigned b_(b);
+ const unsigned flag = unsigned(flag_);
+ const unsigned val = unsigned(!!val_);
+ while (!b.compare_exchange_weak(b_,
+ unsigned((b_ & ~flag) | (flag * val)),
+ std::memory_order_seq_cst,
+ std::memory_order_seq_cst))
+ { /* empty */ }
+ }
+
+ void negate(flags flag_)
+ {
+ unsigned b_(b);
+ const unsigned flag = unsigned(flag_);
+ while (!b.compare_exchange_weak(b_,
+ (b_ & ~flag) | (flag & ~b_),
+ std::memory_order_seq_cst,
+ std::memory_order_seq_cst))
+ { /* empty */ }
+ }
+
+ bool get(flags flag)
+ {
+ return !!(b & flag);
+ }
+
+ bits() : b(0u)
+ {
+ set(f_center, true);
+ set(f_enabled, true);
+ set(f_zero, false);
+ set(f_tcomp_disabled, false);
+ set(f_should_quit, false);
+ }
+};
+
+class OPENTRACK_LOGIC_EXPORT Tracker : private QThread, private bits
{
Q_OBJECT
private:
@@ -64,11 +116,6 @@ private:
state real_rotation, scaled_rotation;
euler_t t_center;
- volatile bool centerp;
- volatile bool enabledp;
- volatile bool zero_;
- volatile bool should_quit;
-
double map(double pos, Map& axis);
void logic();
void t_compensate(const rmat& rmat, const euler_t& ypr, euler_t& output, bool rz);
@@ -88,9 +135,13 @@ public:
rmat get_camera_offset_matrix(double c);
void get_raw_and_mapped_poses(double* mapped, double* raw) const;
void start() { QThread::start(); }
- void toggle_enabled() { enabledp = !enabledp; }
- void set_toggle(bool value) { enabledp = value; }
- void set_zero(bool value) { zero_ = value; }
- void center() { centerp = !centerp; }
- void zero() { zero_ = !zero_; }
+
+ void center() { set(f_center, true); }
+
+ void set_toggle(bool value) { set(f_enabled, value); }
+ void set_zero(bool value) { set(f_zero, value); }
+ void set_tcomp_disabled(bool x) { set(f_tcomp_disabled, x); }
+
+ void zero() { negate(f_zero); }
+ void toggle_enabled() { negate(f_enabled); }
};
diff --git a/logic/work.cpp b/logic/work.cpp
index 8d00270b..c369921c 100644
--- a/logic/work.cpp
+++ b/logic/work.cpp
@@ -53,7 +53,8 @@ Work::Work(Mappings& m, SelectedLibraries& libs, WId handle) :
key_tuple(s.key_zero, [&](bool) -> void { tracker->zero(); }, true),
key_tuple(s.key_toggle_press, [&](bool x) -> void { tracker->set_toggle(!x); }, false),
key_tuple(s.key_zero_press, [&](bool x) -> void { tracker->set_zero(x); }, false),
- }
+ key_tuple(s.key_disable_tcomp_press, [&](bool x) { tracker->set_tcomp_disabled(x); }, false),
+ }
{
reload_shortcuts();
tracker->start();
@@ -66,8 +67,8 @@ void Work::reload_shortcuts()
Work::~Work()
{
- sc = nullptr;
// order matters, otherwise use-after-free -sh
+ sc = nullptr;
tracker = nullptr;
libs = SelectedLibraries();
}