From c169a9707d7a86d3064645b3cf6c512ef5c9076e Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 4 Nov 2015 09:33:45 +1100 Subject: [PATCH] General cleanup of OSX in OpenTK, may break things. --- Launcher2/Gui/PlatformDrawer.cs | 19 + Launcher2/LauncherWindow.cs | 4 +- OpenTK/Debug.cs | 5 - OpenTK/Platform/MacOS/AglContext.cs | 67 +-- OpenTK/Platform/MacOS/Application.cs | 78 +-- .../MacOS/CarbonBindings/CarbonAPI.cs | 264 ++-------- .../QuartzDisplayServicesAPI.cs | 12 +- OpenTK/Platform/MacOS/CarbonGLNative.cs | 479 +++++------------- OpenTK/Platform/MacOS/CarbonWindowInfo.cs | 66 +-- OpenTK/Platform/MacOS/EventInfo.cs | 74 +-- .../MacOS/QuartzDisplayDeviceDriver.cs | 12 +- 11 files changed, 272 insertions(+), 808 deletions(-) diff --git a/Launcher2/Gui/PlatformDrawer.cs b/Launcher2/Gui/PlatformDrawer.cs index 4b825e65d..185e735da 100644 --- a/Launcher2/Gui/PlatformDrawer.cs +++ b/Launcher2/Gui/PlatformDrawer.cs @@ -34,6 +34,25 @@ namespace Launcher2 { } } + // TODO: broken + public sealed class OSXPlatformDrawer : PlatformDrawer { + + Graphics g; + public override void Init( IWindowInfo info ) { + g = Graphics.FromHwnd( info.WinHandle ); + } + + public override void Resize( IWindowInfo info ) { + if( g != null ) + g.Dispose(); + g = Graphics.FromHwnd( info.WinHandle ); + } + + public override void Draw( IWindowInfo info, Bitmap framebuffer ) { + g.DrawImage( framebuffer, 0, 0, framebuffer.Width, framebuffer.Height ); + } + } + public sealed class X11PlatformDrawer : PlatformDrawer { IntPtr gc; diff --git a/Launcher2/LauncherWindow.cs b/Launcher2/LauncherWindow.cs index 83cf67819..333f27cd7 100644 --- a/Launcher2/LauncherWindow.cs +++ b/Launcher2/LauncherWindow.cs @@ -58,8 +58,8 @@ namespace Launcher2 { platformDrawer = new WinPlatformDrawer(); else if( Configuration.RunningOnX11 ) platformDrawer = new X11PlatformDrawer(); - else - platformDrawer = new WinPlatformDrawer(); // TODO: mac osx support + else if( Configuration.RunningOnMacOS ) + platformDrawer = new OSXPlatformDrawer(); } void FocusedChanged( object sender, EventArgs e ) { diff --git a/OpenTK/Debug.cs b/OpenTK/Debug.cs index 8f8870c03..4cd16bcfa 100644 --- a/OpenTK/Debug.cs +++ b/OpenTK/Debug.cs @@ -5,11 +5,6 @@ namespace OpenTK { /// Placeholder for System.Diagnostics.Debug class because it crashes on some Mono version on Linux. public static class Debug { - public static void Assert( bool condition ) { - if( !condition ) - throw new InvalidOperationException( "Assertion failed!" ); - } - public static void Print( string text ) { Console.WriteLine( text ); } diff --git a/OpenTK/Platform/MacOS/AglContext.cs b/OpenTK/Platform/MacOS/AglContext.cs index d2c91f6d8..c63592431 100644 --- a/OpenTK/Platform/MacOS/AglContext.cs +++ b/OpenTK/Platform/MacOS/AglContext.cs @@ -106,32 +106,15 @@ namespace OpenTK.Platform.MacOS { Update(carbonWindow); MakeCurrent(carbonWindow); - Debug.Print("context: {0}", ContextHandle); } - private IntPtr GetQuartzDevice(CarbonWindowInfo carbonWindow) - { - IntPtr windowRef = carbonWindow.WindowRef; - - if (!CarbonGLNative.WindowRefMap.ContainsKey(windowRef)) - return IntPtr.Zero; - - WeakReference nativeRef = CarbonGLNative.WindowRefMap[windowRef]; - if (!nativeRef.IsAlive) - return IntPtr.Zero; - - CarbonGLNative window = nativeRef.Target as CarbonGLNative; - - if (window == null) - return IntPtr.Zero; - - return QuartzDisplayDeviceDriver.HandleTo(window.TargetDisplayDevice); - + private IntPtr GetQuartzDevice( CarbonWindowInfo carbonWindow ) { + CarbonGLNative nativeWindow = carbonWindow.nativeWindow; + return QuartzDisplayDeviceDriver.HandleTo( nativeWindow.TargetDisplayDevice ); } - void SetDrawable(CarbonWindowInfo carbonWindow) - { + void SetDrawable(CarbonWindowInfo carbonWindow) { IntPtr windowPort = API.GetWindowPort(carbonWindow.WindowRef); //Debug.Print("Setting drawable for context {0} to window port: {1}", Handle.Handle, windowPort); @@ -139,14 +122,12 @@ namespace OpenTK.Platform.MacOS { Agl.CheckReturnValue( code, "aglSetDrawable" ); } - public override void Update(IWindowInfo window) - { - CarbonWindowInfo carbonWindow = (CarbonWindowInfo)window; + public override void Update(IWindowInfo window) { + CarbonWindowInfo winInfo = (CarbonWindowInfo)window; - if (carbonWindow.goFullScreenHack) - { - carbonWindow.goFullScreenHack = false; - CarbonGLNative wind = GetCarbonWindow(carbonWindow); + if (winInfo.goFullScreenHack) { + winInfo.goFullScreenHack = false; + CarbonGLNative wind = winInfo.nativeWindow; if (wind != null) wind.SetFullscreen(this); @@ -154,11 +135,9 @@ namespace OpenTK.Platform.MacOS { Debug.Print("Could not find window!"); return; - } - else if (carbonWindow.goWindowedHack) - { - carbonWindow.goWindowedHack = false; - CarbonGLNative wind = GetCarbonWindow(carbonWindow); + } else if (winInfo.goWindowedHack) { + winInfo.goWindowedHack = false; + CarbonGLNative wind = winInfo.nativeWindow; if (wind != null) wind.UnsetFullscreen(this); @@ -170,22 +149,13 @@ namespace OpenTK.Platform.MacOS { if (mIsFullscreen) return; - SetDrawable(carbonWindow); - + SetDrawable(winInfo); Agl.aglUpdateContext(ContextHandle); } - private CarbonGLNative GetCarbonWindow(CarbonWindowInfo carbonWindow) - { - WeakReference r = CarbonGLNative.WindowRefMap[carbonWindow.WindowRef]; - return r.IsAlive ? (CarbonGLNative)r.Target : null; - } - bool firstFullScreen = false; - internal void SetFullScreen(CarbonWindowInfo info, out int width, out int height) - { - CarbonGLNative wind = GetCarbonWindow(info); - + internal void SetFullScreen(CarbonWindowInfo info, out int width, out int height) { + CarbonGLNative wind = info.nativeWindow; Debug.Print("Switching to full screen {0}x{1} on context {2}", wind.TargetDisplayDevice.Width, wind.TargetDisplayDevice.Height, ContextHandle); @@ -205,12 +175,10 @@ namespace OpenTK.Platform.MacOS { UnsetFullScreen(info); SetFullScreen(info, out width, out height); } - mIsFullscreen = true; } - internal void UnsetFullScreen(CarbonWindowInfo windowInfo) - { + internal void UnsetFullScreen(CarbonWindowInfo windowInfo) { Debug.Print("Unsetting AGL fullscreen."); byte code = Agl.aglSetDrawable(ContextHandle, IntPtr.Zero); Agl.CheckReturnValue( code, "aglSetDrawable" ); @@ -224,7 +192,6 @@ namespace OpenTK.Platform.MacOS { mIsFullscreen = false; } - #region IGraphicsContext Members public override void SwapBuffers() { @@ -238,7 +205,7 @@ namespace OpenTK.Platform.MacOS { } public override bool IsCurrent { - get { return ContextHandle == Agl.aglGetCurrentContext(); } + get { return ContextHandle == Agl.aglGetCurrentContext(); } } public override bool VSync { diff --git a/OpenTK/Platform/MacOS/Application.cs b/OpenTK/Platform/MacOS/Application.cs index 346d797ef..e8c85c294 100644 --- a/OpenTK/Platform/MacOS/Application.cs +++ b/OpenTK/Platform/MacOS/Application.cs @@ -12,41 +12,35 @@ using System.Collections.Generic; using System.IO; using System.Text; -namespace OpenTK.Platform.MacOS.Carbon -{ - static class Application - { +namespace OpenTK.Platform.MacOS.Carbon { + + static class Application { + static bool mInitialized = false; static IntPtr uppHandler; - static CarbonGLNative eventHandler; + public static CarbonGLNative WindowEventHandler; static int osMajor, osMinor, osBugfix; - static Application() - { + static Application() { Initialize(); } - internal static void Initialize() - { + internal static void Initialize() { if (mInitialized) return; API.AcquireRootMenu(); - ConnectEvents(); API.Gestalt(GestaltSelector.SystemVersionMajor, out osMajor); API.Gestalt(GestaltSelector.SystemVersionMinor, out osMinor); API.Gestalt(GestaltSelector.SystemVersionBugFix, out osBugfix); - Debug.Print("Running on Mac OS X {0}.{1}.{2}.", osMajor, osMinor, osBugfix); TransformProcessToForeground(); } - private static void TransformProcessToForeground() - { + private static void TransformProcessToForeground() { Carbon.ProcessSerialNumber psn = new ProcessSerialNumber(); - Debug.Print("Setting process to be foreground application."); API.GetCurrentProcess(ref psn); @@ -54,16 +48,8 @@ namespace OpenTK.Platform.MacOS.Carbon API.SetFrontProcess(ref psn); } - internal static CarbonGLNative WindowEventHandler - { - get { return eventHandler; } - set { eventHandler = value; } - } - - static void ConnectEvents() - { - EventTypeSpec[] eventTypes = new EventTypeSpec[] - { + static void ConnectEvents() { + EventTypeSpec[] eventTypes = new EventTypeSpec[] { new EventTypeSpec(EventClass.Application, AppEventKind.AppActivated), new EventTypeSpec(EventClass.Application, AppEventKind.AppDeactivated), new EventTypeSpec(EventClass.Application, AppEventKind.AppQuit), @@ -88,24 +74,13 @@ namespace OpenTK.Platform.MacOS.Carbon uppHandler = API.NewEventHandlerUPP(handler); API.InstallApplicationEventHandler( - uppHandler, eventTypes, IntPtr.Zero, IntPtr.Zero); - + uppHandler, eventTypes, IntPtr.Zero, IntPtr.Zero); mInitialized = true; } - static OSStatus EventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData) - { - EventInfo evt = new EventInfo(inEvent); - - switch (evt.EventClass) - { - case EventClass.Application: - switch (evt.AppEventKind) - { - default: - return OSStatus.EventNotHandled; - } - + static OSStatus EventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData) { + EventInfo evt = new EventInfo(inEvent); + switch (evt.EventClass) { case EventClass.AppleEvent: // only event here is the apple event. Debug.Print("Processing apple event."); @@ -115,35 +90,10 @@ namespace OpenTK.Platform.MacOS.Carbon case EventClass.Keyboard: case EventClass.Mouse: if (WindowEventHandler != null) - { return WindowEventHandler.DispatchEvent(inCaller, inEvent, evt, userData); - } break; } - return OSStatus.EventNotHandled; } - - public static void Run(CarbonGLNative window) - { - window.Closed += MainWindowClosed; - window.Visible = true; - - API.RunApplicationEventLoop(); - - window.Closed -= MainWindowClosed; - } - - static void MainWindowClosed(object sender, EventArgs e) - { - Debug.Print("Quitting application event loop."); - API.QuitApplicationEventLoop(); - } - - - internal static void ProcessEvents() - { - API.ProcessEvents(); - } } } diff --git a/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs b/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs index bd6edafb4..bb8c7b3fc 100644 --- a/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs +++ b/OpenTK/Platform/MacOS/CarbonBindings/CarbonAPI.cs @@ -354,8 +354,7 @@ namespace OpenTK.Platform.MacOS.Carbon StandardFloating = (CloseBox | CollapseBox) } - internal enum WindowPositionMethod : uint - { + internal enum WindowPositionMethod : uint { CenterOnMainScreen = 1, CenterOnParentWindow = 2, CenterOnParentWindowScreen = 3, @@ -370,8 +369,7 @@ namespace OpenTK.Platform.MacOS.Carbon internal delegate OSStatus MacOSEventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData); - internal enum WindowPartCode : short - { + internal enum WindowPartCode : short { inDesk = 0, inNoWindow = 0, inMenuBar = 1, @@ -402,13 +400,11 @@ namespace OpenTK.Platform.MacOS.Carbon #endregion #region --- Process Manager --- - enum ProcessApplicationTransformState : int - { + enum ProcessApplicationTransformState : int { kProcessTransformToForegroundApplication = 1, } - struct ProcessSerialNumber - { + struct ProcessSerialNumber { public ulong high; public ulong low; } @@ -437,23 +433,8 @@ namespace OpenTK.Platform.MacOS.Carbon #region --- Window Construction --- - [DllImport(carbon,EntryPoint="CreateNewWindow")] - private static extern OSStatus _CreateNewWindow(WindowClass @class, WindowAttributes attributes, ref Rect r, out IntPtr window); - - internal static IntPtr CreateNewWindow(WindowClass @class, WindowAttributes attributes, Rect r) - { - IntPtr retval; - OSStatus stat = _CreateNewWindow(@class, attributes, ref r, out retval); - - Debug.Print("Created Window: {0}", retval); - - if (stat != OSStatus.NoError) - { - throw new MacOSException(stat); - } - - return retval; - } + [DllImport(carbon)] + internal static extern OSStatus CreateNewWindow(WindowClass @class, WindowAttributes attributes, ref Rect r, out IntPtr window); [DllImport(carbon)] internal static extern void DisposeWindow(IntPtr window); @@ -482,15 +463,11 @@ namespace OpenTK.Platform.MacOS.Carbon [DllImport(carbon)] static extern OSStatus GetWindowBounds(IntPtr window, WindowRegionCode regionCode, out Rect globalBounds); - internal static Rect GetWindowBounds(IntPtr window, WindowRegionCode regionCode) - { - Rect retval; - OSStatus result = GetWindowBounds(window, regionCode, out retval); - - if (result != OSStatus.NoError) - throw new MacOSException(result); - - return retval; + internal static Rect GetWindowBounds(IntPtr window, WindowRegionCode regionCode) { + Rect rect; + OSStatus result = GetWindowBounds(window, regionCode, out rect); + CheckReturn(result); + return rect; } //[DllImport(carbon)] @@ -502,11 +479,9 @@ namespace OpenTK.Platform.MacOS.Carbon [DllImport(carbon)] static extern IntPtr GetEventDispatcherTarget(); - [DllImport(carbon,EntryPoint="ReceiveNextEvent")] - static extern OSStatus ReceiveNextEvent(uint inNumTypes, - IntPtr inList, - double inTimeout, - bool inPullEvent, + [DllImport(carbon)] + static extern OSStatus ReceiveNextEvent(uint inNumTypes, IntPtr inList, + double inTimeout, bool inPullEvent, out IntPtr outEvent); [DllImport(carbon)] @@ -515,56 +490,37 @@ namespace OpenTK.Platform.MacOS.Carbon [DllImport(carbon)] static extern void ReleaseEvent(IntPtr theEvent); - internal static void SendEvent(IntPtr theEvent) - { + internal static void SendEvent(IntPtr theEvent) { IntPtr theTarget = GetEventDispatcherTarget(); SendEventToEventTarget(theEvent, theTarget); } // Processes events in the queue and then returns. - internal static void ProcessEvents() - { + internal static void ProcessEvents() { IntPtr theEvent; IntPtr theTarget = GetEventDispatcherTarget(); - for (;;) - { + for (;;) { OSStatus status = ReceiveNextEvent(0, IntPtr.Zero, 0.0, true, out theEvent); if (status == OSStatus.EventLoopTimedOut) break; - if (status != OSStatus.NoError) - { + if (status != OSStatus.NoError) { Debug.Print("Message Loop status: {0}", status); break; } if (theEvent == IntPtr.Zero) break; - try - { - SendEventToEventTarget(theEvent, theTarget); - } - catch (System.ExecutionEngineException e) - { - Console.Error.WriteLine("ExecutionEngineException caught."); - Console.Error.WriteLine("theEvent: " + new EventInfo(theEvent).ToString()); - Console.Error.WriteLine(e.Message); - Console.Error.WriteLine(e.StackTrace); - } - + SendEventToEventTarget(theEvent, theTarget); ReleaseEvent(theEvent); } } - - #region --- Processing apple event --- - + [StructLayout(LayoutKind.Sequential)] - - struct EventRecord - { + struct EventRecord { public ushort what; public uint message; public uint when; @@ -578,33 +534,15 @@ namespace OpenTK.Platform.MacOS.Carbon [DllImport(carbon)] static extern OSStatus AEProcessAppleEvent(ref EventRecord theEventRecord); - static internal void ProcessAppleEvent(IntPtr inEvent) - { + internal static void ProcessAppleEvent(IntPtr inEvent) { EventRecord record; - ConvertEventRefToEventRecord(inEvent, out record); AEProcessAppleEvent(ref record); } - - #endregion - - #endregion - #region --- Getting Event Parameters --- - - [DllImport(carbon)] - static extern OSStatus CreateEvent( IntPtr inAllocator, - EventClass inClassID, UInt32 kind, EventTime when, - EventAttributes flags, out IntPtr outEvent); - internal static IntPtr CreateWindowEvent(WindowEventKind kind) { - IntPtr retval; - OSStatus stat = CreateEvent(IntPtr.Zero, EventClass.Window, (uint)kind, - 0, EventAttributes.kEventAttributeNone, out retval); + #endregion - if (stat != OSStatus.NoError) - throw new MacOSException(stat); - return retval; - } + #region --- Event handlers --- [DllImport(carbon)] static extern OSStatus GetEventParameter( @@ -616,9 +554,7 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.KeyCode, EventParamType.typeUInt32, IntPtr.Zero, sizeof(uint), IntPtr.Zero, (IntPtr)(void*)&code); - - if (result != OSStatus.NoError) - throw new MacOSException(result); + CheckReturn(result); return (MacOSKeyCode)code; } @@ -627,9 +563,7 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.KeyMacCharCode, EventParamType.typeChar, IntPtr.Zero, Marshal.SizeOf(typeof(char)), IntPtr.Zero, (IntPtr)(void*)&code); - - if (result != OSStatus.NoError) - throw new MacOSException(result); + CheckReturn(result); return code; } @@ -660,7 +594,7 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.WindowMouseLocation, EventParamType.typeHIPoint, IntPtr.Zero, Marshal.SizeOf(typeof(HIPoint)), IntPtr.Zero, (IntPtr)(void*)&point); - + CheckReturn(result); pt = point; return result; } @@ -670,7 +604,7 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.WindowRef, EventParamType.typeWindowRef, IntPtr.Zero, sizeof(IntPtr), IntPtr.Zero, (IntPtr)(void*)&retval); - + CheckReturn(result); windowRef = retval; return result; } @@ -680,7 +614,7 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.MouseLocation, EventParamType.typeHIPoint, IntPtr.Zero, Marshal.SizeOf(typeof(HIPoint)), IntPtr.Zero, (IntPtr)(void*)&point); - + CheckReturn(result); pt = point; return result; } @@ -690,74 +624,40 @@ namespace OpenTK.Platform.MacOS.Carbon OSStatus result = API.GetEventParameter(inEvent, EventParamName.KeyModifiers, EventParamType.typeUInt32, IntPtr.Zero, sizeof(uint), IntPtr.Zero, (IntPtr)(void*)&code); - - if (result != OSStatus.NoError) - throw new MacOSException(result); + CheckReturn(result); return (MacOSKeyModifiers)code; } - #endregion - #region --- Event Handlers --- - - [DllImport(carbon,EntryPoint="InstallEventHandler")] - static extern OSStatus _InstallEventHandler( + [DllImport(carbon)] + static extern OSStatus InstallEventHandler( IntPtr eventTargetRef, IntPtr handlerProc, int numtypes, EventTypeSpec[] typeList, IntPtr userData, IntPtr handlerRef); - internal static void InstallWindowEventHandler(IntPtr windowRef, IntPtr uppHandlerProc, - EventTypeSpec[] eventTypes, IntPtr userData, IntPtr handlerRef) - { + internal static void InstallWindowEventHandler(IntPtr windowRef, IntPtr uppHandlerProc, EventTypeSpec[] eventTypes, + IntPtr userData, IntPtr handlerRef) { IntPtr windowTarget = GetWindowEventTarget(windowRef); - - //Debug.Print("Window: {0}", windowRef); - //Debug.Print("Window Target: {0}", windowTarget); - //Debug.Print("Handler: {0}", uppHandlerProc); - //Debug.Print("Num Events: {0}", eventTypes.Length); - //Debug.Print("User Data: {0}", userData); - //Debug.Print("Handler Ref: {0}", handlerRef); - - OSStatus error = _InstallEventHandler(windowTarget, uppHandlerProc, - eventTypes.Length, eventTypes, - userData, handlerRef); - - //Debug.Print("Status: {0}", error); - - if (error != OSStatus.NoError) - { - throw new MacOSException(error); - } + OSStatus error = InstallEventHandler(windowTarget, uppHandlerProc, eventTypes.Length, + eventTypes, userData, handlerRef); + CheckReturn( error ); } - internal static void InstallApplicationEventHandler(IntPtr uppHandlerProc, - EventTypeSpec[] eventTypes, IntPtr userData, IntPtr handlerRef) - { - - OSStatus error = _InstallEventHandler(GetApplicationEventTarget(), uppHandlerProc, + internal static void InstallApplicationEventHandler(IntPtr uppHandlerProc, EventTypeSpec[] eventTypes, + IntPtr userData, IntPtr handlerRef) { + OSStatus error = InstallEventHandler(GetApplicationEventTarget(), uppHandlerProc, eventTypes.Length, eventTypes, userData, handlerRef); - - if (error != OSStatus.NoError) - { - throw new MacOSException(error); - } - + CheckReturn( error ); } [DllImport(carbon)] internal static extern OSStatus RemoveEventHandler(IntPtr inHandlerRef); - #endregion - #region --- GetWindowEventTarget --- - [DllImport(carbon)] internal static extern IntPtr GetWindowEventTarget(IntPtr window); [DllImport(carbon)] internal static extern IntPtr GetApplicationEventTarget(); - - #endregion - #region --- UPP Event Handlers --- [DllImport(carbon)] internal static extern IntPtr NewEventHandlerUPP(MacOSEventHandler handler); @@ -791,26 +691,14 @@ namespace OpenTK.Platform.MacOS.Carbon #endregion - [DllImport(carbon)] - internal static extern OSStatus ActivateWindow (IntPtr inWindow, bool inActivate); - - [DllImport(carbon)] - internal static extern void RunApplicationEventLoop(); - - [DllImport(carbon)] - internal static extern void QuitApplicationEventLoop(); - #region --- SetWindowTitle --- [DllImport(carbon)] static extern void SetWindowTitleWithCFString(IntPtr windowRef, IntPtr title); - internal static void SetWindowTitle(IntPtr windowRef, string title) - { + internal static void SetWindowTitle(IntPtr windowRef, string title) { IntPtr str = __CFStringMakeConstantString(title); - Debug.Print("Setting window title: {0}, CFstring : {1}, Text : {2}", windowRef, str, title); - SetWindowTitleWithCFString(windowRef, str); // Apparently releasing this reference to the CFConstantString here @@ -822,18 +710,6 @@ namespace OpenTK.Platform.MacOS.Carbon #endregion - [DllImport(carbon,EntryPoint="ChangeWindowAttributes")] - static extern OSStatus _ChangeWindowAttributes(IntPtr windowRef, WindowAttributes setTheseAttributes, WindowAttributes clearTheseAttributes); - internal static void ChangeWindowAttributes(IntPtr windowRef, WindowAttributes setTheseAttributes, WindowAttributes clearTheseAttributes) - { - OSStatus error = _ChangeWindowAttributes(windowRef, setTheseAttributes, clearTheseAttributes); - - if (error != OSStatus.NoError) - { - throw new MacOSException(error); - } - } - [DllImport(carbon)] static extern IntPtr __CFStringMakeConstantString(string cStr); @@ -845,59 +721,29 @@ namespace OpenTK.Platform.MacOS.Carbon [DllImport(carbon)] internal static extern IntPtr GetWindowPort(IntPtr windowRef); - - #region --- Menus --- [DllImport(carbon)] internal static extern IntPtr AcquireRootMenu(); - - #endregion - [DllImport(carbon)] internal static extern bool IsWindowCollapsed(IntPtr windowRef); - [DllImport(carbon, EntryPoint = "CollapseWindow")] - static extern OSStatus _CollapseWindow(IntPtr windowRef, bool collapse); - - internal static void CollapseWindow(IntPtr windowRef, bool collapse) - { - OSStatus error = _CollapseWindow(windowRef, collapse); - - if (error != OSStatus.NoError) - { - throw new MacOSException(error); - } - } + [DllImport(carbon)] + internal static extern OSStatus CollapseWindow(IntPtr windowRef, bool collapse); [DllImport(carbon, EntryPoint="IsWindowInStandardState")] static extern bool _IsWindowInStandardState(IntPtr windowRef, IntPtr inIdealSize, IntPtr outIdealStandardState); - internal static bool IsWindowInStandardState(IntPtr windowRef) - { + internal static bool IsWindowInStandardState(IntPtr windowRef) { return _IsWindowInStandardState(windowRef, IntPtr.Zero, IntPtr.Zero); } - [DllImport(carbon, EntryPoint = "ZoomWindowIdeal")] - unsafe static extern OSStatus _ZoomWindowIdeal(IntPtr windowRef, short inPartCode, IntPtr toIdealSize); - - internal static void ZoomWindowIdeal(IntPtr windowRef, WindowPartCode inPartCode, ref CarbonPoint toIdealSize) - { - CarbonPoint pt = toIdealSize; - OSStatus error ; - IntPtr handle = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CarbonPoint))); - Marshal.StructureToPtr(toIdealSize, handle, false); - - error = _ZoomWindowIdeal(windowRef, (short)inPartCode, handle); - - toIdealSize = (CarbonPoint)Marshal.PtrToStructure(handle,typeof(CarbonPoint)); - - Marshal.FreeHGlobal(handle); - - if (error != OSStatus.NoError) - { - throw new MacOSException(error); - } + [DllImport(carbon)] + internal unsafe static extern OSStatus ZoomWindowIdeal(IntPtr windowRef, short inPartCode, ref CarbonPoint toIdealSize); + + internal static void CheckReturn( OSStatus status ) { + if( status != OSStatus.NoError ) + throw new MacOSException( status ); } [DllImport(carbon)] @@ -955,14 +801,8 @@ namespace OpenTK.Platform.MacOS.Carbon #endregion const string gestaltlib = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon"; - - [DllImport(gestaltlib)] internal static extern OSStatus Gestalt(GestaltSelector selector, out int response); } - #endregion - -} - - +} \ No newline at end of file diff --git a/OpenTK/Platform/MacOS/CarbonBindings/QuartzDisplayServicesAPI.cs b/OpenTK/Platform/MacOS/CarbonBindings/QuartzDisplayServicesAPI.cs index 52f064550..eb9bf5123 100644 --- a/OpenTK/Platform/MacOS/CarbonBindings/QuartzDisplayServicesAPI.cs +++ b/OpenTK/Platform/MacOS/CarbonBindings/QuartzDisplayServicesAPI.cs @@ -1,18 +1,14 @@ using System; using System.Runtime.InteropServices; - -namespace OpenTK.Platform.MacOS.Carbon -{ +namespace OpenTK.Platform.MacOS.Carbon { + // Quartz Display services used here are available in MacOS X 10.3 and later. - - enum CGDisplayErr - { + enum CGDisplayErr { } - internal static class CG - { + internal static class CG { const string appServices = "/System/Library/Frameworks/ApplicationServices.framework/Versions/Current/ApplicationServices"; // CGPoint -> HIPoint diff --git a/OpenTK/Platform/MacOS/CarbonGLNative.cs b/OpenTK/Platform/MacOS/CarbonGLNative.cs index ea561f10c..9627b7a76 100644 --- a/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -26,19 +26,15 @@ #endregion using System; -using System.Collections.Generic; using System.ComponentModel; using System.Drawing; -using OpenTK.Graphics; -using OpenTK.Platform.MacOS.Carbon; using OpenTK.Input; +using OpenTK.Platform.MacOS.Carbon; -namespace OpenTK.Platform.MacOS -{ - class CarbonGLNative : INativeWindow - { - #region Fields - +namespace OpenTK.Platform.MacOS { + + class CarbonGLNative : INativeWindow { + CarbonWindowInfo window; static MacOSKeyMap Keymap = new MacOSKeyMap(); IntPtr uppHandler; @@ -46,150 +42,88 @@ namespace OpenTK.Platform.MacOS string title = "OpenTK Window"; Rectangle bounds, clientRectangle; Rectangle windowedBounds; - bool mIsDisposed = false; - bool mExists = true; - DisplayDevice mDisplayDevice; + bool isDisposed = false; + bool exists = true; + DisplayDevice displayDevice; - WindowAttributes mWindowAttrib; - WindowClass mWindowClass; - WindowPositionMethod mPositionMethod = WindowPositionMethod.CenterOnMainScreen; - int mTitlebarHeight; + WindowPositionMethod positionMethod = WindowPositionMethod.CenterOnMainScreen; + int titlebarHeight; private WindowState windowState = WindowState.Normal; - static Dictionary mWindows = new Dictionary(); + KeyPressEventArgs keyPressArgs = new KeyPressEventArgs(); + bool mouseIn = false, isActive = false; + Icon icon; + internal DisplayDevice TargetDisplayDevice { get { return displayDevice; } } - KeyPressEventArgs mKeyPressArgs = new KeyPressEventArgs(); - - bool mMouseIn = false; - bool mIsActive = false; - - Icon mIcon; - - #endregion - - #region AGL Device Hack - - static internal Dictionary WindowRefMap { get { return mWindows; } } - internal DisplayDevice TargetDisplayDevice { get { return mDisplayDevice; } } - - #endregion - - #region Constructors - - static CarbonGLNative() - { + static CarbonGLNative() { Application.Initialize(); } - CarbonGLNative() - : this(WindowClass.Document, - WindowAttributes.StandardDocument | - WindowAttributes.StandardHandler | - WindowAttributes.InWindowMenu | - WindowAttributes.LiveResize) - { } - - - CarbonGLNative(WindowClass @class, WindowAttributes attrib) - { - mWindowClass = @class; - mWindowAttrib = attrib; - } - - public CarbonGLNative(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device) - { + public CarbonGLNative(int x, int y, int width, int height, string title, + GameWindowFlags options, DisplayDevice device) { + this.title = title; CreateNativeWindow(WindowClass.Document, WindowAttributes.StandardDocument | WindowAttributes.StandardHandler | WindowAttributes.InWindowMenu | WindowAttributes.LiveResize, new Rect((short)x, (short)y, (short)width, (short)height)); - - mDisplayDevice = device; + displayDevice = device; } - #endregion - - #region IDisposable - - public void Dispose() - { + public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } - protected virtual void Dispose(bool disposing) - { - if (mIsDisposed) + protected virtual void Dispose(bool disposing) { + if (isDisposed) return; Debug.Print("Disposing of CarbonGLNative window."); - API.DisposeWindow(window.WindowRef); + isDisposed = true; + exists = false; - mIsDisposed = true; - mExists = false; - - if (disposing) - { - mWindows.Remove(window.WindowRef); - + if (disposing) { window.Dispose(); window = null; } - DisposeUPP(); - } - ~CarbonGLNative() - { + ~CarbonGLNative() { Dispose(false); } - #endregion - - #region Private Members - - void DisposeUPP() - { - if (uppHandler != IntPtr.Zero) - { - //API.RemoveEventHandler(uppHandler); - //API.DisposeEventHandlerUPP(uppHandler); + void DisposeUPP() { + if (uppHandler != IntPtr.Zero) { + API.RemoveEventHandler(uppHandler); + API.DisposeEventHandlerUPP(uppHandler); } - uppHandler = IntPtr.Zero; } - void CreateNativeWindow(WindowClass @class, WindowAttributes attrib, Rect r) - { + void CreateNativeWindow(WindowClass @class, WindowAttributes attrib, Rect r) { Debug.Print("Creating window..."); - - IntPtr windowRef = API.CreateNewWindow(@class, attrib, r); + IntPtr windowRef; + OSStatus err = API.CreateNewWindow(@class, attrib, ref r, out windowRef); + Debug.Print("Created Window: {0}", windowRef); + API.CheckReturn(err); + API.SetWindowTitle(windowRef, title); - - window = new CarbonWindowInfo(windowRef, true); - + window = new CarbonWindowInfo(windowRef, this, true); SetLocation(r.X, r.Y); SetSize(r.Width, r.Height); - Debug.Print("Created window."); - - mWindows.Add(windowRef, new WeakReference(this)); - LoadSize(); - Rect titleSize = API.GetWindowBounds(window.WindowRef, WindowRegionCode.TitleBarRegion); - mTitlebarHeight = titleSize.Height; + titlebarHeight = titleSize.Height; Debug.Print("Titlebar size: {0}", titleSize); - ConnectEvents(); - Debug.Print("Attached window events."); } - void ConnectEvents() - { + void ConnectEvents() { EventTypeSpec[] eventTypes = new EventTypeSpec[] { new EventTypeSpec(EventClass.Window, WindowEventKind.WindowClose), @@ -214,9 +148,7 @@ namespace OpenTK.Platform.MacOS MacOSEventHandler handler = EventHandler; uppHandler = API.NewEventHandlerUPP(handler); - API.InstallWindowEventHandler(window.WindowRef, uppHandler, eventTypes, window.WindowRef, IntPtr.Zero); - Application.WindowEventHandler = this; } @@ -226,7 +158,7 @@ namespace OpenTK.Platform.MacOS void Show() { API.ShowWindow(window.WindowRef); - API.RepositionWindow(window.WindowRef, IntPtr.Zero, WindowPositionMethod); + API.RepositionWindow(window.WindowRef, IntPtr.Zero, positionMethod); API.SelectWindow(window.WindowRef); } @@ -244,7 +176,7 @@ namespace OpenTK.Platform.MacOS Debug.Print("New Size: {0}, {1}", Width, Height); // TODO: if we go full screen we need to make this use the device specified. - bounds = mDisplayDevice.Bounds; + bounds = displayDevice.Bounds; windowState = WindowState.Fullscreen; } @@ -253,106 +185,53 @@ namespace OpenTK.Platform.MacOS Debug.Print("Telling Carbon to reset window state to " + windowState.ToString()); SetCarbonWindowState(); - SetSize((short)windowedBounds.Width, (short)windowedBounds.Height); } - bool IsDisposed - { - get { return mIsDisposed; } - } - - WindowPositionMethod WindowPositionMethod - { - get { return mPositionMethod; } - set { mPositionMethod = value; } - } - - internal OSStatus DispatchEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) - { - switch (evt.EventClass) - { + internal OSStatus DispatchEvent( IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData ) { + switch (evt.EventClass) { case EventClass.Window: return ProcessWindowEvent(inCaller, inEvent, evt, userData); - case EventClass.Mouse: return ProcessMouseEvent(inCaller, inEvent, evt, userData); - case EventClass.Keyboard: return ProcessKeyboardEvent(inCaller, inEvent, evt, userData); - default: return OSStatus.EventNotHandled; } } - protected static OSStatus EventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData) - { + protected OSStatus EventHandler( IntPtr inCaller, IntPtr inEvent, IntPtr userData ) { // bail out if the window passed in is not actually our window. - // I think this happens if using winforms with a GameWindow sometimes. - if (!mWindows.ContainsKey(userData)) + if( userData != window.WindowRef) return OSStatus.EventNotHandled; - - WeakReference reference = mWindows[userData]; - - // bail out if the CarbonGLNative window has been garbage collected. - if (!reference.IsAlive) { - mWindows.Remove(userData); - return OSStatus.EventNotHandled; - } - - CarbonGLNative window = (CarbonGLNative)reference.Target; + EventInfo evt = new EventInfo(inEvent); - - //Debug.Print("Processing {0} event for {1}.", evt, window.window); - - if (window == null) { - Debug.Print("Window for event not found."); - return OSStatus.EventNotHandled; - } - - switch (evt.EventClass) - { - case EventClass.Window: - return window.ProcessWindowEvent(inCaller, inEvent, evt, userData); - - case EventClass.Mouse: - return window.ProcessMouseEvent(inCaller, inEvent, evt, userData); - - case EventClass.Keyboard: - return window.ProcessKeyboardEvent(inCaller, inEvent, evt, userData); - - default: - return OSStatus.EventNotHandled; - } + return DispatchEvent( inCaller, inEvent, evt, userData ); } - private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) - { - Debug.Assert(evt.EventClass == EventClass.Keyboard); + private OSStatus ProcessKeyboardEvent( IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData ) { MacOSKeyCode code = (MacOSKeyCode)0; char charCode = '\0'; //Debug.Print("Processing keyboard event {0}", evt.KeyboardEventKind); - switch (evt.KeyboardEventKind) - { + switch ((KeyboardEventKind)evt.EventKind) { case KeyboardEventKind.RawKeyDown: case KeyboardEventKind.RawKeyRepeat: case KeyboardEventKind.RawKeyUp: GetCharCodes(inEvent, out code, out charCode); - mKeyPressArgs.KeyChar = charCode; + keyPressArgs.KeyChar = charCode; break; } - switch (evt.KeyboardEventKind) - { + switch ((KeyboardEventKind)evt.EventKind) { case KeyboardEventKind.RawKeyRepeat: keyboard.KeyRepeat = true; goto case KeyboardEventKind.RawKeyDown; case KeyboardEventKind.RawKeyDown: - OnKeyPress(mKeyPressArgs); + OnKeyPress(keyPressArgs); keyboard[Keymap[code]] = true; return OSStatus.NoError; @@ -369,12 +248,8 @@ namespace OpenTK.Platform.MacOS } } - private OSStatus ProcessWindowEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) - { - Debug.Assert(evt.EventClass == EventClass.Window); - - switch (evt.WindowEventKind) - { + private OSStatus ProcessWindowEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) { + switch ((WindowEventKind)evt.EventKind) { case WindowEventKind.WindowClose: CancelEventArgs cancel = new CancelEventArgs(); OnClosing(cancel); @@ -385,7 +260,7 @@ namespace OpenTK.Platform.MacOS return OSStatus.EventNotHandled; case WindowEventKind.WindowClosed: - mExists = false; + exists = false; OnClosed(); return OSStatus.NoError; @@ -410,42 +285,34 @@ namespace OpenTK.Platform.MacOS return OSStatus.EventNotHandled; default: - Debug.Print("{0}", evt); - + Debug.Print("unhandled {0}", evt); return OSStatus.EventNotHandled; } } - protected OSStatus ProcessMouseEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) - { - Debug.Assert(evt.EventClass == EventClass.Mouse); + + protected OSStatus ProcessMouseEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) { MacOSMouseButton button; HIPoint pt = new HIPoint(); HIPoint screenLoc = new HIPoint(); OSStatus err = API.GetEventMouseLocation(inEvent, out screenLoc); - if (this.windowState == WindowState.Fullscreen) - { + if (this.windowState == WindowState.Fullscreen) { pt = screenLoc; - } - else - { + } else { err = API.GetEventWindowMouseLocation(inEvent, out pt); } - if (err != OSStatus.NoError) - { + if (err != OSStatus.NoError) { // this error comes up from the application event handler. - if (err != OSStatus.EventParameterNotFound) - { + if (err != OSStatus.EventParameterNotFound) { throw new MacOSException(err); } } Point mousePosInClient = new Point((int)pt.X, (int)pt.Y); - if (this.windowState != WindowState.Fullscreen) - { - mousePosInClient.Y -= mTitlebarHeight; + if (this.windowState != WindowState.Fullscreen) { + mousePosInClient.Y -= titlebarHeight; } // check for enter/leave events @@ -453,8 +320,7 @@ namespace OpenTK.Platform.MacOS API.GetEventWindowRef(inEvent, out thisEventWindow); CheckEnterLeaveEvents(thisEventWindow, mousePosInClient); - switch (evt.MouseEventKind) - { + switch ((MouseEventKind)evt.EventKind) { case MouseEventKind.MouseDown: button = API.GetEventMouseButton(inEvent); @@ -521,13 +387,12 @@ namespace OpenTK.Platform.MacOS return OSStatus.EventNotHandled; default: - Debug.Print("{0}", evt); + Debug.Print("unhandled {0}", evt); return OSStatus.EventNotHandled; } } - private void CheckEnterLeaveEvents(IntPtr eventWindowRef, Point pt) - { + private void CheckEnterLeaveEvents(IntPtr eventWindowRef, Point pt) { if (window == null) return; @@ -536,25 +401,21 @@ namespace OpenTK.Platform.MacOS if (pt.Y < 0) thisIn = false; - if (thisIn != mMouseIn) - { - mMouseIn = thisIn; + if (thisIn == mouseIn) return; + mouseIn = thisIn; - if (mMouseIn) - OnMouseEnter(); - else - OnMouseLeave(); - } + if (mouseIn) + OnMouseEnter(); + else + OnMouseLeave(); } - private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char charCode) - { + private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char charCode) { code = API.GetEventKeyboardKeyCode(inEvent); charCode = API.GetEventKeyboardChar(inEvent); } - private void ProcessModifierKey(IntPtr inEvent) - { + private void ProcessModifierKey(IntPtr inEvent) { MacOSKeyModifiers modifiers = API.GetEventKeyModifiers(inEvent); bool caps = (modifiers & MacOSKeyModifiers.CapsLock) != 0; @@ -579,11 +440,6 @@ namespace OpenTK.Platform.MacOS if (keyboard[Key.CapsLock] ^ caps) keyboard[Key.CapsLock] = caps; - - } - - Rect GetRegion() { - return API.GetWindowBounds(window.WindowRef, WindowRegionCode.ContentRegion); } void SetLocation(short x, short y) { @@ -631,53 +487,43 @@ namespace OpenTK.Platform.MacOS clientRectangle = new Rectangle(0, 0, r.Width, r.Height); } - #endregion - #region INativeWindow Members - public void ProcessEvents() - { - Application.ProcessEvents(); + public void ProcessEvents() { + API.ProcessEvents(); } - public Point PointToClient(Point point) - { + public Point PointToClient(Point point) { IntPtr handle = window.WindowRef; - Rect r = Carbon.API.GetWindowBounds(window.WindowRef, WindowRegionCode.ContentRegion); Debug.Print("Rect: {0}", r); - return new Point(point.X - r.X, point.Y - r.Y); } - public Point PointToScreen(Point point) - { + + public Point PointToScreen(Point point) { IntPtr handle = window.WindowRef; - Rect r = Carbon.API.GetWindowBounds(window.WindowRef, WindowRegionCode.ContentRegion); Debug.Print("Rect: {0}", r); - return new Point(point.X + r.X, point.Y + r.Y); } - public bool Exists - { - get { return mExists; } + public bool Exists { + get { return exists; } } - public IWindowInfo WindowInfo - { + public IWindowInfo WindowInfo { get { return window; } } public Icon Icon { - get { return mIcon; } + get { return icon; } set { + icon = value; SetIcon(value); } } - private void SetIcon(Icon icon) - { + private void SetIcon(Icon icon) { // The code for this function was adapted from Mono's // XplatUICarbon implementation, written by Geoff Norton // http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/Managed.Windows.Forms/System.Windows.Forms/XplatUICarbon.cs?view=markup&pathrev=136932 @@ -727,45 +573,31 @@ namespace OpenTK.Platform.MacOS } } - public string Title - { - get - { - return title; - } - set - { + public string Title { + get { return title; } + set { API.SetWindowTitle(window.WindowRef, value); title = value; } } - public bool Visible - { + public bool Visible { get { return API.IsWindowVisible(window.WindowRef); } - set - { - if (value && Visible == false) + set { + if (value && !Visible) Show(); else Hide(); } } - public bool Focused - { - get { return this.mIsActive; } + public bool Focused { + get { return isActive; } } - public Rectangle Bounds - { - get - { - - return bounds; - } - set - { + public Rectangle Bounds { + get { return bounds; } + set { Location = value.Location; Size = value.Size; } @@ -781,14 +613,12 @@ namespace OpenTK.Platform.MacOS set { SetSize((short)value.Width, (short)value.Height); } } - public int Width - { + public int Width { get { return ClientRectangle.Width; } set { SetClientSize((short)value, (short)Height); } } - public int Height - { + public int Height { get { return ClientRectangle.Height; } set { SetClientSize((short)Width, (short)value); } } @@ -803,35 +633,24 @@ namespace OpenTK.Platform.MacOS set { Location = new Point(X, value); } } - public Rectangle ClientRectangle - { - get - { - return clientRectangle; - } - set - { + public Rectangle ClientRectangle { + get { return clientRectangle; } + set { // just set the size, and ignore the location value. // this is the behavior of the Windows WinGLNative. ClientSize = value.Size; } } - public Size ClientSize - { - get - { - return clientRectangle.Size; - } - set - { + public Size ClientSize { + get { return clientRectangle.Size; } + set { API.SizeWindow(window.WindowRef, (short)value.Width, (short)value.Height, true); OnResize(); } } - public void Close() - { + public void Close() { CancelEventArgs e = new CancelEventArgs(); OnClosing(e); @@ -839,14 +658,11 @@ namespace OpenTK.Platform.MacOS return; OnClosed(); - Dispose(); } - public WindowState WindowState - { - get - { + public WindowState WindowState { + get { if (windowState == WindowState.Fullscreen) return WindowState.Fullscreen; @@ -854,14 +670,11 @@ namespace OpenTK.Platform.MacOS return WindowState.Minimized; if (Carbon.API.IsWindowInStandardState(window.WindowRef)) - { return WindowState.Maximized; - } return WindowState.Normal; } - set - { + set { if (value == WindowState) return; @@ -870,8 +683,7 @@ namespace OpenTK.Platform.MacOS windowState = value; - if (oldState == WindowState.Fullscreen) - { + if (oldState == WindowState.Fullscreen) { window.goWindowedHack = true; // when returning from full screen, wait until the context is updated @@ -879,21 +691,20 @@ namespace OpenTK.Platform.MacOS return; } - if (oldState == WindowState.Minimized) - { - API.CollapseWindow(window.WindowRef, false); + if (oldState == WindowState.Minimized) { + OSStatus err = API.CollapseWindow(window.WindowRef, false); + API.CheckReturn( err ); } SetCarbonWindowState(); } } - private void SetCarbonWindowState() - { + private void SetCarbonWindowState() { CarbonPoint idealSize; + OSStatus err; - switch (windowState) - { + switch (windowState) { case WindowState.Fullscreen: window.goFullScreenHack = true; break; @@ -903,84 +714,70 @@ namespace OpenTK.Platform.MacOS // meaning they are maximized up to their reported ideal size. So we report a // large ideal size. idealSize = new CarbonPoint(9000, 9000); - API.ZoomWindowIdeal(window.WindowRef, WindowPartCode.inZoomOut, ref idealSize); + err = API.ZoomWindowIdeal(window.WindowRef, (short)WindowPartCode.inZoomOut, ref idealSize); + API.CheckReturn( err ); break; case WindowState.Normal: - if (WindowState == WindowState.Maximized) - { + if (WindowState == WindowState.Maximized) { idealSize = new CarbonPoint(); - API.ZoomWindowIdeal(window.WindowRef, WindowPartCode.inZoomIn, ref idealSize); + err = API.ZoomWindowIdeal(window.WindowRef, (short)WindowPartCode.inZoomIn, ref idealSize); + API.CheckReturn( err ); } break; case WindowState.Minimized: - API.CollapseWindow(window.WindowRef, true); - + err = API.CollapseWindow(window.WindowRef, true); + API.CheckReturn( err ); break; } - - + OnWindowStateChanged(); - OnResize(); } - #region --- Event wrappers --- - - private void OnKeyPress(KeyPressEventArgs keyPressArgs) - { + private void OnKeyPress(KeyPressEventArgs keyPressArgs) { if (KeyPress != null) KeyPress(this, keyPressArgs); } - - private void OnWindowStateChanged() - { + private void OnWindowStateChanged() { if (WindowStateChanged != null) WindowStateChanged(this, EventArgs.Empty); } - protected virtual void OnClosing(CancelEventArgs e) - { + protected virtual void OnClosing(CancelEventArgs e) { if (Closing != null) Closing(this, e); } - protected virtual void OnClosed() - { + protected virtual void OnClosed() { if (Closed != null) Closed(this, EventArgs.Empty); } - - private void OnMouseLeave() - { + private void OnMouseLeave() { if (MouseLeave != null) MouseLeave(this, EventArgs.Empty); } - private void OnMouseEnter() - { + private void OnMouseEnter() { if (MouseEnter != null) MouseEnter(this, EventArgs.Empty); } - private void OnActivate() - { - mIsActive = true; + private void OnActivate() { + isActive = true; if (FocusedChanged != null) FocusedChanged(this, EventArgs.Empty); } - private void OnDeactivate() - { - mIsActive = false; + + private void OnDeactivate() { + isActive = false; if (FocusedChanged != null) FocusedChanged(this, EventArgs.Empty); } - #endregion - public event EventHandler Load; public event EventHandler Unload; public event EventHandler Move; @@ -997,10 +794,8 @@ namespace OpenTK.Platform.MacOS public event EventHandler KeyPress; public event EventHandler MouseEnter; public event EventHandler MouseLeave; - - #endregion - #region IInputDriver Members + #endregion KeyboardDevice keyboard = new KeyboardDevice(); MouseDevice mouse = new MouseDevice(); @@ -1040,7 +835,5 @@ namespace OpenTK.Platform.MacOS CG.CGDisplayHideCursor(CG.CGMainDisplayID()); } } - - #endregion } } diff --git a/OpenTK/Platform/MacOS/CarbonWindowInfo.cs b/OpenTK/Platform/MacOS/CarbonWindowInfo.cs index 814ac5a70..9d0e8f62a 100644 --- a/OpenTK/Platform/MacOS/CarbonWindowInfo.cs +++ b/OpenTK/Platform/MacOS/CarbonWindowInfo.cs @@ -26,64 +26,35 @@ #endregion using System; -using System.Collections.Generic; -using System.Text; -namespace OpenTK.Platform.MacOS -{ +namespace OpenTK.Platform.MacOS { + /// \internal - /// - /// Describes a Carbon window. - /// - sealed class CarbonWindowInfo : IWindowInfo - { - IntPtr windowRef; + /// Describes a Carbon window. + sealed class CarbonWindowInfo : IWindowInfo { + + public IntPtr WindowRef; bool ownHandle = false; bool disposed = false; internal bool goFullScreenHack = false; internal bool goWindowedHack = false; + internal CarbonGLNative nativeWindow; - #region Constructors - - /// - /// Constructs a new instance with the specified parameters. - /// - /// A valid Carbon window reference. - /// - public CarbonWindowInfo(IntPtr windowRef, bool ownHandle) - { - this.windowRef = windowRef; + public CarbonWindowInfo( IntPtr windowRef, CarbonGLNative nativeWindow, bool ownHandle ) { + this.WindowRef = windowRef; + this.nativeWindow = nativeWindow; this.ownHandle = ownHandle; } - #endregion - - #region Public Members - - /// - /// Gets the window reference for this instance. - /// - internal IntPtr WindowRef - { - get { return this.windowRef; } + public override string ToString() { + return String.Format("CarbonWindowInfo: Handle {0}", WindowRef); } - - /// Returns a System.String that represents the current window. - /// A System.String that represents the current window. - public override string ToString() - { - return String.Format("MacOS.CarbonWindowInfo: Handle {0}", WindowRef); - } - - #endregion // TODO: I have no idea if this is right. public IntPtr WinHandle { - get { return windowRef; } + get { return WindowRef; } } - #region IDisposable Members - public void Dispose() { Dispose(true); } @@ -92,11 +63,10 @@ namespace OpenTK.Platform.MacOS if (disposed) return; - if (ownHandle) - { - Debug.Print("Disposing window {0}.", windowRef); - Carbon.API.DisposeWindow(this.windowRef); - windowRef = IntPtr.Zero; + if (ownHandle) { + Debug.Print("Disposing window {0}.", WindowRef); + Carbon.API.DisposeWindow(this.WindowRef); + WindowRef = IntPtr.Zero; } disposed = true; @@ -105,7 +75,5 @@ namespace OpenTK.Platform.MacOS ~CarbonWindowInfo() { Dispose(false); } - - #endregion } } diff --git a/OpenTK/Platform/MacOS/EventInfo.cs b/OpenTK/Platform/MacOS/EventInfo.cs index cac723b8c..cc4e7960f 100644 --- a/OpenTK/Platform/MacOS/EventInfo.cs +++ b/OpenTK/Platform/MacOS/EventInfo.cs @@ -14,76 +14,18 @@ using System.Text; namespace OpenTK.Platform.MacOS.Carbon { - internal struct EventInfo - { - internal EventInfo(IntPtr eventRef) - { - this._eventClass = API.GetEventClass(eventRef); - this._eventKind = API.GetEventKind(eventRef); - } - - uint _eventKind; - EventClass _eventClass; - - public EventClass EventClass { get { return _eventClass; }} + internal struct EventInfo { - public WindowEventKind WindowEventKind - { - get - { - if (EventClass == EventClass.Window) - return (WindowEventKind) _eventKind; - else - throw new InvalidCastException("Event is not a Window event."); - } - } - public KeyboardEventKind KeyboardEventKind - { - get - { - if (EventClass == EventClass.Keyboard) - return (KeyboardEventKind) _eventKind; - else - throw new InvalidCastException("Event is not a Keyboard event."); - } - } - public MouseEventKind MouseEventKind - { - get - { - if (EventClass == EventClass.Mouse) - return (MouseEventKind) _eventKind; - else - throw new InvalidCastException("Event is not an Mouse event."); - } - } - public AppEventKind AppEventKind - { - get - { - if (EventClass == EventClass.Application) - return (AppEventKind) _eventKind; - else - throw new InvalidCastException("Event is not an Application event."); - } + internal EventInfo(IntPtr eventRef) { + EventClass = API.GetEventClass(eventRef); + EventKind = API.GetEventKind(eventRef); } + public uint EventKind; + public EventClass EventClass; - public override string ToString() - { - switch(EventClass) - { - case EventClass.Application: - return "Event: App " + AppEventKind.ToString(); - case EventClass.Keyboard: - return "Event: Keyboard " + KeyboardEventKind.ToString(); - case EventClass.Mouse: - return "Event: Mouse " + MouseEventKind.ToString(); - case EventClass.Window: - return "Event: Window " + WindowEventKind.ToString(); - } - - return "Event: Unknown Class " + EventClass.ToString() + " kind: " + _eventKind.ToString(); + public override string ToString() { + return "Event class " + EventClass + ", kind: " + EventKind; } } } diff --git a/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs b/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs index 23744e9d6..462e8b859 100644 --- a/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs +++ b/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs @@ -86,9 +86,7 @@ namespace OpenTK.Platform.MacOS } } - - internal static IntPtr HandleTo(DisplayDevice displayDevice) - { + internal static IntPtr HandleTo(DisplayDevice displayDevice) { if (displayMap.ContainsKey(displayDevice)) return displayMap[displayDevice]; else @@ -127,9 +125,7 @@ namespace OpenTK.Platform.MacOS } Debug.Print("Changing resolution to {0}x{1}x{2}@{3}.", width, height, bpp, freq); - CG.CGDisplaySwitchToMode(display, displayModes[j]); - return true; } @@ -137,12 +133,10 @@ namespace OpenTK.Platform.MacOS return false; } - public bool TryRestoreResolution(DisplayDevice device) - { + public bool TryRestoreResolution(DisplayDevice device) { IntPtr display = displayMap[device]; - if (storedModes.ContainsKey(display)) - { + if (storedModes.ContainsKey(display)) { Debug.Print("Restoring resolution."); CG.CGDisplaySwitchToMode(display, storedModes[display]);