diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-06-16 12:34:31 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-06-16 12:35:51 +0200 |
commit | 0760fe011114fa440275b487eaf766b015f40e5b (patch) | |
tree | a0141fb291b7dc1e38d16dd0eb2c767ec93b310a /opentrack-logic/win32-joystick.hpp | |
parent | 60460f56cabe0155996adf8ba5e9f6730ef0b203 (diff) |
all: split "api" into "api" and "logic"
Here, the "logic" module has all the stuff for building one's own
graphical user interface.
The "api" module has stuff used by other trackers.
While at it, each of "api", "logic", and "compat" need their own export
headers. This is because of preprocessor symbol clashes.
This is all because a change in the "gui"-only dependency required a
relink of all the trackers, protocols, and flters. It takes too long
when building in the release configuration. With the split, only the
"gui" module gets rebuilt. Since it has close to no static dependencies,
it's fast enough.
Diffstat (limited to 'opentrack-logic/win32-joystick.hpp')
-rw-r--r-- | opentrack-logic/win32-joystick.hpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/opentrack-logic/win32-joystick.hpp b/opentrack-logic/win32-joystick.hpp new file mode 100644 index 00000000..d9b62e45 --- /dev/null +++ b/opentrack-logic/win32-joystick.hpp @@ -0,0 +1,96 @@ +#pragma once + +#ifdef _WIN32 + +#include "export.hpp" + +#include <cstring> +#include <memory> +#include <vector> +#include <functional> +#include <algorithm> +#include <unordered_map> +#ifndef DIRECTINPUT_VERSION +# define DIRECTINPUT_VERSION 0x800 +#endif +#include <dinput.h> +#include <windows.h> +#include "opentrack-compat/timer.hpp" +#include <QString> +#include <QDebug> +#include <QMutex> +#include <QMutexLocker> +#include <QMainWindow> + +namespace std { +template<> +struct hash<QString> +{ + inline std::size_t operator()(const QString& value) const + { + return qHash(value); + } +}; +} + +struct OPENTRACK_LOGIC_EXPORT win32_joy_ctx +{ + using fn = std::function<void(const QString& guid, int btn, bool held)>; + + enum { joy_axis_size = 65535 }; + + struct joy_info + { + QString name, guid; + }; + + void poll(fn f); + bool poll_axis(const QString& guid, int axes[8]); + std::vector<joy_info> get_joy_info(); + + win32_joy_ctx(const win32_joy_ctx&) = delete; + win32_joy_ctx& operator=(const win32_joy_ctx&) = delete; + + win32_joy_ctx(); + ~win32_joy_ctx(); + void refresh(); +private: + QMutex mtx; + QMainWindow fake_main_window; + LPDIRECTINPUT8 di; + + static QString guid_to_string(const GUID guid); + void release(); + + struct joy + { + LPDIRECTINPUTDEVICE8 joy_handle; + QString guid, name; + bool pressed[128]; + Timer first_timer; + DIJOYSTATE2 js_old; + + joy(LPDIRECTINPUTDEVICE8 handle, const QString& guid, const QString& name); + ~joy(); + + void release(); + bool poll(fn f); + }; + + std::unordered_map<QString, std::shared_ptr<joy>> joys; + + class enum_state + { + std::unordered_map<QString, std::shared_ptr<joy>> joys; + QMainWindow& fake_main_window; + LPDIRECTINPUT8 di; + + std::vector<QString> all; + static BOOL CALLBACK EnumJoysticksCallback(const DIDEVICEINSTANCE* pdidInstance, VOID* pContext); + static BOOL CALLBACK EnumObjectsCallback(const DIDEVICEOBJECTINSTANCE* pdidoi, VOID* ctx); + public: + enum_state(std::unordered_map<QString, std::shared_ptr<joy>>& joys, QMainWindow& fake_main_window, LPDIRECTINPUT8 di); + }; +}; + +#endif |