diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2015-12-05 23:32:43 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2015-12-05 23:32:43 +0100 |
commit | 86708a2311c1a3efd975798ff42d934eea0078ed (patch) | |
tree | fc13ca166c346fd30dbd01d712bb30d66d78feb4 /opentrack | |
parent | 578749c5b7f245aac08c4e514d2767dba059c4db (diff) |
api/shortcuts: no longer keep shortcuts hardcoded
Diffstat (limited to 'opentrack')
-rw-r--r-- | opentrack/shortcuts.cpp | 48 | ||||
-rw-r--r-- | opentrack/shortcuts.h | 34 | ||||
-rw-r--r-- | opentrack/tracker.h | 6 | ||||
-rw-r--r-- | opentrack/work.hpp | 32 |
4 files changed, 67 insertions, 53 deletions
diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp index b961294f..560a31ab 100644 --- a/opentrack/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -9,7 +9,7 @@ #include "shortcuts.h" #include "win32-shortcuts.h" -void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k) +void Shortcuts::bind_keyboard_shortcut(K &key, const key_opts& k) { #if !defined(_WIN32) using sh = QxtGlobalShortcut; @@ -63,33 +63,43 @@ void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k) #ifdef _WIN32 void Shortcuts::receiver(const Key& k) { - std::vector<K*> ks { &keyCenter, &keyToggle, &keyZero }; - for (K* k_ : ks) + const int sz = keys.size(); + for (int i = 0; i < sz; i++) { - if (k.guid != k_->guid) + K& k_ = std::get<0>(keys[i]); + auto& fun = std::get<1>(keys[i]); + if (k.guid != k_.guid) continue; - if (k.keycode != k_->keycode) + if (k.keycode != k_.keycode) continue; if (!k.held) continue; - if (!k_->should_process()) + if (!k_.should_process()) continue; - if (k_->alt && !k.alt) continue; - if (k_->ctrl && !k.ctrl) continue; - if (k_->shift && !k.shift) continue; + if (k_.alt && !k.alt) continue; + if (k_.ctrl && !k.ctrl) continue; + if (k_.shift && !k.shift) continue; - if (k_ == &keyCenter) - emit center(); - else if (k_ == &keyToggle) - emit toggle(); - else if (k_ == &keyZero) - emit zero(); + fun(); } } #endif -void Shortcuts::reload() { - bind_keyboard_shortcut(keyCenter, s.center); - bind_keyboard_shortcut(keyToggle, s.toggle); - bind_keyboard_shortcut(keyZero, s.zero); +void Shortcuts::reload(const std::vector<std::tuple<key_opts&, fun> > &keys_) +{ + const int sz = keys_.size(); + keys = std::vector<tt>(); + + for (int i = 0; i < sz; i++) + { + const auto& kk = keys_[i]; + const key_opts& opts = std::get<0>(kk); + auto& fun = std::get<1>(kk); + K k; + bind_keyboard_shortcut(k, opts); + keys.push_back(std::tuple<K, Shortcuts::fun>(k, fun)); +#ifndef _WIN32 + connect(k.get(), &QGlobalShortcut::activated, fun); +#endif + } } diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index 8acc5ba5..4dec7c2c 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -8,6 +8,9 @@ #pragma once #include <QObject> +#include <tuple> +#include <vector> +#include <functional> #ifdef BUILD_api # include "opentrack-compat/export.hpp" @@ -36,10 +39,10 @@ public: Key #endif ; - - K keyCenter; - K keyToggle; - K keyZero; + + using fun = std::function<void(void)>; + using tt = std::tuple<K, fun>; + std::vector<tt> keys; #ifdef _WIN32 KeybindingWorker::Token key_token; #endif @@ -56,32 +59,29 @@ public: }; struct settings : opts { - key_opts center, toggle, zero; + key_opts center, toggle, zero, start_tracking, stop_tracking, toggle_tracking; main_settings s_main; settings() : opts("keyboard-shortcuts"), center(b, "center"), toggle(b, "toggle"), - zero(b, "zero") + zero(b, "zero"), + start_tracking(b, "start-tracking"), + stop_tracking(b, "stop-tracking"), + toggle_tracking(b, "toggle-tracking") {} } s; - Shortcuts() + Shortcuts() : #ifdef _WIN32 - : key_token(KeybindingWorker::add_receiver([&](const Key& k) { receiver(k); })) + key_token([&](const Key& k) { receiver(k); }) #endif - { - reload(); - } + {} - void reload(); + void reload(const std::vector<std::tuple<Shortcuts::key_opts &, Shortcuts::fun> > &keys); private: - void bind_keyboard_shortcut(K &key, key_opts& k); + void bind_keyboard_shortcut(K &key, const key_opts& k); #ifdef _WIN32 void receiver(const Key& k); #endif -signals: - void center(); - void toggle(); - void zero(); }; diff --git a/opentrack/tracker.h b/opentrack/tracker.h index e26c3f7a..36b5cad4 100644 --- a/opentrack/tracker.h +++ b/opentrack/tracker.h @@ -80,7 +80,7 @@ public: void get_raw_and_mapped_poses(double* mapped, double* raw) const; void start() { QThread::start(); } - void toggle_enabled() { enabledp = !enabledp; } - void center() { centerp = !centerp; } - void zero() { zero_ = !zero_; } + void toggle_enabled() { qDebug() << "toggle enabled"; enabledp = !enabledp; } + void center() { qDebug() << "toggle center"; centerp = !centerp; } + void zero() { qDebug() << "toggle zero"; zero_ = !zero_; } }; diff --git a/opentrack/work.hpp b/opentrack/work.hpp index e338e9f4..a3c0e014 100644 --- a/opentrack/work.hpp +++ b/opentrack/work.hpp @@ -16,6 +16,9 @@ #include <QObject> #include <QFrame> #include <memory> +#include <vector> +#include <functional> +#include <tuple> struct Work { @@ -24,28 +27,29 @@ struct Work mem<Tracker> tracker; mem<Shortcuts> sc; WId handle; - - Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv, WId handle) : + using fn = std::function<void(void)>; + using key_opts = Shortcuts::key_opts; + using tt = std::tuple<key_opts&, fn>; + std::vector<std::tuple<key_opts&, fn>> keys; + + Work(main_settings& s, Mappings& m, SelectedLibraries& libs, WId handle) : s(s), libs(libs), tracker(std::make_shared<Tracker>(s, m, libs)), sc(std::make_shared<Shortcuts>()), - handle(handle) + handle(handle), + keys { + tt(sc->s.center, [&]() -> void { tracker->center(); }), + tt(sc->s.toggle, [&]() -> void { tracker->toggle_enabled(); }), + tt(sc->s.zero, [&]() -> void { tracker->zero(); }), + } { -#ifndef _WIN32 - QObject::connect(sc->keyCenter.get(), SIGNAL(activated()), recv, SLOT(shortcutRecentered())); - QObject::connect(sc->keyToggle.get(), SIGNAL(activated()), recv, SLOT(shortcutToggled())); - QObject::connect(sc->keyZero.get(), SIGNAL(activated()), recv, SLOT(shortcutZeroed())); -#else - QObject::connect(sc.get(), SIGNAL(center()), recv, SLOT(shortcutRecentered())); - QObject::connect(sc.get(), SIGNAL(toggle()), recv, SLOT(shortcutToggled())); - QObject::connect(sc.get(), SIGNAL(zero()), recv, SLOT(shortcutZeroed())); -#endif - tracker->start(); + reload_shortcuts(); + tracker->start(); } void reload_shortcuts() { - sc->reload(); + sc->reload(keys); } ~Work() |