-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (76 loc) · 3.02 KB
/
main.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
#include <iostream>
#include "src/XPad.hpp"
int main() {
// Instantiate object
XPad pad;
// Event loop
#pragma clang diagnostic push
#pragma ide diagnostic ignored "EndlessLoop"
while (true) {
// Resolve joystick device if disconnected
if (!pad.isActive()) {
try {
auto device = XPad::getAvailableControllerPath();
pad.connect(std::get<0>(device), std::get<1>(device));
std::cout << "Succesfully connected to device " << std::get<0>(device) << " ("
<< (std::get<1>(device) == XPadType::ONE_WIRED ? "Wired" : "Wireless") << ")\n";
} catch (...) {
std::cerr << "Unable to connect to pad...\n";
sleep(1);
continue;
}
}
// Skip if no event was read
if (!pad.readEvent()) {
continue;
}
// Read & validate event
auto event = pad.getEvent();
if (!event.isInputType()) {
continue;
}
std::cout << "Event: " << event.getSourceName() << "\n";
if (event.getNativeType() == XEventType::EVENT_BUTTON) {
std::cout << "\t[Button]: Pressed: " << (event.isButtonPressed() ? "true" : "false")
<< ", Released: " << (event.isButtonReleased() ? "true" : "false") << "\n";
if (event.getSource() == XEventSource::BUTTON_X) {
std::cout << "Trigger rumble (1)\n";
pad.rumble(1);
}
if (event.getSource() == XEventSource::BUTTON_Y) {
std::cout << "Trigger rumble (3)\n";
pad.rumble(3);
}
}
if (event.getNativeType() == XEventType::EVENT_AXIS) {
// Analog
const XEventSource Sources[] = {
XEventSource::ANALOG_LEFT_JOYSTICK_X,
XEventSource::ANALOG_LEFT_JOYSTICK_Y,
XEventSource::ANALOG_RIGHT_JOYSTICK_X,
XEventSource::ANALOG_RIGHT_JOYSTICK_Y,
};
for (const auto &joystickSource: Sources) {
if (event.getSource() == joystickSource) {
std::cout << "\t[Joystick] Position: " << event.getJoystickValue() << "\n";
}
}
// Trigger
const XEventSource triggerSources[] = {
XEventSource::ANALOG_LEFT_TRIGGER,
XEventSource::ANALOG_RIGHT_TRIGGER,
};
for (const auto &triggerSource: triggerSources) {
if (event.getSource() == triggerSource) {
std::cout << "\t[Trigger] Position: " << event.getTriggerValue() << "\n";
}
}
// DPAD
if (event.getSource() == XEventSource::DPAD_Y || event.getSource() == XEventSource::DPAD_X) {
std::cout << "\t[DPAD] Direction: " << event.getDpadDirection() << "\n";
}
}
std::cout << "\n";
}
#pragma clang diagnostic pop
}