1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org> |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | #include "../SDL_internal.h" |
22 | |
23 | /* This is the joystick API for Simple DirectMedia Layer */ |
24 | |
25 | #include "SDL.h" |
26 | #include "SDL_atomic.h" |
27 | #include "SDL_events.h" |
28 | #include "SDL_sysjoystick.h" |
29 | #include "SDL_hints.h" |
30 | |
31 | #if !SDL_EVENTS_DISABLED |
32 | #include "../events/SDL_events_c.h" |
33 | #endif |
34 | #include "../video/SDL_sysvideo.h" |
35 | #include "hidapi/SDL_hidapijoystick_c.h" |
36 | |
37 | /* This is included in only one place because it has a large static list of controllers */ |
38 | #include "controller_type.h" |
39 | |
40 | #ifdef __WIN32__ |
41 | /* Needed for checking for input remapping programs */ |
42 | #include "../core/windows/SDL_windows.h" |
43 | |
44 | #undef UNICODE /* We want ASCII functions */ |
45 | #include <tlhelp32.h> |
46 | #endif |
47 | |
48 | #if SDL_JOYSTICK_VIRTUAL |
49 | #include "./virtual/SDL_virtualjoystick_c.h" |
50 | #endif |
51 | |
52 | static SDL_JoystickDriver *SDL_joystick_drivers[] = { |
53 | #ifdef SDL_JOYSTICK_HIDAPI /* Before WINDOWS_ driver, as WINDOWS wants to check if this driver is handling things */ |
54 | &SDL_HIDAPI_JoystickDriver, |
55 | #endif |
56 | #ifdef SDL_JOYSTICK_RAWINPUT /* Before WINDOWS_ driver, as WINDOWS wants to check if this driver is handling things */ |
57 | &SDL_RAWINPUT_JoystickDriver, |
58 | #endif |
59 | #if defined(SDL_JOYSTICK_WGI) |
60 | &SDL_WGI_JoystickDriver, |
61 | #endif |
62 | #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT) |
63 | &SDL_WINDOWS_JoystickDriver, |
64 | #endif |
65 | #if defined(SDL_JOYSTICK_WINMM) |
66 | &SDL_WINMM_JoystickDriver, |
67 | #endif |
68 | #ifdef SDL_JOYSTICK_LINUX |
69 | &SDL_LINUX_JoystickDriver, |
70 | #endif |
71 | #ifdef SDL_JOYSTICK_IOKIT |
72 | &SDL_DARWIN_JoystickDriver, |
73 | #endif |
74 | #if (defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)) && !defined(SDL_JOYSTICK_DISABLED) |
75 | &SDL_IOS_JoystickDriver, |
76 | #endif |
77 | #ifdef SDL_JOYSTICK_ANDROID |
78 | &SDL_ANDROID_JoystickDriver, |
79 | #endif |
80 | #ifdef SDL_JOYSTICK_EMSCRIPTEN |
81 | &SDL_EMSCRIPTEN_JoystickDriver, |
82 | #endif |
83 | #ifdef SDL_JOYSTICK_HAIKU |
84 | &SDL_HAIKU_JoystickDriver, |
85 | #endif |
86 | #ifdef SDL_JOYSTICK_USBHID /* !!! FIXME: "USBHID" is a generic name, and doubly-confusing with HIDAPI next to it. This is the *BSD interface, rename this. */ |
87 | &SDL_BSD_JoystickDriver, |
88 | #endif |
89 | #ifdef SDL_JOYSTICK_OS2 |
90 | &SDL_OS2_JoystickDriver, |
91 | #endif |
92 | #ifdef SDL_JOYSTICK_PSP |
93 | &SDL_PSP_JoystickDriver, |
94 | #endif |
95 | #ifdef SDL_JOYSTICK_VIRTUAL |
96 | &SDL_VIRTUAL_JoystickDriver, |
97 | #endif |
98 | #ifdef SDL_JOYSTICK_VITA |
99 | &SDL_VITA_JoystickDriver |
100 | #endif |
101 | #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED) |
102 | &SDL_DUMMY_JoystickDriver |
103 | #endif |
104 | }; |
105 | static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE; |
106 | static SDL_Joystick *SDL_joysticks = NULL; |
107 | static SDL_bool SDL_updating_joystick = SDL_FALSE; |
108 | static SDL_mutex *SDL_joystick_lock = NULL; /* This needs to support recursive locks */ |
109 | static SDL_atomic_t SDL_next_joystick_instance_id; |
110 | static int SDL_joystick_player_count = 0; |
111 | static SDL_JoystickID *SDL_joystick_players = NULL; |
112 | |
113 | void |
114 | SDL_LockJoysticks(void) |
115 | { |
116 | if (SDL_joystick_lock) { |
117 | SDL_LockMutex(SDL_joystick_lock); |
118 | } |
119 | } |
120 | |
121 | void |
122 | SDL_UnlockJoysticks(void) |
123 | { |
124 | if (SDL_joystick_lock) { |
125 | SDL_UnlockMutex(SDL_joystick_lock); |
126 | } |
127 | } |
128 | |
129 | static int |
130 | SDL_FindFreePlayerIndex() |
131 | { |
132 | int player_index; |
133 | |
134 | for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) { |
135 | if (SDL_joystick_players[player_index] == -1) { |
136 | return player_index; |
137 | } |
138 | } |
139 | return player_index; |
140 | } |
141 | |
142 | static int |
143 | SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id) |
144 | { |
145 | int player_index; |
146 | |
147 | for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) { |
148 | if (instance_id == SDL_joystick_players[player_index]) { |
149 | break; |
150 | } |
151 | } |
152 | if (player_index == SDL_joystick_player_count) { |
153 | player_index = -1; |
154 | } |
155 | return player_index; |
156 | } |
157 | |
158 | static SDL_JoystickID |
159 | SDL_GetJoystickIDForPlayerIndex(int player_index) |
160 | { |
161 | if (player_index < 0 || player_index >= SDL_joystick_player_count) { |
162 | return -1; |
163 | } |
164 | return SDL_joystick_players[player_index]; |
165 | } |
166 | |
167 | static SDL_bool |
168 | SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id) |
169 | { |
170 | SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index); |
171 | SDL_JoystickDriver *driver; |
172 | int device_index; |
173 | int existing_player_index; |
174 | |
175 | if (player_index >= SDL_joystick_player_count) { |
176 | SDL_JoystickID *new_players = (SDL_JoystickID *)SDL_realloc(SDL_joystick_players, (player_index + 1)*sizeof(*SDL_joystick_players)); |
177 | if (!new_players) { |
178 | SDL_OutOfMemory(); |
179 | return SDL_FALSE; |
180 | } |
181 | |
182 | SDL_joystick_players = new_players; |
183 | SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0xFF, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0])); |
184 | SDL_joystick_player_count = player_index + 1; |
185 | } else if (SDL_joystick_players[player_index] == instance_id) { |
186 | /* Joystick is already assigned the requested player index */ |
187 | return SDL_TRUE; |
188 | } |
189 | |
190 | /* Clear the old player index */ |
191 | existing_player_index = SDL_GetPlayerIndexForJoystickID(instance_id); |
192 | if (existing_player_index >= 0) { |
193 | SDL_joystick_players[existing_player_index] = -1; |
194 | } |
195 | |
196 | if (player_index >= 0) { |
197 | SDL_joystick_players[player_index] = instance_id; |
198 | } |
199 | |
200 | /* Update the driver with the new index */ |
201 | device_index = SDL_JoystickGetDeviceIndexFromInstanceID(instance_id); |
202 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
203 | driver->SetDevicePlayerIndex(device_index, player_index); |
204 | } |
205 | |
206 | /* Move any existing joystick to another slot */ |
207 | if (existing_instance >= 0) { |
208 | SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance); |
209 | } |
210 | return SDL_TRUE; |
211 | } |
212 | |
213 | static void SDLCALL |
214 | SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint) |
215 | { |
216 | if (hint && *hint == '1') { |
217 | SDL_joystick_allows_background_events = SDL_TRUE; |
218 | } else { |
219 | SDL_joystick_allows_background_events = SDL_FALSE; |
220 | } |
221 | } |
222 | |
223 | int |
224 | SDL_JoystickInit(void) |
225 | { |
226 | int i, status; |
227 | |
228 | SDL_GameControllerInitMappings(); |
229 | |
230 | /* Create the joystick list lock */ |
231 | if (!SDL_joystick_lock) { |
232 | SDL_joystick_lock = SDL_CreateMutex(); |
233 | } |
234 | |
235 | /* See if we should allow joystick events while in the background */ |
236 | SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, |
237 | SDL_JoystickAllowBackgroundEventsChanged, NULL); |
238 | |
239 | #if !SDL_EVENTS_DISABLED |
240 | if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) { |
241 | return -1; |
242 | } |
243 | #endif /* !SDL_EVENTS_DISABLED */ |
244 | |
245 | status = -1; |
246 | for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) { |
247 | if (SDL_joystick_drivers[i]->Init() >= 0) { |
248 | status = 0; |
249 | } |
250 | } |
251 | return status; |
252 | } |
253 | |
254 | /* |
255 | * Count the number of joysticks attached to the system |
256 | */ |
257 | int |
258 | SDL_NumJoysticks(void) |
259 | { |
260 | int i, total_joysticks = 0; |
261 | SDL_LockJoysticks(); |
262 | for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) { |
263 | total_joysticks += SDL_joystick_drivers[i]->GetCount(); |
264 | } |
265 | SDL_UnlockJoysticks(); |
266 | return total_joysticks; |
267 | } |
268 | |
269 | /* |
270 | * Return the next available joystick instance ID |
271 | * This may be called by drivers from multiple threads, unprotected by any locks |
272 | */ |
273 | SDL_JoystickID SDL_GetNextJoystickInstanceID() |
274 | { |
275 | return SDL_AtomicIncRef(&SDL_next_joystick_instance_id); |
276 | } |
277 | |
278 | /* |
279 | * Get the driver and device index for an API device index |
280 | * This should be called while the joystick lock is held, to prevent another thread from updating the list |
281 | */ |
282 | SDL_bool |
283 | SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index) |
284 | { |
285 | int i, num_joysticks, total_joysticks = 0; |
286 | |
287 | if (device_index >= 0) { |
288 | for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) { |
289 | num_joysticks = SDL_joystick_drivers[i]->GetCount(); |
290 | if (device_index < num_joysticks) { |
291 | *driver = SDL_joystick_drivers[i]; |
292 | *driver_index = device_index; |
293 | return SDL_TRUE; |
294 | } |
295 | device_index -= num_joysticks; |
296 | total_joysticks += num_joysticks; |
297 | } |
298 | } |
299 | |
300 | SDL_SetError("There are %d joysticks available" , total_joysticks); |
301 | return SDL_FALSE; |
302 | } |
303 | |
304 | /* |
305 | * Get the implementation dependent name of a joystick |
306 | */ |
307 | const char * |
308 | SDL_JoystickNameForIndex(int device_index) |
309 | { |
310 | SDL_JoystickDriver *driver; |
311 | const char *name = NULL; |
312 | |
313 | SDL_LockJoysticks(); |
314 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
315 | name = driver->GetDeviceName(device_index); |
316 | } |
317 | SDL_UnlockJoysticks(); |
318 | |
319 | /* FIXME: Really we should reference count this name so it doesn't go away after unlock */ |
320 | return name; |
321 | } |
322 | |
323 | /* |
324 | * Get the player index of a joystick, or -1 if it's not available |
325 | */ |
326 | int |
327 | SDL_JoystickGetDevicePlayerIndex(int device_index) |
328 | { |
329 | int player_index; |
330 | |
331 | SDL_LockJoysticks(); |
332 | player_index = SDL_GetPlayerIndexForJoystickID(SDL_JoystickGetDeviceInstanceID(device_index)); |
333 | SDL_UnlockJoysticks(); |
334 | |
335 | return player_index; |
336 | } |
337 | |
338 | /* |
339 | * Return true if this joystick is known to have all axes centered at zero |
340 | * This isn't generally needed unless the joystick never generates an initial axis value near zero, |
341 | * e.g. it's emulating axes with digital buttons |
342 | */ |
343 | static SDL_bool |
344 | SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick) |
345 | { |
346 | static Uint32 zero_centered_joysticks[] = { |
347 | MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */ |
348 | MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */ |
349 | }; |
350 | |
351 | int i; |
352 | Uint32 id = MAKE_VIDPID(SDL_JoystickGetVendor(joystick), |
353 | SDL_JoystickGetProduct(joystick)); |
354 | |
355 | /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/ |
356 | |
357 | if (joystick->naxes == 2) { |
358 | /* Assume D-pad or thumbstick style axes are centered at 0 */ |
359 | return SDL_TRUE; |
360 | } |
361 | |
362 | for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) { |
363 | if (id == zero_centered_joysticks[i]) { |
364 | return SDL_TRUE; |
365 | } |
366 | } |
367 | return SDL_FALSE; |
368 | } |
369 | |
370 | /* |
371 | * Open a joystick for use - the index passed as an argument refers to |
372 | * the N'th joystick on the system. This index is the value which will |
373 | * identify this joystick in future joystick events. |
374 | * |
375 | * This function returns a joystick identifier, or NULL if an error occurred. |
376 | */ |
377 | SDL_Joystick * |
378 | SDL_JoystickOpen(int device_index) |
379 | { |
380 | SDL_JoystickDriver *driver; |
381 | SDL_JoystickID instance_id; |
382 | SDL_Joystick *joystick; |
383 | SDL_Joystick *joysticklist; |
384 | const char *joystickname = NULL; |
385 | |
386 | SDL_LockJoysticks(); |
387 | |
388 | if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
389 | SDL_UnlockJoysticks(); |
390 | return NULL; |
391 | } |
392 | |
393 | joysticklist = SDL_joysticks; |
394 | /* If the joystick is already open, return it |
395 | * it is important that we have a single joystick * for each instance id |
396 | */ |
397 | instance_id = driver->GetDeviceInstanceID(device_index); |
398 | while (joysticklist) { |
399 | if (instance_id == joysticklist->instance_id) { |
400 | joystick = joysticklist; |
401 | ++joystick->ref_count; |
402 | SDL_UnlockJoysticks(); |
403 | return joystick; |
404 | } |
405 | joysticklist = joysticklist->next; |
406 | } |
407 | |
408 | /* Create and initialize the joystick */ |
409 | joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1); |
410 | if (joystick == NULL) { |
411 | SDL_OutOfMemory(); |
412 | SDL_UnlockJoysticks(); |
413 | return NULL; |
414 | } |
415 | joystick->driver = driver; |
416 | joystick->instance_id = instance_id; |
417 | joystick->attached = SDL_TRUE; |
418 | joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN; |
419 | joystick->led_expiration = SDL_GetTicks(); |
420 | |
421 | if (driver->Open(joystick, device_index) < 0) { |
422 | SDL_free(joystick); |
423 | SDL_UnlockJoysticks(); |
424 | return NULL; |
425 | } |
426 | |
427 | joystickname = driver->GetDeviceName(device_index); |
428 | if (joystickname) { |
429 | joystick->name = SDL_strdup(joystickname); |
430 | } else { |
431 | joystick->name = NULL; |
432 | } |
433 | |
434 | joystick->guid = driver->GetDeviceGUID(device_index); |
435 | |
436 | if (joystick->naxes > 0) { |
437 | joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo)); |
438 | } |
439 | if (joystick->nhats > 0) { |
440 | joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8)); |
441 | } |
442 | if (joystick->nballs > 0) { |
443 | joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls)); |
444 | } |
445 | if (joystick->nbuttons > 0) { |
446 | joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8)); |
447 | } |
448 | if (((joystick->naxes > 0) && !joystick->axes) |
449 | || ((joystick->nhats > 0) && !joystick->hats) |
450 | || ((joystick->nballs > 0) && !joystick->balls) |
451 | || ((joystick->nbuttons > 0) && !joystick->buttons)) { |
452 | SDL_OutOfMemory(); |
453 | SDL_JoystickClose(joystick); |
454 | SDL_UnlockJoysticks(); |
455 | return NULL; |
456 | } |
457 | |
458 | /* If this joystick is known to have all zero centered axes, skip the auto-centering code */ |
459 | if (SDL_JoystickAxesCenteredAtZero(joystick)) { |
460 | int i; |
461 | |
462 | for (i = 0; i < joystick->naxes; ++i) { |
463 | joystick->axes[i].has_initial_value = SDL_TRUE; |
464 | } |
465 | } |
466 | |
467 | joystick->is_game_controller = SDL_IsGameController(device_index); |
468 | |
469 | /* Add joystick to list */ |
470 | ++joystick->ref_count; |
471 | /* Link the joystick in the list */ |
472 | joystick->next = SDL_joysticks; |
473 | SDL_joysticks = joystick; |
474 | |
475 | SDL_UnlockJoysticks(); |
476 | |
477 | driver->Update(joystick); |
478 | |
479 | return joystick; |
480 | } |
481 | |
482 | int |
483 | SDL_JoystickAttachVirtual(SDL_JoystickType type, |
484 | int naxes, int nbuttons, int nhats) |
485 | { |
486 | #if SDL_JOYSTICK_VIRTUAL |
487 | return SDL_JoystickAttachVirtualInner(type, naxes, nbuttons, nhats); |
488 | #else |
489 | return SDL_SetError("SDL not built with virtual-joystick support" ); |
490 | #endif |
491 | } |
492 | |
493 | int |
494 | SDL_JoystickDetachVirtual(int device_index) |
495 | { |
496 | #if SDL_JOYSTICK_VIRTUAL |
497 | SDL_JoystickDriver *driver; |
498 | |
499 | SDL_LockJoysticks(); |
500 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
501 | if (driver == &SDL_VIRTUAL_JoystickDriver) { |
502 | const int result = SDL_JoystickDetachVirtualInner(device_index); |
503 | SDL_UnlockJoysticks(); |
504 | return result; |
505 | } |
506 | } |
507 | SDL_UnlockJoysticks(); |
508 | |
509 | return SDL_SetError("Virtual joystick not found at provided index" ); |
510 | #else |
511 | return SDL_SetError("SDL not built with virtual-joystick support" ); |
512 | #endif |
513 | } |
514 | |
515 | SDL_bool |
516 | SDL_JoystickIsVirtual(int device_index) |
517 | { |
518 | #if SDL_JOYSTICK_VIRTUAL |
519 | SDL_JoystickDriver *driver; |
520 | int driver_device_index; |
521 | SDL_bool is_virtual = SDL_FALSE; |
522 | |
523 | SDL_LockJoysticks(); |
524 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) { |
525 | if (driver == &SDL_VIRTUAL_JoystickDriver) { |
526 | is_virtual = SDL_TRUE; |
527 | } |
528 | } |
529 | SDL_UnlockJoysticks(); |
530 | |
531 | return is_virtual; |
532 | #else |
533 | return SDL_FALSE; |
534 | #endif |
535 | } |
536 | |
537 | int |
538 | SDL_JoystickSetVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value) |
539 | { |
540 | #if SDL_JOYSTICK_VIRTUAL |
541 | return SDL_JoystickSetVirtualAxisInner(joystick, axis, value); |
542 | #else |
543 | return SDL_SetError("SDL not built with virtual-joystick support" ); |
544 | #endif |
545 | } |
546 | |
547 | int |
548 | SDL_JoystickSetVirtualButton(SDL_Joystick *joystick, int button, Uint8 value) |
549 | { |
550 | #if SDL_JOYSTICK_VIRTUAL |
551 | return SDL_JoystickSetVirtualButtonInner(joystick, button, value); |
552 | #else |
553 | return SDL_SetError("SDL not built with virtual-joystick support" ); |
554 | #endif |
555 | } |
556 | |
557 | int |
558 | SDL_JoystickSetVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value) |
559 | { |
560 | #if SDL_JOYSTICK_VIRTUAL |
561 | return SDL_JoystickSetVirtualHatInner(joystick, hat, value); |
562 | #else |
563 | return SDL_SetError("SDL not built with virtual-joystick support" ); |
564 | #endif |
565 | } |
566 | |
567 | /* |
568 | * Checks to make sure the joystick is valid. |
569 | */ |
570 | SDL_bool |
571 | SDL_PrivateJoystickValid(SDL_Joystick *joystick) |
572 | { |
573 | SDL_bool valid; |
574 | |
575 | if (joystick == NULL) { |
576 | SDL_SetError("Joystick hasn't been opened yet" ); |
577 | valid = SDL_FALSE; |
578 | } else { |
579 | valid = SDL_TRUE; |
580 | } |
581 | |
582 | return valid; |
583 | } |
584 | |
585 | SDL_bool |
586 | SDL_PrivateJoystickGetAutoGamepadMapping(int device_index, SDL_GamepadMapping * out) |
587 | { |
588 | SDL_JoystickDriver *driver; |
589 | SDL_bool is_ok = SDL_FALSE; |
590 | |
591 | SDL_LockJoysticks(); |
592 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
593 | is_ok = driver->GetGamepadMapping(device_index, out); |
594 | } |
595 | SDL_UnlockJoysticks(); |
596 | |
597 | return is_ok; |
598 | } |
599 | |
600 | /* |
601 | * Get the number of multi-dimensional axis controls on a joystick |
602 | */ |
603 | int |
604 | SDL_JoystickNumAxes(SDL_Joystick *joystick) |
605 | { |
606 | if (!SDL_PrivateJoystickValid(joystick)) { |
607 | return -1; |
608 | } |
609 | return joystick->naxes; |
610 | } |
611 | |
612 | /* |
613 | * Get the number of hats on a joystick |
614 | */ |
615 | int |
616 | SDL_JoystickNumHats(SDL_Joystick *joystick) |
617 | { |
618 | if (!SDL_PrivateJoystickValid(joystick)) { |
619 | return -1; |
620 | } |
621 | return joystick->nhats; |
622 | } |
623 | |
624 | /* |
625 | * Get the number of trackballs on a joystick |
626 | */ |
627 | int |
628 | SDL_JoystickNumBalls(SDL_Joystick *joystick) |
629 | { |
630 | if (!SDL_PrivateJoystickValid(joystick)) { |
631 | return -1; |
632 | } |
633 | return joystick->nballs; |
634 | } |
635 | |
636 | /* |
637 | * Get the number of buttons on a joystick |
638 | */ |
639 | int |
640 | SDL_JoystickNumButtons(SDL_Joystick *joystick) |
641 | { |
642 | if (!SDL_PrivateJoystickValid(joystick)) { |
643 | return -1; |
644 | } |
645 | return joystick->nbuttons; |
646 | } |
647 | |
648 | /* |
649 | * Get the current state of an axis control on a joystick |
650 | */ |
651 | Sint16 |
652 | SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis) |
653 | { |
654 | Sint16 state; |
655 | |
656 | if (!SDL_PrivateJoystickValid(joystick)) { |
657 | return 0; |
658 | } |
659 | if (axis < joystick->naxes) { |
660 | state = joystick->axes[axis].value; |
661 | } else { |
662 | SDL_SetError("Joystick only has %d axes" , joystick->naxes); |
663 | state = 0; |
664 | } |
665 | return state; |
666 | } |
667 | |
668 | /* |
669 | * Get the initial state of an axis control on a joystick |
670 | */ |
671 | SDL_bool |
672 | SDL_JoystickGetAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state) |
673 | { |
674 | if (!SDL_PrivateJoystickValid(joystick)) { |
675 | return SDL_FALSE; |
676 | } |
677 | if (axis >= joystick->naxes) { |
678 | SDL_SetError("Joystick only has %d axes" , joystick->naxes); |
679 | return SDL_FALSE; |
680 | } |
681 | if (state) { |
682 | *state = joystick->axes[axis].initial_value; |
683 | } |
684 | return joystick->axes[axis].has_initial_value; |
685 | } |
686 | |
687 | /* |
688 | * Get the current state of a hat on a joystick |
689 | */ |
690 | Uint8 |
691 | SDL_JoystickGetHat(SDL_Joystick *joystick, int hat) |
692 | { |
693 | Uint8 state; |
694 | |
695 | if (!SDL_PrivateJoystickValid(joystick)) { |
696 | return 0; |
697 | } |
698 | if (hat < joystick->nhats) { |
699 | state = joystick->hats[hat]; |
700 | } else { |
701 | SDL_SetError("Joystick only has %d hats" , joystick->nhats); |
702 | state = 0; |
703 | } |
704 | return state; |
705 | } |
706 | |
707 | /* |
708 | * Get the ball axis change since the last poll |
709 | */ |
710 | int |
711 | SDL_JoystickGetBall(SDL_Joystick *joystick, int ball, int *dx, int *dy) |
712 | { |
713 | int retval; |
714 | |
715 | if (!SDL_PrivateJoystickValid(joystick)) { |
716 | return -1; |
717 | } |
718 | |
719 | retval = 0; |
720 | if (ball < joystick->nballs) { |
721 | if (dx) { |
722 | *dx = joystick->balls[ball].dx; |
723 | } |
724 | if (dy) { |
725 | *dy = joystick->balls[ball].dy; |
726 | } |
727 | joystick->balls[ball].dx = 0; |
728 | joystick->balls[ball].dy = 0; |
729 | } else { |
730 | return SDL_SetError("Joystick only has %d balls" , joystick->nballs); |
731 | } |
732 | return retval; |
733 | } |
734 | |
735 | /* |
736 | * Get the current state of a button on a joystick |
737 | */ |
738 | Uint8 |
739 | SDL_JoystickGetButton(SDL_Joystick *joystick, int button) |
740 | { |
741 | Uint8 state; |
742 | |
743 | if (!SDL_PrivateJoystickValid(joystick)) { |
744 | return 0; |
745 | } |
746 | if (button < joystick->nbuttons) { |
747 | state = joystick->buttons[button]; |
748 | } else { |
749 | SDL_SetError("Joystick only has %d buttons" , joystick->nbuttons); |
750 | state = 0; |
751 | } |
752 | return state; |
753 | } |
754 | |
755 | /* |
756 | * Return if the joystick in question is currently attached to the system, |
757 | * \return SDL_FALSE if not plugged in, SDL_TRUE if still present. |
758 | */ |
759 | SDL_bool |
760 | SDL_JoystickGetAttached(SDL_Joystick *joystick) |
761 | { |
762 | if (!SDL_PrivateJoystickValid(joystick)) { |
763 | return SDL_FALSE; |
764 | } |
765 | |
766 | return joystick->attached; |
767 | } |
768 | |
769 | /* |
770 | * Get the instance id for this opened joystick |
771 | */ |
772 | SDL_JoystickID |
773 | SDL_JoystickInstanceID(SDL_Joystick *joystick) |
774 | { |
775 | if (!SDL_PrivateJoystickValid(joystick)) { |
776 | return -1; |
777 | } |
778 | |
779 | return joystick->instance_id; |
780 | } |
781 | |
782 | /* |
783 | * Return the SDL_Joystick associated with an instance id. |
784 | */ |
785 | SDL_Joystick * |
786 | SDL_JoystickFromInstanceID(SDL_JoystickID instance_id) |
787 | { |
788 | SDL_Joystick *joystick; |
789 | |
790 | SDL_LockJoysticks(); |
791 | for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { |
792 | if (joystick->instance_id == instance_id) { |
793 | break; |
794 | } |
795 | } |
796 | SDL_UnlockJoysticks(); |
797 | return joystick; |
798 | } |
799 | |
800 | /** |
801 | * Return the SDL_Joystick associated with a player index. |
802 | */ |
803 | SDL_Joystick * |
804 | SDL_JoystickFromPlayerIndex(int player_index) |
805 | { |
806 | SDL_JoystickID instance_id; |
807 | SDL_Joystick *joystick; |
808 | |
809 | SDL_LockJoysticks(); |
810 | instance_id = SDL_GetJoystickIDForPlayerIndex(player_index); |
811 | for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { |
812 | if (joystick->instance_id == instance_id) { |
813 | break; |
814 | } |
815 | } |
816 | SDL_UnlockJoysticks(); |
817 | return joystick; |
818 | } |
819 | |
820 | /* |
821 | * Get the friendly name of this joystick |
822 | */ |
823 | const char * |
824 | SDL_JoystickName(SDL_Joystick *joystick) |
825 | { |
826 | if (!SDL_PrivateJoystickValid(joystick)) { |
827 | return NULL; |
828 | } |
829 | |
830 | return joystick->name; |
831 | } |
832 | |
833 | /** |
834 | * Get the player index of an opened joystick, or -1 if it's not available |
835 | */ |
836 | int |
837 | SDL_JoystickGetPlayerIndex(SDL_Joystick *joystick) |
838 | { |
839 | int player_index; |
840 | |
841 | if (!SDL_PrivateJoystickValid(joystick)) { |
842 | return -1; |
843 | } |
844 | |
845 | SDL_LockJoysticks(); |
846 | player_index = SDL_GetPlayerIndexForJoystickID(joystick->instance_id); |
847 | SDL_UnlockJoysticks(); |
848 | |
849 | return player_index; |
850 | } |
851 | |
852 | /** |
853 | * Set the player index of an opened joystick |
854 | */ |
855 | void |
856 | SDL_JoystickSetPlayerIndex(SDL_Joystick *joystick, int player_index) |
857 | { |
858 | if (!SDL_PrivateJoystickValid(joystick)) { |
859 | return; |
860 | } |
861 | |
862 | SDL_LockJoysticks(); |
863 | SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id); |
864 | SDL_UnlockJoysticks(); |
865 | } |
866 | |
867 | int |
868 | SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms) |
869 | { |
870 | int result; |
871 | |
872 | if (!SDL_PrivateJoystickValid(joystick)) { |
873 | return -1; |
874 | } |
875 | |
876 | SDL_LockJoysticks(); |
877 | if (low_frequency_rumble == joystick->low_frequency_rumble && |
878 | high_frequency_rumble == joystick->high_frequency_rumble) { |
879 | /* Just update the expiration */ |
880 | result = 0; |
881 | } else { |
882 | result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble); |
883 | } |
884 | |
885 | /* Save the rumble value regardless of success, so we don't spam the driver */ |
886 | joystick->low_frequency_rumble = low_frequency_rumble; |
887 | joystick->high_frequency_rumble = high_frequency_rumble; |
888 | |
889 | if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) { |
890 | joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS); |
891 | if (!joystick->rumble_expiration) { |
892 | joystick->rumble_expiration = 1; |
893 | } |
894 | } else { |
895 | joystick->rumble_expiration = 0; |
896 | } |
897 | SDL_UnlockJoysticks(); |
898 | |
899 | return result; |
900 | } |
901 | |
902 | int |
903 | SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms) |
904 | { |
905 | int result; |
906 | |
907 | if (!SDL_PrivateJoystickValid(joystick)) { |
908 | return -1; |
909 | } |
910 | |
911 | SDL_LockJoysticks(); |
912 | if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) { |
913 | /* Just update the expiration */ |
914 | result = 0; |
915 | } else { |
916 | result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble); |
917 | } |
918 | |
919 | /* Save the rumble value regardless of success, so we don't spam the driver */ |
920 | joystick->left_trigger_rumble = left_rumble; |
921 | joystick->right_trigger_rumble = right_rumble; |
922 | |
923 | if ((left_rumble || right_rumble) && duration_ms) { |
924 | joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS); |
925 | if (!joystick->trigger_rumble_expiration) { |
926 | joystick->trigger_rumble_expiration = 1; |
927 | } |
928 | } else { |
929 | joystick->trigger_rumble_expiration = 0; |
930 | } |
931 | SDL_UnlockJoysticks(); |
932 | |
933 | return result; |
934 | } |
935 | |
936 | SDL_bool |
937 | SDL_JoystickHasLED(SDL_Joystick *joystick) |
938 | { |
939 | SDL_bool result; |
940 | |
941 | if (!SDL_PrivateJoystickValid(joystick)) { |
942 | return SDL_FALSE; |
943 | } |
944 | |
945 | SDL_LockJoysticks(); |
946 | |
947 | result = joystick->driver->HasLED(joystick); |
948 | |
949 | SDL_UnlockJoysticks(); |
950 | |
951 | return result; |
952 | } |
953 | |
954 | int |
955 | SDL_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue) |
956 | { |
957 | int result; |
958 | SDL_bool isfreshvalue; |
959 | |
960 | if (!SDL_PrivateJoystickValid(joystick)) { |
961 | return -1; |
962 | } |
963 | |
964 | SDL_LockJoysticks(); |
965 | |
966 | isfreshvalue = red != joystick->led_red || |
967 | green != joystick->led_green || |
968 | blue != joystick->led_blue; |
969 | |
970 | if ( isfreshvalue || SDL_TICKS_PASSED( SDL_GetTicks(), joystick->led_expiration ) ) { |
971 | result = joystick->driver->SetLED(joystick, red, green, blue); |
972 | joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS; |
973 | } |
974 | else { |
975 | /* Avoid spamming the driver */ |
976 | result = 0; |
977 | } |
978 | |
979 | /* Save the LED value regardless of success, so we don't spam the driver */ |
980 | joystick->led_red = red; |
981 | joystick->led_green = green; |
982 | joystick->led_blue = blue; |
983 | |
984 | SDL_UnlockJoysticks(); |
985 | |
986 | return result; |
987 | } |
988 | |
989 | /* |
990 | * Close a joystick previously opened with SDL_JoystickOpen() |
991 | */ |
992 | void |
993 | SDL_JoystickClose(SDL_Joystick *joystick) |
994 | { |
995 | SDL_Joystick *joysticklist; |
996 | SDL_Joystick *joysticklistprev; |
997 | int i; |
998 | |
999 | if (!SDL_PrivateJoystickValid(joystick)) { |
1000 | return; |
1001 | } |
1002 | |
1003 | SDL_LockJoysticks(); |
1004 | |
1005 | /* First decrement ref count */ |
1006 | if (--joystick->ref_count > 0) { |
1007 | SDL_UnlockJoysticks(); |
1008 | return; |
1009 | } |
1010 | |
1011 | if (SDL_updating_joystick) { |
1012 | SDL_UnlockJoysticks(); |
1013 | return; |
1014 | } |
1015 | |
1016 | if (joystick->rumble_expiration) { |
1017 | SDL_JoystickRumble(joystick, 0, 0, 0); |
1018 | } |
1019 | if (joystick->trigger_rumble_expiration) { |
1020 | SDL_JoystickRumbleTriggers(joystick, 0, 0, 0); |
1021 | } |
1022 | |
1023 | joystick->driver->Close(joystick); |
1024 | joystick->hwdata = NULL; |
1025 | |
1026 | joysticklist = SDL_joysticks; |
1027 | joysticklistprev = NULL; |
1028 | while (joysticklist) { |
1029 | if (joystick == joysticklist) { |
1030 | if (joysticklistprev) { |
1031 | /* unlink this entry */ |
1032 | joysticklistprev->next = joysticklist->next; |
1033 | } else { |
1034 | SDL_joysticks = joystick->next; |
1035 | } |
1036 | break; |
1037 | } |
1038 | joysticklistprev = joysticklist; |
1039 | joysticklist = joysticklist->next; |
1040 | } |
1041 | |
1042 | SDL_free(joystick->name); |
1043 | SDL_free(joystick->serial); |
1044 | |
1045 | /* Free the data associated with this joystick */ |
1046 | SDL_free(joystick->axes); |
1047 | SDL_free(joystick->hats); |
1048 | SDL_free(joystick->balls); |
1049 | SDL_free(joystick->buttons); |
1050 | for (i = 0; i < joystick->ntouchpads; i++) { |
1051 | SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i]; |
1052 | SDL_free(touchpad->fingers); |
1053 | } |
1054 | SDL_free(joystick->touchpads); |
1055 | SDL_free(joystick->sensors); |
1056 | SDL_free(joystick); |
1057 | |
1058 | SDL_UnlockJoysticks(); |
1059 | } |
1060 | |
1061 | void |
1062 | SDL_JoystickQuit(void) |
1063 | { |
1064 | int i; |
1065 | |
1066 | /* Make sure we're not getting called in the middle of updating joysticks */ |
1067 | SDL_LockJoysticks(); |
1068 | while (SDL_updating_joystick) { |
1069 | SDL_UnlockJoysticks(); |
1070 | SDL_Delay(1); |
1071 | SDL_LockJoysticks(); |
1072 | } |
1073 | |
1074 | /* Stop the event polling */ |
1075 | while (SDL_joysticks) { |
1076 | SDL_joysticks->ref_count = 1; |
1077 | SDL_JoystickClose(SDL_joysticks); |
1078 | } |
1079 | |
1080 | /* Quit the joystick setup */ |
1081 | for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) { |
1082 | SDL_joystick_drivers[i]->Quit(); |
1083 | } |
1084 | |
1085 | if (SDL_joystick_players) { |
1086 | SDL_free(SDL_joystick_players); |
1087 | SDL_joystick_players = NULL; |
1088 | SDL_joystick_player_count = 0; |
1089 | } |
1090 | SDL_UnlockJoysticks(); |
1091 | |
1092 | #if !SDL_EVENTS_DISABLED |
1093 | SDL_QuitSubSystem(SDL_INIT_EVENTS); |
1094 | #endif |
1095 | |
1096 | SDL_DelHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, |
1097 | SDL_JoystickAllowBackgroundEventsChanged, NULL); |
1098 | |
1099 | if (SDL_joystick_lock) { |
1100 | SDL_mutex *mutex = SDL_joystick_lock; |
1101 | SDL_joystick_lock = NULL; |
1102 | SDL_DestroyMutex(mutex); |
1103 | } |
1104 | |
1105 | SDL_GameControllerQuitMappings(); |
1106 | } |
1107 | |
1108 | |
1109 | static SDL_bool |
1110 | SDL_PrivateJoystickShouldIgnoreEvent() |
1111 | { |
1112 | if (SDL_joystick_allows_background_events) { |
1113 | return SDL_FALSE; |
1114 | } |
1115 | |
1116 | if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) { |
1117 | /* We have windows but we don't have focus, ignore the event. */ |
1118 | return SDL_TRUE; |
1119 | } |
1120 | return SDL_FALSE; |
1121 | } |
1122 | |
1123 | /* These are global for SDL_sysjoystick.c and SDL_events.c */ |
1124 | |
1125 | void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers) |
1126 | { |
1127 | int ntouchpads = joystick->ntouchpads + 1; |
1128 | SDL_JoystickTouchpadInfo *touchpads = (SDL_JoystickTouchpadInfo *)SDL_realloc(joystick->touchpads, (ntouchpads * sizeof(SDL_JoystickTouchpadInfo))); |
1129 | if (touchpads) { |
1130 | SDL_JoystickTouchpadInfo *touchpad = &touchpads[ntouchpads - 1]; |
1131 | SDL_JoystickTouchpadFingerInfo *fingers = (SDL_JoystickTouchpadFingerInfo *)SDL_calloc(nfingers, sizeof(SDL_JoystickTouchpadFingerInfo)); |
1132 | |
1133 | if (fingers) { |
1134 | touchpad->nfingers = nfingers; |
1135 | touchpad->fingers = fingers; |
1136 | } else { |
1137 | /* Out of memory, this touchpad won't be active */ |
1138 | touchpad->nfingers = 0; |
1139 | touchpad->fingers = NULL; |
1140 | } |
1141 | |
1142 | joystick->ntouchpads = ntouchpads; |
1143 | joystick->touchpads = touchpads; |
1144 | } |
1145 | } |
1146 | |
1147 | void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type) |
1148 | { |
1149 | int nsensors = joystick->nsensors + 1; |
1150 | SDL_JoystickSensorInfo *sensors = (SDL_JoystickSensorInfo *)SDL_realloc(joystick->sensors, (nsensors * sizeof(SDL_JoystickSensorInfo))); |
1151 | if (sensors) { |
1152 | SDL_JoystickSensorInfo *sensor = &sensors[nsensors - 1]; |
1153 | |
1154 | SDL_zerop(sensor); |
1155 | sensor->type = type; |
1156 | |
1157 | joystick->nsensors = nsensors; |
1158 | joystick->sensors = sensors; |
1159 | } |
1160 | } |
1161 | |
1162 | void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance) |
1163 | { |
1164 | SDL_JoystickDriver *driver; |
1165 | int driver_device_index; |
1166 | int player_index = -1; |
1167 | int device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance); |
1168 | if (device_index < 0) { |
1169 | return; |
1170 | } |
1171 | |
1172 | SDL_LockJoysticks(); |
1173 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) { |
1174 | player_index = driver->GetDevicePlayerIndex(driver_device_index); |
1175 | } |
1176 | if (player_index < 0 && SDL_IsGameController(device_index)) { |
1177 | player_index = SDL_FindFreePlayerIndex(); |
1178 | } |
1179 | if (player_index >= 0) { |
1180 | SDL_SetJoystickIDForPlayerIndex(player_index, device_instance); |
1181 | } |
1182 | SDL_UnlockJoysticks(); |
1183 | |
1184 | #if !SDL_EVENTS_DISABLED |
1185 | { |
1186 | SDL_Event event; |
1187 | |
1188 | event.type = SDL_JOYDEVICEADDED; |
1189 | |
1190 | if (SDL_GetEventState(event.type) == SDL_ENABLE) { |
1191 | event.jdevice.which = device_index; |
1192 | SDL_PushEvent(&event); |
1193 | } |
1194 | } |
1195 | #endif /* !SDL_EVENTS_DISABLED */ |
1196 | } |
1197 | |
1198 | /* |
1199 | * If there is an existing add event in the queue, it needs to be modified |
1200 | * to have the right value for which, because the number of controllers in |
1201 | * the system is now one less. |
1202 | */ |
1203 | static void UpdateEventsForDeviceRemoval(int device_index) |
1204 | { |
1205 | int i, num_events; |
1206 | SDL_Event *events; |
1207 | SDL_bool isstack; |
1208 | |
1209 | num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED); |
1210 | if (num_events <= 0) { |
1211 | return; |
1212 | } |
1213 | |
1214 | events = SDL_small_alloc(SDL_Event, num_events, &isstack); |
1215 | if (!events) { |
1216 | return; |
1217 | } |
1218 | |
1219 | num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED); |
1220 | for (i = 0; i < num_events; ++i) { |
1221 | if (events[i].cdevice.which < device_index) { |
1222 | /* No change for index values lower than the removed device */ |
1223 | } |
1224 | else if (events[i].cdevice.which == device_index) { |
1225 | /* Drop this event entirely */ |
1226 | SDL_memmove(&events[i], &events[i + 1], sizeof(*events) * (num_events - (i + 1))); |
1227 | --num_events; |
1228 | --i; |
1229 | } |
1230 | else { |
1231 | /* Fix up the device index if greater than the removed device */ |
1232 | --events[i].cdevice.which; |
1233 | } |
1234 | } |
1235 | SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0); |
1236 | |
1237 | SDL_small_free(events, isstack); |
1238 | } |
1239 | |
1240 | static void |
1241 | SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick) |
1242 | { |
1243 | int i, j; |
1244 | |
1245 | /* Tell the app that everything is centered/unpressed... */ |
1246 | for (i = 0; i < joystick->naxes; i++) { |
1247 | if (joystick->axes[i].has_initial_value) { |
1248 | SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero); |
1249 | } |
1250 | } |
1251 | |
1252 | for (i = 0; i < joystick->nbuttons; i++) { |
1253 | SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED); |
1254 | } |
1255 | |
1256 | for (i = 0; i < joystick->nhats; i++) { |
1257 | SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED); |
1258 | } |
1259 | |
1260 | for (i = 0; i < joystick->ntouchpads; i++) { |
1261 | SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i]; |
1262 | |
1263 | for (j = 0; j < touchpad->nfingers; ++j) { |
1264 | SDL_PrivateJoystickTouchpad(joystick, i, j, SDL_RELEASED, 0.0f, 0.0f, 0.0f); |
1265 | } |
1266 | } |
1267 | } |
1268 | |
1269 | void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance) |
1270 | { |
1271 | SDL_Joystick *joystick = NULL; |
1272 | int player_index; |
1273 | int device_index; |
1274 | #if !SDL_EVENTS_DISABLED |
1275 | SDL_Event event; |
1276 | #endif |
1277 | |
1278 | /* Find this joystick... */ |
1279 | device_index = 0; |
1280 | for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { |
1281 | if (joystick->instance_id == device_instance) { |
1282 | SDL_PrivateJoystickForceRecentering(joystick); |
1283 | joystick->attached = SDL_FALSE; |
1284 | break; |
1285 | } |
1286 | |
1287 | ++device_index; |
1288 | } |
1289 | |
1290 | #if !SDL_EVENTS_DISABLED |
1291 | SDL_zero(event); |
1292 | event.type = SDL_JOYDEVICEREMOVED; |
1293 | |
1294 | if (SDL_GetEventState(event.type) == SDL_ENABLE) { |
1295 | event.jdevice.which = device_instance; |
1296 | SDL_PushEvent(&event); |
1297 | } |
1298 | |
1299 | UpdateEventsForDeviceRemoval(device_index); |
1300 | #endif /* !SDL_EVENTS_DISABLED */ |
1301 | |
1302 | SDL_LockJoysticks(); |
1303 | player_index = SDL_GetPlayerIndexForJoystickID(device_instance); |
1304 | if (player_index >= 0) { |
1305 | SDL_joystick_players[player_index] = -1; |
1306 | } |
1307 | SDL_UnlockJoysticks(); |
1308 | } |
1309 | |
1310 | int |
1311 | SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value) |
1312 | { |
1313 | int posted; |
1314 | SDL_JoystickAxisInfo *info; |
1315 | |
1316 | /* Make sure we're not getting garbage or duplicate events */ |
1317 | if (axis >= joystick->naxes) { |
1318 | return 0; |
1319 | } |
1320 | |
1321 | info = &joystick->axes[axis]; |
1322 | if (!info->has_initial_value || |
1323 | (!info->has_second_value && (info->initial_value <= -32767 || info->initial_value == 32767) && SDL_abs(value) < (SDL_JOYSTICK_AXIS_MAX / 4))) { |
1324 | info->initial_value = value; |
1325 | info->value = value; |
1326 | info->zero = value; |
1327 | info->has_initial_value = SDL_TRUE; |
1328 | } else if (value == info->value) { |
1329 | return 0; |
1330 | } else { |
1331 | info->has_second_value = SDL_TRUE; |
1332 | } |
1333 | if (!info->sent_initial_value) { |
1334 | /* Make sure we don't send motion until there's real activity on this axis */ |
1335 | const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */ |
1336 | if (SDL_abs(value - info->value) <= MAX_ALLOWED_JITTER) { |
1337 | return 0; |
1338 | } |
1339 | info->sent_initial_value = SDL_TRUE; |
1340 | info->value = ~value; /* Just so we pass the check above */ |
1341 | SDL_PrivateJoystickAxis(joystick, axis, info->initial_value); |
1342 | } |
1343 | |
1344 | /* We ignore events if we don't have keyboard focus, except for centering |
1345 | * events. |
1346 | */ |
1347 | if (SDL_PrivateJoystickShouldIgnoreEvent()) { |
1348 | if ((value > info->zero && value >= info->value) || |
1349 | (value < info->zero && value <= info->value)) { |
1350 | return 0; |
1351 | } |
1352 | } |
1353 | |
1354 | /* Update internal joystick state */ |
1355 | info->value = value; |
1356 | |
1357 | /* Post the event, if desired */ |
1358 | posted = 0; |
1359 | #if !SDL_EVENTS_DISABLED |
1360 | if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) { |
1361 | SDL_Event event; |
1362 | event.type = SDL_JOYAXISMOTION; |
1363 | event.jaxis.which = joystick->instance_id; |
1364 | event.jaxis.axis = axis; |
1365 | event.jaxis.value = value; |
1366 | posted = SDL_PushEvent(&event) == 1; |
1367 | } |
1368 | #endif /* !SDL_EVENTS_DISABLED */ |
1369 | return posted; |
1370 | } |
1371 | |
1372 | int |
1373 | SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value) |
1374 | { |
1375 | int posted; |
1376 | |
1377 | /* Make sure we're not getting garbage or duplicate events */ |
1378 | if (hat >= joystick->nhats) { |
1379 | return 0; |
1380 | } |
1381 | if (value == joystick->hats[hat]) { |
1382 | return 0; |
1383 | } |
1384 | |
1385 | /* We ignore events if we don't have keyboard focus, except for centering |
1386 | * events. |
1387 | */ |
1388 | if (SDL_PrivateJoystickShouldIgnoreEvent()) { |
1389 | if (value != SDL_HAT_CENTERED) { |
1390 | return 0; |
1391 | } |
1392 | } |
1393 | |
1394 | /* Update internal joystick state */ |
1395 | joystick->hats[hat] = value; |
1396 | |
1397 | /* Post the event, if desired */ |
1398 | posted = 0; |
1399 | #if !SDL_EVENTS_DISABLED |
1400 | if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) { |
1401 | SDL_Event event; |
1402 | event.jhat.type = SDL_JOYHATMOTION; |
1403 | event.jhat.which = joystick->instance_id; |
1404 | event.jhat.hat = hat; |
1405 | event.jhat.value = value; |
1406 | posted = SDL_PushEvent(&event) == 1; |
1407 | } |
1408 | #endif /* !SDL_EVENTS_DISABLED */ |
1409 | return posted; |
1410 | } |
1411 | |
1412 | int |
1413 | SDL_PrivateJoystickBall(SDL_Joystick *joystick, Uint8 ball, |
1414 | Sint16 xrel, Sint16 yrel) |
1415 | { |
1416 | int posted; |
1417 | |
1418 | /* Make sure we're not getting garbage events */ |
1419 | if (ball >= joystick->nballs) { |
1420 | return 0; |
1421 | } |
1422 | |
1423 | /* We ignore events if we don't have keyboard focus. */ |
1424 | if (SDL_PrivateJoystickShouldIgnoreEvent()) { |
1425 | return 0; |
1426 | } |
1427 | |
1428 | /* Update internal mouse state */ |
1429 | joystick->balls[ball].dx += xrel; |
1430 | joystick->balls[ball].dy += yrel; |
1431 | |
1432 | /* Post the event, if desired */ |
1433 | posted = 0; |
1434 | #if !SDL_EVENTS_DISABLED |
1435 | if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) { |
1436 | SDL_Event event; |
1437 | event.jball.type = SDL_JOYBALLMOTION; |
1438 | event.jball.which = joystick->instance_id; |
1439 | event.jball.ball = ball; |
1440 | event.jball.xrel = xrel; |
1441 | event.jball.yrel = yrel; |
1442 | posted = SDL_PushEvent(&event) == 1; |
1443 | } |
1444 | #endif /* !SDL_EVENTS_DISABLED */ |
1445 | return posted; |
1446 | } |
1447 | |
1448 | int |
1449 | SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state) |
1450 | { |
1451 | int posted; |
1452 | #if !SDL_EVENTS_DISABLED |
1453 | SDL_Event event; |
1454 | |
1455 | switch (state) { |
1456 | case SDL_PRESSED: |
1457 | event.type = SDL_JOYBUTTONDOWN; |
1458 | break; |
1459 | case SDL_RELEASED: |
1460 | event.type = SDL_JOYBUTTONUP; |
1461 | break; |
1462 | default: |
1463 | /* Invalid state -- bail */ |
1464 | return 0; |
1465 | } |
1466 | #endif /* !SDL_EVENTS_DISABLED */ |
1467 | |
1468 | /* Make sure we're not getting garbage or duplicate events */ |
1469 | if (button >= joystick->nbuttons) { |
1470 | return 0; |
1471 | } |
1472 | if (state == joystick->buttons[button]) { |
1473 | return 0; |
1474 | } |
1475 | |
1476 | /* We ignore events if we don't have keyboard focus, except for button |
1477 | * release. */ |
1478 | if (SDL_PrivateJoystickShouldIgnoreEvent()) { |
1479 | if (state == SDL_PRESSED) { |
1480 | return 0; |
1481 | } |
1482 | } |
1483 | |
1484 | /* Update internal joystick state */ |
1485 | joystick->buttons[button] = state; |
1486 | |
1487 | /* Post the event, if desired */ |
1488 | posted = 0; |
1489 | #if !SDL_EVENTS_DISABLED |
1490 | if (SDL_GetEventState(event.type) == SDL_ENABLE) { |
1491 | event.jbutton.which = joystick->instance_id; |
1492 | event.jbutton.button = button; |
1493 | event.jbutton.state = state; |
1494 | posted = SDL_PushEvent(&event) == 1; |
1495 | } |
1496 | #endif /* !SDL_EVENTS_DISABLED */ |
1497 | return posted; |
1498 | } |
1499 | |
1500 | void |
1501 | SDL_JoystickUpdate(void) |
1502 | { |
1503 | int i; |
1504 | SDL_Joystick *joystick, *next; |
1505 | |
1506 | if (!SDL_WasInit(SDL_INIT_JOYSTICK)) { |
1507 | return; |
1508 | } |
1509 | |
1510 | SDL_LockJoysticks(); |
1511 | |
1512 | if (SDL_updating_joystick) { |
1513 | /* The joysticks are already being updated */ |
1514 | SDL_UnlockJoysticks(); |
1515 | return; |
1516 | } |
1517 | |
1518 | SDL_updating_joystick = SDL_TRUE; |
1519 | |
1520 | /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */ |
1521 | SDL_UnlockJoysticks(); |
1522 | |
1523 | #ifdef SDL_JOYSTICK_HIDAPI |
1524 | /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */ |
1525 | HIDAPI_UpdateDevices(); |
1526 | #endif /* SDL_JOYSTICK_HIDAPI */ |
1527 | |
1528 | for (joystick = SDL_joysticks; joystick; joystick = joystick->next) { |
1529 | if (joystick->attached) { |
1530 | /* This should always be true, but seeing a crash in the wild...? */ |
1531 | if (joystick->driver) { |
1532 | joystick->driver->Update(joystick); |
1533 | } |
1534 | |
1535 | if (joystick->delayed_guide_button) { |
1536 | SDL_GameControllerHandleDelayedGuideButton(joystick); |
1537 | } |
1538 | } |
1539 | |
1540 | if (joystick->rumble_expiration) { |
1541 | SDL_LockJoysticks(); |
1542 | /* Double check now that the lock is held */ |
1543 | if (joystick->rumble_expiration && |
1544 | SDL_TICKS_PASSED(SDL_GetTicks(), joystick->rumble_expiration)) { |
1545 | SDL_JoystickRumble(joystick, 0, 0, 0); |
1546 | } |
1547 | SDL_UnlockJoysticks(); |
1548 | } |
1549 | |
1550 | if (joystick->trigger_rumble_expiration) { |
1551 | SDL_LockJoysticks(); |
1552 | /* Double check now that the lock is held */ |
1553 | if (joystick->trigger_rumble_expiration && |
1554 | SDL_TICKS_PASSED(SDL_GetTicks(), joystick->trigger_rumble_expiration)) { |
1555 | SDL_JoystickRumbleTriggers(joystick, 0, 0, 0); |
1556 | } |
1557 | SDL_UnlockJoysticks(); |
1558 | } |
1559 | } |
1560 | |
1561 | SDL_LockJoysticks(); |
1562 | |
1563 | SDL_updating_joystick = SDL_FALSE; |
1564 | |
1565 | /* If any joysticks were closed while updating, free them here */ |
1566 | for (joystick = SDL_joysticks; joystick; joystick = next) { |
1567 | next = joystick->next; |
1568 | if (joystick->ref_count <= 0) { |
1569 | SDL_JoystickClose(joystick); |
1570 | } |
1571 | } |
1572 | |
1573 | /* this needs to happen AFTER walking the joystick list above, so that any |
1574 | dangling hardware data from removed devices can be free'd |
1575 | */ |
1576 | for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) { |
1577 | SDL_joystick_drivers[i]->Detect(); |
1578 | } |
1579 | |
1580 | SDL_UnlockJoysticks(); |
1581 | } |
1582 | |
1583 | int |
1584 | SDL_JoystickEventState(int state) |
1585 | { |
1586 | #if SDL_EVENTS_DISABLED |
1587 | return SDL_DISABLE; |
1588 | #else |
1589 | const Uint32 event_list[] = { |
1590 | SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION, |
1591 | SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED |
1592 | }; |
1593 | unsigned int i; |
1594 | |
1595 | switch (state) { |
1596 | case SDL_QUERY: |
1597 | state = SDL_DISABLE; |
1598 | for (i = 0; i < SDL_arraysize(event_list); ++i) { |
1599 | state = SDL_EventState(event_list[i], SDL_QUERY); |
1600 | if (state == SDL_ENABLE) { |
1601 | break; |
1602 | } |
1603 | } |
1604 | break; |
1605 | default: |
1606 | for (i = 0; i < SDL_arraysize(event_list); ++i) { |
1607 | SDL_EventState(event_list[i], state); |
1608 | } |
1609 | break; |
1610 | } |
1611 | return state; |
1612 | #endif /* SDL_EVENTS_DISABLED */ |
1613 | } |
1614 | |
1615 | void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version) |
1616 | { |
1617 | Uint16 *guid16 = (Uint16 *)guid.data; |
1618 | |
1619 | /* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */ |
1620 | if (/* guid16[0] is device bus type */ |
1621 | guid16[1] == 0x0000 && |
1622 | /* guid16[2] is vendor ID */ |
1623 | guid16[3] == 0x0000 && |
1624 | /* guid16[4] is product ID */ |
1625 | guid16[5] == 0x0000 |
1626 | /* guid16[6] is product version */ |
1627 | ) { |
1628 | if (vendor) { |
1629 | *vendor = guid16[2]; |
1630 | } |
1631 | if (product) { |
1632 | *product = guid16[4]; |
1633 | } |
1634 | if (version) { |
1635 | *version = guid16[6]; |
1636 | } |
1637 | } else { |
1638 | if (vendor) { |
1639 | *vendor = 0; |
1640 | } |
1641 | if (product) { |
1642 | *product = 0; |
1643 | } |
1644 | if (version) { |
1645 | *version = 0; |
1646 | } |
1647 | } |
1648 | } |
1649 | |
1650 | static int |
1651 | PrefixMatch(const char *a, const char *b) |
1652 | { |
1653 | int matchlen = 0; |
1654 | while (*a && *b) { |
1655 | if (SDL_tolower(*a++) == SDL_tolower(*b++)) { |
1656 | ++matchlen; |
1657 | } else { |
1658 | break; |
1659 | } |
1660 | } |
1661 | return matchlen; |
1662 | } |
1663 | |
1664 | char * |
1665 | SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name) |
1666 | { |
1667 | static struct { |
1668 | const char *prefix; |
1669 | const char *replacement; |
1670 | } replacements[] = { |
1671 | { "NVIDIA Corporation " , "" }, |
1672 | { "Performance Designed Products" , "PDP" }, |
1673 | { "HORI CO.,LTD." , "HORI" }, |
1674 | { "HORI CO.,LTD" , "HORI" }, |
1675 | }; |
1676 | const char *custom_name; |
1677 | char *name; |
1678 | size_t i, len; |
1679 | |
1680 | custom_name = GuessControllerName(vendor, product); |
1681 | if (custom_name) { |
1682 | return SDL_strdup(custom_name); |
1683 | } |
1684 | |
1685 | if (!vendor_name) { |
1686 | vendor_name = "" ; |
1687 | } |
1688 | if (!product_name) { |
1689 | product_name = "" ; |
1690 | } |
1691 | |
1692 | while (*vendor_name == ' ') { |
1693 | ++vendor_name; |
1694 | } |
1695 | while (*product_name == ' ') { |
1696 | ++product_name; |
1697 | } |
1698 | |
1699 | if (*vendor_name && *product_name) { |
1700 | len = (SDL_strlen(vendor_name) + 1 + SDL_strlen(product_name) + 1); |
1701 | name = (char *)SDL_malloc(len); |
1702 | if (!name) { |
1703 | return NULL; |
1704 | } |
1705 | SDL_snprintf(name, len, "%s %s" , vendor_name, product_name); |
1706 | } else if (*product_name) { |
1707 | name = SDL_strdup(product_name); |
1708 | } else if (vendor || product) { |
1709 | len = (6 + 1 + 6 + 1); |
1710 | name = (char *)SDL_malloc(len); |
1711 | if (!name) { |
1712 | return NULL; |
1713 | } |
1714 | SDL_snprintf(name, len, "0x%.4x/0x%.4x" , vendor, product); |
1715 | } else { |
1716 | name = SDL_strdup("Controller" ); |
1717 | } |
1718 | |
1719 | /* Trim trailing whitespace */ |
1720 | for (len = SDL_strlen(name); (len > 0 && name[len - 1] == ' '); --len) { |
1721 | /* continue */ |
1722 | } |
1723 | name[len] = '\0'; |
1724 | |
1725 | /* Compress duplicate spaces */ |
1726 | for (i = 0; i < (len - 1); ) { |
1727 | if (name[i] == ' ' && name[i+1] == ' ') { |
1728 | SDL_memmove(&name[i], &name[i+1], (len - i)); |
1729 | --len; |
1730 | } else { |
1731 | ++i; |
1732 | } |
1733 | } |
1734 | |
1735 | /* Remove duplicate manufacturer or product in the name */ |
1736 | for (i = 1; i < (len - 1); ++i) { |
1737 | int matchlen = PrefixMatch(name, &name[i]); |
1738 | if (matchlen > 0 && name[matchlen-1] == ' ') { |
1739 | SDL_memmove(name, name+matchlen, len-matchlen+1); |
1740 | len -= matchlen; |
1741 | break; |
1742 | } else if (matchlen > 0 && name[matchlen] == ' ') { |
1743 | SDL_memmove(name, name+matchlen+1, len-matchlen); |
1744 | len -= (matchlen + 1); |
1745 | break; |
1746 | } |
1747 | } |
1748 | |
1749 | /* Perform any manufacturer replacements */ |
1750 | for (i = 0; i < SDL_arraysize(replacements); ++i) { |
1751 | size_t prefixlen = SDL_strlen(replacements[i].prefix); |
1752 | if (SDL_strncasecmp(name, replacements[i].prefix, prefixlen) == 0) { |
1753 | size_t replacementlen = SDL_strlen(replacements[i].replacement); |
1754 | SDL_memcpy(name, replacements[i].replacement, replacementlen); |
1755 | SDL_memmove(name+replacementlen, name+prefixlen, (len-prefixlen+1)); |
1756 | break; |
1757 | } |
1758 | } |
1759 | |
1760 | return name; |
1761 | } |
1762 | |
1763 | SDL_GameControllerType |
1764 | SDL_GetJoystickGameControllerTypeFromVIDPID(Uint16 vendor, Uint16 product) |
1765 | { |
1766 | return SDL_GetJoystickGameControllerType(NULL, vendor, product, -1, 0, 0, 0); |
1767 | } |
1768 | |
1769 | SDL_GameControllerType |
1770 | SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name) |
1771 | { |
1772 | SDL_GameControllerType type; |
1773 | Uint16 vendor, product; |
1774 | |
1775 | SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL); |
1776 | type = SDL_GetJoystickGameControllerType(name, vendor, product, -1, 0, 0, 0); |
1777 | if (type == SDL_CONTROLLER_TYPE_UNKNOWN) { |
1778 | if (SDL_IsJoystickXInput(guid)) { |
1779 | /* This is probably an Xbox One controller */ |
1780 | return SDL_CONTROLLER_TYPE_XBOXONE; |
1781 | } |
1782 | } |
1783 | return type; |
1784 | } |
1785 | |
1786 | SDL_GameControllerType |
1787 | SDL_GetJoystickGameControllerType(const char *name, Uint16 vendor, Uint16 product, int interface_number, int interface_class, int interface_subclass, int interface_protocol) |
1788 | { |
1789 | static const int LIBUSB_CLASS_VENDOR_SPEC = 0xFF; |
1790 | static const int XB360_IFACE_SUBCLASS = 93; |
1791 | static const int XB360_IFACE_PROTOCOL = 1; /* Wired */ |
1792 | static const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */ |
1793 | static const int XBONE_IFACE_SUBCLASS = 71; |
1794 | static const int XBONE_IFACE_PROTOCOL = 208; |
1795 | |
1796 | SDL_GameControllerType type = SDL_CONTROLLER_TYPE_UNKNOWN; |
1797 | |
1798 | /* This code should match the checks in libusb/hid.c and HIDDeviceManager.java */ |
1799 | if (interface_class == LIBUSB_CLASS_VENDOR_SPEC && |
1800 | interface_subclass == XB360_IFACE_SUBCLASS && |
1801 | (interface_protocol == XB360_IFACE_PROTOCOL || |
1802 | interface_protocol == XB360W_IFACE_PROTOCOL)) { |
1803 | |
1804 | static const int SUPPORTED_VENDORS[] = { |
1805 | 0x0079, /* GPD Win 2 */ |
1806 | 0x044f, /* Thrustmaster */ |
1807 | 0x045e, /* Microsoft */ |
1808 | 0x046d, /* Logitech */ |
1809 | 0x056e, /* Elecom */ |
1810 | 0x06a3, /* Saitek */ |
1811 | 0x0738, /* Mad Catz */ |
1812 | 0x07ff, /* Mad Catz */ |
1813 | 0x0e6f, /* PDP */ |
1814 | 0x0f0d, /* Hori */ |
1815 | 0x1038, /* SteelSeries */ |
1816 | 0x11c9, /* Nacon */ |
1817 | 0x12ab, /* Unknown */ |
1818 | 0x1430, /* RedOctane */ |
1819 | 0x146b, /* BigBen */ |
1820 | 0x1532, /* Razer Sabertooth */ |
1821 | 0x15e4, /* Numark */ |
1822 | 0x162e, /* Joytech */ |
1823 | 0x1689, /* Razer Onza */ |
1824 | 0x1949, /* Lab126, Inc. */ |
1825 | 0x1bad, /* Harmonix */ |
1826 | 0x20d6, /* PowerA */ |
1827 | 0x24c6, /* PowerA */ |
1828 | }; |
1829 | |
1830 | int i; |
1831 | for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) { |
1832 | if (vendor == SUPPORTED_VENDORS[i]) { |
1833 | type = SDL_CONTROLLER_TYPE_XBOX360; |
1834 | break; |
1835 | } |
1836 | } |
1837 | } |
1838 | |
1839 | if (interface_number == 0 && |
1840 | interface_class == LIBUSB_CLASS_VENDOR_SPEC && |
1841 | interface_subclass == XBONE_IFACE_SUBCLASS && |
1842 | interface_protocol == XBONE_IFACE_PROTOCOL) { |
1843 | |
1844 | static const int SUPPORTED_VENDORS[] = { |
1845 | 0x045e, /* Microsoft */ |
1846 | 0x0738, /* Mad Catz */ |
1847 | 0x0e6f, /* PDP */ |
1848 | 0x0f0d, /* Hori */ |
1849 | 0x1532, /* Razer Wildcat */ |
1850 | 0x20d6, /* PowerA */ |
1851 | 0x24c6, /* PowerA */ |
1852 | 0x2e24, /* Hyperkin */ |
1853 | }; |
1854 | |
1855 | int i; |
1856 | for (i = 0; i < SDL_arraysize(SUPPORTED_VENDORS); ++i) { |
1857 | if (vendor == SUPPORTED_VENDORS[i]) { |
1858 | type = SDL_CONTROLLER_TYPE_XBOXONE; |
1859 | break; |
1860 | } |
1861 | } |
1862 | } |
1863 | |
1864 | if (type == SDL_CONTROLLER_TYPE_UNKNOWN) { |
1865 | if (vendor == 0x0000 && product == 0x0000) { |
1866 | /* Some devices are only identifiable by their name */ |
1867 | if (name && |
1868 | (SDL_strcmp(name, "Lic Pro Controller" ) == 0 || |
1869 | SDL_strcmp(name, "Nintendo Wireless Gamepad" ) == 0 || |
1870 | SDL_strcmp(name, "Wireless Gamepad" ) == 0)) { |
1871 | /* HORI or PowerA Switch Pro Controller clone */ |
1872 | type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO; |
1873 | } else if (name && SDL_strcmp(name, "Virtual Joystick" ) == 0) { |
1874 | type = SDL_CONTROLLER_TYPE_VIRTUAL; |
1875 | } else { |
1876 | type = SDL_CONTROLLER_TYPE_UNKNOWN; |
1877 | } |
1878 | |
1879 | } else if (vendor == 0x0001 && product == 0x0001) { |
1880 | type = SDL_CONTROLLER_TYPE_UNKNOWN; |
1881 | |
1882 | } else { |
1883 | switch (GuessControllerType(vendor, product)) { |
1884 | case k_eControllerType_XBox360Controller: |
1885 | type = SDL_CONTROLLER_TYPE_XBOX360; |
1886 | break; |
1887 | case k_eControllerType_XBoxOneController: |
1888 | type = SDL_CONTROLLER_TYPE_XBOXONE; |
1889 | break; |
1890 | case k_eControllerType_PS3Controller: |
1891 | type = SDL_CONTROLLER_TYPE_PS3; |
1892 | break; |
1893 | case k_eControllerType_PS4Controller: |
1894 | type = SDL_CONTROLLER_TYPE_PS4; |
1895 | break; |
1896 | case k_eControllerType_PS5Controller: |
1897 | type = SDL_CONTROLLER_TYPE_PS5; |
1898 | break; |
1899 | case k_eControllerType_SwitchProController: |
1900 | case k_eControllerType_SwitchInputOnlyController: |
1901 | type = SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO; |
1902 | break; |
1903 | case k_eControllerType_SwitchJoyConLeft: |
1904 | case k_eControllerType_SwitchJoyConRight: |
1905 | type = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_JOY_CONS, SDL_FALSE) ? SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO : SDL_CONTROLLER_TYPE_UNKNOWN; |
1906 | break; |
1907 | default: |
1908 | type = SDL_CONTROLLER_TYPE_UNKNOWN; |
1909 | break; |
1910 | } |
1911 | } |
1912 | } |
1913 | return type; |
1914 | } |
1915 | |
1916 | SDL_bool |
1917 | SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id) |
1918 | { |
1919 | if (vendor_id == USB_VENDOR_MICROSOFT) { |
1920 | if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 || |
1921 | product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2 || |
1922 | product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) { |
1923 | return SDL_TRUE; |
1924 | } |
1925 | } |
1926 | return SDL_FALSE; |
1927 | } |
1928 | |
1929 | SDL_bool |
1930 | SDL_IsJoystickXboxOneSeriesX(Uint16 vendor_id, Uint16 product_id) |
1931 | { |
1932 | if (vendor_id == USB_VENDOR_MICROSOFT) { |
1933 | if (product_id == USB_PRODUCT_XBOX_ONE_SERIES_X || |
1934 | product_id == USB_PRODUCT_XBOX_ONE_SERIES_X_BLUETOOTH) { |
1935 | return SDL_TRUE; |
1936 | } |
1937 | } |
1938 | if (vendor_id == USB_VENDOR_POWERA_ALT) { |
1939 | if (product_id == USB_PRODUCT_XBOX_ONE_SERIES_X_POWERA) { |
1940 | return SDL_TRUE; |
1941 | } |
1942 | } |
1943 | return SDL_FALSE; |
1944 | } |
1945 | |
1946 | SDL_bool |
1947 | SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id) |
1948 | { |
1949 | if (vendor_id == USB_VENDOR_MICROSOFT) { |
1950 | if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH || |
1951 | product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH || |
1952 | product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH || |
1953 | product_id == USB_PRODUCT_XBOX_ONE_SERIES_X_BLUETOOTH) { |
1954 | return SDL_TRUE; |
1955 | } |
1956 | } |
1957 | return SDL_FALSE; |
1958 | } |
1959 | |
1960 | SDL_bool |
1961 | SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id) |
1962 | { |
1963 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
1964 | return (eType == k_eControllerType_PS4Controller); |
1965 | } |
1966 | |
1967 | SDL_bool |
1968 | SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id) |
1969 | { |
1970 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
1971 | return (eType == k_eControllerType_PS5Controller); |
1972 | } |
1973 | |
1974 | SDL_bool |
1975 | SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id) |
1976 | { |
1977 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
1978 | return (eType == k_eControllerType_SwitchProController || |
1979 | eType == k_eControllerType_SwitchInputOnlyController); |
1980 | } |
1981 | |
1982 | SDL_bool |
1983 | SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id) |
1984 | { |
1985 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
1986 | return (eType == k_eControllerType_SwitchInputOnlyController); |
1987 | } |
1988 | |
1989 | SDL_bool |
1990 | SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id) |
1991 | { |
1992 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
1993 | return (eType == k_eControllerType_SwitchJoyConLeft || |
1994 | eType == k_eControllerType_SwitchJoyConRight); |
1995 | } |
1996 | |
1997 | SDL_bool |
1998 | SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id) |
1999 | { |
2000 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
2001 | return (eType == k_eControllerType_SwitchJoyConLeft); |
2002 | } |
2003 | |
2004 | SDL_bool |
2005 | SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id) |
2006 | { |
2007 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
2008 | return (eType == k_eControllerType_SwitchJoyConRight); |
2009 | } |
2010 | |
2011 | SDL_bool |
2012 | SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id) |
2013 | { |
2014 | EControllerType eType = GuessControllerType(vendor_id, product_id); |
2015 | return (eType == k_eControllerType_SteamController || |
2016 | eType == k_eControllerType_SteamControllerV2); |
2017 | } |
2018 | |
2019 | SDL_bool |
2020 | SDL_IsJoystickXInput(SDL_JoystickGUID guid) |
2021 | { |
2022 | return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE; |
2023 | } |
2024 | |
2025 | SDL_bool |
2026 | SDL_IsJoystickWGI(SDL_JoystickGUID guid) |
2027 | { |
2028 | return (guid.data[14] == 'w') ? SDL_TRUE : SDL_FALSE; |
2029 | } |
2030 | |
2031 | SDL_bool |
2032 | SDL_IsJoystickHIDAPI(SDL_JoystickGUID guid) |
2033 | { |
2034 | return (guid.data[14] == 'h') ? SDL_TRUE : SDL_FALSE; |
2035 | } |
2036 | |
2037 | SDL_bool |
2038 | SDL_IsJoystickRAWINPUT(SDL_JoystickGUID guid) |
2039 | { |
2040 | return (guid.data[14] == 'r') ? SDL_TRUE : SDL_FALSE; |
2041 | } |
2042 | |
2043 | SDL_bool |
2044 | SDL_IsJoystickVirtual(SDL_JoystickGUID guid) |
2045 | { |
2046 | return (guid.data[14] == 'v') ? SDL_TRUE : SDL_FALSE; |
2047 | } |
2048 | |
2049 | static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid) |
2050 | { |
2051 | static Uint32 wheel_joysticks[] = { |
2052 | MAKE_VIDPID(0x046d, 0xc294), /* Logitech generic wheel */ |
2053 | MAKE_VIDPID(0x046d, 0xc295), /* Logitech Momo Force */ |
2054 | MAKE_VIDPID(0x046d, 0xc298), /* Logitech Driving Force Pro */ |
2055 | MAKE_VIDPID(0x046d, 0xc299), /* Logitech G25 */ |
2056 | MAKE_VIDPID(0x046d, 0xc29a), /* Logitech Driving Force GT */ |
2057 | MAKE_VIDPID(0x046d, 0xc29b), /* Logitech G27 */ |
2058 | MAKE_VIDPID(0x046d, 0xc24f), /* Logitech G29 (PS3) */ |
2059 | MAKE_VIDPID(0x046d, 0xc260), /* Logitech G29 (PS4) */ |
2060 | MAKE_VIDPID(0x046d, 0xc261), /* Logitech G920 (initial mode) */ |
2061 | MAKE_VIDPID(0x046d, 0xc262), /* Logitech G920 (active mode) */ |
2062 | MAKE_VIDPID(0x044f, 0xb65d), /* Thrustmaster Wheel FFB */ |
2063 | MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster Wheel FFB */ |
2064 | MAKE_VIDPID(0x044f, 0xb677), /* Thrustmaster T150 */ |
2065 | MAKE_VIDPID(0x044f, 0xb664), /* Thrustmaster TX (initial mode) */ |
2066 | MAKE_VIDPID(0x044f, 0xb669), /* Thrustmaster TX (active mode) */ |
2067 | }; |
2068 | int i; |
2069 | |
2070 | for (i = 0; i < SDL_arraysize(wheel_joysticks); ++i) { |
2071 | if (vidpid == wheel_joysticks[i]) { |
2072 | return SDL_TRUE; |
2073 | } |
2074 | } |
2075 | return SDL_FALSE; |
2076 | } |
2077 | |
2078 | static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid) |
2079 | { |
2080 | static Uint32 flightstick_joysticks[] = { |
2081 | MAKE_VIDPID(0x044f, 0x0402), /* HOTAS Warthog Joystick */ |
2082 | MAKE_VIDPID(0x0738, 0x2221), /* Saitek Pro Flight X-56 Rhino Stick */ |
2083 | }; |
2084 | int i; |
2085 | |
2086 | for (i = 0; i < SDL_arraysize(flightstick_joysticks); ++i) { |
2087 | if (vidpid == flightstick_joysticks[i]) { |
2088 | return SDL_TRUE; |
2089 | } |
2090 | } |
2091 | return SDL_FALSE; |
2092 | } |
2093 | |
2094 | static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid) |
2095 | { |
2096 | static Uint32 throttle_joysticks[] = { |
2097 | MAKE_VIDPID(0x044f, 0x0404), /* HOTAS Warthog Throttle */ |
2098 | MAKE_VIDPID(0x0738, 0xa221), /* Saitek Pro Flight X-56 Rhino Throttle */ |
2099 | }; |
2100 | int i; |
2101 | |
2102 | for (i = 0; i < SDL_arraysize(throttle_joysticks); ++i) { |
2103 | if (vidpid == throttle_joysticks[i]) { |
2104 | return SDL_TRUE; |
2105 | } |
2106 | } |
2107 | return SDL_FALSE; |
2108 | } |
2109 | |
2110 | static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid) |
2111 | { |
2112 | Uint16 vendor; |
2113 | Uint16 product; |
2114 | Uint32 vidpid; |
2115 | |
2116 | if (SDL_IsJoystickXInput(guid)) { |
2117 | /* XInput GUID, get the type based on the XInput device subtype */ |
2118 | switch (guid.data[15]) { |
2119 | case 0x01: /* XINPUT_DEVSUBTYPE_GAMEPAD */ |
2120 | return SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
2121 | case 0x02: /* XINPUT_DEVSUBTYPE_WHEEL */ |
2122 | return SDL_JOYSTICK_TYPE_WHEEL; |
2123 | case 0x03: /* XINPUT_DEVSUBTYPE_ARCADE_STICK */ |
2124 | return SDL_JOYSTICK_TYPE_ARCADE_STICK; |
2125 | case 0x04: /* XINPUT_DEVSUBTYPE_FLIGHT_STICK */ |
2126 | return SDL_JOYSTICK_TYPE_FLIGHT_STICK; |
2127 | case 0x05: /* XINPUT_DEVSUBTYPE_DANCE_PAD */ |
2128 | return SDL_JOYSTICK_TYPE_DANCE_PAD; |
2129 | case 0x06: /* XINPUT_DEVSUBTYPE_GUITAR */ |
2130 | case 0x07: /* XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE */ |
2131 | case 0x0B: /* XINPUT_DEVSUBTYPE_GUITAR_BASS */ |
2132 | return SDL_JOYSTICK_TYPE_GUITAR; |
2133 | case 0x08: /* XINPUT_DEVSUBTYPE_DRUM_KIT */ |
2134 | return SDL_JOYSTICK_TYPE_DRUM_KIT; |
2135 | case 0x13: /* XINPUT_DEVSUBTYPE_ARCADE_PAD */ |
2136 | return SDL_JOYSTICK_TYPE_ARCADE_PAD; |
2137 | default: |
2138 | return SDL_JOYSTICK_TYPE_UNKNOWN; |
2139 | } |
2140 | } |
2141 | |
2142 | if (SDL_IsJoystickWGI(guid)) { |
2143 | return (SDL_JoystickType)guid.data[15]; |
2144 | } |
2145 | |
2146 | if (SDL_IsJoystickVirtual(guid)) { |
2147 | return (SDL_JoystickType)guid.data[15]; |
2148 | } |
2149 | |
2150 | SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL); |
2151 | vidpid = MAKE_VIDPID(vendor, product); |
2152 | |
2153 | if (SDL_IsJoystickProductWheel(vidpid)) { |
2154 | return SDL_JOYSTICK_TYPE_WHEEL; |
2155 | } |
2156 | |
2157 | if (SDL_IsJoystickProductFlightStick(vidpid)) { |
2158 | return SDL_JOYSTICK_TYPE_FLIGHT_STICK; |
2159 | } |
2160 | |
2161 | if (SDL_IsJoystickProductThrottle(vidpid)) { |
2162 | return SDL_JOYSTICK_TYPE_THROTTLE; |
2163 | } |
2164 | |
2165 | if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) { |
2166 | return SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
2167 | } |
2168 | |
2169 | return SDL_JOYSTICK_TYPE_UNKNOWN; |
2170 | } |
2171 | |
2172 | static SDL_bool SDL_IsPS4RemapperRunning(void) |
2173 | { |
2174 | #ifdef __WIN32__ |
2175 | const char *mapper_processes[] = { |
2176 | "DS4Windows.exe" , |
2177 | "InputMapper.exe" , |
2178 | }; |
2179 | int i; |
2180 | PROCESSENTRY32 pe32; |
2181 | SDL_bool found = SDL_FALSE; |
2182 | |
2183 | /* Take a snapshot of all processes in the system */ |
2184 | HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
2185 | if (hProcessSnap != INVALID_HANDLE_VALUE) { |
2186 | pe32.dwSize = sizeof(PROCESSENTRY32); |
2187 | if (Process32First(hProcessSnap, &pe32)) { |
2188 | do |
2189 | { |
2190 | for (i = 0; i < SDL_arraysize(mapper_processes); ++i) { |
2191 | if (SDL_strcasecmp(pe32.szExeFile, mapper_processes[i]) == 0) { |
2192 | found = SDL_TRUE; |
2193 | } |
2194 | } |
2195 | } while (Process32Next(hProcessSnap, &pe32) && !found); |
2196 | } |
2197 | CloseHandle(hProcessSnap); |
2198 | } |
2199 | return found; |
2200 | #else |
2201 | return SDL_FALSE; |
2202 | #endif |
2203 | } |
2204 | |
2205 | SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid) |
2206 | { |
2207 | /* This list is taken from: |
2208 | https://raw.githubusercontent.com/denilsonsa/udev-joystick-blacklist/master/generate_rules.py |
2209 | */ |
2210 | static Uint32 joystick_blacklist[] = { |
2211 | /* Microsoft Microsoft Wireless Optical Desktop 2.10 */ |
2212 | /* Microsoft Wireless Desktop - Comfort Edition */ |
2213 | MAKE_VIDPID(0x045e, 0x009d), |
2214 | |
2215 | /* Microsoft Microsoft Digital Media Pro Keyboard */ |
2216 | /* Microsoft Corp. Digital Media Pro Keyboard */ |
2217 | MAKE_VIDPID(0x045e, 0x00b0), |
2218 | |
2219 | /* Microsoft Microsoft Digital Media Keyboard */ |
2220 | /* Microsoft Corp. Digital Media Keyboard 1.0A */ |
2221 | MAKE_VIDPID(0x045e, 0x00b4), |
2222 | |
2223 | /* Microsoft Microsoft Digital Media Keyboard 3000 */ |
2224 | MAKE_VIDPID(0x045e, 0x0730), |
2225 | |
2226 | /* Microsoft Microsoft 2.4GHz Transceiver v6.0 */ |
2227 | /* Microsoft Microsoft 2.4GHz Transceiver v8.0 */ |
2228 | /* Microsoft Corp. Nano Transceiver v1.0 for Bluetooth */ |
2229 | /* Microsoft Wireless Mobile Mouse 1000 */ |
2230 | /* Microsoft Wireless Desktop 3000 */ |
2231 | MAKE_VIDPID(0x045e, 0x0745), |
2232 | |
2233 | /* Microsoft SideWinder(TM) 2.4GHz Transceiver */ |
2234 | MAKE_VIDPID(0x045e, 0x0748), |
2235 | |
2236 | /* Microsoft Corp. Wired Keyboard 600 */ |
2237 | MAKE_VIDPID(0x045e, 0x0750), |
2238 | |
2239 | /* Microsoft Corp. Sidewinder X4 keyboard */ |
2240 | MAKE_VIDPID(0x045e, 0x0768), |
2241 | |
2242 | /* Microsoft Corp. Arc Touch Mouse Transceiver */ |
2243 | MAKE_VIDPID(0x045e, 0x0773), |
2244 | |
2245 | /* Microsoft 2.4GHz Transceiver v9.0 */ |
2246 | /* Microsoft Nano Transceiver v2.1 */ |
2247 | /* Microsoft Sculpt Ergonomic Keyboard (5KV-00001) */ |
2248 | MAKE_VIDPID(0x045e, 0x07a5), |
2249 | |
2250 | /* Microsoft Nano Transceiver v1.0 */ |
2251 | /* Microsoft Wireless Keyboard 800 */ |
2252 | MAKE_VIDPID(0x045e, 0x07b2), |
2253 | |
2254 | /* Microsoft Nano Transceiver v2.0 */ |
2255 | MAKE_VIDPID(0x045e, 0x0800), |
2256 | |
2257 | MAKE_VIDPID(0x046d, 0xc30a), /* Logitech, Inc. iTouch Composite keboard */ |
2258 | |
2259 | MAKE_VIDPID(0x04d9, 0xa0df), /* Tek Syndicate Mouse (E-Signal USB Gaming Mouse) */ |
2260 | |
2261 | /* List of Wacom devices at: http://linuxwacom.sourceforge.net/wiki/index.php/Device_IDs */ |
2262 | MAKE_VIDPID(0x056a, 0x0010), /* Wacom ET-0405 Graphire */ |
2263 | MAKE_VIDPID(0x056a, 0x0011), /* Wacom ET-0405A Graphire2 (4x5) */ |
2264 | MAKE_VIDPID(0x056a, 0x0012), /* Wacom ET-0507A Graphire2 (5x7) */ |
2265 | MAKE_VIDPID(0x056a, 0x0013), /* Wacom CTE-430 Graphire3 (4x5) */ |
2266 | MAKE_VIDPID(0x056a, 0x0014), /* Wacom CTE-630 Graphire3 (6x8) */ |
2267 | MAKE_VIDPID(0x056a, 0x0015), /* Wacom CTE-440 Graphire4 (4x5) */ |
2268 | MAKE_VIDPID(0x056a, 0x0016), /* Wacom CTE-640 Graphire4 (6x8) */ |
2269 | MAKE_VIDPID(0x056a, 0x0017), /* Wacom CTE-450 Bamboo Fun (4x5) */ |
2270 | MAKE_VIDPID(0x056a, 0x0018), /* Wacom CTE-650 Bamboo Fun 6x8 */ |
2271 | MAKE_VIDPID(0x056a, 0x0019), /* Wacom CTE-631 Bamboo One */ |
2272 | MAKE_VIDPID(0x056a, 0x00d1), /* Wacom Bamboo Pen and Touch CTH-460 */ |
2273 | MAKE_VIDPID(0x056a, 0x030e), /* Wacom Intuos Pen (S) CTL-480 */ |
2274 | |
2275 | MAKE_VIDPID(0x09da, 0x054f), /* A4 Tech Co., G7 750 mouse */ |
2276 | MAKE_VIDPID(0x09da, 0x1410), /* A4 Tech Co., Ltd Bloody AL9 mouse */ |
2277 | MAKE_VIDPID(0x09da, 0x3043), /* A4 Tech Co., Ltd Bloody R8A Gaming Mouse */ |
2278 | MAKE_VIDPID(0x09da, 0x31b5), /* A4 Tech Co., Ltd Bloody TL80 Terminator Laser Gaming Mouse */ |
2279 | MAKE_VIDPID(0x09da, 0x3997), /* A4 Tech Co., Ltd Bloody RT7 Terminator Wireless */ |
2280 | MAKE_VIDPID(0x09da, 0x3f8b), /* A4 Tech Co., Ltd Bloody V8 mouse */ |
2281 | MAKE_VIDPID(0x09da, 0x51f4), /* Modecom MC-5006 Keyboard */ |
2282 | MAKE_VIDPID(0x09da, 0x5589), /* A4 Tech Co., Ltd Terminator TL9 Laser Gaming Mouse */ |
2283 | MAKE_VIDPID(0x09da, 0x7b22), /* A4 Tech Co., Ltd Bloody V5 */ |
2284 | MAKE_VIDPID(0x09da, 0x7f2d), /* A4 Tech Co., Ltd Bloody R3 mouse */ |
2285 | MAKE_VIDPID(0x09da, 0x8090), /* A4 Tech Co., Ltd X-718BK Oscar Optical Gaming Mouse */ |
2286 | MAKE_VIDPID(0x09da, 0x9033), /* A4 Tech Co., X7 X-705K */ |
2287 | MAKE_VIDPID(0x09da, 0x9066), /* A4 Tech Co., Sharkoon Fireglider Optical */ |
2288 | MAKE_VIDPID(0x09da, 0x9090), /* A4 Tech Co., Ltd XL-730K / XL-750BK / XL-755BK Laser Mouse */ |
2289 | MAKE_VIDPID(0x09da, 0x90c0), /* A4 Tech Co., Ltd X7 G800V keyboard */ |
2290 | MAKE_VIDPID(0x09da, 0xf012), /* A4 Tech Co., Ltd Bloody V7 mouse */ |
2291 | MAKE_VIDPID(0x09da, 0xf32a), /* A4 Tech Co., Ltd Bloody B540 keyboard */ |
2292 | MAKE_VIDPID(0x09da, 0xf613), /* A4 Tech Co., Ltd Bloody V2 mouse */ |
2293 | MAKE_VIDPID(0x09da, 0xf624), /* A4 Tech Co., Ltd Bloody B120 Keyboard */ |
2294 | |
2295 | MAKE_VIDPID(0x1b1c, 0x1b3c), /* Corsair Harpoon RGB gaming mouse */ |
2296 | |
2297 | MAKE_VIDPID(0x1d57, 0xad03), /* [T3] 2.4GHz and IR Air Mouse Remote Control */ |
2298 | |
2299 | MAKE_VIDPID(0x1e7d, 0x2e4a), /* Roccat Tyon Mouse */ |
2300 | |
2301 | MAKE_VIDPID(0x20a0, 0x422d), /* Winkeyless.kr Keyboards */ |
2302 | |
2303 | MAKE_VIDPID(0x2516, 0x001f), /* Cooler Master Storm Mizar Mouse */ |
2304 | MAKE_VIDPID(0x2516, 0x0028), /* Cooler Master Storm Alcor Mouse */ |
2305 | }; |
2306 | |
2307 | unsigned int i; |
2308 | Uint32 id; |
2309 | Uint16 vendor; |
2310 | Uint16 product; |
2311 | SDL_GameControllerType type; |
2312 | |
2313 | SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL); |
2314 | |
2315 | /* Check the joystick blacklist */ |
2316 | id = MAKE_VIDPID(vendor, product); |
2317 | for (i = 0; i < SDL_arraysize(joystick_blacklist); ++i) { |
2318 | if (id == joystick_blacklist[i]) { |
2319 | return SDL_TRUE; |
2320 | } |
2321 | } |
2322 | |
2323 | type = SDL_GetJoystickGameControllerType(name, vendor, product, -1, 0, 0, 0); |
2324 | if ((type == SDL_CONTROLLER_TYPE_PS4 || type == SDL_CONTROLLER_TYPE_PS5) && SDL_IsPS4RemapperRunning()) { |
2325 | return SDL_TRUE; |
2326 | } |
2327 | |
2328 | if (SDL_IsGameControllerNameAndGUID(name, guid) && |
2329 | SDL_ShouldIgnoreGameController(name, guid)) { |
2330 | return SDL_TRUE; |
2331 | } |
2332 | |
2333 | return SDL_FALSE; |
2334 | } |
2335 | |
2336 | /* return the guid for this index */ |
2337 | SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index) |
2338 | { |
2339 | SDL_JoystickDriver *driver; |
2340 | SDL_JoystickGUID guid; |
2341 | |
2342 | SDL_LockJoysticks(); |
2343 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
2344 | guid = driver->GetDeviceGUID(device_index); |
2345 | } else { |
2346 | SDL_zero(guid); |
2347 | } |
2348 | SDL_UnlockJoysticks(); |
2349 | |
2350 | return guid; |
2351 | } |
2352 | |
2353 | Uint16 SDL_JoystickGetDeviceVendor(int device_index) |
2354 | { |
2355 | Uint16 vendor; |
2356 | SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index); |
2357 | |
2358 | SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL); |
2359 | return vendor; |
2360 | } |
2361 | |
2362 | Uint16 SDL_JoystickGetDeviceProduct(int device_index) |
2363 | { |
2364 | Uint16 product; |
2365 | SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index); |
2366 | |
2367 | SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL); |
2368 | return product; |
2369 | } |
2370 | |
2371 | Uint16 SDL_JoystickGetDeviceProductVersion(int device_index) |
2372 | { |
2373 | Uint16 version; |
2374 | SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index); |
2375 | |
2376 | SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version); |
2377 | return version; |
2378 | } |
2379 | |
2380 | SDL_JoystickType SDL_JoystickGetDeviceType(int device_index) |
2381 | { |
2382 | SDL_JoystickType type; |
2383 | SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index); |
2384 | |
2385 | type = SDL_GetJoystickGUIDType(guid); |
2386 | if (type == SDL_JOYSTICK_TYPE_UNKNOWN) { |
2387 | if (SDL_IsGameController(device_index)) { |
2388 | type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
2389 | } |
2390 | } |
2391 | return type; |
2392 | } |
2393 | |
2394 | SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index) |
2395 | { |
2396 | SDL_JoystickDriver *driver; |
2397 | SDL_JoystickID instance_id = -1; |
2398 | |
2399 | SDL_LockJoysticks(); |
2400 | if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) { |
2401 | instance_id = driver->GetDeviceInstanceID(device_index); |
2402 | } |
2403 | SDL_UnlockJoysticks(); |
2404 | |
2405 | return instance_id; |
2406 | } |
2407 | |
2408 | int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id) |
2409 | { |
2410 | int i, num_joysticks, device_index = -1; |
2411 | |
2412 | SDL_LockJoysticks(); |
2413 | num_joysticks = SDL_NumJoysticks(); |
2414 | for (i = 0; i < num_joysticks; ++i) { |
2415 | if (SDL_JoystickGetDeviceInstanceID(i) == instance_id) { |
2416 | device_index = i; |
2417 | break; |
2418 | } |
2419 | } |
2420 | SDL_UnlockJoysticks(); |
2421 | |
2422 | return device_index; |
2423 | } |
2424 | |
2425 | SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick) |
2426 | { |
2427 | if (!SDL_PrivateJoystickValid(joystick)) { |
2428 | SDL_JoystickGUID emptyGUID; |
2429 | SDL_zero(emptyGUID); |
2430 | return emptyGUID; |
2431 | } |
2432 | return joystick->guid; |
2433 | } |
2434 | |
2435 | Uint16 SDL_JoystickGetVendor(SDL_Joystick *joystick) |
2436 | { |
2437 | Uint16 vendor; |
2438 | SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
2439 | |
2440 | SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL); |
2441 | return vendor; |
2442 | } |
2443 | |
2444 | Uint16 SDL_JoystickGetProduct(SDL_Joystick *joystick) |
2445 | { |
2446 | Uint16 product; |
2447 | SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
2448 | |
2449 | SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL); |
2450 | return product; |
2451 | } |
2452 | |
2453 | Uint16 SDL_JoystickGetProductVersion(SDL_Joystick *joystick) |
2454 | { |
2455 | Uint16 version; |
2456 | SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
2457 | |
2458 | SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version); |
2459 | return version; |
2460 | } |
2461 | |
2462 | const char *SDL_JoystickGetSerial(SDL_Joystick *joystick) |
2463 | { |
2464 | if (!SDL_PrivateJoystickValid(joystick)) { |
2465 | return NULL; |
2466 | } |
2467 | return joystick->serial; |
2468 | } |
2469 | |
2470 | SDL_JoystickType SDL_JoystickGetType(SDL_Joystick *joystick) |
2471 | { |
2472 | SDL_JoystickType type; |
2473 | SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick); |
2474 | |
2475 | type = SDL_GetJoystickGUIDType(guid); |
2476 | if (type == SDL_JOYSTICK_TYPE_UNKNOWN) { |
2477 | if (joystick && joystick->is_game_controller) { |
2478 | type = SDL_JOYSTICK_TYPE_GAMECONTROLLER; |
2479 | } |
2480 | } |
2481 | return type; |
2482 | } |
2483 | |
2484 | /* convert the guid to a printable string */ |
2485 | void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID) |
2486 | { |
2487 | static const char k_rgchHexToASCII[] = "0123456789abcdef" ; |
2488 | int i; |
2489 | |
2490 | if ((pszGUID == NULL) || (cbGUID <= 0)) { |
2491 | return; |
2492 | } |
2493 | |
2494 | for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) { |
2495 | /* each input byte writes 2 ascii chars, and might write a null byte. */ |
2496 | /* If we don't have room for next input byte, stop */ |
2497 | unsigned char c = guid.data[i]; |
2498 | |
2499 | *pszGUID++ = k_rgchHexToASCII[c >> 4]; |
2500 | *pszGUID++ = k_rgchHexToASCII[c & 0x0F]; |
2501 | } |
2502 | *pszGUID = '\0'; |
2503 | } |
2504 | |
2505 | /*----------------------------------------------------------------------------- |
2506 | * Purpose: Returns the 4 bit nibble for a hex character |
2507 | * Input : c - |
2508 | * Output : unsigned char |
2509 | *-----------------------------------------------------------------------------*/ |
2510 | static unsigned char nibble(char c) |
2511 | { |
2512 | if ((c >= '0') && (c <= '9')) { |
2513 | return (unsigned char)(c - '0'); |
2514 | } |
2515 | |
2516 | if ((c >= 'A') && (c <= 'F')) { |
2517 | return (unsigned char)(c - 'A' + 0x0a); |
2518 | } |
2519 | |
2520 | if ((c >= 'a') && (c <= 'f')) { |
2521 | return (unsigned char)(c - 'a' + 0x0a); |
2522 | } |
2523 | |
2524 | /* received an invalid character, and no real way to return an error */ |
2525 | /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */ |
2526 | return 0; |
2527 | } |
2528 | |
2529 | /* convert the string version of a joystick guid to the struct */ |
2530 | SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID) |
2531 | { |
2532 | SDL_JoystickGUID guid; |
2533 | int maxoutputbytes= sizeof(guid); |
2534 | size_t len = SDL_strlen(pchGUID); |
2535 | Uint8 *p; |
2536 | size_t i; |
2537 | |
2538 | /* Make sure it's even */ |
2539 | len = (len) & ~0x1; |
2540 | |
2541 | SDL_memset(&guid, 0x00, sizeof(guid)); |
2542 | |
2543 | p = (Uint8 *)&guid; |
2544 | for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) { |
2545 | *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]); |
2546 | } |
2547 | |
2548 | return guid; |
2549 | } |
2550 | |
2551 | /* update the power level for this joystick */ |
2552 | void SDL_PrivateJoystickBatteryLevel(SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel) |
2553 | { |
2554 | joystick->epowerlevel = ePowerLevel; |
2555 | } |
2556 | |
2557 | /* return its power level */ |
2558 | SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel(SDL_Joystick *joystick) |
2559 | { |
2560 | if (!SDL_PrivateJoystickValid(joystick)) { |
2561 | return SDL_JOYSTICK_POWER_UNKNOWN; |
2562 | } |
2563 | return joystick->epowerlevel; |
2564 | } |
2565 | |
2566 | int SDL_PrivateJoystickTouchpad(SDL_Joystick *joystick, int touchpad, int finger, Uint8 state, float x, float y, float pressure) |
2567 | { |
2568 | SDL_JoystickTouchpadInfo *touchpad_info; |
2569 | SDL_JoystickTouchpadFingerInfo *finger_info; |
2570 | int posted; |
2571 | #if !SDL_EVENTS_DISABLED |
2572 | Uint32 event_type; |
2573 | #endif |
2574 | |
2575 | if (touchpad < 0 || touchpad >= joystick->ntouchpads) { |
2576 | return 0; |
2577 | } |
2578 | |
2579 | touchpad_info = &joystick->touchpads[touchpad]; |
2580 | if (finger < 0 || finger >= touchpad_info->nfingers) { |
2581 | return 0; |
2582 | } |
2583 | |
2584 | finger_info = &touchpad_info->fingers[finger]; |
2585 | |
2586 | if (!state) { |
2587 | if (x == 0.0f && y == 0.0f) { |
2588 | x = finger_info->x; |
2589 | y = finger_info->y; |
2590 | } |
2591 | pressure = 0.0f; |
2592 | } |
2593 | |
2594 | if (x < 0.0f) { |
2595 | x = 0.0f; |
2596 | } else if (x > 1.0f) { |
2597 | x = 1.0f; |
2598 | } |
2599 | if (y < 0.0f) { |
2600 | y = 0.0f; |
2601 | } else if (y > 1.0f) { |
2602 | y = 1.0f; |
2603 | } |
2604 | if (pressure < 0.0f) { |
2605 | pressure = 0.0f; |
2606 | } else if (pressure > 1.0f) { |
2607 | pressure = 1.0f; |
2608 | } |
2609 | |
2610 | if (state == finger_info->state) { |
2611 | if (!state || |
2612 | (x == finger_info->x && y == finger_info->y && pressure == finger_info->pressure)) { |
2613 | return 0; |
2614 | } |
2615 | } |
2616 | |
2617 | #if !SDL_EVENTS_DISABLED |
2618 | if (state == finger_info->state) { |
2619 | event_type = SDL_CONTROLLERTOUCHPADMOTION; |
2620 | } else if (state) { |
2621 | event_type = SDL_CONTROLLERTOUCHPADDOWN; |
2622 | } else { |
2623 | event_type = SDL_CONTROLLERTOUCHPADUP; |
2624 | } |
2625 | #endif |
2626 | |
2627 | /* Update internal joystick state */ |
2628 | finger_info->state = state; |
2629 | finger_info->x = x; |
2630 | finger_info->y = y; |
2631 | finger_info->pressure = pressure; |
2632 | |
2633 | /* Post the event, if desired */ |
2634 | posted = 0; |
2635 | #if !SDL_EVENTS_DISABLED |
2636 | if (SDL_GetEventState(event_type) == SDL_ENABLE) { |
2637 | SDL_Event event; |
2638 | event.type = event_type; |
2639 | event.ctouchpad.which = joystick->instance_id; |
2640 | event.ctouchpad.touchpad = touchpad; |
2641 | event.ctouchpad.finger = finger; |
2642 | event.ctouchpad.x = x; |
2643 | event.ctouchpad.y = y; |
2644 | event.ctouchpad.pressure = pressure; |
2645 | posted = SDL_PushEvent(&event) == 1; |
2646 | } |
2647 | #endif /* !SDL_EVENTS_DISABLED */ |
2648 | return posted; |
2649 | } |
2650 | |
2651 | int SDL_PrivateJoystickSensor(SDL_Joystick *joystick, SDL_SensorType type, const float *data, int num_values) |
2652 | { |
2653 | int i; |
2654 | int posted = 0; |
2655 | |
2656 | for (i = 0; i < joystick->nsensors; ++i) { |
2657 | SDL_JoystickSensorInfo *sensor = &joystick->sensors[i]; |
2658 | |
2659 | if (sensor->type == type) { |
2660 | if (sensor->enabled) { |
2661 | num_values = SDL_min(num_values, SDL_arraysize(sensor->data)); |
2662 | if (SDL_memcmp(data, sensor->data, num_values*sizeof(*data)) != 0) { |
2663 | |
2664 | /* Update internal sensor state */ |
2665 | SDL_memcpy(sensor->data, data, num_values*sizeof(*data)); |
2666 | |
2667 | /* Post the event, if desired */ |
2668 | #if !SDL_EVENTS_DISABLED |
2669 | if (SDL_GetEventState(SDL_CONTROLLERSENSORUPDATE) == SDL_ENABLE) { |
2670 | SDL_Event event; |
2671 | event.type = SDL_CONTROLLERSENSORUPDATE; |
2672 | event.csensor.which = joystick->instance_id; |
2673 | event.csensor.sensor = type; |
2674 | num_values = SDL_min(num_values, SDL_arraysize(event.csensor.data)); |
2675 | SDL_memset(event.csensor.data, 0, sizeof(event.csensor.data)); |
2676 | SDL_memcpy(event.csensor.data, data, num_values*sizeof(*data)); |
2677 | posted = SDL_PushEvent(&event) == 1; |
2678 | } |
2679 | #endif /* !SDL_EVENTS_DISABLED */ |
2680 | } |
2681 | } |
2682 | break; |
2683 | } |
2684 | } |
2685 | return posted; |
2686 | } |
2687 | |
2688 | /* vi: set ts=4 sw=4 expandtab: */ |
2689 | |