summaryrefslogtreecommitdiffhomepage
path: root/proto-iokit-foohid/iokitprotocol.cpp
diff options
context:
space:
mode:
authorEike Ziller <git@eikeziller.de>2017-11-19 21:18:26 +0100
committerEike Ziller <git@eikeziller.de>2017-11-23 20:23:28 +0100
commit7bacbdf10b97040c9ed282a22485759bd082fa63 (patch)
tree2a02884f633ddbc7ede387a5bf2aa8d7b0396e5f /proto-iokit-foohid/iokitprotocol.cpp
parentf548847ea43006b2dc540e84d11d3c3662a94148 (diff)
Add virtual joystick output for macOS
Based of the foohid IOKit driver for implementing virtual HID devices. Issue: #236
Diffstat (limited to 'proto-iokit-foohid/iokitprotocol.cpp')
-rw-r--r--proto-iokit-foohid/iokitprotocol.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/proto-iokit-foohid/iokitprotocol.cpp b/proto-iokit-foohid/iokitprotocol.cpp
new file mode 100644
index 00000000..4af6ad5b
--- /dev/null
+++ b/proto-iokit-foohid/iokitprotocol.cpp
@@ -0,0 +1,51 @@
+/* Copyright (c) 2017, Eike Ziller *
+ * *
+ * Permission to use, copy, modify, and/or distribute this software for any *
+ * purpose with or without fee is hereby granted, provided that the above *
+ * copyright notice and this permission notice appear in all copies. *
+ */
+
+#include "iokitprotocol.h"
+
+#include "foohidjoystick.h"
+#include "iokitprotocoldialog.h"
+
+#include <QDebug>
+
+IOKitProtocol::IOKitProtocol()
+{
+ joystick = std::make_unique<FooHIDJoystick>("OpenTrack Virtual Joystick",
+ "SN 983457");
+ if (joystick->hasError())
+ qWarning("%s\n", qPrintable(joystick->errorMessage()));
+}
+
+bool IOKitProtocol::correct()
+{
+ return joystick && !joystick->hasError();
+}
+
+static uint8_t valueToStick(FooHIDJoystick *stick, double min, double value, double max)
+{
+ return uint8_t(qBound(stick->minValue(),
+ int(round((value - min) * stick->range() / (max - min) - stick->minValue())),
+ stick->minValue() + stick->range()));
+}
+
+void IOKitProtocol::pose(const double *headpose)
+{
+ const uint8_t x = valueToStick(joystick.get(), -75., headpose[0], +75.);
+ const uint8_t y = valueToStick(joystick.get(), -75., headpose[1], +75.);
+ const uint8_t z = valueToStick(joystick.get(), -75., headpose[2], +75.);
+ const uint8_t rx = valueToStick(joystick.get(), -180., headpose[3], +180.);
+ const uint8_t ry = valueToStick(joystick.get(), -180., headpose[4], +180.);
+ const uint8_t rz = valueToStick(joystick.get(), -180., headpose[5], +180.);
+ joystick->setValue({x, y, z, rx, ry, rz});
+}
+
+QString IOKitProtocol::game_name()
+{
+ return QString();
+}
+
+OPENTRACK_DECLARE_PROTOCOL(IOKitProtocol, IOKitProtocolDialog, IOKitProtocolMetadata)