-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepadserver.cpp
99 lines (81 loc) · 3.86 KB
/
gamepadserver.cpp
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
#include "gamepadserver.h"
#include "constantvalues.h"
#include <Windows.h>
#include <Xinput.h>
#include <QDebug>
#include <QTimer>
GamepadServer * GamepadServer::s_instance = nullptr;
// Local declarations
namespace GamepadServerLocal {
void prepareStateToSend(GamepadState & gps, const XINPUT_STATE & xState);
void updateButtonPress(GamepadState & gps, const uint32_t & btn);
void setButton(bool & gamePadButtonState, const uint32_t & btn, const uint32_t & expectedValue);
void updateAnalogs(GamepadState & gps, const XINPUT_GAMEPAD & xStatePad);
}
// Member defintions
GamepadServer::GamepadServer()
{
QTimer * gameServerTimer = new QTimer(this);
connect(gameServerTimer, SIGNAL(timeout()), this, SLOT(readState()));
gameServerTimer->start(constantValues::UPDATE_PERIOD_MS);
}
void GamepadServer::readState() {
DWORD dwResult;
for (DWORD i=0; i < XUSER_MAX_COUNT; i++) {
XINPUT_STATE state;
ZeroMemory( & state, sizeof(XINPUT_STATE));
dwResult = XInputGetState(i, &state);
if (dwResult == ERROR_SUCCESS) {
//qDebug () << "Controller is connected.";
GamepadState gps;
GamepadServerLocal::prepareStateToSend(gps, state);
emit stateUpdate(gps, i);
} else {
// Ignore everything in life... //
}
}
}
// Local Definitions
namespace GamepadServerLocal {
void prepareStateToSend(GamepadState & gps, const XINPUT_STATE & xState) {
updateButtonPress(gps, xState.Gamepad.wButtons);
updateAnalogs(gps, xState.Gamepad);
}
void updateButtonPress(GamepadState & gps, const uint32_t & btn)
{
setButton(gps.m_pad_a, btn, XINPUT_GAMEPAD_A);
setButton(gps.m_pad_b, btn, XINPUT_GAMEPAD_B);
setButton(gps.m_pad_x, btn, XINPUT_GAMEPAD_X);
setButton(gps.m_pad_y, btn, XINPUT_GAMEPAD_Y);
setButton(gps.m_pad_up, btn, XINPUT_GAMEPAD_DPAD_UP);
setButton(gps.m_pad_down, btn, XINPUT_GAMEPAD_DPAD_DOWN);
setButton(gps.m_pad_left, btn, XINPUT_GAMEPAD_DPAD_LEFT);
setButton(gps.m_pad_right, btn, XINPUT_GAMEPAD_DPAD_RIGHT);
setButton(gps.m_pad_start, btn, XINPUT_GAMEPAD_START);
setButton(gps.m_pad_back, btn, XINPUT_GAMEPAD_BACK);
setButton(gps.m_lThumb.pressed, btn, XINPUT_GAMEPAD_LEFT_THUMB);
setButton(gps.m_rThumb.pressed, btn, XINPUT_GAMEPAD_RIGHT_THUMB);
setButton(gps.m_lShoulder, btn, XINPUT_GAMEPAD_LEFT_SHOULDER);
setButton(gps.m_rShoulder, btn, XINPUT_GAMEPAD_RIGHT_SHOULDER);
}
void setButton(bool & gamePadButtonState, const uint32_t & btn, const uint32_t & expectedValue) {
if ((btn & expectedValue) == expectedValue) {
gamePadButtonState = true;
} else {
gamePadButtonState = false;
}
}
void updateAnalogs(GamepadState & gps, const XINPUT_GAMEPAD & xStatePad) {
gps.m_lTrigger = (double) xStatePad.bLeftTrigger/constantValues::maxTriggerValue;
gps.m_rTrigger = (double) xStatePad.bRightTrigger/constantValues::maxTriggerValue;
gps.m_lThumb.xAxis = (double) xStatePad.sThumbLX/constantValues::maxJoystickValue;
gps.m_lThumb.yAxis = (double) xStatePad.sThumbLY/constantValues::maxJoystickValue;
gps.m_rThumb.xAxis = (double) xStatePad.sThumbRX/constantValues::maxJoystickValue;
gps.m_rThumb.yAxis = (double) xStatePad.sThumbRY/constantValues::maxJoystickValue;
// Factoring in deadzone
gps.m_lThumb.xAxis = (abs(gps.m_lThumb.xAxis) < constantValues::deadzoneX ? 0 : gps.m_lThumb.xAxis);
gps.m_lThumb.yAxis = (abs(gps.m_lThumb.yAxis) < constantValues::deadzoneY ? 0 : gps.m_lThumb.yAxis);
gps.m_rThumb.xAxis = (abs(gps.m_rThumb.xAxis) < constantValues::deadzoneX ? 0 : gps.m_rThumb.xAxis);
gps.m_rThumb.yAxis = (abs(gps.m_rThumb.yAxis) < constantValues::deadzoneY ? 0 : gps.m_rThumb.yAxis);
}
}