00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 using System;
00011 using System.Collections.Generic;
00012 using System.Diagnostics;
00013 using System.IO;
00014 using System.Text;
00015
00016 namespace OpenTK.Platform.MacOS.Carbon
00017 {
00018 static class Application
00019 {
00020 static bool mInitialized = false;
00021 static IntPtr uppHandler;
00022 static CarbonGLNative eventHandler;
00023 static int osMajor, osMinor, osBugfix;
00024
00025 static Application()
00026 {
00027 Initialize();
00028 }
00029
00030 internal static void Initialize()
00031 {
00032 if (mInitialized) return;
00033
00034 API.AcquireRootMenu();
00035
00036 ConnectEvents();
00037
00038 API.Gestalt(GestaltSelector.SystemVersionMajor, out osMajor);
00039 API.Gestalt(GestaltSelector.SystemVersionMinor, out osMinor);
00040 API.Gestalt(GestaltSelector.SystemVersionBugFix, out osBugfix);
00041
00042 Debug.Print("Running on Mac OS X {0}.{1}.{2}.", osMajor, osMinor, osBugfix);
00043
00044 TransformProcessToForeground();
00045 }
00046
00047 private static void TransformProcessToForeground()
00048 {
00049 Carbon.ProcessSerialNumber psn = new ProcessSerialNumber();
00050
00051 Debug.Print("Setting process to be foreground application.");
00052
00053 API.GetCurrentProcess(ref psn);
00054 API.TransformProcessType(ref psn, ProcessApplicationTransformState.kProcessTransformToForegroundApplication);
00055 API.SetFrontProcess(ref psn);
00056 }
00057
00058 internal static CarbonGLNative WindowEventHandler
00059 {
00060 get { return eventHandler; }
00061 set { eventHandler = value; }
00062 }
00063
00064 static void ConnectEvents()
00065 {
00066 EventTypeSpec[] eventTypes = new EventTypeSpec[]
00067 {
00068 new EventTypeSpec(EventClass.Application, AppEventKind.AppActivated),
00069 new EventTypeSpec(EventClass.Application, AppEventKind.AppDeactivated),
00070 new EventTypeSpec(EventClass.Application, AppEventKind.AppQuit),
00071
00072 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDown),
00073 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseUp),
00074 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseMoved),
00075 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDragged),
00076 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseEntered),
00077 new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseExited),
00078 new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelMoved),
00079
00080 new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyDown),
00081 new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyRepeat),
00082 new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyUp),
00083 new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyModifiersChanged),
00084
00085 new EventTypeSpec(EventClass.AppleEvent, AppleEventKind.AppleEvent),
00086 };
00087
00088 MacOSEventHandler handler = EventHandler;
00089 uppHandler = API.NewEventHandlerUPP(handler);
00090
00091 API.InstallApplicationEventHandler(
00092 uppHandler, eventTypes, IntPtr.Zero, IntPtr.Zero);
00093
00094 mInitialized = true;
00095 }
00096
00097 static OSStatus EventHandler(IntPtr inCaller, IntPtr inEvent, IntPtr userData)
00098 {
00099 EventInfo evt = new EventInfo(inEvent);
00100
00101 switch (evt.EventClass)
00102 {
00103 case EventClass.Application:
00104 switch (evt.AppEventKind)
00105 {
00106 default:
00107 return OSStatus.EventNotHandled;
00108 }
00109
00110 case EventClass.AppleEvent:
00111
00112 Debug.Print("Processing apple event.");
00113 API.ProcessAppleEvent(inEvent);
00114 break;
00115
00116 case EventClass.Keyboard:
00117 case EventClass.Mouse:
00118 if (WindowEventHandler != null)
00119 {
00120 return WindowEventHandler.DispatchEvent(inCaller, inEvent, evt, userData);
00121 }
00122 break;
00123 }
00124
00125 return OSStatus.EventNotHandled;
00126 }
00127
00128 public static void Run(CarbonGLNative window)
00129 {
00130 window.Closed += MainWindowClosed;
00131 window.Visible = true;
00132
00133 API.RunApplicationEventLoop();
00134
00135 window.Closed -= MainWindowClosed;
00136 }
00137
00138 static void MainWindowClosed(object sender, EventArgs e)
00139 {
00140 Debug.Print("Quitting application event loop.");
00141 API.QuitApplicationEventLoop();
00142 }
00143
00144
00145 internal static void ProcessEvents()
00146 {
00147 API.ProcessEvents();
00148 }
00149 }
00150 }