From c0da74ccd25837186ebb48af5e21ec250d2a6bdf Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 11 Feb 2019 22:12:47 +0100 Subject: proto/vjoystick: fix threading issue Acquiring given joystick id must be done on the same thread as the feeder apparently. Provide diagnostics on failure. Issue: #860 --- proto-vjoystick/lang/nl_NL.ts | 49 ++++++++----- proto-vjoystick/lang/ru_RU.ts | 49 ++++++++----- proto-vjoystick/lang/stub.ts | 49 ++++++++----- proto-vjoystick/lang/zh_CN.ts | 49 ++++++++----- proto-vjoystick/vjoystick.cpp | 135 +++++++++++++++++++++-------------- proto-vjoystick/vjoystick.h | 41 ++++------- proto-vjoystick/vjoystick.ui | 21 ++---- proto-vjoystick/vjoystick_dialog.cpp | 1 + 8 files changed, 235 insertions(+), 159 deletions(-) (limited to 'proto-vjoystick') diff --git a/proto-vjoystick/lang/nl_NL.ts b/proto-vjoystick/lang/nl_NL.ts index 027e5f39..22644924 100644 --- a/proto-vjoystick/lang/nl_NL.ts +++ b/proto-vjoystick/lang/nl_NL.ts @@ -4,49 +4,66 @@ vjoystick - VJoy + vjoystick won't work without the driver installed. - <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a></p></body></html> + Download the driver - - - vjoystick_metadata - Joystick emulation -- vjoystick + Visit project site - - - vjoystick_proto - vjoystick driver missing + Virtual joystick - vjoystick won't work without the driver installed. + driver/SDK version mismatch - Download the driver + BUG: joystick claims it's in use - Visit project site + Virtual joystick already in use - vjoystick not installed or disabled + Device missing. Add joystick #1. - can't initialize vjoystick + Unknown error - Virtual joystick + Unknown error #%1 + + + + vjoystick driver problem + + + + Driver problem. + + + + vjoystick + + + + <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a>.</p></body></html> + + + + + vjoystick_metadata + + Joystick emulation -- vjoystick diff --git a/proto-vjoystick/lang/ru_RU.ts b/proto-vjoystick/lang/ru_RU.ts index 583144ef..b02030e3 100644 --- a/proto-vjoystick/lang/ru_RU.ts +++ b/proto-vjoystick/lang/ru_RU.ts @@ -4,49 +4,66 @@ vjoystick - VJoy + vjoystick won't work without the driver installed. - <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a></p></body></html> + Download the driver - - - vjoystick_metadata - Joystick emulation -- vjoystick + Visit project site - - - vjoystick_proto - vjoystick driver missing + Virtual joystick - vjoystick won't work without the driver installed. + driver/SDK version mismatch - Download the driver + BUG: joystick claims it's in use - Visit project site + Virtual joystick already in use - vjoystick not installed or disabled + Device missing. Add joystick #1. - can't initialize vjoystick + Unknown error - Virtual joystick + Unknown error #%1 + + + + vjoystick driver problem + + + + Driver problem. + + + + vjoystick + + + + <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a>.</p></body></html> + + + + + vjoystick_metadata + + Joystick emulation -- vjoystick diff --git a/proto-vjoystick/lang/stub.ts b/proto-vjoystick/lang/stub.ts index a71e2882..0fe38ef2 100644 --- a/proto-vjoystick/lang/stub.ts +++ b/proto-vjoystick/lang/stub.ts @@ -4,49 +4,66 @@ vjoystick - VJoy + vjoystick won't work without the driver installed. - <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a></p></body></html> + Download the driver - - - vjoystick_metadata - Joystick emulation -- vjoystick + Visit project site - - - vjoystick_proto - vjoystick driver missing + Virtual joystick - vjoystick won't work without the driver installed. + driver/SDK version mismatch - Download the driver + BUG: joystick claims it's in use - Visit project site + Virtual joystick already in use - vjoystick not installed or disabled + Device missing. Add joystick #1. - can't initialize vjoystick + Unknown error - Virtual joystick + Unknown error #%1 + + + + vjoystick driver problem + + + + Driver problem. + + + + vjoystick + + + + <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a>.</p></body></html> + + + + + vjoystick_metadata + + Joystick emulation -- vjoystick diff --git a/proto-vjoystick/lang/zh_CN.ts b/proto-vjoystick/lang/zh_CN.ts index a71e2882..0fe38ef2 100644 --- a/proto-vjoystick/lang/zh_CN.ts +++ b/proto-vjoystick/lang/zh_CN.ts @@ -4,49 +4,66 @@ vjoystick - VJoy + vjoystick won't work without the driver installed. - <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a></p></body></html> + Download the driver - - - vjoystick_metadata - Joystick emulation -- vjoystick + Visit project site - - - vjoystick_proto - vjoystick driver missing + Virtual joystick - vjoystick won't work without the driver installed. + driver/SDK version mismatch - Download the driver + BUG: joystick claims it's in use - Visit project site + Virtual joystick already in use - vjoystick not installed or disabled + Device missing. Add joystick #1. - can't initialize vjoystick + Unknown error - Virtual joystick + Unknown error #%1 + + + + vjoystick driver problem + + + + Driver problem. + + + + vjoystick + + + + <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a>.</p></body></html> + + + + + vjoystick_metadata + + Joystick emulation -- vjoystick diff --git a/proto-vjoystick/vjoystick.cpp b/proto-vjoystick/vjoystick.cpp index d781d8e1..268fa343 100644 --- a/proto-vjoystick/vjoystick.cpp +++ b/proto-vjoystick/vjoystick.cpp @@ -26,7 +26,7 @@ #define OPENTRACK_VJOYSTICK_ID 1 -const unsigned char handle::axis_ids[6] = +const unsigned char vjoystick::axis_ids[6] = { HID_USAGE_X, HID_USAGE_Y, @@ -49,69 +49,96 @@ static constexpr double val_minmax[6] = 180 }; -bool handle::init() +bool vjoystick::init() { if (!AcquireVJD(OPENTRACK_VJOYSTICK_ID)) return false; unsigned cnt = 0; - bool status = true; for (unsigned i = 0; i < axis_count; i++) { - if (!GetVJDAxisExist(OPENTRACK_VJOYSTICK_ID, axis_ids[i])) - continue; - cnt++; + bool status = true; + + status &= !!GetVJDAxisExist(OPENTRACK_VJOYSTICK_ID, axis_ids[i]); status &= !!GetVJDAxisMin(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_min[i]); status &= !!GetVJDAxisMax(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_max[i]); - } - //(void)ResetVJD(OPENTRACK_VJOYSTICK_ID); - - return status && cnt; -} -handle::handle() -{ - if (!isVJDExists(OPENTRACK_VJOYSTICK_ID)) - joy_state = state::notent; - else if (init()) - joy_state = state::success; - else - joy_state = state::fail; -} + if (!status) + { + axis_min[i] = 0; + axis_max[i] = 0; + } + else + cnt++; + } -handle::~handle() -{ - if (joy_state == state::success) + if (!cnt) + { RelinquishVJD(OPENTRACK_VJOYSTICK_ID); + return false; + } + else + return true; } -bool handle::to_axis_value(unsigned axis_id, double val, int& ret) const +int vjoystick::to_axis_value(unsigned axis_id, double val) const { - if (!axis_min[axis_id] && !axis_max[axis_id]) - return false; - const double minmax = val_minmax[axis_id]; const double min = axis_min[axis_id]; const double max = axis_max[axis_id]; - ret = int(clamp((val+minmax) * max / (2*minmax) - min, min, max)); - return true; + return (int)(clamp((val+minmax) * max / (2*minmax) - min, min, max)); } -vjoystick_proto::vjoystick_proto() = default; -vjoystick_proto::~vjoystick_proto() = default; +vjoystick::vjoystick() = default; +vjoystick::~vjoystick() +{ + if (status) + RelinquishVJD(OPENTRACK_VJOYSTICK_ID); +} -module_status vjoystick_proto::initialize() +module_status vjoystick::initialize() { - h = handle{}; + QString msg; - if (h->get_state() != state::success) + if (!vJoyEnabled()) + msg = tr("vjoystick won't work without the driver installed."); + else if (WORD VerDll, VerDrv; !DriverMatch(&VerDll, &VerDrv)) + msg = tr("driver/SDK version mismatch"); + else + { + int code; + switch (code = GetVJDStatus(OPENTRACK_VJOYSTICK_ID)) + { + case VJD_STAT_OWN: + msg = tr("BUG: handle leak."); + break; + case VJD_STAT_FREE: + break; + case VJD_STAT_BUSY: + msg = tr("Virtual joystick already in use."); + break; + case VJD_STAT_MISS: + msg = tr("Device missing. Add joystick #1."); + break; + case VJD_STAT_UNKN: + msg = tr("Unknown error."); + break; + default: + msg = tr("Unknown error #%1.").arg(code); + break; + } + } + + status = msg.isNull(); + + if (!status) { QMessageBox msgbox; msgbox.setIcon(QMessageBox::Critical); - msgbox.setText(tr("vjoystick driver missing")); - msgbox.setInformativeText(tr("vjoystick won't work without the driver installed.")); + msgbox.setText(tr("vjoystick driver problem")); + msgbox.setInformativeText(msg); QPushButton* driver_button = msgbox.addButton(tr("Download the driver"), QMessageBox::ActionRole); QPushButton* project_site_button = msgbox.addButton(tr("Visit project site"), QMessageBox::ActionRole); @@ -131,30 +158,32 @@ module_status vjoystick_proto::initialize() } } - switch (h->get_state()) - { - default: - case state::notent: - return error(tr("vjoystick not installed or disabled")); - case state::fail: - return error(tr("can't initialize vjoystick")); - case state::success: - return status_ok(); - } + if (!status) + return error(tr("Driver problem.")); + else + return {}; } -void vjoystick_proto::pose(const double *pose) +void vjoystick::pose(const double *pose) { - if (h->get_state() != state::success) + if (first_run) + { + status = init(); + //status &= !!ResetVJD(OPENTRACK_VJOYSTICK_ID); + first_run = false; + } + + if (!status) return; - for (unsigned i = 0; i < handle::axis_count; i++) + for (unsigned i = 0; i < vjoystick::axis_count; i++) { - int val; - if (!h->to_axis_value(i, pose[i], val)) + if (axis_min[i] == axis_max[i]) continue; - SetAxis(val, OPENTRACK_VJOYSTICK_ID, handle::axis_ids[i]); + + int val = to_axis_value(i, pose[i]); + SetAxis(val, OPENTRACK_VJOYSTICK_ID, vjoystick::axis_ids[i]); } } -OPENTRACK_DECLARE_PROTOCOL(vjoystick_proto, vjoystick_dialog, vjoystick_metadata) +OPENTRACK_DECLARE_PROTOCOL(vjoystick, vjoystick_dialog, vjoystick_metadata) diff --git a/proto-vjoystick/vjoystick.h b/proto-vjoystick/vjoystick.h index 96d10e9e..7e119b21 100644 --- a/proto-vjoystick/vjoystick.h +++ b/proto-vjoystick/vjoystick.h @@ -10,45 +10,32 @@ #include "api/plugin-api.hpp" #include "compat/macros.hpp" -#include - -enum class state : int +enum status { - notent = -1, - fail = -2, - success = 1, }; -class handle final +class vjoystick : public TR, public IProtocol { + Q_OBJECT + public: - static constexpr unsigned axis_count = 6; - static const unsigned char axis_ids[axis_count]; + vjoystick(); + ~vjoystick() override; + module_status initialize() override; + void pose( const double *headpose ) override; + QString game_name() override { return tr("Virtual joystick"); } private: - state joy_state = state::notent; long axis_min[6] {}; long axis_max[6] {}; [[nodiscard]] bool init(); -public: - handle(); - ~handle(); - state get_state() const { return joy_state; } - [[nodiscard]] bool to_axis_value(unsigned axis_id, double val, int& ret) const; -}; + int to_axis_value(unsigned axis_id, double val) const; -class vjoystick_proto : public TR, public IProtocol -{ - Q_OBJECT + static constexpr unsigned axis_count = 6; + static const unsigned char axis_ids[axis_count]; - std::optional h; -public: - vjoystick_proto(); - ~vjoystick_proto() override; - module_status initialize() override; - void pose( const double *headpose ) override; - QString game_name() override { return tr("Virtual joystick"); } -private: + bool status = false; + bool first_run = true; }; class vjoystick_dialog final : public IProtocolDialog diff --git a/proto-vjoystick/vjoystick.ui b/proto-vjoystick/vjoystick.ui index 6e6a2b33..5bfdb02a 100644 --- a/proto-vjoystick/vjoystick.ui +++ b/proto-vjoystick/vjoystick.ui @@ -2,9 +2,6 @@ vjoystick - - Qt::NonModal - 0 @@ -14,23 +11,17 @@ - VJoy + vjoystick :/images/vjoystick.png:/images/vjoystick.png - - Qt::LeftToRight - - - false - - - + + - <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a></p></body></html> + <html><head/><body><p>Go to the <a href="http://vjoystick.sourceforge.net/site/"><span style=" text-decoration: underline; color:#0000ff;">vjoystick</span></a> project site or <a href="https://sourceforge.net/projects/vjoystick/files/latest/download"><span style=" text-decoration: underline; color:#0000ff;">download directly</span></a>.</p></body></html> Qt::AlignCenter @@ -40,10 +31,10 @@ - + - QDialogButtonBox::Ok + QDialogButtonBox::Close diff --git a/proto-vjoystick/vjoystick_dialog.cpp b/proto-vjoystick/vjoystick_dialog.cpp index 0a1fa9b0..382d8fb3 100644 --- a/proto-vjoystick/vjoystick_dialog.cpp +++ b/proto-vjoystick/vjoystick_dialog.cpp @@ -7,4 +7,5 @@ vjoystick_dialog::vjoystick_dialog() { ui.setupUi(this); connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &QWidget::close); + connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close); } -- cgit v1.2.3