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;
43 @implementation SDLApplication
53 static void Cocoa_DispatchEvent(NSEvent *theEvent)
57 switch ([theEvent
type]) {
58 case NSEventTypeLeftMouseDown:
59 case NSEventTypeOtherMouseDown:
60 case NSEventTypeRightMouseDown:
61 case NSEventTypeLeftMouseUp:
62 case NSEventTypeOtherMouseUp:
63 case NSEventTypeRightMouseUp:
64 case NSEventTypeLeftMouseDragged:
65 case NSEventTypeRightMouseDragged:
66 case NSEventTypeOtherMouseDragged:
67 case NSEventTypeMouseMoved:
68 case NSEventTypeScrollWheel:
71 case NSEventTypeKeyDown:
72 case NSEventTypeKeyUp:
73 case NSEventTypeFlagsChanged:
84 - (
void)sendEvent:(NSEvent *)theEvent
86 if (s_bShouldHandleEventsInSDLApplication) {
87 Cocoa_DispatchEvent(theEvent);
90 [
super sendEvent:theEvent];
96 @interface NSApplication(NSAppleMenu)
97 - (
void)setAppleMenu:(NSMenu *)menu;
100 @interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
102 BOOL seenFirstActivate;
108 @implementation SDLAppDelegate : NSObject
113 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
115 seenFirstActivate = NO;
117 [center addObserver:self
118 selector:@selector(windowWillClose:)
119 name:NSWindowWillCloseNotification
122 [center addObserver:self
123 selector:@selector(focusSomeWindow:)
124 name:NSApplicationDidBecomeActiveNotification
133 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
135 [center removeObserver:self name:NSWindowWillCloseNotification object:nil];
136 [center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
141 - (
void)windowWillClose:(NSNotification *)notification;
143 NSWindow *win = (NSWindow*)[notification
object];
145 if (![win isKeyWindow]) {
158 for (NSWindow *
window in [NSApp orderedWindows]) {
160 if (![
window isOnActiveSpace]) {
172 for (NSNumber *
num in [NSWindow windowNumbersWithOptions:0]) {
174 if (window && window != win && [window canBecomeKeyWindow]) {
175 [window makeKeyAndOrderFront:self];
181 - (
void)focusSomeWindow:(NSNotification *)aNotification
188 if (!seenFirstActivate) {
189 seenFirstActivate = YES;
194 if (device && device->
windows) {
199 if (fullscreen_window) {
201 SDL_RestoreWindow(fullscreen_window);
208 SDL_RestoreWindow(window);
210 SDL_RaiseWindow(window);
215 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
220 - (
void)applicationDidFinishLaunching:(NSNotification *)notification
228 [NSApp activateIgnoringOtherApps:YES];
233 static SDLAppDelegate *appDelegate = nil;
236 GetApplicationName(
void)
241 appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
243 appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
247 appName = [[NSProcessInfo processInfo] processName];
254 CreateApplicationMenus(
void)
262 NSMenuItem *menuItem;
269 mainMenu = [[NSMenu alloc] init];
272 [NSApp setMainMenu:mainMenu];
278 appName = GetApplicationName();
279 appleMenu = [[NSMenu alloc] initWithTitle:@""];
282 title = [@"About " stringByAppendingString:appName];
283 [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
285 [appleMenu addItem:[NSMenuItem separatorItem]];
287 [appleMenu addItemWithTitle:@"Preferencesâ €¦" action:nil keyEquivalent:@","];
289 [appleMenu addItem:[NSMenuItem separatorItem]];
291 serviceMenu = [[NSMenu alloc] initWithTitle:@""];
292 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Services" action:nil keyEquivalent:
@""];
293 [menuItem setSubmenu:serviceMenu];
295 [NSApp setServicesMenu:serviceMenu];
296 [serviceMenu release];
298 [appleMenu addItem:[NSMenuItem separatorItem]];
300 title = [@"Hide " stringByAppendingString:appName];
301 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
303 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:
@"Hide Others" action:
@selector(hideOtherApplications:) keyEquivalent:
@"h"];
304 [menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
306 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
308 [appleMenu addItem:[NSMenuItem separatorItem]];
310 title = [@"Quit " stringByAppendingString:appName];
311 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
314 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
315 [menuItem setSubmenu:appleMenu];
316 [[NSApp mainMenu] addItem:menuItem];
320 [NSApp setAppleMenu:appleMenu];
325 windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
328 [windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
330 [windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
333 menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
334 [menuItem setSubmenu:windowMenu];
335 [[NSApp mainMenu] addItem:menuItem];
339 [NSApp setWindowsMenu:windowMenu];
340 [windowMenu release];
344 if (
floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
346 viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
349 menuItem = [viewMenu addItemWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
350 [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
353 menuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
354 [menuItem setSubmenu:viewMenu];
355 [[NSApp mainMenu] addItem:menuItem];
369 [SDLApplication sharedApplication];
372 s_bShouldHandleEventsInSDLApplication =
SDL_TRUE;
375 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
378 if ([NSApp mainMenu] == nil) {
379 CreateApplicationMenus();
381 [NSApp finishLaunching];
382 NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
383 [NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
384 [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
385 [NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
387 [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
388 [appDefaults release];
390 if (NSApp && !appDelegate) {
391 appDelegate = [[SDLAppDelegate alloc] init];
396 if (![NSApp delegate]) {
397 [(NSApplication *)NSApp setDelegate:appDelegate];
399 appDelegate->seenFirstActivate = YES;
408 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1070 415 UpdateSystemActivity(UsrActivity);
422 NSEvent *
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES ];
423 if (
event == nil ) {
427 if (!s_bShouldHandleEventsInSDLApplication) {
428 Cocoa_DispatchEvent(
event);
432 [NSApp sendEvent:event];
457 NSString *
name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
458 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.