SDL_joystick.c 122 KB

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