Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | // |
| 2 | // |
||
| 3 | // xCSCarbon |
||
| 4 | // |
||
| 5 | // Created by Erik Ylvisaker on 3/17/08. |
||
| 6 | // Copyright 2008 __MyCompanyName__. All rights reserved. |
||
| 7 | // |
||
| 8 | // |
||
| 9 | |||
| 10 | using System; |
||
| 11 | using System.Collections.Generic; |
||
| 12 | using System.Diagnostics; |
||
| 13 | using System.IO; |
||
| 14 | using System.Text; |
||
| 15 | |||
| 16 | namespace OpenTK.Platform.MacOS.Carbon |
||
| 17 | { |
||
| 18 | static class Application |
||
| 19 | { |
||
| 20 | static bool mInitialized = false; |
||
| 21 | static IntPtr uppHandler; |
||
| 22 | static CarbonGLNative eventHandler; |
||
| 23 | static int osMajor, osMinor, osBugfix; |
||
| 24 | |||
| 25 | static Application() |
||
| 26 | { |
||
| 27 | Initialize(); |
||
| 28 | } |
||
| 29 | |||
| 30 | internal static void Initialize() |
||
| 31 | { |
||
| 32 | if (mInitialized) return; |
||
| 33 | |||
| 34 | API.AcquireRootMenu(); |
||
| 35 | |||
| 36 | ConnectEvents(); |
||
| 37 | |||
| 38 | API.Gestalt(GestaltSelector.SystemVersionMajor, out osMajor); |
||
| 39 | API.Gestalt(GestaltSelector.SystemVersionMinor, out osMinor); |
||
| 40 | API.Gestalt(GestaltSelector.SystemVersionBugFix, out osBugfix); |
||
| 41 | |||
| 42 | Debug.Print("Running on Mac OS X {0}.{1}.{2}.", osMajor, osMinor, osBugfix); |
||
| 43 | |||
| 44 | TransformProcessToForeground(); |
||
| 45 | } |
||
| 46 | |||
| 47 | private static void TransformProcessToForeground() |
||
| 48 | { |
||
| 49 | Carbon.ProcessSerialNumber psn = new ProcessSerialNumber(); |
||
| 50 | |||
| 51 | Debug.Print("Setting process to be foreground application."); |
||
| 52 | |||
| 53 | API.GetCurrentProcess(ref psn); |
||
| 54 | API.TransformProcessType(ref psn, ProcessApplicationTransformState.kProcessTransformToForegroundApplication); |
||
| 55 | API.SetFrontProcess(ref psn); |
||
| 56 | } |
||
| 57 | |||
| 58 | internal static CarbonGLNative WindowEventHandler |
||
| 59 | { |
||
| 60 | get { return eventHandler; } |
||
| 61 | set { eventHandler = value; } |
||
| 62 | } |
||
| 63 | |||
| 64 | static void ConnectEvents() |
||
| 65 | { |
||
| 66 | EventTypeSpec[] eventTypes = new EventTypeSpec[] |
||
| 67 | { |
||
| 68 | new EventTypeSpec(EventClass.Application, AppEventKind.AppActivated), |
||
| 69 | new EventTypeSpec(EventClass.Application, AppEventKind.AppDeactivated), |
||
| 70 | new EventTypeSpec(EventClass.Application, AppEventKind.AppQuit), |
||
| 71 | |||
| 72 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDown), |
||
| 73 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseUp), |
||
| 74 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseMoved), |
||
| 75 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDragged), |
||
| 76 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseEntered), |
||
| 77 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseExited), |
||
| 78 | new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelMoved), |
||
| 79 | |||
| 80 | new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyDown), |
||
| 81 | new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyRepeat), |
||
| 82 | new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyUp), |
||
| 83 | new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyModifiersChanged), |
||
| 84 | |||
| 85 | new EventTypeSpec(EventClass.AppleEvent, AppleEventKind.AppleEvent), |
||
| 86 | }; |
||
| 87 | |||
| 88 | MacOSEventHandler handler = EventHandler; |
||
| 89 | uppHandler = API.NewEventHandlerUPP(handler); |
||
| 90 | |||
| 91 | API.InstallApplicationEventHandler( |
||
| 92 | uppHandler, eventTypes, IntPtr.Zero, IntPtr.Zero); |
||
| 93 | |||
| 94 | mInitialized = true; |
||
| 95 | } |
||
| 96 | |||
| 97 | static OSStatus EventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData) |
||
| 98 | { |
||
| 99 | EventInfo evt = new EventInfo(inEvent); |
||
| 100 | |||
| 101 | switch (evt.EventClass) |
||
| 102 | { |
||
| 103 | case EventClass.Application: |
||
| 104 | switch (evt.AppEventKind) |
||
| 105 | { |
||
| 106 | default: |
||
| 107 | return OSStatus.EventNotHandled; |
||
| 108 | } |
||
| 109 | |||
| 110 | case EventClass.AppleEvent: |
||
| 111 | // only event here is the apple event. |
||
| 112 | Debug.Print("Processing apple event."); |
||
| 113 | API.ProcessAppleEvent(inEvent); |
||
| 114 | break; |
||
| 115 | |||
| 116 | case EventClass.Keyboard: |
||
| 117 | case EventClass.Mouse: |
||
| 118 | if (WindowEventHandler != null) |
||
| 119 | { |
||
| 120 | return WindowEventHandler.DispatchEvent(inCaller, inEvent, evt, userData); |
||
| 121 | } |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | |||
| 125 | return OSStatus.EventNotHandled; |
||
| 126 | } |
||
| 127 | |||
| 128 | public static void Run(CarbonGLNative window) |
||
| 129 | { |
||
| 130 | window.Closed += MainWindowClosed; |
||
| 131 | window.Visible = true; |
||
| 132 | |||
| 133 | API.RunApplicationEventLoop(); |
||
| 134 | |||
| 135 | window.Closed -= MainWindowClosed; |
||
| 136 | } |
||
| 137 | |||
| 138 | static void MainWindowClosed(object sender, EventArgs e) |
||
| 139 | { |
||
| 140 | Debug.Print("Quitting application event loop."); |
||
| 141 | API.QuitApplicationEventLoop(); |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | internal static void ProcessEvents() |
||
| 146 | { |
||
| 147 | API.ProcessEvents(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |