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)
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 424 UpdateSystemActivity(UsrActivity);
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];
466 NSString *
name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
467 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.