DepthFrameSource.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using RootSystem = System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace Windows.Kinect
  5. {
  6. //
  7. // Windows.Kinect.DepthFrameSource
  8. //
  9. public sealed partial class DepthFrameSource : Helper.INativeWrapper
  10. {
  11. internal RootSystem.IntPtr _pNative;
  12. RootSystem.IntPtr Helper.INativeWrapper.nativePtr { get { return _pNative; } }
  13. // Constructors and Finalizers
  14. internal DepthFrameSource(RootSystem.IntPtr pNative)
  15. {
  16. _pNative = pNative;
  17. Windows_Kinect_DepthFrameSource_AddRefObject(ref _pNative);
  18. }
  19. ~DepthFrameSource()
  20. {
  21. Dispose(false);
  22. }
  23. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  24. private static extern void Windows_Kinect_DepthFrameSource_ReleaseObject(ref RootSystem.IntPtr pNative);
  25. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  26. private static extern void Windows_Kinect_DepthFrameSource_AddRefObject(ref RootSystem.IntPtr pNative);
  27. private void Dispose(bool disposing)
  28. {
  29. if (_pNative == RootSystem.IntPtr.Zero)
  30. {
  31. return;
  32. }
  33. __EventCleanup();
  34. Helper.NativeObjectCache.RemoveObject<DepthFrameSource>(_pNative);
  35. Windows_Kinect_DepthFrameSource_ReleaseObject(ref _pNative);
  36. _pNative = RootSystem.IntPtr.Zero;
  37. }
  38. // Public Properties
  39. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  40. private static extern ushort Windows_Kinect_DepthFrameSource_get_DepthMaxReliableDistance(RootSystem.IntPtr pNative);
  41. public ushort DepthMaxReliableDistance
  42. {
  43. get
  44. {
  45. if (_pNative == RootSystem.IntPtr.Zero)
  46. {
  47. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  48. }
  49. return Windows_Kinect_DepthFrameSource_get_DepthMaxReliableDistance(_pNative);
  50. }
  51. }
  52. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  53. private static extern ushort Windows_Kinect_DepthFrameSource_get_DepthMinReliableDistance(RootSystem.IntPtr pNative);
  54. public ushort DepthMinReliableDistance
  55. {
  56. get
  57. {
  58. if (_pNative == RootSystem.IntPtr.Zero)
  59. {
  60. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  61. }
  62. return Windows_Kinect_DepthFrameSource_get_DepthMinReliableDistance(_pNative);
  63. }
  64. }
  65. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  66. private static extern RootSystem.IntPtr Windows_Kinect_DepthFrameSource_get_FrameDescription(RootSystem.IntPtr pNative);
  67. public Windows.Kinect.FrameDescription FrameDescription
  68. {
  69. get
  70. {
  71. if (_pNative == RootSystem.IntPtr.Zero)
  72. {
  73. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  74. }
  75. RootSystem.IntPtr objectPointer = Windows_Kinect_DepthFrameSource_get_FrameDescription(_pNative);
  76. Helper.ExceptionHelper.CheckLastError();
  77. if (objectPointer == RootSystem.IntPtr.Zero)
  78. {
  79. return null;
  80. }
  81. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.FrameDescription>(objectPointer, n => new Windows.Kinect.FrameDescription(n));
  82. }
  83. }
  84. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  85. private static extern bool Windows_Kinect_DepthFrameSource_get_IsActive(RootSystem.IntPtr pNative);
  86. public bool IsActive
  87. {
  88. get
  89. {
  90. if (_pNative == RootSystem.IntPtr.Zero)
  91. {
  92. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  93. }
  94. return Windows_Kinect_DepthFrameSource_get_IsActive(_pNative);
  95. }
  96. }
  97. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  98. private static extern RootSystem.IntPtr Windows_Kinect_DepthFrameSource_get_KinectSensor(RootSystem.IntPtr pNative);
  99. public Windows.Kinect.KinectSensor KinectSensor
  100. {
  101. get
  102. {
  103. if (_pNative == RootSystem.IntPtr.Zero)
  104. {
  105. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  106. }
  107. RootSystem.IntPtr objectPointer = Windows_Kinect_DepthFrameSource_get_KinectSensor(_pNative);
  108. Helper.ExceptionHelper.CheckLastError();
  109. if (objectPointer == RootSystem.IntPtr.Zero)
  110. {
  111. return null;
  112. }
  113. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.KinectSensor>(objectPointer, n => new Windows.Kinect.KinectSensor(n));
  114. }
  115. }
  116. // Events
  117. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle;
  118. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  119. private delegate void _Windows_Kinect_FrameCapturedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  120. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>> Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>>>();
  121. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Kinect_FrameCapturedEventArgs_Delegate))]
  122. private static void Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  123. {
  124. List<RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs>> callbackList = null;
  125. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  126. lock(callbackList)
  127. {
  128. var objThis = Helper.NativeObjectCache.GetObject<DepthFrameSource>(pNative);
  129. var args = new Windows.Kinect.FrameCapturedEventArgs(result);
  130. foreach(var func in callbackList)
  131. {
  132. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  133. }
  134. }
  135. }
  136. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  137. private static extern void Windows_Kinect_DepthFrameSource_add_FrameCaptured(RootSystem.IntPtr pNative, _Windows_Kinect_FrameCapturedEventArgs_Delegate eventCallback, bool unsubscribe);
  138. public event RootSystem.EventHandler<Windows.Kinect.FrameCapturedEventArgs> FrameCaptured
  139. {
  140. add
  141. {
  142. Helper.EventPump.EnsureInitialized();
  143. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  144. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  145. lock (callbackList)
  146. {
  147. callbackList.Add(value);
  148. if(callbackList.Count == 1)
  149. {
  150. var del = new _Windows_Kinect_FrameCapturedEventArgs_Delegate(Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler);
  151. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  152. Windows_Kinect_DepthFrameSource_add_FrameCaptured(_pNative, del, false);
  153. }
  154. }
  155. }
  156. remove
  157. {
  158. if (_pNative == RootSystem.IntPtr.Zero)
  159. {
  160. return;
  161. }
  162. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  163. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  164. lock (callbackList)
  165. {
  166. callbackList.Remove(value);
  167. if(callbackList.Count == 0)
  168. {
  169. Windows_Kinect_DepthFrameSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  170. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  171. }
  172. }
  173. }
  174. }
  175. private static RootSystem.Runtime.InteropServices.GCHandle _Windows_Data_PropertyChangedEventArgs_Delegate_Handle;
  176. [RootSystem.Runtime.InteropServices.UnmanagedFunctionPointer(RootSystem.Runtime.InteropServices.CallingConvention.Cdecl)]
  177. private delegate void _Windows_Data_PropertyChangedEventArgs_Delegate(RootSystem.IntPtr args, RootSystem.IntPtr pNative);
  178. private static Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>> Windows_Data_PropertyChangedEventArgs_Delegate_callbacks = new Helper.CollectionMap<RootSystem.IntPtr, List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>>>();
  179. [AOT.MonoPInvokeCallbackAttribute(typeof(_Windows_Data_PropertyChangedEventArgs_Delegate))]
  180. private static void Windows_Data_PropertyChangedEventArgs_Delegate_Handler(RootSystem.IntPtr result, RootSystem.IntPtr pNative)
  181. {
  182. List<RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs>> callbackList = null;
  183. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryGetValue(pNative, out callbackList);
  184. lock(callbackList)
  185. {
  186. var objThis = Helper.NativeObjectCache.GetObject<DepthFrameSource>(pNative);
  187. var args = new Windows.Data.PropertyChangedEventArgs(result);
  188. foreach(var func in callbackList)
  189. {
  190. Helper.EventPump.Instance.Enqueue(() => { try { func(objThis, args); } catch { } });
  191. }
  192. }
  193. }
  194. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  195. private static extern void Windows_Kinect_DepthFrameSource_add_PropertyChanged(RootSystem.IntPtr pNative, _Windows_Data_PropertyChangedEventArgs_Delegate eventCallback, bool unsubscribe);
  196. public event RootSystem.EventHandler<Windows.Data.PropertyChangedEventArgs> PropertyChanged
  197. {
  198. add
  199. {
  200. Helper.EventPump.EnsureInitialized();
  201. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  202. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  203. lock (callbackList)
  204. {
  205. callbackList.Add(value);
  206. if(callbackList.Count == 1)
  207. {
  208. var del = new _Windows_Data_PropertyChangedEventArgs_Delegate(Windows_Data_PropertyChangedEventArgs_Delegate_Handler);
  209. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle = RootSystem.Runtime.InteropServices.GCHandle.Alloc(del);
  210. Windows_Kinect_DepthFrameSource_add_PropertyChanged(_pNative, del, false);
  211. }
  212. }
  213. }
  214. remove
  215. {
  216. if (_pNative == RootSystem.IntPtr.Zero)
  217. {
  218. return;
  219. }
  220. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  221. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  222. lock (callbackList)
  223. {
  224. callbackList.Remove(value);
  225. if(callbackList.Count == 0)
  226. {
  227. Windows_Kinect_DepthFrameSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  228. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  229. }
  230. }
  231. }
  232. }
  233. // Public Methods
  234. [RootSystem.Runtime.InteropServices.DllImport("KinectUnityAddin", CallingConvention=RootSystem.Runtime.InteropServices.CallingConvention.Cdecl, SetLastError=true)]
  235. private static extern RootSystem.IntPtr Windows_Kinect_DepthFrameSource_OpenReader(RootSystem.IntPtr pNative);
  236. public Windows.Kinect.DepthFrameReader OpenReader()
  237. {
  238. if (_pNative == RootSystem.IntPtr.Zero)
  239. {
  240. throw new RootSystem.ObjectDisposedException("DepthFrameSource");
  241. }
  242. RootSystem.IntPtr objectPointer = Windows_Kinect_DepthFrameSource_OpenReader(_pNative);
  243. Helper.ExceptionHelper.CheckLastError();
  244. if (objectPointer == RootSystem.IntPtr.Zero)
  245. {
  246. return null;
  247. }
  248. return Helper.NativeObjectCache.CreateOrGetObject<Windows.Kinect.DepthFrameReader>(objectPointer, n => new Windows.Kinect.DepthFrameReader(n));
  249. }
  250. private void __EventCleanup()
  251. {
  252. {
  253. Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  254. var callbackList = Windows_Kinect_FrameCapturedEventArgs_Delegate_callbacks[_pNative];
  255. lock (callbackList)
  256. {
  257. if (callbackList.Count > 0)
  258. {
  259. callbackList.Clear();
  260. if (_pNative != RootSystem.IntPtr.Zero)
  261. {
  262. Windows_Kinect_DepthFrameSource_add_FrameCaptured(_pNative, Windows_Kinect_FrameCapturedEventArgs_Delegate_Handler, true);
  263. }
  264. _Windows_Kinect_FrameCapturedEventArgs_Delegate_Handle.Free();
  265. }
  266. }
  267. }
  268. {
  269. Windows_Data_PropertyChangedEventArgs_Delegate_callbacks.TryAddDefault(_pNative);
  270. var callbackList = Windows_Data_PropertyChangedEventArgs_Delegate_callbacks[_pNative];
  271. lock (callbackList)
  272. {
  273. if (callbackList.Count > 0)
  274. {
  275. callbackList.Clear();
  276. if (_pNative != RootSystem.IntPtr.Zero)
  277. {
  278. Windows_Kinect_DepthFrameSource_add_PropertyChanged(_pNative, Windows_Data_PropertyChangedEventArgs_Delegate_Handler, true);
  279. }
  280. _Windows_Data_PropertyChangedEventArgs_Delegate_Handle.Free();
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }