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:"
100 } ControllerMapping_t;
144 if (hint && *hint ==
'@') {
166 if (entries ==
NULL) {
208 if (num_events <= 0) {
218 for (
i = 0;
i < num_events; ++
i) {
228 if (
a->outputType !=
b->outputType) {
233 return (
a->output.axis.axis ==
b->output.axis.axis);
235 return (
a->output.button ==
b->output.button);
254 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
274 if (last_match && (!match || !
HasSameOutput(last_match, match))) {
297 gamecontroller->last_match_axis[
axis] = match;
304 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
322 Uint8 last_mask = gamecontroller->last_hat_mask[hat];
325 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
328 if ((changed_mask & binding->
input.
hat.hat_mask) != 0) {
341 gamecontroller->last_hat_mask[hat] =
value;
349 switch(
event->type) {
353 while (controllerlist) {
354 if (controllerlist->joystick->instance_id ==
event->jaxis.which) {
358 controllerlist = controllerlist->next;
366 while (controllerlist) {
367 if (controllerlist->joystick->instance_id ==
event->jbutton.which) {
371 controllerlist = controllerlist->next;
378 while (controllerlist) {
379 if (controllerlist->joystick->instance_id ==
event->jhat.which) {
383 controllerlist = controllerlist->next;
400 while (controllerlist) {
401 if (controllerlist->joystick->instance_id ==
event->jdevice.which) {
411 controllerlist = controllerlist->next;
428 while (pSupportedController) {
429 if (
SDL_memcmp(guid, &pSupportedController->guid,
sizeof(*guid)) == 0) {
430 return pSupportedController;
432 pSupportedController = pSupportedController->next;
439 #if SDL_JOYSTICK_XINPUT
466 if (pchString && (*pchString ==
'+' || *pchString ==
'-')) {
470 if (!pchString || !pchString[0]) {
517 if (!pchString || !pchString[0])
547 char half_axis_input = 0;
548 char half_axis_output = 0;
550 if (*szGameButton ==
'+' || *szGameButton ==
'-') {
551 half_axis_output = *szGameButton++;
563 if (half_axis_output ==
'+') {
566 }
else if (half_axis_output ==
'-') {
578 SDL_SetError(
"Unexpected controller element %s", szGameButton);
582 if (*szJoystickButton ==
'+' || *szJoystickButton ==
'-') {
583 half_axis_input = *szJoystickButton++;
585 if (szJoystickButton[
SDL_strlen(szJoystickButton) - 1] ==
'~') {
589 if (szJoystickButton[0] ==
'a' &&
SDL_isdigit(szJoystickButton[1])) {
592 if (half_axis_input ==
'+') {
595 }
else if (half_axis_input ==
'-') {
607 }
else if (szJoystickButton[0] ==
'b' &&
SDL_isdigit(szJoystickButton[1])) {
610 }
else if (szJoystickButton[0] ==
'h' &&
SDL_isdigit(szJoystickButton[1]) &&
611 szJoystickButton[2] ==
'.' &&
SDL_isdigit(szJoystickButton[3])) {
612 int hat =
SDL_atoi(&szJoystickButton[1]);
618 SDL_SetError(
"Unexpected joystick element: %s", szJoystickButton);
622 ++gamecontroller->num_bindings;
624 if (!gamecontroller->bindings) {
625 gamecontroller->num_bindings = 0;
629 gamecontroller->bindings[gamecontroller->num_bindings - 1] = bind;
639 char szGameButton[20];
640 char szJoystickButton[20];
643 const char *pchPos = pchString;
648 while (pchPos && *pchPos) {
649 if (*pchPos ==
':') {
652 }
else if (*pchPos ==
' ') {
654 }
else if (*pchPos ==
',') {
661 }
else if (bGameButton) {
662 if (
i >=
sizeof(szGameButton)) {
663 SDL_SetError(
"Button name too large: %s", szGameButton);
666 szGameButton[
i] = *pchPos;
669 if (
i >=
sizeof(szJoystickButton)) {
670 SDL_SetError(
"Joystick button name too large: %s", szJoystickButton);
673 szJoystickButton[
i] = *pchPos;
680 if (szGameButton[0] !=
'\0' || szJoystickButton[0] !=
'\0') {
692 gamecontroller->name = pchName;
693 gamecontroller->num_bindings = 0;
694 SDL_memset(gamecontroller->last_match_axis, 0, gamecontroller->joystick->naxes *
sizeof(*gamecontroller->last_match_axis));
699 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
705 if (binding->
input.
axis.axis < gamecontroller->joystick->naxes) {
706 gamecontroller->joystick->axes[binding->
input.
axis.axis].value =
719 const char *pFirstComma =
SDL_strchr(pMapping,
',');
721 char *pchGUID =
SDL_malloc(pFirstComma - pMapping + 1);
726 SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
727 pchGUID[pFirstComma - pMapping] =
'\0';
732 SDL_memcmp(&pchGUID[20],
"504944564944", 12) == 0) {
740 SDL_memcmp(&pchGUID[4],
"000000000000", 12) == 0 &&
741 SDL_memcmp(&pchGUID[20],
"000000000000", 12) == 0) {
758 const char *pFirstComma, *pSecondComma;
765 pSecondComma =
SDL_strchr(pFirstComma + 1,
',');
769 pchName =
SDL_malloc(pSecondComma - pFirstComma);
774 SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
775 pchName[pSecondComma - pFirstComma - 1] = 0;
785 const char *pFirstComma, *pSecondComma;
791 pSecondComma =
SDL_strchr(pFirstComma + 1,
',');
804 while (gamecontrollerlist) {
805 if (!
SDL_memcmp(&gamecontrollerlist->joystick->guid, &pControllerMapping->guid,
sizeof(pControllerMapping->guid))) {
812 event.cdevice.which = gamecontrollerlist->joystick->instance_id;
817 gamecontrollerlist = gamecontrollerlist->next;
824 static ControllerMapping_t *
829 ControllerMapping_t *pControllerMapping;
833 SDL_SetError(
"Couldn't parse name from %s", mappingString);
845 if (pControllerMapping) {
847 if (pControllerMapping->priority <= priority) {
850 pControllerMapping->name = pchName;
851 SDL_free(pControllerMapping->mapping);
852 pControllerMapping->mapping = pchMapping;
853 pControllerMapping->priority = priority;
862 pControllerMapping =
SDL_malloc(
sizeof(*pControllerMapping));
863 if (!pControllerMapping) {
869 pControllerMapping->guid = jGUID;
870 pControllerMapping->name = pchName;
871 pControllerMapping->mapping = pchMapping;
872 pControllerMapping->next =
NULL;
873 pControllerMapping->priority = priority;
877 ControllerMapping_t *pCurrMapping, *pPrevMapping;
881 pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next ) {
884 pPrevMapping->next = pControllerMapping;
890 return pControllerMapping;
897 static ControllerMapping_t *SDL_CreateMappingForAndroidController(
const char *
name,
SDL_JoystickGUID guid)
900 char name_string[128];
901 char mapping_string[1024];
907 if (!button_mask && !axis_mask) {
916 for (spot = name_string; *spot; ++spot) {
922 SDL_snprintf(mapping_string,
sizeof(mapping_string),
"none,%s,", name_string);
924 SDL_strlcat(mapping_string,
"a:b0,",
sizeof(mapping_string));
927 SDL_strlcat(mapping_string,
"b:b1,",
sizeof(mapping_string));
930 SDL_strlcat(mapping_string,
"b:b4,",
sizeof(mapping_string));
934 SDL_strlcat(mapping_string,
"x:b2,",
sizeof(mapping_string));
937 SDL_strlcat(mapping_string,
"y:b3,",
sizeof(mapping_string));
940 SDL_strlcat(mapping_string,
"back:b4,",
sizeof(mapping_string));
944 SDL_strlcat(mapping_string,
"guide:b5,",
sizeof(mapping_string));
950 SDL_strlcat(mapping_string,
"guide:b6,",
sizeof(mapping_string));
956 SDL_strlcat(mapping_string,
"start:b6,",
sizeof(mapping_string));
959 SDL_strlcat(mapping_string,
"leftstick:b7,",
sizeof(mapping_string));
962 SDL_strlcat(mapping_string,
"rightstick:b8,",
sizeof(mapping_string));
965 SDL_strlcat(mapping_string,
"leftshoulder:b9,",
sizeof(mapping_string));
968 SDL_strlcat(mapping_string,
"rightshoulder:b10,",
sizeof(mapping_string));
971 SDL_strlcat(mapping_string,
"dpup:b11,",
sizeof(mapping_string));
974 SDL_strlcat(mapping_string,
"dpdown:b12,",
sizeof(mapping_string));
977 SDL_strlcat(mapping_string,
"dpleft:b13,",
sizeof(mapping_string));
980 SDL_strlcat(mapping_string,
"dpright:b14,",
sizeof(mapping_string));
983 SDL_strlcat(mapping_string,
"leftx:a0,",
sizeof(mapping_string));
986 SDL_strlcat(mapping_string,
"lefty:a1,",
sizeof(mapping_string));
989 SDL_strlcat(mapping_string,
"rightx:a2,",
sizeof(mapping_string));
992 SDL_strlcat(mapping_string,
"righty:a3,",
sizeof(mapping_string));
995 SDL_strlcat(mapping_string,
"lefttrigger:a4,",
sizeof(mapping_string));
998 SDL_strlcat(mapping_string,
"righttrigger:a5,",
sizeof(mapping_string));
1003 int pos = (int)
SDL_strlen(mapping_string) - 1;
1005 if (mapping_string[pos] ==
',') {
1006 mapping_string[pos] =
'\0';
1031 "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",
1044 mapping = SDL_CreateMappingForAndroidController(
name, guid);
1082 char *
buf, *line, *line_end, *tmp, *comma, line_platform[64];
1083 size_t db_size, platform_len;
1095 return SDL_SetError(
"Could not allocate space to read DB into memory");
1110 buf[db_size] =
'\0';
1113 while (line <
buf + db_size) {
1115 if (line_end !=
NULL) {
1118 line_end =
buf + db_size;
1126 if (comma !=
NULL) {
1127 platform_len = comma - tmp + 1;
1138 line = line_end + 1;
1157 ControllerMapping_t *pControllerMapping;
1159 if (!mappingString) {
1165 return SDL_SetError(
"Couldn't parse GUID from %s", mappingString);
1178 if (!pControllerMapping) {
1185 if (is_default_mapping) {
1187 }
else if (is_hidapi_mapping) {
1189 }
else if (is_xinput_mapping) {
1211 int num_mappings = 0;
1220 return num_mappings;
1235 if (mapping_index == 0) {
1236 char *pMappingString;
1244 if (!pMappingString) {
1249 return pMappingString;
1262 char *pMappingString =
NULL;
1271 if (!pMappingString) {
1277 return pMappingString;
1286 if (!gamecontroller) {
1297 if (hint && hint[0]) {
1299 char *pUserMappings =
SDL_malloc(nchHints + 1);
1300 char *pTempMappings = pUserMappings;
1302 pUserMappings[nchHints] =
'\0';
1303 while (pUserMappings) {
1304 char *pchNewLine =
NULL;
1306 pchNewLine =
SDL_strchr(pUserMappings,
'\n');
1313 pUserMappings = pchNewLine + 1;
1315 pUserMappings =
NULL;
1330 if (hint && *hint) {
1334 #if defined(__ANDROID__)
1347 char szControllerMapPath[1024];
1349 const char *pMappingString =
NULL;
1351 while (pMappingString) {
1402 if (pSupportedController) {
1403 if (
SDL_strcmp(pSupportedController->name,
"*") == 0) {
1406 return pSupportedController->name;
1421 char *pMappingString =
NULL;
1435 if (!pMappingString) {
1443 return pMappingString;
1454 if (pSupportedController) {
1467 if (pSupportedController) {
1484 #if defined(__LINUX__)
1501 #if defined(__LINUX__)
1502 bSteamVirtualGamepad = (vendor == 0x28DE && product == 0x11FF);
1503 #elif defined(__MACOSX__)
1504 bSteamVirtualGamepad = (vendor == 0x045E && product == 0x028E && version == 1);
1505 #elif defined(__WIN32__)
1509 if (bSteamVirtualGamepad) {
1540 SDL_GameController *
1544 SDL_GameController *gamecontroller;
1545 SDL_GameController *gamecontrollerlist;
1546 ControllerMapping_t *pSupportedController =
NULL;
1553 while (gamecontrollerlist) {
1554 if (instance_id == gamecontrollerlist->joystick->instance_id) {
1555 gamecontroller = gamecontrollerlist;
1556 ++gamecontroller->ref_count;
1558 return (gamecontroller);
1560 gamecontrollerlist = gamecontrollerlist->next;
1565 if (!pSupportedController) {
1566 SDL_SetError(
"Couldn't find mapping for device (%d)", device_index);
1572 gamecontroller = (SDL_GameController *)
SDL_calloc(1,
sizeof(*gamecontroller));
1573 if (gamecontroller ==
NULL) {
1580 if (!gamecontroller->joystick) {
1586 if (gamecontroller->joystick->naxes) {
1588 if (!gamecontroller->last_match_axis) {
1596 if (gamecontroller->joystick->nhats) {
1597 gamecontroller->last_hat_mask = (
Uint8 *)
SDL_calloc(gamecontroller->joystick->nhats,
sizeof(*gamecontroller->last_hat_mask));
1598 if (!gamecontroller->last_hat_mask) {
1601 SDL_free(gamecontroller->last_match_axis);
1611 ++gamecontroller->ref_count;
1618 return (gamecontroller);
1639 if (!gamecontroller)
1642 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1656 if (valid_input_range) {
1671 if (hat_mask & binding->
input.
hat.hat_mask) {
1682 if (
value != 0 && valid_output_range) {
1698 if (!gamecontroller)
1701 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1711 if (valid_input_range) {
1716 if (valid_input_range) {
1734 if (!gamecontroller)
1737 if (
SDL_strcmp(gamecontroller->name,
"*") == 0) {
1740 return gamecontroller->name;
1775 if (!gamecontroller)
1786 if (!gamecontroller)
1789 return gamecontroller->joystick;
1796 SDL_GameController *
1799 SDL_GameController *gamecontroller;
1803 while (gamecontroller) {
1804 if (gamecontroller->joystick->instance_id == joyid) {
1806 return gamecontroller;
1808 gamecontroller = gamecontroller->next;
1827 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1859 for (
i = 0;
i < gamecontroller->num_bindings; ++
i) {
1887 SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
1889 if (!gamecontroller)
1895 if (--gamecontroller->ref_count > 0) {
1903 gamecontrollerlistprev =
NULL;
1904 while (gamecontrollerlist) {
1905 if (gamecontroller == gamecontrollerlist) {
1906 if (gamecontrollerlistprev) {
1908 gamecontrollerlistprev->next = gamecontrollerlist->next;
1914 gamecontrollerlistprev = gamecontrollerlist;
1915 gamecontrollerlist = gamecontrollerlist->next;
1918 SDL_free(gamecontroller->bindings);
1919 SDL_free(gamecontroller->last_match_axis);
1920 SDL_free(gamecontroller->last_hat_mask);
1944 ControllerMapping_t *pControllerMap;
1981 #if !SDL_EVENTS_DISABLED
1985 event.caxis.which = gamecontroller->joystick->instance_id;
1986 event.caxis.axis =
axis;
1987 event.caxis.value =
value;
2002 #if !SDL_EVENTS_DISABLED
2024 gamecontroller->guide_button_down = now;
2026 if (gamecontroller->joystick->delayed_guide_button) {
2032 gamecontroller->joystick->delayed_guide_button =
SDL_TRUE;
2035 gamecontroller->joystick->delayed_guide_button =
SDL_FALSE;
2041 #if !SDL_EVENTS_DISABLED
2043 event.cbutton.which = gamecontroller->joystick->instance_id;
2044 event.cbutton.button =
button;
2045 event.cbutton.state =
state;
2058 #if SDL_EVENTS_DISABLED
2061 const Uint32 event_list[] = {
2091 while (controllerlist) {
2092 if (controllerlist->joystick ==
joystick) {
2096 controllerlist = controllerlist->next;