2009-02-22 10:43:35 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// xCSCarbon
|
|
|
|
//
|
|
|
|
// Created by Erik Ylvisaker on 3/17/08.
|
|
|
|
// Copyright 2008 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.MacOS.Carbon
|
|
|
|
{
|
2009-07-19 17:50:41 +00:00
|
|
|
static class Application
|
|
|
|
{
|
2009-02-22 10:43:35 +00:00
|
|
|
static bool mInitialized = false;
|
|
|
|
static IntPtr uppHandler;
|
|
|
|
static CarbonGLNative eventHandler;
|
2009-03-02 01:49:23 +00:00
|
|
|
static int osMajor, osMinor, osBugfix;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
static Application()
|
|
|
|
{
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static void Initialize()
|
|
|
|
{
|
|
|
|
if (mInitialized) return;
|
2009-07-19 17:50:41 +00:00
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
API.AcquireRootMenu();
|
|
|
|
|
2009-03-02 01:49:23 +00:00
|
|
|
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);
|
2009-11-16 18:45:41 +00:00
|
|
|
|
|
|
|
TransformProcessToForeground();
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 18:45:41 +00:00
|
|
|
private static void TransformProcessToForeground()
|
|
|
|
{
|
|
|
|
Carbon.ProcessSerialNumber psn = new ProcessSerialNumber();
|
|
|
|
|
|
|
|
Debug.Print("Setting process to be foreground application.");
|
|
|
|
|
|
|
|
API.GetCurrentProcess(ref psn);
|
|
|
|
API.TransformProcessType(ref psn, ProcessApplicationTransformState.kProcessTransformToForegroundApplication);
|
|
|
|
API.SetFrontProcess(ref psn);
|
|
|
|
}
|
|
|
|
|
2009-02-22 10:43:35 +00:00
|
|
|
internal static CarbonGLNative WindowEventHandler
|
|
|
|
{
|
|
|
|
get { return eventHandler; }
|
|
|
|
set { eventHandler = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDown),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseUp),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseMoved),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseDragged),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseEntered),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.MouseExited),
|
|
|
|
new EventTypeSpec(EventClass.Mouse, MouseEventKind.WheelMoved),
|
|
|
|
|
|
|
|
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyDown),
|
|
|
|
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyRepeat),
|
|
|
|
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyUp),
|
|
|
|
new EventTypeSpec(EventClass.Keyboard, KeyboardEventKind.RawKeyModifiersChanged),
|
|
|
|
|
|
|
|
new EventTypeSpec(EventClass.AppleEvent, AppleEventKind.AppleEvent),
|
|
|
|
};
|
|
|
|
|
|
|
|
MacOSEventHandler handler = EventHandler;
|
|
|
|
uppHandler = API.NewEventHandlerUPP(handler);
|
|
|
|
|
|
|
|
API.InstallApplicationEventHandler(
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EventClass.AppleEvent:
|
|
|
|
// only event here is the apple event.
|
|
|
|
Debug.Print("Processing apple event.");
|
|
|
|
API.ProcessAppleEvent(inEvent);
|
|
|
|
break;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2009-06-24 14:42:40 +00:00
|
|
|
window.Closed += MainWindowClosed;
|
2009-06-02 15:49:39 +00:00
|
|
|
window.Visible = true;
|
2009-02-22 10:43:35 +00:00
|
|
|
|
|
|
|
API.RunApplicationEventLoop();
|
|
|
|
|
2009-06-24 14:42:40 +00:00
|
|
|
window.Closed -= MainWindowClosed;
|
2009-02-22 10:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MainWindowClosed(object sender, EventArgs e)
|
|
|
|
{
|
2009-07-19 17:50:41 +00:00
|
|
|
Debug.Print("Quitting application event loop.");
|
2009-02-22 10:43:35 +00:00
|
|
|
API.QuitApplicationEventLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal static void ProcessEvents()
|
|
|
|
{
|
|
|
|
API.ProcessEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|