XDeviceAttributions.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Collections.Generic;
  4. //#if UNITY_EDITOR || UNITY_IOS || UNITY_ANDROID || UNITY_WIN || UNITY_MAC
  5. #if true
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using XDebug = UnityEngine.Debug;
  9. #else
  10. using XDebug = System.Diagnostics.Debug;
  11. #endif // UNITY_EDITOR
  12. using NativeHandle = System.IntPtr;
  13. using NativeExHandle = System.IntPtr;
  14. using AOT;
  15. namespace Ximmerse.XR
  16. {
  17. public partial class XDevicePlugin
  18. {
  19. ///////////////////////////////////////////////////////
  20. /// @enum XControllerButtonMasks
  21. /// @brief Masks of Controller buttons.
  22. // Reference : https://msdn.microsoft.com/en-us/library/windows/apps/microsoft.directx_sdk.reference.xinput_gamepad
  23. public enum XControllerButtonMasks
  24. {
  25. kXControllerButton_DpapUp = 0x0001,
  26. kXControllerButton_DpapDown = 0x0002,
  27. kXControllerButton_DpadLeft = 0x0004,
  28. kXControllerButton_DpadRight = 0x0008,
  29. kXControllerButton_Start = 0x0010,
  30. kXControllerButton_Back = 0x0020,
  31. kXControllerButton_LeftThumb = 0x0040,
  32. kXControllerButton_RightThumb = 0x0080,
  33. kXControllerButton_LeftShoulder = 0x0100,
  34. kXControllerButton_RightShoulder = 0x0200,
  35. kXControllerButton_Guide = 0x0400,
  36. kXControllerButton_A = 0x1000,
  37. kXControllerButton_B = 0x2000,
  38. kXControllerButton_X = 0x4000,
  39. kXControllerButton_Y = 0x8000,
  40. // Emulation
  41. kXControllerButton_LeftThumbMove = 0x10000,
  42. kXControllerButton_RightThumbMove = 0x20000,
  43. kXControllerButton_LeftTrigger = 0x40000,
  44. kXControllerButton_RightTrigger = 0x80000,
  45. kXControllerButton_LeftThumbUp = 0x100000,
  46. kXControllerButton_LeftThumbDown = 0x200000,
  47. kXControllerButton_LeftThumbLeft = 0x400000,
  48. kXControllerButton_LeftThumbRight = 0x800000,
  49. kXControllerButton_RightThumbUp = 0x1000000,
  50. kXControllerButton_RightThumbDown = 0x2000000,
  51. kXControllerButton_RightThumbLeft = 0x4000000,
  52. kXControllerButton_RightThumbRight = 0x8000000,
  53. //
  54. kXControllerButton_None = 0x0,
  55. kXControllerButton_ANY = ~kXControllerButton_None,
  56. };
  57. public enum XContextStates
  58. {
  59. kXContextStInited, ///< context state, context enviroment allocted, but not working.
  60. kXContextStStarted, ///< context state, devices in context working.
  61. kXContextStWillRelease, ///< context state, moment before release.
  62. };
  63. ////////////////////////////////////////////////////////
  64. /// @enum XContextAttributes
  65. /// @brief Attributes of Context
  66. /// @deprecated
  67. public enum XContextAttributes
  68. {
  69. //kXCtxAttr_DeviceVersion,
  70. kXCtxAttr_Int_SdkVersion,
  71. kXCtxAttr_Str_SdkVersion,
  72. kXCtxAttr_Int_SDKALGVersion, // int
  73. kXCtxAttr_Int_State, ///< State value indicated context working state,
  74. kXCtxAttr_Str_SdkBuildDetail,
  75. };
  76. ////////////////////////////////////////////////////////
  77. /// @enum XVpuAttributes
  78. /// @brief Attributes of VPU Device.
  79. /// @deprecated
  80. public enum XVpuAttributes
  81. {
  82. kXVpuAttr_Str_SoftwareRevision,
  83. kXVpuAttr_Str_HardwareRevision,
  84. kXVpuAttr_Str_SerialNumber,
  85. kXVpuAttr_Str_DeviceName,
  86. kXVpuAttr_Str_ModelName,
  87. kXVpuAttr_Str_FPGAVersion,
  88. kXVpuAttr_Str_ALGVersion,
  89. kXVpuAttr_Int_ImuFps,
  90. kXVpuAttr_Int_FpgaFps,
  91. /// Reversed
  92. kXVpuAttr_Int_AlgorithmPoseFps,
  93. kXVpuAttr_Int_ErrorCode,
  94. /// Connection state of Controller, see \ref XConnectionStates
  95. kXVpuAttr_Int_ConnectionState,
  96. kXVpuAttr_Int_PowerMode,
  97. /// Battery Level. Invalid if battery mode is external power
  98. kXVpuAttr_Int_Battery,
  99. kXVpuAttr_Int_BatteryVoltage,
  100. kXVpuAttr_Int_BatteryTemperature,
  101. /// IMU info of device, see \ref XAttrImuInfo
  102. kXVpuAttr_Obj_ImuInfo,
  103. /// IMU info of device, Output to variable aguments
  104. kXVpuAttr_V_ImuInfo,
  105. /// 6Dof info of device, see \ref XAttr6DofInfo
  106. kXVpuAttr_Obj_6DofInfo,
  107. /// 6Dof info of device, Output to variable aguments
  108. kXVpuAttr_V_6DofInfo,
  109. /// Pressed button bits
  110. kXVpuAttr_Int_ButtonBits, // int
  111. /// Button events, \ref XButtonEvents
  112. kXVpuAttr_Int_ButtonEvent = kXVpuAttr_Int_ButtonBits,
  113. /// Number of Paired controllers
  114. kXVpuAttr_Int_PairedNumber,
  115. /// Device infos, imu, 6dof, buttons ... see \ref XAttrControllerState
  116. kXVpuAttr_Obj_ControllerState,
  117. /// VPU cammera tracking object pose info. see \ref XAttrTrackingInfo
  118. kXVpuAttr_Obj_TrackingInfo,
  119. /// VPU cammera tracking object pose info. Output to variable aguments
  120. kXVpuAttr_V_TrackingInfo,
  121. //kXVpuAttr_Evt_TrackingPoseUpdate,
  122. /// VPU paired controllers, see \ref XAttrPairedInfos
  123. kXVpuAttr_Obj_PairedInfos,
  124. };
  125. ////////////////////////////////////////////////////////
  126. /// @enum XControllerAttributes
  127. /// @brief Attribute types of controller.
  128. /// @deprecated
  129. public enum XControllerAttributes
  130. {
  131. kXCAttr_Str_FirmwareRevision,
  132. kXCAttr_Str_SoftwareRevision,
  133. kXCAttr_Str_HardwareRevision,
  134. kXCAttr_Str_SerialNumber,
  135. kXCAttr_Str_DeviceName,
  136. kXCAttr_Str_ModelName,
  137. kXCAttr_Str_ManufacturerName,
  138. /// FPS of controller device reporting IMU data
  139. kXCAttr_Int_ImuFps,
  140. kXCAttr_Int_ErrorCode,
  141. /// Connection state of Controller, see \ref XConnectionStates
  142. kXCAttr_Int_ConnectionState,
  143. kXCAttr_Int_PowerMode,
  144. /// Battery Level. Invalid if battery mode is external power
  145. kXCAttr_Int_Battery,
  146. kXCAttr_Int_BatteryVoltage,
  147. kXCAttr_Int_BatteryTemperature,
  148. /// IMU info of device, see \ref XAttrImuInfo
  149. kXCAttr_Obj_ImuInfo,
  150. /// IMU info of device, Output to variable aguments
  151. kXCAttr_V_ImuInfo,
  152. /// 6Dof info of device, see \ref XAttr6DofInfo
  153. kXCAttr_Obj_6DofInfo,
  154. /// 6Dof info of device, Output to variable aguments
  155. kXCAttr_V_6DofInfo,
  156. /// Pressed button bits
  157. kXCAttr_Int_ButtonBits,
  158. /// Button events, \ref XButtonEvents
  159. kXCAttr_Int_ButtonEvent = kXCAttr_Int_ButtonBits,
  160. kXCAttr_Int_Trigger,
  161. /// Device touchpad state info. see \ref XAttrTouchPadInfo
  162. kXCAttr_Obj_TouchPadState,
  163. //kXCAttr_Int_TouchPadState, //
  164. /// Device infos, imu, 6dof, buttons ... see \ref XAttrControllerState
  165. kXCAttr_Obj_ControllerState,
  166. /// Get Controller device type, see \ref XControllerTypes.
  167. kXCAttr_Int_Type,
  168. /// Get Controller device address (MAC address)
  169. kXCAttr_Obj_Address,
  170. /// Get Hex String MAC Address of controller.
  171. kXCAttr_Str_Address,
  172. /// Get Bind ID of connected controller.
  173. kXCAttr_Int_BindID,
  174. };
  175. public enum XPowerModes
  176. {
  177. kXPowerModeExternal = 0, ///< External power supply
  178. kXPowerModeBattery = 1, ///< battery
  179. };
  180. /// /////////////////////////////////////////////////////////////////////
  181. /// \enum XControllerTypes
  182. /// \bref Types defined of Ximmerse Controllers
  183. public enum XControllerTypes
  184. {
  185. kControllerType_Saber = 0x0000,
  186. kControllerType_Kylo = 0x0001,
  187. kControllerType_DType = 0x0002,
  188. kControllerType_PickUp = 0x0003,
  189. kControllerType_3Dof = 0x0200,
  190. kControllerType_Tag = 0x0100,
  191. kControllerType_TagLeft = kControllerType_Tag,
  192. kControllerType_TagRight = 0x0101,
  193. kControllerType_TagRfid = 0x0102,
  194. kControllerType_Cube = 0x0103,
  195. kControllerType_TouchPad = 0x0104,
  196. kControllerType_ShortGun = 0x0105,
  197. kControllerType_LongGun = 0x0106,
  198. kControllerType_CircularLeft = 0x010A,
  199. kControllerType_CircularRight = 0x010B,
  200. kControllerType_Unknow = 0xffff,
  201. kControllerType_BitMask = 0xffff,
  202. }
  203. ////////////////////////////////////////////////////////
  204. /// @enum XControllerTrackModes
  205. /// @brief Algorithm tracking types of controller.
  206. public enum XControllerTrackModes
  207. {
  208. kControllerTrackModeNone,
  209. kControllerTrackModeVisable,
  210. kControllerTrackModeTagMark,
  211. };
  212. //////////////////////////////////////
  213. /// @enum XButtonEvents
  214. /// @brief Button events
  215. public enum XButtonEvents
  216. {
  217. kXBtnEvt_Down,
  218. kXBtnEvt_Up,
  219. //kXBtnEvt_LongPress,
  220. };
  221. ///////////////////////////////////////
  222. /// @struct XButtonEventParam
  223. /// @brief Parameters for button event notification
  224. [StructLayout(LayoutKind.Sequential)]
  225. public struct XButtonEventParam
  226. {
  227. /// Button key value.
  228. public int btn;
  229. /// Event value. see \ref XButtonEvents
  230. public int evt;
  231. public XButtonEventParam(int btn, int evt)
  232. {
  233. this.btn = btn;
  234. this.evt = evt;
  235. }
  236. };
  237. ////////////////////////////////////////
  238. /// @enum XConnectionStates
  239. /// @brief Connection state of device
  240. public enum XConnectionStates
  241. {
  242. kXConnSt_Disconnected = 0,
  243. kXConnSt_Scanning = 1,
  244. kXConnSt_Connecting = 2,
  245. kXConnSt_Connected = 3,
  246. kXConnSt_Disconnecting = 4,
  247. //kXConnSt_Disconnected = ?
  248. };
  249. public enum XPairingStates
  250. {
  251. kXPairStUnpaired = 0,
  252. kXPairStPairing = 1,
  253. kXPairStPaired = 2,
  254. kXPairStUnpairing = 3,
  255. };
  256. public enum XTrackResults
  257. {
  258. kXTrackResultNone = 0,
  259. kXTrackResultPose = 1,
  260. kXTrackResultPosition = 2,
  261. kXTrackResultAll = kXTrackResultPosition | kXTrackResultPose,
  262. };
  263. ///////////////////////////////////////////////
  264. /// @struct XAttr6DofInfo
  265. /// @brief Structure for gettting 6Dof information
  266. [StructLayout(LayoutKind.Sequential)]
  267. public struct XAttr6DofInfo
  268. {
  269. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  270. public float[] position; /// < float buffer for gettting position values
  271. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  272. public float[] rotation; /// < float buffer for getting quaternions of rotation.
  273. public UInt64 timestamp;
  274. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  275. public float[] euler; /// < float buffer for gettting euler rotation.
  276. public XAttr6DofInfo(UInt64 timestamp = 0, float[] position = null, float[] rotation = null)
  277. {
  278. this.timestamp = timestamp;
  279. this.position = position == null ? new float[3] { 0, 0, 0 } : position;
  280. this.rotation = rotation == null ? new float[4] { 0, 0, 0, 0 } : rotation;
  281. this.euler = new float[3];
  282. }
  283. };
  284. //////////////////////////////////////////////
  285. /// @struct XAttrImuInfo
  286. /// @brief Structure for getting IMU information.
  287. [StructLayout(LayoutKind.Sequential)]
  288. public struct XAttrImuInfo
  289. {
  290. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  291. public float[] accelerometer; ///< (out) float buffer for getting accelerometer values.
  292. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  293. public float[] gyroscope; ///< float buffer for getting gyroscope values.
  294. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  295. public float[] magnetism; /// < float buffer for getting magnetism values.
  296. public UInt64 timestamp;
  297. public XAttrImuInfo(
  298. UInt64 timestamp = 0,
  299. float[] accelerometer = null,
  300. float[] gyroscope = null, float[] magnetism = null)
  301. {
  302. this.timestamp = timestamp;
  303. this.accelerometer = accelerometer == null ? new float[3] { 0, 0, 0 } : accelerometer;
  304. this.gyroscope = gyroscope == null ? new float[3] { 0, 0, 0 } : gyroscope;
  305. this.magnetism = magnetism == null ? new float[3] { 0, 0, 0 } : magnetism;
  306. }
  307. };
  308. /////////////////////////////////////////////
  309. /// @struct XAttrTrackingInfo
  310. /// @brief VPU cammera tracking object pose info.
  311. [StructLayout(LayoutKind.Sequential)]
  312. public struct XAttrTrackingInfo
  313. {
  314. public int index;
  315. public int state;
  316. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  317. public float[] position;
  318. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  319. public float[] rotation;
  320. public UInt64 timestamp;
  321. public UInt64 recognized_markers_mask;
  322. public double confidence;
  323. public double marker_distance;
  324. public XAttrTrackingInfo(
  325. UInt64 timestamp = 0,
  326. int index = 0,
  327. int state = 0,
  328. float[] position = null,
  329. float[] rotation = null,
  330. double confidence = 0,
  331. double marker_distance = 0)
  332. {
  333. this.timestamp = timestamp;
  334. this.index = index;
  335. this.state = state;
  336. this.position = position == null ? new float[3] { 0, 0, 0 } : position;
  337. this.rotation = rotation == null ? new float[4] { 0, 0, 0, 0 } : rotation;
  338. this.recognized_markers_mask = 0;
  339. this.confidence = 0;
  340. this.marker_distance = 0;
  341. }
  342. public override string ToString() {
  343. Quaternion q = new Quaternion(rotation[0], rotation[1], rotation[2], rotation[3]);
  344. return string.Format("id:{0},st:{1},time:{2},P:{3},{4},{5}, R:{6},{7},{8}",
  345. index, state, timestamp,
  346. position[0], position[1], position[2],
  347. q.eulerAngles.x, q.eulerAngles.y, q.eulerAngles.z);
  348. }
  349. };
  350. //////////////////////////////////////
  351. /// @struct XAttrControllerState
  352. /// @brief Structure for getting controller state informations.
  353. [StructLayout(LayoutKind.Sequential)]
  354. public struct XAttrControllerState
  355. {
  356. /// quaternion
  357. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  358. public float[] rotation;
  359. /// x, y, z
  360. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  361. public float[] position;
  362. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  363. public float[] accelerometer;
  364. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  365. public float[] gyroscope;
  366. public UInt64 timestamp;
  367. /// bit map indicating button pressed state of controller.
  368. public UInt32 button_state;
  369. //uint8_t valid_flag; ///
  370. // TouchPad, reverve
  371. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
  372. public float[] axes;
  373. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
  374. public float[] euler; /// < float buffer for gettting euler rotation.
  375. public int trigger;
  376. public XAttrControllerState(
  377. UInt64 timestamp = 0,
  378. UInt32 button_state = 0,
  379. float[] rotation = null,
  380. float[] position = null,
  381. float[] accelerometer = null,
  382. float[] gyroscope = null,
  383. float[] axes = null,
  384. int trigger = 0)
  385. {
  386. this.timestamp = timestamp;
  387. this.button_state = button_state;
  388. this.rotation = rotation == null ? new float[4] { 0, 0, 0, 0 } : rotation;
  389. this.position = position == null ? new float[3] { 0, 0, 0 } : position;
  390. this.accelerometer = accelerometer == null ? new float[3] { 0, 0, 0 } : accelerometer;
  391. this.gyroscope = gyroscope == null ? new float[3] { 0, 0, 0 } : gyroscope;
  392. this.axes = axes == null ? new float[6] { 0, 0, 0, 0, 0, 0 } : axes;
  393. euler = new float[3];
  394. this.trigger = trigger;
  395. }
  396. };
  397. public enum XAttrTouchPadEvents
  398. {
  399. kAttrTpadEvt_Idle = 0x00,
  400. kAttrTpadEvt_Press = 0x01,
  401. kAttrTpadEvt_Release = 0x02,
  402. kAttrTpadEvt_Move = 0x03,
  403. };
  404. [StructLayout(LayoutKind.Sequential)]
  405. public struct XAttrTouchPadState
  406. {
  407. public bool pressed; ///< indicated touchpad is touching.
  408. public float x; ///< x coordinate value, from 0 to 1.
  409. public float y; ///< y coordinate value, from 0 to 1.
  410. };
  411. public struct XAttrMacAddress
  412. {
  413. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
  414. public byte[] mac;
  415. }
  416. /////////////////////////////
  417. /// \struct XAttrPairedInfos
  418. /// \bref Structure for getting infomation of paired controllers.
  419. [StructLayout(LayoutKind.Sequential)]
  420. public struct XAttrPairedInfos
  421. {
  422. public int c; ///< Number of paired controller.
  423. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  424. public int[] bind_id_a; ///< An array containing bind ID of paired controllers.
  425. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6 * 16)]
  426. public XAttrMacAddress[] mac_a; ///< An array containing MAC address of paired controllers.
  427. };
  428. public enum XErrorCodes
  429. {
  430. kErrNoError = 0,
  431. kErrUnknonw = -1,
  432. kErrBegin = -1000,
  433. kErrOperationFail,
  434. kErrDeviceNotFound,
  435. kErrInvalidArgument,
  436. kErrVpuNotArrived,
  437. kErrFileNotExist,
  438. kErrFileEmpty,
  439. kErrChecksumFail,
  440. kErrInWrongState,
  441. kErrVpuAuthFailed,
  442. kErrReadMarkerConfigFailed,
  443. kErrNotFound, // no usage
  444. kErrApiInvalid,
  445. kErrVpuCommandFail,
  446. kErrNoDeviceToConnect,
  447. kErrWrongConnectState,
  448. kErrWrongPairingState,
  449. kErrPairListIsFull,
  450. kErrWaitTimeout,
  451. kErrBindIdInvalid,
  452. kErrOutoffMemory,
  453. kErrServiceNotRunning,
  454. kErrServiceInvalid,
  455. kErrCodeError,
  456. // kErrOpClient = -2000,
  457. kErrEnd,
  458. };
  459. public enum XEvents{
  460. kXEvtVpuRequestSuccess,
  461. kXEvtVpuRequestFailed,
  462. kXEvtVpuFwUpgradingStart = 100,
  463. kXEvtVpuFwUpgradingProgress,
  464. kXEvtVpuFwUpgradingEndSuccess,
  465. kXEvtVpuFwUpgradingEndFail,
  466. kXEvtAuthResult = 200,
  467. kXEvtJavaBroadcast = 201,
  468. kXEvtJavaOpcReport = 202,
  469. };
  470. }
  471. }