Skip to content

Commit

Permalink
Delay VisualInfo construction (fixes #17)
Browse files Browse the repository at this point in the history
In OpenTK 1.1, GraphicsMode queries the platform for a mode id lazily.
By delaying VisualInfo selection until the GraphicsContext is constructed
we ensure that a concrete GraphicsMode is selected and ready for use.
  • Loading branch information
parallels authored and parallels committed Dec 14, 2013
1 parent 19d9beb commit 61f334f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
27 changes: 16 additions & 11 deletions Source/GLControl/X11GLControl.cs
Expand Up @@ -63,6 +63,10 @@ public override string ToString()
GraphicsMode mode;
IWindowInfo window_info;
IntPtr display;
IntPtr rootWindow;

// Use reflection to retrieve the necessary values from Mono's Windows.Forms implementation.
Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms");

#endregion

Expand All @@ -77,16 +81,24 @@ internal X11GLControl(GraphicsMode mode, Control control)

this.mode = mode;

// Use reflection to retrieve the necessary values from Mono's Windows.Forms implementation.
Type xplatui = Type.GetType("System.Windows.Forms.XplatUIX11, System.Windows.Forms");
if (xplatui == null) throw new PlatformNotSupportedException(
"System.Windows.Forms.XplatUIX11 missing. Unsupported platform or Mono runtime version, aborting.");

// get the required handles from the X11 API.
display = (IntPtr)GetStaticFieldValue(xplatui, "DisplayHandle");
IntPtr rootWindow = (IntPtr)GetStaticFieldValue(xplatui, "RootWindow");
rootWindow = (IntPtr)GetStaticFieldValue(xplatui, "RootWindow");
int screen = (int)GetStaticFieldValue(xplatui, "ScreenNo");

window_info = Utilities.CreateX11WindowInfo(display, screen, control.Handle, rootWindow, IntPtr.Zero);
}

#region IGLControl Members

public IGraphicsContext CreateContext(int major, int minor, GraphicsContextFlags flags)
{
GraphicsContext context = new GraphicsContext(mode, this.WindowInfo, major, minor, flags);
mode = context.GraphicsMode;

// get the XVisualInfo for this GraphicsMode
XVisualInfo info = new XVisualInfo();
info.VisualID = mode.Index.Value;
Expand All @@ -98,14 +110,7 @@ internal X11GLControl(GraphicsMode mode, Control control)
SetStaticFieldValue(xplatui, "CustomVisual", info.Visual);
SetStaticFieldValue(xplatui, "CustomColormap", XCreateColormap(display, rootWindow, info.Visual, 0));

window_info = Utilities.CreateX11WindowInfo(display, screen, control.Handle, rootWindow, infoPtr);
}

#region IGLControl Members

public IGraphicsContext CreateContext(int major, int minor, GraphicsContextFlags flags)
{
return new GraphicsContext(mode, this.WindowInfo, major, minor, flags);
return context;
}

public bool IsIdle
Expand Down
5 changes: 4 additions & 1 deletion Source/OpenTK/Platform/Utilities.cs
Expand Up @@ -212,7 +212,10 @@ public static IWindowInfo CreateX11WindowInfo(IntPtr display, int screen, IntPtr
window.Screen = screen;
window.Handle = windowHandle;
window.RootWindow = rootWindow;
window.VisualInfo = (X11.XVisualInfo)Marshal.PtrToStructure(visualInfo, typeof(X11.XVisualInfo));
if (visualInfo != IntPtr.Zero)
{
window.VisualInfo = (X11.XVisualInfo)Marshal.PtrToStructure(visualInfo, typeof(X11.XVisualInfo));
}

return window;
}
Expand Down

0 comments on commit 61f334f

Please sign in to comment.