21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_COCOA
27 #include "../../events/SDL_events_c.h"
32 #ifndef kIOPMAssertPreventUserIdleDisplaySleep
33 #define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
36 @interface SDLApplication : NSApplication
39 - (
void)sendEvent:(NSEvent *)theEvent;
41 + (
void)registerUserDefaults;
45 @implementation SDLApplication
55 static void Cocoa_DispatchEvent(NSEvent *theEvent)
59 switch ([theEvent
type]) {
60 case NSEventTypeLeftMouseDown:
61 case NSEventTypeOtherMouseDown:
62 case NSEventTypeRightMouseDown:
63 case NSEventTypeLeftMouseUp:
64 case NSEventTypeOtherMouseUp:
65 case NSEventTypeRightMouseUp:
66 case NSEventTypeLeftMouseDragged:
67 case NSEventTypeRightMouseDragged:
68 case NSEventTypeOtherMouseDragged:
69 case NSEventTypeMouseMoved:
70 case NSEventTypeScrollWheel:
73 case NSEventTypeKeyDown:
74 case NSEventTypeKeyUp:
75 case NSEventTypeFlagsChanged:
86 - (
void)sendEvent:(NSEvent *)theEvent
88 if (s_bShouldHandleEventsInSDLApplication) {
89 Cocoa_DispatchEvent(theEvent);
92 [
super sendEvent:theEvent];
95 + (
void)registerUserDefaults
97 NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
98 [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
99 [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
100 [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
102 [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
103 [appDefaults release];
109 @interface NSApplication(NSAppleMenu)
110 - (
void)setAppleMenu:(NSMenu *)menu;
113 @interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
115 BOOL seenFirstActivate;
121 @implementation SDLAppDelegate : NSObject
126 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
128 seenFirstActivate = NO;
130 [center addObserver:self
131 selector:@selector(windowWillClose:)
132 name:NSWindowWillCloseNotification
135 [center addObserver:self
136 selector:@selector(focusSomeWindow:)
137 name:NSApplicationDidBecomeActiveNotification
146 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
148 [center removeObserver:self name:NSWindowWillCloseNotification object:nil];
149 [center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
154 - (
void)windowWillClose:(NSNotification *)notification;
156 NSWindow *win = (NSWindow*)[notification
object];
158 if (![win isKeyWindow]) {
171 for (NSWindow *
window in [NSApp orderedWindows]) {
173 if (![
window isOnActiveSpace]) {
185 for (NSNumber *
num in [NSWindow windowNumbersWithOptions:0]) {
188 [window makeKeyAndOrderFront:self];
194 - (
void)focusSomeWindow:(NSNotification *)aNotification
201 if (!seenFirstActivate) {
202 seenFirstActivate = YES;
210 for (
i = 0;
i <
device->num_displays; ++
i) {
212 if (fullscreen_window) {
214 SDL_RestoreWindow(fullscreen_window);
221 SDL_RestoreWindow(window);
223 SDL_RaiseWindow(window);
228 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
233 - (
void)applicationDidFinishLaunching:(NSNotification *)notification
241 [NSApp activateIgnoringOtherApps:YES];
246 [SDLApplication registerUserDefaults];
250 static SDLAppDelegate *appDelegate = nil;
253 GetApplicationName(
void)
258 appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
260 appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
264 appName = [[NSProcessInfo processInfo] processName];
271 CreateApplicationMenus(
void)
278 NSMenuItem *menuItem;
285 mainMenu = [[NSMenu alloc] init];
288 [NSApp setMainMenu:mainMenu];
294 appName = GetApplicationName();
295 appleMenu = [[NSMenu alloc] initWithTitle:@""];
298 title = [@"About " stringByAppendingString:appName];
299 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
301 [appleMenu addItem:[NSMenuItem separatorItem]];
303 [appleMenu addItemWithTitle:@"Preferencesâ €¦" action:nil keyEquivalent:@","];
305 [appleMenu addItem:[NSMenuItem separatorItem]];
307 serviceMenu = [[NSMenu alloc] initWithTitle:@""];
308 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Services" action:nil keyEquivalent:
@""];
309 [menuItem setSubmenu:serviceMenu];
311 [NSApp setServicesMenu:serviceMenu];
312 [serviceMenu release];
314 [appleMenu addItem:[NSMenuItem separatorItem]];
316 title = [@"Hide " stringByAppendingString:appName];
317 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
319 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Hide Others" action:
@selector(hideOtherApplications:) keyEquivalent:
@"h"];
320 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
322 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
324 [appleMenu addItem:[NSMenuItem separatorItem]];
326 title = [@"Quit " stringByAppendingString:appName];
327 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
330 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
331 [menuItem setSubmenu:appleMenu];
332 [[NSApp mainMenu] addItem:menuItem];
336 [NSApp setAppleMenu:appleMenu];
341 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
344 [windowMenu addItemWithTitle:@"Close" action:@selector(performClose:) keyEquivalent:@"w"];
346 [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
348 [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
351 if (
floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
355 menuItem = [[NSMenuItem alloc] initWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
356 [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
357 [windowMenu addItem:menuItem];
362 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
363 [menuItem setSubmenu:windowMenu];
364 [[NSApp mainMenu] addItem:menuItem];
368 [NSApp setWindowsMenu:windowMenu];
369 [windowMenu release];
379 [SDLApplication sharedApplication];
382 s_bShouldHandleEventsInSDLApplication =
SDL_TRUE;
385 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
388 if ([NSApp mainMenu] == nil) {
389 CreateApplicationMenus();
391 [NSApp finishLaunching];
392 if ([NSApp delegate]) {
396 [SDLApplication registerUserDefaults];
399 if (NSApp && !appDelegate) {
400 appDelegate = [[SDLAppDelegate alloc] init];
405 if (![NSApp delegate]) {
406 [(NSApplication *)NSApp setDelegate:appDelegate];
408 appDelegate->seenFirstActivate = YES;
417 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
422 if (!
data->screensaver_activity ||
424 UpdateSystemActivity(UsrActivity);
425 data->screensaver_activity = now;
431 NSEvent *
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
432 if (
event == nil ) {
436 if (!s_bShouldHandleEventsInSDLApplication) {
437 Cocoa_DispatchEvent(
event);
441 [NSApp sendEvent:event];
451 if (!
data->screensaver_use_iopm) {
455 if (
data->screensaver_assertion) {
456 IOPMAssertionRelease(
data->screensaver_assertion);
457 data->screensaver_assertion = 0;
466 NSString *
name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
467 IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
470 &
data->screensaver_assertion);