From 7ed453de4a77266e7e29f2f944b285b4fe3b248b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 15 May 2016 12:30:35 +0200 Subject: api,gui: disable all keyboard shortcuts while binding a key Previous disallowed binding an already-bound key on Unix since Qxt doesn't pass through bound keys unlike the Windows implementation. Refactor some common code. The Windows implementation isn't even compile-tested. --- gui/options-dialog.cpp | 3 --- gui/options-dialog.hpp | 3 +-- gui/ui.cpp | 33 +++++++++++++++++++++++---------- gui/ui.h | 2 +- opentrack/keybinding-worker.cpp | 2 +- opentrack/keybinding-worker.hpp | 3 ++- opentrack/shortcuts.cpp | 21 +++++++++++++++++---- opentrack/shortcuts.h | 1 + 8 files changed, 46 insertions(+), 22 deletions(-) diff --git a/gui/options-dialog.cpp b/gui/options-dialog.cpp index c99bc730..e7a54bb2 100755 --- a/gui/options-dialog.cpp +++ b/gui/options-dialog.cpp @@ -30,10 +30,8 @@ static QString kopts_to_string(const key_opts& kopts) } OptionsDialog::OptionsDialog(main_settings& main, - std::function register_global_keys, std::function pause_keybindings) : main(main), - register_global_keys(register_global_keys), pause_keybindings(pause_keybindings) { ui.setupUi(this); @@ -131,7 +129,6 @@ void OptionsDialog::bind_key(key_opts& kopts, QLabel* label) d.show(); d.exec(); pause_keybindings(false); - register_global_keys(); label->setText(kopts_to_string(kopts)); delete l; } diff --git a/gui/options-dialog.hpp b/gui/options-dialog.hpp index 7700162b..a879a208 100644 --- a/gui/options-dialog.hpp +++ b/gui/options-dialog.hpp @@ -12,10 +12,9 @@ class OptionsDialog: public QWidget signals: void reload(); public: - OptionsDialog(main_settings& main, std::function register_global_keys, std::function pause_keybindings); + OptionsDialog(main_settings& main, std::function pause_keybindings); private: main_settings& main; - std::function register_global_keys; std::function pause_keybindings; Ui::UI_Settings ui; void closeEvent(QCloseEvent *) override { doCancel(); } diff --git a/gui/ui.cpp b/gui/ui.cpp index 2832126b..004b67c4 100755 --- a/gui/ui.cpp +++ b/gui/ui.cpp @@ -22,8 +22,7 @@ MainWindow::MainWindow() : pose_update_timer(this), kbd_quit(QKeySequence("Ctrl+Q"), this), - is_refreshing_profiles(false), - keys_paused(false) + is_refreshing_profiles(false) { ui.setupUi(this); @@ -109,19 +108,19 @@ MainWindow::MainWindow() : QMessageBox::Ok, QMessageBox::NoButton); connect(this, &MainWindow::emit_start_tracker, - this, [&]() -> void { if (keys_paused) return; qDebug() << "start tracker"; startTracker(); }, + this, [&]() -> void { qDebug() << "start tracker"; startTracker(); }, Qt::QueuedConnection); connect(this, &MainWindow::emit_stop_tracker, - this, [&]() -> void { if (keys_paused) return; qDebug() << "stop tracker"; stopTracker(); }, + this, [&]() -> void { qDebug() << "stop tracker"; stopTracker(); }, Qt::QueuedConnection); connect(this, &MainWindow::emit_toggle_tracker, - this, [&]() -> void { if (keys_paused) return; qDebug() << "toggle tracker"; if (work) stopTracker(); else startTracker(); }, + this, [&]() -> void { qDebug() << "toggle tracker"; if (work) stopTracker(); else startTracker(); }, Qt::QueuedConnection); connect(this, &MainWindow::emit_restart_tracker, - this, [&]() -> void { if (keys_paused) return; qDebug() << "retart tracker"; stopTracker(); startTracker(); }, + this, [&]() -> void { qDebug() << "restart tracker"; stopTracker(); startTracker(); }, Qt::QueuedConnection); register_shortcuts(); @@ -504,11 +503,10 @@ bool mk_window(mem* place, Args&&... params) } void MainWindow::show_options_dialog() { - if (mk_window(&options_widget, - s, - [&]() -> void { register_shortcuts(); }, - [&](bool flag) -> void { keys_paused = flag; })) + if (mk_window(&options_widget, s, [&](bool flag) -> void { set_keys_enabled(!flag); })) + { connect(options_widget.get(), SIGNAL(reload()), this, SLOT(reload_options())); + } } void MainWindow::showCurveConfiguration() { @@ -606,6 +604,21 @@ void MainWindow::maybe_start_profile_from_executable() } } +void MainWindow::set_keys_enabled(bool flag) +{ + if (!flag) + { + if (work) + work->sc->reload({}); + global_shortcuts.reload({}); + } + else + { + register_shortcuts(); + } + qDebug() << "keybindings set to" << flag; +} + void MainWindow::set_profile(const QString &profile) { QSettings settings(OPENTRACK_ORG); diff --git a/gui/ui.h b/gui/ui.h index 9adb69a2..fbfd1237 100755 --- a/gui/ui.h +++ b/gui/ui.h @@ -55,7 +55,6 @@ class MainWindow : public QMainWindow, private State process_detector_worker det; QMenu profile_menu; bool is_refreshing_profiles; - volatile bool keys_paused; QTimer save_timer; mem current_tracker() @@ -120,4 +119,5 @@ public: void save_mappings(); void load_mappings(); static void set_working_directory(); + void set_keys_enabled(bool flag); }; diff --git a/opentrack/keybinding-worker.cpp b/opentrack/keybinding-worker.cpp index 2d133c2f..8bb99b8c 100644 --- a/opentrack/keybinding-worker.cpp +++ b/opentrack/keybinding-worker.cpp @@ -16,7 +16,7 @@ bool Key::should_process() { - if (keycode == 0 && guid == "") + if (!enabled || keycode == 0 && guid == "") return false; bool ret = timer.elapsed_ms() > 100; timer.start(); diff --git a/opentrack/keybinding-worker.hpp b/opentrack/keybinding-worker.hpp index fa50a974..19db0fa9 100644 --- a/opentrack/keybinding-worker.hpp +++ b/opentrack/keybinding-worker.hpp @@ -34,9 +34,10 @@ struct Key { bool ctrl; bool alt; bool held; + bool enabled; Timer timer; public: - Key() : keycode(0), shift(false), ctrl(false), alt(false), held(true) {} + Key() : keycode(0), shift(false), ctrl(false), alt(false), held(true), enabled(true) {} bool should_process(); }; diff --git a/opentrack/shortcuts.cpp b/opentrack/shortcuts.cpp index 2caa1790..e1087008 100644 --- a/opentrack/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -9,17 +9,30 @@ #include "shortcuts.h" #include "win32-shortcuts.h" -void Shortcuts::bind_keyboard_shortcut(K &key, const key_opts& k, unused(bool, held)) +void Shortcuts::free_binding(K& key) { -#if !defined(_WIN32) - using sh = QxtGlobalShortcut; +#ifndef _WIN32 if (key) { key->setEnabled(false); key->setShortcut(QKeySequence::UnknownKey); - std::shared_ptr tmp(nullptr); + std::shared_ptr tmp(nullptr); key.swap(tmp); } +#else + key.keycode = 0; + key.guid = ""; +#endif +} + +void Shortcuts::bind_keyboard_shortcut(K &key, const key_opts& k, unused(bool, held)) +{ +#if !defined(_WIN32) + using sh = QxtGlobalShortcut; + if (key) + { + free_binding(key); + } key = std::make_shared(); diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h index b4a6f586..e9935b01 100644 --- a/opentrack/shortcuts.h +++ b/opentrack/shortcuts.h @@ -61,6 +61,7 @@ public: void reload(const std::vector> &keys_); private: + void free_binding(K& key); void bind_keyboard_shortcut(K &key, const key_opts& k, bool held); #ifdef _WIN32 void receiver(const Key& k); -- cgit v1.2.3