summaryrefslogtreecommitdiffhomepage
path: root/proto-vjoystick/vjoystick.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'proto-vjoystick/vjoystick.cpp')
-rw-r--r--proto-vjoystick/vjoystick.cpp151
1 files changed, 102 insertions, 49 deletions
diff --git a/proto-vjoystick/vjoystick.cpp b/proto-vjoystick/vjoystick.cpp
index 557b5e99..292e8259 100644
--- a/proto-vjoystick/vjoystick.cpp
+++ b/proto-vjoystick/vjoystick.cpp
@@ -7,7 +7,7 @@
*/
#include "vjoystick.h"
#include "api/plugin-api.hpp"
-#include "compat/util.hpp"
+#include "compat/math.hpp"
#include <cstring>
#include <QDebug>
@@ -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,
@@ -39,71 +39,112 @@ const unsigned char handle::axis_ids[6] =
// HID_USAGE_WHL,
};
-constexpr double handle::val_minmax[6];
+static constexpr double val_minmax[6] =
+{
+ 50,
+ 50,
+ 50,
+ 180,
+ 180,
+ 180
+};
-void handle::init()
+bool vjoystick::init()
{
+ if (!AcquireVJD(OPENTRACK_VJOYSTICK_ID))
+ return false;
+
+ unsigned cnt = 0;
+
for (unsigned i = 0; i < axis_count; i++)
{
- if (!GetVJDAxisExist(OPENTRACK_VJOYSTICK_ID, axis_ids[i]))
+ 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]);
+
+ if (!status)
{
- // avoid floating point division by zero
axis_min[i] = 0;
- axis_max[i] = 1;
- continue;
+ axis_max[i] = 0;
}
- GetVJDAxisMin(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_min[i]);
- GetVJDAxisMax(OPENTRACK_VJOYSTICK_ID, axis_ids[i], &axis_max[i]);
- }
- (void) ResetVJD(OPENTRACK_VJOYSTICK_ID);
-}
-
-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;
+ cnt++;
}
- else
- {
- joy_state = state_success;
- init();
- }
-}
-handle::~handle()
-{
- if (joy_state == state_success)
+ if (!cnt)
{
- (void) RelinquishVJD(OPENTRACK_VJOYSTICK_ID);
- joy_state = state_fail;
+ RelinquishVJD(OPENTRACK_VJOYSTICK_ID);
+ return false;
}
+ else
+ return true;
}
-LONG handle::to_axis_value(unsigned axis_id, double val)
+int vjoystick::to_axis_value(unsigned axis_id, double val) const
{
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));
+ return (int)(std::clamp((val+minmax) * max / (2*minmax) - min, min, max));
}
-vjoystick_proto::vjoystick_proto()
+vjoystick::vjoystick() = default;
+vjoystick::~vjoystick()
{
- if (h.get_state() != state_success)
+ if (status)
+ RelinquishVJD(OPENTRACK_VJOYSTICK_ID);
+}
+
+module_status vjoystick::initialize()
+{
+ QString msg;
+
+ if (!vJoyEnabled())
+ msg = tr("vjoystick won't work without the driver installed.");
+#if 0
+ else if (WORD VerDll, VerDrv; !DriverMatch(&VerDll, &VerDrv))
+ msg = tr("driver/SDK version mismatch (dll 0x%1, driver 0x%2)")
+ .arg(QString::number(VerDll, 16), QString::number(VerDrv, 16));
+#endif
+ else
+ {
+ int code;
+ switch (code = GetVJDStatus(OPENTRACK_VJOYSTICK_ID))
+ {
+ case VJD_STAT_OWN:
+ msg = tr("BUG: handle leak.");
+ 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;
+ case VJD_STAT_FREE:
+ // we're good
+ status = true;
+ break;
+ }
+ }
+
+ if (!status)
{
QMessageBox msgbox;
msgbox.setIcon(QMessageBox::Critical);
- msgbox.setText(QCoreApplication::translate("vjoystick_proto", "vjoystick driver missing"));
- msgbox.setInformativeText(QCoreApplication::translate("vjoystick_proto", "vjoystick won't work without the driver installed."));
+ msgbox.setText(tr("vjoystick driver problem"));
+ msgbox.setInformativeText(msg);
- QPushButton* driver_button = msgbox.addButton(QCoreApplication::translate("vjoystick_proto", "Download the driver"), QMessageBox::ActionRole);
- QPushButton* project_site_button = msgbox.addButton(QCoreApplication::translate("vjoystick_proto", "Visit project site"), QMessageBox::ActionRole);
+ QPushButton* driver_button = msgbox.addButton(tr("Download the driver"), QMessageBox::ActionRole);
+ QPushButton* project_site_button = msgbox.addButton(tr("Visit project site"), QMessageBox::ActionRole);
msgbox.addButton(QMessageBox::Close);
(void) msgbox.exec();
@@ -119,21 +160,33 @@ vjoystick_proto::vjoystick_proto()
QDesktopServices::openUrl(QUrl(project_site_url, QUrl::StrictMode));
}
}
-}
-vjoystick_proto::~vjoystick_proto()
-{
+ if (!status)
+ return error(tr("Driver problem."));
+ else
+ return {};
}
-void vjoystick_proto::pose(const double *pose)
+void vjoystick::pose(const double *pose, const double*)
{
- 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++)
{
- SetAxis(h.to_axis_value(i, pose[i]), OPENTRACK_VJOYSTICK_ID, handle::axis_ids[i]);
+ if (axis_min[i] == axis_max[i])
+ continue;
+
+ 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)