glfw/src/cocoa_init.m

273 lines
9.4 KiB
Mathematica
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
// GLFW - An OpenGL library
2010-09-07 15:34:51 +00:00
// Platform: Cocoa/NSOpenGL
2010-09-07 15:41:26 +00:00
// API Version: 3.0
2010-09-07 15:34:51 +00:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
// Needed for _NSGetProgname
#include <crt_externs.h>
#include "internal.h"
2010-09-16 03:09:36 +00:00
2010-09-16 22:44:33 +00:00
//========================================================================
// GLFW application class
//========================================================================
2010-09-07 15:34:51 +00:00
@interface GLFWApplication : NSApplication
@end
@implementation GLFWApplication
// From http://cocoadev.com/index.pl?GameKeyboardHandlingAlmost
// This works around an AppKit bug, where key up events while holding
// down the command key don't get sent to the key window.
- (void)sendEvent:(NSEvent *)event
{
2010-09-15 16:57:25 +00:00
if ([event type] == NSKeyUp && ([event modifierFlags] & NSCommandKeyMask))
2010-09-07 15:34:51 +00:00
[[self keyWindow] sendEvent:event];
else
[super sendEvent:event];
}
@end
2010-09-16 22:44:33 +00:00
2010-09-07 15:34:51 +00:00
// Prior to Snow Leopard, we need to use this oddly-named semi-private API
// to get the application menu working properly. Need to be careful in
// case it goes away in a future OS update.
@interface NSApplication (NSAppleMenu)
2010-09-15 16:57:25 +00:00
- (void)setAppleMenu:(NSMenu*)m;
2010-09-07 15:34:51 +00:00
@end
// Keys to search for as potential application names
2010-09-15 16:57:25 +00:00
NSString* GLFWNameKeys[] =
2010-09-07 15:34:51 +00:00
{
@"CFBundleDisplayName",
@"CFBundleName",
@"CFBundleExecutable",
};
2010-09-16 22:44:33 +00:00
2010-09-07 15:34:51 +00:00
//========================================================================
// Try to figure out what the calling application is called
//========================================================================
2010-09-15 16:57:25 +00:00
static NSString* findAppName(void)
2010-09-07 15:34:51 +00:00
{
unsigned int i;
2010-09-15 16:57:25 +00:00
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
for (i = 0; i < sizeof(GLFWNameKeys) / sizeof(GLFWNameKeys[0]); i++)
2010-09-07 15:34:51 +00:00
{
id name = [infoDictionary objectForKey:GLFWNameKeys[i]];
if (name &&
[name isKindOfClass:[NSString class]] &&
![@"" isEqualToString:name])
{
return name;
}
}
// If we get here, we're unbundled
2010-09-15 16:57:25 +00:00
if (!_glfwLibrary.NS.unbundled)
2010-09-07 15:34:51 +00:00
{
// Could do this only if we discover we're unbundled, but it should
// do no harm...
ProcessSerialNumber psn = { 0, kCurrentProcess };
2010-09-15 16:57:25 +00:00
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
2010-09-07 15:34:51 +00:00
// Having the app in front of the terminal window is also generally
// handy. There is an NSApplication API to do this, but...
2010-09-15 16:57:25 +00:00
SetFrontProcess(&psn);
2010-09-07 15:34:51 +00:00
2010-09-15 16:57:25 +00:00
_glfwLibrary.NS.unbundled = GL_TRUE;
2010-09-07 15:34:51 +00:00
}
2010-09-15 16:57:25 +00:00
char** progname = _NSGetProgname();
if (progname && *progname)
2010-09-07 15:34:51 +00:00
{
2011-11-02 16:34:27 +00:00
// TODO: UTF-8?
2010-09-07 15:34:51 +00:00
return [NSString stringWithUTF8String:*progname];
}
// Really shouldn't get here
return @"GLFW Application";
}
//========================================================================
// Set up the menu bar (manually)
// This is nasty, nasty stuff -- calls to undocumented semi-private APIs that
// could go away at any moment, lots of stuff that really should be
// localize(d|able), etc. Loading a nib would save us this horror, but that
// doesn't seem like a good thing to require of GLFW's clients.
//========================================================================
2010-09-16 03:08:04 +00:00
static void setUpMenuBar(void)
2010-09-07 15:34:51 +00:00
{
2010-09-16 03:09:36 +00:00
NSString* appName = findAppName();
2010-09-07 15:34:51 +00:00
2010-09-16 03:09:36 +00:00
NSMenu* bar = [[NSMenu alloc] init];
2010-09-07 15:34:51 +00:00
[NSApp setMainMenu:bar];
2010-09-16 03:09:36 +00:00
NSMenuItem* appMenuItem =
2010-09-07 15:34:51 +00:00
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
2010-09-16 03:09:36 +00:00
NSMenu* appMenu = [[NSMenu alloc] init];
2010-09-07 15:34:51 +00:00
[appMenuItem setSubmenu:appMenu];
[appMenu addItemWithTitle:[NSString stringWithFormat:@"About %@", appName]
action:@selector(orderFrontStandardAboutPanel:)
keyEquivalent:@""];
[appMenu addItem:[NSMenuItem separatorItem]];
2010-09-16 03:09:36 +00:00
NSMenu* servicesMenu = [[NSMenu alloc] init];
2010-09-07 15:34:51 +00:00
[NSApp setServicesMenu:servicesMenu];
[[appMenu addItemWithTitle:@"Services"
action:NULL
keyEquivalent:@""] setSubmenu:servicesMenu];
[appMenu addItem:[NSMenuItem separatorItem]];
[appMenu addItemWithTitle:[NSString stringWithFormat:@"Hide %@", appName]
action:@selector(hide:)
keyEquivalent:@"h"];
[[appMenu addItemWithTitle:@"Hide Others"
action:@selector(hideOtherApplications:)
keyEquivalent:@"h"]
setKeyEquivalentModifierMask:NSAlternateKeyMask | NSCommandKeyMask];
[appMenu addItemWithTitle:@"Show All"
action:@selector(unhideAllApplications:)
keyEquivalent:@""];
[appMenu addItem:[NSMenuItem separatorItem]];
[appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %@", appName]
action:@selector(terminate:)
keyEquivalent:@"q"];
2010-09-16 03:09:36 +00:00
NSMenuItem* windowMenuItem =
2010-09-07 15:34:51 +00:00
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
2010-09-16 03:09:36 +00:00
NSMenu* windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
2010-09-07 15:34:51 +00:00
[NSApp setWindowsMenu:windowMenu];
[windowMenuItem setSubmenu:windowMenu];
[windowMenu addItemWithTitle:@"Miniaturize"
action:@selector(performMiniaturize:)
keyEquivalent:@"m"];
[windowMenu addItemWithTitle:@"Zoom"
action:@selector(performZoom:)
keyEquivalent:@""];
[windowMenu addItem:[NSMenuItem separatorItem]];
[windowMenu addItemWithTitle:@"Bring All to Front"
action:@selector(arrangeInFront:)
keyEquivalent:@""];
// At least guard the call to private API to avoid an exception if it
// goes away. Hopefully that means the worst we'll break in future is to
// look ugly...
2010-09-15 16:57:25 +00:00
if ([NSApp respondsToSelector:@selector(setAppleMenu:)])
2010-09-07 15:34:51 +00:00
[NSApp setAppleMenu:appMenu];
}
2010-09-16 01:25:36 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
//========================================================================
// Initialize the GLFW library
//========================================================================
2010-09-15 16:57:25 +00:00
int _glfwPlatformInit(void)
2010-09-07 15:34:51 +00:00
{
2010-09-15 16:57:25 +00:00
_glfwLibrary.NS.autoreleasePool = [[NSAutoreleasePool alloc] init];
2010-09-07 15:34:51 +00:00
// Implicitly create shared NSApplication instance
[GLFWApplication sharedApplication];
_glfwLibrary.NS.OpenGLFramework =
2011-11-02 16:34:27 +00:00
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
if (_glfwLibrary.NS.OpenGLFramework == NULL)
{
_glfwSetError(GLFW_PLATFORM_ERROR,
"glfwInit: Failed to locate OpenGL framework");
return GL_FALSE;
}
2010-09-07 15:34:51 +00:00
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
2010-09-15 16:57:25 +00:00
if (access([resourcePath cStringUsingEncoding:NSUTF8StringEncoding], R_OK) == 0)
chdir([resourcePath cStringUsingEncoding:NSUTF8StringEncoding]);
2010-09-07 15:34:51 +00:00
// Setting up menu bar must go exactly here else weirdness ensues
setUpMenuBar();
[NSApp finishLaunching];
2010-09-15 16:57:25 +00:00
_glfwPlatformSetTime(0.0);
2010-09-07 15:34:51 +00:00
2010-09-15 16:57:25 +00:00
_glfwLibrary.NS.desktopMode =
(NSDictionary*) CGDisplayCurrentMode(CGMainDisplayID());
2010-09-07 15:34:51 +00:00
// Save the original gamma ramp
_glfwLibrary.originalRampSize = CGDisplayGammaTableCapacity(CGMainDisplayID());
_glfwPlatformGetGammaRamp(&_glfwLibrary.originalRamp);
_glfwLibrary.currentRamp = _glfwLibrary.originalRamp;
2011-11-02 16:34:27 +00:00
2011-09-18 19:05:00 +00:00
_glfwInitJoysticks();
2010-09-07 15:34:51 +00:00
return GL_TRUE;
}
//========================================================================
// Close window, if open, and shut down GLFW
//========================================================================
2010-09-16 03:09:36 +00:00
int _glfwPlatformTerminate(void)
2010-09-07 15:34:51 +00:00
{
// TODO: Probably other cleanup
2011-11-02 16:34:27 +00:00
// Restore the original gamma ramp
_glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp);
2011-11-02 16:34:27 +00:00
[NSApp setDelegate:nil];
[_glfwLibrary.NS.delegate release];
_glfwLibrary.NS.delegate = nil;
2010-09-15 16:57:25 +00:00
[_glfwLibrary.NS.autoreleasePool release];
_glfwLibrary.NS.autoreleasePool = nil;
2010-09-07 15:34:51 +00:00
2011-09-18 19:05:00 +00:00
_glfwTerminateJoysticks();
2010-09-07 15:34:51 +00:00
return GL_TRUE;
}
2010-09-15 16:57:25 +00:00
//========================================================================
// Get the GLFW version string
//========================================================================
const char* _glfwPlatformGetVersionString(void)
{
const char* version = _GLFW_VERSION_FULL " Cocoa";
2010-09-15 16:57:25 +00:00
return version;
}