diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 08:09:08 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2014-10-20 08:09:08 +0200 |
commit | feb12bd0eecc9f09ef7a1ab7fc60858ea519edbe (patch) | |
tree | 5e2132290e27490d6878f442a6762b406934d503 | |
parent | f493c94a8d8e44efa9bfbccba81ae8016efb9fee (diff) |
refactor 1/2 (?)
-rw-r--r-- | facetracknoir/facetracknoir.cpp | 212 | ||||
-rw-r--r-- | facetracknoir/facetracknoir.h | 44 | ||||
-rw-r--r-- | facetracknoir/ftnoir_keyboardshortcuts.ui | 125 | ||||
-rw-r--r-- | facetracknoir/shortcuts.h | 83 | ||||
-rw-r--r-- | opentrack/main-settings.hpp | 20 | ||||
-rw-r--r-- | opentrack/shortcuts.cpp (renamed from facetracknoir/shortcuts.cpp) | 78 | ||||
-rw-r--r-- | opentrack/shortcuts.h | 139 | ||||
-rw-r--r-- | opentrack/tracker.h | 1 | ||||
-rw-r--r-- | opentrack/work.hpp | 63 |
9 files changed, 399 insertions, 366 deletions
diff --git a/facetracknoir/facetracknoir.cpp b/facetracknoir/facetracknoir.cpp index 005e85f5..4c074e6e 100644 --- a/facetracknoir/facetracknoir.cpp +++ b/facetracknoir/facetracknoir.cpp @@ -28,23 +28,16 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), b(bundle("opentrack-ui")), s(b), - #if defined(_WIN32) - keybindingWorker(NULL), - #else - keyCenter(this), - keyToggle(this), - #endif pose(std::vector<axis_opts*>{&s.a_x, &s.a_y, &s.a_z, &s.a_yaw, &s.a_pitch, &s.a_roll}), timUpdateHeadPose(this), kbd_quit(QKeySequence("Ctrl+Q"), this), - no_feed_pixmap(":/uielements/no-feed.png"), - module_list(dylib::enum_libraries()) + no_feed_pixmap(":/uielements/no-feed.png") { ui.setupUi(this); + setFixedSize(size()); - ui.video_frame_label->setPixmap(no_feed_pixmap); updateButtonState(false, false); - + ui.video_frame_label->setPixmap(no_feed_pixmap); QDir::setCurrent(QCoreApplication::applicationDirPath()); connect(ui.btnLoad, SIGNAL(clicked()), this, SLOT(open())); @@ -57,12 +50,17 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), connect(ui.btnShowServerControls, SIGNAL(clicked()), this, SLOT(showServerControls())); connect(ui.btnShowFilterControls, SIGNAL(clicked()), this, SLOT(showFilterControls())); - filter_modules.push_back(nullptr); + modules.filters().push_back(nullptr); ui.iconcomboFilter->addItem(QIcon(), ""); - fill_combobox(dylib::Tracker, module_list, tracker_modules, ui.iconcomboTrackerSource); - fill_combobox(dylib::Protocol, module_list, protocol_modules, ui.iconcomboProtocol); - fill_combobox(dylib::Filter, module_list, filter_modules, ui.iconcomboFilter); + for (auto x : modules.trackers()) + ui.iconcomboTrackerSource->addItem(x->icon, x->name); + + for (auto x : modules.protocols()) + ui.iconcomboProtocol->addItem(x->icon, x->name); + + for (auto x : modules.filters()) + ui.iconcomboFilter->addItem(x->icon, x->name); fill_profile_combobox(); @@ -72,15 +70,9 @@ FaceTrackNoIR::FaceTrackNoIR() : QMainWindow(nullptr), connect(ui.btnStartTracker, SIGNAL(clicked()), this, SLOT(startTracker())); connect(ui.btnStopTracker, SIGNAL(clicked()), this, SLOT(stopTracker())); - connect(ui.iconcomboProfile, SIGNAL(currentIndexChanged(int)), this, SLOT(profileSelected(int))); + connect(&timUpdateHeadPose, SIGNAL(timeout()), this, SLOT(showHeadPose())); - -#ifndef _WIN32 - connect(&keyCenter, SIGNAL(activated()), this, SLOT(shortcutRecentered())); - connect(&keyToggle, SIGNAL(activated()), this, SLOT(shortcutToggled())); -#endif - connect(&kbd_quit, SIGNAL(activated()), this, SLOT(exit())); kbd_quit.setEnabled(true); } @@ -91,25 +83,6 @@ FaceTrackNoIR::~FaceTrackNoIR() save(); } -void FaceTrackNoIR::fill_combobox(dylib::Type t, - QList<ptr<dylib>> list, - QList<ptr<dylib>>& out_list, - QComboBox* cbx) -{ - for (auto x : list) - { - if (t == x->type) - { - cbx->addItem(x->icon, x->name); - out_list.append(x); - } - } -} - -QFrame* FaceTrackNoIR::video_frame() { - return ui.video_frame; -} - void FaceTrackNoIR::open() { QFileDialog dialog(this); dialog.setFileMode(QFileDialog::ExistingFile); @@ -226,15 +199,27 @@ void FaceTrackNoIR::updateButtonState(bool running, bool inertialp) ui.video_frame_label->setVisible(not_running || inertialp); } +void FaceTrackNoIR::bindKeyboardShortcuts() +{ + if (work) + work->sc = std::make_shared<Shortcuts>(); +} + void FaceTrackNoIR::startTracker( ) { b->save(); loadSettings(); bindKeyboardShortcuts(); - // Tracker dtor needs run first - tracker = nullptr; + // tracker dtor needs run first + work = nullptr; - libs = SelectedLibraries(video_frame(), current_tracker(), current_protocol(), current_filter()); + libs = SelectedLibraries(ui.video_frame, current_tracker(), current_protocol(), current_filter()); + work = std::make_shared<Work>(s, pose, libs, this); + + { + double p[6] = {0,0,0, 0,0,0}; + display_pose(p, p); + } if (!libs.correct) { @@ -252,8 +237,6 @@ void FaceTrackNoIR::startTracker( ) { ui.video_frame->show(); timUpdateHeadPose.start(50); - tracker = std::make_shared<Tracker>(s, pose, libs); - tracker->start(); // NB check valid since SelectedLibraries ctor called // trackers take care of layout state updates @@ -293,41 +276,53 @@ void FaceTrackNoIR::stopTracker( ) { pFilterDialog = nullptr; } - tracker = nullptr; + work = nullptr; libs = SelectedLibraries(); + + { + double p[6] = {0,0,0, 0,0,0}; + display_pose(p, p); + } updateButtonState(false, false); } -void FaceTrackNoIR::showHeadPose() +void FaceTrackNoIR::display_pose(const double *mapped, const double *raw) { - double mapped[6], raw[6]; - - tracker->get_raw_and_mapped_poses(mapped, raw); - ui.pose_display->rotateBy(mapped[Yaw], mapped[Roll], mapped[Pitch]); if (mapping_widget) mapping_widget->update(); + double mapped_[6], raw_[6]; + for (int i = 0; i < 6; i++) { - mapped[i] = (int) mapped[i]; - raw[i] = (int) raw[i]; + mapped_[i] = (int) mapped[i]; + raw_[i] = (int) raw[i]; } - ui.lcdNumX->display(raw[TX]); - ui.lcdNumY->display(raw[TY]); - ui.lcdNumZ->display(raw[TZ]); - ui.lcdNumRotX->display(raw[Yaw]); - ui.lcdNumRotY->display(raw[Pitch]); - ui.lcdNumRotZ->display(raw[Roll]); + ui.lcdNumX->display(raw_[TX]); + ui.lcdNumY->display(raw_[TY]); + ui.lcdNumZ->display(raw_[TZ]); + ui.lcdNumRotX->display(raw_[Yaw]); + ui.lcdNumRotY->display(raw_[Pitch]); + ui.lcdNumRotZ->display(raw_[Roll]); + + ui.lcdNumOutputPosX->display(mapped_[TX]); + ui.lcdNumOutputPosY->display(mapped_[TY]); + ui.lcdNumOutputPosZ->display(mapped_[TZ]); + ui.lcdNumOutputRotX->display(mapped_[Yaw]); + ui.lcdNumOutputRotY->display(mapped_[Pitch]); + ui.lcdNumOutputRotZ->display(mapped_[Roll]); +} + +void FaceTrackNoIR::showHeadPose() +{ + double mapped[6], raw[6]; + + work->tracker->get_raw_and_mapped_poses(mapped, raw); - ui.lcdNumOutputPosX->display(mapped[TX]); - ui.lcdNumOutputPosY->display(mapped[TY]); - ui.lcdNumOutputPosZ->display(mapped[TZ]); - ui.lcdNumOutputRotX->display(mapped[Yaw]); - ui.lcdNumOutputRotY->display(mapped[Pitch]); - ui.lcdNumOutputRotZ->display(mapped[Roll]); + display_pose(mapped, raw); if (libs.pProtocol) { @@ -338,7 +333,7 @@ void FaceTrackNoIR::showHeadPose() void FaceTrackNoIR::showTrackerSettings() { - ptr<dylib> lib = tracker_modules.value(ui.iconcomboTrackerSource->currentIndex(), nullptr); + ptr<dylib> lib = modules.trackers().value(ui.iconcomboTrackerSource->currentIndex(), nullptr); if (lib) { pTrackerDialog = ptr<ITrackerDialog>(reinterpret_cast<ITrackerDialog*>(lib->Dialog())); @@ -349,7 +344,7 @@ void FaceTrackNoIR::showTrackerSettings() } void FaceTrackNoIR::showServerControls() { - ptr<dylib> lib = protocol_modules.value(ui.iconcomboProtocol->currentIndex(), nullptr); + ptr<dylib> lib = modules.protocols().value(ui.iconcomboProtocol->currentIndex(), nullptr); if (lib) { pProtocolDialog = ptr<IProtocolDialog>(reinterpret_cast<IProtocolDialog*>(lib->Dialog())); @@ -359,7 +354,7 @@ void FaceTrackNoIR::showServerControls() { } void FaceTrackNoIR::showFilterControls() { - ptr<dylib> lib = filter_modules.value(ui.iconcomboFilter->currentIndex(), nullptr); + ptr<dylib> lib = modules.filters().value(ui.iconcomboFilter->currentIndex(), nullptr); if (lib) { pFilterDialog = ptr<IFilterDialog>(reinterpret_cast<IFilterDialog*>(lib->Dialog())); @@ -369,9 +364,10 @@ void FaceTrackNoIR::showFilterControls() { } } void FaceTrackNoIR::showKeyboardShortcuts() { - shortcuts_widget = std::make_shared<KeyboardShortcutDialog>( this, this ); + shortcuts_widget = std::make_shared<KeyboardShortcutDialog>(); shortcuts_widget->show(); shortcuts_widget->raise(); + connect(shortcuts_widget.get(), SIGNAL(reload()), this, SLOT(bindKeyboardShortcuts())); } void FaceTrackNoIR::showCurveConfiguration() { mapping_widget = std::make_shared<MapWidget>(pose, s, this); @@ -394,86 +390,16 @@ void FaceTrackNoIR::profileSelected(int index) loadSettings(); } -#if !defined(_WIN32) -void FaceTrackNoIR::bind_keyboard_shortcut(QxtGlobalShortcut& key, key_opts& k) -{ - key.setShortcut(QKeySequence::fromString("")); - key.setDisabled(); - const int idx = k.key_index; - if (idx > 0) - { - QString seq(global_key_sequences.value(idx, "")); - if (!seq.isEmpty()) - { - if (k.shift) - seq = "Shift+" + seq; - if (k.alt) - seq = "Alt+" + seq; - if (k.ctrl) - seq = "Ctrl+" + seq; - key.setShortcut(QKeySequence::fromString(seq, QKeySequence::PortableText)); - key.setEnabled(); - } else { - key.setDisabled(); - } - } -} -#else -static void bind_keyboard_shortcut(Key& key, key_opts& k) -{ - int idx = k.key_index; - if (idx > 0) - { - key.keycode = 0; - key.shift = key.alt = key.ctrl = 0; - if (idx < global_windows_key_sequences.size()) - key.keycode = global_windows_key_sequences[idx]; - key.shift = k.shift; - key.alt = k.alt; - key.ctrl = k.ctrl; - } -} -#endif - -void FaceTrackNoIR::bindKeyboardShortcuts() -{ -#if !defined(_WIN32) - bind_keyboard_shortcut(keyCenter, s.center_key); - bind_keyboard_shortcut(keyToggle, s.toggle_key); -#else - bind_keyboard_shortcut(keyCenter, s.center_key); - bind_keyboard_shortcut(keyToggle, s.toggle_key); -#endif - if (tracker) /* running already */ - { -#if defined(_WIN32) - if (keybindingWorker) - { - keybindingWorker->should_quit = true; - keybindingWorker->wait(); - delete keybindingWorker; - keybindingWorker = NULL; - } - keybindingWorker = new KeybindingWorker(*this, keyCenter, keyToggle); - keybindingWorker->start(); -#endif - } -} - void FaceTrackNoIR::shortcutRecentered() { qDebug() << "Center"; - if (s.dingp) - QApplication::beep(); - if (tracker) - tracker->center(); + if (work) + work->tracker->center(); } void FaceTrackNoIR::shortcutToggled() { qDebug() << "Toggle"; - if (s.dingp) - QApplication::beep(); - if (tracker) - tracker->toggle_enabled(); + if (work) + work->tracker->toggle_enabled(); } diff --git a/facetracknoir/facetracknoir.h b/facetracknoir/facetracknoir.h index 32bf8239..13c2bb59 100644 --- a/facetracknoir/facetracknoir.h +++ b/facetracknoir/facetracknoir.h @@ -48,7 +48,8 @@ #include "opentrack/main-settings.hpp" #include "opentrack/plugin-support.h" #include "opentrack/tracker.h" -#include "shortcuts.h" +#include "opentrack/shortcuts.h" +#include "opentrack/work.hpp" #include "curve-config.h" using namespace options; @@ -59,70 +60,54 @@ class FaceTrackNoIR : public QMainWindow public: pbundle b; main_settings s; - ptr<Tracker> tracker; - - // XXX move kbd handling into class its own -sh 20141019 -#ifndef _WIN32 - void bind_keyboard_shortcut(QxtGlobalShortcut&, key_opts& k); -#endif private: -#if defined(_WIN32) - Key keyCenter; - Key keyToggle; - KeybindingWorker* keybindingWorker; -#else - QxtGlobalShortcut keyCenter; - QxtGlobalShortcut keyToggle; -#endif // XXX move the shit outta the _widget_, establish a class // for running tracker state, etc -sh 20141014 Mappings pose; Ui::OpentrackUI ui; + QTimer timUpdateHeadPose; SelectedLibraries libs; - ptr<ITrackerDialog> pTrackerDialog; - ptr<IProtocolDialog> pProtocolDialog; - ptr<IFilterDialog> pFilterDialog; + ptr<Work> work; ptr<KeyboardShortcutDialog> shortcuts_widget; ptr<MapWidget> mapping_widget; QShortcut kbd_quit; QPixmap no_feed_pixmap; - - QList<ptr<dylib>> filter_modules; - QList<ptr<dylib>> tracker_modules; - QList<ptr<dylib>> protocol_modules; - - QList<ptr<dylib>> module_list; + ptr<IFilterDialog> pFilterDialog; + ptr<IProtocolDialog> pProtocolDialog; + ptr<ITrackerDialog> pTrackerDialog; + Modules modules; // XXX this shit stinks -sh 20141004 // TODO move to separate class representing running tracker state ptr<dylib> current_tracker() { - return tracker_modules.value(ui.iconcomboTrackerSource->currentIndex(), nullptr); + return modules.trackers().value(ui.iconcomboTrackerSource->currentIndex(), nullptr); } ptr<dylib> current_protocol() { - return protocol_modules.value(ui.iconcomboProtocol->currentIndex(), nullptr); + return modules.protocols().value(ui.iconcomboProtocol->currentIndex(), nullptr); } ptr<dylib> current_filter() { - return filter_modules.value(ui.iconcomboFilter->currentIndex(), nullptr); + return modules.filters().value(ui.iconcomboFilter->currentIndex(), nullptr); } void createIconGroupBox(); void loadSettings(); void updateButtonState(bool running, bool inertialp); - void fill_combobox(dylib::Type t, QList<ptr<dylib>> list, QList<ptr<dylib> > &out_list, QComboBox* cbx); void fill_profile_combobox(); - QFrame *video_frame(); + void display_pose(const double* mapped, const double* raw); + public slots: void shortcutRecentered(); void shortcutToggled(); + void bindKeyboardShortcuts(); private slots: void open(); void save(); @@ -146,5 +131,4 @@ public: ~FaceTrackNoIR(); void save_mappings(); void load_mappings(); - void bindKeyboardShortcuts(); }; diff --git a/facetracknoir/ftnoir_keyboardshortcuts.ui b/facetracknoir/ftnoir_keyboardshortcuts.ui index 245b503a..f576d8fb 100644 --- a/facetracknoir/ftnoir_keyboardshortcuts.ui +++ b/facetracknoir/ftnoir_keyboardshortcuts.ui @@ -6,8 +6,8 @@ <rect> <x>0</x> <y>0</y> - <width>371</width> - <height>125</height> + <width>415</width> + <height>143</height> </rect> </property> <property name="sizePolicy"> @@ -30,6 +30,38 @@ <bool>false</bool> </property> <layout class="QGridLayout" name="gridLayout"> + <item row="2" column="0"> + <widget class="QLabel" name="textLabel2_5"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Maximum" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Toggle</string> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + </widget> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="textLabel2_3"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Maximum" vsizetype="Fixed"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Center</string> + </property> + <property name="wordWrap"> + <bool>false</bool> + </property> + </widget> + </item> <item row="2" column="1"> <widget class="QCheckBox" name="chkToggleShift"> <property name="maximumSize"> @@ -59,22 +91,6 @@ </property> </widget> </item> - <item row="2" column="0"> - <widget class="QLabel" name="textLabel2_5"> - <property name="sizePolicy"> - <sizepolicy hsizetype="Minimum" vsizetype="Fixed"> - <horstretch>0</horstretch> - <verstretch>0</verstretch> - </sizepolicy> - </property> - <property name="text"> - <string>Toggle</string> - </property> - <property name="wordWrap"> - <bool>false</bool> - </property> - </widget> - </item> <item row="1" column="3"> <widget class="QCheckBox" name="chkCenterAlt"> <property name="maximumSize"> @@ -88,8 +104,8 @@ </property> </widget> </item> - <item row="1" column="2"> - <widget class="QCheckBox" name="chkCenterCtrl"> + <item row="2" column="2"> + <widget class="QCheckBox" name="chkToggleCtrl"> <property name="maximumSize"> <size> <width>50</width> @@ -101,22 +117,6 @@ </property> </widget> </item> - <item row="1" column="4"> - <widget class="QComboBox" name="cbxCenterKey"> - <property name="minimumSize"> - <size> - <width>90</width> - <height>0</height> - </size> - </property> - <property name="toolTip"> - <string>Select Number</string> - </property> - <property name="insertPolicy"> - <enum>QComboBox::InsertAlphabetically</enum> - </property> - </widget> - </item> <item row="0" column="4"> <widget class="QLabel" name="textLabel2_4"> <property name="sizePolicy"> @@ -136,15 +136,8 @@ </property> </widget> </item> - <item row="4" column="3" colspan="2"> - <widget class="QDialogButtonBox" name="buttonBox"> - <property name="standardButtons"> - <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> - </property> - </widget> - </item> - <item row="2" column="2"> - <widget class="QCheckBox" name="chkToggleCtrl"> + <item row="2" column="3"> + <widget class="QCheckBox" name="chkToggleAlt"> <property name="maximumSize"> <size> <width>50</width> @@ -152,28 +145,19 @@ </size> </property> <property name="text"> - <string>Ctrl</string> + <string>Alt</string> </property> </widget> </item> - <item row="1" column="0"> - <widget class="QLabel" name="textLabel2_3"> - <property name="sizePolicy"> - <sizepolicy hsizetype="Minimum" vsizetype="Fixed"> - <horstretch>0</horstretch> - <verstretch>0</verstretch> - </sizepolicy> - </property> - <property name="text"> - <string>Center</string> - </property> - <property name="wordWrap"> - <bool>false</bool> + <item row="3" column="3" colspan="2"> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> </item> - <item row="2" column="3"> - <widget class="QCheckBox" name="chkToggleAlt"> + <item row="1" column="2"> + <widget class="QCheckBox" name="chkCenterCtrl"> <property name="maximumSize"> <size> <width>50</width> @@ -181,7 +165,7 @@ </size> </property> <property name="text"> - <string>Alt</string> + <string>Ctrl</string> </property> </widget> </item> @@ -198,10 +182,19 @@ </property> </widget> </item> - <item row="3" column="0" colspan="2"> - <widget class="QCheckBox" name="ding"> - <property name="text"> - <string>Ding!</string> + <item row="1" column="4"> + <widget class="QComboBox" name="cbxCenterKey"> + <property name="minimumSize"> + <size> + <width>90</width> + <height>0</height> + </size> + </property> + <property name="toolTip"> + <string>Select Number</string> + </property> + <property name="insertPolicy"> + <enum>QComboBox::InsertAlphabetically</enum> </property> </widget> </item> diff --git a/facetracknoir/shortcuts.h b/facetracknoir/shortcuts.h deleted file mode 100644 index 8e665211..00000000 --- a/facetracknoir/shortcuts.h +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once -#include <QWidget> -#include <QElapsedTimer> -#include <QThread> -#include <QMessageBox> -#include <QCheckBox> -#include <QComboBox> -#include <QSettings> -#include "ui_ftnoir_keyboardshortcuts.h" - -class FaceTrackNoIR; - -class KeyboardShortcutDialog: public QWidget -{ - Q_OBJECT -public: - KeyboardShortcutDialog( FaceTrackNoIR *ftnoir, QWidget *parent ); -private: - Ui::UICKeyboardShortcutDialog ui; - FaceTrackNoIR *mainApp; -private slots: - void doOK(); - void doCancel(); -}; - -extern QList<QString> global_key_sequences; - -#if defined(_WIN32) -extern QList<int> global_windows_key_sequences; -# undef DIRECTINPUT_VERSION -# define DIRECTINPUT_VERSION 0x0800 -# include <windows.h> -# include <dinput.h> - -struct Key { - BYTE keycode; - bool shift; - bool ctrl; - bool alt; - bool ever_pressed; - QElapsedTimer timer; -public: - Key() : keycode(0), shift(false), ctrl(false), alt(false), ever_pressed(false) - { - } -}; -#else -typedef unsigned char BYTE; -struct Key { int foo; }; -#endif - -#if defined(_WIN32) -class KeybindingWorkerImpl { -private: - LPDIRECTINPUT8 din; - LPDIRECTINPUTDEVICE8 dinkeyboard; - Key kCenter; - Key kToggle; - FaceTrackNoIR& window; -public: - volatile bool should_quit; - ~KeybindingWorkerImpl(); - KeybindingWorkerImpl(FaceTrackNoIR& w, Key keyCenter, Key keyToggle); - void run(); -}; -#else -class KeybindingWorkerImpl { -public: - KeybindingWorkerImpl(FaceTrackNoIR& w, Key keyCenter, Key keyToggle); - void run() {} -}; -#endif - -class KeybindingWorker : public QThread, public KeybindingWorkerImpl { - Q_OBJECT -public: - KeybindingWorker(FaceTrackNoIR& w, Key keyCenter, Key keyToggle) : KeybindingWorkerImpl(w, keyCenter, keyToggle) - { - } - void run() { - KeybindingWorkerImpl::run(); - } -}; diff --git a/opentrack/main-settings.hpp b/opentrack/main-settings.hpp index 841d3bca..e41a23c9 100644 --- a/opentrack/main-settings.hpp +++ b/opentrack/main-settings.hpp @@ -6,17 +6,6 @@ using namespace options; -struct key_opts { - value<int> key_index; - value<bool> ctrl, alt, shift; - key_opts(pbundle b, const QString& name) : - key_index(b, QString("key-index-%1").arg(name), 0), - ctrl(b, QString("key-ctrl-%1").arg(name), 0), - alt(b, QString("key-alt-%1").arg(name), 0), - shift(b, QString("key-shift-%1").arg(name), 0) - {} -}; - struct axis_opts { value<double> zero; value<bool> invert, altp; @@ -35,15 +24,11 @@ private: struct main_settings { pbundle b; - key_opts center_key; - key_opts toggle_key; value<QString> tracker_dll, tracker2_dll, filter_dll, protocol_dll; axis_opts a_x, a_y, a_z, a_yaw, a_pitch, a_roll; - value<bool> tcomp_p, tcomp_tz, dingp; + value<bool> tcomp_p, tcomp_tz; main_settings(pbundle b) : b(b), - center_key(b, "center"), - toggle_key(b, "toggle"), tracker_dll(b, "tracker-dll", ""), tracker2_dll(b, "tracker2-dll", ""), filter_dll(b, "filter-dll", ""), @@ -55,7 +40,6 @@ struct main_settings { a_pitch(b, "pitch", Pitch), a_roll(b, "roll", Roll), tcomp_p(b, "compensate-translation", true), - tcomp_tz(b, "compensate-translation-disable-z-axis", false), - dingp(b, "ding", true) + tcomp_tz(b, "compensate-translation-disable-z-axis", false) {} }; diff --git a/facetracknoir/shortcuts.cpp b/opentrack/shortcuts.cpp index 53d67957..18a3a312 100644 --- a/facetracknoir/shortcuts.cpp +++ b/opentrack/shortcuts.cpp @@ -1,18 +1,9 @@ -#include "facetracknoir.h" #include "shortcuts.h" -// XXX todo remove ref to ui class -sh 20141019 - -KeyboardShortcutDialog::KeyboardShortcutDialog( FaceTrackNoIR *ftnoir, QWidget *parent ) - : QWidget( parent, Qt::Dialog) +KeyboardShortcutDialog::KeyboardShortcutDialog() { ui.setupUi( this ); - QPoint offsetpos(100, 100); - this->move(parent->pos() + offsetpos); - - mainApp = ftnoir; - connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK())); connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel())); @@ -21,28 +12,25 @@ KeyboardShortcutDialog::KeyboardShortcutDialog( FaceTrackNoIR *ftnoir, QWidget * ui.cbxToggleKey->addItem(global_key_sequences.at(i)); } - tie_setting(mainApp->s.center_key.key_index, ui.cbxCenterKey); - tie_setting(mainApp->s.center_key.alt, ui.chkCenterAlt); - tie_setting(mainApp->s.center_key.shift, ui.chkCenterShift); - tie_setting(mainApp->s.center_key.ctrl, ui.chkCenterCtrl); - - tie_setting(mainApp->s.toggle_key.key_index, ui.cbxToggleKey); - tie_setting(mainApp->s.toggle_key.alt, ui.chkToggleAlt); - tie_setting(mainApp->s.toggle_key.shift, ui.chkToggleShift); - tie_setting(mainApp->s.toggle_key.ctrl, ui.chkToggleCtrl); + tie_setting(s.center.key_index, ui.cbxCenterKey); + tie_setting(s.center.alt, ui.chkCenterAlt); + tie_setting(s.center.shift, ui.chkCenterShift); + tie_setting(s.center.ctrl, ui.chkCenterCtrl); - tie_setting(mainApp->s.dingp, ui.ding); + tie_setting(s.toggle.key_index, ui.cbxToggleKey); + tie_setting(s.toggle.alt, ui.chkToggleAlt); + tie_setting(s.toggle.shift, ui.chkToggleShift); + tie_setting(s.toggle.ctrl, ui.chkToggleCtrl); } void KeyboardShortcutDialog::doOK() { - mainApp->b->save(); + s.b->save(); this->close(); - if (mainApp->tracker) - mainApp->bindKeyboardShortcuts(); + emit reload(); } void KeyboardShortcutDialog::doCancel() { - mainApp->s.b->reload(); + s.b->reload(); close(); } @@ -50,6 +38,8 @@ void KeyboardShortcutDialog::doCancel() { #include <windows.h> KeybindingWorkerImpl::~KeybindingWorkerImpl() { + should_quit = true; + wait(); if (dinkeyboard) { dinkeyboard->Unacquire(); dinkeyboard->Release(); @@ -58,7 +48,7 @@ KeybindingWorkerImpl::~KeybindingWorkerImpl() { din->Release(); } -KeybindingWorkerImpl::KeybindingWorkerImpl(FaceTrackNoIR& w, Key keyCenter, Key keyToggle) +KeybindingWorkerImpl::KeybindingWorkerImpl(Key keyCenter, Key keyToggle) : din(0), dinkeyboard(0), kCenter(keyCenter), kToggle(keyToggle), window(w), should_quit(true) { if (DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&din, NULL) != DI_OK) { @@ -142,3 +132,41 @@ void KeybindingWorkerImpl::run() { #else #endif + +void Shortcuts::bind_keyboard_shortcut(K &key, key_opts& k) +{ +#if !defined(_WIN32) + key.setShortcut(QKeySequence::fromString("")); + key.setDisabled(); + const int idx = k.key_index; + if (idx > 0) + { + QString seq(global_key_sequences.value(idx, "")); + if (!seq.isEmpty()) + { + if (k.shift) + seq = "Shift+" + seq; + if (k.alt) + seq = "Alt+" + seq; + if (k.ctrl) + seq = "Ctrl+" + seq; + key.setShortcut(QKeySequence::fromString(seq, QKeySequence::PortableText)); + key.setEnabled(); + } else { + key.setDisabled(); + } + } +#else + int idx = k.key_index; + if (idx > 0) + { + key.keycode = 0; + key.shift = key.alt = key.ctrl = 0; + if (idx < global_windows_key_sequences.size()) + key.keycode = global_windows_key_sequences[idx]; + key.shift = k.shift; + key.alt = k.alt; + key.ctrl = k.ctrl; + } +#endif +}
\ No newline at end of file diff --git a/opentrack/shortcuts.h b/opentrack/shortcuts.h new file mode 100644 index 00000000..2962f4fd --- /dev/null +++ b/opentrack/shortcuts.h @@ -0,0 +1,139 @@ +#pragma once +#include <QWidget> +#include <QElapsedTimer> +#include <QThread> +#include <QMessageBox> +#include <QCheckBox> +#include <QComboBox> +#include <QSettings> + +#include "qxt-mini/QxtGlobalShortcut" +#include "opentrack/plugin-support.h" +#include "opentrack/options.hpp" +#include "ui_ftnoir_keyboardshortcuts.h" + +using namespace options; + +extern QList<QString> global_key_sequences; + +struct key_opts { + value<int> key_index; + value<bool> ctrl, alt, shift; + key_opts(pbundle b, const QString& name) : + key_index(b, QString("key-index-%1").arg(name), 0), + ctrl(b, QString("key-ctrl-%1").arg(name), 0), + alt(b, QString("key-alt-%1").arg(name), 0), + shift(b, QString("key-shift-%1").arg(name), 0) + {} +}; + +#if defined(_WIN32) +extern QList<int> global_windows_key_sequences; +# undef DIRECTINPUT_VERSION +# define DIRECTINPUT_VERSION 0x0800 +# include <windows.h> +# include <dinput.h> + +struct Key { + BYTE keycode; + bool shift; + bool ctrl; + bool alt; + bool ever_pressed; + QElapsedTimer timer; +public: + Key() : keycode(0), shift(false), ctrl(false), alt(false), ever_pressed(false) + { + } +}; +#else +typedef unsigned char BYTE; +struct Key { int foo; }; +#endif + +#if defined(_WIN32) +class KeybindingWorkerImpl { +private: + LPDIRECTINPUT8 din; + LPDIRECTINPUTDEVICE8 dinkeyboard; + Key kCenter; + Key kToggle; +public: + volatile bool should_quit; + ~KeybindingWorkerImpl(); + KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + void run(); +}; +#else +class KeybindingWorkerImpl { +public: + KeybindingWorkerImpl(Key keyCenter, Key keyToggle); + void run() {} +}; +#endif + +template<typename t_self> +struct KeybindingWorker : public QThread, public KeybindingWorkerImpl { + KeybindingWorker(Key keyCenter, Key keyToggle) : KeybindingWorkerImpl(keyCenter, keyToggle) + { + } + void run() { + KeybindingWorkerImpl::run(); + } +}; + + +struct Shortcuts { + using K = +#ifndef _WIN32 + QxtGlobalShortcut +#else + Key +#endif + ; + + K keyCenter; + K keyToggle; +#ifdef _WIN32 + ptr<KeybindingWorker> keybindingWorker; +#endif + + struct settings { + pbundle b; + key_opts center, toggle; + settings() : + b(bundle("keyboard-shortcuts")), + center(b, "center"), + toggle(b, "toggle") + {} + } s; + + Shortcuts() + { + bind_keyboard_shortcut(keyCenter, s.center); + bind_keyboard_shortcut(keyToggle, s.toggle); +#ifdef _WIN32 + keybindingWorker = nullptr; + keybindingWorker = std::make_shared<KeybindingWorker>(*this, keyCenter, keyToggle); + keybindingWorker.start(); +#endif + } +private: + void bind_keyboard_shortcut(K& key, key_opts& k); +}; + +class KeyboardShortcutDialog: public QWidget +{ + Q_OBJECT +public: + KeyboardShortcutDialog(); +private: + Ui::UICKeyboardShortcutDialog ui; + Shortcuts::settings s; + ptr<Shortcuts> sc; +signals: + void reload(); +private slots: + void doOK(); + void doCancel(); +};
\ No newline at end of file diff --git a/opentrack/tracker.h b/opentrack/tracker.h index 02d6bee2..5ec44968 100644 --- a/opentrack/tracker.h +++ b/opentrack/tracker.h @@ -20,7 +20,6 @@ class Tracker : private QThread { private: QMutex mtx; main_settings& s; - // XXX can be const-cast when functionconfig const-correct -sh 20141004 Mappings& m; Timer t; diff --git a/opentrack/work.hpp b/opentrack/work.hpp new file mode 100644 index 00000000..3e51b480 --- /dev/null +++ b/opentrack/work.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include "opentrack/main-settings.hpp" +#include "opentrack/plugin-support.h" +#include "opentrack/tracker.h" +#include "opentrack/shortcuts.h" + +#include <QObject> +#include <QFrame> +#include <memory> + +struct Modules { + Modules() : + module_list(dylib::enum_libraries()), + filter_modules(filter(dylib::Filter)), + tracker_modules(filter(dylib::Tracker)), + protocol_modules(filter(dylib::Protocol)) + {} + QList<ptr<dylib>>& filters() { return filter_modules; } + QList<ptr<dylib>>& trackers() { return tracker_modules; } + QList<ptr<dylib>>& protocols() { return protocol_modules; } +private: + QList<ptr<dylib>> module_list; + QList<ptr<dylib>> filter_modules; + QList<ptr<dylib>> tracker_modules; + QList<ptr<dylib>> protocol_modules; + + QList<ptr<dylib>> filter(dylib::Type t) + { + QList<ptr<dylib>> ret; + for (auto x : module_list) + if (x->type == t) + ret.push_back(x); + return ret; + } +}; + +struct Work +{ + main_settings& s; + SelectedLibraries libs; + ptr<Tracker> tracker; + ptr<Shortcuts> sc; + + Work(main_settings& s, Mappings& m, SelectedLibraries& libs, QObject* recv) : + s(s), libs(libs), + tracker(std::make_shared<Tracker>(s, m, libs)), + sc(std::make_shared<Shortcuts>()) + { +#ifndef _WIN32 + QObject::connect(&sc->keyCenter, SIGNAL(activated()), recv, SLOT(shortcutRecentered())); + QObject::connect(&sc->keyToggle, SIGNAL(activated()), recv, SLOT(shortcutToggled())); +#endif + tracker->start(); + } + + ~Work() + { + // order matters, otherwise use-after-free -sh + tracker = nullptr; + libs = SelectedLibraries(); + } +};
\ No newline at end of file |