Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputUser.cs 91KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. using System;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.Utilities;
  6. using UnityEngine.Profiling;
  7. ////REVIEW: remove users automatically when exiting play mode?
  8. ////REVIEW: do we need to handle the case where devices are added to a user that are each associated with a different user account
  9. ////REVIEW: how should we handle pairings of devices *not* called for by a control scheme? should that result in a failed match?
  10. ////TODO: option to bind to *all* devices instead of just the paired ones (bindToAllDevices)
  11. ////TODO: the account selection stuff needs cleanup; the current flow is too convoluted
  12. namespace UnityEngine.InputSystem.Users
  13. {
  14. /// <summary>
  15. /// Represents a specific user/player interacting with one or more devices and input actions.
  16. /// </summary>
  17. /// <remarks>
  18. /// Principally, an InputUser represents a human interacting with the application. Moreover, at any point
  19. /// each InputUser represents a human actor distinct from all other InputUsers in the system.
  20. ///
  21. /// Each user has one or more paired devices. In general, these devices are unique to each user. However,
  22. /// it is permitted to use <see cref="PerformPairingWithDevice"/> to pair the same device to multiple users.
  23. /// This can be useful in setups such as split-keyboard (e.g. one user using left side of keyboard and the
  24. /// other the right one) use or hotseat-style gameplay (e.g. two players taking turns on the same game controller).
  25. ///
  26. /// Note that the InputUser API, like <see cref="InputAction"/>) is a play mode-only feature. When exiting play mode,
  27. /// all users are automatically removed and all devices automatically unpaired.
  28. /// </remarks>
  29. /// <seealso cref="InputUserChange"/>
  30. public struct InputUser : IEquatable<InputUser>
  31. {
  32. public const uint InvalidId = 0;
  33. /// <summary>
  34. /// Whether this is a currently active user record in <see cref="all"/>.
  35. /// </summary>
  36. /// <remarks>
  37. /// Users that are removed (<see cref="UnpairDevicesAndRemoveUser"/>) will become invalid.
  38. /// </remarks>
  39. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  40. /// <seealso cref="InputUserChange.Removed"/>
  41. public bool valid
  42. {
  43. get
  44. {
  45. if (m_Id == InvalidId)
  46. return false;
  47. // See if there's a currently active user with the given ID.
  48. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  49. if (s_GlobalState.allUsers[i].m_Id == m_Id)
  50. return true;
  51. return false;
  52. }
  53. }
  54. /// <summary>
  55. /// The sequence number of the user.
  56. /// </summary>
  57. /// <remarks>
  58. /// It can be useful to establish a sorting of players locally such that it is
  59. /// known who is the first player, who is the second, and so on. This property
  60. /// gives the positioning of the user within <see cref="all"/>.
  61. ///
  62. /// Note that the index of a user may change as users are added and removed.
  63. /// </remarks>
  64. /// <seealso cref="all"/>
  65. public int index
  66. {
  67. get
  68. {
  69. if (m_Id == InvalidId)
  70. throw new InvalidOperationException("Invalid user");
  71. var userIndex = TryFindUserIndex(m_Id);
  72. if (userIndex == -1)
  73. throw new InvalidOperationException($"User with ID {m_Id} is no longer valid");
  74. return userIndex;
  75. }
  76. }
  77. /// <summary>
  78. /// The unique numeric ID of the user.
  79. /// </summary>
  80. /// <remarks>
  81. /// The ID of a user is internally assigned and cannot be changed over its lifetime. No two users, even
  82. /// if not concurrently active, will receive the same ID.
  83. ///
  84. /// The ID stays valid and unique even if the user is removed and no longer <see cref="valid"/>.
  85. /// </remarks>
  86. public uint id => m_Id;
  87. ////TODO: bring documentation for these back when user management is implemented on Xbox and PS
  88. public InputUserAccountHandle? platformUserAccountHandle => s_GlobalState.allUserData[index].platformUserAccountHandle;
  89. public string platformUserAccountName => s_GlobalState.allUserData[index].platformUserAccountName;
  90. public string platformUserAccountId => s_GlobalState.allUserData[index].platformUserAccountId;
  91. ////REVIEW: Does it make sense to track used devices separately from paired devices?
  92. /// <summary>
  93. /// Devices assigned/paired/linked to the user.
  94. /// </summary>
  95. /// <remarks>
  96. /// It is generally valid for a device to be assigned to multiple users. For example, two users could
  97. /// both use the local keyboard in a split-keyboard or hot seat setup. However, a platform may restrict this
  98. /// and mandate that a device never belong to more than one user. This is the case on Xbox and PS4, for
  99. /// example.
  100. ///
  101. /// To associate devices with users, use <see cref="PerformPairingWithDevice"/>. To remove devices, use
  102. /// <see cref="UnpairDevice"/> or <see cref="UnpairDevicesAndRemoveUser"/>.
  103. ///
  104. /// The array will be empty for a user who is currently not paired to any devices.
  105. ///
  106. /// If <see cref="actions"/> is set (<see cref="AssociateActionsWithUser(IInputActionCollection)"/>), then
  107. /// <see cref="IInputActionCollection.devices"/> will be kept synchronized with the devices paired to the user.
  108. /// </remarks>
  109. /// <seealso cref="PerformPairingWithDevice"/>
  110. /// <seealso cref="UnpairDevice"/>
  111. /// <seealso cref="UnpairDevices"/>
  112. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  113. /// <seealso cref="InputUserChange.DevicePaired"/>
  114. /// <seealso cref="InputUserChange.DeviceUnpaired"/>
  115. public ReadOnlyArray<InputDevice> pairedDevices
  116. {
  117. get
  118. {
  119. var userIndex = index;
  120. return new ReadOnlyArray<InputDevice>(s_GlobalState.allPairedDevices, s_GlobalState.allUserData[userIndex].deviceStartIndex,
  121. s_GlobalState.allUserData[userIndex].deviceCount);
  122. }
  123. }
  124. /// <summary>
  125. /// Devices that were removed while they were still paired to the user.
  126. /// </summary>
  127. /// <remarks>
  128. ///
  129. /// This list is cleared once the user has either regained lost devices or has regained other devices
  130. /// such that the <see cref="controlScheme"/> is satisfied.
  131. /// </remarks>
  132. /// <seealso cref="InputUserChange.DeviceRegained"/>
  133. /// <seealso cref="InputUserChange.DeviceLost"/>
  134. public ReadOnlyArray<InputDevice> lostDevices
  135. {
  136. get
  137. {
  138. var userIndex = index;
  139. return new ReadOnlyArray<InputDevice>(s_GlobalState.allLostDevices, s_GlobalState.allUserData[userIndex].lostDeviceStartIndex,
  140. s_GlobalState.allUserData[userIndex].lostDeviceCount);
  141. }
  142. }
  143. /// <summary>
  144. /// Actions associated with the user.
  145. /// </summary>
  146. /// <remarks>
  147. /// Associating actions with a user will synchronize the actions with the devices paired to the
  148. /// user. Also, it makes it possible to use support for control scheme activation (<see
  149. /// cref="ActivateControlScheme(InputControlScheme)"/> and related APIs like <see cref="controlScheme"/>
  150. /// and <see cref="controlSchemeMatch"/>).
  151. ///
  152. /// Note that is generally does not make sense for users to share actions. Instead, each user should
  153. /// receive a set of actions private to the user.
  154. /// </remarks>
  155. /// <seealso cref="AssociateActionsWithUser(IInputActionCollection)"/>
  156. /// <seealso cref="InputActionMap"/>
  157. /// <seealso cref="InputActionAsset"/>
  158. /// <seealso cref="InputUserChange.ControlsChanged"/>
  159. public IInputActionCollection actions => s_GlobalState.allUserData[index].actions;
  160. /// <summary>
  161. /// The control scheme currently employed by the user.
  162. /// </summary>
  163. /// <remarks>
  164. /// This is null by default.
  165. ///
  166. /// Any time the value of this property changes (whether by <see cref="ActivateControlScheme(string)"/>
  167. /// or by automatic switching), a notification is sent on <see cref="onChange"/> with
  168. /// <see cref="InputUserChange.ControlSchemeChanged"/>.
  169. ///
  170. /// Be aware that using control schemes with InputUsers requires <see cref="actions"/> to
  171. /// be set, i.e. input actions to be associated with the user (<see
  172. /// cref="AssociateActionsWithUser(IInputActionCollection)"/>).
  173. /// </remarks>
  174. /// <seealso cref="ActivateControlScheme(string)"/>
  175. /// <seealso cref="ActivateControlScheme(InputControlScheme)"/>
  176. /// <seealso cref="InputUserChange.ControlSchemeChanged"/>
  177. public InputControlScheme? controlScheme => s_GlobalState.allUserData[index].controlScheme;
  178. /// <summary>
  179. /// The result of matching the device requirements given by <see cref="controlScheme"/> against
  180. /// the devices paired to the user (<see cref="pairedDevices"/>).
  181. /// </summary>
  182. /// <remarks>
  183. /// When devices are paired to or unpaired from a user, as well as when a new control scheme is
  184. /// activated on a user, this property is updated automatically.
  185. /// </remarks>
  186. /// <seealso cref="InputControlScheme.deviceRequirements"/>
  187. /// <seealso cref="InputControlScheme.PickDevicesFrom{TDevices}"/>
  188. public InputControlScheme.MatchResult controlSchemeMatch => s_GlobalState.allUserData[index].controlSchemeMatch;
  189. /// <summary>
  190. /// Whether the user is missing devices required by the <see cref="controlScheme"/> activated
  191. /// on the user.
  192. /// </summary>
  193. /// <remarks>
  194. /// This will only take required devices into account. Device requirements marked optional (<see
  195. /// cref="InputControlScheme.DeviceRequirement.isOptional"/>) will not be considered missing
  196. /// devices if they cannot be satisfied based on the devices paired to the user.
  197. /// </remarks>
  198. /// <seealso cref="InputControlScheme.deviceRequirements"/>
  199. public bool hasMissingRequiredDevices => s_GlobalState.allUserData[index].controlSchemeMatch.hasMissingRequiredDevices;
  200. /// <summary>
  201. /// List of all current users.
  202. /// </summary>
  203. /// <remarks>
  204. /// Use <see cref="PerformPairingWithDevice"/> to add new users and <see cref="UnpairDevicesAndRemoveUser"/> to
  205. /// remove users.
  206. ///
  207. /// Note that this array does not necessarily correspond to the list of users present at the platform level
  208. /// (e.g. Xbox and PS4). There can be users present at the platform level that are not present in this array
  209. /// (e.g. because they are not joined to the game) and users can even be present more than once (e.g. if
  210. /// playing on the user account but as two different players in the game). Also, there can be users in the array
  211. /// that are not present at the platform level.
  212. /// </remarks>
  213. /// <seealso cref="PerformPairingWithDevice"/>
  214. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  215. public static ReadOnlyArray<InputUser> all => new ReadOnlyArray<InputUser>(s_GlobalState.allUsers, 0, s_GlobalState.allUserCount);
  216. /// <summary>
  217. /// Event that is triggered when the <see cref="InputUser">user</see> setup in the system
  218. /// changes.
  219. /// </summary>
  220. /// <remarks>
  221. /// Each notification receives the user that was affected by the change and, in the form of <see cref="InputUserChange"/>,
  222. /// a description of what has changed about the user. The third parameter may be null but if the change will be related
  223. /// to an input device, will reference the device involved in the change.
  224. /// </remarks>
  225. public static event Action<InputUser, InputUserChange, InputDevice> onChange
  226. {
  227. add
  228. {
  229. if (value == null)
  230. throw new ArgumentNullException(nameof(value));
  231. s_GlobalState.onChange.AddCallback(value);
  232. }
  233. remove
  234. {
  235. if (value == null)
  236. throw new ArgumentNullException(nameof(value));
  237. s_GlobalState.onChange.RemoveCallback(value);
  238. }
  239. }
  240. /// <summary>
  241. /// Event that is triggered when a device is used that is not currently paired to any user.
  242. /// </summary>
  243. /// <remarks>
  244. /// A device is considered "used" when it has magnitude (<see cref="InputControl.EvaluateMagnitude()"/>) greater than zero
  245. /// on a control that is not noisy (<see cref="InputControl.noisy"/>) and not synthetic (i.e. not a control that is
  246. /// "made up" like <see cref="Keyboard.anyKey"/>; <see cref="InputControl.synthetic"/>).
  247. ///
  248. /// Detecting the use of unpaired devices has a non-zero cost. While multiple levels of tests are applied to try to
  249. /// cheaply ignore devices that have events sent to them that do not contain user activity, finding out whether
  250. /// a device had real user activity will eventually require going through the device control by control.
  251. ///
  252. /// To enable detection of the use of unpaired devices, set <see cref="listenForUnpairedDeviceActivity"/> to true.
  253. /// It is disabled by default.
  254. ///
  255. /// The callback is invoked for each non-leaf, non-synthetic, non-noisy control that has been actuated on the device.
  256. /// It being restricted to non-leaf controls means that if, say, the stick on a gamepad is actuated in both X and Y
  257. /// direction, you will see two calls: one with stick/x and one with stick/y.
  258. ///
  259. /// The reason that the callback is invoked for each individual control is that pairing often relies on checking
  260. /// for specific kinds of interactions. For example, a pairing callback may listen exclusively for button presses.
  261. ///
  262. /// Note that whether the use of unpaired devices leads to them getting paired is under the control of the application.
  263. /// If the device should be paired, invoke <see cref="PerformPairingWithDevice"/> from the callback. If you do so,
  264. /// no further callbacks will get triggered for other controls that may have been actuated in the same event.
  265. ///
  266. /// Be aware that the callback is fired <em>before</em> input is actually incorporated into the device (it is
  267. /// indirectly triggered from <see cref="InputSystem.onEvent"/>). This means at the time the callback is run,
  268. /// the state of the given device does not yet have the input that triggered the callback. For this reason, the
  269. /// callback receives a second argument that references the event from which the use of an unpaired device was
  270. /// detected.
  271. ///
  272. /// What this sequence allows is to make changes to the system before the input is processed. For example, an
  273. /// action that is enabled as part of the callback will subsequently respond to the input that triggered the
  274. /// callback.
  275. ///
  276. /// <example>
  277. /// <code>
  278. /// // Activate support for listening to device activity.
  279. /// ++InputUser.listenForUnpairedDeviceActivity;
  280. ///
  281. /// // When a button on an unpaired device is pressed, pair the device to a new
  282. /// // or existing user.
  283. /// InputUser.onUnpairedDeviceUsed +=
  284. /// usedControl =>
  285. /// {
  286. /// // Only react to button presses on unpaired devices.
  287. /// if (!(usedControl is ButtonControl))
  288. /// return;
  289. ///
  290. /// // Pair the device to a user.
  291. /// InputUser.PerformPairingWithDevice(usedControl.device);
  292. /// };
  293. /// </code>
  294. /// </example>
  295. ///
  296. /// Another possible use of the callback is for implementing automatic control scheme switching for a user such that
  297. /// the user can, for example, switch from keyboard&amp;mouse to gamepad seamlessly by simply picking up the gamepad
  298. /// and starting to play.
  299. /// </remarks>
  300. public static event Action<InputControl, InputEventPtr> onUnpairedDeviceUsed
  301. {
  302. add
  303. {
  304. if (value == null)
  305. throw new ArgumentNullException(nameof(value));
  306. s_GlobalState.onUnpairedDeviceUsed.AddCallback(value);
  307. if (s_GlobalState.listenForUnpairedDeviceActivity > 0)
  308. HookIntoEvents();
  309. }
  310. remove
  311. {
  312. if (value == null)
  313. throw new ArgumentNullException(nameof(value));
  314. s_GlobalState.onUnpairedDeviceUsed.RemoveCallback(value);
  315. if (s_GlobalState.onUnpairedDeviceUsed.length == 0)
  316. UnhookFromDeviceStateChange();
  317. }
  318. }
  319. /// <summary>
  320. /// Callback that works in combination with <see cref="onUnpairedDeviceUsed"/>. If all callbacks
  321. /// added to this event return <c>false</c> for a
  322. /// </summary>
  323. /// <remarks>
  324. /// Checking a given event for activity of interest is relatively fast but is still costlier than
  325. /// not doing it all. In case only certain devices are of interest for <see cref="onUnpairedDeviceUsed"/>,
  326. /// this "pre-filter" can be used to quickly reject entire devices and thus skip looking closer at
  327. /// an event.
  328. ///
  329. /// The first argument is the <see cref="InputDevice"/> than an event has been received for.
  330. /// The second argument is the <see cref="InputEvent"/> that is being looked at.
  331. ///
  332. /// A callback should return <c>true</c> if it wants <see cref="onUnpairedDeviceUsed"/> to proceed
  333. /// looking at the event and should return <c>false</c> if the event should be skipped.
  334. ///
  335. /// If multiple callbacks are added to the event, it is enough for any single one callback
  336. /// to return <c>true</c> for the event to get looked at.
  337. /// </remarks>
  338. /// <seealso cref="onUnpairedDeviceUsed"/>
  339. /// <seealso cref="listenForUnpairedDeviceActivity"/>
  340. public static event Func<InputDevice, InputEventPtr, bool> onPrefilterUnpairedDeviceActivity
  341. {
  342. add
  343. {
  344. if (value == null)
  345. throw new ArgumentNullException(nameof(value));
  346. s_GlobalState.onPreFilterUnpairedDeviceUsed.AddCallback(value);
  347. }
  348. remove
  349. {
  350. if (value == null)
  351. throw new ArgumentNullException(nameof(value));
  352. s_GlobalState.onPreFilterUnpairedDeviceUsed.RemoveCallback(value);
  353. }
  354. }
  355. ////TODO: After 1.0, make this a simple bool API that *underneath* uses a counter rather than exposing the counter
  356. //// directly to the user.
  357. /// <summary>
  358. /// Whether to listen for user activity on currently unpaired devices and invoke <see cref="onUnpairedDeviceUsed"/>
  359. /// if such activity is detected.
  360. /// </summary>
  361. /// <remarks>
  362. /// This is off by default.
  363. ///
  364. /// Note that enabling this has a non-zero cost. Whenever the state changes of a device that is not currently paired
  365. /// to a user, the system has to spend time figuring out whether there was a meaningful change or whether it's just
  366. /// noise on the device.
  367. ///
  368. /// This is an integer rather than a bool to allow multiple systems to concurrently use to listen for unpaired
  369. /// device activity without treading on each other when enabling/disabling the code path.
  370. /// </remarks>
  371. /// <seealso cref="onUnpairedDeviceUsed"/>
  372. /// <seealso cref="pairedDevices"/>
  373. /// <seealso cref="PerformPairingWithDevice"/>
  374. public static int listenForUnpairedDeviceActivity
  375. {
  376. get => s_GlobalState.listenForUnpairedDeviceActivity;
  377. set
  378. {
  379. if (value < 0)
  380. throw new ArgumentOutOfRangeException(nameof(value), "Cannot be negative");
  381. if (value > 0 && s_GlobalState.onUnpairedDeviceUsed.length > 0)
  382. HookIntoEvents();
  383. else if (value == 0)
  384. UnhookFromDeviceStateChange();
  385. s_GlobalState.listenForUnpairedDeviceActivity = value;
  386. }
  387. }
  388. public override string ToString()
  389. {
  390. if (!valid)
  391. return $"<Invalid> (id: {m_Id})";
  392. var deviceList = string.Join(",", pairedDevices);
  393. return $"User #{index} (id: {m_Id}, devices: {deviceList}, actions: {actions})";
  394. }
  395. /// <summary>
  396. /// Associate a collection of <see cref="InputAction"/>s with the user.
  397. /// </summary>
  398. /// <param name="actions">Actions to associate with the user, either an <see cref="InputActionAsset"/>
  399. /// or an <see cref="InputActionMap"/>. Can be <c>null</c> to unset the current association.</param>
  400. /// <exception cref="InvalidOperationException">The user instance is invalid.</exception>
  401. /// <remarks>
  402. /// Associating actions with a user will ensure that the <see cref="IInputActionCollection.devices"/> and
  403. /// <see cref="IInputActionCollection.bindingMask"/> property of the action collection are automatically
  404. /// kept in sync with the device paired to the user (see <see cref="pairedDevices"/>) and the control
  405. /// scheme active on the user (see <see cref="controlScheme"/>).
  406. ///
  407. /// <example>
  408. /// <code>
  409. /// var gamepad = Gamepad.all[0];
  410. ///
  411. /// // Pair the gamepad to a user.
  412. /// var user = InputUser.PerformPairingWithDevice(gamepad);
  413. ///
  414. /// // Create an action map with an action.
  415. /// var actionMap = new InputActionMap():
  416. /// actionMap.AddAction("Fire", binding: "&lt;Gamepad&gt;/buttonSouth");
  417. ///
  418. /// // Associate the action map with the user (the same works for an asset).
  419. /// user.AssociateActionsWithUser(actionMap);
  420. ///
  421. /// // Now the action map is restricted to just the gamepad that is paired
  422. /// // with the user, even if there are more gamepads currently connected.
  423. /// </code>
  424. /// </example>
  425. /// </remarks>
  426. /// <seealso cref="actions"/>
  427. public void AssociateActionsWithUser(IInputActionCollection actions)
  428. {
  429. var userIndex = index; // Throws if user is invalid.
  430. if (s_GlobalState.allUserData[userIndex].actions == actions)
  431. return;
  432. // If we already had actions associated, reset the binding mask and device list.
  433. var oldActions = s_GlobalState.allUserData[userIndex].actions;
  434. if (oldActions != null)
  435. {
  436. oldActions.devices = null;
  437. oldActions.bindingMask = null;
  438. }
  439. s_GlobalState.allUserData[userIndex].actions = actions;
  440. // If we've switched to a different set of actions, synchronize our state.
  441. if (actions != null)
  442. {
  443. HookIntoActionChange();
  444. actions.devices = pairedDevices;
  445. if (s_GlobalState.allUserData[userIndex].controlScheme != null)
  446. ActivateControlSchemeInternal(userIndex, s_GlobalState.allUserData[userIndex].controlScheme.Value);
  447. }
  448. }
  449. public ControlSchemeChangeSyntax ActivateControlScheme(string schemeName)
  450. {
  451. // Look up control scheme by name in actions.
  452. if (!string.IsNullOrEmpty(schemeName))
  453. {
  454. FindControlScheme(schemeName, out InputControlScheme scheme); // throws if not found
  455. return ActivateControlScheme(scheme);
  456. }
  457. return ActivateControlScheme(new InputControlScheme());
  458. }
  459. private bool TryFindControlScheme(string schemeName, out InputControlScheme scheme)
  460. {
  461. if (string.IsNullOrEmpty(schemeName))
  462. {
  463. scheme = default;
  464. return false;
  465. }
  466. // Need actions to be available to be able to activate control schemes by name only.
  467. if (s_GlobalState.allUserData[index].actions == null)
  468. throw new InvalidOperationException(
  469. $"Cannot set control scheme '{schemeName}' by name on user #{index} as not actions have been associated with the user yet (AssociateActionsWithUser)");
  470. // Attempt to find control scheme by name
  471. var controlSchemes = s_GlobalState.allUserData[index].actions.controlSchemes;
  472. for (var i = 0; i < controlSchemes.Count; ++i)
  473. {
  474. if (string.Compare(controlSchemes[i].name, schemeName,
  475. StringComparison.InvariantCultureIgnoreCase) == 0)
  476. {
  477. scheme = controlSchemes[i];
  478. return true;
  479. }
  480. }
  481. scheme = default;
  482. return false;
  483. }
  484. internal void FindControlScheme(string schemeName, out InputControlScheme scheme)
  485. {
  486. if (TryFindControlScheme(schemeName, out scheme))
  487. return;
  488. throw new ArgumentException(
  489. $"Cannot find control scheme '{schemeName}' in actions '{s_GlobalState.allUserData[index].actions}'");
  490. }
  491. public ControlSchemeChangeSyntax ActivateControlScheme(InputControlScheme scheme)
  492. {
  493. var userIndex = index; // Throws if user is invalid.
  494. if (s_GlobalState.allUserData[userIndex].controlScheme != scheme ||
  495. (scheme == default && s_GlobalState.allUserData[userIndex].controlScheme != null))
  496. {
  497. ActivateControlSchemeInternal(userIndex, scheme);
  498. Notify(userIndex, InputUserChange.ControlSchemeChanged, null);
  499. }
  500. return new ControlSchemeChangeSyntax { m_UserIndex = userIndex };
  501. }
  502. private void ActivateControlSchemeInternal(int userIndex, InputControlScheme scheme)
  503. {
  504. var isEmpty = scheme == default;
  505. if (isEmpty)
  506. s_GlobalState.allUserData[userIndex].controlScheme = null;
  507. else
  508. s_GlobalState.allUserData[userIndex].controlScheme = scheme;
  509. if (s_GlobalState.allUserData[userIndex].actions != null)
  510. {
  511. if (isEmpty)
  512. {
  513. s_GlobalState.allUserData[userIndex].actions.bindingMask = null;
  514. s_GlobalState.allUserData[userIndex].controlSchemeMatch.Dispose();
  515. s_GlobalState.allUserData[userIndex].controlSchemeMatch = new InputControlScheme.MatchResult();
  516. }
  517. else
  518. {
  519. s_GlobalState.allUserData[userIndex].actions.bindingMask = new InputBinding { groups = scheme.bindingGroup };
  520. UpdateControlSchemeMatch(userIndex);
  521. // If we had lost some devices, flush the list. We haven't regained the device
  522. // but we're no longer missing devices to play.
  523. if (s_GlobalState.allUserData[userIndex].controlSchemeMatch.isSuccessfulMatch)
  524. RemoveLostDevicesForUser(userIndex);
  525. }
  526. }
  527. }
  528. /// <summary>
  529. /// Unpair a single device from the user.
  530. /// </summary>
  531. /// <param name="device">Device to unpair from the user. If the device is not currently paired to the user,
  532. /// the method does nothing.</param>
  533. /// <exception cref="ArgumentNullException"><paramref name="device"/> is <c>null</c>.</exception>
  534. /// <remarks>
  535. /// If actions are associated with the user (<see cref="actions"/>), the list of devices used by the
  536. /// actions (<see cref="IInputActionCollection.devices"/>) is automatically updated.
  537. ///
  538. /// If a control scheme is activated on the user (<see cref="controlScheme"/>), <see cref="controlSchemeMatch"/>
  539. /// is automatically updated.
  540. ///
  541. /// Sends <see cref="InputUserChange.DeviceUnpaired"/> through <see cref="onChange"/>.
  542. /// </remarks>
  543. /// <seealso cref="PerformPairingWithDevice"/>
  544. /// <seealso cref="pairedDevices"/>
  545. /// <seealso cref="UnpairDevices"/>
  546. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  547. /// <seealso cref="InputUserChange.DeviceUnpaired"/>
  548. public void UnpairDevice(InputDevice device)
  549. {
  550. if (device == null)
  551. throw new ArgumentNullException(nameof(device));
  552. var userIndex = index; // Throws if user is invalid.
  553. // Ignore if not currently paired to user.
  554. if (!pairedDevices.ContainsReference(device))
  555. return;
  556. RemoveDeviceFromUser(userIndex, device);
  557. }
  558. /// <summary>
  559. /// Unpair all devices from the user.
  560. /// </summary>
  561. /// <remarks>
  562. /// If actions are associated with the user (<see cref="actions"/>), the list of devices used by the
  563. /// actions (<see cref="IInputActionCollection.devices"/>) is automatically updated.
  564. ///
  565. /// If a control scheme is activated on the user (<see cref="controlScheme"/>), <see cref="controlSchemeMatch"/>
  566. /// is automatically updated.
  567. ///
  568. /// Sends <see cref="InputUserChange.DeviceUnpaired"/> through <see cref="onChange"/> for every device
  569. /// unpaired from the user.
  570. /// </remarks>
  571. /// <seealso cref="PerformPairingWithDevice"/>
  572. /// <seealso cref="pairedDevices"/>
  573. /// <seealso cref="UnpairDevice"/>
  574. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  575. /// <seealso cref="InputUserChange.DeviceUnpaired"/>
  576. public void UnpairDevices()
  577. {
  578. var userIndex = index; // Throws if user is invalid.
  579. RemoveLostDevicesForUser(userIndex);
  580. using (InputActionRebindingExtensions.DeferBindingResolution())
  581. {
  582. // We could remove the devices in bulk here but we still have to notify one
  583. // by one which ends up being more complicated than just unpairing the devices
  584. // individually here.
  585. while (s_GlobalState.allUserData[userIndex].deviceCount > 0)
  586. UnpairDevice(s_GlobalState.allPairedDevices[s_GlobalState.allUserData[userIndex].deviceStartIndex + s_GlobalState.allUserData[userIndex].deviceCount - 1]);
  587. }
  588. // Update control scheme, if necessary.
  589. if (s_GlobalState.allUserData[userIndex].controlScheme != null)
  590. UpdateControlSchemeMatch(userIndex);
  591. }
  592. private static void RemoveLostDevicesForUser(int userIndex)
  593. {
  594. var lostDeviceCount = s_GlobalState.allUserData[userIndex].lostDeviceCount;
  595. if (lostDeviceCount > 0)
  596. {
  597. var lostDeviceStartIndex = s_GlobalState.allUserData[userIndex].lostDeviceStartIndex;
  598. ArrayHelpers.EraseSliceWithCapacity(ref s_GlobalState.allLostDevices, ref s_GlobalState.allLostDeviceCount,
  599. lostDeviceStartIndex, lostDeviceCount);
  600. s_GlobalState.allUserData[userIndex].lostDeviceCount = 0;
  601. s_GlobalState.allUserData[userIndex].lostDeviceStartIndex = 0;
  602. // Adjust indices of other users.
  603. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  604. {
  605. if (s_GlobalState.allUserData[i].lostDeviceStartIndex > lostDeviceStartIndex)
  606. s_GlobalState.allUserData[i].lostDeviceStartIndex -= lostDeviceCount;
  607. }
  608. }
  609. }
  610. /// <summary>
  611. /// Unpair all devices from the user and remove the user.
  612. /// </summary>
  613. /// <remarks>
  614. /// If actions are associated with the user (<see cref="actions"/>), the list of devices used by the
  615. /// actions (<see cref="IInputActionCollection.devices"/>) is reset as is the binding mask (<see
  616. /// cref="IInputActionCollection.bindingMask"/>) in case a control scheme is activated on the user.
  617. ///
  618. /// Sends <see cref="InputUserChange.DeviceUnpaired"/> through <see cref="onChange"/> for every device
  619. /// unpaired from the user.
  620. ///
  621. /// Sends <see cref="InputUserChange.Removed"/>.
  622. /// </remarks>
  623. /// <seealso cref="PerformPairingWithDevice"/>
  624. /// <seealso cref="pairedDevices"/>
  625. /// <seealso cref="UnpairDevice"/>
  626. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  627. /// <seealso cref="InputUserChange.DeviceUnpaired"/>
  628. /// <seealso cref="InputUserChange.Removed"/>
  629. public void UnpairDevicesAndRemoveUser()
  630. {
  631. UnpairDevices();
  632. var userIndex = index;
  633. RemoveUser(userIndex);
  634. m_Id = default;
  635. }
  636. /// <summary>
  637. /// Return a list of all currently added devices that are not paired to any user.
  638. /// </summary>
  639. /// <returns>A (possibly empty) list of devices that are currently not paired to a user.</returns>
  640. /// <remarks>
  641. /// The resulting list uses <see cref="Allocator.Temp"> temporary, unmanaged memory</see>. If not disposed of
  642. /// explicitly, the list will automatically be deallocated at the end of the frame and will become unusable.
  643. /// </remarks>
  644. /// <seealso cref="InputSystem.devices"/>
  645. /// <seealso cref="pairedDevices"/>
  646. /// <seealso cref="PerformPairingWithDevice"/>
  647. public static InputControlList<InputDevice> GetUnpairedInputDevices()
  648. {
  649. var list = new InputControlList<InputDevice>(Allocator.Temp);
  650. GetUnpairedInputDevices(ref list);
  651. return list;
  652. }
  653. /// <summary>
  654. /// Add all currently added devices that are not paired to any user to <paramref name="list"/>.
  655. /// </summary>
  656. /// <param name="list">List to add the devices to. Devices will be added to the end.</param>
  657. /// <returns>Number of devices added to <paramref name="list"/>.</returns>
  658. /// <seealso cref="InputSystem.devices"/>
  659. /// <seealso cref="pairedDevices"/>
  660. /// <seealso cref="PerformPairingWithDevice"/>
  661. public static int GetUnpairedInputDevices(ref InputControlList<InputDevice> list)
  662. {
  663. var countBefore = list.Count;
  664. foreach (var device in InputSystem.devices)
  665. {
  666. // If it's in s_AllPairedDevices, there is *some* user that is using the device.
  667. // We don't care which one it is here.
  668. if (ArrayHelpers.ContainsReference(s_GlobalState.allPairedDevices, s_GlobalState.allPairedDeviceCount, device))
  669. continue;
  670. list.Add(device);
  671. }
  672. return list.Count - countBefore;
  673. }
  674. /// <summary>
  675. /// Find the user (if any) that <paramref name="device"/> is currently paired to.
  676. /// </summary>
  677. /// <param name="device">An input device.</param>
  678. /// <returns>The user that <paramref name="device"/> is currently paired to or <c>null</c> if the device
  679. /// is not currently paired to an user.</returns>
  680. /// <remarks>
  681. /// Note that multiple users may be paired to the same device. If that is the case for <paramref name="device"/>,
  682. /// the method will return one of the users with no guarantee which one it is.
  683. ///
  684. /// To find all users paired to a device requires manually going through the list of users and their paired
  685. /// devices.
  686. /// </remarks>
  687. /// <exception cref="ArgumentNullException"><paramref name="device"/> is <c>null</c>.</exception>
  688. /// <seealso cref="pairedDevices"/>
  689. /// <seealso cref="PerformPairingWithDevice"/>
  690. public static InputUser? FindUserPairedToDevice(InputDevice device)
  691. {
  692. if (device == null)
  693. throw new ArgumentNullException(nameof(device));
  694. var userIndex = TryFindUserIndex(device);
  695. if (userIndex == -1)
  696. return null;
  697. return s_GlobalState.allUsers[userIndex];
  698. }
  699. public static InputUser? FindUserByAccount(InputUserAccountHandle platformUserAccountHandle)
  700. {
  701. if (platformUserAccountHandle == default(InputUserAccountHandle))
  702. throw new ArgumentException("Empty platform user account handle", nameof(platformUserAccountHandle));
  703. var userIndex = TryFindUserIndex(platformUserAccountHandle);
  704. if (userIndex == -1)
  705. return null;
  706. return s_GlobalState.allUsers[userIndex];
  707. }
  708. public static InputUser CreateUserWithoutPairedDevices()
  709. {
  710. var userIndex = AddUser();
  711. return s_GlobalState.allUsers[userIndex];
  712. }
  713. ////REVIEW: allow re-adding a user through this method?
  714. /// <summary>
  715. /// Pair the given device to a user.
  716. /// </summary>
  717. /// <param name="device">Device to pair to a user.</param>
  718. /// <param name="user">Optional parameter. If given, instead of creating a new user to pair the device
  719. /// to, the device is paired to the given user.</param>
  720. /// <param name="options">Optional set of options to modify pairing behavior.</param>
  721. /// <remarks>
  722. /// By default, a new user is created and <paramref name="device"/> is added <see cref="pairedDevices"/>
  723. /// of the user and <see cref="InputUserChange.DevicePaired"/> is sent on <see cref="onChange"/>.
  724. ///
  725. /// If a valid user is supplied to <paramref name="user"/>, the device is paired to the given user instead
  726. /// of creating a new user. By default, the device is added to the list of already paired devices for the user.
  727. /// This can be changed by using <see cref="InputUserPairingOptions.UnpairCurrentDevicesFromUser"/> which causes
  728. /// devices currently paired to the user to first be unpaired.
  729. ///
  730. /// The method will not prevent pairing of the same device to multiple users.
  731. ///
  732. /// Note that if the user has an associated set of actions (<see cref="actions"/>), the list of devices on the
  733. /// actions (<see cref="IInputActionCollection.devices"/>) will automatically be updated meaning that the newly
  734. /// paired devices will automatically reflect in the set of devices available to the user's actions. If the
  735. /// user has a control scheme that is currently activated (<see cref="controlScheme"/>), then <see cref="controlSchemeMatch"/>
  736. /// will also automatically update to reflect the matching of devices to the control scheme's device requirements.
  737. ///
  738. /// <example>
  739. /// <code>
  740. /// // Pair device to new user.
  741. /// var user = InputUser.PerformPairingWithDevice(wand1);
  742. ///
  743. /// // Pair another device to the same user.
  744. /// InputUser.PerformPairingWithDevice(wand2, user: user);
  745. /// </code>
  746. /// </example>
  747. /// </remarks>
  748. /// <seealso cref="pairedDevices"/>
  749. /// <seealso cref="UnpairDevice"/>
  750. /// <seealso cref="UnpairDevices"/>
  751. /// <seealso cref="UnpairDevicesAndRemoveUser"/>
  752. /// <seealso cref="InputUserChange.DevicePaired"/>
  753. public static InputUser PerformPairingWithDevice(InputDevice device,
  754. InputUser user = default,
  755. InputUserPairingOptions options = InputUserPairingOptions.None)
  756. {
  757. if (device == null)
  758. throw new ArgumentNullException(nameof(device));
  759. if (user != default && !user.valid)
  760. throw new ArgumentException("Invalid user", nameof(user));
  761. // Create new user, if needed.
  762. int userIndex;
  763. if (user == default)
  764. {
  765. userIndex = AddUser();
  766. }
  767. else
  768. {
  769. // We have an existing user.
  770. userIndex = user.index;
  771. // See if we're supposed to clear out the user's currently paired devices first.
  772. if ((options & InputUserPairingOptions.UnpairCurrentDevicesFromUser) != 0)
  773. user.UnpairDevices();
  774. // Ignore call if device is already paired to user.
  775. if (user.pairedDevices.ContainsReference(device))
  776. {
  777. // Still might have to initiate user account selection.
  778. if ((options & InputUserPairingOptions.ForcePlatformUserAccountSelection) != 0)
  779. InitiateUserAccountSelection(userIndex, device, options);
  780. return user;
  781. }
  782. }
  783. // Handle the user account side of pairing.
  784. var accountSelectionInProgress = InitiateUserAccountSelection(userIndex, device, options);
  785. // Except if we have initiate user account selection, pair the device to
  786. // to the user now.
  787. if (!accountSelectionInProgress)
  788. AddDeviceToUser(userIndex, device);
  789. return s_GlobalState.allUsers[userIndex];
  790. }
  791. private static bool InitiateUserAccountSelection(int userIndex, InputDevice device,
  792. InputUserPairingOptions options)
  793. {
  794. // See if there's a platform user account we can get from the device.
  795. // NOTE: We don't query the current user account if the caller has opted to force account selection.
  796. var queryUserAccountResult =
  797. (options & InputUserPairingOptions.ForcePlatformUserAccountSelection) == 0
  798. ? UpdatePlatformUserAccount(userIndex, device)
  799. : 0;
  800. ////REVIEW: what should we do if there already is an account selection in progress? InvalidOperationException?
  801. // If the device supports user account selection but we didn't get one,
  802. // try to initiate account selection.
  803. if ((options & InputUserPairingOptions.ForcePlatformUserAccountSelection) != 0 ||
  804. (queryUserAccountResult != InputDeviceCommand.GenericFailure &&
  805. (queryUserAccountResult & (long)QueryPairedUserAccountCommand.Result.DevicePairedToUserAccount) == 0 &&
  806. (options & InputUserPairingOptions.ForceNoPlatformUserAccountSelection) == 0))
  807. {
  808. if (InitiateUserAccountSelectionAtPlatformLevel(device))
  809. {
  810. s_GlobalState.allUserData[userIndex].flags |= UserFlags.UserAccountSelectionInProgress;
  811. s_GlobalState.ongoingAccountSelections.Append(
  812. new OngoingAccountSelection
  813. {
  814. device = device,
  815. userId = s_GlobalState.allUsers[userIndex].id,
  816. });
  817. // Make sure we receive a notification for the configuration event.
  818. HookIntoDeviceChange();
  819. // Tell listeners that we started an account selection.
  820. Notify(userIndex, InputUserChange.AccountSelectionInProgress, device);
  821. return true;
  822. }
  823. }
  824. return false;
  825. }
  826. public bool Equals(InputUser other)
  827. {
  828. return m_Id == other.m_Id;
  829. }
  830. public override bool Equals(object obj)
  831. {
  832. if (ReferenceEquals(null, obj))
  833. return false;
  834. return obj is InputUser && Equals((InputUser)obj);
  835. }
  836. public override int GetHashCode()
  837. {
  838. return (int)m_Id;
  839. }
  840. public static bool operator==(InputUser left, InputUser right)
  841. {
  842. return left.m_Id == right.m_Id;
  843. }
  844. public static bool operator!=(InputUser left, InputUser right)
  845. {
  846. return left.m_Id != right.m_Id;
  847. }
  848. /// <summary>
  849. /// Add a new user.
  850. /// </summary>
  851. /// <returns>Index of the newly created user.</returns>
  852. /// <remarks>
  853. /// Adding a user sends a notification with <see cref="InputUserChange.Added"/> through <see cref="onChange"/>.
  854. ///
  855. /// The user will start out with no devices and no actions assigned.
  856. ///
  857. /// The user is added to <see cref="all"/>.
  858. /// </remarks>
  859. private static int AddUser()
  860. {
  861. var id = ++s_GlobalState.lastUserId;
  862. // Add to list.
  863. var userCount = s_GlobalState.allUserCount;
  864. ArrayHelpers.AppendWithCapacity(ref s_GlobalState.allUsers, ref userCount, new InputUser { m_Id = id });
  865. var userIndex = ArrayHelpers.AppendWithCapacity(ref s_GlobalState.allUserData, ref s_GlobalState.allUserCount, new UserData());
  866. // Send notification.
  867. Notify(userIndex, InputUserChange.Added, null);
  868. return userIndex;
  869. }
  870. /// <summary>
  871. /// Remove an active user.
  872. /// </summary>
  873. /// <param name="userIndex">Index of active user.</param>
  874. /// <remarks>
  875. /// Removing a user also unassigns all currently assigned devices from the user. On completion of this
  876. /// method, <see cref="pairedDevices"/> of <paramref name="user"/> will be empty.
  877. /// </remarks>
  878. private static void RemoveUser(int userIndex)
  879. {
  880. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  881. Debug.Assert(s_GlobalState.allUserData[userIndex].deviceCount == 0, "User must not have paired devices still");
  882. // Reset data from control scheme.
  883. if (s_GlobalState.allUserData[userIndex].controlScheme != null)
  884. {
  885. if (s_GlobalState.allUserData[userIndex].actions != null)
  886. s_GlobalState.allUserData[userIndex].actions.bindingMask = null;
  887. }
  888. s_GlobalState.allUserData[userIndex].controlSchemeMatch.Dispose();
  889. // Remove lost devices.
  890. RemoveLostDevicesForUser(userIndex);
  891. // Remove account selections that are in progress.
  892. for (var i = 0; i < s_GlobalState.ongoingAccountSelections.length; ++i)
  893. {
  894. if (s_GlobalState.ongoingAccountSelections[i].userId != s_GlobalState.allUsers[userIndex].id)
  895. continue;
  896. s_GlobalState.ongoingAccountSelections.RemoveAtByMovingTailWithCapacity(i);
  897. --i;
  898. }
  899. // Send notification (do before we actually remove the user).
  900. Notify(userIndex, InputUserChange.Removed, null);
  901. // Remove.
  902. var userCount = s_GlobalState.allUserCount;
  903. s_GlobalState.allUsers.EraseAtWithCapacity(ref userCount, userIndex);
  904. s_GlobalState.allUserData.EraseAtWithCapacity(ref s_GlobalState.allUserCount, userIndex);
  905. // Remove our hook if we no longer need it.
  906. if (s_GlobalState.allUserCount == 0)
  907. {
  908. UnhookFromDeviceChange();
  909. UnhookFromActionChange();
  910. }
  911. }
  912. private static void Notify(int userIndex, InputUserChange change, InputDevice device)
  913. {
  914. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  915. if (s_GlobalState.onChange.length == 0)
  916. return;
  917. Profiler.BeginSample("InputUser.onChange");
  918. s_GlobalState.onChange.LockForChanges();
  919. for (var i = 0; i < s_GlobalState.onChange.length; ++i)
  920. {
  921. try
  922. {
  923. s_GlobalState.onChange[i](s_GlobalState.allUsers[userIndex], change, device);
  924. }
  925. catch (Exception exception)
  926. {
  927. Debug.LogError($"{exception.GetType().Name} while executing 'InputUser.onChange' callbacks");
  928. Debug.LogException(exception);
  929. }
  930. }
  931. s_GlobalState.onChange.UnlockForChanges();
  932. Profiler.EndSample();
  933. }
  934. private static int TryFindUserIndex(uint userId)
  935. {
  936. Debug.Assert(userId != InvalidId, "User ID is invalid");
  937. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  938. {
  939. if (s_GlobalState.allUsers[i].m_Id == userId)
  940. return i;
  941. }
  942. return -1;
  943. }
  944. private static int TryFindUserIndex(InputUserAccountHandle platformHandle)
  945. {
  946. Debug.Assert(platformHandle != default, "User platform handle is invalid");
  947. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  948. {
  949. if (s_GlobalState.allUserData[i].platformUserAccountHandle == platformHandle)
  950. return i;
  951. }
  952. return -1;
  953. }
  954. /// <summary>
  955. /// Find the user (if any) that is currently assigned the given <paramref name="device"/>.
  956. /// </summary>
  957. /// <param name="device">An input device that has been added to the system.</param>
  958. /// <returns>Index of the user that has <paramref name="device"/> among its <see cref="pairedDevices"/> or -1 if
  959. /// no user is currently assigned the given device.</returns>
  960. private static int TryFindUserIndex(InputDevice device)
  961. {
  962. Debug.Assert(device != null, "Device cannot be null");
  963. var indexOfDevice = s_GlobalState.allPairedDevices.IndexOfReference(device, s_GlobalState.allPairedDeviceCount);
  964. if (indexOfDevice == -1)
  965. return -1;
  966. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  967. {
  968. var startIndex = s_GlobalState.allUserData[i].deviceStartIndex;
  969. if (startIndex <= indexOfDevice && indexOfDevice < startIndex + s_GlobalState.allUserData[i].deviceCount)
  970. return i;
  971. }
  972. return -1;
  973. }
  974. /// <summary>
  975. /// Add the given device to the user as either a lost device or a paired device.
  976. /// </summary>
  977. /// <param name="userIndex"></param>
  978. /// <param name="device"></param>
  979. /// <param name="asLostDevice"></param>
  980. private static void AddDeviceToUser(int userIndex, InputDevice device, bool asLostDevice = false, bool dontUpdateControlScheme = false)
  981. {
  982. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  983. Debug.Assert(device != null, "Device cannot be null");
  984. if (asLostDevice)
  985. Debug.Assert(!s_GlobalState.allUsers[userIndex].lostDevices.ContainsReference(device), "Device already in set of lostDevices for user");
  986. else
  987. Debug.Assert(!s_GlobalState.allUsers[userIndex].pairedDevices.ContainsReference(device), "Device already in set of pairedDevices for user");
  988. var deviceCount = asLostDevice
  989. ? s_GlobalState.allUserData[userIndex].lostDeviceCount
  990. : s_GlobalState.allUserData[userIndex].deviceCount;
  991. var deviceStartIndex = asLostDevice
  992. ? s_GlobalState.allUserData[userIndex].lostDeviceStartIndex
  993. : s_GlobalState.allUserData[userIndex].deviceStartIndex;
  994. ++s_GlobalState.pairingStateVersion;
  995. // Move our devices to end of array.
  996. if (deviceCount > 0)
  997. {
  998. ArrayHelpers.MoveSlice(asLostDevice ? s_GlobalState.allLostDevices : s_GlobalState.allPairedDevices, deviceStartIndex,
  999. asLostDevice ? s_GlobalState.allLostDeviceCount - deviceCount : s_GlobalState.allPairedDeviceCount - deviceCount,
  1000. deviceCount);
  1001. // Adjust users that have been impacted by the change.
  1002. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1003. {
  1004. if (i == userIndex)
  1005. continue;
  1006. if ((asLostDevice ? s_GlobalState.allUserData[i].lostDeviceStartIndex : s_GlobalState.allUserData[i].deviceStartIndex) <= deviceStartIndex)
  1007. continue;
  1008. if (asLostDevice)
  1009. s_GlobalState.allUserData[i].lostDeviceStartIndex -= deviceCount;
  1010. else
  1011. s_GlobalState.allUserData[i].deviceStartIndex -= deviceCount;
  1012. }
  1013. }
  1014. // Append to array.
  1015. if (asLostDevice)
  1016. {
  1017. s_GlobalState.allUserData[userIndex].lostDeviceStartIndex = s_GlobalState.allLostDeviceCount - deviceCount;
  1018. ArrayHelpers.AppendWithCapacity(ref s_GlobalState.allLostDevices, ref s_GlobalState.allLostDeviceCount, device);
  1019. ++s_GlobalState.allUserData[userIndex].lostDeviceCount;
  1020. }
  1021. else
  1022. {
  1023. s_GlobalState.allUserData[userIndex].deviceStartIndex = s_GlobalState.allPairedDeviceCount - deviceCount;
  1024. ArrayHelpers.AppendWithCapacity(ref s_GlobalState.allPairedDevices, ref s_GlobalState.allPairedDeviceCount, device);
  1025. ++s_GlobalState.allUserData[userIndex].deviceCount;
  1026. // If the user has actions, sync the devices on them with what we have now.
  1027. var actions = s_GlobalState.allUserData[userIndex].actions;
  1028. if (actions != null)
  1029. {
  1030. actions.devices = s_GlobalState.allUsers[userIndex].pairedDevices;
  1031. // Also, if we have a control scheme, update the matching of device requirements
  1032. // against the device we now have.
  1033. if (!dontUpdateControlScheme && s_GlobalState.allUserData[userIndex].controlScheme != null)
  1034. UpdateControlSchemeMatch(userIndex);
  1035. }
  1036. }
  1037. // Make sure we get OnDeviceChange notifications.
  1038. HookIntoDeviceChange();
  1039. // Let listeners know.
  1040. Notify(userIndex, asLostDevice ? InputUserChange.DeviceLost : InputUserChange.DevicePaired, device);
  1041. }
  1042. private static void RemoveDeviceFromUser(int userIndex, InputDevice device, bool asLostDevice = false)
  1043. {
  1044. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  1045. Debug.Assert(device != null, "Device cannot be null");
  1046. var deviceIndex = asLostDevice
  1047. ? s_GlobalState.allLostDevices.IndexOfReference(device, s_GlobalState.allLostDeviceCount)
  1048. : s_GlobalState.allPairedDevices.IndexOfReference(device, s_GlobalState.allUserData[userIndex].deviceStartIndex,
  1049. s_GlobalState.allUserData[userIndex].deviceCount);
  1050. if (deviceIndex == -1)
  1051. {
  1052. // Device not in list. Ignore.
  1053. return;
  1054. }
  1055. if (asLostDevice)
  1056. {
  1057. s_GlobalState.allLostDevices.EraseAtWithCapacity(ref s_GlobalState.allLostDeviceCount, deviceIndex);
  1058. --s_GlobalState.allUserData[userIndex].lostDeviceCount;
  1059. }
  1060. else
  1061. {
  1062. ++s_GlobalState.pairingStateVersion;
  1063. s_GlobalState.allPairedDevices.EraseAtWithCapacity(ref s_GlobalState.allPairedDeviceCount, deviceIndex);
  1064. --s_GlobalState.allUserData[userIndex].deviceCount;
  1065. }
  1066. // Adjust indices of other users.
  1067. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1068. {
  1069. if ((asLostDevice ? s_GlobalState.allUserData[i].lostDeviceStartIndex : s_GlobalState.allUserData[i].deviceStartIndex) <= deviceIndex)
  1070. continue;
  1071. if (asLostDevice)
  1072. --s_GlobalState.allUserData[i].lostDeviceStartIndex;
  1073. else
  1074. --s_GlobalState.allUserData[i].deviceStartIndex;
  1075. }
  1076. if (!asLostDevice)
  1077. {
  1078. // Remove any ongoing account selections for the user on the given device.
  1079. for (var i = 0; i < s_GlobalState.ongoingAccountSelections.length; ++i)
  1080. {
  1081. if (s_GlobalState.ongoingAccountSelections[i].userId != s_GlobalState.allUsers[userIndex].id ||
  1082. s_GlobalState.ongoingAccountSelections[i].device != device)
  1083. continue;
  1084. s_GlobalState.ongoingAccountSelections.RemoveAtByMovingTailWithCapacity(i);
  1085. --i;
  1086. }
  1087. // If the user has actions, sync the devices on them with what we have now.
  1088. var actions = s_GlobalState.allUserData[userIndex].actions;
  1089. if (actions != null)
  1090. {
  1091. actions.devices = s_GlobalState.allUsers[userIndex].pairedDevices;
  1092. if (s_GlobalState.allUsers[userIndex].controlScheme != null)
  1093. UpdateControlSchemeMatch(userIndex);
  1094. }
  1095. // Notify listeners.
  1096. Notify(userIndex, InputUserChange.DeviceUnpaired, device);
  1097. }
  1098. }
  1099. private static void UpdateControlSchemeMatch(int userIndex, bool autoPairMissing = false)
  1100. {
  1101. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  1102. // Nothing to do if we don't have a control scheme.
  1103. if (s_GlobalState.allUserData[userIndex].controlScheme == null)
  1104. return;
  1105. // Get rid of last match result and start new match.
  1106. s_GlobalState.allUserData[userIndex].controlSchemeMatch.Dispose();
  1107. var matchResult = new InputControlScheme.MatchResult();
  1108. try
  1109. {
  1110. // Match the control scheme's requirements against the devices paired to the user.
  1111. var scheme = s_GlobalState.allUserData[userIndex].controlScheme.Value;
  1112. if (scheme.deviceRequirements.Count > 0)
  1113. {
  1114. var availableDevices = new InputControlList<InputDevice>(Allocator.Temp);
  1115. try
  1116. {
  1117. // Add devices already paired to user.
  1118. availableDevices.AddSlice(s_GlobalState.allUsers[userIndex].pairedDevices);
  1119. // If we're supposed to grab whatever additional devices we need from what's
  1120. // available, add all unpaired devices to the list.
  1121. // NOTE: These devices go *after* the devices already paired (if any) meaning that
  1122. // the control scheme matching will grab already paired devices *first*.
  1123. if (autoPairMissing)
  1124. {
  1125. var startIndex = availableDevices.Count;
  1126. var count = GetUnpairedInputDevices(ref availableDevices);
  1127. // We want to favor devices that are already assigned to the same platform user account.
  1128. // Sort the unpaired devices we've added to the list such that the ones belonging to the
  1129. // same user account come first.
  1130. if (s_GlobalState.allUserData[userIndex].platformUserAccountHandle != null)
  1131. availableDevices.Sort(startIndex, count,
  1132. new CompareDevicesByUserAccount
  1133. {
  1134. platformUserAccountHandle = s_GlobalState.allUserData[userIndex].platformUserAccountHandle.Value
  1135. });
  1136. }
  1137. matchResult = scheme.PickDevicesFrom(availableDevices);
  1138. if (matchResult.isSuccessfulMatch)
  1139. {
  1140. // Control scheme is satisfied with the devices we have available.
  1141. // If we may have grabbed as of yet unpaired devices, go and pair them to the user.
  1142. if (autoPairMissing)
  1143. {
  1144. // Update match result on user before potentially invoking callbacks.
  1145. s_GlobalState.allUserData[userIndex].controlSchemeMatch = matchResult;
  1146. foreach (var device in matchResult.devices)
  1147. {
  1148. // Skip if already paired to user.
  1149. if (s_GlobalState.allUsers[userIndex].pairedDevices.ContainsReference(device))
  1150. continue;
  1151. AddDeviceToUser(userIndex, device, dontUpdateControlScheme: true);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. finally
  1157. {
  1158. availableDevices.Dispose();
  1159. }
  1160. }
  1161. s_GlobalState.allUserData[userIndex].controlSchemeMatch = matchResult;
  1162. }
  1163. catch (Exception)
  1164. {
  1165. // If we had an exception and are bailing out, make sure we aren't leaking native memory
  1166. // we allocated.
  1167. matchResult.Dispose();
  1168. throw;
  1169. }
  1170. }
  1171. private static long UpdatePlatformUserAccount(int userIndex, InputDevice device)
  1172. {
  1173. Debug.Assert(userIndex >= 0 && userIndex < s_GlobalState.allUserCount, "User index is invalid");
  1174. // Fetch account details from backend.
  1175. var queryResult = QueryPairedPlatformUserAccount(device, out var platformUserAccountHandle,
  1176. out var platformUserAccountName, out var platformUserAccountId);
  1177. // Nothing much to do if not supported by device.
  1178. if (queryResult == InputDeviceCommand.GenericFailure)
  1179. {
  1180. // Check if there's an account selection in progress. There shouldn't be as it's
  1181. // weird for the device to no signal it does not support querying user account, but
  1182. // just to be safe, we check.
  1183. if ((s_GlobalState.allUserData[userIndex].flags & UserFlags.UserAccountSelectionInProgress) != 0)
  1184. Notify(userIndex, InputUserChange.AccountSelectionCanceled, null);
  1185. s_GlobalState.allUserData[userIndex].platformUserAccountHandle = null;
  1186. s_GlobalState.allUserData[userIndex].platformUserAccountName = null;
  1187. s_GlobalState.allUserData[userIndex].platformUserAccountId = null;
  1188. return queryResult;
  1189. }
  1190. // Check if there's an account selection that we have initiated.
  1191. if ((s_GlobalState.allUserData[userIndex].flags & UserFlags.UserAccountSelectionInProgress) != 0)
  1192. {
  1193. // Yes, there is. See if it is complete.
  1194. if ((queryResult & (long)QueryPairedUserAccountCommand.Result.UserAccountSelectionInProgress) != 0)
  1195. {
  1196. // No, still in progress.
  1197. }
  1198. else if ((queryResult & (long)QueryPairedUserAccountCommand.Result.UserAccountSelectionCanceled) != 0)
  1199. {
  1200. // Got canceled.
  1201. Notify(userIndex, InputUserChange.AccountSelectionCanceled, device);
  1202. }
  1203. else
  1204. {
  1205. // Yes, it is complete.
  1206. s_GlobalState.allUserData[userIndex].flags &= ~UserFlags.UserAccountSelectionInProgress;
  1207. s_GlobalState.allUserData[userIndex].platformUserAccountHandle = platformUserAccountHandle;
  1208. s_GlobalState.allUserData[userIndex].platformUserAccountName = platformUserAccountName;
  1209. s_GlobalState.allUserData[userIndex].platformUserAccountId = platformUserAccountId;
  1210. Notify(userIndex, InputUserChange.AccountSelectionComplete, device);
  1211. }
  1212. }
  1213. // Check if user account details have changed.
  1214. else if (s_GlobalState.allUserData[userIndex].platformUserAccountHandle != platformUserAccountHandle ||
  1215. s_GlobalState.allUserData[userIndex].platformUserAccountId != platformUserAccountId)
  1216. {
  1217. s_GlobalState.allUserData[userIndex].platformUserAccountHandle = platformUserAccountHandle;
  1218. s_GlobalState.allUserData[userIndex].platformUserAccountName = platformUserAccountName;
  1219. s_GlobalState.allUserData[userIndex].platformUserAccountId = platformUserAccountId;
  1220. Notify(userIndex, InputUserChange.AccountChanged, device);
  1221. }
  1222. else if (s_GlobalState.allUserData[userIndex].platformUserAccountName != platformUserAccountName)
  1223. {
  1224. Notify(userIndex, InputUserChange.AccountNameChanged, device);
  1225. }
  1226. return queryResult;
  1227. }
  1228. ////TODO: bring documentation for these back when user management is implemented on Xbox and PS
  1229. private static long QueryPairedPlatformUserAccount(InputDevice device,
  1230. out InputUserAccountHandle? platformAccountHandle, out string platformAccountName, out string platformAccountId)
  1231. {
  1232. Debug.Assert(device != null, "Device cannot be null");
  1233. // Query user account info from backend.
  1234. var queryPairedUser = QueryPairedUserAccountCommand.Create();
  1235. var result = device.ExecuteCommand(ref queryPairedUser);
  1236. if (result == InputDeviceCommand.GenericFailure)
  1237. {
  1238. // Not currently paired to user account in backend.
  1239. platformAccountHandle = null;
  1240. platformAccountName = null;
  1241. platformAccountId = null;
  1242. return InputDeviceCommand.GenericFailure;
  1243. }
  1244. // Success. There is a user account currently paired to the device and we now have the
  1245. // platform's user account details.
  1246. if ((result & (long)QueryPairedUserAccountCommand.Result.DevicePairedToUserAccount) != 0)
  1247. {
  1248. platformAccountHandle =
  1249. new InputUserAccountHandle(device.description.interfaceName ?? "<Unknown>", queryPairedUser.handle);
  1250. platformAccountName = queryPairedUser.name;
  1251. platformAccountId = queryPairedUser.id;
  1252. }
  1253. else
  1254. {
  1255. // The device supports QueryPairedUserAccountCommand but reports that the
  1256. // device is not currently paired to a user.
  1257. //
  1258. // NOTE: On Switch, where the system itself does not store account<->pairing, we will always
  1259. // end up here until we've initiated an account selection through the backend itself.
  1260. platformAccountHandle = null;
  1261. platformAccountName = null;
  1262. platformAccountId = null;
  1263. }
  1264. return result;
  1265. }
  1266. /// <summary>
  1267. /// Try to initiate user account pairing for the given device at the platform level.
  1268. /// </summary>
  1269. /// <param name="device"></param>
  1270. /// <returns>True if the device accepted the request and an account picker has been raised.</returns>
  1271. /// <remarks>
  1272. /// Sends <see cref="InitiateUserAccountPairingCommand"/> to the device.
  1273. /// </remarks>
  1274. private static bool InitiateUserAccountSelectionAtPlatformLevel(InputDevice device)
  1275. {
  1276. Debug.Assert(device != null, "Device cannot be null");
  1277. var initiateUserPairing = InitiateUserAccountPairingCommand.Create();
  1278. var initiatePairingResult = device.ExecuteCommand(ref initiateUserPairing);
  1279. if (initiatePairingResult == (long)InitiateUserAccountPairingCommand.Result.ErrorAlreadyInProgress)
  1280. throw new InvalidOperationException("User pairing already in progress");
  1281. return initiatePairingResult == (long)InitiateUserAccountPairingCommand.Result.SuccessfullyInitiated;
  1282. }
  1283. private static void OnActionChange(object obj, InputActionChange change)
  1284. {
  1285. if (change == InputActionChange.BoundControlsChanged)
  1286. {
  1287. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1288. {
  1289. ref var user = ref s_GlobalState.allUsers[i];
  1290. if (ReferenceEquals(user.actions, obj))
  1291. Notify(i, InputUserChange.ControlsChanged, null);
  1292. }
  1293. }
  1294. }
  1295. /// <summary>
  1296. /// Invoked in response to <see cref="InputSystem.onDeviceChange"/>.
  1297. /// </summary>
  1298. /// <param name="device"></param>
  1299. /// <param name="change"></param>
  1300. /// <remarks>
  1301. /// We monitor the device setup in the system for activity that impacts the user setup.
  1302. /// </remarks>
  1303. private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
  1304. {
  1305. switch (change)
  1306. {
  1307. // Existing device removed. May mean a user has lost a device due to the battery running
  1308. // out or the device being unplugged.
  1309. // NOTE: We ignore Disconnected here. Removed is what gets sent whenever a device is taken off of
  1310. // InputSystem.devices -- which is what we're interested in here.
  1311. case InputDeviceChange.Removed:
  1312. {
  1313. // Could have been removed from multiple users. Repeatedly search in s_AllPairedDevices
  1314. // until we can't find the device anymore.
  1315. var deviceIndex = s_GlobalState.allPairedDevices.IndexOfReference(device, s_GlobalState.allPairedDeviceCount);
  1316. while (deviceIndex != -1)
  1317. {
  1318. // Find user. Must be there as we found the device in s_AllPairedDevices.
  1319. var userIndex = -1;
  1320. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1321. {
  1322. var deviceStartIndex = s_GlobalState.allUserData[i].deviceStartIndex;
  1323. if (deviceStartIndex <= deviceIndex && deviceIndex < deviceStartIndex + s_GlobalState.allUserData[i].deviceCount)
  1324. {
  1325. userIndex = i;
  1326. break;
  1327. }
  1328. }
  1329. // Add device to list of lost devices.
  1330. // NOTE: This will also send a DeviceLost notification.
  1331. // NOTE: Temporarily the device is on both lists.
  1332. AddDeviceToUser(userIndex, device, asLostDevice: true);
  1333. // Remove it from the user.
  1334. RemoveDeviceFromUser(userIndex, device);
  1335. // Search for another user paired to the same device.
  1336. deviceIndex = s_GlobalState.allPairedDevices.IndexOfReference(device, s_GlobalState.allPairedDeviceCount);
  1337. }
  1338. break;
  1339. }
  1340. // New device was added. See if it was a device we previously lost on a user.
  1341. case InputDeviceChange.Added:
  1342. {
  1343. // Search all lost devices. Could affect multiple users.
  1344. // Note that RemoveDeviceFromUser removes one element, hence no advancement of deviceIndex.
  1345. for (var deviceIndex = FindLostDevice(device); deviceIndex != -1;
  1346. deviceIndex = FindLostDevice(device, deviceIndex))
  1347. {
  1348. // Find user. Must be there as we found the device in s_AllLostDevices.
  1349. var userIndex = -1;
  1350. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1351. {
  1352. var deviceStartIndex = s_GlobalState.allUserData[i].lostDeviceStartIndex;
  1353. if (deviceStartIndex <= deviceIndex && deviceIndex < deviceStartIndex + s_GlobalState.allUserData[i].lostDeviceCount)
  1354. {
  1355. userIndex = i;
  1356. break;
  1357. }
  1358. }
  1359. // Remove from list of lost devices. No notification. Notice that we need to use device
  1360. // from lost device list even if its another instance.
  1361. RemoveDeviceFromUser(userIndex, s_GlobalState.allLostDevices[deviceIndex], asLostDevice: true);
  1362. // Notify.
  1363. Notify(userIndex, InputUserChange.DeviceRegained, device);
  1364. // Add back as normally paired device.
  1365. AddDeviceToUser(userIndex, device);
  1366. }
  1367. break;
  1368. }
  1369. // Device had its configuration changed which may mean we have a different user account paired
  1370. // to the device now.
  1371. case InputDeviceChange.ConfigurationChanged:
  1372. {
  1373. // See if the this is a device that we were waiting for an account selection on. If so, pair
  1374. // it to the user that was waiting.
  1375. var wasOngoingAccountSelection = false;
  1376. for (var i = 0; i < s_GlobalState.ongoingAccountSelections.length; ++i)
  1377. {
  1378. if (s_GlobalState.ongoingAccountSelections[i].device != device)
  1379. continue;
  1380. var userIndex = new InputUser { m_Id = s_GlobalState.ongoingAccountSelections[i].userId }.index;
  1381. var queryResult = UpdatePlatformUserAccount(userIndex, device);
  1382. if ((queryResult & (long)QueryPairedUserAccountCommand.Result.UserAccountSelectionInProgress) == 0)
  1383. {
  1384. wasOngoingAccountSelection = true;
  1385. s_GlobalState.ongoingAccountSelections.RemoveAtByMovingTailWithCapacity(i);
  1386. --i;
  1387. // If the device wasn't paired to the user, pair it now.
  1388. if (!s_GlobalState.allUsers[userIndex].pairedDevices.ContainsReference(device))
  1389. AddDeviceToUser(userIndex, device);
  1390. }
  1391. }
  1392. // If it wasn't a configuration change event from an account selection, go and check whether
  1393. // there was a user account change that happened outside the application.
  1394. if (!wasOngoingAccountSelection)
  1395. {
  1396. // Could be paired to multiple users. Repeatedly search in s_AllPairedDevices
  1397. // until we can't find the device anymore.
  1398. var deviceIndex = s_GlobalState.allPairedDevices.IndexOfReference(device, s_GlobalState.allPairedDeviceCount);
  1399. while (deviceIndex != -1)
  1400. {
  1401. // Find user. Must be there as we found the device in s_AllPairedDevices.
  1402. var userIndex = -1;
  1403. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1404. {
  1405. var deviceStartIndex = s_GlobalState.allUserData[i].deviceStartIndex;
  1406. if (deviceStartIndex <= deviceIndex && deviceIndex < deviceStartIndex + s_GlobalState.allUserData[i].deviceCount)
  1407. {
  1408. userIndex = i;
  1409. break;
  1410. }
  1411. }
  1412. // Check user account.
  1413. UpdatePlatformUserAccount(userIndex, device);
  1414. // Search for another user paired to the same device.
  1415. // Note that action is tied to user and hence we can skip to end of slice associated
  1416. // with the current user or at least one element forward.
  1417. var offsetNextSlice = deviceIndex + Math.Max(1, s_GlobalState.allUserData[userIndex].deviceCount);
  1418. deviceIndex = s_GlobalState.allPairedDevices.IndexOfReference(device, offsetNextSlice, s_GlobalState.allPairedDeviceCount - offsetNextSlice);
  1419. }
  1420. }
  1421. break;
  1422. }
  1423. }
  1424. }
  1425. private static int FindLostDevice(InputDevice device, int startIndex = 0)
  1426. {
  1427. // Compare both by device ID and by reference. We may be looking at a device that was recreated
  1428. // due to layout changes (new InputDevice instance, same ID) or a device that was reconnected
  1429. // and thus fetched out of `disconnectedDevices` (same InputDevice instance, new ID).
  1430. var newDeviceId = device.deviceId;
  1431. for (var i = startIndex; i < s_GlobalState.allLostDeviceCount; ++i)
  1432. {
  1433. var lostDevice = s_GlobalState.allLostDevices[i];
  1434. if (device == lostDevice || lostDevice.deviceId == newDeviceId) return i;
  1435. }
  1436. return -1;
  1437. }
  1438. // We hook this into InputSystem.onEvent when listening for activity on unpaired devices.
  1439. // What this means is that we get to run *before* state reaches the device. This in turn
  1440. // means that should the device get paired as a result, actions that are enabled as part
  1441. // of the pairing will immediately get triggered. This would not be the case if we hook
  1442. // into InputState.onDeviceChange instead which only triggers once state has been altered.
  1443. //
  1444. // NOTE: This also means that unpaired device activity will *only* be detected from events,
  1445. // NOT from state changes applied directly through InputState.Change.
  1446. private static void OnEvent(InputEventPtr eventPtr, InputDevice device)
  1447. {
  1448. Debug.Assert(s_GlobalState.listenForUnpairedDeviceActivity != 0,
  1449. "This should only be called while listening for unpaired device activity");
  1450. if (s_GlobalState.listenForUnpairedDeviceActivity == 0)
  1451. return;
  1452. // Ignore input in editor.
  1453. #if UNITY_EDITOR
  1454. if (InputState.currentUpdateType == InputUpdateType.Editor)
  1455. return;
  1456. #endif
  1457. // Ignore any state change not triggered from a state event.
  1458. var eventType = eventPtr.type;
  1459. if (eventType != StateEvent.Type && eventType != DeltaStateEvent.Type)
  1460. return;
  1461. // Ignore event if device is disabled.
  1462. if (!device.enabled)
  1463. return;
  1464. // See if it's a device not belonging to any user.
  1465. if (ArrayHelpers.ContainsReference(s_GlobalState.allPairedDevices, s_GlobalState.allPairedDeviceCount, device))
  1466. {
  1467. // No, it's a device already paired to a player so do nothing.
  1468. return;
  1469. }
  1470. Profiler.BeginSample("InputCheckForUnpairedDeviceActivity");
  1471. // Apply the pre-filter. If there's callbacks and none of them return true,
  1472. // we early out and ignore the event entirely.
  1473. if (!DelegateHelpers.InvokeCallbacksSafe_AnyCallbackReturnsTrue(
  1474. ref s_GlobalState.onPreFilterUnpairedDeviceUsed, device, eventPtr, "InputUser.onPreFilterUnpairedDeviceActivity"))
  1475. {
  1476. Profiler.EndSample();
  1477. return;
  1478. }
  1479. // Go through the changed controls in the event and look for ones actuated
  1480. // above a magnitude of a little above zero.
  1481. foreach (var control in eventPtr.EnumerateChangedControls(device: device, magnitudeThreshold: 0.0001f))
  1482. {
  1483. var deviceHasBeenPaired = false;
  1484. s_GlobalState.onUnpairedDeviceUsed.LockForChanges();
  1485. for (var n = 0; n < s_GlobalState.onUnpairedDeviceUsed.length; ++n)
  1486. {
  1487. var pairingStateVersionBefore = s_GlobalState.pairingStateVersion;
  1488. try
  1489. {
  1490. s_GlobalState.onUnpairedDeviceUsed[n](control, eventPtr);
  1491. }
  1492. catch (Exception exception)
  1493. {
  1494. Debug.LogError($"{exception.GetType().Name} while executing 'InputUser.onUnpairedDeviceUsed' callbacks");
  1495. Debug.LogException(exception);
  1496. }
  1497. if (pairingStateVersionBefore != s_GlobalState.pairingStateVersion
  1498. && FindUserPairedToDevice(device) != null)
  1499. {
  1500. deviceHasBeenPaired = true;
  1501. break;
  1502. }
  1503. }
  1504. s_GlobalState.onUnpairedDeviceUsed.UnlockForChanges();
  1505. // If the device was paired in one of the callbacks, stop processing
  1506. // changes on it.
  1507. if (deviceHasBeenPaired)
  1508. break;
  1509. }
  1510. Profiler.EndSample();
  1511. }
  1512. /// <summary>
  1513. /// Syntax for configuring a control scheme on a user.
  1514. /// </summary>
  1515. public struct ControlSchemeChangeSyntax
  1516. {
  1517. /// <summary>
  1518. /// Leave the user's paired devices in place but pair any available devices
  1519. /// that are still required by the control scheme.
  1520. /// </summary>
  1521. /// <returns></returns>
  1522. /// <remarks>
  1523. /// If there are unpaired devices that, at the platform level, are associated with the same
  1524. /// user account, those will take precedence over other unpaired devices.
  1525. /// </remarks>
  1526. public ControlSchemeChangeSyntax AndPairRemainingDevices()
  1527. {
  1528. UpdateControlSchemeMatch(m_UserIndex, autoPairMissing: true);
  1529. return this;
  1530. }
  1531. internal int m_UserIndex;
  1532. }
  1533. private uint m_Id;
  1534. [Flags]
  1535. internal enum UserFlags
  1536. {
  1537. BindToAllDevices = 1 << 0,
  1538. /// <summary>
  1539. /// Whether we have initiated a user account selection.
  1540. /// </summary>
  1541. UserAccountSelectionInProgress = 1 << 1,
  1542. }
  1543. /// <summary>
  1544. /// Data we store for each user.
  1545. /// </summary>
  1546. private struct UserData
  1547. {
  1548. /// <summary>
  1549. /// The platform handle associated with the user.
  1550. /// </summary>
  1551. /// <remarks>
  1552. /// If set, this identifies the user on the platform. It also means that the devices
  1553. /// assigned to the user may be paired at the platform level.
  1554. /// </remarks>
  1555. public InputUserAccountHandle? platformUserAccountHandle;
  1556. /// <summary>
  1557. /// Plain-text user name as returned by the underlying platform. Null if not associated with user on platform.
  1558. /// </summary>
  1559. public string platformUserAccountName;
  1560. /// <summary>
  1561. /// Platform-specific ID that identifies the user across sessions even if the user
  1562. /// name changes.
  1563. /// </summary>
  1564. /// <remarks>
  1565. /// This might not be a human-readable string.
  1566. /// </remarks>
  1567. public string platformUserAccountId;
  1568. /// <summary>
  1569. /// Number of devices in <see cref="InputUser.s_AllPairedDevices"/> assigned to the user.
  1570. /// </summary>
  1571. public int deviceCount;
  1572. /// <summary>
  1573. /// Index in <see cref="InputUser.s_AllPairedDevices"/> where the devices for this user start. Only valid
  1574. /// if <see cref="deviceCount"/> is greater than zero.
  1575. /// </summary>
  1576. public int deviceStartIndex;
  1577. /// <summary>
  1578. /// Input actions associated with the user.
  1579. /// </summary>
  1580. public IInputActionCollection actions;
  1581. /// <summary>
  1582. /// Currently active control scheme or null if no control scheme has been set on the user.
  1583. /// </summary>
  1584. /// <remarks>
  1585. /// This also dictates the binding mask that we're using with <see cref="actions"/>.
  1586. /// </remarks>
  1587. public InputControlScheme? controlScheme;
  1588. public InputControlScheme.MatchResult controlSchemeMatch;
  1589. /// <summary>
  1590. /// Number of devices in <see cref="InputUser.s_AllLostDevices"/> assigned to the user.
  1591. /// </summary>
  1592. public int lostDeviceCount;
  1593. /// <summary>
  1594. /// Index in <see cref="InputUser.s_AllLostDevices"/> where the lost devices for this user start. Only valid
  1595. /// if <see cref="lostDeviceCount"/> is greater than zero.
  1596. /// </summary>
  1597. public int lostDeviceStartIndex;
  1598. ////TODO
  1599. //public InputUserSettings settings;
  1600. public UserFlags flags;
  1601. }
  1602. /// <summary>
  1603. /// Compare two devices for being associated with a specific platform user account.
  1604. /// </summary>
  1605. private struct CompareDevicesByUserAccount : IComparer<InputDevice>
  1606. {
  1607. public InputUserAccountHandle platformUserAccountHandle;
  1608. public int Compare(InputDevice x, InputDevice y)
  1609. {
  1610. var firstAccountHandle = GetUserAccountHandleForDevice(x);
  1611. var secondAccountHandle = GetUserAccountHandleForDevice(x);
  1612. if (firstAccountHandle == platformUserAccountHandle &&
  1613. secondAccountHandle == platformUserAccountHandle)
  1614. return 0;
  1615. if (firstAccountHandle == platformUserAccountHandle)
  1616. return -1;
  1617. if (secondAccountHandle == platformUserAccountHandle)
  1618. return 1;
  1619. return 0;
  1620. }
  1621. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "device", Justification = "Keep this for future implementation")]
  1622. private static InputUserAccountHandle? GetUserAccountHandleForDevice(InputDevice device)
  1623. {
  1624. ////TODO (need to cache this)
  1625. return null;
  1626. }
  1627. }
  1628. private struct OngoingAccountSelection
  1629. {
  1630. public InputDevice device;
  1631. public uint userId;
  1632. }
  1633. private struct GlobalState
  1634. {
  1635. internal int pairingStateVersion;
  1636. internal uint lastUserId;
  1637. internal int allUserCount;
  1638. internal int allPairedDeviceCount;
  1639. internal int allLostDeviceCount;
  1640. internal InputUser[] allUsers;
  1641. internal UserData[] allUserData;
  1642. internal InputDevice[] allPairedDevices; // We keep a single array that we slice out to each user.
  1643. internal InputDevice[] allLostDevices; // We keep a single array that we slice out to each user.
  1644. internal InlinedArray<OngoingAccountSelection> ongoingAccountSelections;
  1645. internal CallbackArray<Action<InputUser, InputUserChange, InputDevice>> onChange;
  1646. internal CallbackArray<Action<InputControl, InputEventPtr>> onUnpairedDeviceUsed;
  1647. internal CallbackArray<Func<InputDevice, InputEventPtr, bool>> onPreFilterUnpairedDeviceUsed;
  1648. internal Action<object, InputActionChange> actionChangeDelegate;
  1649. internal Action<InputDevice, InputDeviceChange> onDeviceChangeDelegate;
  1650. internal Action<InputEventPtr, InputDevice> onEventDelegate;
  1651. internal bool onActionChangeHooked;
  1652. internal bool onDeviceChangeHooked;
  1653. internal bool onEventHooked;
  1654. internal int listenForUnpairedDeviceActivity;
  1655. }
  1656. private static GlobalState s_GlobalState;
  1657. internal static ISavedState SaveAndResetState()
  1658. {
  1659. // Save current state and provide an opaque interface to restore it
  1660. var savedState = new SavedStructState<GlobalState>(
  1661. ref s_GlobalState,
  1662. (ref GlobalState state) => s_GlobalState = state, // restore
  1663. () => DisposeAndResetGlobalState()); // static dispose
  1664. // Reset global state
  1665. s_GlobalState = default;
  1666. return savedState;
  1667. }
  1668. private static void HookIntoActionChange()
  1669. {
  1670. if (s_GlobalState.onActionChangeHooked)
  1671. return;
  1672. if (s_GlobalState.actionChangeDelegate == null)
  1673. s_GlobalState.actionChangeDelegate = OnActionChange;
  1674. InputSystem.onActionChange += OnActionChange;
  1675. s_GlobalState.onActionChangeHooked = true;
  1676. }
  1677. private static void UnhookFromActionChange()
  1678. {
  1679. if (!s_GlobalState.onActionChangeHooked)
  1680. return;
  1681. InputSystem.onActionChange -= OnActionChange;
  1682. s_GlobalState.onActionChangeHooked = false;
  1683. }
  1684. private static void HookIntoDeviceChange()
  1685. {
  1686. if (s_GlobalState.onDeviceChangeHooked)
  1687. return;
  1688. if (s_GlobalState.onDeviceChangeDelegate == null)
  1689. s_GlobalState.onDeviceChangeDelegate = OnDeviceChange;
  1690. InputSystem.onDeviceChange += s_GlobalState.onDeviceChangeDelegate;
  1691. s_GlobalState.onDeviceChangeHooked = true;
  1692. }
  1693. private static void UnhookFromDeviceChange()
  1694. {
  1695. if (!s_GlobalState.onDeviceChangeHooked)
  1696. return;
  1697. InputSystem.onDeviceChange -= s_GlobalState.onDeviceChangeDelegate;
  1698. s_GlobalState.onDeviceChangeHooked = false;
  1699. }
  1700. private static void HookIntoEvents()
  1701. {
  1702. if (s_GlobalState.onEventHooked)
  1703. return;
  1704. if (s_GlobalState.onEventDelegate == null)
  1705. s_GlobalState.onEventDelegate = OnEvent;
  1706. InputSystem.onEvent += s_GlobalState.onEventDelegate;
  1707. s_GlobalState.onEventHooked = true;
  1708. }
  1709. private static void UnhookFromDeviceStateChange()
  1710. {
  1711. if (!s_GlobalState.onEventHooked)
  1712. return;
  1713. InputSystem.onEvent -= s_GlobalState.onEventDelegate;
  1714. s_GlobalState.onEventHooked = false;
  1715. }
  1716. private static void DisposeAndResetGlobalState()
  1717. {
  1718. // Release native memory held by control scheme match results.
  1719. for (var i = 0; i < s_GlobalState.allUserCount; ++i)
  1720. s_GlobalState.allUserData[i].controlSchemeMatch.Dispose();
  1721. // Don't reset s_LastUserId and just let it increment instead so we never generate
  1722. // the same ID twice.
  1723. var storedLastUserId = s_GlobalState.lastUserId;
  1724. s_GlobalState = default;
  1725. s_GlobalState.lastUserId = storedLastUserId;
  1726. }
  1727. internal static void ResetGlobals()
  1728. {
  1729. UnhookFromActionChange();
  1730. UnhookFromDeviceChange();
  1731. UnhookFromDeviceStateChange();
  1732. DisposeAndResetGlobalState();
  1733. }
  1734. }
  1735. }