forked from AetherCollective/ProconXInput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.hpp
145 lines (126 loc) · 3.25 KB
/
Controller.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#pragma once
#include <memory>
#include <optional>
#include <stdexcept>
#include <chrono>
#include <thread>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#include <Xinput.h>
#include "Common.hpp"
#include "hidapi.h"
namespace Procon {
constexpr size_t exchangeLen{ 0x400 };
struct AxisRange {
uchar min;
uchar max;
};
struct StickRange {
AxisRange x;
AxisRange y;
};
struct StickPoint {
uchar x;
uchar y;
};
struct CalibrationData {
StickRange left;
StickRange right;
StickPoint leftCenter;
StickPoint rightCenter;
};
void SetDefaultCalibration(CalibrationData &dat);
struct HIDCloser {
void operator()(hid_device *ptr);
};
struct ExpandedPadState {
XINPUT_GAMEPAD xinState;
StickPoint leftStick;
StickPoint rightStick;
bool sharePressed;
};
void zeroPadState(ExpandedPadState &state);
// Switch Procon class.
// Create, then call openDevice(hid_device_info) to initialize.
// Call pollInput() to send input to ViGEm, such as in a main loop.
// Cleanup is automatic when the object is destroyed.
// Throws Procon::Controller exceptions from openDevice.
class Controller {
bool _connected;
std::unique_ptr<hid_device, HIDCloser> device;
uchar rumbleCounter{ 0 };
using clock = std::chrono::steady_clock;
clock::time_point lastStatus{ clock::now() };
uchar port{ 0 };
ExpandedPadState padStatus{};
CalibrationData calib;
public:
Controller(uchar port);
Controller(Controller &&);
Controller(const Controller&) = delete;
Controller& operator=(const Controller&) = delete;
Controller& operator=(Controller &&);
~Controller();
void openDevice(hid_device_info *dev);
void pollInput();
bool connected() const;
uchar getPort() const;
const ExpandedPadState& getState() const;
void setCalibrationCenter(const StickPoint &left, const StickPoint &right);
private:
void updateStatus();
using exchangeArray = std::optional<std::array<uchar, exchangeLen>>;
template<size_t len>
exchangeArray exchange(std::array<uchar, len> const &data) {
if (!device) return {};
if (hid_write(device.get(), data.data(), len) < 0) {
return {};
}
std::array<uchar, exchangeLen> ret;
ret.fill(0);
hid_read(device.get(), ret.data(), exchangeLen);
return ret;
}
template<size_t len>
exchangeArray sendCommand(uchar command, std::array<uchar, len> const &data) {
std::array<uchar, len + 0x9> buf;
buf.fill(0);
buf[0x0] = 0x80;
buf[0x1] = 0x92;
buf[0x3] = 0x31;
buf[0x8] = command;
if (len > 0) {
memcpy(buf.data() + 0x9, data.data(), len);
}
return exchange(buf);
}
template<size_t len>
exchangeArray sendSubcommand(uchar command, uchar subcommand, std::array<uchar, len> const& data) {
std::array<uchar, 10 + len> buf
{
static_cast<uchar>(rumbleCounter++ & 0xF),
0x00_uc,
0x01_uc,
0x40_uc,
0x40_uc,
0x00_uc,
0x01_uc,
0x40_uc,
0x40_uc,
subcommand
};
if (len > 0) {
memcpy(buf.data() + 10, data.data(), len);
}
return sendCommand(command, buf);
}
exchangeArray sendRumble(uchar largeMotor, uchar smallMotor);
};
class ControllerException : public std::runtime_error {
public:
explicit ControllerException(const std::string& what);
explicit ControllerException(const char* what);
};
};