controller_type.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright (C) Valve Corporation
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. */
  17. #include "../SDL_internal.h"
  18. #include "SDL_hints.h"
  19. #include "SDL_gamecontroller.h"
  20. #include "controller_type.h"
  21. #include "controller_list.h"
  22. static const char *GetControllerTypeOverride( int nVID, int nPID )
  23. {
  24. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERTYPE);
  25. if (hint) {
  26. char key[32];
  27. const char *spot = NULL;
  28. SDL_snprintf(key, sizeof(key), "0x%.4x/0x%.4x=", nVID, nPID);
  29. spot = SDL_strstr(hint, key);
  30. if (!spot) {
  31. SDL_snprintf(key, sizeof(key), "0x%.4X/0x%.4X=", nVID, nPID);
  32. spot = SDL_strstr(hint, key);
  33. }
  34. if (spot) {
  35. spot += SDL_strlen(key);
  36. if (SDL_strncmp(spot, "k_eControllerType_", 18) == 0) {
  37. spot += 18;
  38. }
  39. return spot;
  40. }
  41. }
  42. return NULL;
  43. }
  44. EControllerType GuessControllerType( int nVID, int nPID )
  45. {
  46. #if 0//def _DEBUG
  47. // Verify that there are no duplicates in the controller list
  48. // If the list were sorted, we could do this much more efficiently, as well as improve lookup speed.
  49. static bool s_bCheckedForDuplicates;
  50. if ( !s_bCheckedForDuplicates )
  51. {
  52. s_bCheckedForDuplicates = true;
  53. int i, j;
  54. for ( i = 0; i < sizeof( arrControllers ) / sizeof( arrControllers[ 0 ] ); ++i )
  55. {
  56. for ( j = i + 1; j < sizeof( arrControllers ) / sizeof( arrControllers[ 0 ] ); ++j )
  57. {
  58. if ( arrControllers[ i ].m_unDeviceID == arrControllers[ j ].m_unDeviceID )
  59. {
  60. Log( "Duplicate controller entry found for VID 0x%.4x PID 0x%.4x\n", ( arrControllers[ i ].m_unDeviceID >> 16 ), arrControllers[ i ].m_unDeviceID & 0xFFFF );
  61. }
  62. }
  63. }
  64. }
  65. #endif // _DEBUG
  66. unsigned int unDeviceID = MAKE_CONTROLLER_ID( nVID, nPID );
  67. int iIndex;
  68. const char *pszOverride = GetControllerTypeOverride( nVID, nPID );
  69. if ( pszOverride )
  70. {
  71. if ( SDL_strncasecmp( pszOverride, "Xbox360", 7 ) == 0 )
  72. {
  73. return k_eControllerType_XBox360Controller;
  74. }
  75. if ( SDL_strncasecmp( pszOverride, "XboxOne", 7 ) == 0 )
  76. {
  77. return k_eControllerType_XBoxOneController;
  78. }
  79. if ( SDL_strncasecmp( pszOverride, "PS3", 3 ) == 0 )
  80. {
  81. return k_eControllerType_PS3Controller;
  82. }
  83. if ( SDL_strncasecmp( pszOverride, "PS4", 3 ) == 0 )
  84. {
  85. return k_eControllerType_PS4Controller;
  86. }
  87. if ( SDL_strncasecmp( pszOverride, "PS5", 3 ) == 0 )
  88. {
  89. return k_eControllerType_PS5Controller;
  90. }
  91. if ( SDL_strncasecmp( pszOverride, "SwitchPro", 9 ) == 0 )
  92. {
  93. return k_eControllerType_SwitchProController;
  94. }
  95. if ( SDL_strncasecmp( pszOverride, "Steam", 5 ) == 0 )
  96. {
  97. return k_eControllerType_SteamController;
  98. }
  99. return k_eControllerType_UnknownNonSteamController;
  100. }
  101. for ( iIndex = 0; iIndex < sizeof( arrControllers ) / sizeof( arrControllers[0] ); ++iIndex )
  102. {
  103. if ( unDeviceID == arrControllers[ iIndex ].m_unDeviceID )
  104. {
  105. return arrControllers[ iIndex ].m_eControllerType;
  106. }
  107. }
  108. return k_eControllerType_UnknownNonSteamController;
  109. }
  110. const char *GuessControllerName( int nVID, int nPID )
  111. {
  112. unsigned int unDeviceID = MAKE_CONTROLLER_ID( nVID, nPID );
  113. int iIndex;
  114. for ( iIndex = 0; iIndex < sizeof( arrControllers ) / sizeof( arrControllers[0] ); ++iIndex )
  115. {
  116. if ( unDeviceID == arrControllers[ iIndex ].m_unDeviceID )
  117. {
  118. return arrControllers[ iIndex ].m_pszName;
  119. }
  120. }
  121. return NULL;
  122. }
  123. #undef MAKE_CONTROLLER_ID
  124. /* vi: set ts=4 sw=4 noexpandtab: */