PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Graphics/OpenTK/WindowInfoHelper.cs

#
C# | 246 lines | 124 code | 26 blank | 96 comment | 2 complexity | 735609d10cbfe2762f511e1bae191698 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.InteropServices;
  4. using OpenTK.Graphics;
  5. using OpenTK.Platform;
  6. using OpenTK.Platform.X11;
  7. using OpenTKUtilities = OpenTK.Platform.Utilities;
  8. namespace Delta.Graphics.OpenTK
  9. {
  10. /// <summary>
  11. /// Window info helper
  12. /// </summary>
  13. public class WindowInfoHelper
  14. {
  15. #region XVisualInfo Struct
  16. /// <summary>
  17. /// XVisual info
  18. /// </summary>
  19. [StructLayout(LayoutKind.Sequential)]
  20. internal struct XVisualInfo
  21. {
  22. #region Visual (Public)
  23. /// <summary>
  24. /// Visual
  25. /// </summary>
  26. public IntPtr Visual;
  27. #endregion
  28. #region VisualID (Public)
  29. /// <summary>
  30. /// Visual ID
  31. /// </summary>
  32. public IntPtr VisualID;
  33. #endregion
  34. #region Screen (Public)
  35. /// <summary>
  36. /// Screen
  37. /// </summary>
  38. public int Screen;
  39. #endregion
  40. #region Depth (Public)
  41. /// <summary>
  42. /// Depth
  43. /// </summary>
  44. public int Depth;
  45. #endregion
  46. #region Class (Public)
  47. /// <summary>
  48. /// Class
  49. /// </summary>
  50. public XVisualClass Class;
  51. #endregion
  52. #region RedMask (Public)
  53. /// <summary>
  54. /// Red mask
  55. /// </summary>
  56. public long RedMask;
  57. #endregion
  58. #region GreenMask (Public)
  59. /// <summary>
  60. /// Green mask
  61. /// </summary>
  62. public long GreenMask;
  63. #endregion
  64. #region blueMask (Public)
  65. /// <summary>
  66. /// Blue mask
  67. /// </summary>
  68. public long blueMask;
  69. #endregion
  70. #region ColormapSize (Public)
  71. /// <summary>
  72. /// Colormap size
  73. /// </summary>
  74. public int ColormapSize;
  75. #endregion
  76. #region BitsPerRgb (Public)
  77. /// <summary>
  78. /// Bits per RGB
  79. /// </summary>
  80. public int BitsPerRgb;
  81. #endregion
  82. #region ToString (Public)
  83. /// <summary>
  84. /// To string
  85. /// </summary>
  86. public override string ToString()
  87. {
  88. return "id (" + VisualID + "), " + "screen (" + Screen + "), " +
  89. "depth (" + Depth + "), " + "class (" + Class + ")";
  90. }
  91. #endregion
  92. }
  93. #endregion
  94. #region Create (Static)
  95. /// <summary>
  96. /// Create
  97. /// </summary>
  98. /// <param name="windowHandle">The window handle.</param>
  99. /// <returns></returns>
  100. public static IWindowInfo Create(IntPtr windowHandle)
  101. {
  102. return OpenTKUtilities.CreateWindowsWindowInfo(windowHandle);
  103. }
  104. #endregion
  105. #region CreateX11 (Static)
  106. /// <summary>
  107. /// Create X11
  108. /// </summary>
  109. /// <param name="mode">The mode.</param>
  110. /// <param name="windowHandle">The window handle.</param>
  111. /// <returns></returns>
  112. public static IWindowInfo CreateX11(GraphicsMode mode, IntPtr windowHandle)
  113. {
  114. Type type = Type.GetType("System.Windows.Forms.XplatUIX11, " +
  115. "System.Windows.Forms");
  116. if (type == null)
  117. {
  118. throw new PlatformNotSupportedException(
  119. "System.Windows.Forms.XplatUIX11 is missing. " +
  120. "Unsupported platform or Mono runtime version, aborting.");
  121. }
  122. return null;
  123. }
  124. #endregion
  125. #region Methods (Private)
  126. #region XOpenDisplay
  127. /// <summary>
  128. /// XOpenDisplay
  129. /// </summary>
  130. /// <param name="display"></param>
  131. /// <returns></returns>
  132. [DllImport("libX11", EntryPoint = "XOpenDisplay")]
  133. private static extern IntPtr XOpenDisplay(IntPtr display);
  134. #endregion
  135. #region XLockDisplay
  136. /// <summary>
  137. /// XLockDisplay
  138. /// </summary>
  139. /// <param name="display"></param>
  140. [DllImport("libX11")]
  141. private static extern void XLockDisplay(IntPtr display);
  142. #endregion
  143. #region XUnlockDisplay
  144. /// <summary>
  145. /// XUnlockDisplay
  146. /// </summary>
  147. /// <param name="display"></param>
  148. [DllImport("libX11")]
  149. private static extern void XUnlockDisplay(IntPtr display);
  150. #endregion
  151. #region XDefaultScreen
  152. /// <summary>
  153. /// XDefaultScreen
  154. /// </summary>
  155. /// <param name="display"></param>
  156. /// <returns></returns>
  157. [DllImport("libX11")]
  158. private static extern int XDefaultScreen(IntPtr display);
  159. #endregion
  160. #region XRootWindow
  161. /// <summary>
  162. /// XRoot window
  163. /// </summary>
  164. /// <param name="display"></param>
  165. /// <param name="screen_number"></param>
  166. /// <returns></returns>
  167. [DllImport("libX11")]
  168. private static extern IntPtr XRootWindow(IntPtr display,
  169. int screen_number);
  170. #endregion
  171. #region XGetVisualInfo
  172. /// <summary>
  173. /// XGet visual info
  174. /// </summary>
  175. /// <param name="display"></param>
  176. /// <param name="vinfoMask"></param>
  177. /// <param name="template"></param>
  178. /// <param name="nitems"></param>
  179. /// <returns></returns>
  180. [DllImport("libX11", EntryPoint = "XGetVisualInfo")]
  181. private static extern IntPtr XGetVisualInfo(IntPtr display,
  182. IntPtr vinfoMask, ref XVisualInfo template, out int nitems);
  183. #endregion
  184. #region XCreateColormap
  185. /// <summary>
  186. /// XCreate colormap
  187. /// </summary>
  188. /// <param name="display"></param>
  189. /// <param name="window"></param>
  190. /// <param name="visual"></param>
  191. /// <param name="alloc"></param>
  192. /// <returns></returns>
  193. [DllImport("libX11")]
  194. private static extern IntPtr XCreateColormap(IntPtr display, IntPtr window,
  195. IntPtr visual, int alloc);
  196. #endregion
  197. #region GetStaticFieldValue
  198. /// <summary>
  199. /// Get static field value
  200. /// </summary>
  201. private static object GetStaticFieldValue(Type type, string fieldName)
  202. {
  203. return type.GetField(fieldName, BindingFlags.NonPublic |
  204. BindingFlags.Static).GetValue(null);
  205. }
  206. #endregion
  207. #region SetStaticFieldValue
  208. /// <summary>
  209. /// Set static field value
  210. /// </summary>
  211. private static void SetStaticFieldValue(Type type, string fieldName,
  212. object value)
  213. {
  214. type.GetField(fieldName, BindingFlags.NonPublic |
  215. BindingFlags.Static).SetValue(null, value);
  216. }
  217. #endregion
  218. #endregion
  219. }
  220. }