PageRenderTime 114ms CodeModel.GetById 91ms app.highlight 7ms RepoModel.GetById 13ms app.codeStats 0ms

/Graphics/OpenTK/WindowInfoHelper.cs

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