SDL_joystick.c 114 KB

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