diff --git a/Source/OpenTK/Graphics/DisplayDevice.cs b/Source/OpenTK/Graphics/DisplayDevice.cs index 272ff96e..44a01399 100644 --- a/Source/OpenTK/Graphics/DisplayDevice.cs +++ b/Source/OpenTK/Graphics/DisplayDevice.cs @@ -25,7 +25,7 @@ namespace OpenTK.Graphics // TODO: Does not detect changes to primary device. // TODO: Mono does not support System.Windows.Forms.Screen.BitsPerPixel -- find workaround! - DisplayResolution current_resolution; + DisplayResolution current_resolution, original_resolution; List available_resolutions = new List(); bool primary; @@ -183,18 +183,21 @@ namespace OpenTK.Graphics /// The new height of the DisplayDevice. /// The new bits per pixel of the DisplayDevice. /// The new refresh rate of the DisplayDevice. - /// Thrown if the requested resolution change failed. + /// Thrown if the requested resolution could not be set. public void ChangeResolution(DisplayResolution resolution) { if (resolution == null) throw new ArgumentNullException("resulotion", "Must be a valid resolution."); + if (resolution == current_resolution) + return; if (implementation.TryChangeResolution(this, resolution)) { + if (original_resolution == null) + original_resolution = current_resolution; current_resolution = resolution; } - else - throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}.", + else throw new GraphicsModeException(String.Format("Device {0}: Failed to change resolution to {1}.", this, resolution)); } @@ -202,9 +205,14 @@ namespace OpenTK.Graphics #region public void RestoreResolution() + /// Restores the original resolution of the DisplayDevice. + /// Thrown if the original resolution could not be restored. public void RestoreResolution() { - implementation.RestoreResolution(this); + if (original_resolution != null) + if (implementation.TryRestoreResolution(this)) + current_resolution = original_resolution; + else throw new GraphicsModeException(String.Format("Device {0}: Failed to restore resolution.", this)); } #endregion diff --git a/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs index 6b4eac1c..23b7b18c 100644 --- a/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs +++ b/Source/OpenTK/Graphics/IDisplayDeviceDriver.cs @@ -15,7 +15,7 @@ namespace OpenTK.Graphics internal interface IDisplayDeviceDriver { bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution); - void RestoreResolution(DisplayDevice device); + bool TryRestoreResolution(DisplayDevice device); //DisplayDevice[] AvailableDevices { get; } //DisplayResolution[] } diff --git a/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs b/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs index 5b0891e9..617eb34a 100644 --- a/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs +++ b/Source/OpenTK/Platform/Windows/WinDisplayDevice.cs @@ -95,30 +95,31 @@ namespace OpenTK.Platform.Windows public bool TryChangeResolution(OpenTK.Graphics.DisplayDevice device, DisplayResolution resolution) { - DeviceMode mode = new DeviceMode(); - mode.PelsWidth = resolution.Width; - mode.PelsHeight = resolution.Height; - mode.BitsPerPel = resolution.BitsPerPixel; - mode.DisplayFrequency = (int)resolution.RefreshRate; - mode.Fields = Constants.DM_BITSPERPEL - | Constants.DM_PELSWIDTH - | Constants.DM_PELSHEIGHT - | Constants.DM_DISPLAYFREQUENCY; + DeviceMode mode = null; + if (resolution != null) + { + mode = new DeviceMode(); + mode.PelsWidth = resolution.Width; + mode.PelsHeight = resolution.Height; + mode.BitsPerPel = resolution.BitsPerPixel; + mode.DisplayFrequency = (int)resolution.RefreshRate; + mode.Fields = Constants.DM_BITSPERPEL + | Constants.DM_PELSWIDTH + | Constants.DM_PELSHEIGHT + | Constants.DM_DISPLAYFREQUENCY; + } - //return Functions.ChangeDisplaySettings(settings, ChangeDisplaySettingsEnum.Fullscreen) == - // Constants.DISP_CHANGE_SUCCESSFUL; - return Functions.ChangeDisplaySettingsEx(available_device_names[device], mode, IntPtr.Zero, 0, IntPtr.Zero) == - Constants.DISP_CHANGE_SUCCESSFUL; + return Constants.DISP_CHANGE_SUCCESSFUL == + Functions.ChangeDisplaySettingsEx(available_device_names[device], mode, IntPtr.Zero, 0, IntPtr.Zero); } #endregion - #region public void RestoreResolution(OpenTK.Graphics.DisplayDevice device) + #region public TryRestoreResolution TryRestoreResolution(OpenTK.Graphics.DisplayDevice device) - public void RestoreResolution(OpenTK.Graphics.DisplayDevice device) + public bool TryRestoreResolution(OpenTK.Graphics.DisplayDevice device) { - //Functions.ChangeDisplaySettings(null, (ChangeDisplaySettingsEnum)0); - Functions.ChangeDisplaySettingsEx(available_device_names[device], null, IntPtr.Zero, 0, IntPtr.Zero); + return TryChangeResolution(device, null); } #endregion diff --git a/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs b/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs index fcbd2d72..e4680a22 100644 --- a/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs +++ b/Source/OpenTK/Platform/X11/X11XrandrDisplayDevice.cs @@ -165,9 +165,9 @@ namespace OpenTK.Platform.X11 current_rotation, (short)(resolution != null ? resolution.RefreshRate : 0), lastConfigUpdate[screen]); } - public void RestoreResolution(DisplayDevice device) + public bool TryRestoreResolution(DisplayDevice device) { - TryChangeResolution(device, null); + return TryChangeResolution(device, null); //System.Diagnostics.Process.Start("xrandr", "-s -0").WaitForExit(); // Hack, but works ;) }