Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/src/pc/controller/wup.c
7861 views
1
#if !defined(__MINGW32__) && !defined(__BSD__) && !defined(TARGET_WEB) && !defined(TARGET_MACOS)
2
// See LICENSE for license
3
4
#define _XOPEN_SOURCE 600
5
6
#include <time.h>
7
#include <stdbool.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <stdint.h>
12
13
#include <linux/input.h>
14
#include <sys/types.h>
15
#include <sys/stat.h>
16
#include <fcntl.h>
17
#include <unistd.h>
18
#include <signal.h>
19
#include <errno.h>
20
21
//#include <libudev.h>
22
#include <libusb.h>
23
#include <pthread.h>
24
25
#include "macros.h"
26
27
#if (!defined(LIBUSBX_API_VERSION) || LIBUSBX_API_VERSION < 0x01000102) && (!defined(LIBUSB_API_VERSION) || LIBUSB_API_VERSION < 0x01000102)
28
#error libusb(x) 1.0.16 or higher is required
29
#endif
30
31
#define EP_IN 0x81
32
#define EP_OUT 0x02
33
34
#define STATE_NORMAL 0x10
35
#define STATE_WAVEBIRD 0x20
36
37
const int BUTTON_OFFSET_VALUES[16] = {
38
BTN_START,
39
BTN_TR2,
40
BTN_TR,
41
BTN_TL,
42
-1,
43
-1,
44
-1,
45
-1,
46
BTN_SOUTH,
47
BTN_WEST,
48
BTN_EAST,
49
BTN_NORTH,
50
BTN_DPAD_LEFT,
51
BTN_DPAD_RIGHT,
52
BTN_DPAD_DOWN,
53
BTN_DPAD_UP,
54
};
55
56
const int AXIS_OFFSET_VALUES[6] = {
57
ABS_X,
58
ABS_Y,
59
ABS_RX,
60
ABS_RY,
61
ABS_Z,
62
ABS_RZ
63
};
64
65
struct ports
66
{
67
bool connected;
68
bool extra_power;
69
unsigned char type;
70
uint16_t buttons;
71
uint8_t axis[6];
72
};
73
74
struct adapter
75
{
76
volatile bool quitting;
77
struct libusb_device *device;
78
struct libusb_device_handle *handle;
79
pthread_t thread;
80
unsigned char rumble[5];
81
struct ports controllers[4];
82
struct adapter *next;
83
};
84
85
static bool raw_mode;
86
87
static volatile int quitting;
88
89
static struct adapter adapters;
90
91
static const char *uinput_path;
92
93
bool wup_get_controller_input(uint16_t *buttons, uint8_t axis[6]) {
94
struct adapter *adapter = adapters.next;
95
if (adapter != NULL) {
96
*buttons = adapter->controllers[0].buttons;
97
memcpy(axis, adapter->controllers[0].axis, 6);
98
return true;
99
} else {
100
return false;
101
}
102
}
103
104
static unsigned char connected_type(unsigned char status)
105
{
106
unsigned char type = status & (STATE_NORMAL | STATE_WAVEBIRD);
107
switch (type)
108
{
109
case STATE_NORMAL:
110
case STATE_WAVEBIRD:
111
return type;
112
default:
113
return 0;
114
}
115
}
116
117
static void handle_payload(int i, struct ports *port, unsigned char *payload)
118
{
119
unsigned char status = payload[0];
120
unsigned char type = connected_type(status);
121
122
if (type != 0 && !port->connected)
123
{
124
//uinput_create(i, port, type);
125
port->type = type;
126
port->connected = true;
127
}
128
else if (type == 0 && port->connected)
129
{
130
//uinput_destroy(i, port);
131
port->connected = false;
132
}
133
134
if (!port->connected)
135
return;
136
137
port->extra_power = ((status & 0x04) != 0);
138
139
if (type != port->type)
140
{
141
fprintf(stderr, "controller on port %d changed controller type???", i+1);
142
port->type = type;
143
}
144
145
uint16_t btns = (uint16_t) payload[1] << 8 | (uint16_t) payload[2];
146
port->buttons = btns;
147
//printf("Btns: %04x\n", btns);
148
149
//printf("Axis:");
150
for (int j = 0; j < 6; j++)
151
{
152
unsigned char value = payload[j+3];
153
port->axis[j] = value;
154
//printf(" %02x", value);
155
}
156
//puts("");
157
}
158
159
static int64_t to_ms(struct timespec* t) {
160
return t->tv_sec * 1000 + t->tv_nsec / 1000000;
161
}
162
163
static void *adapter_thread(void *data)
164
{
165
struct adapter *a = (struct adapter *)data;
166
167
int bytes_transferred;
168
unsigned char payload[1] = { 0x13 };
169
170
int transfer_ret = libusb_interrupt_transfer(a->handle, EP_OUT, payload, sizeof(payload), &bytes_transferred, 0);
171
172
if (transfer_ret != 0) {
173
fprintf(stderr, "libusb_interrupt_transfer: %s\n", libusb_error_name(transfer_ret));
174
return NULL;
175
}
176
if (bytes_transferred != sizeof(payload)) {
177
fprintf(stderr, "libusb_interrupt_transfer %d/%lu bytes transferred.\n", bytes_transferred, sizeof(payload));
178
return NULL;
179
}
180
181
while (!a->quitting)
182
{
183
//struct timespec time_before = { 0 }, time_after = { 0 };
184
unsigned char payload[37];
185
int size = 0;
186
//clock_gettime(CLOCK_MONOTONIC, &time_before);
187
int transfer_ret = libusb_interrupt_transfer(a->handle, EP_IN, payload, sizeof(payload), &size, 0);
188
//clock_gettime(CLOCK_MONOTONIC, &time_after);
189
//printf("Time taken: %d\n", (int)(to_ms(&time_after) - to_ms(&time_before)));
190
if (transfer_ret != 0) {
191
fprintf(stderr, "libusb_interrupt_transfer error %d\n", transfer_ret);
192
a->quitting = true;
193
break;
194
}
195
if (size != 37 || payload[0] != 0x21)
196
continue;
197
198
unsigned char *controller = &payload[1];
199
200
unsigned char rumble[5] = { 0x11, 0, 0, 0, 0 };
201
//struct timespec current_time = { 0 };
202
//clock_gettime(CLOCK_REALTIME, &current_time);
203
//printf("Time: %d %d\n", (int)current_time.tv_sec, (int)current_time.tv_nsec);
204
for (int i = 0; i < 4; i++, controller += 9)
205
{
206
handle_payload(i, &a->controllers[i], controller);
207
rumble[i+1] = 0;
208
/*if (a->controllers[i].extra_power && a->controllers[i].type == STATE_NORMAL)
209
{
210
for (int j = 0; j < MAX_FF_EVENTS; j++)
211
{
212
struct ff_event *e = &a->controllers[i].ff_events[j];
213
if (e->in_use)
214
{
215
if (ts_lessthan(&e->start_time, &current_time) && ts_greaterthan(&e->end_time, &current_time))
216
rumble[i+1] = 1;
217
else
218
update_ff_start_stop(e, &current_time);
219
}
220
}
221
}*/
222
}
223
224
if (memcmp(rumble, a->rumble, sizeof(rumble)) != 0)
225
{
226
memcpy(a->rumble, rumble, sizeof(rumble));
227
transfer_ret = libusb_interrupt_transfer(a->handle, EP_OUT, a->rumble, sizeof(a->rumble), &size, 0);
228
if (transfer_ret != 0) {
229
fprintf(stderr, "libusb_interrupt_transfer error %d\n", transfer_ret);
230
a->quitting = true;
231
break;
232
}
233
}
234
}
235
236
for (int i = 0; i < 4; i++)
237
{
238
/*if (a->controllers[i].connected)
239
uinput_destroy(i, &a->controllers[i]);*/
240
}
241
242
return NULL;
243
}
244
245
static void add_adapter(struct libusb_device *dev)
246
{
247
struct adapter *a = calloc(1, sizeof(struct adapter));
248
if (a == NULL)
249
{
250
fprintf(stderr, "FATAL: calloc() failed");
251
exit(-1);
252
}
253
a->device = dev;
254
255
if (libusb_open(a->device, &a->handle) != 0)
256
{
257
fprintf(stderr, "Error opening device 0x%p\n", a->device);
258
return;
259
}
260
261
if (libusb_kernel_driver_active(a->handle, 0) == 1) {
262
fprintf(stderr, "Detaching kernel driver\n");
263
if (libusb_detach_kernel_driver(a->handle, 0)) {
264
fprintf(stderr, "Error detaching handle 0x%p from kernel\n", a->handle);
265
return;
266
}
267
}
268
269
struct adapter *old_head = adapters.next;
270
adapters.next = a;
271
a->next = old_head;
272
273
pthread_create(&a->thread, NULL, adapter_thread, a);
274
275
fprintf(stderr, "adapter 0x%p connected\n", a->device);
276
}
277
278
static void remove_adapter(struct libusb_device *dev)
279
{
280
struct adapter *a = &adapters;
281
while (a->next != NULL)
282
{
283
if (a->next->device == dev)
284
{
285
a->next->quitting = true;
286
pthread_join(a->next->thread, NULL);
287
fprintf(stderr, "adapter 0x%p disconnected\n", a->next->device);
288
libusb_close(a->next->handle);
289
struct adapter *new_next = a->next->next;
290
free(a->next);
291
a->next = new_next;
292
return;
293
}
294
295
a = a->next;
296
}
297
}
298
299
static int LIBUSB_CALL hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, libusb_hotplug_event event, void *user_data)
300
{
301
(void)ctx;
302
(void)user_data;
303
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
304
{
305
add_adapter(dev);
306
}
307
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
308
{
309
remove_adapter(dev);
310
}
311
312
return 0;
313
}
314
315
void *wup_start(UNUSED void *a)
316
{
317
libusb_init(NULL);
318
319
struct libusb_device **devices;
320
321
int count = libusb_get_device_list(NULL, &devices);
322
323
for (int i = 0; i < count; i++)
324
{
325
struct libusb_device_descriptor desc;
326
libusb_get_device_descriptor(devices[i], &desc);
327
if (desc.idVendor == 0x057e && desc.idProduct == 0x0337)
328
add_adapter(devices[i]);
329
}
330
331
if (count > 0)
332
libusb_free_device_list(devices, 1);
333
334
libusb_hotplug_callback_handle callback;
335
336
int hotplug_capability = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG);
337
if (hotplug_capability) {
338
int hotplug_ret = libusb_hotplug_register_callback(NULL,
339
LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
340
LIBUSB_HOTPLUG_NO_FLAGS, 0x057e, 0x0337,
341
LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, &callback);
342
343
if (hotplug_ret != LIBUSB_SUCCESS) {
344
fprintf(stderr, "cannot register hotplug callback, hotplugging not enabled\n");
345
hotplug_capability = 0;
346
}
347
}
348
349
// pump events until shutdown & all helper threads finish cleaning up
350
while (!quitting)
351
libusb_handle_events_completed(NULL, (int *)&quitting);
352
353
while (adapters.next)
354
remove_adapter(adapters.next->device);
355
356
if (hotplug_capability)
357
libusb_hotplug_deregister_callback(NULL, callback);
358
359
libusb_exit(NULL);
360
return (void *)0;
361
}
362
#endif
363
364