-
Notifications
You must be signed in to change notification settings - Fork 4
/
devices_status.c
227 lines (206 loc) · 7.17 KB
/
devices_status.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "devices_status.h"
#include <pthread.h>
void kbd_status_init(keyboard_status_t *const stats) {
stats->connected = true;
stats->q = 0;
stats->w = 0;
stats->e = 0;
stats->r = 0;
stats->t = 0;
stats->y = 0;
stats->u = 0;
stats->i = 0;
stats->o = 0;
stats->p = 0;
stats->a = 0;
stats->s = 0;
stats->d = 0;
stats->f = 0;
stats->g = 0;
stats->h = 0;
stats->j = 0;
stats->k = 0;
stats->l = 0;
stats->z = 0;
stats->x = 0;
stats->c = 0;
stats->v = 0;
stats->b = 0;
stats->n = 0;
stats->m = 0;
stats->num_1 = 0;
stats->num_2 = 0;
stats->num_3 = 0;
stats->num_4 = 0;
stats->num_5 = 0;
stats->num_6 = 0;
stats->num_7 = 0;
stats->num_8 = 0;
stats->num_9 = 0;
stats->num_0 = 0;
stats->up = 0;
stats->down = 0;
stats->left = 0;
stats->right = 0;
stats->lctrl = 0;
}
void mouse_status_init(mouse_status_t *const stats) {
stats->connected = true;
stats->x = 0;
stats->y = 0;
stats->btn_left = 0;
stats->btn_middle = 0;
stats->btn_right = 0;
}
void gamepad_status_init(gamepad_status_t *const stats) {
stats->connected = true;
stats->joystick_positions[0][0] = 0;
stats->joystick_positions[0][1] = 0;
stats->joystick_positions[1][0] = 0;
stats->joystick_positions[1][1] = 0;
stats->dpad = 0x00;
stats->l2_trigger = 0;
stats->r2_trigger = 0;
stats->triangle = 0;
stats->circle = 0;
stats->cross = 0;
stats->square = 0;
stats->r3 = 0;
stats->r3 = 0;
stats->option = 0;
stats->share = 0;
stats->center = 0;
stats->r4 = 0;
stats->l4 = 0;
stats->r5 = 0;
stats->l5 = 0;
stats->motors_intensity[0] = 0;
stats->motors_intensity[1] = 0;
stats->rumble_events_count = 0;
stats->gyro[0] = 0;
stats->gyro[1] = 0;
stats->gyro[2] = 0;
stats->accel[0] = 0;
stats->accel[1] = 0;
stats->accel[2] = 0;
stats->leds_events_count = 0;
stats->leds_colors[0] = 0;
stats->leds_colors[1] = 0;
stats->leds_colors[2] = 0;
stats->touchpad_touch_num = -1;
stats->touchpad_x = 0;
stats->touchpad_y = 0;
stats->join_left_analog_and_gyroscope = 0;
stats->join_right_analog_and_gyroscope = 0;
stats->flags = 0;
}
void devices_status_init(devices_status_t *const stats) {
pthread_mutex_init(&stats->mutex, NULL);
gamepad_status_init(&stats->gamepad);
kbd_status_init(&stats->kbd);
mouse_status_init(&stats->mouse);
}
void gamepad_status_qam_quirk(gamepad_status_t *const gamepad_stats) {
static struct timeval press_time;
if (gamepad_stats->flags & GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER) {
struct timeval now;
gettimeofday(&now, NULL);
// Calculate elapsed time in milliseconds
const int64_t elapsed_time = (now.tv_sec - press_time.tv_sec) * 1000 +
(now.tv_usec - press_time.tv_usec) / 1000;
if (gamepad_stats->center) {
// If the center button is pressed and at least X ms have passed
if (elapsed_time >= PRESS_AND_RELEASE_DURATION_FOR_CENTER_BUTTON_MS) {
gamepad_stats->center = 0;
gamepad_stats->flags &= ~GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER;
}
} else {
// If the center button is pressed
gamepad_stats->center = 1;
gettimeofday(&press_time, NULL);
}
} else if (gamepad_stats->flags & GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM) {
struct timeval now;
gettimeofday(&now, NULL);
static int releasing = 0;
// Calculate elapsed time in milliseconds
int64_t elapsed_time = (now.tv_sec - press_time.tv_sec) * 1000 +
(now.tv_usec - press_time.tv_usec) / 1000;
if ((gamepad_stats->center) && (!gamepad_stats->cross)) {
if ((!releasing) && (elapsed_time >= PRESS_TIME_BEFORE_CROSS_BUTTON_MS)) {
gamepad_stats->center = 1;
gamepad_stats->cross = 1;
press_time = now;
} else if ((releasing) && (elapsed_time >= PRESS_TIME_AFTER_CROSS_BUTTON_MS)) {
gamepad_stats->center = 0;
gamepad_stats->cross = 0;
press_time = now;
gamepad_stats->flags &= ~GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM;
}
} else if ((gamepad_stats->center) && (gamepad_stats->cross)) {
if (elapsed_time >= PRESS_TIME_CROSS_BUTTON_MS) {
gamepad_stats->center = 1;
gamepad_stats->cross = 0;
releasing = 1;
press_time = now;
}
} else {
gamepad_stats->center = 1;
gamepad_stats->cross = 0;
releasing = 0;
gettimeofday(&press_time, NULL);
}
}
}
void gamepad_status_qam_quirk_ext_time(gamepad_status_t *const gamepad_stats) {
static struct timeval press_time;
if (gamepad_stats->flags & GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER) {
struct timeval now;
gettimeofday(&now, NULL);
// Calculate elapsed time in milliseconds
const int64_t elapsed_time = (now.tv_sec - press_time.tv_sec) * 1000 +
(now.tv_usec - press_time.tv_usec) / 1000;
if (gamepad_stats->center) {
// If the center button is pressed and at least X ms have passed
if (elapsed_time >= PRESS_AND_RELEASE_DURATION_FOR_CENTER_BUTTON_MS) {
gamepad_stats->center = 0;
gamepad_stats->flags &= ~GAMEPAD_STATUS_FLAGS_PRESS_AND_REALEASE_CENTER;
}
} else {
// If the center button is pressed
gamepad_stats->center = 1;
gettimeofday(&press_time, NULL);
}
} else if (gamepad_stats->flags & GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM) {
struct timeval now;
gettimeofday(&now, NULL);
static int releasing = 0;
// Calculate elapsed time in milliseconds
int64_t elapsed_time = (now.tv_sec - press_time.tv_sec) * 1000 +
(now.tv_usec - press_time.tv_usec) / 1000;
if ((gamepad_stats->center) && (!gamepad_stats->cross)) {
if ((!releasing) && (elapsed_time >= PRESS_TIME_BEFORE_CROSS_BUTTON_MS)) {
gamepad_stats->center = 1;
gamepad_stats->cross = 1;
press_time = now;
} else if ((releasing) && (elapsed_time >= PRESS_TIME_AFTER_CROSS_BUTTON_MS)) {
gamepad_stats->center = 0;
gamepad_stats->cross = 0;
press_time = now;
gamepad_stats->flags &= ~GAMEPAD_STATUS_FLAGS_OPEN_STEAM_QAM;
}
} else if ((gamepad_stats->center) && (gamepad_stats->cross)) {
if (elapsed_time >= PRESS_TIME_CROSS_BUTTON_MS) {
gamepad_stats->center = 1;
gamepad_stats->cross = 0;
releasing = 1;
press_time = now;
}
} else {
gamepad_stats->center = 1;
gamepad_stats->cross = 0;
releasing = 0;
gettimeofday(&press_time, NULL);
}
}
}