From ed87814a7f59fe1aa6b4d51d102e8d9a351cfec7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olle=20H=C3=A5kansson?= Date: Mon, 21 Apr 2014 19:41:45 +0200 Subject: [PATCH] Cleanups. --- .../OpenTK/Platform/MacOS/Cocoa/NSApplication.cs | 8 +- Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs | 205 +++++++++++---------- 2 files changed, 111 insertions(+), 102 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs b/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs index badc24f..1ff0b6f 100644 --- a/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs +++ b/Source/OpenTK/Platform/MacOS/Cocoa/NSApplication.cs @@ -7,12 +7,12 @@ namespace OpenTK.Platform.MacOS static class NSApplication { internal static IntPtr Handle; + internal static IntPtr AutoreleasePool; internal static void Initialize() { // Create the NSAutoreleasePool - Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSAutoreleasePool"), Selector.Alloc), - Selector.Init); + AutoreleasePool = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSAutoreleasePool"), Selector.Alloc), Selector.Init); // Fetch the application handle Handle = Cocoa.SendIntPtr(Class.Get("NSApplication"), Selector.Get("sharedApplication")); @@ -20,7 +20,6 @@ namespace OpenTK.Platform.MacOS // Setup the application Cocoa.SendBool(Handle, Selector.Get("setActivationPolicy:"), (int)NSApplicationActivationPolicy.Regular); Cocoa.SendVoid(Handle, Selector.Get("activateIgnoringOtherApps:"), true); - Cocoa.SendVoid(Handle, Selector.Get("finishLaunching")); // Create the menu bar var menubar = Cocoa.SendIntPtr(Cocoa.SendIntPtr(Class.Get("NSMenu"), Selector.Alloc), @@ -32,6 +31,9 @@ namespace OpenTK.Platform.MacOS // Add menu item to bar, and bar to application Cocoa.SendIntPtr(menubar, Selector.Get("addItem:"), menuItem); Cocoa.SendIntPtr(Handle, Selector.Get("setMainMenu:"), menubar); + + // Tell cocoa we're ready to run the application (usually called by [NSApp run]). + Cocoa.SendVoid(Handle, Selector.Get("finishLaunching")); } } } diff --git a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs index f14f08a..541d09f 100644 --- a/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs +++ b/Source/OpenTK/Platform/MacOS/CocoaNativeWindow.cs @@ -266,6 +266,7 @@ namespace OpenTK.Platform.MacOS if (trackingArea != IntPtr.Zero) { Cocoa.SendVoid(owner, selRemoveTrackingArea, trackingArea); + Cocoa.SendVoid(trackingArea, Selector.Release); } var ownerBounds = Cocoa.SendRect(owner, selBounds); @@ -329,132 +330,138 @@ namespace OpenTK.Platform.MacOS public void ProcessEvents() { - var e = Cocoa.SendIntPtr(NSApplication.Handle, selNextEventMatchingMask, uint.MaxValue, IntPtr.Zero, NSDefaultRunLoopMode, true); - - if (e == IntPtr.Zero) - return; - - var type = (NSEventType)Cocoa.SendInt(e, selType); - switch (type) + while (true) { - case NSEventType.KeyDown: - { - var keyCode = Cocoa.SendUshort(e, selKeyCode); - var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags); - var isARepeat = Cocoa.SendBool(e, selIsARepeat); - GetKey(keyCode, modifierFlags, keyArgs); - InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, true); + var e = Cocoa.SendIntPtr(NSApplication.Handle, selNextEventMatchingMask, uint.MaxValue, IntPtr.Zero, NSDefaultRunLoopMode, true); - if (!isARepeat || InputDriver.Keyboard[0].KeyRepeat) - { - KeyDown(this, keyArgs); - } + if (e == IntPtr.Zero) + break; - var s = Cocoa.FromNSString(Cocoa.SendIntPtr(e, selCharactersIgnoringModifiers)); - foreach (var c in s) + var type = (NSEventType)Cocoa.SendInt(e, selType); + switch (type) + { + case NSEventType.KeyDown: { - int intVal = (int)c; - if (!Char.IsControl(c) && (intVal < 63232 || intVal > 63235)) + var keyCode = Cocoa.SendUshort(e, selKeyCode); + var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags); + var isARepeat = Cocoa.SendBool(e, selIsARepeat); + GetKey(keyCode, modifierFlags, keyArgs); + InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, true); + + if (!isARepeat || InputDriver.Keyboard[0].KeyRepeat) { - // For some reason, arrow keys (mapped 63232-63235) are seen as non-control characters, so get rid of those. + KeyDown(this, keyArgs); + } - keyPressArgs.KeyChar = c; - KeyPress(this, keyPressArgs); + var s = Cocoa.FromNSString(Cocoa.SendIntPtr(e, selCharactersIgnoringModifiers)); + foreach (var c in s) + { + int intVal = (int)c; + if (!Char.IsControl(c) && (intVal < 63232 || intVal > 63235)) + { + // For some reason, arrow keys (mapped 63232-63235) are seen as non-control characters, so get rid of those. + + keyPressArgs.KeyChar = c; + KeyPress(this, keyPressArgs); + } } - } - // Steal all keydown events to avoid the annoying "bleep" sound. - return; - } + // Steal all keydown events to avoid the annoying "bleep" sound. + return; + } - case NSEventType.KeyUp: - { - var keyCode = Cocoa.SendUshort(e, selKeyCode); - var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags); + case NSEventType.KeyUp: + { + var keyCode = Cocoa.SendUshort(e, selKeyCode); + var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags); - GetKey(keyCode, modifierFlags, keyArgs); - InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, false); + GetKey(keyCode, modifierFlags, keyArgs); + InputDriver.Keyboard[0].SetKey(keyArgs.Key, keyArgs.ScanCode, false); - KeyUp(this, keyArgs); - } - break; + KeyUp(this, keyArgs); + } + break; - case NSEventType.MouseEntered: - { - var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea); - var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner); - if (trackingAreaOwner == windowInfo.ViewHandle) + case NSEventType.MouseEntered: { - if (!cursorVisible) + var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea); + var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner); + if (trackingAreaOwner == windowInfo.ViewHandle) { - SetCursorVisible(false); - } + if (!cursorVisible) + { + SetCursorVisible(false); + } - MouseEnter(this, EventArgs.Empty); + MouseEnter(this, EventArgs.Empty); + } } - } - break; + break; - case NSEventType.MouseExited: - { - var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea); - var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner); - if (trackingAreaOwner == windowInfo.ViewHandle) + case NSEventType.MouseExited: { - if (!cursorVisible) + var eventTrackingArea = Cocoa.SendIntPtr(e, selTrackingArea); + var trackingAreaOwner = Cocoa.SendIntPtr(eventTrackingArea, selOwner); + if (trackingAreaOwner == windowInfo.ViewHandle) { - SetCursorVisible(true); - } + if (!cursorVisible) + { + SetCursorVisible(true); + } - MouseLeave(this, EventArgs.Empty); + MouseLeave(this, EventArgs.Empty); + } } - } - break; + break; - case NSEventType.MouseMoved: - { - var pf = Cocoa.SendPoint(e, selLocationInWindowOwner); - var p = new Point((int)pf.X, (int)pf.Y); - - var s = ClientSize; - if (p.X < 0) p.X = 0; - if (p.Y < 0) p.Y = 0; - if (p.X > s.Width) p.X = s.Width; - if (p.Y > s.Height) p.Y = s.Height; - p.Y = s.Height - p.Y; + case NSEventType.MouseMoved: + { + var pf = Cocoa.SendPoint(e, selLocationInWindowOwner); + var p = new Point((int)pf.X, (int)pf.Y); + + var s = ClientSize; + if (p.X < 0) + p.X = 0; + if (p.Y < 0) + p.Y = 0; + if (p.X > s.Width) + p.X = s.Width; + if (p.Y > s.Height) + p.Y = s.Height; + p.Y = s.Height - p.Y; + + InputDriver.Mouse[0].Position = p; + } + break; - InputDriver.Mouse[0].Position = p; - } - break; + case NSEventType.ScrollWheel: + { + var scrollingDelta = Cocoa.SendFloat(e, selScrollingDeltaY); + InputDriver.Mouse[0].WheelPrecise += scrollingDelta; + } + break; - case NSEventType.ScrollWheel: - { - var scrollingDelta = Cocoa.SendFloat(e, selScrollingDeltaY); - InputDriver.Mouse[0].WheelPrecise += scrollingDelta; - } - break; + case NSEventType.LeftMouseDown: + case NSEventType.RightMouseDown: + case NSEventType.OtherMouseDown: + { + var buttonNumber = Cocoa.SendInt(e, selButtonNumber); + InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = true; + } + break; - case NSEventType.LeftMouseDown: - case NSEventType.RightMouseDown: - case NSEventType.OtherMouseDown: - { - var buttonNumber = Cocoa.SendInt(e, selButtonNumber); - InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = true; - } - break; + case NSEventType.LeftMouseUp: + case NSEventType.RightMouseUp: + case NSEventType.OtherMouseUp: + { + var buttonNumber = Cocoa.SendInt(e, selButtonNumber); + InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = false; + } + break; + } - case NSEventType.LeftMouseUp: - case NSEventType.RightMouseUp: - case NSEventType.OtherMouseUp: - { - var buttonNumber = Cocoa.SendInt(e, selButtonNumber); - InputDriver.Mouse[0][GetMouseButton(buttonNumber)] = false; - } - break; + Cocoa.SendVoid(NSApplication.Handle, selSendEvent, e); } - - Cocoa.SendVoid(NSApplication.Handle, selSendEvent, e); - Cocoa.SendVoid(NSApplication.Handle, selUpdateWindows); } public System.Drawing.Point PointToClient(System.Drawing.Point point) -- 2.7.4