From bdbab6bbfef596011302b595cab9b09aec147c55 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 16 Jan 2019 06:21:48 +0100 Subject: proto/mouse: add legacy input method --- proto-mouse/ftnoir_mousecontrols.ui | 201 ++++++++++++--------------- proto-mouse/ftnoir_protocol_mouse.cpp | 49 ++++--- proto-mouse/ftnoir_protocol_mouse.h | 10 +- proto-mouse/ftnoir_protocol_mouse_dialog.cpp | 19 ++- proto-mouse/lang/nl_NL.ts | 30 ++-- proto-mouse/lang/ru_RU.ts | 30 ++-- proto-mouse/lang/stub.ts | 30 ++-- proto-mouse/lang/zh_CN.ts | 30 ++-- proto-mouse/mouse-settings.hpp | 22 +-- proto-vjoystick/lang/nl_NL.ts | 4 - proto-vjoystick/lang/ru_RU.ts | 4 - proto-vjoystick/lang/stub.ts | 4 - proto-vjoystick/lang/zh_CN.ts | 4 - proto-vjoystick/vjoystick.cpp | 78 ++++++----- proto-vjoystick/vjoystick.h | 25 ++-- 15 files changed, 271 insertions(+), 269 deletions(-) diff --git a/proto-mouse/ftnoir_mousecontrols.ui b/proto-mouse/ftnoir_mousecontrols.ui index f970f887..45b33470 100644 --- a/proto-mouse/ftnoir_mousecontrols.ui +++ b/proto-mouse/ftnoir_mousecontrols.ui @@ -1,7 +1,7 @@ - UICMOUSEControls - + UI_mouse + Qt::NonModal @@ -9,70 +9,42 @@ 0 0 - 413 - 155 + 360 + 146 - 413 + 360 0 - Mouse protocol settings + Mouse protocol :/images/mouse.png:/images/mouse.png - - Qt::LeftToRight - - - false - - + + + 9 + + + 11 + + + 6 + - - - 0 - 0 - - - - Map mouse X to: - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - false - - - - - - - - 0 - 0 - - - Map mouse Y to: - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - false + X axis - - + + 0 @@ -81,16 +53,10 @@ - 80 + 105 16777215 - - Select Number - - - QComboBox::InsertAlphabetically - None @@ -128,8 +94,38 @@ - - + + + + Sensitivity + + + + + + + + 0 + 0 + + + + Qt::Horizontal + + + 25 + + + + + + + Y axis + + + + + 0 @@ -138,16 +134,10 @@ - 80 + 105 16777215 - - Select Number - - - QComboBox::InsertAlphabetically - None @@ -185,36 +175,17 @@ - - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - - - 0 - 0 - - + + - X axis sensitivity - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - false + Sensitivity - - + + - + 0 0 @@ -227,38 +198,49 @@ - - + + + + Method + + + + + - + 0 0 - - Y axis sensitivity - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - false + + + 105 + 16777215 + + + + Direct input + + + + + Legacy + + - - + + - + 0 0 - - Qt::Horizontal - - - 25 + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok @@ -268,9 +250,4 @@ - - startEngineClicked() - stopEngineClicked() - cameraSettingsClicked() - diff --git a/proto-mouse/ftnoir_protocol_mouse.cpp b/proto-mouse/ftnoir_protocol_mouse.cpp index 60c04cbe..2b3af2f0 100644 --- a/proto-mouse/ftnoir_protocol_mouse.cpp +++ b/proto-mouse/ftnoir_protocol_mouse.cpp @@ -9,14 +9,11 @@ #include "api/plugin-api.hpp" #include "compat/math.hpp" + #include #include #include -#ifndef MOUSEEVENTF_MOVE_NOCOALESCE -# define MOUSEEVENTF_MOVE_NOCOALESCE 0x2000 -#endif - static const double invert[] = { 1., 1., 1., 1., -1., 1. @@ -24,8 +21,8 @@ static const double invert[] = { void mouse::pose(const double* headpose) { - const int axis_x = s.Mouse_X - 1; - const int axis_y = s.Mouse_Y - 1; + const int axis_x = s.mouse_x - 1; + const int axis_y = s.mouse_y - 1; int mouse_x = 0, mouse_y = 0; @@ -42,18 +39,38 @@ void mouse::pose(const double* headpose) const int dx = get_delta(mouse_x, last_x), dy = get_delta(mouse_y, last_y); + last_x = mouse_x; last_y = mouse_y; + if (dx || dy) { - INPUT input; - input.type = INPUT_MOUSE; - MOUSEINPUT& mi = input.mi; - mi = {}; - mi.dx = dx; - mi.dy = dy; - mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_MOVE_NOCOALESCE; - - (void)SendInput(1, &input, sizeof(input)); - last_x = mouse_x; last_y = mouse_y; + switch (s.input_method) + { + default: + eval_once(qDebug() << "proto/mouse: invalid input method"); + [[fallthrough]]; + case input_direct: + { + INPUT input; + input.type = INPUT_MOUSE; + MOUSEINPUT& mi = input.mi; + mi = {}; + mi.dx = dx; + mi.dy = dy; + mi.dwFlags = MOUSEEVENTF_MOVE; + + (void)SendInput(1, &input, sizeof(input)); + + break; + } + case input_legacy: + { + POINT pt{}; + (void)GetCursorPos(&pt); + (void)SetCursorPos(pt.x + dx, pt.y + dy); + + break; + } + } } } diff --git a/proto-mouse/ftnoir_protocol_mouse.h b/proto-mouse/ftnoir_protocol_mouse.h index c8709604..b7cf61cf 100644 --- a/proto-mouse/ftnoir_protocol_mouse.h +++ b/proto-mouse/ftnoir_protocol_mouse.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2015 Stanislaw Halik +/* Copyright (c) 2015, 2019 Stanislaw Halik * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -10,10 +10,10 @@ #include "ui_ftnoir_mousecontrols.h" #include "mouse-settings.hpp" -#include "compat/tr.hpp" +#include "api/plugin-api.hpp" #include -#include "api/plugin-api.hpp" + using namespace options; class mouse : public TR, public IProtocol @@ -37,7 +37,7 @@ class MOUSEControls: public IProtocolDialog { Q_OBJECT - Ui::UICMOUSEControls ui; + Ui::UI_mouse ui; mouse_settings s; private slots: @@ -46,7 +46,7 @@ private slots: public: MOUSEControls(); - void register_protocol(IProtocol *) override {} + void register_protocol(IProtocol*) override {} void unregister_protocol() override {} }; diff --git a/proto-mouse/ftnoir_protocol_mouse_dialog.cpp b/proto-mouse/ftnoir_protocol_mouse_dialog.cpp index 77b1ff2e..5646718c 100644 --- a/proto-mouse/ftnoir_protocol_mouse_dialog.cpp +++ b/proto-mouse/ftnoir_protocol_mouse_dialog.cpp @@ -3,19 +3,25 @@ MOUSEControls::MOUSEControls() { - ui.setupUi( this ); + ui.setupUi(this); - connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK())); - connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel())); + connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &MOUSEControls::doOK); + connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &MOUSEControls::doCancel); - tie_setting(s.Mouse_X, ui.cbxSelectMouse_X); - tie_setting(s.Mouse_Y, ui.cbxSelectMouse_Y); + tie_setting(s.mouse_x, ui.axis_x); + tie_setting(s.mouse_y, ui.axis_y); tie_setting(s.sensitivity_x, ui.sensitivity_x); tie_setting(s.sensitivity_y, ui.sensitivity_y); + + const int data[] = { input_direct, input_legacy }; + for (unsigned k = 0; k < std::size(data); k++) + ui.input_method->setItemData(k, data[k]); + tie_setting(s.input_method, ui.input_method); } -void MOUSEControls::doOK() { +void MOUSEControls::doOK() +{ s.b->save(); close(); } @@ -24,4 +30,3 @@ void MOUSEControls::doCancel() { close(); } - diff --git a/proto-mouse/lang/nl_NL.ts b/proto-mouse/lang/nl_NL.ts index 8994756b..13a75520 100644 --- a/proto-mouse/lang/nl_NL.ts +++ b/proto-mouse/lang/nl_NL.ts @@ -2,21 +2,13 @@ - UICMOUSEControls + UI_mouse - Mouse protocol settings + Mouse protocol - Map mouse X to: - - - - Map mouse Y to: - - - - Select Number + X axis @@ -48,11 +40,23 @@ - X axis sensitivity + Y axis + + + + Sensitivity + + + + Method + + + + Direct input - Y axis sensitivity + Legacy diff --git a/proto-mouse/lang/ru_RU.ts b/proto-mouse/lang/ru_RU.ts index ab5e650c..bdadb6bd 100644 --- a/proto-mouse/lang/ru_RU.ts +++ b/proto-mouse/lang/ru_RU.ts @@ -2,21 +2,13 @@ - UICMOUSEControls + UI_mouse - Mouse protocol settings + Mouse protocol - Map mouse X to: - - - - Map mouse Y to: - - - - Select Number + X axis @@ -48,11 +40,23 @@ - X axis sensitivity + Y axis + + + + Sensitivity + + + + Method + + + + Direct input - Y axis sensitivity + Legacy diff --git a/proto-mouse/lang/stub.ts b/proto-mouse/lang/stub.ts index 1bbd5991..2a811df1 100644 --- a/proto-mouse/lang/stub.ts +++ b/proto-mouse/lang/stub.ts @@ -2,21 +2,13 @@ - UICMOUSEControls + UI_mouse - Mouse protocol settings + Mouse protocol - Map mouse X to: - - - - Map mouse Y to: - - - - Select Number + X axis @@ -48,11 +40,23 @@ - X axis sensitivity + Y axis + + + + Sensitivity + + + + Method + + + + Direct input - Y axis sensitivity + Legacy diff --git a/proto-mouse/lang/zh_CN.ts b/proto-mouse/lang/zh_CN.ts index 1bbd5991..2a811df1 100644 --- a/proto-mouse/lang/zh_CN.ts +++ b/proto-mouse/lang/zh_CN.ts @@ -2,21 +2,13 @@ - UICMOUSEControls + UI_mouse - Mouse protocol settings + Mouse protocol - Map mouse X to: - - - - Map mouse Y to: - - - - Select Number + X axis @@ -48,11 +40,23 @@ - X axis sensitivity + Y axis + + + + Sensitivity + + + + Method + + + + Direct input - Y axis sensitivity + Legacy diff --git a/proto-mouse/mouse-settings.hpp b/proto-mouse/mouse-settings.hpp index c485e534..fda06166 100644 --- a/proto-mouse/mouse-settings.hpp +++ b/proto-mouse/mouse-settings.hpp @@ -2,20 +2,22 @@ #include "options/options.hpp" +enum input_method { + input_direct = 0, input_legacy = 1, +}; + namespace mouse_impl { using namespace options; -struct mouse_settings : opts { - value Mouse_X, Mouse_Y; - value sensitivity_x, sensitivity_y; - mouse_settings() : - opts("mouse-proto"), - Mouse_X(b, "mouse-x", 0), - Mouse_Y(b, "mouse-y", 0), - sensitivity_x(b, "mouse-sensitivity-x", { 200, 25, 500 }), - sensitivity_y(b, "mouse-sensitivity-y", { 200, 25, 500 }) - {} +struct mouse_settings : opts +{ + value mouse_x { b, "mouse-x", 0 }, mouse_y { b, "mouse-y", 0 }; + value sensitivity_x { b, "mouse-sensitivity-x", { 200, 25, 500 } }; + value sensitivity_y { b, "mouse-sensitivity-y", { 200, 25, 500 } }; + value input_method { b, "input-method", input_direct }; + + mouse_settings() : opts("mouse-proto") {} }; } // ns mouse_impl diff --git a/proto-vjoystick/lang/nl_NL.ts b/proto-vjoystick/lang/nl_NL.ts index 46d0398d..027e5f39 100644 --- a/proto-vjoystick/lang/nl_NL.ts +++ b/proto-vjoystick/lang/nl_NL.ts @@ -45,10 +45,6 @@ can't initialize vjoystick - - unknown error - - Virtual joystick diff --git a/proto-vjoystick/lang/ru_RU.ts b/proto-vjoystick/lang/ru_RU.ts index 6a618904..583144ef 100644 --- a/proto-vjoystick/lang/ru_RU.ts +++ b/proto-vjoystick/lang/ru_RU.ts @@ -45,10 +45,6 @@ can't initialize vjoystick - - unknown error - - Virtual joystick diff --git a/proto-vjoystick/lang/stub.ts b/proto-vjoystick/lang/stub.ts index c8909cfe..a71e2882 100644 --- a/proto-vjoystick/lang/stub.ts +++ b/proto-vjoystick/lang/stub.ts @@ -45,10 +45,6 @@ can't initialize vjoystick - - unknown error - - Virtual joystick diff --git a/proto-vjoystick/lang/zh_CN.ts b/proto-vjoystick/lang/zh_CN.ts index c8909cfe..a71e2882 100644 --- a/proto-vjoystick/lang/zh_CN.ts +++ b/proto-vjoystick/lang/zh_CN.ts @@ -45,10 +45,6 @@ can't initialize vjoystick - - unknown error - - Virtual joystick diff --git a/proto-vjoystick/vjoystick.cpp b/proto-vjoystick/vjoystick.cpp index bb0f0f51..d781d8e1 100644 --- a/proto-vjoystick/vjoystick.cpp +++ b/proto-vjoystick/vjoystick.cpp @@ -39,7 +39,7 @@ const unsigned char handle::axis_ids[6] = // HID_USAGE_WHL, }; -static const double val_minmax[6] = +static constexpr double val_minmax[6] = { 50, 50, @@ -49,56 +49,54 @@ static const double val_minmax[6] = 180 }; -void handle::init() +bool handle::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])) - { - // avoid floating point division by zero - axis_min[i] = 0; - axis_max[i] = 1; continue; - } - GetVJDAxisMin(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_min[i]); - GetVJDAxisMax(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_max[i]); + cnt++; + 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); + //(void)ResetVJD(OPENTRACK_VJOYSTICK_ID); + + return status && cnt; } handle::handle() { - const bool ret = AcquireVJD(OPENTRACK_VJOYSTICK_ID); - if (!ret) - { - if (!isVJDExists(OPENTRACK_VJOYSTICK_ID)) - joy_state = state_notent; - else - joy_state = state_fail; - } + if (!isVJDExists(OPENTRACK_VJOYSTICK_ID)) + joy_state = state::notent; + else if (init()) + joy_state = state::success; else - { - joy_state = state_success; - init(); - } + joy_state = state::fail; } handle::~handle() { - if (joy_state == state_success) - { - (void) RelinquishVJD(OPENTRACK_VJOYSTICK_ID); - joy_state = state_fail; - } + if (joy_state == state::success) + RelinquishVJD(OPENTRACK_VJOYSTICK_ID); } -LONG handle::to_axis_value(unsigned axis_id, double val) +bool handle::to_axis_value(unsigned axis_id, double val, int& ret) 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]; - return LONG(clamp((val+minmax) * max / (2*minmax) - min, min, max)); + ret = int(clamp((val+minmax) * max / (2*minmax) - min, min, max)); + return true; } vjoystick_proto::vjoystick_proto() = default; @@ -106,7 +104,9 @@ vjoystick_proto::~vjoystick_proto() = default; module_status vjoystick_proto::initialize() { - if (h.get_state() != state_success) + h = handle{}; + + if (h->get_state() != state::success) { QMessageBox msgbox; msgbox.setIcon(QMessageBox::Critical); @@ -131,27 +131,29 @@ module_status vjoystick_proto::initialize() } } - switch (h.get_state()) + switch (h->get_state()) { - case state_notent: + default: + case state::notent: return error(tr("vjoystick not installed or disabled")); - case state_fail: + case state::fail: return error(tr("can't initialize vjoystick")); - case state_success: + case state::success: return status_ok(); - default: - return error(tr("unknown error")); } } void vjoystick_proto::pose(const double *pose) { - if (h.get_state() != state_success) + if (h->get_state() != state::success) return; for (unsigned i = 0; i < handle::axis_count; i++) { - SetAxis(h.to_axis_value(i, pose[i]), OPENTRACK_VJOYSTICK_ID, handle::axis_ids[i]); + int val; + if (!h->to_axis_value(i, pose[i], val)) + continue; + SetAxis(val, OPENTRACK_VJOYSTICK_ID, handle::axis_ids[i]); } } diff --git a/proto-vjoystick/vjoystick.h b/proto-vjoystick/vjoystick.h index 0de86638..96d10e9e 100644 --- a/proto-vjoystick/vjoystick.h +++ b/proto-vjoystick/vjoystick.h @@ -10,13 +10,13 @@ #include "api/plugin-api.hpp" #include "compat/macros.hpp" -#include +#include -enum state : signed char +enum class state : int { - state_notent = -1, - state_fail = -2, - state_success = 1, + notent = -1, + fail = -2, + success = 1, }; class handle final @@ -26,23 +26,22 @@ public: static const unsigned char axis_ids[axis_count]; private: - state joy_state; - LONG axis_min[6] {}; - LONG axis_max[6] {}; - - void init(); + state joy_state = state::notent; + long axis_min[6] {}; + long axis_max[6] {}; + [[nodiscard]] bool init(); public: handle(); ~handle(); - state get_state() { return joy_state; } - LONG to_axis_value(unsigned axis_id, double val); + state get_state() const { return joy_state; } + [[nodiscard]] bool to_axis_value(unsigned axis_id, double val, int& ret) const; }; class vjoystick_proto : public TR, public IProtocol { Q_OBJECT - handle h; + std::optional h; public: vjoystick_proto(); ~vjoystick_proto() override; -- cgit v1.2.3