summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2015-12-05 23:32:43 +0100
committerStanislaw Halik <sthalik@misaki.pl>2015-12-05 23:32:43 +0100
commit86708a2311c1a3efd975798ff42d934eea0078ed (patch)
treefc13ca166c346fd30dbd01d712bb30d66d78feb4
parent578749c5b7f245aac08c4e514d2767dba059c4db (diff)
api/shortcuts: no longer keep shortcuts hardcoded
-rw-r--r--gui/options-dialog.cpp3
-rw-r--r--gui/ui.cpp23
-rw-r--r--gui/ui.h4
-rw-r--r--opentrack/shortcuts.cpp48
-rw-r--r--opentrack/shortcuts.h34
-rw-r--r--opentrack/tracker.h6
-rw-r--r--opentrack/work.hpp32
7 files changed, 71 insertions, 79 deletions
diff --git a/gui/options-dialog.cpp b/gui/options-dialog.cpp
index 9631d41e..70d5be62 100644
--- a/gui/options-dialog.cpp
+++ b/gui/options-dialog.cpp
@@ -73,6 +73,9 @@ OptionsDialog::OptionsDialog()
connect(ui.bind_center, &QPushButton::pressed, [&]() -> void { bind_key(s.center, ui.center_text); });
connect(ui.bind_zero, &QPushButton::pressed, [&]() -> void { bind_key(s.zero, ui.zero_text); });
connect(ui.bind_toggle, &QPushButton::pressed, [&]() -> void { bind_key(s.toggle, ui.toggle_text); });
+ connect(ui.bind_start, &QPushButton::pressed, [&]() -> void { bind_key(s.start_tracking, ui.start_tracking_text); });
+ connect(ui.bind_stop, &QPushButton::pressed, [&]() -> void { bind_key(s.stop_tracking, ui.stop_tracking_text); });
+ connect(ui.bind_toggle_tracking, &QPushButton::pressed, [&]() -> void { bind_key(s.toggle_tracking, ui.toggle_tracking_text); });
ui.center_text->setText(kopts_to_string(s.center));
ui.toggle_text->setText(kopts_to_string(s.toggle));
diff --git a/gui/ui.cpp b/gui/ui.cpp
index fc6d4754..c3e47912 100644
--- a/gui/ui.cpp
+++ b/gui/ui.cpp
@@ -281,7 +281,7 @@ void MainWindow::startTracker() {
return;
}
- work = std::make_shared<Work>(s, pose, libs, this, winId());
+ work = std::make_shared<Work>(s, pose, libs, winId());
reload_options();
@@ -489,27 +489,6 @@ void MainWindow::profileSelected(QString name)
}
}
-void MainWindow::shortcutRecentered()
-{
- qDebug() << "Center";
- if (work)
- work->tracker->center();
-}
-
-void MainWindow::shortcutToggled()
-{
- qDebug() << "Toggle";
- if (work)
- work->tracker->toggle_enabled();
-}
-
-void MainWindow::shortcutZeroed()
-{
- qDebug() << "Zero";
- if (work)
- work->tracker->zero();
-}
-
void MainWindow::ensure_tray()
{
if (tray)
diff --git a/gui/ui.h b/gui/ui.h
index 44bcde0c..b4bab271 100644
--- a/gui/ui.h
+++ b/gui/ui.h
@@ -100,10 +100,6 @@ private slots:
void startTracker();
void stopTracker();
void reload_options();
-public slots:
- void shortcutRecentered();
- void shortcutToggled();
- void shortcutZeroed();
public:
MainWindow();
~MainWindow();
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()