2010-09-07 11:34:51 -04:00
|
|
|
//========================================================================
|
2016-10-19 18:50:54 -04:00
|
|
|
// GLFW 3.3 macOS - www.glfw.org
|
2010-09-07 11:34:51 -04:00
|
|
|
//------------------------------------------------------------------------
|
2019-04-15 14:50:00 -04:00
|
|
|
// Copyright (c) 2009-2019 Camilla Löwy <elmindreda@glfw.org>
|
2010-09-07 11:34:51 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
|
|
|
#include "internal.h"
|
2012-04-16 05:33:34 -04:00
|
|
|
#include <sys/param.h> // For MAXPATHLEN
|
2010-09-15 23:09:36 -04:00
|
|
|
|
2019-01-10 19:05:00 -05:00
|
|
|
// Needed for _NSGetProgname
|
|
|
|
#include <crt_externs.h>
|
|
|
|
|
2013-02-04 07:22:10 -05:00
|
|
|
// Change to our application bundle's resources directory, if present
|
|
|
|
//
|
2012-02-14 18:33:01 -05:00
|
|
|
static void changeToResourcesDirectory(void)
|
|
|
|
{
|
2012-02-14 19:57:43 -05:00
|
|
|
char resourcesPath[MAXPATHLEN];
|
|
|
|
|
2012-02-14 19:44:55 -05:00
|
|
|
CFBundleRef bundle = CFBundleGetMainBundle();
|
|
|
|
if (!bundle)
|
|
|
|
return;
|
2010-09-15 12:57:25 -04:00
|
|
|
|
2012-02-14 19:44:55 -05:00
|
|
|
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(bundle);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-02-14 19:57:43 -05:00
|
|
|
CFStringRef last = CFURLCopyLastPathComponent(resourcesURL);
|
|
|
|
if (CFStringCompare(CFSTR("Resources"), last, 0) != kCFCompareEqualTo)
|
|
|
|
{
|
|
|
|
CFRelease(last);
|
|
|
|
CFRelease(resourcesURL);
|
2012-02-14 19:44:55 -05:00
|
|
|
return;
|
2012-02-14 19:57:43 -05:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-02-14 19:57:43 -05:00
|
|
|
CFRelease(last);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-02-14 19:44:55 -05:00
|
|
|
if (!CFURLGetFileSystemRepresentation(resourcesURL,
|
2012-02-14 19:57:43 -05:00
|
|
|
true,
|
2012-02-14 19:44:55 -05:00
|
|
|
(UInt8*) resourcesPath,
|
2012-02-14 19:57:43 -05:00
|
|
|
MAXPATHLEN))
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-02-14 19:44:55 -05:00
|
|
|
CFRelease(resourcesURL);
|
|
|
|
return;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2012-02-14 19:44:55 -05:00
|
|
|
CFRelease(resourcesURL);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-02-14 19:44:55 -05:00
|
|
|
chdir(resourcesPath);
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:05:00 -05:00
|
|
|
// 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. Add a nib to save us this horror.
|
|
|
|
//
|
|
|
|
static void createMenuBar(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
NSString* appName = nil;
|
|
|
|
NSDictionary* bundleInfo = [[NSBundle mainBundle] infoDictionary];
|
|
|
|
NSString* nameKeys[] =
|
|
|
|
{
|
|
|
|
@"CFBundleDisplayName",
|
|
|
|
@"CFBundleName",
|
|
|
|
@"CFBundleExecutable",
|
|
|
|
};
|
|
|
|
|
|
|
|
// Try to figure out what the calling application is called
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(nameKeys) / sizeof(nameKeys[0]); i++)
|
|
|
|
{
|
|
|
|
id name = bundleInfo[nameKeys[i]];
|
|
|
|
if (name &&
|
|
|
|
[name isKindOfClass:[NSString class]] &&
|
|
|
|
![name isEqualToString:@""])
|
|
|
|
{
|
|
|
|
appName = name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!appName)
|
|
|
|
{
|
|
|
|
char** progname = _NSGetProgname();
|
|
|
|
if (progname && *progname)
|
|
|
|
appName = @(*progname);
|
|
|
|
else
|
|
|
|
appName = @"GLFW Application";
|
|
|
|
}
|
|
|
|
|
|
|
|
NSMenu* bar = [[NSMenu alloc] init];
|
|
|
|
[NSApp setMainMenu:bar];
|
|
|
|
|
|
|
|
NSMenuItem* appMenuItem =
|
|
|
|
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
|
|
|
|
NSMenu* appMenu = [[NSMenu alloc] init];
|
|
|
|
[appMenuItem setSubmenu:appMenu];
|
|
|
|
|
|
|
|
[appMenu addItemWithTitle:[NSString stringWithFormat:@"About %@", appName]
|
|
|
|
action:@selector(orderFrontStandardAboutPanel:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
NSMenu* servicesMenu = [[NSMenu alloc] init];
|
|
|
|
[NSApp setServicesMenu:servicesMenu];
|
|
|
|
[[appMenu addItemWithTitle:@"Services"
|
|
|
|
action:NULL
|
|
|
|
keyEquivalent:@""] setSubmenu:servicesMenu];
|
|
|
|
[servicesMenu release];
|
|
|
|
[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:NSEventModifierFlagOption | NSEventModifierFlagCommand];
|
|
|
|
[appMenu addItemWithTitle:@"Show All"
|
|
|
|
action:@selector(unhideAllApplications:)
|
|
|
|
keyEquivalent:@""];
|
|
|
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
[appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %@", appName]
|
|
|
|
action:@selector(terminate:)
|
|
|
|
keyEquivalent:@"q"];
|
|
|
|
|
|
|
|
NSMenuItem* windowMenuItem =
|
|
|
|
[bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
|
|
|
|
[bar release];
|
|
|
|
NSMenu* windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
|
|
|
|
[NSApp setWindowsMenu:windowMenu];
|
|
|
|
[windowMenuItem setSubmenu:windowMenu];
|
|
|
|
|
|
|
|
[windowMenu addItemWithTitle:@"Minimize"
|
|
|
|
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:@""];
|
|
|
|
|
|
|
|
// TODO: Make this appear at the bottom of the menu (for consistency)
|
|
|
|
[windowMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
[[windowMenu addItemWithTitle:@"Enter Full Screen"
|
|
|
|
action:@selector(toggleFullScreen:)
|
|
|
|
keyEquivalent:@"f"]
|
|
|
|
setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
|
|
|
|
|
|
|
|
// Prior to Snow Leopard, we need to use this oddly-named semi-private API
|
|
|
|
// to get the application menu working properly.
|
|
|
|
SEL setAppleMenuSelector = NSSelectorFromString(@"setAppleMenu:");
|
|
|
|
[NSApp performSelector:setAppleMenuSelector withObject:appMenu];
|
|
|
|
}
|
|
|
|
|
2014-03-30 10:23:22 -04:00
|
|
|
// Create key code translation tables
|
|
|
|
//
|
|
|
|
static void createKeyTables(void)
|
|
|
|
{
|
2015-07-02 08:24:50 -04:00
|
|
|
int scancode;
|
|
|
|
|
2016-09-07 09:43:39 -04:00
|
|
|
memset(_glfw.ns.keycodes, -1, sizeof(_glfw.ns.keycodes));
|
|
|
|
memset(_glfw.ns.scancodes, -1, sizeof(_glfw.ns.scancodes));
|
|
|
|
|
|
|
|
_glfw.ns.keycodes[0x1D] = GLFW_KEY_0;
|
|
|
|
_glfw.ns.keycodes[0x12] = GLFW_KEY_1;
|
|
|
|
_glfw.ns.keycodes[0x13] = GLFW_KEY_2;
|
|
|
|
_glfw.ns.keycodes[0x14] = GLFW_KEY_3;
|
|
|
|
_glfw.ns.keycodes[0x15] = GLFW_KEY_4;
|
|
|
|
_glfw.ns.keycodes[0x17] = GLFW_KEY_5;
|
|
|
|
_glfw.ns.keycodes[0x16] = GLFW_KEY_6;
|
|
|
|
_glfw.ns.keycodes[0x1A] = GLFW_KEY_7;
|
|
|
|
_glfw.ns.keycodes[0x1C] = GLFW_KEY_8;
|
|
|
|
_glfw.ns.keycodes[0x19] = GLFW_KEY_9;
|
|
|
|
_glfw.ns.keycodes[0x00] = GLFW_KEY_A;
|
|
|
|
_glfw.ns.keycodes[0x0B] = GLFW_KEY_B;
|
|
|
|
_glfw.ns.keycodes[0x08] = GLFW_KEY_C;
|
|
|
|
_glfw.ns.keycodes[0x02] = GLFW_KEY_D;
|
|
|
|
_glfw.ns.keycodes[0x0E] = GLFW_KEY_E;
|
|
|
|
_glfw.ns.keycodes[0x03] = GLFW_KEY_F;
|
|
|
|
_glfw.ns.keycodes[0x05] = GLFW_KEY_G;
|
|
|
|
_glfw.ns.keycodes[0x04] = GLFW_KEY_H;
|
|
|
|
_glfw.ns.keycodes[0x22] = GLFW_KEY_I;
|
|
|
|
_glfw.ns.keycodes[0x26] = GLFW_KEY_J;
|
|
|
|
_glfw.ns.keycodes[0x28] = GLFW_KEY_K;
|
|
|
|
_glfw.ns.keycodes[0x25] = GLFW_KEY_L;
|
|
|
|
_glfw.ns.keycodes[0x2E] = GLFW_KEY_M;
|
|
|
|
_glfw.ns.keycodes[0x2D] = GLFW_KEY_N;
|
|
|
|
_glfw.ns.keycodes[0x1F] = GLFW_KEY_O;
|
|
|
|
_glfw.ns.keycodes[0x23] = GLFW_KEY_P;
|
|
|
|
_glfw.ns.keycodes[0x0C] = GLFW_KEY_Q;
|
|
|
|
_glfw.ns.keycodes[0x0F] = GLFW_KEY_R;
|
|
|
|
_glfw.ns.keycodes[0x01] = GLFW_KEY_S;
|
|
|
|
_glfw.ns.keycodes[0x11] = GLFW_KEY_T;
|
|
|
|
_glfw.ns.keycodes[0x20] = GLFW_KEY_U;
|
|
|
|
_glfw.ns.keycodes[0x09] = GLFW_KEY_V;
|
|
|
|
_glfw.ns.keycodes[0x0D] = GLFW_KEY_W;
|
|
|
|
_glfw.ns.keycodes[0x07] = GLFW_KEY_X;
|
|
|
|
_glfw.ns.keycodes[0x10] = GLFW_KEY_Y;
|
|
|
|
_glfw.ns.keycodes[0x06] = GLFW_KEY_Z;
|
|
|
|
|
|
|
|
_glfw.ns.keycodes[0x27] = GLFW_KEY_APOSTROPHE;
|
|
|
|
_glfw.ns.keycodes[0x2A] = GLFW_KEY_BACKSLASH;
|
|
|
|
_glfw.ns.keycodes[0x2B] = GLFW_KEY_COMMA;
|
|
|
|
_glfw.ns.keycodes[0x18] = GLFW_KEY_EQUAL;
|
|
|
|
_glfw.ns.keycodes[0x32] = GLFW_KEY_GRAVE_ACCENT;
|
|
|
|
_glfw.ns.keycodes[0x21] = GLFW_KEY_LEFT_BRACKET;
|
|
|
|
_glfw.ns.keycodes[0x1B] = GLFW_KEY_MINUS;
|
|
|
|
_glfw.ns.keycodes[0x2F] = GLFW_KEY_PERIOD;
|
|
|
|
_glfw.ns.keycodes[0x1E] = GLFW_KEY_RIGHT_BRACKET;
|
|
|
|
_glfw.ns.keycodes[0x29] = GLFW_KEY_SEMICOLON;
|
|
|
|
_glfw.ns.keycodes[0x2C] = GLFW_KEY_SLASH;
|
|
|
|
_glfw.ns.keycodes[0x0A] = GLFW_KEY_WORLD_1;
|
|
|
|
|
|
|
|
_glfw.ns.keycodes[0x33] = GLFW_KEY_BACKSPACE;
|
|
|
|
_glfw.ns.keycodes[0x39] = GLFW_KEY_CAPS_LOCK;
|
|
|
|
_glfw.ns.keycodes[0x75] = GLFW_KEY_DELETE;
|
|
|
|
_glfw.ns.keycodes[0x7D] = GLFW_KEY_DOWN;
|
|
|
|
_glfw.ns.keycodes[0x77] = GLFW_KEY_END;
|
|
|
|
_glfw.ns.keycodes[0x24] = GLFW_KEY_ENTER;
|
|
|
|
_glfw.ns.keycodes[0x35] = GLFW_KEY_ESCAPE;
|
|
|
|
_glfw.ns.keycodes[0x7A] = GLFW_KEY_F1;
|
|
|
|
_glfw.ns.keycodes[0x78] = GLFW_KEY_F2;
|
|
|
|
_glfw.ns.keycodes[0x63] = GLFW_KEY_F3;
|
|
|
|
_glfw.ns.keycodes[0x76] = GLFW_KEY_F4;
|
|
|
|
_glfw.ns.keycodes[0x60] = GLFW_KEY_F5;
|
|
|
|
_glfw.ns.keycodes[0x61] = GLFW_KEY_F6;
|
|
|
|
_glfw.ns.keycodes[0x62] = GLFW_KEY_F7;
|
|
|
|
_glfw.ns.keycodes[0x64] = GLFW_KEY_F8;
|
|
|
|
_glfw.ns.keycodes[0x65] = GLFW_KEY_F9;
|
|
|
|
_glfw.ns.keycodes[0x6D] = GLFW_KEY_F10;
|
|
|
|
_glfw.ns.keycodes[0x67] = GLFW_KEY_F11;
|
|
|
|
_glfw.ns.keycodes[0x6F] = GLFW_KEY_F12;
|
|
|
|
_glfw.ns.keycodes[0x69] = GLFW_KEY_F13;
|
|
|
|
_glfw.ns.keycodes[0x6B] = GLFW_KEY_F14;
|
|
|
|
_glfw.ns.keycodes[0x71] = GLFW_KEY_F15;
|
|
|
|
_glfw.ns.keycodes[0x6A] = GLFW_KEY_F16;
|
|
|
|
_glfw.ns.keycodes[0x40] = GLFW_KEY_F17;
|
|
|
|
_glfw.ns.keycodes[0x4F] = GLFW_KEY_F18;
|
|
|
|
_glfw.ns.keycodes[0x50] = GLFW_KEY_F19;
|
|
|
|
_glfw.ns.keycodes[0x5A] = GLFW_KEY_F20;
|
|
|
|
_glfw.ns.keycodes[0x73] = GLFW_KEY_HOME;
|
|
|
|
_glfw.ns.keycodes[0x72] = GLFW_KEY_INSERT;
|
|
|
|
_glfw.ns.keycodes[0x7B] = GLFW_KEY_LEFT;
|
|
|
|
_glfw.ns.keycodes[0x3A] = GLFW_KEY_LEFT_ALT;
|
|
|
|
_glfw.ns.keycodes[0x3B] = GLFW_KEY_LEFT_CONTROL;
|
|
|
|
_glfw.ns.keycodes[0x38] = GLFW_KEY_LEFT_SHIFT;
|
|
|
|
_glfw.ns.keycodes[0x37] = GLFW_KEY_LEFT_SUPER;
|
|
|
|
_glfw.ns.keycodes[0x6E] = GLFW_KEY_MENU;
|
|
|
|
_glfw.ns.keycodes[0x47] = GLFW_KEY_NUM_LOCK;
|
|
|
|
_glfw.ns.keycodes[0x79] = GLFW_KEY_PAGE_DOWN;
|
|
|
|
_glfw.ns.keycodes[0x74] = GLFW_KEY_PAGE_UP;
|
|
|
|
_glfw.ns.keycodes[0x7C] = GLFW_KEY_RIGHT;
|
|
|
|
_glfw.ns.keycodes[0x3D] = GLFW_KEY_RIGHT_ALT;
|
|
|
|
_glfw.ns.keycodes[0x3E] = GLFW_KEY_RIGHT_CONTROL;
|
|
|
|
_glfw.ns.keycodes[0x3C] = GLFW_KEY_RIGHT_SHIFT;
|
|
|
|
_glfw.ns.keycodes[0x36] = GLFW_KEY_RIGHT_SUPER;
|
|
|
|
_glfw.ns.keycodes[0x31] = GLFW_KEY_SPACE;
|
|
|
|
_glfw.ns.keycodes[0x30] = GLFW_KEY_TAB;
|
|
|
|
_glfw.ns.keycodes[0x7E] = GLFW_KEY_UP;
|
|
|
|
|
|
|
|
_glfw.ns.keycodes[0x52] = GLFW_KEY_KP_0;
|
|
|
|
_glfw.ns.keycodes[0x53] = GLFW_KEY_KP_1;
|
|
|
|
_glfw.ns.keycodes[0x54] = GLFW_KEY_KP_2;
|
|
|
|
_glfw.ns.keycodes[0x55] = GLFW_KEY_KP_3;
|
|
|
|
_glfw.ns.keycodes[0x56] = GLFW_KEY_KP_4;
|
|
|
|
_glfw.ns.keycodes[0x57] = GLFW_KEY_KP_5;
|
|
|
|
_glfw.ns.keycodes[0x58] = GLFW_KEY_KP_6;
|
|
|
|
_glfw.ns.keycodes[0x59] = GLFW_KEY_KP_7;
|
|
|
|
_glfw.ns.keycodes[0x5B] = GLFW_KEY_KP_8;
|
|
|
|
_glfw.ns.keycodes[0x5C] = GLFW_KEY_KP_9;
|
|
|
|
_glfw.ns.keycodes[0x45] = GLFW_KEY_KP_ADD;
|
|
|
|
_glfw.ns.keycodes[0x41] = GLFW_KEY_KP_DECIMAL;
|
|
|
|
_glfw.ns.keycodes[0x4B] = GLFW_KEY_KP_DIVIDE;
|
|
|
|
_glfw.ns.keycodes[0x4C] = GLFW_KEY_KP_ENTER;
|
|
|
|
_glfw.ns.keycodes[0x51] = GLFW_KEY_KP_EQUAL;
|
|
|
|
_glfw.ns.keycodes[0x43] = GLFW_KEY_KP_MULTIPLY;
|
|
|
|
_glfw.ns.keycodes[0x4E] = GLFW_KEY_KP_SUBTRACT;
|
2015-07-02 08:24:50 -04:00
|
|
|
|
|
|
|
for (scancode = 0; scancode < 256; scancode++)
|
|
|
|
{
|
|
|
|
// Store the reverse translation for faster key name lookup
|
2016-09-07 09:43:39 -04:00
|
|
|
if (_glfw.ns.keycodes[scancode] >= 0)
|
|
|
|
_glfw.ns.scancodes[_glfw.ns.keycodes[scancode]] = scancode;
|
2015-07-02 08:24:50 -04:00
|
|
|
}
|
2014-03-30 10:23:22 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 15:33:15 -04:00
|
|
|
// Retrieve Unicode data for the current keyboard layout
|
|
|
|
//
|
|
|
|
static GLFWbool updateUnicodeDataNS(void)
|
|
|
|
{
|
|
|
|
if (_glfw.ns.inputSource)
|
|
|
|
{
|
|
|
|
CFRelease(_glfw.ns.inputSource);
|
|
|
|
_glfw.ns.inputSource = NULL;
|
|
|
|
_glfw.ns.unicodeData = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
_glfw.ns.inputSource = TISCopyCurrentKeyboardLayoutInputSource();
|
|
|
|
if (!_glfw.ns.inputSource)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to retrieve keyboard layout input source");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
2017-09-17 08:39:17 -04:00
|
|
|
_glfw.ns.unicodeData =
|
|
|
|
TISGetInputSourceProperty(_glfw.ns.inputSource,
|
|
|
|
kTISPropertyUnicodeKeyLayoutData);
|
2016-05-24 15:33:15 -04:00
|
|
|
if (!_glfw.ns.unicodeData)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to retrieve keyboard layout Unicode data");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GLFW_TRUE;
|
|
|
|
}
|
|
|
|
|
2016-05-23 06:46:57 -04:00
|
|
|
// Load HIToolbox.framework and the TIS symbols we need from it
|
|
|
|
//
|
|
|
|
static GLFWbool initializeTIS(void)
|
|
|
|
{
|
2016-05-24 06:32:38 -04:00
|
|
|
// This works only because Cocoa has already loaded it properly
|
2017-09-17 08:39:17 -04:00
|
|
|
_glfw.ns.tis.bundle =
|
|
|
|
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.HIToolbox"));
|
2016-05-23 06:46:57 -04:00
|
|
|
if (!_glfw.ns.tis.bundle)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to load HIToolbox.framework");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFStringRef* kPropertyUnicodeKeyLayoutData =
|
|
|
|
CFBundleGetDataPointerForName(_glfw.ns.tis.bundle,
|
|
|
|
CFSTR("kTISPropertyUnicodeKeyLayoutData"));
|
|
|
|
_glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource =
|
|
|
|
CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
|
|
|
|
CFSTR("TISCopyCurrentKeyboardLayoutInputSource"));
|
|
|
|
_glfw.ns.tis.GetInputSourceProperty =
|
|
|
|
CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
|
|
|
|
CFSTR("TISGetInputSourceProperty"));
|
|
|
|
_glfw.ns.tis.GetKbdType =
|
|
|
|
CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
|
|
|
|
CFSTR("LMGetKbdType"));
|
|
|
|
|
|
|
|
if (!kPropertyUnicodeKeyLayoutData ||
|
|
|
|
!TISCopyCurrentKeyboardLayoutInputSource ||
|
|
|
|
!TISGetInputSourceProperty ||
|
|
|
|
!LMGetKbdType)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to load TIS API symbols");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:33:15 -04:00
|
|
|
_glfw.ns.tis.kPropertyUnicodeKeyLayoutData =
|
|
|
|
*kPropertyUnicodeKeyLayoutData;
|
2016-05-23 06:46:57 -04:00
|
|
|
|
2016-05-24 15:33:15 -04:00
|
|
|
return updateUnicodeDataNS();
|
|
|
|
}
|
2016-05-23 06:46:57 -04:00
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
@interface GLFWHelper : NSObject
|
2016-05-24 15:33:15 -04:00
|
|
|
@end
|
2016-05-23 06:46:57 -04:00
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
@implementation GLFWHelper
|
2016-05-23 06:46:57 -04:00
|
|
|
|
2016-05-24 15:33:15 -04:00
|
|
|
- (void)selectedKeyboardInputSourceChanged:(NSObject* )object
|
|
|
|
{
|
|
|
|
updateUnicodeDataNS();
|
2016-05-23 06:46:57 -04:00
|
|
|
}
|
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
- (void)doNothing:(id)object
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@end // GLFWHelper
|
2016-05-24 15:33:15 -04:00
|
|
|
|
2019-01-10 19:05:00 -05:00
|
|
|
@interface GLFWApplicationDelegate : NSObject <NSApplicationDelegate>
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GLFWApplicationDelegate
|
|
|
|
|
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
|
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
|
|
|
|
|
|
|
for (window = _glfw.windowListHead; window; window = window->next)
|
|
|
|
_glfwInputWindowCloseRequest(window);
|
|
|
|
|
|
|
|
return NSTerminateCancel;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidChangeScreenParameters:(NSNotification *) notification
|
|
|
|
{
|
|
|
|
_GLFWwindow* window;
|
|
|
|
|
|
|
|
for (window = _glfw.windowListHead; window; window = window->next)
|
|
|
|
{
|
|
|
|
if (window->context.client != GLFW_NO_API)
|
|
|
|
[window->context.nsgl.object update];
|
|
|
|
}
|
|
|
|
|
|
|
|
_glfwPollMonitorsNS();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillFinishLaunching:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
if (_glfw.hints.init.ns.menubar)
|
|
|
|
{
|
|
|
|
// In case we are unbundled, make us a proper UI application
|
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
|
|
|
|
|
|
// Menu bar setup must go between sharedApplication above and
|
|
|
|
// finishLaunching below, in order to properly emulate the behavior
|
|
|
|
// of NSApplicationMain
|
|
|
|
|
|
|
|
if ([[NSBundle mainBundle] pathForResource:@"MainMenu" ofType:@"nib"])
|
|
|
|
{
|
|
|
|
[[NSBundle mainBundle] loadNibNamed:@"MainMenu"
|
|
|
|
owner:NSApp
|
|
|
|
topLevelObjects:&_glfw.ns.nibObjects];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
createMenuBar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[NSApp stop:nil];
|
|
|
|
|
|
|
|
_glfwPlatformPostEmptyEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidHide:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < _glfw.monitorCount; i++)
|
|
|
|
_glfwRestoreVideoModeNS(_glfw.monitors[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end // GLFWApplicationDelegate
|
|
|
|
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2010-09-15 21:25:36 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2010-09-15 12:57:25 -04:00
|
|
|
int _glfwPlatformInit(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2019-03-05 13:05:57 -05:00
|
|
|
@autoreleasepool {
|
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
_glfw.ns.helper = [[GLFWHelper alloc] init];
|
|
|
|
|
|
|
|
[NSThread detachNewThreadSelector:@selector(doNothing:)
|
|
|
|
toTarget:_glfw.ns.helper
|
|
|
|
withObject:nil];
|
|
|
|
|
2019-02-24 20:41:05 -05:00
|
|
|
if (NSApp)
|
|
|
|
_glfw.ns.finishedLaunching = GLFW_TRUE;
|
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
[NSApplication sharedApplication];
|
|
|
|
|
2019-01-10 19:05:00 -05:00
|
|
|
_glfw.ns.delegate = [[GLFWApplicationDelegate alloc] init];
|
|
|
|
if (_glfw.ns.delegate == nil)
|
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Cocoa: Failed to create application delegate");
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
[NSApp setDelegate:_glfw.ns.delegate];
|
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
NSEvent* (^block)(NSEvent*) = ^ NSEvent* (NSEvent* event)
|
|
|
|
{
|
|
|
|
if ([event modifierFlags] & NSEventModifierFlagCommand)
|
|
|
|
[[NSApp keyWindow] sendEvent:event];
|
|
|
|
|
|
|
|
return event;
|
|
|
|
};
|
|
|
|
|
|
|
|
_glfw.ns.keyUpMonitor =
|
|
|
|
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp
|
|
|
|
handler:block];
|
2012-03-05 10:26:15 -05:00
|
|
|
|
2017-02-14 09:43:31 -05:00
|
|
|
if (_glfw.hints.init.ns.chdir)
|
|
|
|
changeToResourcesDirectory();
|
|
|
|
|
2019-01-10 19:05:00 -05:00
|
|
|
// Press and Hold prevents some keys from emitting repeated characters
|
|
|
|
NSDictionary* defaults = @{@"ApplePressAndHoldEnabled":@NO};
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
|
|
|
|
|
2016-10-14 10:56:45 -04:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2018-12-24 17:31:58 -05:00
|
|
|
addObserver:_glfw.ns.helper
|
2016-05-24 15:33:15 -04:00
|
|
|
selector:@selector(selectedKeyboardInputSourceChanged:)
|
2016-10-14 10:56:45 -04:00
|
|
|
name:NSTextInputContextKeyboardSelectionDidChangeNotification
|
2016-05-24 15:33:15 -04:00
|
|
|
object:nil];
|
|
|
|
|
2014-03-30 10:23:22 -04:00
|
|
|
createKeyTables();
|
|
|
|
|
2013-01-01 19:40:42 -05:00
|
|
|
_glfw.ns.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
|
|
|
|
if (!_glfw.ns.eventSource)
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2012-03-20 10:23:35 -04:00
|
|
|
|
2013-01-01 19:40:42 -05:00
|
|
|
CGEventSourceSetLocalEventsSuppressionInterval(_glfw.ns.eventSource, 0.0);
|
2012-03-20 10:23:35 -04:00
|
|
|
|
2016-05-23 06:46:57 -04:00
|
|
|
if (!initializeTIS())
|
2015-07-02 08:24:50 -04:00
|
|
|
return GLFW_FALSE;
|
|
|
|
|
2015-12-03 12:16:46 -05:00
|
|
|
_glfwInitTimerNS();
|
|
|
|
_glfwInitJoysticksNS();
|
2013-06-24 08:38:00 -04:00
|
|
|
|
2016-12-04 19:19:48 -05:00
|
|
|
_glfwPollMonitorsNS();
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_TRUE;
|
2019-03-05 13:05:57 -05:00
|
|
|
|
|
|
|
} // autoreleasepool
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2012-12-29 19:15:48 -05:00
|
|
|
void _glfwPlatformTerminate(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2019-03-05 13:05:57 -05:00
|
|
|
@autoreleasepool {
|
|
|
|
|
2015-07-02 08:24:50 -04:00
|
|
|
if (_glfw.ns.inputSource)
|
|
|
|
{
|
|
|
|
CFRelease(_glfw.ns.inputSource);
|
|
|
|
_glfw.ns.inputSource = NULL;
|
2016-05-24 15:33:15 -04:00
|
|
|
_glfw.ns.unicodeData = nil;
|
2015-07-02 08:24:50 -04:00
|
|
|
}
|
|
|
|
|
2013-01-01 19:40:42 -05:00
|
|
|
if (_glfw.ns.eventSource)
|
2012-03-20 10:23:35 -04:00
|
|
|
{
|
2013-01-01 19:40:42 -05:00
|
|
|
CFRelease(_glfw.ns.eventSource);
|
|
|
|
_glfw.ns.eventSource = NULL;
|
2012-03-20 10:23:35 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 11:49:12 -05:00
|
|
|
if (_glfw.ns.delegate)
|
2014-12-16 16:34:15 -05:00
|
|
|
{
|
|
|
|
[NSApp setDelegate:nil];
|
2015-01-04 11:49:12 -05:00
|
|
|
[_glfw.ns.delegate release];
|
|
|
|
_glfw.ns.delegate = nil;
|
2014-12-16 16:34:15 -05:00
|
|
|
}
|
2010-09-15 21:05:55 -04:00
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
if (_glfw.ns.helper)
|
2016-05-24 15:33:15 -04:00
|
|
|
{
|
2016-10-14 10:56:45 -04:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2018-12-24 17:31:58 -05:00
|
|
|
removeObserver:_glfw.ns.helper
|
2016-10-14 10:56:45 -04:00
|
|
|
name:NSTextInputContextKeyboardSelectionDidChangeNotification
|
2016-05-24 15:33:15 -04:00
|
|
|
object:nil];
|
2016-10-14 10:56:45 -04:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2018-12-24 17:31:58 -05:00
|
|
|
removeObserver:_glfw.ns.helper];
|
|
|
|
[_glfw.ns.helper release];
|
|
|
|
_glfw.ns.helper = nil;
|
2016-05-24 15:33:15 -04:00
|
|
|
}
|
|
|
|
|
2018-12-24 17:31:58 -05:00
|
|
|
if (_glfw.ns.keyUpMonitor)
|
|
|
|
[NSEvent removeMonitor:_glfw.ns.keyUpMonitor];
|
|
|
|
|
2013-06-24 08:08:50 -04:00
|
|
|
free(_glfw.ns.clipboardString);
|
|
|
|
|
2015-12-03 12:16:46 -05:00
|
|
|
_glfwTerminateNSGL();
|
|
|
|
_glfwTerminateJoysticksNS();
|
2016-03-01 13:36:05 -05:00
|
|
|
|
2019-03-05 13:05:57 -05:00
|
|
|
} // autoreleasepool
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2010-09-15 12:57:25 -04:00
|
|
|
const char* _glfwPlatformGetVersionString(void)
|
|
|
|
{
|
2019-04-08 14:51:05 -04:00
|
|
|
return _GLFW_VERSION_NUMBER " Cocoa NSGL EGL OSMesa"
|
2013-02-14 07:13:07 -05:00
|
|
|
#if defined(_GLFW_BUILD_DLL)
|
|
|
|
" dynamic"
|
2012-03-29 08:06:26 -04:00
|
|
|
#endif
|
|
|
|
;
|
2010-09-15 12:57:25 -04:00
|
|
|
}
|
|
|
|
|