Debian Codegraph @

src/win/wjoyxi.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows XInput joystick driver.
12 *
13 * By Beoran.
14 *
15 *
16 * See readme.txt for copyright information.
17 */
18
19
20 #define ALLEGRO_NO_COMPATIBILITY
21
22 /* For waitable timers */
23 #define _WIN32_WINNT 0x0501
24
25 #include "allegro5/allegro.h"
26 #include "allegro5/internal/aintern.h"
27 #include "allegro5/platform/aintwin.h"
28 #include "allegro5/internal/aintern_events.h"
29 #include "allegro5/internal/aintern_joystick.h"
30 #include "allegro5/internal/aintern_bitmap.h"
31
32
33 #ifdef ALLEGRO_CFG_XINPUT
34 /* Don't compile this lot if xinput isn't supported. */
35
36 #ifndef ALLEGRO_WINDOWS
37 #error something is wrong with the makefile
38 #endif
39
40 #ifdef ALLEGRO_MINGW32
41 #undef MAKEFOURCC
42 #endif
43
44 /* Poll connected joysticks frequently and non-connected ones infrequently. */
45 #ifndef ALLEGRO_XINPUT_POLL_DELAY
46 #define ALLEGRO_XINPUT_POLL_DELAY 0.01
47 #endif
48
49 #ifndef ALLEGRO_XINPUT_DISCONNECTED_POLL_DELAY
50 #define ALLEGRO_XINPUT_DISCONNECTED_POLL_DELAY 1.50
51 #endif
52
53
54 #include <stdio.h>
55 #include <allegro5/joystick.h>
56 #include <mmsystem.h>
57 #include <process.h>
58
59 /* The official DirectX xinput.h uses SAL annotations.
60 * Need the sal.h header to get rid of them. On some other platforms
61 * such as MinGW on Linux this header is lacking because it is not needed there.
62 * So, simply try to include sal.h IF we have it , and if not, hope
63 * for the best.
64 * This does no harm on msys2 either, they have a sal.h header.
65 */
66 #ifdef ALLEGRO_HAVE_SAL_H
67 #include <sal.h>
68 #endif
69
70 #include <xinput.h>
71
72 ALLEGRO_DEBUG_CHANNEL("xinput")
73
74 #include "allegro5/joystick.h"
75 #include "allegro5/internal/aintern_joystick.h"
76 #include "allegro5/internal/aintern_wjoyxi.h"
77
78
79 /* forward declarations */
80 static bool joyxi_init_joystick(void);
81 static void joyxi_exit_joystick(void);
82 static bool joyxi_reconfigure_joysticks(void);
83 static int joyxi_get_num_joysticks(void);
84 static ALLEGRO_JOYSTICK *joyxi_get_joystick(int num);
85 static void joyxi_release_joystick(ALLEGRO_JOYSTICK *joy);
86 static void joyxi_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state);
87 static const char *joyxi_get_name(ALLEGRO_JOYSTICK *joy);
88 static bool joyxi_get_active(ALLEGRO_JOYSTICK *joy);
89
90
91 /* the driver vtable */
92 ALLEGRO_JOYSTICK_DRIVER _al_joydrv_xinput =
93 {
94 AL_JOY_TYPE_XINPUT,
95 "",
96 "",
97 "XInput Joystick",
98 joyxi_init_joystick,
99 joyxi_exit_joystick,
100 joyxi_reconfigure_joysticks,
101 joyxi_get_num_joysticks,
102 joyxi_get_joystick,
103 joyxi_release_joystick,
104 joyxi_get_joystick_state,
105 joyxi_get_name,
106 joyxi_get_active
107 };
108
109 #define XINPUT_MIN_VERSION 3
110 #define XINPUT_MAX_VERSION 4
111
112 #ifndef ALLEGRO_CFG_HAVE_XINPUT_CAPABILITIES_EX
113 typedef struct _XINPUT_CAPABILITIES_EX
114 {
115 XINPUT_CAPABILITIES Capabilities;
116 WORD VendorId;
117 WORD ProductId;
118 WORD VersionNumber;
119 WORD unk1;
120 DWORD unk2;
121 } XINPUT_CAPABILITIES_EX, * PXINPUT_CAPABILITIES_EX;
122 #endif
123
124 typedef void (WINAPI *XInputEnablePROC)(BOOL);
125 typedef DWORD (WINAPI *XInputSetStatePROC)(DWORD, XINPUT_VIBRATION*);
126 typedef DWORD (WINAPI *XInputGetStatePROC)(DWORD, XINPUT_STATE*);
127 typedef DWORD (WINAPI *XInputGetCapabilitiesPROC)(DWORD, DWORD, XINPUT_CAPABILITIES*);
128 typedef DWORD (WINAPI *XInputGetCapabilitiesExPROC)(DWORD, DWORD, DWORD, XINPUT_CAPABILITIES_EX*);
129
130 static HMODULE _imp_xinput_module = 0;
131
132 static XInputEnablePROC _imp_XInputEnable = NULL;
133 static XInputGetStatePROC _imp_XInputGetState = NULL;
134 static XInputGetCapabilitiesPROC _imp_XInputGetCapabilities = NULL;
135 static XInputGetCapabilitiesExPROC _imp_XInputGetCapabilitiesEx = NULL;
136 XInputSetStatePROC _al_imp_XInputSetState = NULL;
137
138 /* the joystick structures */
139 static ALLEGRO_JOYSTICK_XINPUT joyxi_joysticks[MAX_JOYSTICKS];
140
141 /* For the background threads. */
142 static ALLEGRO_THREAD *joyxi_thread = NULL;
143 static ALLEGRO_THREAD *joyxi_disconnected_thread = NULL;
144 static ALLEGRO_MUTEX *joyxi_mutex = NULL;
145 /* Use condition variables to put the thread to sleep and prevent too
146 frequent polling*/
147 static ALLEGRO_COND *joyxi_cond = NULL;
148 static ALLEGRO_COND *joyxi_disconnected_cond = NULL;
149
150 /* Names for things in because XInput doesn't provide them. */
151
152 /* Names of the sticks.*/
153 static char *const joyxi_stick_names[MAX_STICKS] = {
154 "Left Thumbstick",
155 "Right Thumbstick",
156 "Left Trigger",
157 "Right Trigger",
158 };
159
160 /* Names of the axis */
161 static char *const joyxi_axis_names[MAX_STICKS][MAX_AXES] = {
162 { "X", "Y" },
163 { "X", "Y" },
164 { "Ramp", "Error" },
165 { "Ramp", "Error" },
166 };
167
168 /* Sticks per axis */
169 static const int joyxi_axis_per_stick[MAX_STICKS] = {
170 2, 2, 1, 1,
171 };
172
173
174 /* Struct to help mapping. */
175 struct _AL_XINPUT_BUTTON_MAPPING
176 {
177 int flags;
178 int button;
179 const char *name;
180 };
181
182 /* The data in this array helps to map from XINPUT button input to
183 ALLEGRO's. */
184 static const struct _AL_XINPUT_BUTTON_MAPPING
185 joyxi_button_mapping[MAX_BUTTONS] = {
186 { XINPUT_GAMEPAD_A, 0, "A" },
187 { XINPUT_GAMEPAD_B, 1, "B" },
188 { XINPUT_GAMEPAD_X, 2, "X" },
189 { XINPUT_GAMEPAD_Y, 3, "Y" },
190 { XINPUT_GAMEPAD_RIGHT_SHOULDER, 4, "RB" },
191 { XINPUT_GAMEPAD_LEFT_SHOULDER, 5, "LB" },
192 { XINPUT_GAMEPAD_RIGHT_THUMB, 6, "RT" },
193 { XINPUT_GAMEPAD_LEFT_THUMB, 7, "LT" },
194 { XINPUT_GAMEPAD_BACK, 8, "BACK" },
195 { XINPUT_GAMEPAD_START, 9, "START" },
196 { XINPUT_GAMEPAD_DPAD_RIGHT, 10, "RIGHT DPAD" },
197 { XINPUT_GAMEPAD_DPAD_LEFT, 11, "LEFT DPAD" },
198 { XINPUT_GAMEPAD_DPAD_DOWN, 12, "DOWN DPAD" },
199 { XINPUT_GAMEPAD_DPAD_UP, 13, "UP DPAD" },
200 };
201
202 static bool compat_5_2_10(void) {
203 /* New layout. */
204 return _al_get_joystick_compat_version() < AL_ID(5, 2, 11, 0);
205 }
206
207 static void unload_xinput_module(void)
208 {
209 FreeLibrary(_imp_xinput_module);
210 _imp_xinput_module = NULL;
211 }
212
213 static bool _imp_load_xinput_module_version(int version)
214 {
215 char module_name[16];
216
217 sprintf(module_name, "xinput1_%d.dll", version);
218
219 _imp_xinput_module = _al_win_safe_load_library(module_name);
220 if (NULL == _imp_xinput_module)
221 return false;
222
223 _imp_XInputEnable = (XInputEnablePROC)GetProcAddress(_imp_xinput_module, "XInputEnable");
224 if (NULL == _imp_XInputEnable) {
225 FreeLibrary(_imp_xinput_module);
226 _imp_xinput_module = NULL;
227 return false;
228 }
229 _imp_XInputGetState = (XInputGetStatePROC)GetProcAddress(_imp_xinput_module, "XInputGetState");
230 _imp_XInputGetCapabilities = (XInputGetCapabilitiesPROC)GetProcAddress(_imp_xinput_module, "XInputGetCapabilities");
231 if (version == 4)
232 _imp_XInputGetCapabilitiesEx = (XInputGetCapabilitiesExPROC)GetProcAddress(_imp_xinput_module, (char*)108);
233 _al_imp_XInputSetState = (XInputSetStatePROC)GetProcAddress(_imp_xinput_module, "XInputSetState");
234
235 ALLEGRO_INFO("Module \"%s\" loaded.\n", module_name);
236
237 return true;
238 }
239
240 static bool load_xinput_module(void)
241 {
242 long version;
243 char const *value;
244
245 if (_imp_xinput_module) {
246 return true;
247 }
248
249 value = al_get_config_value(al_get_system_config(),
250 "joystick", "force_xinput_version");
251 if (value) {
252 errno = 0;
253 version = strtol(value, NULL, 10);
254 if (errno) {
255 ALLEGRO_ERROR("Failed to override XInput version. \"%s\" is not valid integer number.", value);
256 return false;
257 }
258 else
259 return _imp_load_xinput_module_version((int)version);
260 }
261
262 // Iterate over all valid versions.
263 for (version = XINPUT_MAX_VERSION; version >= XINPUT_MIN_VERSION; version--)
264 if (_imp_load_xinput_module_version((int)version))
265 return true;
266
267 ALLEGRO_ERROR("Failed to load XInput library. Library is not installed.");
268
269 return false;
270 }
271
272 /* generate_axis_event: [joystick thread]
273 * Helper to generate an event when reconfiguration is needed.
274 * The joystick must be locked BEFORE entering this function.
275 */
276
277 static void joyxi_generate_reconfigure_event(void)
278 {
279 ALLEGRO_EVENT_SOURCE *source;
280 ALLEGRO_EVENT event;
281
282 /* There is a potential race condition with all of the generate functions
283 * in this source file where al_get_joystick_event_source is returning NULL
284 * because the joystick driver init functions haven't finished and
285 * therefore new_joystick_driver in joynu.c isn't set yet. So we check that
286 * the driver has fully installed before proceeding.
287 */
288 if (!al_is_joystick_installed())
289 return;
290
291 source = al_get_joystick_event_source();
292
293 if (!_al_event_source_needs_to_generate_event(source))
294 return;
295
296 event.type = ALLEGRO_EVENT_JOYSTICK_CONFIGURATION;
297 event.any.timestamp = al_get_time();
298 event.any.source = source;
299 _al_event_source_emit_event(source, &event);
300 }
301
302 /* Helper to generate an event after an axis is moved.
303 * The joystick must be locked BEFORE entering this function.
304 */
305 static void joyxi_generate_axis_event(ALLEGRO_JOYSTICK_XINPUT *joy, int stick, int axis, float pos)
306 {
307 ALLEGRO_EVENT event;
308 ALLEGRO_EVENT_SOURCE *es;
309
310 if (!al_is_joystick_installed())
311 return;
312
313 es = al_get_joystick_event_source();
314
315 if (!_al_event_source_needs_to_generate_event(es))
316 return;
317
318 event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS;
319 event.joystick.timestamp = al_get_time();
320 event.joystick.id = (ALLEGRO_JOYSTICK *)&joy->parent;
321 event.joystick.stick = stick;
322 event.joystick.axis = axis;
323 event.joystick.pos = pos;
324 event.joystick.button = 0;
325 ALLEGRO_DEBUG("Generating an axis event on stick %d axis %d value %f:\n", stick, axis, pos);
326
327 _al_event_source_emit_event(es, &event);
328 }
329
330
331 /* Helper to generate an event after a button is pressed or released.
332 * The joystick must be locked BEFORE entering this function.
333 */
334 static void joyxi_generate_button_event(ALLEGRO_JOYSTICK_XINPUT *joy, int button, ALLEGRO_EVENT_TYPE event_type)
335 {
336 ALLEGRO_EVENT event;
337 ALLEGRO_EVENT_SOURCE *es;
338
339 if (!al_is_joystick_installed())
340 return;
341
342 es = al_get_joystick_event_source();
343
344 if (!_al_event_source_needs_to_generate_event(es))
345 return;
346
347 event.joystick.type = event_type;
348 event.joystick.timestamp = al_get_time();
349 event.joystick.id = (ALLEGRO_JOYSTICK *)joy;
350 event.joystick.stick = 0;
351 event.joystick.axis = 0;
352 event.joystick.pos = 0.0;
353 event.joystick.button = button;
354 ALLEGRO_DEBUG("Generating an button event on button %d type %d:\n", button, event_type);
355
356 _al_event_source_emit_event(es, &event);
357 }
358
359
360 /* Converts an XINPUT axis value to one suitable for Allegro's axes.*/
361 static float joyxi_convert_axis(SHORT value)
362 {
363 if (value >= 0) return(((float)value) / 32767.0);
364 return(((float)value) / 32768.0);
365 }
366
367 /* Converts an XINPUT trigger value to one suitable for Allegro's axes.*/
368 static float joyxi_convert_trigger(BYTE value)
369 {
370 return(((float)value) / 255.0);
371 }
372
373 static int joyxi_convert_button(int value)
374 {
375 return value ? 32767 : 0;
376 }
377
378 /* Converts an XInput state to an Allegro joystick state. */
379 static void joyxi_convert_state(ALLEGRO_JOYSTICK_STATE *alstate, XINPUT_STATE *xistate)
380 {
381 int index;
382 /* Wipe the allegro state clean. */
383 memset(alstate, 0, sizeof(*alstate));
384
385 if (!compat_5_2_10()) {
386 alstate->button[ALLEGRO_GAMEPAD_BUTTON_A] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_A);
387 alstate->button[ALLEGRO_GAMEPAD_BUTTON_B] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_B);
388 alstate->button[ALLEGRO_GAMEPAD_BUTTON_X] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_X);
389 alstate->button[ALLEGRO_GAMEPAD_BUTTON_Y] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_Y);
390 alstate->button[ALLEGRO_GAMEPAD_BUTTON_LEFT_SHOULDER] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER);
391 alstate->button[ALLEGRO_GAMEPAD_BUTTON_RIGHT_SHOULDER] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER);
392 alstate->button[ALLEGRO_GAMEPAD_BUTTON_BACK] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_BACK);
393 alstate->button[ALLEGRO_GAMEPAD_BUTTON_START] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_START);
394 alstate->button[ALLEGRO_GAMEPAD_BUTTON_LEFT_THUMB] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB);
395 alstate->button[ALLEGRO_GAMEPAD_BUTTON_RIGHT_THUMB] = joyxi_convert_button(xistate->Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB);
396
397 float dpadx = 0;
398 float dpady = 0;
399 if (xistate->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) {
400 dpadx += 1.;
401 }
402 if (xistate->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) {
403 dpadx -= 1.;
404 }
405 if (xistate->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) {
406 dpady += 1.;
407 }
408 if (xistate->Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) {
409 dpady -= 1.;
410 }
411 alstate->stick[ALLEGRO_GAMEPAD_STICK_DPAD].axis[0] = dpadx;
412 alstate->stick[ALLEGRO_GAMEPAD_STICK_DPAD].axis[1] = dpady;
413 alstate->stick[ALLEGRO_GAMEPAD_STICK_LEFT_THUMB].axis[0] = joyxi_convert_axis(xistate->Gamepad.sThumbLX);
414 alstate->stick[ALLEGRO_GAMEPAD_STICK_LEFT_THUMB].axis[1] = -joyxi_convert_axis(xistate->Gamepad.sThumbLY);
415 alstate->stick[ALLEGRO_GAMEPAD_STICK_RIGHT_THUMB].axis[0] = joyxi_convert_axis(xistate->Gamepad.sThumbRX);
416 alstate->stick[ALLEGRO_GAMEPAD_STICK_RIGHT_THUMB].axis[1] = -joyxi_convert_axis(xistate->Gamepad.sThumbRY);
417 alstate->stick[ALLEGRO_GAMEPAD_STICK_LEFT_TRIGGER].axis[0] = joyxi_convert_trigger(xistate->Gamepad.bLeftTrigger);
418 alstate->stick[ALLEGRO_GAMEPAD_STICK_RIGHT_TRIGGER].axis[0] = joyxi_convert_trigger(xistate->Gamepad.bRightTrigger);
419 }
420 else {
421 /* Map the buttons. Make good use of the mapping data. */
422 for (index = 0; index < MAX_BUTTONS; index++) {
423 const struct _AL_XINPUT_BUTTON_MAPPING *mapping = joyxi_button_mapping + index;
424 alstate->button[mapping->button] = joyxi_convert_button(xistate->Gamepad.wButtons & mapping->flags);
425 }
426
427 /* Map the x and y axes of both sticks. */
428 alstate->stick[0].axis[0] = joyxi_convert_axis(xistate->Gamepad.sThumbLX);
429 alstate->stick[0].axis[1] = -joyxi_convert_axis(xistate->Gamepad.sThumbLY);
430 alstate->stick[1].axis[0] = joyxi_convert_axis(xistate->Gamepad.sThumbRX);
431 alstate->stick[1].axis[1] = -joyxi_convert_axis(xistate->Gamepad.sThumbRY);
432 /* Map the triggers as two individual sticks and axes each . */
433 alstate->stick[2].axis[0] = joyxi_convert_trigger(xistate->Gamepad.bLeftTrigger);
434 alstate->stick[3].axis[0] = joyxi_convert_trigger(xistate->Gamepad.bRightTrigger);
435 }
436 }
437
438
439 /* Emits joystick events for the difference between the new and old joystick state. */
440 static void joyxi_emit_events(
441 ALLEGRO_JOYSTICK_XINPUT *xjoy, ALLEGRO_JOYSTICK_STATE *newstate, ALLEGRO_JOYSTICK_STATE *oldstate, _AL_JOYSTICK_INFO *info)
442 {
443 int index, subdex;
444 /* Send events for buttons. */
445 for (index = 0; index < info->num_buttons; index++) {
446 int newbutton = newstate->button[index];
447 int oldbutton = oldstate->button[index];
448 if (newbutton != oldbutton) {
449 int type = (oldbutton > newbutton ?
450 ALLEGRO_EVENT_JOYSTICK_BUTTON_UP :
451 ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN);
452 joyxi_generate_button_event(xjoy, index, type);
453 }
454 }
455 /* Send events for the thumb pad axes and triggers . */
456 for (index = 0; index < info->num_sticks; index++) {
457 for (subdex = 0; subdex < info->stick[index].num_axes; subdex++) {
458 float oldaxis = oldstate->stick[index].axis[subdex];
459 float newaxis = newstate->stick[index].axis[subdex];
460 if (oldaxis != newaxis) {
461 joyxi_generate_axis_event(xjoy, index, subdex, newaxis);
462 }
463 }
464 }
465 }
466
467
468 /* Polling function for a joystick that is currently active. */
469 static void joyxi_poll_connected_joystick(ALLEGRO_JOYSTICK_XINPUT *xjoy)
470 {
471 XINPUT_STATE xistate;
472 ALLEGRO_JOYSTICK_STATE alstate;
473 DWORD res = _imp_XInputGetState(xjoy->index, &xistate);
474 if (res != ERROR_SUCCESS) {
475 /* Assume joystick was disconnected, need to reconfigure. */
476 joyxi_generate_reconfigure_event();
477 return;
478 }
479
480 /* Check if the sequence is different. If not, no state change so
481 don't do anything. */
482 if (xistate.dwPacketNumber == xjoy->state.dwPacketNumber)
483 return;
484
485 ALLEGRO_DEBUG("XInput joystick state change detected.\n");
486
487 /* If we get here translate the state and send the needed events. */
488 joyxi_convert_state(&alstate, &xistate);
489 joyxi_emit_events(xjoy, &alstate, &xjoy->joystate, &xjoy->parent.info);
490
491 /* Finally copy over the states. */
492 xjoy->state = xistate;
493 xjoy->joystate = alstate;
494 }
495
496
497 /* Polling function for a joystick that is currently not active. Care is taken to do this infrequently so
498 performance doesn't suffer too much. */
499 static void joyxi_poll_disconnected_joystick(ALLEGRO_JOYSTICK_XINPUT *xjoy)
500 {
501 XINPUT_CAPABILITIES xicapas;
502 DWORD res;
503 res = _imp_XInputGetCapabilities(xjoy->index, 0, &xicapas);
504 if (res == ERROR_SUCCESS) {
505 /* Got capabilities, joystick was connected, need to reconfigure. */
506 joyxi_generate_reconfigure_event();
507 return;
508 }
509 /* Nothing to do if we get here. */
510 }
511
512
513 /** Polls all connected joysticks. */
514 static void joyxi_poll_connected_joysticks(void)
515 {
516 int index;
517
518 for (index = 0; index < MAX_JOYSTICKS; index++) {
519 if ((joyxi_joysticks + index)->active) {
520 joyxi_poll_connected_joystick(joyxi_joysticks + index);
521 }
522 }
523 }
524
525 /** Polls all disconnected joysticks. */
526 static void joyxi_poll_disconnected_joysticks(void)
527 {
528 int index;
529
530 for (index = 0; index < MAX_JOYSTICKS; index++) {
531 if (!((joyxi_joysticks + index)->active)) {
532 joyxi_poll_disconnected_joystick(joyxi_joysticks + index);
533 }
534 }
535 }
536
537 /** Thread function that polls the active xinput joysticks. */
538 static void *joyxi_poll_thread(ALLEGRO_THREAD *thread, void *arg)
539 {
540 ALLEGRO_TIMEOUT timeout;
541 al_lock_mutex(joyxi_mutex);
542 /* Poll once every so much time, 10ms by default. */
543 while (!al_get_thread_should_stop(thread)) {
544 al_init_timeout(&timeout, ALLEGRO_XINPUT_POLL_DELAY);
545 /* Wait for the condition for the polling time in stead of using
546 al_rest to allows the polling thread to be awoken when needed. */
547 al_wait_cond_until(joyxi_cond, joyxi_mutex, &timeout);
548 /* If we get here poll joystick for new input or connection
549 * and dispatch events. The mutexhas always been locked
550 * so this should be OK. */
551 joyxi_poll_connected_joysticks();
552 }
553 al_unlock_mutex(joyxi_mutex);
554 return arg;
555 }
556
557 /** Thread function that polls the disconnected joysticks. */
558 static void *joyxi_poll_disconnected_thread(ALLEGRO_THREAD *thread, void *arg)
559 {
560 ALLEGRO_TIMEOUT timeout;
561 al_lock_mutex(joyxi_mutex);
562 /* Poll once every so much time, 10ms by default. */
563 while (!al_get_thread_should_stop(thread)) {
564 al_init_timeout(&timeout, ALLEGRO_XINPUT_DISCONNECTED_POLL_DELAY);
565 /* Wait for the condition for the polling time in stead of using
566 al_rest to allows the polling thread to be awoken when needed. */
567 al_wait_cond_until(joyxi_disconnected_cond, joyxi_mutex, &timeout);
568 /* If we get here poll joystick for new input or connection
569 * and dispatch events. The mutex has always been locked
570 * so this should be OK. */
571 joyxi_poll_disconnected_joysticks();
572 }
573 al_unlock_mutex(joyxi_mutex);
574 return arg;
575 }
576
577
578
579 /* Initializes the info part of the joystick. */
580 static void joyxi_init_joystick_info(ALLEGRO_JOYSTICK_XINPUT *xjoy)
581 {
582 /* TODO: Fill the GUID? */
583 if (!compat_5_2_10()) {
584 _al_fill_gamepad_info(&xjoy->parent.info);
585 }
586 else {
587 int index, subdex;
588 _AL_JOYSTICK_INFO *info = &xjoy->parent.info;
589 /* Map xinput to 4 sticks: 2 thumb pads and 2 triggers. */
590 info->num_sticks = 4;
591 /* Map xinput to 14 buttons */
592 info->num_buttons = MAX_BUTTONS;
593 /* Map button names. */
594 for (index = 0; index < MAX_BUTTONS; index++) {
595 info->button[index].name = _al_strdup(joyxi_button_mapping[index].name);
596 }
597 /* Map stick and axis names. */
598 for (index = 0; index < MAX_STICKS; index++) {
599 info->stick[index].name = _al_strdup(joyxi_stick_names[index]);
600 info->stick[index].num_axes = joyxi_axis_per_stick[index];
601 info->stick[index].flags = ALLEGRO_JOYFLAG_ANALOGUE;
602 for (subdex = 0; subdex < joyxi_axis_per_stick[index]; subdex++) {
603 info->stick[index].axis[subdex].name = _al_strdup(joyxi_axis_names[index][subdex]);
604 }
605 }
606 }
607 }
608
609
610 // Source: https://github.com/xan105/node-xinput-ffi/blob/master/lib/util/HardwareID.js
611 static const char *joyxi_lookup_device_name(WORD vid, WORD pid)
612 {
613 ALLEGRO_DEBUG("Looking up name for joystick vendor: %d, product: %d\n", vid, pid);
614 if (vid == 0x045E) {
615 switch (pid) {
616 case 0x028E: return "Xbox360 Controller";
617 case 0x02A1: return "Xbox360 Controller";
618 case 0x028F: return "Xbox360 Wireless Controller";
619 case 0x02E0: return "Xbox One S Controller";
620 case 0x02FF: return "Xbox One Elite Controller";
621 case 0x0202: return "Xbox Controller";
622 case 0x0285: return "Xbox Controller S";
623 case 0x0289: return "Xbox Controller S";
624 case 0x02E3: return "Xbox One Elite Controller";
625 case 0x02EA: return "Xbox One S Controller";
626 case 0x02FD: return "Xbox One S Controller";
627 case 0x02D1: return "Xbox One Controller";
628 case 0x02DD: return "Xbox One Controller";
629 case 0x0B13: return "Xbox Series X/S controller";
630 }
631 return "Microsoft Corp.";
632 }
633 if (vid == 0x054C) {
634 switch (pid) {
635 case 0x0268: return "DualShock 3 / Sixaxis";
636 case 0x05C4: return "DualShock 4";
637 case 0x09CC: return "DualShock 4 (v2)";
638 case 0x0BA0: return "DualShock 4 USB Wireless Adaptor";
639 case 0x0CE6: return "DualSense Wireless Controller"; //PS5
640 }
641 return "Sony Corp.";
642 }
643 if (vid == 0x057E) {
644 switch (pid) {
645 case 0x0306: return "Wii Remote Controller";
646 case 0x0337: return "Wii U GameCube Controller Adapter";
647 case 0x2006: return "Joy-Con L";
648 case 0x2007: return "Joy-Con R";
649 case 0x2009: return "Switch Pro Controller";
650 case 0x200E: return "Joy-Con Charging Grip";
651 }
652 return "Nintendo Co., Ltd";
653 }
654 if (vid == 0x28DE) {
655 switch (pid) {
656 case 0x11FC: return "Steam Controller";
657 case 0x1102: return "Steam Controller";
658 case 0x1142: return "Wireless Steam Controller";
659 }
660 return "Valve Corp.";
661 }
662 if (vid == 0x046D) {
663 switch (pid) {
664 case 0xC21D: return "Logitech Gamepad F310";
665 case 0xC21E: return "Logitech Gamepad F510";
666 case 0xC21F: return "Logitech Gamepad F710";
667 case 0xC242: return "Logitech Chillstream Controller";
668 }
669 return "Logitech Inc.";
670 }
671 return "";
672 }
673
674
675 static void joyxi_set_name(ALLEGRO_JOYSTICK_XINPUT *xjoy)
676 {
677 if (_imp_XInputGetCapabilitiesEx) {
678 XINPUT_CAPABILITIES_EX xicapas;
679 int res = _imp_XInputGetCapabilitiesEx(1, xjoy->index, 0, &xicapas);
680 if (res == ERROR_SUCCESS) {
681 const char *device_name = joyxi_lookup_device_name(xicapas.VendorId, xicapas.ProductId);
682 if (device_name[0] != '\0')
683 sprintf(xjoy->name, device_name);
684 else
685 sprintf(xjoy->name, "XInput Joystick %d", (int)xjoy->index);
686 return;
687 }
688 }
689 sprintf(xjoy->name, "XInput Joystick %d", (int)xjoy->index);
690 }
691
692
693 /* Initialization API function. */
694 static bool joyxi_init_joystick(void)
695 {
696 int index;
697
698 if (!load_xinput_module())
699 return false;
700
701 /* Create the mutex and two condition variables. */
702 joyxi_mutex = al_create_mutex_recursive();
703 if (!joyxi_mutex)
704 return false;
705 joyxi_cond = al_create_cond();
706 if (!joyxi_cond)
707 return false;
708 joyxi_disconnected_cond = al_create_cond();
709 if (!joyxi_disconnected_cond)
710 return false;
711
712 al_lock_mutex(joyxi_mutex);
713
714 /* Fill in the joystick structs */
715 for (index = 0; index < MAX_JOYSTICKS; index++) {
716 joyxi_joysticks[index].active = false;
717 joyxi_joysticks[index].index = (DWORD)index;
718 joyxi_init_joystick_info(joyxi_joysticks + index);
719 joyxi_set_name(joyxi_joysticks + index);
720 }
721 /* Now, enable XInput*/
722 _imp_XInputEnable(TRUE);
723 /* Now check which joysticks are enabled and poll them for the first time
724 * but without sending any events.
725 */
726 for (index = 0; index < MAX_JOYSTICKS; index++) {
727 DWORD res = _imp_XInputGetCapabilities(joyxi_joysticks[index].index, 0, &joyxi_joysticks[index].capabilities);
728 joyxi_joysticks[index].active = (res == ERROR_SUCCESS);
729 if (joyxi_joysticks[index].active) {
730 res = _imp_XInputGetState(joyxi_joysticks[index].index, &joyxi_joysticks[index].state);
731 joyxi_joysticks[index].active = (res == ERROR_SUCCESS);
732 }
733 }
734 /* Now start two polling background thread, since XInput is a polled API.
735 * The first thread will poll the active joysticks frequently, the second
736 * thread will poll the inactive joysticks infrequently.
737 * This is done like this to preserve performance.
738 */
739 joyxi_thread = al_create_thread(joyxi_poll_thread, NULL);
740 joyxi_disconnected_thread = al_create_thread(joyxi_poll_disconnected_thread, NULL);
741
742 al_unlock_mutex(joyxi_mutex);
743
744 if (joyxi_thread) al_start_thread(joyxi_thread);
745 if (joyxi_disconnected_thread) al_start_thread(joyxi_disconnected_thread);
746
747 return (joyxi_thread != NULL) && (joyxi_disconnected_thread != NULL);
748 }
749
750
751 static void joyxi_exit_joystick(void)
752 {
753 int index;
754 void *ret_value = NULL;
755 if (!joyxi_mutex) return;
756 if (!joyxi_cond) return;
757 if (!joyxi_thread) return;
758 /* Request the event thread to shut down, signal the condition, then join the thread. */
759 al_set_thread_should_stop(joyxi_thread);
760 al_signal_cond(joyxi_cond);
761 al_join_thread(joyxi_thread, &ret_value);
762 al_set_thread_should_stop(joyxi_disconnected_thread);
763 al_signal_cond(joyxi_disconnected_cond);
764 al_join_thread(joyxi_disconnected_thread, &ret_value);
765
766 /* clean it all up. */
767 al_destroy_thread(joyxi_disconnected_thread);
768 al_destroy_cond(joyxi_disconnected_cond);
769 al_destroy_thread(joyxi_thread);
770 al_destroy_cond(joyxi_cond);
771
772 al_lock_mutex(joyxi_mutex);
773 /* Disable xinput */
774 _imp_XInputEnable(FALSE);
775 /* Wipe the joystick structs */
776 for (index = 0; index < MAX_JOYSTICKS; index++) {
777 joyxi_joysticks[index].active = false;
778 _al_destroy_joystick_info(&joyxi_joysticks[index].parent.info);
779 }
780 al_unlock_mutex(joyxi_mutex);
781 al_destroy_mutex(joyxi_mutex);
782
783 unload_xinput_module();
784 }
785
786
787 static bool joyxi_reconfigure_joysticks(void)
788 {
789 int index;
790 al_lock_mutex(joyxi_mutex);
791 for (index = 0; index < MAX_JOYSTICKS; index++) {
792 DWORD res = _imp_XInputGetCapabilities(joyxi_joysticks[index].index, 0, &joyxi_joysticks[index].capabilities);
793 joyxi_joysticks[index].active = (res == ERROR_SUCCESS);
794 if (joyxi_joysticks[index].active) {
795 res = _imp_XInputGetState(joyxi_joysticks[index].index, &joyxi_joysticks[index].state);
796 joyxi_joysticks[index].active = (res == ERROR_SUCCESS);
797 joyxi_set_name(joyxi_joysticks + index);
798 }
799 }
800 al_unlock_mutex(joyxi_mutex);
801 /** Signal the conditions so new events are sent immediately for the new joysticks. */
802 al_signal_cond(joyxi_cond);
803 /** Signal the disconnected thread in case another joystick got connected. */
804 al_signal_cond(joyxi_disconnected_cond);
805 return true;
806 }
807
808 static int joyxi_get_num_joysticks(void)
809 {
810 int result = 0, index;
811 for (index = 0; index < MAX_JOYSTICKS; index++) {
812 if (joyxi_joysticks[index].active)
813 result++;
814 }
815 return result;
816 }
817
818 static ALLEGRO_JOYSTICK *joyxi_get_joystick(int num)
819 {
820 int al_number = 0, index;
821 /* Use a linear scan, so the first active joystick ends up as the first
822 * allegro joystick etc. */
823 for (index = 0; index < MAX_JOYSTICKS; index++) {
824 if (joyxi_joysticks[index].active) {
825 if (num == al_number)
826 return &joyxi_joysticks[index].parent;
827 else
828 al_number++;
829 }
830 }
831 return NULL;
832 }
833
834
835 static void joyxi_release_joystick(ALLEGRO_JOYSTICK *joy)
836 {
837 /* No need to do anything. */
838 (void)joy;
839 }
840
841 static void joyxi_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state)
842 {
843 ALLEGRO_JOYSTICK_XINPUT *xjoy = (ALLEGRO_JOYSTICK_XINPUT *)joy;
844 ASSERT(xjoy);
845 ASSERT(ret_state);
846 /* Copy the data with the mutex
847 * locked to prevent changes during copying. */
848 al_lock_mutex(joyxi_mutex);
849 (*ret_state) = xjoy->joystate;
850 al_unlock_mutex(joyxi_mutex);
851 }
852
853
854 static const char *joyxi_get_name(ALLEGRO_JOYSTICK *joy)
855 {
856 ALLEGRO_JOYSTICK_XINPUT *xjoy = (ALLEGRO_JOYSTICK_XINPUT *)joy;
857 ASSERT(xjoy);
858 return xjoy->name;
859 }
860
861
862 static bool joyxi_get_active(ALLEGRO_JOYSTICK *joy)
863 {
864 ALLEGRO_JOYSTICK_XINPUT *xjoy = (ALLEGRO_JOYSTICK_XINPUT *)joy;
865 ASSERT(xjoy);
866 return xjoy->active;
867 }
868
869
870 #endif /* #ifdef ALLEGRO_CFG_XINPUT */