SDL_joystick.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. // This is the joystick API for Simple DirectMedia Layer
  20. #include "SDL_sysjoystick.h"
  21. #include "../SDL_hints_c.h"
  22. #include "SDL_gamepad_c.h"
  23. #include "SDL_joystick_c.h"
  24. #include "SDL_steam_virtual_gamepad.h"
  25. #include "../events/SDL_events_c.h"
  26. #include "../video/SDL_sysvideo.h"
  27. #include "../sensor/SDL_sensor_c.h"
  28. #include "hidapi/SDL_hidapijoystick_c.h"
  29. // This is included in only one place because it has a large static list of controllers
  30. #include "controller_type.h"
  31. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  32. // Needed for checking for input remapping programs
  33. #include "../core/windows/SDL_windows.h"
  34. #undef UNICODE // We want ASCII functions
  35. #include <tlhelp32.h>
  36. #endif
  37. #ifdef SDL_JOYSTICK_VIRTUAL
  38. #include "./virtual/SDL_virtualjoystick_c.h"
  39. #endif
  40. static SDL_JoystickDriver *SDL_joystick_drivers[] = {
  41. #ifdef SDL_JOYSTICK_HIDAPI // Highest priority driver for supported devices
  42. &SDL_HIDAPI_JoystickDriver,
  43. #endif
  44. #ifdef SDL_JOYSTICK_PRIVATE
  45. &SDL_PRIVATE_JoystickDriver,
  46. #endif
  47. #ifdef SDL_JOYSTICK_GAMEINPUT // Higher priority than other Windows drivers
  48. &SDL_GAMEINPUT_JoystickDriver,
  49. #endif
  50. #ifdef SDL_JOYSTICK_RAWINPUT // Before WINDOWS driver, as WINDOWS wants to check if this driver is handling things
  51. &SDL_RAWINPUT_JoystickDriver,
  52. #endif
  53. #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT) // Before WGI driver, as WGI wants to check if this driver is handling things
  54. &SDL_WINDOWS_JoystickDriver,
  55. #endif
  56. #ifdef SDL_JOYSTICK_WGI
  57. &SDL_WGI_JoystickDriver,
  58. #endif
  59. #ifdef SDL_JOYSTICK_WINMM
  60. &SDL_WINMM_JoystickDriver,
  61. #endif
  62. #ifdef SDL_JOYSTICK_LINUX
  63. &SDL_LINUX_JoystickDriver,
  64. #endif
  65. #ifdef SDL_JOYSTICK_IOKIT
  66. &SDL_DARWIN_JoystickDriver,
  67. #endif
  68. #if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS)) && !defined(SDL_JOYSTICK_DISABLED)
  69. &SDL_IOS_JoystickDriver,
  70. #endif
  71. #ifdef SDL_JOYSTICK_ANDROID
  72. &SDL_ANDROID_JoystickDriver,
  73. #endif
  74. #ifdef SDL_JOYSTICK_EMSCRIPTEN
  75. &SDL_EMSCRIPTEN_JoystickDriver,
  76. #endif
  77. #ifdef SDL_JOYSTICK_HAIKU
  78. &SDL_HAIKU_JoystickDriver,
  79. #endif
  80. #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. */
  81. &SDL_BSD_JoystickDriver,
  82. #endif
  83. #ifdef SDL_JOYSTICK_PS2
  84. &SDL_PS2_JoystickDriver,
  85. #endif
  86. #ifdef SDL_JOYSTICK_PSP
  87. &SDL_PSP_JoystickDriver,
  88. #endif
  89. #ifdef SDL_JOYSTICK_VIRTUAL
  90. &SDL_VIRTUAL_JoystickDriver,
  91. #endif
  92. #ifdef SDL_JOYSTICK_VITA
  93. &SDL_VITA_JoystickDriver,
  94. #endif
  95. #ifdef SDL_JOYSTICK_N3DS
  96. &SDL_N3DS_JoystickDriver
  97. #endif
  98. #if defined(SDL_JOYSTICK_DUMMY) || defined(SDL_JOYSTICK_DISABLED)
  99. &SDL_DUMMY_JoystickDriver
  100. #endif
  101. };
  102. #ifndef SDL_THREAD_SAFETY_ANALYSIS
  103. static
  104. #endif
  105. SDL_Mutex *SDL_joystick_lock = NULL; // This needs to support recursive locks
  106. static SDL_AtomicInt SDL_joystick_lock_pending;
  107. static int SDL_joysticks_locked;
  108. static bool SDL_joysticks_initialized;
  109. static bool SDL_joysticks_quitting;
  110. static bool SDL_joystick_being_added;
  111. static SDL_Joystick *SDL_joysticks SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  112. static int SDL_joystick_player_count SDL_GUARDED_BY(SDL_joystick_lock) = 0;
  113. static SDL_JoystickID *SDL_joystick_players SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
  114. static bool SDL_joystick_allows_background_events = false;
  115. static Uint32 initial_arcadestick_devices[] = {
  116. MAKE_VIDPID(0x0079, 0x181a), // Venom Arcade Stick
  117. MAKE_VIDPID(0x0079, 0x181b), // Venom Arcade Stick
  118. MAKE_VIDPID(0x0c12, 0x0ef6), // Hitbox Arcade Stick
  119. MAKE_VIDPID(0x0e6f, 0x0109), // PDP Versus Fighting Pad
  120. MAKE_VIDPID(0x0f0d, 0x0016), // Hori Real Arcade Pro.EX
  121. MAKE_VIDPID(0x0f0d, 0x001b), // Hori Real Arcade Pro VX
  122. MAKE_VIDPID(0x0f0d, 0x0063), // Hori Real Arcade Pro Hayabusa (USA) Xbox One
  123. MAKE_VIDPID(0x0f0d, 0x006a), // Real Arcade Pro 4
  124. MAKE_VIDPID(0x0f0d, 0x0078), // Hori Real Arcade Pro V Kai Xbox One
  125. MAKE_VIDPID(0x0f0d, 0x008a), // HORI Real Arcade Pro 4
  126. MAKE_VIDPID(0x0f0d, 0x008c), // Hori Real Arcade Pro 4
  127. MAKE_VIDPID(0x0f0d, 0x00aa), // HORI Real Arcade Pro V Hayabusa in Switch Mode
  128. MAKE_VIDPID(0x0f0d, 0x00ed), // Hori Fighting Stick mini 4 kai
  129. MAKE_VIDPID(0x0f0d, 0x011c), // Hori Fighting Stick Alpha in PS4 Mode
  130. MAKE_VIDPID(0x0f0d, 0x011e), // Hori Fighting Stick Alpha in PC Mode
  131. MAKE_VIDPID(0x0f0d, 0x0184), // Hori Fighting Stick Alpha in PS5 Mode
  132. MAKE_VIDPID(0x146b, 0x0604), // NACON Daija Arcade Stick
  133. MAKE_VIDPID(0x1532, 0x0a00), // Razer Atrox Arcade Stick
  134. MAKE_VIDPID(0x1bad, 0xf03d), // Street Fighter IV Arcade Stick TE - Chun Li
  135. MAKE_VIDPID(0x1bad, 0xf502), // Hori Real Arcade Pro.VX SA
  136. MAKE_VIDPID(0x1bad, 0xf504), // Hori Real Arcade Pro. EX
  137. MAKE_VIDPID(0x1bad, 0xf506), // Hori Real Arcade Pro.EX Premium VLX
  138. MAKE_VIDPID(0x20d6, 0xa715), // PowerA Nintendo Switch Fusion Arcade Stick
  139. MAKE_VIDPID(0x24c6, 0x5000), // Razer Atrox Arcade Stick
  140. MAKE_VIDPID(0x24c6, 0x5501), // Hori Real Arcade Pro VX-SA
  141. MAKE_VIDPID(0x24c6, 0x550e), // Hori Real Arcade Pro V Kai 360
  142. MAKE_VIDPID(0x2c22, 0x2300), // Qanba Obsidian Arcade Joystick in PS4 Mode
  143. MAKE_VIDPID(0x2c22, 0x2302), // Qanba Obsidian Arcade Joystick in PS3 Mode
  144. MAKE_VIDPID(0x2c22, 0x2303), // Qanba Obsidian Arcade Joystick in PC Mode
  145. MAKE_VIDPID(0x2c22, 0x2500), // Qanba Dragon Arcade Joystick in PS4 Mode
  146. MAKE_VIDPID(0x2c22, 0x2502), // Qanba Dragon Arcade Joystick in PS3 Mode
  147. MAKE_VIDPID(0x2c22, 0x2503), // Qanba Dragon Arcade Joystick in PC Mode
  148. };
  149. static SDL_vidpid_list arcadestick_devices = {
  150. SDL_HINT_JOYSTICK_ARCADESTICK_DEVICES, 0, 0, NULL,
  151. SDL_HINT_JOYSTICK_ARCADESTICK_DEVICES_EXCLUDED, 0, 0, NULL,
  152. SDL_arraysize(initial_arcadestick_devices), initial_arcadestick_devices,
  153. false
  154. };
  155. /* This list is taken from:
  156. https://raw.githubusercontent.com/denilsonsa/udev-joystick-blacklist/master/generate_rules.py
  157. */
  158. static Uint32 initial_blacklist_devices[] = {
  159. // Microsoft Microsoft Wireless Optical Desktop 2.10
  160. // Microsoft Wireless Desktop - Comfort Edition
  161. MAKE_VIDPID(0x045e, 0x009d),
  162. // Microsoft Microsoft Digital Media Pro Keyboard
  163. // Microsoft Corp. Digital Media Pro Keyboard
  164. MAKE_VIDPID(0x045e, 0x00b0),
  165. // Microsoft Microsoft Digital Media Keyboard
  166. // Microsoft Corp. Digital Media Keyboard 1.0A
  167. MAKE_VIDPID(0x045e, 0x00b4),
  168. // Microsoft Microsoft Digital Media Keyboard 3000
  169. MAKE_VIDPID(0x045e, 0x0730),
  170. // Microsoft Microsoft 2.4GHz Transceiver v6.0
  171. // Microsoft Microsoft 2.4GHz Transceiver v8.0
  172. // Microsoft Corp. Nano Transceiver v1.0 for Bluetooth
  173. // Microsoft Wireless Mobile Mouse 1000
  174. // Microsoft Wireless Desktop 3000
  175. MAKE_VIDPID(0x045e, 0x0745),
  176. // Microsoft SideWinder(TM) 2.4GHz Transceiver
  177. MAKE_VIDPID(0x045e, 0x0748),
  178. // Microsoft Corp. Wired Keyboard 600
  179. MAKE_VIDPID(0x045e, 0x0750),
  180. // Microsoft Corp. Sidewinder X4 keyboard
  181. MAKE_VIDPID(0x045e, 0x0768),
  182. // Microsoft Corp. Arc Touch Mouse Transceiver
  183. MAKE_VIDPID(0x045e, 0x0773),
  184. // Microsoft 2.4GHz Transceiver v9.0
  185. // Microsoft Nano Transceiver v2.1
  186. // Microsoft Sculpt Ergonomic Keyboard (5KV-00001)
  187. MAKE_VIDPID(0x045e, 0x07a5),
  188. // Microsoft Nano Transceiver v1.0
  189. // Microsoft Wireless Keyboard 800
  190. MAKE_VIDPID(0x045e, 0x07b2),
  191. // Microsoft Nano Transceiver v2.0
  192. MAKE_VIDPID(0x045e, 0x0800),
  193. MAKE_VIDPID(0x046d, 0xc30a), // Logitech, Inc. iTouch Composite keyboard
  194. MAKE_VIDPID(0x04d9, 0xa0df), // Tek Syndicate Mouse (E-Signal USB Gaming Mouse)
  195. // List of Wacom devices at: http://linuxwacom.sourceforge.net/wiki/index.php/Device_IDs
  196. MAKE_VIDPID(0x056a, 0x0010), // Wacom ET-0405 Graphire
  197. MAKE_VIDPID(0x056a, 0x0011), // Wacom ET-0405A Graphire2 (4x5)
  198. MAKE_VIDPID(0x056a, 0x0012), // Wacom ET-0507A Graphire2 (5x7)
  199. MAKE_VIDPID(0x056a, 0x0013), // Wacom CTE-430 Graphire3 (4x5)
  200. MAKE_VIDPID(0x056a, 0x0014), // Wacom CTE-630 Graphire3 (6x8)
  201. MAKE_VIDPID(0x056a, 0x0015), // Wacom CTE-440 Graphire4 (4x5)
  202. MAKE_VIDPID(0x056a, 0x0016), // Wacom CTE-640 Graphire4 (6x8)
  203. MAKE_VIDPID(0x056a, 0x0017), // Wacom CTE-450 Bamboo Fun (4x5)
  204. MAKE_VIDPID(0x056a, 0x0018), // Wacom CTE-650 Bamboo Fun 6x8
  205. MAKE_VIDPID(0x056a, 0x0019), // Wacom CTE-631 Bamboo One
  206. MAKE_VIDPID(0x056a, 0x00d1), // Wacom Bamboo Pen and Touch CTH-460
  207. MAKE_VIDPID(0x056a, 0x030e), // Wacom Intuos Pen (S) CTL-480
  208. MAKE_VIDPID(0x09da, 0x054f), // A4 Tech Co., G7 750 mouse
  209. MAKE_VIDPID(0x09da, 0x1410), // A4 Tech Co., Ltd Bloody AL9 mouse
  210. MAKE_VIDPID(0x09da, 0x3043), // A4 Tech Co., Ltd Bloody R8A Gaming Mouse
  211. MAKE_VIDPID(0x09da, 0x31b5), // A4 Tech Co., Ltd Bloody TL80 Terminator Laser Gaming Mouse
  212. MAKE_VIDPID(0x09da, 0x3997), // A4 Tech Co., Ltd Bloody RT7 Terminator Wireless
  213. MAKE_VIDPID(0x09da, 0x3f8b), // A4 Tech Co., Ltd Bloody V8 mouse
  214. MAKE_VIDPID(0x09da, 0x51f4), // Modecom MC-5006 Keyboard
  215. MAKE_VIDPID(0x09da, 0x5589), // A4 Tech Co., Ltd Terminator TL9 Laser Gaming Mouse
  216. MAKE_VIDPID(0x09da, 0x7b22), // A4 Tech Co., Ltd Bloody V5
  217. MAKE_VIDPID(0x09da, 0x7f2d), // A4 Tech Co., Ltd Bloody R3 mouse
  218. MAKE_VIDPID(0x09da, 0x8090), // A4 Tech Co., Ltd X-718BK Oscar Optical Gaming Mouse
  219. MAKE_VIDPID(0x09da, 0x9033), // A4 Tech Co., X7 X-705K
  220. MAKE_VIDPID(0x09da, 0x9066), // A4 Tech Co., Sharkoon Fireglider Optical
  221. MAKE_VIDPID(0x09da, 0x9090), // A4 Tech Co., Ltd XL-730K / XL-750BK / XL-755BK Laser Mouse
  222. MAKE_VIDPID(0x09da, 0x90c0), // A4 Tech Co., Ltd X7 G800V keyboard
  223. MAKE_VIDPID(0x09da, 0xf012), // A4 Tech Co., Ltd Bloody V7 mouse
  224. MAKE_VIDPID(0x09da, 0xf32a), // A4 Tech Co., Ltd Bloody B540 keyboard
  225. MAKE_VIDPID(0x09da, 0xf613), // A4 Tech Co., Ltd Bloody V2 mouse
  226. MAKE_VIDPID(0x09da, 0xf624), // A4 Tech Co., Ltd Bloody B120 Keyboard
  227. MAKE_VIDPID(0x1b1c, 0x1b3c), // Corsair Harpoon RGB gaming mouse
  228. MAKE_VIDPID(0x1d57, 0xad03), // [T3] 2.4GHz and IR Air Mouse Remote Control
  229. MAKE_VIDPID(0x1e7d, 0x2e4a), // Roccat Tyon Mouse
  230. MAKE_VIDPID(0x20a0, 0x422d), // Winkeyless.kr Keyboards
  231. MAKE_VIDPID(0x2516, 0x001f), // Cooler Master Storm Mizar Mouse
  232. MAKE_VIDPID(0x2516, 0x0028), // Cooler Master Storm Alcor Mouse
  233. /*****************************************************************/
  234. // Additional entries
  235. /*****************************************************************/
  236. MAKE_VIDPID(0x04d9, 0x8008), // OBINLB USB-HID Keyboard (Anne Pro II)
  237. MAKE_VIDPID(0x04d9, 0x8009), // OBINLB USB-HID Keyboard (Anne Pro II)
  238. MAKE_VIDPID(0x04d9, 0xa292), // OBINLB USB-HID Keyboard (Anne Pro II)
  239. MAKE_VIDPID(0x04d9, 0xa293), // OBINLB USB-HID Keyboard (Anne Pro II)
  240. MAKE_VIDPID(0x1532, 0x0266), // Razer Huntsman V2 Analog, non-functional DInput device
  241. MAKE_VIDPID(0x1532, 0x0282), // Razer Huntsman Mini Analog, non-functional DInput device
  242. MAKE_VIDPID(0x26ce, 0x01a2), // ASRock LED Controller
  243. MAKE_VIDPID(0x20d6, 0x0002), // PowerA Enhanced Wireless Controller for Nintendo Switch (charging port only)
  244. };
  245. static SDL_vidpid_list blacklist_devices = {
  246. SDL_HINT_JOYSTICK_BLACKLIST_DEVICES, 0, 0, NULL,
  247. SDL_HINT_JOYSTICK_BLACKLIST_DEVICES_EXCLUDED, 0, 0, NULL,
  248. SDL_arraysize(initial_blacklist_devices), initial_blacklist_devices,
  249. false
  250. };
  251. static Uint32 initial_flightstick_devices[] = {
  252. MAKE_VIDPID(0x044f, 0x0402), // HOTAS Warthog Joystick
  253. MAKE_VIDPID(0x0738, 0x2221), // Saitek Pro Flight X-56 Rhino Stick
  254. MAKE_VIDPID(0x044f, 0xb10a), // ThrustMaster, Inc. T.16000M Joystick
  255. MAKE_VIDPID(0x046d, 0xc215), // Logitech Extreme 3D
  256. MAKE_VIDPID(0x231d, 0x0126), // Gunfighter Mk.III 'Space Combat Edition' (right)
  257. MAKE_VIDPID(0x231d, 0x0127), // Gunfighter Mk.III 'Space Combat Edition' (left)
  258. MAKE_VIDPID(0x362c, 0x0001), // Yawman Arrow
  259. };
  260. static SDL_vidpid_list flightstick_devices = {
  261. SDL_HINT_JOYSTICK_FLIGHTSTICK_DEVICES, 0, 0, NULL,
  262. SDL_HINT_JOYSTICK_FLIGHTSTICK_DEVICES_EXCLUDED, 0, 0, NULL,
  263. SDL_arraysize(initial_flightstick_devices), initial_flightstick_devices,
  264. false
  265. };
  266. static Uint32 initial_gamecube_devices[] = {
  267. MAKE_VIDPID(0x0e6f, 0x0185), // PDP Wired Fight Pad Pro for Nintendo Switch
  268. MAKE_VIDPID(0x20d6, 0xa711), // PowerA Wired Controller Nintendo GameCube Style
  269. };
  270. static SDL_vidpid_list gamecube_devices = {
  271. SDL_HINT_JOYSTICK_GAMECUBE_DEVICES, 0, 0, NULL,
  272. SDL_HINT_JOYSTICK_GAMECUBE_DEVICES_EXCLUDED, 0, 0, NULL,
  273. SDL_arraysize(initial_gamecube_devices), initial_gamecube_devices,
  274. false
  275. };
  276. static Uint32 initial_rog_gamepad_mice[] = {
  277. MAKE_VIDPID(0x0b05, 0x1906), // ROG Pugio II
  278. MAKE_VIDPID(0x0b05, 0x1958), // ROG Chakram Core Mouse
  279. MAKE_VIDPID(0x0b05, 0x18e3), // ROG Chakram (wired) Mouse
  280. MAKE_VIDPID(0x0b05, 0x18e5), // ROG Chakram (wireless) Mouse
  281. MAKE_VIDPID(0x0b05, 0x1a18), // ROG Chakram X (wired) Mouse
  282. MAKE_VIDPID(0x0b05, 0x1a1a), // ROG Chakram X (wireless) Mouse
  283. MAKE_VIDPID(0x0b05, 0x1a1c), // ROG Chakram X (Bluetooth) Mouse
  284. };
  285. static SDL_vidpid_list rog_gamepad_mice = {
  286. SDL_HINT_ROG_GAMEPAD_MICE, 0, 0, NULL,
  287. SDL_HINT_ROG_GAMEPAD_MICE_EXCLUDED, 0, 0, NULL,
  288. SDL_arraysize(initial_rog_gamepad_mice), initial_rog_gamepad_mice,
  289. false
  290. };
  291. static Uint32 initial_throttle_devices[] = {
  292. MAKE_VIDPID(0x044f, 0x0404), // HOTAS Warthog Throttle
  293. MAKE_VIDPID(0x0738, 0xa221), // Saitek Pro Flight X-56 Rhino Throttle
  294. };
  295. static SDL_vidpid_list throttle_devices = {
  296. SDL_HINT_JOYSTICK_THROTTLE_DEVICES, 0, 0, NULL,
  297. SDL_HINT_JOYSTICK_THROTTLE_DEVICES_EXCLUDED, 0, 0, NULL,
  298. SDL_arraysize(initial_throttle_devices), initial_throttle_devices,
  299. false
  300. };
  301. static Uint32 initial_wheel_devices[] = {
  302. MAKE_VIDPID(0x0079, 0x1864), // DragonRise Inc. Wired Wheel (active mode) (also known as PXN V900 (PS3), Superdrive SV-750, or a Genesis Seaborg 400)
  303. MAKE_VIDPID(0x046d, 0xc294), // Logitech generic wheel
  304. MAKE_VIDPID(0x046d, 0xc295), // Logitech Momo Force
  305. MAKE_VIDPID(0x046d, 0xc298), // Logitech Driving Force Pro
  306. MAKE_VIDPID(0x046d, 0xc299), // Logitech G25
  307. MAKE_VIDPID(0x046d, 0xc29a), // Logitech Driving Force GT
  308. MAKE_VIDPID(0x046d, 0xc29b), // Logitech G27
  309. MAKE_VIDPID(0x046d, 0xc24f), // Logitech G29 (PS3)
  310. MAKE_VIDPID(0x046d, 0xc260), // Logitech G29 (PS4)
  311. MAKE_VIDPID(0x046d, 0xc261), // Logitech G920 (initial mode)
  312. MAKE_VIDPID(0x046d, 0xc262), // Logitech G920 (active mode)
  313. MAKE_VIDPID(0x046d, 0xc268), // Logitech PRO Racing Wheel (PC mode)
  314. MAKE_VIDPID(0x046d, 0xc269), // Logitech PRO Racing Wheel (PS4/PS5 mode)
  315. MAKE_VIDPID(0x046d, 0xc272), // Logitech PRO Racing Wheel for Xbox (PC mode)
  316. MAKE_VIDPID(0x046d, 0xc26d), // Logitech G923 (Xbox)
  317. MAKE_VIDPID(0x046d, 0xc26e), // Logitech G923
  318. MAKE_VIDPID(0x046d, 0xc266), // Logitech G923 for Playstation 4 and PC (PC mode)
  319. MAKE_VIDPID(0x046d, 0xc267), // Logitech G923 for Playstation 4 and PC (PS4 mode)
  320. MAKE_VIDPID(0x046d, 0xca03), // Logitech Momo Racing
  321. MAKE_VIDPID(0x044f, 0xb65d), // Thrustmaster Wheel FFB
  322. MAKE_VIDPID(0x044f, 0xb66d), // Thrustmaster Wheel FFB
  323. MAKE_VIDPID(0x044f, 0xb677), // Thrustmaster T150
  324. MAKE_VIDPID(0x044f, 0xb696), // Thrustmaster T248
  325. MAKE_VIDPID(0x044f, 0xb66e), // Thrustmaster T300RS (normal mode)
  326. MAKE_VIDPID(0x044f, 0xb66f), // Thrustmaster T300RS (advanced mode)
  327. MAKE_VIDPID(0x044f, 0xb66d), // Thrustmaster T300RS (PS4 mode)
  328. MAKE_VIDPID(0x044f, 0xb65e), // Thrustmaster T500RS
  329. MAKE_VIDPID(0x044f, 0xb664), // Thrustmaster TX (initial mode)
  330. MAKE_VIDPID(0x044f, 0xb669), // Thrustmaster TX (active mode)
  331. MAKE_VIDPID(0x044f, 0xb67f), // Thrustmaster TMX
  332. MAKE_VIDPID(0x044f, 0xb691), // Thrustmaster TS-XW (initial mode)
  333. MAKE_VIDPID(0x044f, 0xb692), // Thrustmaster TS-XW (active mode)
  334. MAKE_VIDPID(0x0483, 0x0522), // Simagic Wheelbase (including M10, Alpha Mini, Alpha, Alpha U)
  335. MAKE_VIDPID(0x0483, 0xa355), // VRS DirectForce Pro Wheel Base
  336. MAKE_VIDPID(0x0eb7, 0x0001), // Fanatec ClubSport Wheel Base V2
  337. MAKE_VIDPID(0x0eb7, 0x0004), // Fanatec ClubSport Wheel Base V2.5
  338. MAKE_VIDPID(0x0eb7, 0x0005), // Fanatec CSL Elite Wheel Base+ (PS4)
  339. MAKE_VIDPID(0x0eb7, 0x0006), // Fanatec Podium Wheel Base DD1
  340. MAKE_VIDPID(0x0eb7, 0x0007), // Fanatec Podium Wheel Base DD2
  341. MAKE_VIDPID(0x0eb7, 0x0011), // Fanatec Forza Motorsport (CSR Wheel / CSR Elite Wheel)
  342. MAKE_VIDPID(0x0eb7, 0x0020), // Fanatec generic wheel / CSL DD / GT DD Pro
  343. MAKE_VIDPID(0x0eb7, 0x0197), // Fanatec Porsche Wheel (Turbo / GT3 RS / Turbo S / GT3 V2 / GT2)
  344. MAKE_VIDPID(0x0eb7, 0x038e), // Fanatec ClubSport Wheel Base V1
  345. MAKE_VIDPID(0x0eb7, 0x0e03), // Fanatec CSL Elite Wheel Base
  346. MAKE_VIDPID(0x11ff, 0x0511), // DragonRise Inc. Wired Wheel (initial mode) (also known as PXN V900 (PS3), Superdrive SV-750, or a Genesis Seaborg 400)
  347. MAKE_VIDPID(0x1209, 0xffb0), // Generic FFBoard OpenFFBoard universal forcefeedback wheel
  348. MAKE_VIDPID(0x16d0, 0x0d5a), // Simucube 1 Wheelbase
  349. MAKE_VIDPID(0x16d0, 0x0d5f), // Simucube 2 Ultimate Wheelbase
  350. MAKE_VIDPID(0x16d0, 0x0d60), // Simucube 2 Pro Wheelbase
  351. MAKE_VIDPID(0x16d0, 0x0d61), // Simucube 2 Sport Wheelbase
  352. MAKE_VIDPID(0x2433, 0xf300), // Asetek SimSports Invicta Wheelbase
  353. MAKE_VIDPID(0x2433, 0xf301), // Asetek SimSports Forte Wheelbase
  354. MAKE_VIDPID(0x2433, 0xf303), // Asetek SimSports La Prima Wheelbase
  355. MAKE_VIDPID(0x2433, 0xf306), // Asetek SimSports Tony Kannan Wheelbase
  356. MAKE_VIDPID(0x3416, 0x0301), // Cammus C5 Wheelbase
  357. MAKE_VIDPID(0x3416, 0x0302), // Cammus C12 Wheelbase
  358. MAKE_VIDPID(0x346e, 0x0000), // Moza R16/R21 Wheelbase
  359. MAKE_VIDPID(0x346e, 0x0002), // Moza R9 Wheelbase
  360. MAKE_VIDPID(0x346e, 0x0004), // Moza R5 Wheelbase
  361. MAKE_VIDPID(0x346e, 0x0005), // Moza R3 Wheelbase
  362. MAKE_VIDPID(0x346e, 0x0006), // Moza R12 Wheelbase
  363. };
  364. static SDL_vidpid_list wheel_devices = {
  365. SDL_HINT_JOYSTICK_WHEEL_DEVICES, 0, 0, NULL,
  366. SDL_HINT_JOYSTICK_WHEEL_DEVICES_EXCLUDED, 0, 0, NULL,
  367. SDL_arraysize(initial_wheel_devices), initial_wheel_devices,
  368. false
  369. };
  370. static Uint32 initial_zero_centered_devices[] = {
  371. MAKE_VIDPID(0x0e8f, 0x3013), // HuiJia SNES USB adapter
  372. MAKE_VIDPID(0x05a0, 0x3232), // 8Bitdo Zero Gamepad
  373. };
  374. static SDL_vidpid_list zero_centered_devices = {
  375. SDL_HINT_JOYSTICK_ZERO_CENTERED_DEVICES, 0, 0, NULL,
  376. NULL, 0, 0, NULL,
  377. SDL_arraysize(initial_zero_centered_devices), initial_zero_centered_devices,
  378. false
  379. };
  380. #define CHECK_JOYSTICK_MAGIC(joystick, result) \
  381. if (!SDL_ObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK)) { \
  382. SDL_InvalidParamError("joystick"); \
  383. SDL_UnlockJoysticks(); \
  384. return result; \
  385. }
  386. bool SDL_JoysticksInitialized(void)
  387. {
  388. return SDL_joysticks_initialized;
  389. }
  390. bool SDL_JoysticksQuitting(void)
  391. {
  392. return SDL_joysticks_quitting;
  393. }
  394. void SDL_LockJoysticks(void)
  395. {
  396. (void)SDL_AtomicIncRef(&SDL_joystick_lock_pending);
  397. SDL_LockMutex(SDL_joystick_lock);
  398. (void)SDL_AtomicDecRef(&SDL_joystick_lock_pending);
  399. ++SDL_joysticks_locked;
  400. }
  401. void SDL_UnlockJoysticks(void)
  402. {
  403. bool last_unlock = false;
  404. --SDL_joysticks_locked;
  405. if (!SDL_joysticks_initialized) {
  406. // NOTE: There's a small window here where another thread could lock the mutex after we've checked for pending locks
  407. if (!SDL_joysticks_locked && SDL_GetAtomicInt(&SDL_joystick_lock_pending) == 0) {
  408. last_unlock = true;
  409. }
  410. }
  411. /* The last unlock after joysticks are uninitialized will cleanup the mutex,
  412. * allowing applications to lock joysticks while reinitializing the system.
  413. */
  414. if (last_unlock) {
  415. SDL_Mutex *joystick_lock = SDL_joystick_lock;
  416. SDL_LockMutex(joystick_lock);
  417. {
  418. SDL_UnlockMutex(SDL_joystick_lock);
  419. SDL_joystick_lock = NULL;
  420. }
  421. SDL_UnlockMutex(joystick_lock);
  422. SDL_DestroyMutex(joystick_lock);
  423. } else {
  424. SDL_UnlockMutex(SDL_joystick_lock);
  425. }
  426. }
  427. bool SDL_JoysticksLocked(void)
  428. {
  429. return (SDL_joysticks_locked > 0);
  430. }
  431. void SDL_AssertJoysticksLocked(void)
  432. {
  433. SDL_assert(SDL_JoysticksLocked());
  434. }
  435. /*
  436. * Get the driver and device index for a joystick instance ID
  437. * This should be called while the joystick lock is held, to prevent another thread from updating the list
  438. */
  439. static bool SDL_GetDriverAndJoystickIndex(SDL_JoystickID instance_id, SDL_JoystickDriver **driver, int *driver_index)
  440. {
  441. int i, num_joysticks, device_index;
  442. SDL_AssertJoysticksLocked();
  443. if (instance_id > 0) {
  444. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  445. num_joysticks = SDL_joystick_drivers[i]->GetCount();
  446. for (device_index = 0; device_index < num_joysticks; ++device_index) {
  447. SDL_JoystickID joystick_id = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);
  448. if (joystick_id == instance_id) {
  449. *driver = SDL_joystick_drivers[i];
  450. *driver_index = device_index;
  451. return true;
  452. }
  453. }
  454. }
  455. }
  456. SDL_SetError("Joystick %" SDL_PRIu32 " not found", instance_id);
  457. return false;
  458. }
  459. static int SDL_FindFreePlayerIndex(void)
  460. {
  461. int player_index;
  462. SDL_AssertJoysticksLocked();
  463. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  464. if (SDL_joystick_players[player_index] == 0) {
  465. break;
  466. }
  467. }
  468. return player_index;
  469. }
  470. static int SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id)
  471. {
  472. int player_index;
  473. SDL_AssertJoysticksLocked();
  474. for (player_index = 0; player_index < SDL_joystick_player_count; ++player_index) {
  475. if (instance_id == SDL_joystick_players[player_index]) {
  476. break;
  477. }
  478. }
  479. if (player_index == SDL_joystick_player_count) {
  480. player_index = -1;
  481. }
  482. return player_index;
  483. }
  484. static SDL_JoystickID SDL_GetJoystickIDForPlayerIndex(int player_index)
  485. {
  486. SDL_AssertJoysticksLocked();
  487. if (player_index < 0 || player_index >= SDL_joystick_player_count) {
  488. return 0;
  489. }
  490. return SDL_joystick_players[player_index];
  491. }
  492. static bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)
  493. {
  494. SDL_JoystickID existing_instance = SDL_GetJoystickIDForPlayerIndex(player_index);
  495. SDL_JoystickDriver *driver;
  496. int device_index;
  497. int existing_player_index;
  498. SDL_AssertJoysticksLocked();
  499. if (player_index >= SDL_joystick_player_count) {
  500. SDL_JoystickID *new_players = (SDL_JoystickID *)SDL_realloc(SDL_joystick_players, (player_index + 1) * sizeof(*SDL_joystick_players));
  501. if (!new_players) {
  502. return false;
  503. }
  504. SDL_joystick_players = new_players;
  505. SDL_memset(&SDL_joystick_players[SDL_joystick_player_count], 0, (player_index - SDL_joystick_player_count + 1) * sizeof(SDL_joystick_players[0]));
  506. SDL_joystick_player_count = player_index + 1;
  507. } else if (player_index >= 0 && SDL_joystick_players[player_index] == instance_id) {
  508. // Joystick is already assigned the requested player index
  509. return true;
  510. }
  511. // Clear the old player index
  512. existing_player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
  513. if (existing_player_index >= 0) {
  514. SDL_joystick_players[existing_player_index] = 0;
  515. }
  516. if (player_index >= 0) {
  517. SDL_joystick_players[player_index] = instance_id;
  518. }
  519. // Update the driver with the new index
  520. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  521. driver->SetDevicePlayerIndex(device_index, player_index);
  522. }
  523. // Move any existing joystick to another slot
  524. if (existing_instance > 0) {
  525. SDL_SetJoystickIDForPlayerIndex(SDL_FindFreePlayerIndex(), existing_instance);
  526. }
  527. return true;
  528. }
  529. static void SDLCALL SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  530. {
  531. if (SDL_GetStringBoolean(hint, false)) {
  532. SDL_joystick_allows_background_events = true;
  533. } else {
  534. SDL_joystick_allows_background_events = false;
  535. }
  536. }
  537. bool SDL_InitJoysticks(void)
  538. {
  539. int i;
  540. bool result = false;
  541. // Create the joystick list lock
  542. if (SDL_joystick_lock == NULL) {
  543. SDL_joystick_lock = SDL_CreateMutex();
  544. }
  545. if (!SDL_InitSubSystem(SDL_INIT_EVENTS)) {
  546. return false;
  547. }
  548. SDL_LockJoysticks();
  549. SDL_joysticks_initialized = true;
  550. SDL_InitGamepadMappings();
  551. SDL_LoadVIDPIDList(&arcadestick_devices);
  552. SDL_LoadVIDPIDList(&blacklist_devices);
  553. SDL_LoadVIDPIDList(&flightstick_devices);
  554. SDL_LoadVIDPIDList(&gamecube_devices);
  555. SDL_LoadVIDPIDList(&rog_gamepad_mice);
  556. SDL_LoadVIDPIDList(&throttle_devices);
  557. SDL_LoadVIDPIDList(&wheel_devices);
  558. SDL_LoadVIDPIDList(&zero_centered_devices);
  559. // See if we should allow joystick events while in the background
  560. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  561. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  562. SDL_InitSteamVirtualGamepadInfo();
  563. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  564. if (SDL_joystick_drivers[i]->Init()) {
  565. result = true;
  566. }
  567. }
  568. SDL_UnlockJoysticks();
  569. if (!result) {
  570. SDL_QuitJoysticks();
  571. }
  572. return result;
  573. }
  574. bool SDL_JoysticksOpened(void)
  575. {
  576. bool opened;
  577. SDL_LockJoysticks();
  578. {
  579. if (SDL_joysticks != NULL) {
  580. opened = true;
  581. } else {
  582. opened = false;
  583. }
  584. }
  585. SDL_UnlockJoysticks();
  586. return opened;
  587. }
  588. bool SDL_JoystickHandledByAnotherDriver(struct SDL_JoystickDriver *driver, Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  589. {
  590. int i;
  591. bool result = false;
  592. SDL_LockJoysticks();
  593. {
  594. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  595. if (driver == SDL_joystick_drivers[i]) {
  596. // Higher priority drivers do not have this device
  597. break;
  598. }
  599. if (SDL_joystick_drivers[i]->IsDevicePresent(vendor_id, product_id, version, name)) {
  600. result = true;
  601. break;
  602. }
  603. }
  604. }
  605. SDL_UnlockJoysticks();
  606. return result;
  607. }
  608. bool SDL_HasJoystick(void)
  609. {
  610. int i;
  611. int total_joysticks = 0;
  612. SDL_LockJoysticks();
  613. {
  614. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  615. total_joysticks += SDL_joystick_drivers[i]->GetCount();
  616. }
  617. }
  618. SDL_UnlockJoysticks();
  619. if (total_joysticks > 0) {
  620. return true;
  621. }
  622. return false;
  623. }
  624. SDL_JoystickID *SDL_GetJoysticks(int *count)
  625. {
  626. int i, num_joysticks, device_index;
  627. int joystick_index = 0, total_joysticks = 0;
  628. SDL_JoystickID *joysticks;
  629. SDL_LockJoysticks();
  630. {
  631. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  632. total_joysticks += SDL_joystick_drivers[i]->GetCount();
  633. }
  634. joysticks = (SDL_JoystickID *)SDL_malloc((total_joysticks + 1) * sizeof(*joysticks));
  635. if (joysticks) {
  636. if (count) {
  637. *count = total_joysticks;
  638. }
  639. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  640. num_joysticks = SDL_joystick_drivers[i]->GetCount();
  641. for (device_index = 0; device_index < num_joysticks; ++device_index) {
  642. SDL_assert(joystick_index < total_joysticks);
  643. joysticks[joystick_index] = SDL_joystick_drivers[i]->GetDeviceInstanceID(device_index);
  644. SDL_assert(joysticks[joystick_index] > 0);
  645. ++joystick_index;
  646. }
  647. }
  648. SDL_assert(joystick_index == total_joysticks);
  649. joysticks[joystick_index] = 0;
  650. } else {
  651. if (count) {
  652. *count = 0;
  653. }
  654. }
  655. }
  656. SDL_UnlockJoysticks();
  657. return joysticks;
  658. }
  659. const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)
  660. {
  661. SDL_JoystickDriver *driver;
  662. int device_index;
  663. const SDL_SteamVirtualGamepadInfo *info = NULL;
  664. if (SDL_SteamVirtualGamepadEnabled() &&
  665. SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  666. info = SDL_GetSteamVirtualGamepadInfo(driver->GetDeviceSteamVirtualGamepadSlot(device_index));
  667. }
  668. return info;
  669. }
  670. /*
  671. * Get the implementation dependent name of a joystick
  672. */
  673. const char *SDL_GetJoystickNameForID(SDL_JoystickID instance_id)
  674. {
  675. SDL_JoystickDriver *driver;
  676. int device_index;
  677. const char *name = NULL;
  678. const SDL_SteamVirtualGamepadInfo *info;
  679. SDL_LockJoysticks();
  680. info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
  681. if (info) {
  682. name = SDL_GetPersistentString(info->name);
  683. } else if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  684. name = SDL_GetPersistentString(driver->GetDeviceName(device_index));
  685. }
  686. SDL_UnlockJoysticks();
  687. return name;
  688. }
  689. /*
  690. * Get the implementation dependent path of a joystick
  691. */
  692. const char *SDL_GetJoystickPathForID(SDL_JoystickID instance_id)
  693. {
  694. SDL_JoystickDriver *driver;
  695. int device_index;
  696. const char *path = NULL;
  697. SDL_LockJoysticks();
  698. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  699. path = SDL_GetPersistentString(driver->GetDevicePath(device_index));
  700. }
  701. SDL_UnlockJoysticks();
  702. if (!path) {
  703. SDL_Unsupported();
  704. }
  705. return path;
  706. }
  707. /*
  708. * Get the player index of a joystick, or -1 if it's not available
  709. */
  710. int SDL_GetJoystickPlayerIndexForID(SDL_JoystickID instance_id)
  711. {
  712. int player_index;
  713. SDL_LockJoysticks();
  714. player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
  715. SDL_UnlockJoysticks();
  716. return player_index;
  717. }
  718. /*
  719. * Return true if this joystick is known to have all axes centered at zero
  720. * This isn't generally needed unless the joystick never generates an initial axis value near zero,
  721. * e.g. it's emulating axes with digital buttons
  722. */
  723. static bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
  724. {
  725. // printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);
  726. if (joystick->naxes == 2) {
  727. // Assume D-pad or thumbstick style axes are centered at 0
  728. return true;
  729. }
  730. return SDL_VIDPIDInList(SDL_GetJoystickVendor(joystick), SDL_GetJoystickProduct(joystick), &zero_centered_devices);
  731. }
  732. static bool IsROGAlly(SDL_Joystick *joystick)
  733. {
  734. Uint16 vendor, product;
  735. SDL_GUID guid = SDL_GetJoystickGUID(joystick);
  736. // The ROG Ally controller spoofs an Xbox 360 controller
  737. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  738. if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX360_WIRED_CONTROLLER) {
  739. // Check to see if this system has the expected sensors
  740. bool has_ally_accel = false;
  741. bool has_ally_gyro = false;
  742. if (SDL_InitSubSystem(SDL_INIT_SENSOR)) {
  743. SDL_SensorID *sensors = SDL_GetSensors(NULL);
  744. if (sensors) {
  745. int i;
  746. for (i = 0; sensors[i]; ++i) {
  747. SDL_SensorID sensor = sensors[i];
  748. if (!has_ally_accel && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_ACCEL) {
  749. const char *sensor_name = SDL_GetSensorNameForID(sensor);
  750. if (sensor_name && SDL_strcmp(sensor_name, "Sensor BMI320 Acc") == 0) {
  751. has_ally_accel = true;
  752. }
  753. }
  754. if (!has_ally_gyro && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_GYRO) {
  755. const char *sensor_name = SDL_GetSensorNameForID(sensor);
  756. if (sensor_name && SDL_strcmp(sensor_name, "Sensor BMI320 Gyr") == 0) {
  757. has_ally_gyro = true;
  758. }
  759. }
  760. }
  761. SDL_free(sensors);
  762. }
  763. SDL_QuitSubSystem(SDL_INIT_SENSOR);
  764. }
  765. if (has_ally_accel && has_ally_gyro) {
  766. return true;
  767. }
  768. }
  769. return false;
  770. }
  771. static bool ShouldAttemptSensorFusion(SDL_Joystick *joystick, bool *invert_sensors)
  772. {
  773. SDL_AssertJoysticksLocked();
  774. *invert_sensors = false;
  775. // The SDL controller sensor API is only available for gamepads (at the moment)
  776. if (!SDL_IsGamepad(joystick->instance_id)) {
  777. return false;
  778. }
  779. // If the controller already has sensors, use those
  780. if (joystick->nsensors > 0) {
  781. return false;
  782. }
  783. const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLER_SENSOR_FUSION);
  784. if (hint && *hint) {
  785. if (*hint == '@' || SDL_strncmp(hint, "0x", 2) == 0) {
  786. SDL_vidpid_list gamepads;
  787. SDL_GUID guid;
  788. Uint16 vendor, product;
  789. bool enabled;
  790. SDL_zero(gamepads);
  791. // See if the gamepad is in our list of devices to enable
  792. guid = SDL_GetJoystickGUID(joystick);
  793. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  794. SDL_LoadVIDPIDListFromHints(&gamepads, hint, NULL);
  795. enabled = SDL_VIDPIDInList(vendor, product, &gamepads);
  796. SDL_FreeVIDPIDList(&gamepads);
  797. if (enabled) {
  798. return true;
  799. }
  800. } else {
  801. return SDL_GetStringBoolean(hint, false);
  802. }
  803. }
  804. // See if this is another known wraparound gamepad
  805. if (joystick->name &&
  806. (SDL_strstr(joystick->name, "Backbone One") ||
  807. SDL_strstr(joystick->name, "Kishi"))) {
  808. return true;
  809. }
  810. if (IsROGAlly(joystick)) {
  811. /* I'm not sure if this is a Windows thing, or a quirk for ROG Ally,
  812. * but we need to invert the sensor data on all axes.
  813. */
  814. *invert_sensors = true;
  815. return true;
  816. }
  817. return false;
  818. }
  819. static void AttemptSensorFusion(SDL_Joystick *joystick, bool invert_sensors)
  820. {
  821. SDL_SensorID *sensors;
  822. unsigned int i, j;
  823. SDL_AssertJoysticksLocked();
  824. if (!SDL_InitSubSystem(SDL_INIT_SENSOR)) {
  825. return;
  826. }
  827. sensors = SDL_GetSensors(NULL);
  828. if (sensors) {
  829. for (i = 0; sensors[i]; ++i) {
  830. SDL_SensorID sensor = sensors[i];
  831. if (!joystick->accel_sensor && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_ACCEL) {
  832. // Increment the sensor subsystem reference count
  833. SDL_InitSubSystem(SDL_INIT_SENSOR);
  834. joystick->accel_sensor = sensor;
  835. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 0.0f);
  836. }
  837. if (!joystick->gyro_sensor && SDL_GetSensorTypeForID(sensor) == SDL_SENSOR_GYRO) {
  838. // Increment the sensor subsystem reference count
  839. SDL_InitSubSystem(SDL_INIT_SENSOR);
  840. joystick->gyro_sensor = sensor;
  841. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 0.0f);
  842. }
  843. }
  844. SDL_free(sensors);
  845. }
  846. SDL_QuitSubSystem(SDL_INIT_SENSOR);
  847. /* SDL defines sensor orientation for phones relative to the natural
  848. orientation, and for gamepads relative to being held in front of you.
  849. When a phone is being used as a gamepad, its orientation changes,
  850. so adjust sensor axes to match.
  851. */
  852. if (SDL_GetNaturalDisplayOrientation(SDL_GetPrimaryDisplay()) == SDL_ORIENTATION_LANDSCAPE) {
  853. /* When a device in landscape orientation is laid flat, the axes change
  854. orientation as follows:
  855. -X to +X becomes -X to +X
  856. -Y to +Y becomes +Z to -Z
  857. -Z to +Z becomes -Y to +Y
  858. */
  859. joystick->sensor_transform[0][0] = 1.0f;
  860. joystick->sensor_transform[1][2] = 1.0f;
  861. joystick->sensor_transform[2][1] = -1.0f;
  862. } else {
  863. /* When a device in portrait orientation is rotated left and laid flat,
  864. the axes change orientation as follows:
  865. -X to +X becomes +Z to -Z
  866. -Y to +Y becomes +X to -X
  867. -Z to +Z becomes -Y to +Y
  868. */
  869. joystick->sensor_transform[0][1] = -1.0f;
  870. joystick->sensor_transform[1][2] = 1.0f;
  871. joystick->sensor_transform[2][0] = -1.0f;
  872. }
  873. if (invert_sensors) {
  874. for (i = 0; i < SDL_arraysize(joystick->sensor_transform); ++i) {
  875. for (j = 0; j < SDL_arraysize(joystick->sensor_transform[i]); ++j) {
  876. joystick->sensor_transform[i][j] *= -1.0f;
  877. }
  878. }
  879. }
  880. }
  881. static void CleanupSensorFusion(SDL_Joystick *joystick)
  882. {
  883. SDL_AssertJoysticksLocked();
  884. if (joystick->accel_sensor || joystick->gyro_sensor) {
  885. if (joystick->accel_sensor) {
  886. if (joystick->accel) {
  887. SDL_CloseSensor(joystick->accel);
  888. joystick->accel = NULL;
  889. }
  890. joystick->accel_sensor = 0;
  891. // Decrement the sensor subsystem reference count
  892. SDL_QuitSubSystem(SDL_INIT_SENSOR);
  893. }
  894. if (joystick->gyro_sensor) {
  895. if (joystick->gyro) {
  896. SDL_CloseSensor(joystick->gyro);
  897. joystick->gyro = NULL;
  898. }
  899. joystick->gyro_sensor = 0;
  900. // Decrement the sensor subsystem reference count
  901. SDL_QuitSubSystem(SDL_INIT_SENSOR);
  902. }
  903. }
  904. }
  905. /*
  906. * Open a joystick for use - the index passed as an argument refers to
  907. * the N'th joystick on the system. This index is the value which will
  908. * identify this joystick in future joystick events.
  909. *
  910. * This function returns a joystick identifier, or NULL if an error occurred.
  911. */
  912. SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
  913. {
  914. SDL_JoystickDriver *driver;
  915. int device_index;
  916. SDL_Joystick *joystick;
  917. SDL_Joystick *joysticklist;
  918. const char *joystickname = NULL;
  919. const char *joystickpath = NULL;
  920. bool invert_sensors = false;
  921. const SDL_SteamVirtualGamepadInfo *info;
  922. SDL_LockJoysticks();
  923. if (!SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  924. SDL_UnlockJoysticks();
  925. return NULL;
  926. }
  927. joysticklist = SDL_joysticks;
  928. /* If the joystick is already open, return it
  929. * it is important that we have a single joystick for each instance id
  930. */
  931. while (joysticklist) {
  932. if (instance_id == joysticklist->instance_id) {
  933. joystick = joysticklist;
  934. ++joystick->ref_count;
  935. SDL_UnlockJoysticks();
  936. return joystick;
  937. }
  938. joysticklist = joysticklist->next;
  939. }
  940. // Create and initialize the joystick
  941. joystick = (SDL_Joystick *)SDL_calloc(1, sizeof(*joystick));
  942. if (!joystick) {
  943. SDL_UnlockJoysticks();
  944. return NULL;
  945. }
  946. SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, true);
  947. joystick->driver = driver;
  948. joystick->instance_id = instance_id;
  949. joystick->attached = true;
  950. joystick->led_expiration = SDL_GetTicks();
  951. joystick->battery_percent = -1;
  952. if (!driver->Open(joystick, device_index)) {
  953. SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, false);
  954. SDL_free(joystick);
  955. SDL_UnlockJoysticks();
  956. return NULL;
  957. }
  958. joystickname = driver->GetDeviceName(device_index);
  959. if (joystickname) {
  960. joystick->name = SDL_strdup(joystickname);
  961. }
  962. joystickpath = driver->GetDevicePath(device_index);
  963. if (joystickpath) {
  964. joystick->path = SDL_strdup(joystickpath);
  965. }
  966. joystick->guid = driver->GetDeviceGUID(device_index);
  967. if (joystick->naxes > 0) {
  968. joystick->axes = (SDL_JoystickAxisInfo *)SDL_calloc(joystick->naxes, sizeof(*joystick->axes));
  969. }
  970. if (joystick->nballs > 0) {
  971. joystick->balls = (SDL_JoystickBallData *)SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
  972. }
  973. if (joystick->nhats > 0) {
  974. joystick->hats = (Uint8 *)SDL_calloc(joystick->nhats, sizeof(*joystick->hats));
  975. }
  976. if (joystick->nbuttons > 0) {
  977. joystick->buttons = (bool *)SDL_calloc(joystick->nbuttons, sizeof(*joystick->buttons));
  978. }
  979. if (((joystick->naxes > 0) && !joystick->axes) ||
  980. ((joystick->nballs > 0) && !joystick->balls) ||
  981. ((joystick->nhats > 0) && !joystick->hats) ||
  982. ((joystick->nbuttons > 0) && !joystick->buttons)) {
  983. SDL_CloseJoystick(joystick);
  984. SDL_UnlockJoysticks();
  985. return NULL;
  986. }
  987. // If this joystick is known to have all zero centered axes, skip the auto-centering code
  988. if (SDL_JoystickAxesCenteredAtZero(joystick)) {
  989. int i;
  990. for (i = 0; i < joystick->naxes; ++i) {
  991. joystick->axes[i].has_initial_value = true;
  992. }
  993. }
  994. // Get the Steam Input API handle
  995. info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
  996. if (info) {
  997. joystick->steam_handle = info->handle;
  998. }
  999. // Use system gyro and accelerometer if the gamepad doesn't have built-in sensors
  1000. if (ShouldAttemptSensorFusion(joystick, &invert_sensors)) {
  1001. AttemptSensorFusion(joystick, invert_sensors);
  1002. }
  1003. // Add joystick to list
  1004. ++joystick->ref_count;
  1005. // Link the joystick in the list
  1006. joystick->next = SDL_joysticks;
  1007. SDL_joysticks = joystick;
  1008. driver->Update(joystick);
  1009. SDL_UnlockJoysticks();
  1010. return joystick;
  1011. }
  1012. SDL_JoystickID SDL_AttachVirtualJoystick(const SDL_VirtualJoystickDesc *desc)
  1013. {
  1014. #ifdef SDL_JOYSTICK_VIRTUAL
  1015. SDL_JoystickID result;
  1016. SDL_LockJoysticks();
  1017. result = SDL_JoystickAttachVirtualInner(desc);
  1018. SDL_UnlockJoysticks();
  1019. return result;
  1020. #else
  1021. SDL_SetError("SDL not built with virtual-joystick support");
  1022. return 0;
  1023. #endif
  1024. }
  1025. bool SDL_DetachVirtualJoystick(SDL_JoystickID instance_id)
  1026. {
  1027. #ifdef SDL_JOYSTICK_VIRTUAL
  1028. bool result;
  1029. SDL_LockJoysticks();
  1030. result = SDL_JoystickDetachVirtualInner(instance_id);
  1031. SDL_UnlockJoysticks();
  1032. return result;
  1033. #else
  1034. return SDL_SetError("SDL not built with virtual-joystick support");
  1035. #endif
  1036. }
  1037. bool SDL_IsJoystickVirtual(SDL_JoystickID instance_id)
  1038. {
  1039. #ifdef SDL_JOYSTICK_VIRTUAL
  1040. SDL_JoystickDriver *driver;
  1041. int device_index;
  1042. bool is_virtual = false;
  1043. SDL_LockJoysticks();
  1044. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  1045. if (driver == &SDL_VIRTUAL_JoystickDriver) {
  1046. is_virtual = true;
  1047. }
  1048. }
  1049. SDL_UnlockJoysticks();
  1050. return is_virtual;
  1051. #else
  1052. return false;
  1053. #endif
  1054. }
  1055. bool SDL_SetJoystickVirtualAxis(SDL_Joystick *joystick, int axis, Sint16 value)
  1056. {
  1057. bool result;
  1058. SDL_LockJoysticks();
  1059. {
  1060. CHECK_JOYSTICK_MAGIC(joystick, false);
  1061. #ifdef SDL_JOYSTICK_VIRTUAL
  1062. result = SDL_SetJoystickVirtualAxisInner(joystick, axis, value);
  1063. #else
  1064. result = SDL_SetError("SDL not built with virtual-joystick support");
  1065. #endif
  1066. }
  1067. SDL_UnlockJoysticks();
  1068. return result;
  1069. }
  1070. bool SDL_SetJoystickVirtualBall(SDL_Joystick *joystick, int ball, Sint16 xrel, Sint16 yrel)
  1071. {
  1072. bool result;
  1073. SDL_LockJoysticks();
  1074. {
  1075. CHECK_JOYSTICK_MAGIC(joystick, false);
  1076. #ifdef SDL_JOYSTICK_VIRTUAL
  1077. result = SDL_SetJoystickVirtualBallInner(joystick, ball, xrel, yrel);
  1078. #else
  1079. result = SDL_SetError("SDL not built with virtual-joystick support");
  1080. #endif
  1081. }
  1082. SDL_UnlockJoysticks();
  1083. return result;
  1084. }
  1085. bool SDL_SetJoystickVirtualButton(SDL_Joystick *joystick, int button, bool down)
  1086. {
  1087. bool result;
  1088. SDL_LockJoysticks();
  1089. {
  1090. CHECK_JOYSTICK_MAGIC(joystick, false);
  1091. #ifdef SDL_JOYSTICK_VIRTUAL
  1092. result = SDL_SetJoystickVirtualButtonInner(joystick, button, down);
  1093. #else
  1094. result = SDL_SetError("SDL not built with virtual-joystick support");
  1095. #endif
  1096. }
  1097. SDL_UnlockJoysticks();
  1098. return result;
  1099. }
  1100. bool SDL_SetJoystickVirtualHat(SDL_Joystick *joystick, int hat, Uint8 value)
  1101. {
  1102. bool result;
  1103. SDL_LockJoysticks();
  1104. {
  1105. CHECK_JOYSTICK_MAGIC(joystick, false);
  1106. #ifdef SDL_JOYSTICK_VIRTUAL
  1107. result = SDL_SetJoystickVirtualHatInner(joystick, hat, value);
  1108. #else
  1109. result = SDL_SetError("SDL not built with virtual-joystick support");
  1110. #endif
  1111. }
  1112. SDL_UnlockJoysticks();
  1113. return result;
  1114. }
  1115. bool SDL_SetJoystickVirtualTouchpad(SDL_Joystick *joystick, int touchpad, int finger, bool down, float x, float y, float pressure)
  1116. {
  1117. bool result;
  1118. SDL_LockJoysticks();
  1119. {
  1120. CHECK_JOYSTICK_MAGIC(joystick, false);
  1121. #ifdef SDL_JOYSTICK_VIRTUAL
  1122. result = SDL_SetJoystickVirtualTouchpadInner(joystick, touchpad, finger, down, x, y, pressure);
  1123. #else
  1124. result = SDL_SetError("SDL not built with virtual-joystick support");
  1125. #endif
  1126. }
  1127. SDL_UnlockJoysticks();
  1128. return result;
  1129. }
  1130. bool SDL_SendJoystickVirtualSensorData(SDL_Joystick *joystick, SDL_SensorType type, Uint64 sensor_timestamp, const float *data, int num_values)
  1131. {
  1132. bool result;
  1133. SDL_LockJoysticks();
  1134. {
  1135. CHECK_JOYSTICK_MAGIC(joystick, false);
  1136. #ifdef SDL_JOYSTICK_VIRTUAL
  1137. result = SDL_SendJoystickVirtualSensorDataInner(joystick, type, sensor_timestamp, data, num_values);
  1138. #else
  1139. result = SDL_SetError("SDL not built with virtual-joystick support");
  1140. #endif
  1141. }
  1142. SDL_UnlockJoysticks();
  1143. return result;
  1144. }
  1145. /*
  1146. * Checks to make sure the joystick is valid.
  1147. */
  1148. bool SDL_IsJoystickValid(SDL_Joystick *joystick)
  1149. {
  1150. SDL_AssertJoysticksLocked();
  1151. return SDL_ObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK);
  1152. }
  1153. bool SDL_PrivateJoystickGetAutoGamepadMapping(SDL_JoystickID instance_id, SDL_GamepadMapping *out)
  1154. {
  1155. SDL_JoystickDriver *driver;
  1156. int device_index;
  1157. bool is_ok = false;
  1158. SDL_LockJoysticks();
  1159. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  1160. is_ok = driver->GetGamepadMapping(device_index, out);
  1161. }
  1162. SDL_UnlockJoysticks();
  1163. return is_ok;
  1164. }
  1165. /*
  1166. * Get the number of multi-dimensional axis controls on a joystick
  1167. */
  1168. int SDL_GetNumJoystickAxes(SDL_Joystick *joystick)
  1169. {
  1170. int result;
  1171. SDL_LockJoysticks();
  1172. {
  1173. CHECK_JOYSTICK_MAGIC(joystick, -1);
  1174. result = joystick->naxes;
  1175. }
  1176. SDL_UnlockJoysticks();
  1177. return result;
  1178. }
  1179. /*
  1180. * Get the number of hats on a joystick
  1181. */
  1182. int SDL_GetNumJoystickHats(SDL_Joystick *joystick)
  1183. {
  1184. int result;
  1185. SDL_LockJoysticks();
  1186. {
  1187. CHECK_JOYSTICK_MAGIC(joystick, -1);
  1188. result = joystick->nhats;
  1189. }
  1190. SDL_UnlockJoysticks();
  1191. return result;
  1192. }
  1193. /*
  1194. * Get the number of trackballs on a joystick
  1195. */
  1196. int SDL_GetNumJoystickBalls(SDL_Joystick *joystick)
  1197. {
  1198. CHECK_JOYSTICK_MAGIC(joystick, -1);
  1199. return joystick->nballs;
  1200. }
  1201. /*
  1202. * Get the number of buttons on a joystick
  1203. */
  1204. int SDL_GetNumJoystickButtons(SDL_Joystick *joystick)
  1205. {
  1206. int result;
  1207. SDL_LockJoysticks();
  1208. {
  1209. CHECK_JOYSTICK_MAGIC(joystick, -1);
  1210. result = joystick->nbuttons;
  1211. }
  1212. SDL_UnlockJoysticks();
  1213. return result;
  1214. }
  1215. /*
  1216. * Get the current state of an axis control on a joystick
  1217. */
  1218. Sint16 SDL_GetJoystickAxis(SDL_Joystick *joystick, int axis)
  1219. {
  1220. Sint16 state;
  1221. SDL_LockJoysticks();
  1222. {
  1223. CHECK_JOYSTICK_MAGIC(joystick, 0);
  1224. if (axis < joystick->naxes) {
  1225. state = joystick->axes[axis].value;
  1226. } else {
  1227. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  1228. state = 0;
  1229. }
  1230. }
  1231. SDL_UnlockJoysticks();
  1232. return state;
  1233. }
  1234. /*
  1235. * Get the initial state of an axis control on a joystick
  1236. */
  1237. bool SDL_GetJoystickAxisInitialState(SDL_Joystick *joystick, int axis, Sint16 *state)
  1238. {
  1239. bool result;
  1240. SDL_LockJoysticks();
  1241. {
  1242. CHECK_JOYSTICK_MAGIC(joystick, false);
  1243. if (axis >= joystick->naxes) {
  1244. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  1245. result = false;
  1246. } else {
  1247. if (state) {
  1248. *state = joystick->axes[axis].initial_value;
  1249. }
  1250. result = joystick->axes[axis].has_initial_value;
  1251. }
  1252. }
  1253. SDL_UnlockJoysticks();
  1254. return result;
  1255. }
  1256. /*
  1257. * Get the current state of a hat on a joystick
  1258. */
  1259. Uint8 SDL_GetJoystickHat(SDL_Joystick *joystick, int hat)
  1260. {
  1261. Uint8 state;
  1262. SDL_LockJoysticks();
  1263. {
  1264. CHECK_JOYSTICK_MAGIC(joystick, 0);
  1265. if (hat < joystick->nhats) {
  1266. state = joystick->hats[hat];
  1267. } else {
  1268. SDL_SetError("Joystick only has %d hats", joystick->nhats);
  1269. state = 0;
  1270. }
  1271. }
  1272. SDL_UnlockJoysticks();
  1273. return state;
  1274. }
  1275. /*
  1276. * Get the ball axis change since the last poll
  1277. */
  1278. bool SDL_GetJoystickBall(SDL_Joystick *joystick, int ball, int *dx, int *dy)
  1279. {
  1280. bool result;
  1281. SDL_LockJoysticks();
  1282. {
  1283. CHECK_JOYSTICK_MAGIC(joystick, false);
  1284. if (ball < joystick->nballs) {
  1285. if (dx) {
  1286. *dx = joystick->balls[ball].dx;
  1287. }
  1288. if (dy) {
  1289. *dy = joystick->balls[ball].dy;
  1290. }
  1291. joystick->balls[ball].dx = 0;
  1292. joystick->balls[ball].dy = 0;
  1293. result = true;
  1294. } else {
  1295. result = SDL_SetError("Joystick only has %d balls", joystick->nballs);
  1296. }
  1297. }
  1298. SDL_UnlockJoysticks();
  1299. return result;
  1300. }
  1301. /*
  1302. * Get the current state of a button on a joystick
  1303. */
  1304. bool SDL_GetJoystickButton(SDL_Joystick *joystick, int button)
  1305. {
  1306. bool down = false;
  1307. SDL_LockJoysticks();
  1308. {
  1309. CHECK_JOYSTICK_MAGIC(joystick, false);
  1310. if (button < joystick->nbuttons) {
  1311. down = joystick->buttons[button];
  1312. } else {
  1313. SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
  1314. }
  1315. }
  1316. SDL_UnlockJoysticks();
  1317. return down;
  1318. }
  1319. /*
  1320. * Return if the joystick in question is currently attached to the system,
  1321. * \return false if not plugged in, true if still present.
  1322. */
  1323. bool SDL_JoystickConnected(SDL_Joystick *joystick)
  1324. {
  1325. bool result;
  1326. SDL_LockJoysticks();
  1327. {
  1328. CHECK_JOYSTICK_MAGIC(joystick, false);
  1329. result = joystick->attached;
  1330. }
  1331. SDL_UnlockJoysticks();
  1332. return result;
  1333. }
  1334. /*
  1335. * Get the instance id for this opened joystick
  1336. */
  1337. SDL_JoystickID SDL_GetJoystickID(SDL_Joystick *joystick)
  1338. {
  1339. SDL_JoystickID result;
  1340. SDL_LockJoysticks();
  1341. {
  1342. CHECK_JOYSTICK_MAGIC(joystick, 0);
  1343. result = joystick->instance_id;
  1344. }
  1345. SDL_UnlockJoysticks();
  1346. return result;
  1347. }
  1348. /*
  1349. * Return the SDL_Joystick associated with an instance id.
  1350. */
  1351. SDL_Joystick *SDL_GetJoystickFromID(SDL_JoystickID instance_id)
  1352. {
  1353. SDL_Joystick *joystick;
  1354. SDL_LockJoysticks();
  1355. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1356. if (joystick->instance_id == instance_id) {
  1357. break;
  1358. }
  1359. }
  1360. SDL_UnlockJoysticks();
  1361. return joystick;
  1362. }
  1363. /**
  1364. * Return the SDL_Joystick associated with a player index.
  1365. */
  1366. SDL_Joystick *SDL_GetJoystickFromPlayerIndex(int player_index)
  1367. {
  1368. SDL_JoystickID instance_id;
  1369. SDL_Joystick *joystick;
  1370. SDL_LockJoysticks();
  1371. instance_id = SDL_GetJoystickIDForPlayerIndex(player_index);
  1372. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1373. if (joystick->instance_id == instance_id) {
  1374. break;
  1375. }
  1376. }
  1377. SDL_UnlockJoysticks();
  1378. return joystick;
  1379. }
  1380. /*
  1381. * Get the properties associated with a joystick
  1382. */
  1383. SDL_PropertiesID SDL_GetJoystickProperties(SDL_Joystick *joystick)
  1384. {
  1385. SDL_PropertiesID result;
  1386. SDL_LockJoysticks();
  1387. {
  1388. CHECK_JOYSTICK_MAGIC(joystick, 0);
  1389. if (joystick->props == 0) {
  1390. joystick->props = SDL_CreateProperties();
  1391. }
  1392. result = joystick->props;
  1393. }
  1394. SDL_UnlockJoysticks();
  1395. return result;
  1396. }
  1397. /*
  1398. * Get the friendly name of this joystick
  1399. */
  1400. const char *SDL_GetJoystickName(SDL_Joystick *joystick)
  1401. {
  1402. const char *result;
  1403. const SDL_SteamVirtualGamepadInfo *info;
  1404. SDL_LockJoysticks();
  1405. {
  1406. CHECK_JOYSTICK_MAGIC(joystick, NULL);
  1407. info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);
  1408. if (info) {
  1409. result = SDL_GetPersistentString(info->name);
  1410. } else {
  1411. result = SDL_GetPersistentString(joystick->name);
  1412. }
  1413. }
  1414. SDL_UnlockJoysticks();
  1415. return result;
  1416. }
  1417. /*
  1418. * Get the implementation dependent path of this joystick
  1419. */
  1420. const char *SDL_GetJoystickPath(SDL_Joystick *joystick)
  1421. {
  1422. const char *result;
  1423. SDL_LockJoysticks();
  1424. {
  1425. CHECK_JOYSTICK_MAGIC(joystick, NULL);
  1426. if (joystick->path) {
  1427. result = SDL_GetPersistentString(joystick->path);
  1428. } else {
  1429. SDL_Unsupported();
  1430. result = NULL;
  1431. }
  1432. }
  1433. SDL_UnlockJoysticks();
  1434. return result;
  1435. }
  1436. /**
  1437. * Get the player index of an opened joystick, or -1 if it's not available
  1438. */
  1439. int SDL_GetJoystickPlayerIndex(SDL_Joystick *joystick)
  1440. {
  1441. int result;
  1442. SDL_LockJoysticks();
  1443. {
  1444. CHECK_JOYSTICK_MAGIC(joystick, -1);
  1445. result = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
  1446. }
  1447. SDL_UnlockJoysticks();
  1448. return result;
  1449. }
  1450. /**
  1451. * Set the player index of an opened joystick
  1452. */
  1453. bool SDL_SetJoystickPlayerIndex(SDL_Joystick *joystick, int player_index)
  1454. {
  1455. bool result;
  1456. SDL_LockJoysticks();
  1457. {
  1458. CHECK_JOYSTICK_MAGIC(joystick, false);
  1459. result = SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
  1460. }
  1461. SDL_UnlockJoysticks();
  1462. return result;
  1463. }
  1464. bool SDL_RumbleJoystick(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  1465. {
  1466. bool result;
  1467. SDL_LockJoysticks();
  1468. {
  1469. CHECK_JOYSTICK_MAGIC(joystick, false);
  1470. if (low_frequency_rumble == joystick->low_frequency_rumble &&
  1471. high_frequency_rumble == joystick->high_frequency_rumble) {
  1472. // Just update the expiration
  1473. result = true;
  1474. } else {
  1475. result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);
  1476. if (result) {
  1477. joystick->rumble_resend = SDL_GetTicks() + SDL_RUMBLE_RESEND_MS;
  1478. if (joystick->rumble_resend == 0) {
  1479. joystick->rumble_resend = 1;
  1480. }
  1481. } else {
  1482. joystick->rumble_resend = 0;
  1483. }
  1484. }
  1485. if (result) {
  1486. joystick->low_frequency_rumble = low_frequency_rumble;
  1487. joystick->high_frequency_rumble = high_frequency_rumble;
  1488. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  1489. joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  1490. if (!joystick->rumble_expiration) {
  1491. joystick->rumble_expiration = 1;
  1492. }
  1493. } else {
  1494. joystick->rumble_expiration = 0;
  1495. joystick->rumble_resend = 0;
  1496. }
  1497. }
  1498. }
  1499. SDL_UnlockJoysticks();
  1500. return result;
  1501. }
  1502. bool SDL_RumbleJoystickTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
  1503. {
  1504. bool result;
  1505. SDL_LockJoysticks();
  1506. {
  1507. CHECK_JOYSTICK_MAGIC(joystick, false);
  1508. if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {
  1509. // Just update the expiration
  1510. result = true;
  1511. } else {
  1512. result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);
  1513. }
  1514. if (result) {
  1515. joystick->left_trigger_rumble = left_rumble;
  1516. joystick->right_trigger_rumble = right_rumble;
  1517. if ((left_rumble || right_rumble) && duration_ms) {
  1518. joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  1519. } else {
  1520. joystick->trigger_rumble_expiration = 0;
  1521. }
  1522. }
  1523. }
  1524. SDL_UnlockJoysticks();
  1525. return result;
  1526. }
  1527. bool SDL_SetJoystickLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1528. {
  1529. bool result;
  1530. bool isfreshvalue;
  1531. SDL_LockJoysticks();
  1532. {
  1533. CHECK_JOYSTICK_MAGIC(joystick, false);
  1534. isfreshvalue = red != joystick->led_red ||
  1535. green != joystick->led_green ||
  1536. blue != joystick->led_blue;
  1537. if (isfreshvalue || SDL_GetTicks() >= joystick->led_expiration) {
  1538. result = joystick->driver->SetLED(joystick, red, green, blue);
  1539. joystick->led_expiration = SDL_GetTicks() + SDL_LED_MIN_REPEAT_MS;
  1540. } else {
  1541. // Avoid spamming the driver
  1542. result = true;
  1543. }
  1544. // Save the LED value regardless of success, so we don't spam the driver
  1545. joystick->led_red = red;
  1546. joystick->led_green = green;
  1547. joystick->led_blue = blue;
  1548. }
  1549. SDL_UnlockJoysticks();
  1550. return result;
  1551. }
  1552. bool SDL_SendJoystickEffect(SDL_Joystick *joystick, const void *data, int size)
  1553. {
  1554. bool result;
  1555. SDL_LockJoysticks();
  1556. {
  1557. CHECK_JOYSTICK_MAGIC(joystick, false);
  1558. result = joystick->driver->SendEffect(joystick, data, size);
  1559. }
  1560. SDL_UnlockJoysticks();
  1561. return result;
  1562. }
  1563. /*
  1564. * Close a joystick previously opened with SDL_OpenJoystick()
  1565. */
  1566. void SDL_CloseJoystick(SDL_Joystick *joystick)
  1567. {
  1568. SDL_Joystick *joysticklist;
  1569. SDL_Joystick *joysticklistprev;
  1570. int i;
  1571. SDL_LockJoysticks();
  1572. {
  1573. CHECK_JOYSTICK_MAGIC(joystick,);
  1574. // First decrement ref count
  1575. if (--joystick->ref_count > 0) {
  1576. SDL_UnlockJoysticks();
  1577. return;
  1578. }
  1579. SDL_DestroyProperties(joystick->props);
  1580. if (joystick->rumble_expiration) {
  1581. SDL_RumbleJoystick(joystick, 0, 0, 0);
  1582. }
  1583. if (joystick->trigger_rumble_expiration) {
  1584. SDL_RumbleJoystickTriggers(joystick, 0, 0, 0);
  1585. }
  1586. CleanupSensorFusion(joystick);
  1587. joystick->driver->Close(joystick);
  1588. joystick->hwdata = NULL;
  1589. SDL_SetObjectValid(joystick, SDL_OBJECT_TYPE_JOYSTICK, false);
  1590. joysticklist = SDL_joysticks;
  1591. joysticklistprev = NULL;
  1592. while (joysticklist) {
  1593. if (joystick == joysticklist) {
  1594. if (joysticklistprev) {
  1595. // unlink this entry
  1596. joysticklistprev->next = joysticklist->next;
  1597. } else {
  1598. SDL_joysticks = joystick->next;
  1599. }
  1600. break;
  1601. }
  1602. joysticklistprev = joysticklist;
  1603. joysticklist = joysticklist->next;
  1604. }
  1605. // Free the data associated with this joystick
  1606. SDL_free(joystick->name);
  1607. SDL_free(joystick->path);
  1608. SDL_free(joystick->serial);
  1609. SDL_free(joystick->axes);
  1610. SDL_free(joystick->balls);
  1611. SDL_free(joystick->hats);
  1612. SDL_free(joystick->buttons);
  1613. for (i = 0; i < joystick->ntouchpads; i++) {
  1614. SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
  1615. SDL_free(touchpad->fingers);
  1616. }
  1617. SDL_free(joystick->touchpads);
  1618. SDL_free(joystick->sensors);
  1619. SDL_free(joystick);
  1620. }
  1621. SDL_UnlockJoysticks();
  1622. }
  1623. void SDL_QuitJoysticks(void)
  1624. {
  1625. int i;
  1626. SDL_JoystickID *joysticks;
  1627. SDL_LockJoysticks();
  1628. SDL_joysticks_quitting = true;
  1629. joysticks = SDL_GetJoysticks(NULL);
  1630. if (joysticks) {
  1631. for (i = 0; joysticks[i]; ++i) {
  1632. SDL_PrivateJoystickRemoved(joysticks[i]);
  1633. }
  1634. SDL_free(joysticks);
  1635. }
  1636. while (SDL_joysticks) {
  1637. SDL_joysticks->ref_count = 1;
  1638. SDL_CloseJoystick(SDL_joysticks);
  1639. }
  1640. // Quit drivers in reverse order to avoid breaking dependencies between drivers
  1641. for (i = SDL_arraysize(SDL_joystick_drivers) - 1; i >= 0; --i) {
  1642. SDL_joystick_drivers[i]->Quit();
  1643. }
  1644. if (SDL_joystick_players) {
  1645. SDL_free(SDL_joystick_players);
  1646. SDL_joystick_players = NULL;
  1647. SDL_joystick_player_count = 0;
  1648. }
  1649. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  1650. SDL_QuitSteamVirtualGamepadInfo();
  1651. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  1652. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  1653. SDL_FreeVIDPIDList(&arcadestick_devices);
  1654. SDL_FreeVIDPIDList(&blacklist_devices);
  1655. SDL_FreeVIDPIDList(&flightstick_devices);
  1656. SDL_FreeVIDPIDList(&gamecube_devices);
  1657. SDL_FreeVIDPIDList(&rog_gamepad_mice);
  1658. SDL_FreeVIDPIDList(&throttle_devices);
  1659. SDL_FreeVIDPIDList(&wheel_devices);
  1660. SDL_FreeVIDPIDList(&zero_centered_devices);
  1661. SDL_QuitGamepadMappings();
  1662. SDL_joysticks_quitting = false;
  1663. SDL_joysticks_initialized = false;
  1664. SDL_UnlockJoysticks();
  1665. }
  1666. static bool SDL_PrivateJoystickShouldIgnoreEvent(void)
  1667. {
  1668. if (SDL_joystick_allows_background_events) {
  1669. return false;
  1670. }
  1671. if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) {
  1672. // We have windows but we don't have focus, ignore the event.
  1673. return true;
  1674. }
  1675. return false;
  1676. }
  1677. // These are global for SDL_sysjoystick.c and SDL_events.c
  1678. void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers)
  1679. {
  1680. int ntouchpads;
  1681. SDL_JoystickTouchpadInfo *touchpads;
  1682. SDL_AssertJoysticksLocked();
  1683. ntouchpads = joystick->ntouchpads + 1;
  1684. touchpads = (SDL_JoystickTouchpadInfo *)SDL_realloc(joystick->touchpads, (ntouchpads * sizeof(SDL_JoystickTouchpadInfo)));
  1685. if (touchpads) {
  1686. SDL_JoystickTouchpadInfo *touchpad = &touchpads[ntouchpads - 1];
  1687. SDL_JoystickTouchpadFingerInfo *fingers = (SDL_JoystickTouchpadFingerInfo *)SDL_calloc(nfingers, sizeof(SDL_JoystickTouchpadFingerInfo));
  1688. if (fingers) {
  1689. touchpad->nfingers = nfingers;
  1690. touchpad->fingers = fingers;
  1691. } else {
  1692. // Out of memory, this touchpad won't be active
  1693. touchpad->nfingers = 0;
  1694. touchpad->fingers = NULL;
  1695. }
  1696. joystick->ntouchpads = ntouchpads;
  1697. joystick->touchpads = touchpads;
  1698. }
  1699. }
  1700. void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate)
  1701. {
  1702. int nsensors;
  1703. SDL_JoystickSensorInfo *sensors;
  1704. SDL_AssertJoysticksLocked();
  1705. nsensors = joystick->nsensors + 1;
  1706. sensors = (SDL_JoystickSensorInfo *)SDL_realloc(joystick->sensors, (nsensors * sizeof(SDL_JoystickSensorInfo)));
  1707. if (sensors) {
  1708. SDL_JoystickSensorInfo *sensor = &sensors[nsensors - 1];
  1709. SDL_zerop(sensor);
  1710. sensor->type = type;
  1711. sensor->rate = rate;
  1712. joystick->nsensors = nsensors;
  1713. joystick->sensors = sensors;
  1714. }
  1715. }
  1716. void SDL_PrivateJoystickSensorRate(SDL_Joystick *joystick, SDL_SensorType type, float rate)
  1717. {
  1718. int i;
  1719. SDL_AssertJoysticksLocked();
  1720. for (i = 0; i < joystick->nsensors; ++i) {
  1721. if (joystick->sensors[i].type == type) {
  1722. joystick->sensors[i].rate = rate;
  1723. }
  1724. }
  1725. }
  1726. void SDL_PrivateJoystickAdded(SDL_JoystickID instance_id)
  1727. {
  1728. SDL_JoystickDriver *driver;
  1729. int device_index;
  1730. int player_index = -1;
  1731. SDL_AssertJoysticksLocked();
  1732. if (SDL_JoysticksQuitting()) {
  1733. return;
  1734. }
  1735. SDL_joystick_being_added = true;
  1736. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  1737. player_index = driver->GetDeviceSteamVirtualGamepadSlot(device_index);
  1738. if (player_index < 0) {
  1739. player_index = driver->GetDevicePlayerIndex(device_index);
  1740. }
  1741. }
  1742. if (player_index < 0 && SDL_IsGamepad(instance_id)) {
  1743. player_index = SDL_FindFreePlayerIndex();
  1744. }
  1745. if (player_index >= 0) {
  1746. SDL_SetJoystickIDForPlayerIndex(player_index, instance_id);
  1747. }
  1748. {
  1749. SDL_Event event;
  1750. event.type = SDL_EVENT_JOYSTICK_ADDED;
  1751. event.common.timestamp = 0;
  1752. if (SDL_EventEnabled(event.type)) {
  1753. event.jdevice.which = instance_id;
  1754. SDL_PushEvent(&event);
  1755. }
  1756. }
  1757. SDL_joystick_being_added = false;
  1758. if (SDL_IsGamepad(instance_id)) {
  1759. SDL_PrivateGamepadAdded(instance_id);
  1760. }
  1761. }
  1762. bool SDL_IsJoystickBeingAdded(void)
  1763. {
  1764. return SDL_joystick_being_added;
  1765. }
  1766. void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
  1767. {
  1768. Uint8 i, j;
  1769. Uint64 timestamp = SDL_GetTicksNS();
  1770. SDL_AssertJoysticksLocked();
  1771. // Tell the app that everything is centered/unpressed...
  1772. for (i = 0; i < joystick->naxes; i++) {
  1773. if (joystick->axes[i].has_initial_value) {
  1774. SDL_SendJoystickAxis(timestamp, joystick, i, joystick->axes[i].zero);
  1775. }
  1776. }
  1777. for (i = 0; i < joystick->nbuttons; i++) {
  1778. SDL_SendJoystickButton(timestamp, joystick, i, false);
  1779. }
  1780. for (i = 0; i < joystick->nhats; i++) {
  1781. SDL_SendJoystickHat(timestamp, joystick, i, SDL_HAT_CENTERED);
  1782. }
  1783. for (i = 0; i < joystick->ntouchpads; i++) {
  1784. SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
  1785. for (j = 0; j < touchpad->nfingers; ++j) {
  1786. SDL_SendJoystickTouchpad(timestamp, joystick, i, j, false, 0.0f, 0.0f, 0.0f);
  1787. }
  1788. }
  1789. }
  1790. void SDL_PrivateJoystickRemoved(SDL_JoystickID instance_id)
  1791. {
  1792. SDL_Joystick *joystick = NULL;
  1793. int player_index;
  1794. SDL_Event event;
  1795. SDL_AssertJoysticksLocked();
  1796. // Find this joystick...
  1797. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1798. if (joystick->instance_id == instance_id) {
  1799. SDL_PrivateJoystickForceRecentering(joystick);
  1800. joystick->attached = false;
  1801. break;
  1802. }
  1803. }
  1804. if (SDL_IsGamepad(instance_id)) {
  1805. SDL_PrivateGamepadRemoved(instance_id);
  1806. }
  1807. event.type = SDL_EVENT_JOYSTICK_REMOVED;
  1808. event.common.timestamp = 0;
  1809. if (SDL_EventEnabled(event.type)) {
  1810. event.jdevice.which = instance_id;
  1811. SDL_PushEvent(&event);
  1812. }
  1813. player_index = SDL_GetPlayerIndexForJoystickID(instance_id);
  1814. if (player_index >= 0) {
  1815. SDL_joystick_players[player_index] = 0;
  1816. }
  1817. }
  1818. void SDL_SendJoystickAxis(Uint64 timestamp, SDL_Joystick *joystick, Uint8 axis, Sint16 value)
  1819. {
  1820. SDL_JoystickAxisInfo *info;
  1821. SDL_AssertJoysticksLocked();
  1822. // Make sure we're not getting garbage or duplicate events
  1823. if (axis >= joystick->naxes) {
  1824. return;
  1825. }
  1826. info = &joystick->axes[axis];
  1827. if (!info->has_initial_value ||
  1828. (!info->has_second_value && (info->initial_value <= -32767 || info->initial_value == 32767) && SDL_abs(value) < (SDL_JOYSTICK_AXIS_MAX / 4))) {
  1829. info->initial_value = value;
  1830. info->value = value;
  1831. info->zero = value;
  1832. info->has_initial_value = true;
  1833. } else if (value == info->value && !info->sending_initial_value) {
  1834. return;
  1835. } else {
  1836. info->has_second_value = true;
  1837. }
  1838. if (!info->sent_initial_value) {
  1839. // Make sure we don't send motion until there's real activity on this axis
  1840. const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; // ShanWan PS3 controller needed 96
  1841. if (SDL_abs(value - info->value) <= MAX_ALLOWED_JITTER &&
  1842. !SDL_IsJoystickVIRTUAL(joystick->guid)) {
  1843. return;
  1844. }
  1845. info->sent_initial_value = true;
  1846. info->sending_initial_value = true;
  1847. SDL_SendJoystickAxis(timestamp, joystick, axis, info->initial_value);
  1848. info->sending_initial_value = false;
  1849. }
  1850. /* We ignore events if we don't have keyboard focus, except for centering
  1851. * events.
  1852. */
  1853. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1854. if (info->sending_initial_value ||
  1855. (value > info->zero && value >= info->value) ||
  1856. (value < info->zero && value <= info->value)) {
  1857. return;
  1858. }
  1859. }
  1860. // Update internal joystick state
  1861. SDL_assert(timestamp != 0);
  1862. info->value = value;
  1863. joystick->update_complete = timestamp;
  1864. // Post the event, if desired
  1865. if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_AXIS_MOTION)) {
  1866. SDL_Event event;
  1867. event.type = SDL_EVENT_JOYSTICK_AXIS_MOTION;
  1868. event.common.timestamp = timestamp;
  1869. event.jaxis.which = joystick->instance_id;
  1870. event.jaxis.axis = axis;
  1871. event.jaxis.value = value;
  1872. SDL_PushEvent(&event);
  1873. }
  1874. }
  1875. void SDL_SendJoystickBall(Uint64 timestamp, SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
  1876. {
  1877. SDL_AssertJoysticksLocked();
  1878. // Make sure we're not getting garbage events
  1879. if (ball >= joystick->nballs) {
  1880. return;
  1881. }
  1882. // We ignore events if we don't have keyboard focus.
  1883. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1884. return;
  1885. }
  1886. // Update internal mouse state
  1887. joystick->balls[ball].dx += xrel;
  1888. joystick->balls[ball].dy += yrel;
  1889. // Post the event, if desired
  1890. if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BALL_MOTION)) {
  1891. SDL_Event event;
  1892. event.type = SDL_EVENT_JOYSTICK_BALL_MOTION;
  1893. event.common.timestamp = timestamp;
  1894. event.jball.which = joystick->instance_id;
  1895. event.jball.ball = ball;
  1896. event.jball.xrel = xrel;
  1897. event.jball.yrel = yrel;
  1898. SDL_PushEvent(&event);
  1899. }
  1900. }
  1901. void SDL_SendJoystickHat(Uint64 timestamp, SDL_Joystick *joystick, Uint8 hat, Uint8 value)
  1902. {
  1903. SDL_AssertJoysticksLocked();
  1904. // Make sure we're not getting garbage or duplicate events
  1905. if (hat >= joystick->nhats) {
  1906. return;
  1907. }
  1908. if (value == joystick->hats[hat]) {
  1909. return;
  1910. }
  1911. /* We ignore events if we don't have keyboard focus, except for centering
  1912. * events.
  1913. */
  1914. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1915. if (value != SDL_HAT_CENTERED) {
  1916. return;
  1917. }
  1918. }
  1919. // Update internal joystick state
  1920. SDL_assert(timestamp != 0);
  1921. joystick->hats[hat] = value;
  1922. joystick->update_complete = timestamp;
  1923. // Post the event, if desired
  1924. if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_HAT_MOTION)) {
  1925. SDL_Event event;
  1926. event.type = SDL_EVENT_JOYSTICK_HAT_MOTION;
  1927. event.common.timestamp = timestamp;
  1928. event.jhat.which = joystick->instance_id;
  1929. event.jhat.hat = hat;
  1930. event.jhat.value = value;
  1931. SDL_PushEvent(&event);
  1932. }
  1933. }
  1934. void SDL_SendJoystickButton(Uint64 timestamp, SDL_Joystick *joystick, Uint8 button, bool down)
  1935. {
  1936. SDL_Event event;
  1937. SDL_AssertJoysticksLocked();
  1938. if (down) {
  1939. event.type = SDL_EVENT_JOYSTICK_BUTTON_DOWN;
  1940. } else {
  1941. event.type = SDL_EVENT_JOYSTICK_BUTTON_UP;
  1942. }
  1943. // Make sure we're not getting garbage or duplicate events
  1944. if (button >= joystick->nbuttons) {
  1945. return;
  1946. }
  1947. if (down == joystick->buttons[button]) {
  1948. return;
  1949. }
  1950. /* We ignore events if we don't have keyboard focus, except for button
  1951. * release. */
  1952. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  1953. if (down) {
  1954. return;
  1955. }
  1956. }
  1957. // Update internal joystick state
  1958. SDL_assert(timestamp != 0);
  1959. joystick->buttons[button] = down;
  1960. joystick->update_complete = timestamp;
  1961. // Post the event, if desired
  1962. if (SDL_EventEnabled(event.type)) {
  1963. event.common.timestamp = timestamp;
  1964. event.jbutton.which = joystick->instance_id;
  1965. event.jbutton.button = button;
  1966. event.jbutton.down = down;
  1967. SDL_PushEvent(&event);
  1968. }
  1969. }
  1970. static void SendSteamHandleUpdateEvents(void)
  1971. {
  1972. SDL_Joystick *joystick;
  1973. const SDL_SteamVirtualGamepadInfo *info;
  1974. // Check to see if any Steam handles changed
  1975. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  1976. bool changed = false;
  1977. if (!SDL_IsGamepad(joystick->instance_id)) {
  1978. continue;
  1979. }
  1980. info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);
  1981. if (info) {
  1982. if (joystick->steam_handle != info->handle) {
  1983. joystick->steam_handle = info->handle;
  1984. changed = true;
  1985. }
  1986. } else {
  1987. if (joystick->steam_handle != 0) {
  1988. joystick->steam_handle = 0;
  1989. changed = true;
  1990. }
  1991. }
  1992. if (changed) {
  1993. SDL_Event event;
  1994. SDL_zero(event);
  1995. event.type = SDL_EVENT_GAMEPAD_STEAM_HANDLE_UPDATED;
  1996. event.common.timestamp = 0;
  1997. event.gdevice.which = joystick->instance_id;
  1998. SDL_PushEvent(&event);
  1999. }
  2000. }
  2001. }
  2002. void SDL_UpdateJoysticks(void)
  2003. {
  2004. int i;
  2005. Uint64 now;
  2006. SDL_Joystick *joystick;
  2007. if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
  2008. return;
  2009. }
  2010. SDL_LockJoysticks();
  2011. if (SDL_UpdateSteamVirtualGamepadInfo()) {
  2012. SendSteamHandleUpdateEvents();
  2013. }
  2014. #ifdef SDL_JOYSTICK_HIDAPI
  2015. // Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks
  2016. HIDAPI_UpdateDevices();
  2017. #endif // SDL_JOYSTICK_HIDAPI
  2018. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  2019. if (!joystick->attached) {
  2020. continue;
  2021. }
  2022. joystick->driver->Update(joystick);
  2023. if (joystick->delayed_guide_button) {
  2024. SDL_GamepadHandleDelayedGuideButton(joystick);
  2025. }
  2026. now = SDL_GetTicks();
  2027. if (joystick->rumble_expiration && now >= joystick->rumble_expiration) {
  2028. SDL_RumbleJoystick(joystick, 0, 0, 0);
  2029. joystick->rumble_resend = 0;
  2030. }
  2031. if (joystick->rumble_resend && now >= joystick->rumble_resend) {
  2032. joystick->driver->Rumble(joystick, joystick->low_frequency_rumble, joystick->high_frequency_rumble);
  2033. joystick->rumble_resend = now + SDL_RUMBLE_RESEND_MS;
  2034. if (joystick->rumble_resend == 0) {
  2035. joystick->rumble_resend = 1;
  2036. }
  2037. }
  2038. if (joystick->trigger_rumble_expiration && now >= joystick->trigger_rumble_expiration) {
  2039. SDL_RumbleJoystickTriggers(joystick, 0, 0, 0);
  2040. }
  2041. }
  2042. if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_UPDATE_COMPLETE)) {
  2043. for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
  2044. if (joystick->update_complete) {
  2045. SDL_Event event;
  2046. event.type = SDL_EVENT_JOYSTICK_UPDATE_COMPLETE;
  2047. event.common.timestamp = joystick->update_complete;
  2048. event.jdevice.which = joystick->instance_id;
  2049. SDL_PushEvent(&event);
  2050. joystick->update_complete = 0;
  2051. }
  2052. }
  2053. }
  2054. /* this needs to happen AFTER walking the joystick list above, so that any
  2055. dangling hardware data from removed devices can be free'd
  2056. */
  2057. for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
  2058. SDL_joystick_drivers[i]->Detect();
  2059. }
  2060. SDL_UnlockJoysticks();
  2061. }
  2062. static const Uint32 SDL_joystick_event_list[] = {
  2063. SDL_EVENT_JOYSTICK_AXIS_MOTION,
  2064. SDL_EVENT_JOYSTICK_BALL_MOTION,
  2065. SDL_EVENT_JOYSTICK_HAT_MOTION,
  2066. SDL_EVENT_JOYSTICK_BUTTON_DOWN,
  2067. SDL_EVENT_JOYSTICK_BUTTON_UP,
  2068. SDL_EVENT_JOYSTICK_ADDED,
  2069. SDL_EVENT_JOYSTICK_REMOVED,
  2070. SDL_EVENT_JOYSTICK_BATTERY_UPDATED
  2071. };
  2072. void SDL_SetJoystickEventsEnabled(bool enabled)
  2073. {
  2074. unsigned int i;
  2075. for (i = 0; i < SDL_arraysize(SDL_joystick_event_list); ++i) {
  2076. SDL_SetEventEnabled(SDL_joystick_event_list[i], enabled);
  2077. }
  2078. }
  2079. bool SDL_JoystickEventsEnabled(void)
  2080. {
  2081. bool enabled = false;
  2082. unsigned int i;
  2083. for (i = 0; i < SDL_arraysize(SDL_joystick_event_list); ++i) {
  2084. enabled = SDL_EventEnabled(SDL_joystick_event_list[i]);
  2085. if (enabled) {
  2086. break;
  2087. }
  2088. }
  2089. return enabled;
  2090. }
  2091. void SDL_GetJoystickGUIDInfo(SDL_GUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version, Uint16 *crc16)
  2092. {
  2093. Uint16 *guid16 = (Uint16 *)guid.data;
  2094. Uint16 bus = SDL_Swap16LE(guid16[0]);
  2095. if ((bus < ' ' || bus == SDL_HARDWARE_BUS_VIRTUAL) && guid16[3] == 0x0000 && guid16[5] == 0x0000) {
  2096. /* This GUID fits the standard form:
  2097. * 16-bit bus
  2098. * 16-bit CRC16 of the joystick name (can be zero)
  2099. * 16-bit vendor ID
  2100. * 16-bit zero
  2101. * 16-bit product ID
  2102. * 16-bit zero
  2103. * 16-bit version
  2104. * 8-bit driver identifier ('h' for HIDAPI, 'x' for XInput, etc.)
  2105. * 8-bit driver-dependent type info
  2106. */
  2107. if (vendor) {
  2108. *vendor = SDL_Swap16LE(guid16[2]);
  2109. }
  2110. if (product) {
  2111. *product = SDL_Swap16LE(guid16[4]);
  2112. }
  2113. if (version) {
  2114. *version = SDL_Swap16LE(guid16[6]);
  2115. }
  2116. if (crc16) {
  2117. *crc16 = SDL_Swap16LE(guid16[1]);
  2118. }
  2119. } else if (bus < ' ' || bus == SDL_HARDWARE_BUS_VIRTUAL) {
  2120. /* This GUID fits the unknown VID/PID form:
  2121. * 16-bit bus
  2122. * 16-bit CRC16 of the joystick name (can be zero)
  2123. * 11 characters of the joystick name, null terminated
  2124. */
  2125. if (vendor) {
  2126. *vendor = 0;
  2127. }
  2128. if (product) {
  2129. *product = 0;
  2130. }
  2131. if (version) {
  2132. *version = 0;
  2133. }
  2134. if (crc16) {
  2135. *crc16 = SDL_Swap16LE(guid16[1]);
  2136. }
  2137. } else {
  2138. if (vendor) {
  2139. *vendor = 0;
  2140. }
  2141. if (product) {
  2142. *product = 0;
  2143. }
  2144. if (version) {
  2145. *version = 0;
  2146. }
  2147. if (crc16) {
  2148. *crc16 = 0;
  2149. }
  2150. }
  2151. }
  2152. static int PrefixMatch(const char *a, const char *b)
  2153. {
  2154. int matchlen = 0;
  2155. while (*a && *b) {
  2156. if (SDL_tolower((unsigned char)*a++) == SDL_tolower((unsigned char)*b++)) {
  2157. ++matchlen;
  2158. } else {
  2159. break;
  2160. }
  2161. }
  2162. return matchlen;
  2163. }
  2164. char *SDL_CreateJoystickName(Uint16 vendor, Uint16 product, const char *vendor_name, const char *product_name)
  2165. {
  2166. static struct
  2167. {
  2168. const char *prefix;
  2169. const char *replacement;
  2170. } replacements[] = {
  2171. { "ASTRO Gaming", "ASTRO" },
  2172. { "Bensussen Deutsch & Associates,Inc.(BDA)", "BDA" },
  2173. { "Guangzhou Chicken Run Network Technology Co., Ltd.", "GameSir" },
  2174. { "HORI CO.,LTD", "HORI" },
  2175. { "HORI CO.,LTD.", "HORI" },
  2176. { "Mad Catz Inc.", "Mad Catz" },
  2177. { "Nintendo Co., Ltd.", "Nintendo" },
  2178. { "NVIDIA Corporation ", "" },
  2179. { "Performance Designed Products", "PDP" },
  2180. { "QANBA USA, LLC", "Qanba" },
  2181. { "QANBA USA,LLC", "Qanba" },
  2182. { "Unknown ", "" },
  2183. };
  2184. const char *custom_name;
  2185. char *name;
  2186. size_t i, len;
  2187. custom_name = GuessControllerName(vendor, product);
  2188. if (custom_name) {
  2189. return SDL_strdup(custom_name);
  2190. }
  2191. if (!vendor_name) {
  2192. vendor_name = "";
  2193. }
  2194. if (!product_name) {
  2195. product_name = "";
  2196. }
  2197. while (*vendor_name == ' ') {
  2198. ++vendor_name;
  2199. }
  2200. while (*product_name == ' ') {
  2201. ++product_name;
  2202. }
  2203. if (*vendor_name && *product_name) {
  2204. len = (SDL_strlen(vendor_name) + 1 + SDL_strlen(product_name) + 1);
  2205. name = (char *)SDL_malloc(len);
  2206. if (name) {
  2207. (void)SDL_snprintf(name, len, "%s %s", vendor_name, product_name);
  2208. }
  2209. } else if (*product_name) {
  2210. name = SDL_strdup(product_name);
  2211. } else if (vendor || product) {
  2212. // Couldn't find a controller name, try to give it one based on device type
  2213. switch (SDL_GetGamepadTypeFromVIDPID(vendor, product, NULL, true)) {
  2214. case SDL_GAMEPAD_TYPE_XBOX360:
  2215. name = SDL_strdup("Xbox 360 Controller");
  2216. break;
  2217. case SDL_GAMEPAD_TYPE_XBOXONE:
  2218. name = SDL_strdup("Xbox One Controller");
  2219. break;
  2220. case SDL_GAMEPAD_TYPE_PS3:
  2221. name = SDL_strdup("PS3 Controller");
  2222. break;
  2223. case SDL_GAMEPAD_TYPE_PS4:
  2224. name = SDL_strdup("PS4 Controller");
  2225. break;
  2226. case SDL_GAMEPAD_TYPE_PS5:
  2227. name = SDL_strdup("DualSense Wireless Controller");
  2228. break;
  2229. case SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO:
  2230. name = SDL_strdup("Nintendo Switch Pro Controller");
  2231. break;
  2232. default:
  2233. len = (6 + 1 + 6 + 1);
  2234. name = (char *)SDL_malloc(len);
  2235. if (name) {
  2236. (void)SDL_snprintf(name, len, "0x%.4x/0x%.4x", vendor, product);
  2237. }
  2238. break;
  2239. }
  2240. } else {
  2241. name = SDL_strdup("Controller");
  2242. }
  2243. if (!name) {
  2244. return NULL;
  2245. }
  2246. // Trim trailing whitespace
  2247. for (len = SDL_strlen(name); (len > 0 && name[len - 1] == ' '); --len) {
  2248. // continue
  2249. }
  2250. name[len] = '\0';
  2251. // Compress duplicate spaces
  2252. for (i = 0; i < (len - 1);) {
  2253. if (name[i] == ' ' && name[i + 1] == ' ') {
  2254. SDL_memmove(&name[i], &name[i + 1], (len - i));
  2255. --len;
  2256. } else {
  2257. ++i;
  2258. }
  2259. }
  2260. // Perform any manufacturer replacements
  2261. for (i = 0; i < SDL_arraysize(replacements); ++i) {
  2262. size_t prefixlen = SDL_strlen(replacements[i].prefix);
  2263. if (SDL_strncasecmp(name, replacements[i].prefix, prefixlen) == 0) {
  2264. size_t replacementlen = SDL_strlen(replacements[i].replacement);
  2265. if (replacementlen <= prefixlen) {
  2266. SDL_memcpy(name, replacements[i].replacement, replacementlen);
  2267. SDL_memmove(name + replacementlen, name + prefixlen, (len - prefixlen) + 1);
  2268. len -= (prefixlen - replacementlen);
  2269. } else {
  2270. // FIXME: Need to handle the expand case by reallocating the string
  2271. }
  2272. break;
  2273. }
  2274. }
  2275. /* Remove duplicate manufacturer or product in the name
  2276. * e.g. Razer Razer Raiju Tournament Edition Wired
  2277. */
  2278. for (i = 1; i < (len - 1); ++i) {
  2279. int matchlen = PrefixMatch(name, &name[i]);
  2280. while (matchlen > 0) {
  2281. if (name[matchlen] == ' ' || name[matchlen] == '-') {
  2282. SDL_memmove(name, name + matchlen + 1, len - matchlen);
  2283. break;
  2284. }
  2285. --matchlen;
  2286. }
  2287. if (matchlen > 0) {
  2288. // We matched the manufacturer's name and removed it
  2289. break;
  2290. }
  2291. }
  2292. return name;
  2293. }
  2294. SDL_GUID SDL_CreateJoystickGUID(Uint16 bus, Uint16 vendor, Uint16 product, Uint16 version, const char *vendor_name, const char *product_name, Uint8 driver_signature, Uint8 driver_data)
  2295. {
  2296. SDL_GUID guid;
  2297. Uint16 *guid16 = (Uint16 *)guid.data;
  2298. Uint16 crc = 0;
  2299. SDL_zero(guid);
  2300. if (vendor_name && *vendor_name && product_name && *product_name) {
  2301. crc = SDL_crc16(crc, vendor_name, SDL_strlen(vendor_name));
  2302. crc = SDL_crc16(crc, " ", 1);
  2303. crc = SDL_crc16(crc, product_name, SDL_strlen(product_name));
  2304. } else if (product_name) {
  2305. crc = SDL_crc16(crc, product_name, SDL_strlen(product_name));
  2306. }
  2307. // We only need 16 bits for each of these; space them out to fill 128.
  2308. // Byteswap so devices get same GUID on little/big endian platforms.
  2309. *guid16++ = SDL_Swap16LE(bus);
  2310. *guid16++ = SDL_Swap16LE(crc);
  2311. if (vendor) {
  2312. *guid16++ = SDL_Swap16LE(vendor);
  2313. *guid16++ = 0;
  2314. *guid16++ = SDL_Swap16LE(product);
  2315. *guid16++ = 0;
  2316. *guid16++ = SDL_Swap16LE(version);
  2317. guid.data[14] = driver_signature;
  2318. guid.data[15] = driver_data;
  2319. } else {
  2320. size_t available_space = sizeof(guid.data) - 4;
  2321. if (driver_signature) {
  2322. available_space -= 2;
  2323. guid.data[14] = driver_signature;
  2324. guid.data[15] = driver_data;
  2325. }
  2326. if (product_name) {
  2327. SDL_strlcpy((char *)guid16, product_name, available_space);
  2328. }
  2329. }
  2330. return guid;
  2331. }
  2332. SDL_GUID SDL_CreateJoystickGUIDForName(const char *name)
  2333. {
  2334. return SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_UNKNOWN, 0, 0, 0, NULL, name, 0, 0);
  2335. }
  2336. void SDL_SetJoystickGUIDVendor(SDL_GUID *guid, Uint16 vendor)
  2337. {
  2338. Uint16 *guid16 = (Uint16 *)guid->data;
  2339. guid16[2] = SDL_Swap16LE(vendor);
  2340. }
  2341. void SDL_SetJoystickGUIDProduct(SDL_GUID *guid, Uint16 product)
  2342. {
  2343. Uint16 *guid16 = (Uint16 *)guid->data;
  2344. guid16[4] = SDL_Swap16LE(product);
  2345. }
  2346. void SDL_SetJoystickGUIDVersion(SDL_GUID *guid, Uint16 version)
  2347. {
  2348. Uint16 *guid16 = (Uint16 *)guid->data;
  2349. guid16[6] = SDL_Swap16LE(version);
  2350. }
  2351. void SDL_SetJoystickGUIDCRC(SDL_GUID *guid, Uint16 crc)
  2352. {
  2353. Uint16 *guid16 = (Uint16 *)guid->data;
  2354. guid16[1] = SDL_Swap16LE(crc);
  2355. }
  2356. SDL_GamepadType SDL_GetGamepadTypeFromVIDPID(Uint16 vendor, Uint16 product, const char *name, bool forUI)
  2357. {
  2358. SDL_GamepadType type = SDL_GAMEPAD_TYPE_STANDARD;
  2359. if (vendor == 0x0000 && product == 0x0000) {
  2360. // Some devices are only identifiable by their name
  2361. if (name &&
  2362. (SDL_strcmp(name, "Lic Pro Controller") == 0 ||
  2363. SDL_strcmp(name, "Nintendo Wireless Gamepad") == 0 ||
  2364. SDL_strcmp(name, "Wireless Gamepad") == 0)) {
  2365. // HORI or PowerA Switch Pro Controller clone
  2366. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
  2367. }
  2368. } else if (vendor == 0x0001 && product == 0x0001) {
  2369. type = SDL_GAMEPAD_TYPE_STANDARD;
  2370. } else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_LEFT) {
  2371. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;
  2372. } else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_RIGHT) {
  2373. if (name && SDL_strstr(name, "NES Controller") != NULL) {
  2374. // We don't have a type for the Nintendo Online NES Controller
  2375. type = SDL_GAMEPAD_TYPE_STANDARD;
  2376. } else {
  2377. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;
  2378. }
  2379. } else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
  2380. if (name && SDL_strstr(name, "(L)") != NULL) {
  2381. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_LEFT;
  2382. } else {
  2383. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT;
  2384. }
  2385. } else if (vendor == USB_VENDOR_NINTENDO && product == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR) {
  2386. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR;
  2387. } else if (forUI && SDL_IsJoystickGameCube(vendor, product)) {
  2388. // We don't have a type for the Nintendo GameCube controller
  2389. type = SDL_GAMEPAD_TYPE_STANDARD;
  2390. } else {
  2391. switch (GuessControllerType(vendor, product)) {
  2392. case k_eControllerType_XBox360Controller:
  2393. type = SDL_GAMEPAD_TYPE_XBOX360;
  2394. break;
  2395. case k_eControllerType_XBoxOneController:
  2396. type = SDL_GAMEPAD_TYPE_XBOXONE;
  2397. break;
  2398. case k_eControllerType_PS3Controller:
  2399. type = SDL_GAMEPAD_TYPE_PS3;
  2400. break;
  2401. case k_eControllerType_PS4Controller:
  2402. type = SDL_GAMEPAD_TYPE_PS4;
  2403. break;
  2404. case k_eControllerType_PS5Controller:
  2405. type = SDL_GAMEPAD_TYPE_PS5;
  2406. break;
  2407. case k_eControllerType_XInputPS4Controller:
  2408. if (forUI) {
  2409. type = SDL_GAMEPAD_TYPE_PS4;
  2410. } else {
  2411. type = SDL_GAMEPAD_TYPE_STANDARD;
  2412. }
  2413. break;
  2414. case k_eControllerType_SwitchProController:
  2415. case k_eControllerType_SwitchInputOnlyController:
  2416. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
  2417. break;
  2418. case k_eControllerType_XInputSwitchController:
  2419. if (forUI) {
  2420. type = SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_PRO;
  2421. } else {
  2422. type = SDL_GAMEPAD_TYPE_STANDARD;
  2423. }
  2424. break;
  2425. default:
  2426. break;
  2427. }
  2428. }
  2429. return type;
  2430. }
  2431. SDL_GamepadType SDL_GetGamepadTypeFromGUID(SDL_GUID guid, const char *name)
  2432. {
  2433. SDL_GamepadType type;
  2434. Uint16 vendor, product;
  2435. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  2436. type = SDL_GetGamepadTypeFromVIDPID(vendor, product, name, true);
  2437. if (type == SDL_GAMEPAD_TYPE_STANDARD) {
  2438. if (SDL_IsJoystickXInput(guid)) {
  2439. // This is probably an Xbox One controller
  2440. return SDL_GAMEPAD_TYPE_XBOXONE;
  2441. }
  2442. #ifdef SDL_JOYSTICK_HIDAPI
  2443. if (SDL_IsJoystickHIDAPI(guid)) {
  2444. return HIDAPI_GetGamepadTypeFromGUID(guid);
  2445. }
  2446. #endif // SDL_JOYSTICK_HIDAPI
  2447. }
  2448. return type;
  2449. }
  2450. bool SDL_JoystickGUIDUsesVersion(SDL_GUID guid)
  2451. {
  2452. Uint16 vendor, product;
  2453. if (SDL_IsJoystickMFI(guid)) {
  2454. // The version bits are used as button capability mask
  2455. return false;
  2456. }
  2457. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  2458. if (vendor && product) {
  2459. return true;
  2460. }
  2461. return false;
  2462. }
  2463. bool SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id)
  2464. {
  2465. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2466. return eType == k_eControllerType_XBoxOneController;
  2467. }
  2468. bool SDL_IsJoystickXboxOneElite(Uint16 vendor_id, Uint16 product_id)
  2469. {
  2470. if (vendor_id == USB_VENDOR_MICROSOFT) {
  2471. if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
  2472. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2 ||
  2473. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||
  2474. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLE) {
  2475. return true;
  2476. }
  2477. }
  2478. return false;
  2479. }
  2480. bool SDL_IsJoystickXboxSeriesX(Uint16 vendor_id, Uint16 product_id)
  2481. {
  2482. if (vendor_id == USB_VENDOR_MICROSOFT) {
  2483. if (product_id == USB_PRODUCT_XBOX_SERIES_X ||
  2484. product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {
  2485. return true;
  2486. }
  2487. }
  2488. if (vendor_id == USB_VENDOR_PDP) {
  2489. if (product_id == USB_PRODUCT_XBOX_SERIES_X_VICTRIX_GAMBIT ||
  2490. product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_BLUE ||
  2491. product_id == USB_PRODUCT_XBOX_SERIES_X_PDP_AFTERGLOW) {
  2492. return true;
  2493. }
  2494. }
  2495. if (vendor_id == USB_VENDOR_POWERA_ALT) {
  2496. if ((product_id >= 0x2001 && product_id <= 0x201a) ||
  2497. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO2 ||
  2498. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO4 ||
  2499. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO_WIRELESS_USB ||
  2500. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_FUSION_PRO_WIRELESS_DONGLE ||
  2501. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_MOGA_XP_ULTRA ||
  2502. product_id == USB_PRODUCT_XBOX_SERIES_X_POWERA_SPECTRA) {
  2503. return true;
  2504. }
  2505. }
  2506. if (vendor_id == USB_VENDOR_HORI) {
  2507. if (product_id == USB_PRODUCT_HORI_FIGHTING_COMMANDER_OCTA_SERIES_X ||
  2508. product_id == USB_PRODUCT_HORI_HORIPAD_PRO_SERIES_X) {
  2509. return true;
  2510. }
  2511. }
  2512. if (vendor_id == USB_VENDOR_HP) {
  2513. if (product_id == USB_PRODUCT_XBOX_SERIES_X_HP_HYPERX ||
  2514. product_id == USB_PRODUCT_XBOX_SERIES_X_HP_HYPERX_RGB) {
  2515. return true;
  2516. }
  2517. }
  2518. if (vendor_id == USB_VENDOR_RAZER) {
  2519. if (product_id == USB_PRODUCT_RAZER_WOLVERINE_V2 ||
  2520. product_id == USB_PRODUCT_RAZER_WOLVERINE_V2_CHROMA) {
  2521. return true;
  2522. }
  2523. }
  2524. if (vendor_id == USB_VENDOR_THRUSTMASTER) {
  2525. if (product_id == USB_PRODUCT_THRUSTMASTER_ESWAPX_PRO) {
  2526. return true;
  2527. }
  2528. }
  2529. if (vendor_id == USB_VENDOR_TURTLE_BEACH) {
  2530. if (product_id == USB_PRODUCT_TURTLE_BEACH_SERIES_X_REACT_R ||
  2531. product_id == USB_PRODUCT_TURTLE_BEACH_SERIES_X_RECON) {
  2532. return true;
  2533. }
  2534. }
  2535. if (vendor_id == USB_VENDOR_8BITDO) {
  2536. if (product_id == USB_PRODUCT_8BITDO_XBOX_CONTROLLER1 ||
  2537. product_id == USB_PRODUCT_8BITDO_XBOX_CONTROLLER2) {
  2538. return true;
  2539. }
  2540. }
  2541. if (vendor_id == USB_VENDOR_GAMESIR) {
  2542. if (product_id == USB_PRODUCT_GAMESIR_G7) {
  2543. return true;
  2544. }
  2545. }
  2546. if (vendor_id == USB_VENDOR_ASUS) {
  2547. if (product_id == USB_PRODUCT_ROG_RAIKIRI) {
  2548. return true;
  2549. }
  2550. }
  2551. return false;
  2552. }
  2553. bool SDL_IsJoystickBluetoothXboxOne(Uint16 vendor_id, Uint16 product_id)
  2554. {
  2555. if (vendor_id == USB_VENDOR_MICROSOFT) {
  2556. if (product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLUETOOTH ||
  2557. product_id == USB_PRODUCT_XBOX_ONE_ADAPTIVE_BLE ||
  2558. product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  2559. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  2560. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLE ||
  2561. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||
  2562. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLE ||
  2563. product_id == USB_PRODUCT_XBOX_SERIES_X_BLE) {
  2564. return true;
  2565. }
  2566. }
  2567. return false;
  2568. }
  2569. bool SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id)
  2570. {
  2571. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2572. return eType == k_eControllerType_PS4Controller;
  2573. }
  2574. bool SDL_IsJoystickPS5(Uint16 vendor_id, Uint16 product_id)
  2575. {
  2576. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2577. return eType == k_eControllerType_PS5Controller;
  2578. }
  2579. bool SDL_IsJoystickDualSenseEdge(Uint16 vendor_id, Uint16 product_id)
  2580. {
  2581. if (vendor_id == USB_VENDOR_SONY) {
  2582. if (product_id == USB_PRODUCT_SONY_DS5_EDGE) {
  2583. return true;
  2584. }
  2585. }
  2586. return false;
  2587. }
  2588. bool SDL_IsJoystickNintendoSwitchPro(Uint16 vendor_id, Uint16 product_id)
  2589. {
  2590. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2591. return eType == k_eControllerType_SwitchProController || eType == k_eControllerType_SwitchInputOnlyController;
  2592. }
  2593. bool SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id)
  2594. {
  2595. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2596. return eType == k_eControllerType_SwitchInputOnlyController;
  2597. }
  2598. bool SDL_IsJoystickNintendoSwitchJoyCon(Uint16 vendor_id, Uint16 product_id)
  2599. {
  2600. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2601. return eType == k_eControllerType_SwitchJoyConLeft || eType == k_eControllerType_SwitchJoyConRight;
  2602. }
  2603. bool SDL_IsJoystickNintendoSwitchJoyConLeft(Uint16 vendor_id, Uint16 product_id)
  2604. {
  2605. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2606. return eType == k_eControllerType_SwitchJoyConLeft;
  2607. }
  2608. bool SDL_IsJoystickNintendoSwitchJoyConRight(Uint16 vendor_id, Uint16 product_id)
  2609. {
  2610. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2611. return eType == k_eControllerType_SwitchJoyConRight;
  2612. }
  2613. bool SDL_IsJoystickNintendoSwitchJoyConGrip(Uint16 vendor_id, Uint16 product_id)
  2614. {
  2615. return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP;
  2616. }
  2617. bool SDL_IsJoystickNintendoSwitchJoyConPair(Uint16 vendor_id, Uint16 product_id)
  2618. {
  2619. return vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_SWITCH_JOYCON_PAIR;
  2620. }
  2621. bool SDL_IsJoystickGameCube(Uint16 vendor_id, Uint16 product_id)
  2622. {
  2623. return SDL_VIDPIDInList(vendor_id, product_id, &gamecube_devices);
  2624. }
  2625. bool SDL_IsJoystickAmazonLunaController(Uint16 vendor_id, Uint16 product_id)
  2626. {
  2627. return ((vendor_id == USB_VENDOR_AMAZON && product_id == USB_PRODUCT_AMAZON_LUNA_CONTROLLER) ||
  2628. (vendor_id == BLUETOOTH_VENDOR_AMAZON && product_id == BLUETOOTH_PRODUCT_LUNA_CONTROLLER));
  2629. }
  2630. bool SDL_IsJoystickGoogleStadiaController(Uint16 vendor_id, Uint16 product_id)
  2631. {
  2632. return vendor_id == USB_VENDOR_GOOGLE && product_id == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER;
  2633. }
  2634. bool SDL_IsJoystickNVIDIASHIELDController(Uint16 vendor_id, Uint16 product_id)
  2635. {
  2636. return (vendor_id == USB_VENDOR_NVIDIA &&
  2637. (product_id == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V103 ||
  2638. product_id == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104));
  2639. }
  2640. bool SDL_IsJoystickSteamVirtualGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version)
  2641. {
  2642. #ifdef SDL_PLATFORM_MACOS
  2643. return (vendor_id == USB_VENDOR_MICROSOFT && product_id == USB_PRODUCT_XBOX360_WIRED_CONTROLLER && version == 0);
  2644. #else
  2645. return (vendor_id == USB_VENDOR_VALVE && product_id == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD);
  2646. #endif
  2647. }
  2648. bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)
  2649. {
  2650. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2651. return eType == k_eControllerType_SteamController || eType == k_eControllerType_SteamControllerV2;
  2652. }
  2653. bool SDL_IsJoystickHoriSteamController(Uint16 vendor_id, Uint16 product_id)
  2654. {
  2655. return vendor_id == USB_VENDOR_HORI && (product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER || product_id == USB_PRODUCT_HORI_STEAM_CONTROLLER_BT);
  2656. }
  2657. bool SDL_IsJoystickSteamDeck(Uint16 vendor_id, Uint16 product_id)
  2658. {
  2659. EControllerType eType = GuessControllerType(vendor_id, product_id);
  2660. return eType == k_eControllerType_SteamControllerNeptune;
  2661. }
  2662. bool SDL_IsJoystickXInput(SDL_GUID guid)
  2663. {
  2664. return (guid.data[14] == 'x') ? true : false;
  2665. }
  2666. bool SDL_IsJoystickWGI(SDL_GUID guid)
  2667. {
  2668. return (guid.data[14] == 'w') ? true : false;
  2669. }
  2670. bool SDL_IsJoystickHIDAPI(SDL_GUID guid)
  2671. {
  2672. return (guid.data[14] == 'h') ? true : false;
  2673. }
  2674. bool SDL_IsJoystickMFI(SDL_GUID guid)
  2675. {
  2676. return (guid.data[14] == 'm') ? true : false;
  2677. }
  2678. bool SDL_IsJoystickRAWINPUT(SDL_GUID guid)
  2679. {
  2680. return (guid.data[14] == 'r') ? true : false;
  2681. }
  2682. bool SDL_IsJoystickVIRTUAL(SDL_GUID guid)
  2683. {
  2684. return (guid.data[14] == 'v') ? true : false;
  2685. }
  2686. static bool SDL_IsJoystickWheel(Uint16 vendor_id, Uint16 product_id)
  2687. {
  2688. return SDL_VIDPIDInList(vendor_id, product_id, &wheel_devices);
  2689. }
  2690. static bool SDL_IsJoystickArcadeStick(Uint16 vendor_id, Uint16 product_id)
  2691. {
  2692. return SDL_VIDPIDInList(vendor_id, product_id, &arcadestick_devices);
  2693. }
  2694. static bool SDL_IsJoystickFlightStick(Uint16 vendor_id, Uint16 product_id)
  2695. {
  2696. return SDL_VIDPIDInList(vendor_id, product_id, &flightstick_devices);
  2697. }
  2698. static bool SDL_IsJoystickThrottle(Uint16 vendor_id, Uint16 product_id)
  2699. {
  2700. return SDL_VIDPIDInList(vendor_id, product_id, &throttle_devices);
  2701. }
  2702. static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_GUID guid)
  2703. {
  2704. Uint16 vendor;
  2705. Uint16 product;
  2706. SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL, NULL);
  2707. if (SDL_IsJoystickWheel(vendor, product)) {
  2708. return SDL_JOYSTICK_TYPE_WHEEL;
  2709. }
  2710. if (SDL_IsJoystickArcadeStick(vendor, product)) {
  2711. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  2712. }
  2713. if (SDL_IsJoystickFlightStick(vendor, product)) {
  2714. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  2715. }
  2716. if (SDL_IsJoystickThrottle(vendor, product)) {
  2717. return SDL_JOYSTICK_TYPE_THROTTLE;
  2718. }
  2719. if (SDL_IsJoystickXInput(guid)) {
  2720. // XInput GUID, get the type based on the XInput device subtype
  2721. switch (guid.data[15]) {
  2722. case 0x01: // XINPUT_DEVSUBTYPE_GAMEPAD
  2723. return SDL_JOYSTICK_TYPE_GAMEPAD;
  2724. case 0x02: // XINPUT_DEVSUBTYPE_WHEEL
  2725. return SDL_JOYSTICK_TYPE_WHEEL;
  2726. case 0x03: // XINPUT_DEVSUBTYPE_ARCADE_STICK
  2727. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  2728. case 0x04: // XINPUT_DEVSUBTYPE_FLIGHT_STICK
  2729. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  2730. case 0x05: // XINPUT_DEVSUBTYPE_DANCE_PAD
  2731. return SDL_JOYSTICK_TYPE_DANCE_PAD;
  2732. case 0x06: // XINPUT_DEVSUBTYPE_GUITAR
  2733. case 0x07: // XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE
  2734. case 0x0B: // XINPUT_DEVSUBTYPE_GUITAR_BASS
  2735. return SDL_JOYSTICK_TYPE_GUITAR;
  2736. case 0x08: // XINPUT_DEVSUBTYPE_DRUM_KIT
  2737. return SDL_JOYSTICK_TYPE_DRUM_KIT;
  2738. case 0x13: // XINPUT_DEVSUBTYPE_ARCADE_PAD
  2739. return SDL_JOYSTICK_TYPE_ARCADE_PAD;
  2740. default:
  2741. return SDL_JOYSTICK_TYPE_UNKNOWN;
  2742. }
  2743. }
  2744. if (SDL_IsJoystickWGI(guid)) {
  2745. return (SDL_JoystickType)guid.data[15];
  2746. }
  2747. if (SDL_IsJoystickVIRTUAL(guid)) {
  2748. return (SDL_JoystickType)guid.data[15];
  2749. }
  2750. #ifdef SDL_JOYSTICK_HIDAPI
  2751. if (SDL_IsJoystickHIDAPI(guid)) {
  2752. return HIDAPI_GetJoystickTypeFromGUID(guid);
  2753. }
  2754. #endif // SDL_JOYSTICK_HIDAPI
  2755. if (GuessControllerType(vendor, product) != k_eControllerType_UnknownNonSteamController) {
  2756. return SDL_JOYSTICK_TYPE_GAMEPAD;
  2757. }
  2758. return SDL_JOYSTICK_TYPE_UNKNOWN;
  2759. }
  2760. bool SDL_ShouldIgnoreJoystick(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  2761. {
  2762. // Check the joystick blacklist
  2763. if (SDL_VIDPIDInList(vendor_id, product_id, &blacklist_devices)) {
  2764. return true;
  2765. }
  2766. if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_ROG_CHAKRAM, false)) {
  2767. if (SDL_VIDPIDInList(vendor_id, product_id, &rog_gamepad_mice)) {
  2768. return true;
  2769. }
  2770. }
  2771. if (SDL_ShouldIgnoreGamepad(vendor_id, product_id, version, name)) {
  2772. return true;
  2773. }
  2774. return false;
  2775. }
  2776. // return the guid for this index
  2777. SDL_GUID SDL_GetJoystickGUIDForID(SDL_JoystickID instance_id)
  2778. {
  2779. SDL_JoystickDriver *driver;
  2780. int device_index;
  2781. SDL_GUID guid;
  2782. SDL_LockJoysticks();
  2783. if (SDL_GetDriverAndJoystickIndex(instance_id, &driver, &device_index)) {
  2784. guid = driver->GetDeviceGUID(device_index);
  2785. } else {
  2786. SDL_zero(guid);
  2787. }
  2788. SDL_UnlockJoysticks();
  2789. return guid;
  2790. }
  2791. Uint16 SDL_GetJoystickVendorForID(SDL_JoystickID instance_id)
  2792. {
  2793. Uint16 vendor;
  2794. const SDL_SteamVirtualGamepadInfo *info;
  2795. SDL_LockJoysticks();
  2796. info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
  2797. if (info) {
  2798. vendor = info->vendor_id;
  2799. } else {
  2800. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2801. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL, NULL);
  2802. }
  2803. SDL_UnlockJoysticks();
  2804. return vendor;
  2805. }
  2806. Uint16 SDL_GetJoystickProductForID(SDL_JoystickID instance_id)
  2807. {
  2808. Uint16 product;
  2809. const SDL_SteamVirtualGamepadInfo *info;
  2810. SDL_LockJoysticks();
  2811. info = SDL_GetJoystickVirtualGamepadInfoForID(instance_id);
  2812. if (info) {
  2813. product = info->product_id;
  2814. } else {
  2815. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2816. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL, NULL);
  2817. }
  2818. SDL_UnlockJoysticks();
  2819. return product;
  2820. }
  2821. Uint16 SDL_GetJoystickProductVersionForID(SDL_JoystickID instance_id)
  2822. {
  2823. Uint16 version;
  2824. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2825. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version, NULL);
  2826. return version;
  2827. }
  2828. SDL_JoystickType SDL_GetJoystickTypeForID(SDL_JoystickID instance_id)
  2829. {
  2830. SDL_JoystickType type;
  2831. SDL_GUID guid = SDL_GetJoystickGUIDForID(instance_id);
  2832. type = SDL_GetJoystickGUIDType(guid);
  2833. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  2834. if (SDL_IsGamepad(instance_id)) {
  2835. type = SDL_JOYSTICK_TYPE_GAMEPAD;
  2836. }
  2837. }
  2838. return type;
  2839. }
  2840. SDL_GUID SDL_GetJoystickGUID(SDL_Joystick *joystick)
  2841. {
  2842. SDL_GUID result;
  2843. SDL_LockJoysticks();
  2844. {
  2845. static SDL_GUID emptyGUID;
  2846. CHECK_JOYSTICK_MAGIC(joystick, emptyGUID);
  2847. result = joystick->guid;
  2848. }
  2849. SDL_UnlockJoysticks();
  2850. return result;
  2851. }
  2852. Uint16 SDL_GetJoystickVendor(SDL_Joystick *joystick)
  2853. {
  2854. Uint16 vendor;
  2855. const SDL_SteamVirtualGamepadInfo *info;
  2856. SDL_LockJoysticks();
  2857. {
  2858. CHECK_JOYSTICK_MAGIC(joystick, 0);
  2859. info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);
  2860. if (info) {
  2861. vendor = info->vendor_id;
  2862. } else {
  2863. SDL_GUID guid = SDL_GetJoystickGUID(joystick);
  2864. SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL, NULL);
  2865. }
  2866. }
  2867. SDL_UnlockJoysticks();
  2868. return vendor;
  2869. }
  2870. Uint16 SDL_GetJoystickProduct(SDL_Joystick *joystick)
  2871. {
  2872. Uint16 product;
  2873. const SDL_SteamVirtualGamepadInfo *info;
  2874. SDL_LockJoysticks();
  2875. {
  2876. CHECK_JOYSTICK_MAGIC(joystick, 0);
  2877. info = SDL_GetJoystickVirtualGamepadInfoForID(joystick->instance_id);
  2878. if (info) {
  2879. product = info->product_id;
  2880. } else {
  2881. SDL_GUID guid = SDL_GetJoystickGUID(joystick);
  2882. SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL, NULL);
  2883. }
  2884. }
  2885. SDL_UnlockJoysticks();
  2886. return product;
  2887. }
  2888. Uint16 SDL_GetJoystickProductVersion(SDL_Joystick *joystick)
  2889. {
  2890. Uint16 version;
  2891. SDL_GUID guid = SDL_GetJoystickGUID(joystick);
  2892. SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version, NULL);
  2893. return version;
  2894. }
  2895. Uint16 SDL_GetJoystickFirmwareVersion(SDL_Joystick *joystick)
  2896. {
  2897. Uint16 result;
  2898. SDL_LockJoysticks();
  2899. {
  2900. CHECK_JOYSTICK_MAGIC(joystick, 0);
  2901. result = joystick->firmware_version;
  2902. }
  2903. SDL_UnlockJoysticks();
  2904. return result;
  2905. }
  2906. const char *SDL_GetJoystickSerial(SDL_Joystick *joystick)
  2907. {
  2908. const char *result;
  2909. SDL_LockJoysticks();
  2910. {
  2911. CHECK_JOYSTICK_MAGIC(joystick, NULL);
  2912. result = SDL_GetPersistentString(joystick->serial);
  2913. }
  2914. SDL_UnlockJoysticks();
  2915. return result;
  2916. }
  2917. SDL_JoystickType SDL_GetJoystickType(SDL_Joystick *joystick)
  2918. {
  2919. SDL_JoystickType type;
  2920. SDL_GUID guid = SDL_GetJoystickGUID(joystick);
  2921. type = SDL_GetJoystickGUIDType(guid);
  2922. if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
  2923. SDL_LockJoysticks();
  2924. {
  2925. CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_TYPE_UNKNOWN);
  2926. if (SDL_IsGamepad(joystick->instance_id)) {
  2927. type = SDL_JOYSTICK_TYPE_GAMEPAD;
  2928. }
  2929. }
  2930. SDL_UnlockJoysticks();
  2931. }
  2932. return type;
  2933. }
  2934. void SDL_SendJoystickPowerInfo(SDL_Joystick *joystick, SDL_PowerState state, int percent)
  2935. {
  2936. SDL_AssertJoysticksLocked();
  2937. if (state != joystick->battery_state || percent != joystick->battery_percent) {
  2938. joystick->battery_state = state;
  2939. joystick->battery_percent = percent;
  2940. if (SDL_EventEnabled(SDL_EVENT_JOYSTICK_BATTERY_UPDATED)) {
  2941. SDL_Event event;
  2942. event.type = SDL_EVENT_JOYSTICK_BATTERY_UPDATED;
  2943. event.common.timestamp = 0;
  2944. event.jbattery.which = joystick->instance_id;
  2945. event.jbattery.state = state;
  2946. event.jbattery.percent = percent;
  2947. SDL_PushEvent(&event);
  2948. }
  2949. }
  2950. }
  2951. SDL_JoystickConnectionState SDL_GetJoystickConnectionState(SDL_Joystick *joystick)
  2952. {
  2953. SDL_JoystickConnectionState result;
  2954. SDL_LockJoysticks();
  2955. {
  2956. CHECK_JOYSTICK_MAGIC(joystick, SDL_JOYSTICK_CONNECTION_INVALID);
  2957. result = joystick->connection_state;
  2958. }
  2959. SDL_UnlockJoysticks();
  2960. return result;
  2961. }
  2962. SDL_PowerState SDL_GetJoystickPowerInfo(SDL_Joystick *joystick, int *percent)
  2963. {
  2964. SDL_PowerState result;
  2965. if (percent) {
  2966. *percent = -1;
  2967. }
  2968. SDL_LockJoysticks();
  2969. {
  2970. CHECK_JOYSTICK_MAGIC(joystick, SDL_POWERSTATE_ERROR);
  2971. result = joystick->battery_state;
  2972. if (percent) {
  2973. *percent = joystick->battery_percent;
  2974. }
  2975. }
  2976. SDL_UnlockJoysticks();
  2977. return result;
  2978. }
  2979. void SDL_SendJoystickTouchpad(Uint64 timestamp, SDL_Joystick *joystick, int touchpad, int finger, bool down, float x, float y, float pressure)
  2980. {
  2981. SDL_JoystickTouchpadInfo *touchpad_info;
  2982. SDL_JoystickTouchpadFingerInfo *finger_info;
  2983. Uint32 event_type;
  2984. SDL_AssertJoysticksLocked();
  2985. if (touchpad < 0 || touchpad >= joystick->ntouchpads) {
  2986. return;
  2987. }
  2988. touchpad_info = &joystick->touchpads[touchpad];
  2989. if (finger < 0 || finger >= touchpad_info->nfingers) {
  2990. return;
  2991. }
  2992. finger_info = &touchpad_info->fingers[finger];
  2993. if (!down) {
  2994. if (x == 0.0f && y == 0.0f) {
  2995. x = finger_info->x;
  2996. y = finger_info->y;
  2997. }
  2998. pressure = 0.0f;
  2999. }
  3000. if (x < 0.0f) {
  3001. x = 0.0f;
  3002. } else if (x > 1.0f) {
  3003. x = 1.0f;
  3004. }
  3005. if (y < 0.0f) {
  3006. y = 0.0f;
  3007. } else if (y > 1.0f) {
  3008. y = 1.0f;
  3009. }
  3010. if (pressure < 0.0f) {
  3011. pressure = 0.0f;
  3012. } else if (pressure > 1.0f) {
  3013. pressure = 1.0f;
  3014. }
  3015. if (down == finger_info->down) {
  3016. if (!down ||
  3017. (x == finger_info->x && y == finger_info->y && pressure == finger_info->pressure)) {
  3018. return;
  3019. }
  3020. }
  3021. if (down == finger_info->down) {
  3022. event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION;
  3023. } else if (down) {
  3024. event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN;
  3025. } else {
  3026. event_type = SDL_EVENT_GAMEPAD_TOUCHPAD_UP;
  3027. }
  3028. // We ignore events if we don't have keyboard focus, except for touch release
  3029. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  3030. if (event_type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP) {
  3031. return;
  3032. }
  3033. }
  3034. // Update internal joystick state
  3035. SDL_assert(timestamp != 0);
  3036. finger_info->down = down;
  3037. finger_info->x = x;
  3038. finger_info->y = y;
  3039. finger_info->pressure = pressure;
  3040. joystick->update_complete = timestamp;
  3041. // Post the event, if desired
  3042. if (SDL_EventEnabled(event_type)) {
  3043. SDL_Event event;
  3044. event.type = event_type;
  3045. event.common.timestamp = timestamp;
  3046. event.gtouchpad.which = joystick->instance_id;
  3047. event.gtouchpad.touchpad = touchpad;
  3048. event.gtouchpad.finger = finger;
  3049. event.gtouchpad.x = x;
  3050. event.gtouchpad.y = y;
  3051. event.gtouchpad.pressure = pressure;
  3052. SDL_PushEvent(&event);
  3053. }
  3054. }
  3055. void SDL_SendJoystickSensor(Uint64 timestamp, SDL_Joystick *joystick, SDL_SensorType type, Uint64 sensor_timestamp, const float *data, int num_values)
  3056. {
  3057. SDL_AssertJoysticksLocked();
  3058. // We ignore events if we don't have keyboard focus
  3059. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  3060. return;
  3061. }
  3062. for (int i = 0; i < joystick->nsensors; ++i) {
  3063. SDL_JoystickSensorInfo *sensor = &joystick->sensors[i];
  3064. if (sensor->type == type) {
  3065. if (sensor->enabled) {
  3066. num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
  3067. // Update internal sensor state
  3068. SDL_memcpy(sensor->data, data, num_values * sizeof(*data));
  3069. joystick->update_complete = timestamp;
  3070. // Post the event, if desired
  3071. if (SDL_EventEnabled(SDL_EVENT_GAMEPAD_SENSOR_UPDATE)) {
  3072. SDL_Event event;
  3073. event.type = SDL_EVENT_GAMEPAD_SENSOR_UPDATE;
  3074. event.common.timestamp = timestamp;
  3075. event.gsensor.which = joystick->instance_id;
  3076. event.gsensor.sensor = type;
  3077. num_values = SDL_min(num_values,
  3078. SDL_arraysize(event.gsensor.data));
  3079. SDL_memset(event.gsensor.data, 0,
  3080. sizeof(event.gsensor.data));
  3081. SDL_memcpy(event.gsensor.data, data,
  3082. num_values * sizeof(*data));
  3083. event.gsensor.sensor_timestamp = sensor_timestamp;
  3084. SDL_PushEvent(&event);
  3085. }
  3086. }
  3087. break;
  3088. }
  3089. }
  3090. }
  3091. static void SDL_LoadVIDPIDListFromHint(const char *hint, int *num_entries, int *max_entries, Uint32 **entries)
  3092. {
  3093. Uint32 entry;
  3094. char *spot;
  3095. char *file = NULL;
  3096. if (hint && *hint == '@') {
  3097. spot = file = (char *)SDL_LoadFile(hint + 1, NULL);
  3098. } else {
  3099. spot = (char *)hint;
  3100. }
  3101. if (!spot) {
  3102. return;
  3103. }
  3104. while ((spot = SDL_strstr(spot, "0x")) != NULL) {
  3105. entry = (Uint16)SDL_strtol(spot, &spot, 0);
  3106. entry <<= 16;
  3107. spot = SDL_strstr(spot, "0x");
  3108. if (!spot) {
  3109. break;
  3110. }
  3111. entry |= (Uint16)SDL_strtol(spot, &spot, 0);
  3112. if (*num_entries == *max_entries) {
  3113. int new_max_entries = *max_entries + 16;
  3114. Uint32 *new_entries = (Uint32 *)SDL_realloc(*entries, new_max_entries * sizeof(**entries));
  3115. if (!new_entries) {
  3116. // Out of memory, go with what we have already
  3117. break;
  3118. }
  3119. *entries = new_entries;
  3120. *max_entries = new_max_entries;
  3121. }
  3122. (*entries)[(*num_entries)++] = entry;
  3123. }
  3124. if (file) {
  3125. SDL_free(file);
  3126. }
  3127. }
  3128. void SDL_LoadVIDPIDListFromHints(SDL_vidpid_list *list, const char *included_list, const char *excluded_list)
  3129. {
  3130. // Empty the list
  3131. list->num_included_entries = 0;
  3132. list->num_excluded_entries = 0;
  3133. // Add the initial entries
  3134. if (list->num_initial_entries > 0) {
  3135. if (list->num_included_entries < list->num_initial_entries) {
  3136. Uint32 *entries = (Uint32 *)SDL_malloc(list->num_initial_entries * sizeof(*entries));
  3137. if (entries) {
  3138. SDL_memcpy(entries, list->initial_entries, list->num_initial_entries * sizeof(*entries));
  3139. list->included_entries = entries;
  3140. list->num_included_entries = list->num_initial_entries;
  3141. list->max_included_entries = list->num_initial_entries;
  3142. }
  3143. }
  3144. }
  3145. // Add the included entries from the hint
  3146. SDL_LoadVIDPIDListFromHint(included_list, &list->num_included_entries, &list->max_included_entries, &list->included_entries);
  3147. // Add the excluded entries from the hint
  3148. SDL_LoadVIDPIDListFromHint(excluded_list, &list->num_excluded_entries, &list->max_excluded_entries, &list->excluded_entries);
  3149. }
  3150. static void SDLCALL SDL_VIDPIDIncludedHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  3151. {
  3152. SDL_vidpid_list *list = (SDL_vidpid_list *)userdata;
  3153. const char *included_list = hint;
  3154. const char *excluded_list = NULL;
  3155. if (!list->initialized) {
  3156. return;
  3157. }
  3158. if (list->excluded_hint_name) {
  3159. excluded_list = SDL_GetHint(list->excluded_hint_name);
  3160. }
  3161. SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);
  3162. }
  3163. static void SDLCALL SDL_VIDPIDExcludedHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  3164. {
  3165. SDL_vidpid_list *list = (SDL_vidpid_list *)userdata;
  3166. const char *included_list = NULL;
  3167. const char *excluded_list = hint;
  3168. if (!list->initialized) {
  3169. return;
  3170. }
  3171. if (list->included_hint_name) {
  3172. included_list = SDL_GetHint(list->included_hint_name);
  3173. }
  3174. SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);
  3175. }
  3176. void SDL_LoadVIDPIDList(SDL_vidpid_list *list)
  3177. {
  3178. const char *included_list = NULL;
  3179. const char *excluded_list = NULL;
  3180. if (list->included_hint_name) {
  3181. SDL_AddHintCallback(list->included_hint_name, SDL_VIDPIDIncludedHintChanged, list);
  3182. }
  3183. if (list->excluded_hint_name) {
  3184. SDL_AddHintCallback(list->excluded_hint_name, SDL_VIDPIDExcludedHintChanged, list);
  3185. }
  3186. list->initialized = true;
  3187. if (list->included_hint_name) {
  3188. included_list = SDL_GetHint(list->included_hint_name);
  3189. }
  3190. if (list->excluded_hint_name) {
  3191. excluded_list = SDL_GetHint(list->excluded_hint_name);
  3192. }
  3193. SDL_LoadVIDPIDListFromHints(list, included_list, excluded_list);
  3194. }
  3195. bool SDL_VIDPIDInList(Uint16 vendor_id, Uint16 product_id, const SDL_vidpid_list *list)
  3196. {
  3197. int i;
  3198. Uint32 vidpid = MAKE_VIDPID(vendor_id, product_id);
  3199. for (i = 0; i < list->num_excluded_entries; ++i) {
  3200. if (vidpid == list->excluded_entries[i]) {
  3201. return false;
  3202. }
  3203. }
  3204. for (i = 0; i < list->num_included_entries; ++i) {
  3205. if (vidpid == list->included_entries[i]) {
  3206. return true;
  3207. }
  3208. }
  3209. return false;
  3210. }
  3211. void SDL_FreeVIDPIDList(SDL_vidpid_list *list)
  3212. {
  3213. if (list->included_hint_name) {
  3214. SDL_RemoveHintCallback(list->included_hint_name, SDL_VIDPIDIncludedHintChanged, list);
  3215. }
  3216. if (list->excluded_hint_name) {
  3217. SDL_RemoveHintCallback(list->excluded_hint_name, SDL_VIDPIDExcludedHintChanged, list);
  3218. }
  3219. if (list->included_entries) {
  3220. SDL_free(list->included_entries);
  3221. list->included_entries = NULL;
  3222. list->num_included_entries = 0;
  3223. list->max_included_entries = 0;
  3224. }
  3225. if (list->excluded_entries) {
  3226. SDL_free(list->excluded_entries);
  3227. list->excluded_entries = NULL;
  3228. list->num_excluded_entries = 0;
  3229. list->max_excluded_entries = 0;
  3230. }
  3231. list->initialized = false;
  3232. }