From 6ae06d82bddbecb12e1aabd64b9ca4205fe68c37 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Wed, 22 Aug 2007 00:30:16 +0000 Subject: [PATCH] Added IDisposable to all input drivers. Updated Cube.cs to use System.Drawing.Colors. Improved DefRawInputProc in windows API.cs --- Source/Examples/WinForms/Cube.cs | 12 ++-- Source/OpenTK/Input/IInputDriver.cs | 4 +- Source/OpenTK/InputDriver.cs | 32 ++++++++- Source/OpenTK/Platform/Windows/API.cs | 29 +++++--- .../OpenTK/Platform/Windows/WinGLContext.cs | 2 +- Source/OpenTK/Platform/Windows/WinGLNative.cs | 66 +++++++++---------- Source/OpenTK/Platform/Windows/WinRawInput.cs | 53 ++++++++++----- .../OpenTK/Platform/Windows/WinRawKeyboard.cs | 44 ++++++++++--- Source/OpenTK/Platform/X11/X11Input.cs | 9 +++ 9 files changed, 174 insertions(+), 77 deletions(-) diff --git a/Source/Examples/WinForms/Cube.cs b/Source/Examples/WinForms/Cube.cs index 458026ab..7094248b 100644 --- a/Source/Examples/WinForms/Cube.cs +++ b/Source/Examples/WinForms/Cube.cs @@ -167,38 +167,38 @@ namespace Examples.WinForms { GL.Begin(GL.Enums.BeginMode.QUADS); - GL.Color3((byte)Color.Silver.R, (byte)Color.Silver.G, (byte)Color.Silver.B); + GL.Color3(Color.Silver); GL.Vertex3(-1.0f, -1.0f, -1.0f); GL.Vertex3(-1.0f, 1.0f, -1.0f); GL.Vertex3(1.0f, 1.0f, -1.0f); GL.Vertex3(1.0f, -1.0f, -1.0f); - GL.Color3((byte)Color.Honeydew.R, (byte)Color.Honeydew.G, (byte)Color.Honeydew.B); + GL.Color3(Color.Honeydew); GL.Vertex3(-1.0f, -1.0f, -1.0f); GL.Vertex3(1.0f, -1.0f, -1.0f); GL.Vertex3(1.0f, -1.0f, 1.0f); GL.Vertex3(-1.0f, -1.0f, 1.0f); - GL.Color3((byte)Color.Moccasin.R, (byte)Color.Moccasin.G, (byte)Color.Moccasin.B); + GL.Color3(Color.Moccasin); GL.Vertex3(-1.0f, -1.0f, -1.0f); GL.Vertex3(-1.0f, -1.0f, 1.0f); GL.Vertex3(-1.0f, 1.0f, 1.0f); GL.Vertex3(-1.0f, 1.0f, -1.0f); - GL.Color3((byte)Color.IndianRed.R, (byte)Color.IndianRed.G, (byte)Color.IndianRed.B); + GL.Color3(Color.IndianRed); GL.Vertex3(-1.0f, -1.0f, 1.0f); GL.Vertex3(1.0f, -1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 1.0f); GL.Vertex3(-1.0f, 1.0f, 1.0f); - GL.Color3((byte)Color.PaleVioletRed.R, (byte)Color.PaleVioletRed.G, (byte)Color.PaleVioletRed.B); + GL.Color3(Color.PaleVioletRed); GL.Vertex3(-1.0f, 1.0f, -1.0f); GL.Vertex3(-1.0f, 1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, -1.0f); - GL.Color3((byte)Color.ForestGreen.R, (byte)Color.ForestGreen.G, (byte)Color.ForestGreen.B); + GL.Color3(Color.ForestGreen); GL.Vertex3(1.0f, -1.0f, -1.0f); GL.Vertex3(1.0f, 1.0f, -1.0f); GL.Vertex3(1.0f, 1.0f, 1.0f); diff --git a/Source/OpenTK/Input/IInputDriver.cs b/Source/OpenTK/Input/IInputDriver.cs index 9d1ceca3..df203e97 100644 --- a/Source/OpenTK/Input/IInputDriver.cs +++ b/Source/OpenTK/Input/IInputDriver.cs @@ -10,11 +10,9 @@ using System.Text; namespace OpenTK.Input { - public interface IInputDriver : IKeyboardDriver, IMouseDriver + public interface IInputDriver : IKeyboardDriver, IMouseDriver, IDisposable { IList InputDevices { get; } void ProcessEvents(); - //IEnumerable Mice { get; } - //IEnumerable Hids { get; } } } diff --git a/Source/OpenTK/InputDriver.cs b/Source/OpenTK/InputDriver.cs index 1ed3dc0b..f2dd9b9d 100644 --- a/Source/OpenTK/InputDriver.cs +++ b/Source/OpenTK/InputDriver.cs @@ -15,7 +15,7 @@ namespace OpenTK { public class InputDriver : IInputDriver { - IInputDriver inputDriver; + private IInputDriver inputDriver; #region --- Constructors --- @@ -63,5 +63,35 @@ namespace OpenTK } #endregion + + #region --- IDisposable Members --- + + private bool disposed; + + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool manual) + { + if (!disposed) + { + if (manual) + { + inputDriver.Dispose(); + } + + disposed = true; + } + } + + ~InputDriver() + { + this.Dispose(false); + } + + #endregion } } diff --git a/Source/OpenTK/Platform/Windows/API.cs b/Source/OpenTK/Platform/Windows/API.cs index 9dad08d4..9dbff65c 100644 --- a/Source/OpenTK/Platform/Windows/API.cs +++ b/Source/OpenTK/Platform/Windows/API.cs @@ -768,19 +768,28 @@ namespace OpenTK.Platform.Windows [CLSCompliant(false)] [System.Security.SuppressUnmanagedCodeSecurity] [DllImport("user32.dll", SetLastError = true)] - public static extern LRESULT DefRawInputProc( - RawInput[] RawInput, - INT Input, - UINT SizeHeader - ); + public static extern LRESULT DefRawInputProc(RawInput[] RawInput, INT Input, UINT SizeHeader); + /* [System.Security.SuppressUnmanagedCodeSecurity] [DllImport("user32.dll", SetLastError = true)] - public static extern LRESULT DefRawInputProc( - RawInput[] RawInput, - INT Input, - INT SizeHeader - ); + public static extern LRESULT DefRawInputProc(RawInput[] RawInput, INT Input, INT SizeHeader); + */ + + /// + /// calls the default raw input procedure to provide default processing for + /// any raw input messages that an application does not process. + /// This function ensures that every message is processed. + /// DefRawInputProc is called with the same parameters received by the window procedure. + /// + /// Pointer to an array of RawInput structures. + /// Number of RawInput structures pointed to by paRawInput. + /// Size, in bytes, of the RawInputHeader structure. + /// If successful, the function returns S_OK. Otherwise it returns an error value. + [CLSCompliant(false)] + [System.Security.SuppressUnmanagedCodeSecurity] + [DllImport("user32.dll", SetLastError = true)] + unsafe public static extern LRESULT DefRawInputProc(ref RawInput RawInput, INT Input, UINT SizeHeader); #endregion diff --git a/Source/OpenTK/Platform/Windows/WinGLContext.cs b/Source/OpenTK/Platform/Windows/WinGLContext.cs index b5e3ab73..68852321 100644 --- a/Source/OpenTK/Platform/Windows/WinGLContext.cs +++ b/Source/OpenTK/Platform/Windows/WinGLContext.cs @@ -281,7 +281,7 @@ namespace OpenTK.Platform.Windows Dispose(false); } - #region public void ReleaseResources() + #region private void ReleaseResources() private void ReleaseResources() { diff --git a/Source/OpenTK/Platform/Windows/WinGLNative.cs b/Source/OpenTK/Platform/Windows/WinGLNative.cs index 900904ed..8e08c862 100644 --- a/Source/OpenTK/Platform/Windows/WinGLNative.cs +++ b/Source/OpenTK/Platform/Windows/WinGLNative.cs @@ -333,9 +333,10 @@ namespace OpenTK.Platform.Windows { Debug.Print("Window handle {0} destroyed.", this.Handle); //this.DestroyHandle(); // Destroyed automatically by DefWndProc + //this.Dispose(); exists = false; } - API.PostQuitMessage(0); + //API.PostQuitMessage(0); } public event DestroyEvent Destroy; @@ -344,38 +345,6 @@ namespace OpenTK.Platform.Windows #endregion - #region --- IDisposable Members --- - - public void Dispose() - { - this.Dispose(true); - GC.SuppressFinalize(this); - - } - - private void Dispose(bool calledManually) - { - if (!disposed) - { - // Clean unmanaged resources here: - - if (calledManually) - { - // Safe to clean managed resources - glContext.Dispose(); - base.DestroyHandle(); - } - disposed = true; - } - } - - ~WinGLNative() - { - Dispose(false); - } - - #endregion - #region --- IResizable Members --- #region public int Width @@ -453,6 +422,37 @@ namespace OpenTK.Platform.Windows } #endregion + + #region --- IDisposable Members --- + + public void Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool calledManually) + { + if (!disposed) + { + // Clean unmanaged resources here: + + if (calledManually) + { + // Safe to clean managed resources + glContext.Dispose(); + base.DestroyHandle(); + } + disposed = true; + } + } + + ~WinGLNative() + { + Dispose(false); + } + + #endregion } #region class WindowHandle : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid diff --git a/Source/OpenTK/Platform/Windows/WinRawInput.cs b/Source/OpenTK/Platform/Windows/WinRawInput.cs index a48ab2ab..b49afc6f 100644 --- a/Source/OpenTK/Platform/Windows/WinRawInput.cs +++ b/Source/OpenTK/Platform/Windows/WinRawInput.cs @@ -89,16 +89,17 @@ namespace OpenTK.Platform.Windows switch (data.Header.Type) { case API.RawInputDeviceType.KEYBOARD: - keyboardDriver.ProcessKeyboardEvent(data); - break; + if (!keyboardDriver.ProcessKeyboardEvent(data)) + API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize); + return; case API.RawInputDeviceType.MOUSE: - //throw new NotImplementedException(); - break; + API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize); + return; case API.RawInputDeviceType.HID: - //throw new NotImplementedException(); - break; + API.DefRawInputProc(ref data, 1, (uint)API.RawInputHeaderSize); + return; } } else @@ -111,13 +112,14 @@ namespace OpenTK.Platform.Windows case API.Constants.WM_CLOSE: case API.Constants.WM_DESTROY: - Debug.Print("Input window detached from parent {0}.", Handle); - ReleaseHandle(); - break; + Debug.Print("Input window detached from parent {0}.", Handle); + ReleaseHandle(); + break; case API.Constants.WM_QUIT: - Debug.WriteLine("Input window quit."); - break; + Debug.WriteLine("Input window quit."); + this.Dispose(); + break; } base.WndProc(ref msg); @@ -137,7 +139,7 @@ namespace OpenTK.Platform.Windows get { return keyboardDriver.Keyboard; } } - public IList Mouse + public IList Mouse { get { throw new Exception("The method or operation is not implemented."); } } @@ -149,11 +151,32 @@ namespace OpenTK.Platform.Windows #endregion - #region IMouseDriver Members + #region --- IDisposable Members --- - IList IMouseDriver.Mouse + private bool disposed; + + public void Dispose() { - get { throw new Exception("The method or operation is not implemented."); } + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool manual) + { + if (!disposed) + { + if (manual) + { + keyboardDriver.Dispose(); + } + + disposed = true; + } + } + + ~WinRawInput() + { + Dispose(false); } #endregion diff --git a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs index ce723f51..f0a2a3e9 100644 --- a/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs +++ b/Source/OpenTK/Platform/Windows/WinRawKeyboard.cs @@ -18,7 +18,7 @@ using OpenTK.Input; namespace OpenTK.Platform.Windows { - internal class WinRawKeyboard : IKeyboardDriver + internal class WinRawKeyboard : IKeyboardDriver, IDisposable { private List keyboards = new List(); private IntPtr windowHandle; @@ -175,9 +175,7 @@ namespace OpenTK.Platform.Windows // This is a terminal services devices, skip it. continue; } - else if ( - ridl[i].Type == API.RawInputDeviceType.KEYBOARD || - ridl[i].Type == API.RawInputDeviceType.HID) + else if (ridl[i].Type == API.RawInputDeviceType.KEYBOARD || ridl[i].Type == API.RawInputDeviceType.HID) { //It's a keyboard – or a USB device that could be a keyboard @@ -283,15 +281,15 @@ namespace OpenTK.Platform.Windows { case API.VirtualKeys.SHIFT: keyboards[0][Input.Key.ShiftLeft] = keyboards[0][Input.Key.ShiftRight] = pressed; - return false; + return true; case API.VirtualKeys.CONTROL: keyboards[0][Input.Key.ControlLeft] = keyboards[0][Input.Key.ControlRight] = pressed; - return false; + return true; case API.VirtualKeys.MENU: keyboards[0][Input.Key.AltLeft] = keyboards[0][Input.Key.AltRight] = pressed; - return false; + return true; default: if (!WinRawKeyboard.KeyMap.ContainsKey(rin.Data.Keyboard.VKey)) @@ -302,7 +300,7 @@ namespace OpenTK.Platform.Windows { keyboards[0][WinRawKeyboard.KeyMap[rin.Data.Keyboard.VKey]] = pressed; } - break; + return false; } break; @@ -336,5 +334,35 @@ namespace OpenTK.Platform.Windows } #endregion + + #region --- IDisposable Members --- + + private bool disposed; + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool manual) + { + if (!disposed) + { + if (manual) + { + //keyboards.Clear(); + } + + disposed = true; + } + } + + ~WinRawKeyboard() + { + Dispose(false); + } + + #endregion } } diff --git a/Source/OpenTK/Platform/X11/X11Input.cs b/Source/OpenTK/Platform/X11/X11Input.cs index 036446a8..cd869bfe 100644 --- a/Source/OpenTK/Platform/X11/X11Input.cs +++ b/Source/OpenTK/Platform/X11/X11Input.cs @@ -146,5 +146,14 @@ namespace OpenTK.Platform.X11 } #endregion + + #region --- IDisposable Members --- + + public void Dispose() + { + throw new Exception("The method or operation is not implemented."); + } + + #endregion } }