33#include <crt_externs.h>
37static void changeToResourcesDirectory(
void)
39 char resourcesPath[MAXPATHLEN];
41 CFBundleRef bundle = CFBundleGetMainBundle();
45 CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(bundle);
47 CFStringRef last = CFURLCopyLastPathComponent(resourcesURL);
48 if (CFStringCompare(CFSTR(
"Resources"), last, 0) != kCFCompareEqualTo)
51 CFRelease(resourcesURL);
57 if (!CFURLGetFileSystemRepresentation(resourcesURL,
59 (UInt8*) resourcesPath,
62 CFRelease(resourcesURL);
66 CFRelease(resourcesURL);
76static void createMenuBar(
void)
79 NSString* appName = nil;
80 NSDictionary* bundleInfo = [[NSBundle mainBundle] infoDictionary];
81 NSString* nameKeys[] =
83 @"CFBundleDisplayName",
85 @"CFBundleExecutable",
90 for (i = 0; i <
sizeof(nameKeys) /
sizeof(nameKeys[0]); i++)
92 id name = bundleInfo[nameKeys[i]];
94 [name isKindOfClass:[NSString
class]] &&
95 ![name isEqualToString:
@""])
104 char** progname = _NSGetProgname();
105 if (progname && *progname)
106 appName = @(*progname);
108 appName =
@"GLFW Application";
111 NSMenu* bar = [[NSMenu alloc] init];
112 [NSApp setMainMenu:bar];
114 NSMenuItem* appMenuItem =
115 [bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
116 NSMenu* appMenu = [[NSMenu alloc] init];
117 [appMenuItem setSubmenu:appMenu];
119 [appMenu addItemWithTitle:[NSString stringWithFormat:@"About %@", appName]
120 action:@selector(orderFrontStandardAboutPanel:)
122 [appMenu addItem:[NSMenuItem separatorItem]];
123 NSMenu* servicesMenu = [[NSMenu alloc] init];
124 [NSApp setServicesMenu:servicesMenu];
125 [[appMenu addItemWithTitle:@"Services"
127 keyEquivalent:@""] setSubmenu:servicesMenu];
128 [servicesMenu release];
129 [appMenu addItem:[NSMenuItem separatorItem]];
130 [appMenu addItemWithTitle:[NSString stringWithFormat:@"Hide %@", appName]
131 action:@selector(hide:)
133 [[appMenu addItemWithTitle:@"Hide Others"
134 action:@selector(hideOtherApplications:)
136 setKeyEquivalentModifierMask:NSEventModifierFlagOption | NSEventModifierFlagCommand];
137 [appMenu addItemWithTitle:@"Show All"
138 action:@selector(unhideAllApplications:)
140 [appMenu addItem:[NSMenuItem separatorItem]];
141 [appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %@", appName]
142 action:@selector(terminate:)
145 NSMenuItem* windowMenuItem =
146 [bar addItemWithTitle:@"" action:NULL keyEquivalent:@""];
148 NSMenu* windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
149 [NSApp setWindowsMenu:windowMenu];
150 [windowMenuItem setSubmenu:windowMenu];
152 [windowMenu addItemWithTitle:@"Minimize"
153 action:@selector(performMiniaturize:)
155 [windowMenu addItemWithTitle:@"Zoom"
156 action:@selector(performZoom:)
158 [windowMenu addItem:[NSMenuItem separatorItem]];
159 [windowMenu addItemWithTitle:@"Bring All to Front"
160 action:@selector(arrangeInFront:)
164 [windowMenu addItem:[NSMenuItem separatorItem]];
165 [[windowMenu addItemWithTitle:@"Enter Full Screen"
166 action:@selector(toggleFullScreen:)
168 setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
172 SEL setAppleMenuSelector = NSSelectorFromString(
@"setAppleMenu:");
173 [NSApp performSelector:setAppleMenuSelector withObject:appMenu];
178static void createKeyTables(
void)
182 memset(
_glfw.ns.keycodes, -1,
sizeof(
_glfw.ns.keycodes));
183 memset(
_glfw.ns.scancodes, -1,
sizeof(
_glfw.ns.scancodes));
300 for (scancode = 0; scancode < 256; scancode++)
303 if (
_glfw.ns.keycodes[scancode] >= 0)
304 _glfw.ns.scancodes[
_glfw.ns.keycodes[scancode]] = scancode;
310static GLFWbool updateUnicodeDataNS(
void)
312 if (
_glfw.ns.inputSource)
314 CFRelease(
_glfw.ns.inputSource);
316 _glfw.ns.unicodeData = nil;
320 if (!
_glfw.ns.inputSource)
323 "Cocoa: Failed to retrieve keyboard layout input source");
327 _glfw.ns.unicodeData =
330 if (!
_glfw.ns.unicodeData)
333 "Cocoa: Failed to retrieve keyboard layout Unicode data");
345 _glfw.ns.tis.bundle =
346 CFBundleGetBundleWithIdentifier(CFSTR(
"com.apple.HIToolbox"));
347 if (!
_glfw.ns.tis.bundle)
350 "Cocoa: Failed to load HIToolbox.framework");
354 CFStringRef* kPropertyUnicodeKeyLayoutData =
355 CFBundleGetDataPointerForName(
_glfw.ns.tis.bundle,
356 CFSTR(
"kTISPropertyUnicodeKeyLayoutData"));
357 _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource =
358 CFBundleGetFunctionPointerForName(
_glfw.ns.tis.bundle,
359 CFSTR(
"TISCopyCurrentKeyboardLayoutInputSource"));
360 _glfw.ns.tis.GetInputSourceProperty =
361 CFBundleGetFunctionPointerForName(
_glfw.ns.tis.bundle,
362 CFSTR(
"TISGetInputSourceProperty"));
363 _glfw.ns.tis.GetKbdType =
364 CFBundleGetFunctionPointerForName(
_glfw.ns.tis.bundle,
365 CFSTR(
"LMGetKbdType"));
367 if (!kPropertyUnicodeKeyLayoutData ||
373 "Cocoa: Failed to load TIS API symbols");
377 _glfw.ns.tis.kPropertyUnicodeKeyLayoutData =
378 *kPropertyUnicodeKeyLayoutData;
380 return updateUnicodeDataNS();
388- (void)selectedKeyboardInputSourceChanged:(NSObject* )
object
390 updateUnicodeDataNS();
393- (void)doNothing:(
id)
object
404- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
411 return NSTerminateCancel;
414- (void)applicationDidChangeScreenParameters:(NSNotification *) notification
421 [window->context.nsgl.object update];
427- (void)applicationWillFinishLaunching:(NSNotification *)notification
434 if ([[NSBundle mainBundle] pathForResource:
@"MainMenu" ofType:
@"nib"])
436 [[NSBundle mainBundle] loadNibNamed:@"MainMenu"
438 topLevelObjects:&_glfw.ns.nibObjects];
445- (void)applicationDidFinishLaunching:(NSNotification *)notification
451- (void)applicationDidHide:(NSNotification *)notification
468 CFBundleRef bundle = CFBundleGetMainBundle();
473 CFBundleCopyAuxiliaryExecutableURL(bundle, CFSTR(
"libvulkan.1.dylib"));
480 if (CFURLGetFileSystemRepresentation(url,
true, (UInt8*) path,
sizeof(path) - 1))
498 [NSThread detachNewThreadSelector:@selector(doNothing:)
499 toTarget:_glfw.ns.helper
502 [NSApplication sharedApplication];
505 if (
_glfw.ns.delegate == nil)
508 "Cocoa: Failed to create application delegate");
512 [NSApp setDelegate:_glfw.ns.delegate];
514 NSEvent* (^block)(NSEvent*) = ^ NSEvent* (NSEvent* event)
517 [[NSApp keyWindow] sendEvent:event];
522 _glfw.ns.keyUpMonitor =
523 [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyUp
527 changeToResourcesDirectory();
530 NSDictionary* defaults = @{
@"ApplePressAndHoldEnabled":@NO};
531 [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
533 [[NSNotificationCenter defaultCenter]
534 addObserver:_glfw.ns.helper
535 selector:@selector(selectedKeyboardInputSourceChanged:)
536 name:NSTextInputContextKeyboardSelectionDidChangeNotification
541 _glfw.ns.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
542 if (!
_glfw.ns.eventSource)
545 CGEventSourceSetLocalEventsSuppressionInterval(
_glfw.ns.eventSource, 0.0);
547 if (!initializeTIS())
554 if (![[NSRunningApplication currentApplication] isFinishedLaunching])
559 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
570 if (
_glfw.ns.inputSource)
572 CFRelease(
_glfw.ns.inputSource);
574 _glfw.ns.unicodeData = nil;
577 if (
_glfw.ns.eventSource)
579 CFRelease(
_glfw.ns.eventSource);
583 if (
_glfw.ns.delegate)
585 [NSApp setDelegate:nil];
586 [_glfw.ns.delegate release];
587 _glfw.ns.delegate = nil;
592 [[NSNotificationCenter defaultCenter]
593 removeObserver:_glfw.ns.helper
594 name:NSTextInputContextKeyboardSelectionDidChangeNotification
596 [[NSNotificationCenter defaultCenter]
597 removeObserver:_glfw.ns.helper];
598 [_glfw.ns.helper release];
599 _glfw.ns.helper = nil;
602 if (
_glfw.ns.keyUpMonitor)
603 [NSEvent removeMonitor:_glfw.ns.keyUpMonitor];
605 free(
_glfw.ns.clipboardString);
615#if defined(_GLFW_BUILD_DLL)
void * _glfwLoadLocalVulkanLoaderNS(void)
const char * _glfwPlatformGetVersionString(void)
void _glfwPlatformTerminate(void)
int _glfwPlatformInit(void)
void _glfwRestoreVideoModeNS(_GLFWmonitor *monitor)
void _glfwPollMonitorsNS(void)
void _glfwPlatformPostEmptyEvent(void)
#define GLFW_PLATFORM_ERROR
A platform-specific error occurred that does not match any of the more specific categories.
#define GLFW_KEY_NUM_LOCK
#define GLFW_KEY_KP_DECIMAL
#define GLFW_KEY_KP_ENTER
#define GLFW_KEY_APOSTROPHE
#define GLFW_KEY_RIGHT_ALT
#define GLFW_KEY_BACKSPACE
#define GLFW_KEY_GRAVE_ACCENT
#define GLFW_KEY_LEFT_ALT
#define GLFW_KEY_SEMICOLON
#define GLFW_KEY_RIGHT_BRACKET
#define GLFW_KEY_LEFT_SHIFT
#define GLFW_KEY_CAPS_LOCK
#define GLFW_KEY_KP_MULTIPLY
#define GLFW_KEY_LEFT_CONTROL
#define GLFW_KEY_KP_SUBTRACT
#define GLFW_KEY_BACKSLASH
#define GLFW_KEY_KP_DIVIDE
#define GLFW_KEY_LEFT_BRACKET
#define GLFW_KEY_RIGHT_CONTROL
#define GLFW_KEY_RIGHT_SUPER
#define GLFW_KEY_KP_EQUAL
#define GLFW_KEY_PAGE_DOWN
#define GLFW_KEY_PRINT_SCREEN
#define GLFW_KEY_LEFT_SUPER
#define GLFW_KEY_RIGHT_SHIFT
void _glfwInputError(int code, const char *format,...)
#define _GLFW_VERSION_NUMBER
void _glfwInputWindowCloseRequest(_GLFWwindow *window)
void _glfwTerminateNSGL(void)
struct _GLFWinitconfig::@16 ns
_GLFWwindow * windowListHead
struct _GLFWlibrary::@23 hints
struct _GLFWwindow * next