From 0739d5b595be9492c1e574192eba12174111e52c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 30 Oct 2015 09:16:32 +0100 Subject: also rename protocol -> proto --- proto-libevdev/CMakeLists.txt | 10 ++ proto-libevdev/ftnoir_libevdev_controls.ui | 111 +++++++++++++++++++++ proto-libevdev/ftnoir_protocol_libevdev.cpp | 97 ++++++++++++++++++ proto-libevdev/ftnoir_protocol_libevdev.h | 57 +++++++++++ proto-libevdev/ftnoir_protocol_libevdev_dialog.cpp | 21 ++++ proto-libevdev/images/linux.png | Bin 0 -> 668 bytes proto-libevdev/libevdev-protocol.qrc | 5 + 7 files changed, 301 insertions(+) create mode 100644 proto-libevdev/CMakeLists.txt create mode 100644 proto-libevdev/ftnoir_libevdev_controls.ui create mode 100644 proto-libevdev/ftnoir_protocol_libevdev.cpp create mode 100644 proto-libevdev/ftnoir_protocol_libevdev.h create mode 100644 proto-libevdev/ftnoir_protocol_libevdev_dialog.cpp create mode 100644 proto-libevdev/images/linux.png create mode 100644 proto-libevdev/libevdev-protocol.qrc (limited to 'proto-libevdev') diff --git a/proto-libevdev/CMakeLists.txt b/proto-libevdev/CMakeLists.txt new file mode 100644 index 00000000..960a1271 --- /dev/null +++ b/proto-libevdev/CMakeLists.txt @@ -0,0 +1,10 @@ +if(LINUX OR APPLE) + set(SDK_ENABLE_LIBEVDEV FALSE CACHE BOOL "libevdev virtual joystick protocol support") + if(SDK_ENABLE_LIBEVDEV) + include(FindPkgConfig) + opentrack_boilerplate(opentrack-proto-libevdev) + pkg_check_modules(libevdev REQUIRED QUIET libevdev) + target_link_libraries(opentrack-proto-libevdev ${libevdev_LIBRARIES}) + include_directories(opentrack-proto-libevdev SYSTEM PUBLIC ${libevdev_INCLUDE_DIRS}) + endif() +endif() diff --git a/proto-libevdev/ftnoir_libevdev_controls.ui b/proto-libevdev/ftnoir_libevdev_controls.ui new file mode 100644 index 00000000..d2b86445 --- /dev/null +++ b/proto-libevdev/ftnoir_libevdev_controls.ui @@ -0,0 +1,111 @@ + + + UICLibevdevControls + + + Qt::NonModal + + + + 0 + 0 + 228 + 69 + + + + VJoy + + + + :/images/vjoy.png:/images/vjoy.png + + + Qt::LeftToRight + + + false + + + + + + Make sure rw for /dev/input/uinput! + + + + + + + + + QLayout::SetDefaultConstraint + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 100 + 16777215 + + + + OK + + + + + + + + 0 + 0 + + + + + 100 + 0 + + + + + 100 + 16777215 + + + + Cancel + + + + + + + + + + + btnOK + btnCancel + + + + + startEngineClicked() + stopEngineClicked() + cameraSettingsClicked() + + diff --git a/proto-libevdev/ftnoir_protocol_libevdev.cpp b/proto-libevdev/ftnoir_protocol_libevdev.cpp new file mode 100644 index 00000000..96805b39 --- /dev/null +++ b/proto-libevdev/ftnoir_protocol_libevdev.cpp @@ -0,0 +1,97 @@ +#include "ftnoir_protocol_libevdev.h" +#include "opentrack/plugin-api.hpp" +#include +#include + +#include +#include + +#define CHECK_LIBEVDEV(expr) if ((error = (expr)) != 0) goto error; + +static const int max_input = 65535; +static const int mid_input = 32767; +static const int min_input = 0; + +FTNoIR_Protocol::FTNoIR_Protocol() : dev(NULL), uidev(NULL) +{ + int error = 0; + + dev = libevdev_new(); + + if (!dev) + goto error; + + CHECK_LIBEVDEV(libevdev_enable_property(dev, INPUT_PROP_BUTTONPAD)); + + libevdev_set_name(dev, "opentrack headpose"); + + struct input_absinfo absinfo; + + absinfo.minimum = min_input; + absinfo.maximum = max_input; + absinfo.resolution = 1; + absinfo.value = mid_input; + absinfo.flat = 1; + absinfo.fuzz = 0; + + CHECK_LIBEVDEV(libevdev_enable_event_type(dev, EV_ABS)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_X, &absinfo)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_Y, &absinfo)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_Z, &absinfo)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_RX, &absinfo)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_RY, &absinfo)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_ABS, ABS_RZ, &absinfo)); + + /* do not remove next 3 lines or udev scripts won't assign 0664 permissions -sh */ + CHECK_LIBEVDEV(libevdev_enable_event_type(dev, EV_KEY)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_KEY, BTN_JOYSTICK, NULL)); + CHECK_LIBEVDEV(libevdev_enable_event_code(dev, EV_KEY, BTN_TRIGGER, NULL)); + + CHECK_LIBEVDEV(libevdev_uinput_create_from_device(dev, LIBEVDEV_UINPUT_OPEN_MANAGED, &uidev)); + + return; +error: + if (uidev) + libevdev_uinput_destroy(uidev); + if (dev) + libevdev_free(dev); + if (error) + fprintf(stderr, "libevdev error: %d\n", error); + uidev = NULL; + dev = NULL; +} + +FTNoIR_Protocol::~FTNoIR_Protocol() +{ + if (uidev) + libevdev_uinput_destroy(uidev); + if (dev) + libevdev_free(dev); +} + +void FTNoIR_Protocol::pose(const double* headpose) { + static const int axes[] = { + /* translation goes first */ + ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ + }; + + static const int max_value[] = { + 100, + 100, + 100, + 180, + 90, + 180 + }; + + for (int i = 0; i < 6; i++) + { + int value = headpose[i] * mid_input / max_value[i] + mid_input; + int normalized = std::max(std::min(max_input, value), min_input); + (void) libevdev_uinput_write_event(uidev, EV_ABS, axes[i], normalized); + } + + (void) libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0); +} + +OPENTRACK_DECLARE_PROTOCOL(FTNoIR_Protocol, LibevdevControls, FTNoIR_ProtocolDll) diff --git a/proto-libevdev/ftnoir_protocol_libevdev.h b/proto-libevdev/ftnoir_protocol_libevdev.h new file mode 100644 index 00000000..ecb3b201 --- /dev/null +++ b/proto-libevdev/ftnoir_protocol_libevdev.h @@ -0,0 +1,57 @@ +/* Copyright (c) 2013 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 + * copyright notice and this permission notice appear in all copies. + */ +#pragma once +#include "ui_ftnoir_libevdev_controls.h" + +#include +#include "opentrack/plugin-api.hpp" + +extern "C" { +# include +# include +} + +class FTNoIR_Protocol : public IProtocol +{ +public: + FTNoIR_Protocol(); + ~FTNoIR_Protocol() override; + bool correct() { + return dev != NULL; + } + void pose(const double *headpose); + QString game_name() { + return "Virtual joystick for Linux"; + } +private: + struct libevdev* dev; + struct libevdev_uinput* uidev; +}; + +class LibevdevControls: public IProtocolDialog +{ + Q_OBJECT +public: + LibevdevControls(); + void register_protocol(IProtocol *) {} + void unregister_protocol() {} + +private: + Ui::UICLibevdevControls ui; + void save(); + +private slots: + void doOK(); + void doCancel(); +}; + +class FTNoIR_ProtocolDll : public Metadata +{ +public: + QString name() { return QString("libevdev joystick receiver"); } + QIcon icon() { return QIcon(":/images/linux.png"); } +}; diff --git a/proto-libevdev/ftnoir_protocol_libevdev_dialog.cpp b/proto-libevdev/ftnoir_protocol_libevdev_dialog.cpp new file mode 100644 index 00000000..70495a8f --- /dev/null +++ b/proto-libevdev/ftnoir_protocol_libevdev_dialog.cpp @@ -0,0 +1,21 @@ +#include "ftnoir_protocol_libevdev.h" +#include "opentrack/plugin-api.hpp" + +LibevdevControls::LibevdevControls() +{ + ui.setupUi( this ); + connect(ui.btnOK, SIGNAL(clicked()), this, SLOT(doOK())); + connect(ui.btnCancel, SIGNAL(clicked()), this, SLOT(doCancel())); +} + +void LibevdevControls::doOK() { + save(); + this->close(); +} + +void LibevdevControls::doCancel() { + this->close(); +} + +void LibevdevControls::save() { +} diff --git a/proto-libevdev/images/linux.png b/proto-libevdev/images/linux.png new file mode 100644 index 00000000..8836c0e2 Binary files /dev/null and b/proto-libevdev/images/linux.png differ diff --git a/proto-libevdev/libevdev-protocol.qrc b/proto-libevdev/libevdev-protocol.qrc new file mode 100644 index 00000000..70bb415f --- /dev/null +++ b/proto-libevdev/libevdev-protocol.qrc @@ -0,0 +1,5 @@ + + + images/linux.png + + -- cgit v1.2.3