-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.c
146 lines (122 loc) · 3.07 KB
/
main.c
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
146
#include <stdio.h>
#include <ncurses.h>
#include "gamepad.h"
static const char* button_names[] = {
"d-pad up",
"d-pad down",
"d-pad left",
"d-pad right",
"start",
"back",
"left thumb",
"right thumb",
"left shoulder",
"right shoulder",
"???",
"???",
"A",
"B",
"X",
"Y"
};
static int line = 0;
static void logevent(const char* format, ...) {
va_list va;
move(9 + line, 0);
clrtoeol();
move(8 + line, 0);
clrtoeol();
va_start(va, format);
vwprintw(stdscr, format, va);
va_end(va);
if (++line == 14) {
line = 0;
}
}
static void update(GAMEPAD_DEVICE dev) {
float lx, ly, rx, ry;
move(dev, 0);
if (!GamepadIsConnected(dev)) {
printw("%d) n/a\n", dev);
return;
}
GamepadStickNormXY(dev, STICK_LEFT, &lx, &ly);
GamepadStickNormXY(dev, STICK_RIGHT, &rx, &ry);
printw("%d) L:(%+.3f,%+.3f :: %+.3f,%+.3f) R:(%+.3f, %+.3f :: %+.3f,%+.3f) LT:%+.3f RT:%+.3f ",
dev,
lx, ly,
GamepadStickAngle(dev, STICK_LEFT),
GamepadStickLength(dev, STICK_LEFT),
rx, ry,
GamepadStickAngle(dev, STICK_RIGHT),
GamepadStickLength(dev, STICK_RIGHT),
GamepadTriggerLength(dev, TRIGGER_LEFT),
GamepadTriggerLength(dev, TRIGGER_RIGHT));
printw("U:%d D:%d L:%d R:%d ",
GamepadButtonDown(dev, BUTTON_DPAD_UP),
GamepadButtonDown(dev, BUTTON_DPAD_DOWN),
GamepadButtonDown(dev, BUTTON_DPAD_LEFT),
GamepadButtonDown(dev, BUTTON_DPAD_RIGHT));
printw("A:%d B:%d X:%d Y:%d Bk:%d St:%d ",
GamepadButtonDown(dev, BUTTON_A),
GamepadButtonDown(dev, BUTTON_B),
GamepadButtonDown(dev, BUTTON_X),
GamepadButtonDown(dev, BUTTON_Y),
GamepadButtonDown(dev, BUTTON_BACK),
GamepadButtonDown(dev, BUTTON_START));
printw("LB:%d RB:%d LS:%d RS:%d\n",
GamepadButtonDown(dev, BUTTON_LEFT_SHOULDER),
GamepadButtonDown(dev, BUTTON_RIGHT_SHOULDER),
GamepadButtonDown(dev, BUTTON_LEFT_THUMB),
GamepadButtonDown(dev, BUTTON_RIGHT_THUMB));
}
int main() {
int ch, i, j, k;
initscr();
cbreak();
noecho();
timeout(1);
GamepadInit();
while ((ch = getch()) != 'q') {
GamepadUpdate();
if (ch == 'r') {
for (i = 0; i != GAMEPAD_COUNT; ++i) {
GamepadSetRumble((GAMEPAD_DEVICE)i, 0.25f, 0.25f);
}
}
update(GAMEPAD_0);
update(GAMEPAD_1);
update(GAMEPAD_2);
update(GAMEPAD_3);
for (i = 0; i != GAMEPAD_COUNT; ++i) {
if (GamepadIsConnected(i)) {
for (j = 0; j != BUTTON_COUNT; ++j) {
if (GamepadButtonTriggered(i, j)) {
logevent("[%d] button triggered: %s", i, button_names[j]);
} else if (GamepadButtonReleased(i, j)) {
logevent("[%d] button released: %s", i, button_names[j]);
}
}
for (j = 0; j != TRIGGER_COUNT; ++j) {
if (GamepadTriggerTriggered(i, j)) {
logevent("[%d] trigger pressed: %d", i, j);
} else if (GamepadTriggerReleased(i, j)) {
logevent("[%d] trigger released: %d", i, j);
}
}
for (j = 0; j != STICK_COUNT; ++j) {
for (k = 0; k != STICKDIR_COUNT; ++k) {
if (GamepadStickDirTriggered(i, j, k)) {
logevent("[%d] stick direction: %d -> %d", i, j, k);
}
}
}
}
}
move(6, 0);
printw("(q)uit (r)umble");
refresh();
}
endwin();
return 0;
}