DepthFrameReader.cs 15 KB

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