21 #include "../SDL_internal.h"
33 #if !SDL_EVENTS_DISABLED
34 #include "../events/SDL_events_c.h"
37 #if defined(__ANDROID__)
43 #define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS 250
45 #define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
93 typedef struct _ControllerMapping_t
99 struct _ControllerMapping_t *
next;
144 if (hint && *hint ==
'@') {
166 if (entries ==
NULL) {
207 if (num_events <= 0) {
217 for (
i = 0;
i < num_events; ++
i) {
227 if (
a->outputType !=
b->outputType) {
232 return (
a->output.axis.axis ==
b->output.axis.axis);
234 return (
a->output.button ==
b->output.button);
253 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
273 if (last_match && (!match || !
HasSameOutput(last_match, match))) {
296 gamecontroller->last_match_axis[
axis] = match;
303 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
321 Uint8 last_mask = gamecontroller->last_hat_mask[hat];
324 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
327 if ((changed_mask & binding->
input.
hat.hat_mask) != 0) {
340 gamecontroller->last_hat_mask[hat] =
value;
348 switch(
event->type) {
352 while (controllerlist) {
353 if (controllerlist->joystick->instance_id ==
event->jaxis.which) {
357 controllerlist = controllerlist->next;
365 while (controllerlist) {
366 if (controllerlist->joystick->instance_id ==
event->jbutton.which) {
370 controllerlist = controllerlist->next;
377 while (controllerlist) {
378 if (controllerlist->joystick->instance_id ==
event->jhat.which) {
382 controllerlist = controllerlist->next;
399 while (controllerlist) {
400 if (controllerlist->joystick->instance_id ==
event->jdevice.which) {
410 controllerlist = controllerlist->next;
427 while (pSupportedController) {
428 if (
SDL_memcmp(guid, &pSupportedController->
guid,
sizeof(*guid)) == 0) {
429 return pSupportedController;
431 pSupportedController = pSupportedController->
next;
438 #if SDL_JOYSTICK_XINPUT
465 if (pchString && (*pchString ==
'+' || *pchString ==
'-')) {
469 if (!pchString || !pchString[0]) {
516 if (!pchString || !pchString[0])
546 char half_axis_input = 0;
547 char half_axis_output = 0;
549 if (*szGameButton ==
'+' || *szGameButton ==
'-') {
550 half_axis_output = *szGameButton++;
562 if (half_axis_output ==
'+') {
565 }
else if (half_axis_output ==
'-') {
577 SDL_SetError(
"Unexpected controller element %s", szGameButton);
581 if (*szJoystickButton ==
'+' || *szJoystickButton ==
'-') {
582 half_axis_input = *szJoystickButton++;
584 if (szJoystickButton[
SDL_strlen(szJoystickButton) - 1] ==
'~') {
588 if (szJoystickButton[0] ==
'a' &&
SDL_isdigit(szJoystickButton[1])) {
591 if (half_axis_input ==
'+') {
594 }
else if (half_axis_input ==
'-') {
606 }
else if (szJoystickButton[0] ==
'b' &&
SDL_isdigit(szJoystickButton[1])) {
609 }
else if (szJoystickButton[0] ==
'h' &&
SDL_isdigit(szJoystickButton[1]) &&
610 szJoystickButton[2] ==
'.' &&
SDL_isdigit(szJoystickButton[3])) {
611 int hat =
SDL_atoi(&szJoystickButton[1]);
617 SDL_SetError(
"Unexpected joystick element: %s", szJoystickButton);
621 ++gamecontroller->num_bindings;
623 if (!gamecontroller->bindings) {
624 gamecontroller->num_bindings = 0;
628 gamecontroller->bindings[gamecontroller->num_bindings - 1] = bind;
638 char szGameButton[20];
639 char szJoystickButton[20];
642 const char *pchPos = pchString;
647 while (pchPos && *pchPos) {
648 if (*pchPos ==
':') {
651 }
else if (*pchPos ==
' ') {
653 }
else if (*pchPos ==
',') {
660 }
else if (bGameButton) {
661 if (
i >=
sizeof(szGameButton)) {
662 SDL_SetError(
"Button name too large: %s", szGameButton);
665 szGameButton[
i] = *pchPos;
668 if (
i >=
sizeof(szJoystickButton)) {
669 SDL_SetError(
"Joystick button name too large: %s", szJoystickButton);
672 szJoystickButton[
i] = *pchPos;
689 gamecontroller->name = pchName;
690 gamecontroller->num_bindings = 0;
691 SDL_memset(gamecontroller->last_match_axis, 0, gamecontroller->joystick->naxes *
sizeof(*gamecontroller->last_match_axis));
696 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
702 if (binding->
input.
axis.axis < gamecontroller->joystick->naxes) {
703 gamecontroller->joystick->axes[binding->
input.
axis.axis].value =
716 const char *pFirstComma =
SDL_strchr(pMapping,
',');
718 char *pchGUID =
SDL_malloc(pFirstComma - pMapping + 1);
723 SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
724 pchGUID[pFirstComma - pMapping] =
'\0';
729 SDL_memcmp(&pchGUID[20],
"504944564944", 12) == 0) {
737 SDL_memcmp(&pchGUID[4],
"000000000000", 12) == 0 &&
738 SDL_memcmp(&pchGUID[20],
"000000000000", 12) == 0) {
755 const char *pFirstComma, *pSecondComma;
762 pSecondComma =
SDL_strchr(pFirstComma + 1,
',');
766 pchName =
SDL_malloc(pSecondComma - pFirstComma);
771 SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
772 pchName[pSecondComma - pFirstComma - 1] = 0;
782 const char *pFirstComma, *pSecondComma;
788 pSecondComma =
SDL_strchr(pFirstComma + 1,
',');
801 while (gamecontrollerlist) {
802 if (!
SDL_memcmp(&gamecontrollerlist->joystick->guid, &pControllerMapping->
guid,
sizeof(pControllerMapping->
guid))) {
809 event.cdevice.which = gamecontrollerlist->joystick->instance_id;
814 gamecontrollerlist = gamecontrollerlist->next;
830 SDL_SetError(
"Couldn't parse name from %s", mappingString);
842 if (pControllerMapping) {
844 if (pControllerMapping->
priority <= priority) {
847 pControllerMapping->
name = pchName;
849 pControllerMapping->
mapping = pchMapping;
850 pControllerMapping->
priority = priority;
859 pControllerMapping =
SDL_malloc(
sizeof(*pControllerMapping));
860 if (!pControllerMapping) {
866 pControllerMapping->
guid = jGUID;
867 pControllerMapping->
name = pchName;
868 pControllerMapping->
mapping = pchMapping;
870 pControllerMapping->
priority = priority;
878 pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->
next ) {
881 pPrevMapping->
next = pControllerMapping;
887 return pControllerMapping;
897 char name_string[128];
898 char mapping_string[1024];
904 if (!button_mask && !axis_mask) {
913 for (spot = name_string; *spot; ++spot) {
919 SDL_snprintf(mapping_string,
sizeof(mapping_string),
"none,%s,", name_string);
921 SDL_strlcat(mapping_string,
"a:b0,",
sizeof(mapping_string));
924 SDL_strlcat(mapping_string,
"b:b1,",
sizeof(mapping_string));
927 SDL_strlcat(mapping_string,
"b:b4,",
sizeof(mapping_string));
931 SDL_strlcat(mapping_string,
"x:b2,",
sizeof(mapping_string));
934 SDL_strlcat(mapping_string,
"y:b3,",
sizeof(mapping_string));
937 SDL_strlcat(mapping_string,
"back:b4,",
sizeof(mapping_string));
941 SDL_strlcat(mapping_string,
"guide:b5,",
sizeof(mapping_string));
947 SDL_strlcat(mapping_string,
"guide:b6,",
sizeof(mapping_string));
953 SDL_strlcat(mapping_string,
"start:b6,",
sizeof(mapping_string));
956 SDL_strlcat(mapping_string,
"leftstick:b7,",
sizeof(mapping_string));
959 SDL_strlcat(mapping_string,
"rightstick:b8,",
sizeof(mapping_string));
962 SDL_strlcat(mapping_string,
"leftshoulder:b9,",
sizeof(mapping_string));
965 SDL_strlcat(mapping_string,
"rightshoulder:b10,",
sizeof(mapping_string));
968 SDL_strlcat(mapping_string,
"dpup:b11,",
sizeof(mapping_string));
971 SDL_strlcat(mapping_string,
"dpdown:b12,",
sizeof(mapping_string));
974 SDL_strlcat(mapping_string,
"dpleft:b13,",
sizeof(mapping_string));
977 SDL_strlcat(mapping_string,
"dpright:b14,",
sizeof(mapping_string));
980 SDL_strlcat(mapping_string,
"leftx:a0,",
sizeof(mapping_string));
983 SDL_strlcat(mapping_string,
"lefty:a1,",
sizeof(mapping_string));
986 SDL_strlcat(mapping_string,
"rightx:a2,",
sizeof(mapping_string));
989 SDL_strlcat(mapping_string,
"righty:a3,",
sizeof(mapping_string));
992 SDL_strlcat(mapping_string,
"lefttrigger:a4,",
sizeof(mapping_string));
995 SDL_strlcat(mapping_string,
"righttrigger:a5,",
sizeof(mapping_string));
1017 "none,X360 Wireless Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b11,dpright:b12,dpup:b13,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
1030 mapping = SDL_CreateMappingForAndroidController(
name, guid);
1068 char *
buf, *line, *line_end, *tmp, *comma, line_platform[64];
1069 size_t db_size, platform_len;
1081 return SDL_SetError(
"Could not allocate space to read DB into memory");
1096 buf[db_size] =
'\0';
1099 while (line <
buf + db_size) {
1101 if (line_end !=
NULL) {
1104 line_end =
buf + db_size;
1112 if (comma !=
NULL) {
1113 platform_len = comma - tmp + 1;
1124 line = line_end + 1;
1145 if (!mappingString) {
1151 return SDL_SetError(
"Couldn't parse GUID from %s", mappingString);
1164 if (!pControllerMapping) {
1171 if (is_default_mapping) {
1173 }
else if (is_hidapi_mapping) {
1175 }
else if (is_xinput_mapping) {
1197 int num_mappings = 0;
1206 return num_mappings;
1221 if (mapping_index == 0) {
1222 char *pMappingString;
1230 if (!pMappingString) {
1235 return pMappingString;
1248 char *pMappingString =
NULL;
1257 if (!pMappingString) {
1263 return pMappingString;
1272 if (!gamecontroller) {
1283 if (hint && hint[0]) {
1285 char *pUserMappings =
SDL_malloc(nchHints + 1);
1286 char *pTempMappings = pUserMappings;
1288 pUserMappings[nchHints] =
'\0';
1289 while (pUserMappings) {
1290 char *pchNewLine =
NULL;
1292 pchNewLine =
SDL_strchr(pUserMappings,
'\n');
1299 pUserMappings = pchNewLine + 1;
1301 pUserMappings =
NULL;
1315 #ifdef CONTROLLER_MAPPING_FILE
1316 #define STRING(X) SDL_STRINGIFY_ARG(X)
1318 #elif defined(__ANDROID__)
1331 char szControllerMapPath[1024];
1333 const char *pMappingString =
NULL;
1335 while (pMappingString) {
1386 if (pSupportedController) {
1390 return pSupportedController->
name;
1405 char *pMappingString =
NULL;
1419 if (!pMappingString) {
1427 return pMappingString;
1438 if (pSupportedController) {
1451 if (pSupportedController) {
1478 #if defined(__LINUX__)
1479 bSteamVirtualGamepad = (vendor == 0x28DE && product == 0x11FF);
1480 #elif defined(__MACOSX__)
1481 bSteamVirtualGamepad = (vendor == 0x045E && product == 0x028E && version == 1);
1482 #elif defined(__WIN32__)
1486 if (bSteamVirtualGamepad) {
1517 SDL_GameController *
1521 SDL_GameController *gamecontroller;
1522 SDL_GameController *gamecontrollerlist;
1530 while (gamecontrollerlist) {
1531 if (instance_id == gamecontrollerlist->joystick->instance_id) {
1532 gamecontroller = gamecontrollerlist;
1533 ++gamecontroller->ref_count;
1535 return (gamecontroller);
1537 gamecontrollerlist = gamecontrollerlist->next;
1542 if (!pSupportedController) {
1543 SDL_SetError(
"Couldn't find mapping for device (%d)", device_index);
1549 gamecontroller = (SDL_GameController *)
SDL_calloc(1,
sizeof(*gamecontroller));
1550 if (gamecontroller ==
NULL) {
1557 if (!gamecontroller->joystick) {
1563 if (gamecontroller->joystick->naxes) {
1565 if (!gamecontroller->last_match_axis) {
1573 if (gamecontroller->joystick->nhats) {
1574 gamecontroller->last_hat_mask = (
Uint8 *)
SDL_calloc(gamecontroller->joystick->nhats,
sizeof(*gamecontroller->last_hat_mask));
1575 if (!gamecontroller->last_hat_mask) {
1578 SDL_free(gamecontroller->last_match_axis);
1588 ++gamecontroller->ref_count;
1595 return (gamecontroller);
1616 if (!gamecontroller)
1619 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1633 if (valid_input_range) {
1646 if (hat_mask & binding->
input.
hat.hat_mask) {
1657 if (
value != 0 && valid_output_range) {
1673 if (!gamecontroller)
1676 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1686 if (valid_input_range) {
1691 if (valid_input_range) {
1709 if (!gamecontroller)
1712 if (
SDL_strcmp(gamecontroller->name,
"*") == 0) {
1715 return gamecontroller->name;
1750 if (!gamecontroller)
1761 if (!gamecontroller)
1764 return gamecontroller->joystick;
1771 SDL_GameController *
1774 SDL_GameController *gamecontroller;
1778 while (gamecontroller) {
1779 if (gamecontroller->joystick->instance_id == joyid) {
1781 return gamecontroller;
1783 gamecontroller = gamecontroller->next;
1802 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1834 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1862 SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
1864 if (!gamecontroller)
1870 if (--gamecontroller->ref_count > 0) {
1878 gamecontrollerlistprev =
NULL;
1879 while (gamecontrollerlist) {
1880 if (gamecontroller == gamecontrollerlist) {
1881 if (gamecontrollerlistprev) {
1883 gamecontrollerlistprev->next = gamecontrollerlist->next;
1889 gamecontrollerlistprev = gamecontrollerlist;
1890 gamecontrollerlist = gamecontrollerlist->next;
1893 SDL_free(gamecontroller->bindings);
1894 SDL_free(gamecontroller->last_match_axis);
1895 SDL_free(gamecontroller->last_hat_mask);
1956 #if !SDL_EVENTS_DISABLED
1960 event.caxis.which = gamecontroller->joystick->instance_id;
1961 event.caxis.axis =
axis;
1962 event.caxis.value =
value;
1977 #if !SDL_EVENTS_DISABLED
1999 gamecontroller->guide_button_down = now;
2001 if (gamecontroller->joystick->delayed_guide_button) {
2007 gamecontroller->joystick->delayed_guide_button =
SDL_TRUE;
2010 gamecontroller->joystick->delayed_guide_button =
SDL_FALSE;
2016 #if !SDL_EVENTS_DISABLED
2018 event.cbutton.which = gamecontroller->joystick->instance_id;
2019 event.cbutton.button =
button;
2020 event.cbutton.state =
state;
2033 #if SDL_EVENTS_DISABLED
2036 const Uint32 event_list[] = {
2066 while (controllerlist) {
2067 if (controllerlist->joystick ==
joystick) {
2071 controllerlist = controllerlist->next;