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]) {
187 if (window && window != win && [window canBecomeKeyWindow]) {
188 [window makeKeyAndOrderFront:self];
194 - (
void)focusSomeWindow:(NSNotification *)aNotification
201 if (!seenFirstActivate) {
202 seenFirstActivate = YES;
207 if (device && device->
windows) {
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)
279 NSMenuItem *menuItem;
286 mainMenu = [[NSMenu alloc] init];
289 [NSApp setMainMenu:mainMenu];
295 appName = GetApplicationName();
296 appleMenu = [[NSMenu alloc] initWithTitle:@""];
299 title = [@"About " stringByAppendingString:appName];
300 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
302 [appleMenu addItem:[NSMenuItem separatorItem]];
304 [appleMenu addItemWithTitle:@"Preferencesâ €¦" action:nil keyEquivalent:@","];
306 [appleMenu addItem:[NSMenuItem separatorItem]];
308 serviceMenu = [[NSMenu alloc] initWithTitle:@""];
309 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Services" action:nil keyEquivalent:
@""];
310 [menuItem setSubmenu:serviceMenu];
312 [NSApp setServicesMenu:serviceMenu];
313 [serviceMenu release];
315 [appleMenu addItem:[NSMenuItem separatorItem]];
317 title = [@"Hide " stringByAppendingString:appName];
318 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
320 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Hide Others" action:
@selector(hideOtherApplications:) keyEquivalent:
@"h"];
321 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
323 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
325 [appleMenu addItem:[NSMenuItem separatorItem]];
327 title = [@"Quit " stringByAppendingString:appName];
328 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
331 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
332 [menuItem setSubmenu:appleMenu];
333 [[NSApp mainMenu] addItem:menuItem];
337 [NSApp setAppleMenu:appleMenu];
342 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
345 [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
347 [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
350 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
351 [menuItem setSubmenu:windowMenu];
352 [[NSApp mainMenu] addItem:menuItem];
356 [NSApp setWindowsMenu:windowMenu];
357 [windowMenu release];
361 if (
floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
363 viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
366 menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
367 [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
370 menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
371 [menuItem setSubmenu:viewMenu];
372 [[NSApp mainMenu] addItem:menuItem];
386 [SDLApplication sharedApplication];
389 s_bShouldHandleEventsInSDLApplication =
SDL_TRUE;
392 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
395 if ([NSApp mainMenu] == nil) {
396 CreateApplicationMenus();
398 [NSApp finishLaunching];
399 if ([NSApp delegate]) {
403 [SDLApplication registerUserDefaults];
406 if (NSApp && !appDelegate) {
407 appDelegate = [[SDLAppDelegate alloc] init];
412 if (![NSApp delegate]) {
413 [(NSApplication *)NSApp setDelegate:appDelegate];
415 appDelegate->seenFirstActivate = YES;
424 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070 431 UpdateSystemActivity(UsrActivity);
438 NSEvent *
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
439 if (
event == nil ) {
443 if (!s_bShouldHandleEventsInSDLApplication) {
444 Cocoa_DispatchEvent(
event);
448 [NSApp sendEvent:event];
473 NSString *
name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
474 IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
void Cocoa_RegisterApp(void)
void Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
int SDL_SendDropFile(SDL_Window *window, const char *file)
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
void Cocoa_SuspendScreenSaver(_THIS)
IOPMAssertionID screensaver_assertion
int SDL_SendDropComplete(SDL_Window *window)
GLuint const GLchar * name
#define SDL_GetHintBoolean
static SDL_VideoDevice * _this
static SDL_AudioDeviceID device
#define SDL_HINT_MAC_BACKGROUND_APP
When set don't force the SDL app to become a foreground process.
Uint32 screensaver_activity
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
void Cocoa_PumpEvents(_THIS)
BOOL screensaver_use_iopm
SDL_VideoDisplay * displays
void Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
SDL_Window * fullscreen_window
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
#define SDL_assert(condition)
EGLSurface EGLNativeWindowType * window
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
The type used to identify a window.
GLuint GLuint GLsizei GLenum type
SDL_VideoDevice * SDL_GetVideoDevice(void)
SDL_bool suspend_screensaver
GLuint GLsizei GLsizei * length
#define SDL_TICKS_PASSED(A, B)
Compare SDL ticks values, and return true if A has passed B.