From fe5298dd106579e257c05fa6f0c8f63b10504a61 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 5 Sep 2016 22:47:41 +0200 Subject: gui, logic: add shortcut for "translation compensation off while held" Requested-by: @Sone989 Issue: #440 --- logic/main-settings.hpp | 2 ++ logic/tracker.cpp | 24 ++++++++-------- logic/tracker.h | 73 +++++++++++++++++++++++++++++++++++++++++-------- logic/work.cpp | 5 ++-- 4 files changed, 78 insertions(+), 26 deletions(-) (limited to 'logic') 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 tracklogging_enabled; value 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 #include #include @@ -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 #include +#include + #include "export.hpp" using Pose = Mat; -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 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(); } -- cgit v1.2.3