From e05f34377b217944e1ebd097d234373d369c1f54 Mon Sep 17 00:00:00 2001 From: Jarl Gullberg Date: Tue, 13 Jun 2017 22:00:41 +0200 Subject: [PATCH] Broke out window info initialization into their own classes. --- .../OSX/OSXWindowInfoInitializer.cs | 57 +++++++ .../Win/WinWindowsInfoInitializer.cs | 61 +++++++ src/OpenTK.GLWidget/X11/GLXAttribute.cs | 54 ++++++ src/OpenTK.GLWidget/X11/XVisualClass.cs | 39 +++++ src/OpenTK.GLWidget/X11/XVisualInfo.cs | 52 ++++++ src/OpenTK.GLWidget/X11/XVisualInfoMask.cs | 47 +++++ src/OpenTK.GLWidget/X11/XWindowInfoInitializer.cs | 189 +++++++++++++++++++++ 7 files changed, 499 insertions(+) create mode 100644 src/OpenTK.GLWidget/OSX/OSXWindowInfoInitializer.cs create mode 100644 src/OpenTK.GLWidget/Win/WinWindowsInfoInitializer.cs create mode 100644 src/OpenTK.GLWidget/X11/GLXAttribute.cs create mode 100644 src/OpenTK.GLWidget/X11/XVisualClass.cs create mode 100644 src/OpenTK.GLWidget/X11/XVisualInfo.cs create mode 100644 src/OpenTK.GLWidget/X11/XVisualInfoMask.cs create mode 100644 src/OpenTK.GLWidget/X11/XWindowInfoInitializer.cs diff --git a/src/OpenTK.GLWidget/OSX/OSXWindowInfoInitializer.cs b/src/OpenTK.GLWidget/OSX/OSXWindowInfoInitializer.cs new file mode 100644 index 0000000..fcf27bd --- /dev/null +++ b/src/OpenTK.GLWidget/OSX/OSXWindowInfoInitializer.cs @@ -0,0 +1,57 @@ +// +// OSXWindowsInfoInitializer.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Runtime.InteropServices; +using System.Security; +using OpenTK.Platform; + +namespace OpenTK.OSX +{ + public static class OSXWindowInfoInitializer + { +#if GTK3 + const string OSXLibGdkName = "libgdk-3.dylib"; +#else + const string OSXLibGdkName = "libgdk-quartz-2.0.0.dylib"; +#endif + + public static IWindowInfo Initialize(IntPtr gdkWindowHandle) + { + IntPtr windowHandle = gdk_quartz_window_get_nswindow(gdkWindowHandle); + IntPtr viewHandle = gdk_quartz_window_get_nsview(gdkWindowHandle); + + return Utilities.CreateMacOSWindowInfo(windowHandle, viewHandle); + } + + [SuppressUnmanagedCodeSecurity, DllImport(OSXLibGdkName)] + private static extern IntPtr gdk_quartz_window_get_nswindow(IntPtr handle); + + [SuppressUnmanagedCodeSecurity, DllImport(OSXLibGdkName)] + private static extern IntPtr gdk_quartz_window_get_nsview(IntPtr handle); + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/Win/WinWindowsInfoInitializer.cs b/src/OpenTK.GLWidget/Win/WinWindowsInfoInitializer.cs new file mode 100644 index 0000000..d1da1cb --- /dev/null +++ b/src/OpenTK.GLWidget/Win/WinWindowsInfoInitializer.cs @@ -0,0 +1,61 @@ +// +// WinWindowsInfoInitializer.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Runtime.InteropServices; +using System.Security; +using OpenTK.Platform; + +namespace OpenTK.Win +{ + public static class WinWindowsInfoInitializer + { +#if GTK3 + private const string WinLibGDKName = "libgdk-3-0.dll"; +#else + private const string WinLibGDKName = "libgdk-win32-2.0-0.dll"; +#endif + public static IWindowInfo Initialize(IntPtr gdkWindowHandle) + { +#if GTK3 + IntPtr windowHandle = gdk_win32_window_get_handle(gdkWindowHandle); +#else + IntPtr windowHandle = gdk_win32_drawable_get_handle(gdkWindowHandle); +#endif + return Utilities.CreateWindowsWindowInfo(windowHandle); + } + +#if GTK3 + [SuppressUnmanagedCodeSecurity, DllImport(WinLibGDKName, CallingConvention = CallingConvention.Cdecl)] + static extern IntPtr gdk_win32_window_get_handle(IntPtr w); +#else + [SuppressUnmanagedCodeSecurity, DllImport(WinLibGDKName, CallingConvention = CallingConvention.Cdecl)] + static extern IntPtr gdk_win32_drawable_get_handle(IntPtr d); +#endif + + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/X11/GLXAttribute.cs b/src/OpenTK.GLWidget/X11/GLXAttribute.cs new file mode 100644 index 0000000..30037c9 --- /dev/null +++ b/src/OpenTK.GLWidget/X11/GLXAttribute.cs @@ -0,0 +1,54 @@ +// +// GLXAttribute.cs +// +// The Open Toolkit Library License +// +// Author: +// Jarl Gullberg +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace OpenTK.X11 +{ + public enum GLXAttribute + { + None = 0, + UseGL = 1, + BufferSize = 2, + Level = 3, + RGBA = 4, + DoubleBuffer = 5, + Stereo = 6, + AuxBuffers = 7, + RedSize = 8, + GreenSize = 9, + BlueSize = 10, + AlphaSize = 11, + DepthSize = 12, + StencilSize = 13, + AccumRedSize = 14, + AccumGreenSize = 15, + AccumBlueSize = 16, + AccumAlphaSize = 17 + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/X11/XVisualClass.cs b/src/OpenTK.GLWidget/X11/XVisualClass.cs new file mode 100644 index 0000000..d22d973 --- /dev/null +++ b/src/OpenTK.GLWidget/X11/XVisualClass.cs @@ -0,0 +1,39 @@ +// +// XVisualClass.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +namespace OpenTK.X11 +{ + public enum XVisualClass + { + StaticGray = 0, + GrayScale = 1, + StaticColor = 2, + PseudoColor = 3, + TrueColor = 4, + DirectColor = 5 + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/X11/XVisualInfo.cs b/src/OpenTK.GLWidget/X11/XVisualInfo.cs new file mode 100644 index 0000000..ad7544a --- /dev/null +++ b/src/OpenTK.GLWidget/X11/XVisualInfo.cs @@ -0,0 +1,52 @@ +// +// XVisualInfo.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Runtime.InteropServices; + +namespace OpenTK.X11 +{ + [StructLayout(LayoutKind.Sequential)] + public struct XVisualInfo + { + public IntPtr Visual; + public IntPtr VisualID; + public int Screen; + public int Depth; + public XVisualClass Class; + public long RedMask; + public long GreenMask; + public long blueMask; + public int ColormapSize; + public int BitsPerRgb; + + public override string ToString() + { + return $"id ({this.VisualID}), screen ({this.Screen}), depth ({this.Depth}), class ({this.Class})"; + } + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/X11/XVisualInfoMask.cs b/src/OpenTK.GLWidget/X11/XVisualInfoMask.cs new file mode 100644 index 0000000..8056558 --- /dev/null +++ b/src/OpenTK.GLWidget/X11/XVisualInfoMask.cs @@ -0,0 +1,47 @@ +// +// XVisualInfoMask.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; + +namespace OpenTK.X11 +{ + [Flags] + public enum XVisualInfoMask + { + No = 0x0, + ID = 0x1, + Screen = 0x2, + Depth = 0x4, + Class = 0x8, + Red = 0x10, + Green = 0x20, + Blue = 0x40, + ColormapSize = 0x80, + BitsPerRGB = 0x100, + All = 0x1FF + } +} \ No newline at end of file diff --git a/src/OpenTK.GLWidget/X11/XWindowInfoInitializer.cs b/src/OpenTK.GLWidget/X11/XWindowInfoInitializer.cs new file mode 100644 index 0000000..2f2511a --- /dev/null +++ b/src/OpenTK.GLWidget/X11/XWindowInfoInitializer.cs @@ -0,0 +1,189 @@ +// +// XWindowsInfoInitializer.cs +// +// The Open Toolkit Library License +// +// Copyright (c) 2006 - 2009 the Open Toolkit library, except where noted. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights to +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +// the Software, and to permit persons to whom the Software is furnished to do +// so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Security; +using OpenTK.Graphics; +using OpenTK.Platform; + +namespace OpenTK.X11 +{ + public static class XWindowInfoInitializer + { + +#if GTK3 + const string UnixLibGdkName = "libgdk-3.so.0"; +#else + const string UnixLibGdkName = "libgdk-x11-2.0.so.0"; +#endif + private const string UnixLibX11Name = "libX11.so.6"; + private const string UnixLibGLName = "libGL.so.1"; + + public static IWindowInfo Initialize(GraphicsMode mode, IntPtr displayHandle, int screenNumber, IntPtr gdkWindowHandle, IntPtr gdkRootWindowHandle) + { + IntPtr display = gdk_x11_display_get_xdisplay(displayHandle); + +#if GTK3 + IntPtr windowXid = gdk_x11_window_get_xid(gdkWindowHandle); + IntPtr rootWindowXid = gdk_x11_window_get_xid(gdkRootWindowHandle); +#else + IntPtr windowHandle = gdk_x11_drawable_get_xid(gdkWindowHandle); + IntPtr rootWindow = gdk_x11_drawable_get_xid(gdkRootWindowHandle); +#endif + + IntPtr visualInfo; + if (mode.Index.HasValue) + { + XVisualInfo info = new XVisualInfo + { + VisualID = mode.Index.Value + }; + + int dummy; + visualInfo = XGetVisualInfo(display, XVisualInfoMask.ID, ref info, out dummy); + } + else + { + visualInfo = GetVisualInfo(mode, display, screenNumber); + } + + IWindowInfo retval = Utilities.CreateX11WindowInfo(display, screenNumber, windowXid, rootWindowXid, visualInfo); + XFree(visualInfo); + + return retval; + } + + private static IntPtr XGetVisualInfo(IntPtr display, XVisualInfoMask infoMask, ref XVisualInfo template, out int nitems) + { + return XGetVisualInfoInternal(display, (IntPtr)(int)infoMask, ref template, out nitems); + } + + private static IntPtr GetVisualInfo(GraphicsMode mode, IntPtr display, int screenNumber) + { + try + { + int[] attributes = CreateAttributeList(mode).ToArray(); + return glXChooseVisual(display, screenNumber, attributes); + } + catch (DllNotFoundException e) + { + throw new DllNotFoundException("OpenGL dll not found!", e); + } + catch (EntryPointNotFoundException enf) + { + throw new EntryPointNotFoundException("Glx entry point not found!", enf); + } + } + + private static List CreateAttributeList(GraphicsMode mode) + { + List attributeList = new List(24); + + attributeList.Add((int)GLXAttribute.RGBA); + + if (mode.Buffers > 1) + { + attributeList.Add((int)GLXAttribute.DoubleBuffer); + } + + if (mode.Stereo) + { + attributeList.Add((int)GLXAttribute.Stereo); + } + + attributeList.Add((int)GLXAttribute.RedSize); + attributeList.Add(mode.ColorFormat.Red / 4); // TODO support 16-bit + + attributeList.Add((int)GLXAttribute.GreenSize); + attributeList.Add(mode.ColorFormat.Green / 4); // TODO support 16-bit + + attributeList.Add((int)GLXAttribute.BlueSize); + attributeList.Add(mode.ColorFormat.Blue / 4); // TODO support 16-bit + + attributeList.Add((int)GLXAttribute.AlphaSize); + attributeList.Add(mode.ColorFormat.Alpha / 4); // TODO support 16-bit + + attributeList.Add((int)GLXAttribute.DepthSize); + attributeList.Add(mode.Depth); + + attributeList.Add((int)GLXAttribute.StencilSize); + attributeList.Add(mode.Stencil); + + //attributeList.Add(GLX_AUX_BUFFERS); + //attributeList.Add(Buffers); + + attributeList.Add((int)GLXAttribute.AccumRedSize); + attributeList.Add(mode.AccumulatorFormat.Red / 4);// TODO support 16-bit + + attributeList.Add((int)GLXAttribute.AccumGreenSize); + attributeList.Add(mode.AccumulatorFormat.Green / 4);// TODO support 16-bit + + attributeList.Add((int)GLXAttribute.AccumBlueSize); + attributeList.Add(mode.AccumulatorFormat.Blue / 4);// TODO support 16-bit + + attributeList.Add((int)GLXAttribute.AccumAlphaSize); + attributeList.Add(mode.AccumulatorFormat.Alpha / 4);// TODO support 16-bit + + attributeList.Add((int)GLXAttribute.None); + + return attributeList; + } + + [DllImport(UnixLibX11Name, EntryPoint = "XGetVisualInfo")] + private static extern IntPtr XGetVisualInfoInternal(IntPtr display, IntPtr infoMask, ref XVisualInfo template, out int nitems); + + [SuppressUnmanagedCodeSecurity, DllImport(UnixLibX11Name)] + private static extern void XFree(IntPtr handle); + +#if GTK3 + /// Returns the X resource (window or pixmap) belonging to a GdkWindow. + /// XID gdk_x11_window_get_xid(GdkWindow *drawable); + /// The GdkDrawable. + /// The ID of window's X resource. + [SuppressUnmanagedCodeSecurity, DllImport(UnixLibGdkName)] + private static extern IntPtr gdk_x11_window_get_xid(IntPtr gdkDisplay); +#else + /// Returns the X resource (window or pixmap) belonging to a GdkDrawable. + /// XID gdk_x11_drawable_get_xid(GdkDrawable *drawable); + /// The GdkDrawable. + /// The ID of drawable's X resource. + [SuppressUnmanagedCodeSecurity, DllImport(UnixLibGdkName)] + static extern IntPtr gdk_x11_drawable_get_xid(IntPtr gdkDisplay); +#endif + /// Returns the X display of a GdkDisplay. + /// Display* gdk_x11_display_get_xdisplay(GdkDisplay *display); + /// The GdkDrawable. + /// The X Display of the GdkDisplay. + [SuppressUnmanagedCodeSecurity, DllImport(UnixLibGdkName)] + private static extern IntPtr gdk_x11_display_get_xdisplay(IntPtr gdkDisplay); + + [SuppressUnmanagedCodeSecurity, DllImport(UnixLibGLName)] + private static extern IntPtr glXChooseVisual(IntPtr display, int screen, int[] attr); + } +} \ No newline at end of file -- 2.7.4