SDL_joystick.c 121 KB

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