2015-06-21 08:33:46 -07:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2022-01-03 09:40:00 -08:00
|
|
|
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 08:33:46 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
#include "../../SDL_internal.h"
|
|
|
|
|
|
|
|
#if SDL_VIDEO_DRIVER_COCOA
|
2020-01-29 20:09:08 -08:00
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
#include "SDL_timer.h"
|
|
|
|
|
|
|
|
#include "SDL_cocoavideo.h"
|
|
|
|
#include "../../events/SDL_events_c.h"
|
2015-07-29 17:19:15 -07:00
|
|
|
#include "SDL_hints.h"
|
2015-06-21 08:33:46 -07:00
|
|
|
|
|
|
|
/* This define was added in the 10.9 SDK. */
|
|
|
|
#ifndef kIOPMAssertPreventUserIdleDisplaySleep
|
|
|
|
#define kIOPMAssertPreventUserIdleDisplaySleep kIOPMAssertionTypePreventUserIdleDisplaySleep
|
|
|
|
#endif
|
2020-10-08 18:00:00 -07:00
|
|
|
#ifndef NSAppKitVersionNumber10_8
|
|
|
|
#define NSAppKitVersionNumber10_8 1187
|
|
|
|
#endif
|
2021-06-10 18:00:32 -07:00
|
|
|
#ifndef MAC_OS_X_VERSION_10_12
|
|
|
|
#define NSEventTypeApplicationDefined NSApplicationDefined
|
|
|
|
#endif
|
2015-06-21 08:33:46 -07:00
|
|
|
|
2021-04-12 11:25:44 -07:00
|
|
|
static SDL_Window *FindSDLWindowForNSWindow(NSWindow *win)
|
|
|
|
{
|
|
|
|
SDL_Window *sdlwindow = NULL;
|
|
|
|
SDL_VideoDevice *device = SDL_GetVideoDevice();
|
|
|
|
if (device && device->windows) {
|
|
|
|
for (sdlwindow = device->windows; sdlwindow; sdlwindow = sdlwindow->next) {
|
2022-05-07 17:57:23 -07:00
|
|
|
NSWindow *nswindow = ((__bridge SDL_WindowData *) sdlwindow->driverdata).nswindow;
|
2021-04-12 11:25:44 -07:00
|
|
|
if (win == nswindow)
|
|
|
|
return sdlwindow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdlwindow;
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
@interface SDLApplication : NSApplication
|
|
|
|
|
|
|
|
- (void)terminate:(id)sender;
|
2016-10-01 14:34:52 -07:00
|
|
|
- (void)sendEvent:(NSEvent *)theEvent;
|
2015-06-21 08:33:46 -07:00
|
|
|
|
2017-11-09 18:24:09 -08:00
|
|
|
+ (void)registerUserDefaults;
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SDLApplication
|
|
|
|
|
|
|
|
// Override terminate to handle Quit and System Shutdown smoothly.
|
|
|
|
- (void)terminate:(id)sender
|
|
|
|
{
|
|
|
|
SDL_SendQuit();
|
|
|
|
}
|
|
|
|
|
2016-10-14 08:15:39 -07:00
|
|
|
static SDL_bool s_bShouldHandleEventsInSDLApplication = SDL_FALSE;
|
|
|
|
|
|
|
|
static void Cocoa_DispatchEvent(NSEvent *theEvent)
|
2016-10-01 14:34:52 -07:00
|
|
|
{
|
|
|
|
SDL_VideoDevice *_this = SDL_GetVideoDevice();
|
2016-10-14 08:15:39 -07:00
|
|
|
|
2016-10-01 14:34:52 -07:00
|
|
|
switch ([theEvent type]) {
|
2017-07-13 18:59:02 -07:00
|
|
|
case NSEventTypeLeftMouseDown:
|
|
|
|
case NSEventTypeOtherMouseDown:
|
|
|
|
case NSEventTypeRightMouseDown:
|
|
|
|
case NSEventTypeLeftMouseUp:
|
|
|
|
case NSEventTypeOtherMouseUp:
|
|
|
|
case NSEventTypeRightMouseUp:
|
|
|
|
case NSEventTypeLeftMouseDragged:
|
|
|
|
case NSEventTypeRightMouseDragged:
|
|
|
|
case NSEventTypeOtherMouseDragged: /* usually middle mouse dragged */
|
|
|
|
case NSEventTypeMouseMoved:
|
|
|
|
case NSEventTypeScrollWheel:
|
2016-10-01 14:34:52 -07:00
|
|
|
Cocoa_HandleMouseEvent(_this, theEvent);
|
|
|
|
break;
|
2017-07-13 18:59:02 -07:00
|
|
|
case NSEventTypeKeyDown:
|
|
|
|
case NSEventTypeKeyUp:
|
|
|
|
case NSEventTypeFlagsChanged:
|
2016-10-01 14:34:52 -07:00
|
|
|
Cocoa_HandleKeyEvent(_this, theEvent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-10-14 08:15:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch events here so that we can handle events caught by
|
|
|
|
// nextEventMatchingMask in SDL, as well as events caught by other
|
|
|
|
// processes (such as CEF) that are passed down to NSApp.
|
|
|
|
- (void)sendEvent:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
if (s_bShouldHandleEventsInSDLApplication) {
|
|
|
|
Cocoa_DispatchEvent(theEvent);
|
|
|
|
}
|
|
|
|
|
2016-10-01 14:34:52 -07:00
|
|
|
[super sendEvent:theEvent];
|
|
|
|
}
|
|
|
|
|
2017-11-09 18:24:09 -08:00
|
|
|
+ (void)registerUserDefaults
|
|
|
|
{
|
|
|
|
NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithBool:NO], @"AppleMomentumScrollSupported",
|
|
|
|
[NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
|
|
|
|
[NSNumber numberWithBool:YES], @"ApplePersistenceIgnoreState",
|
|
|
|
nil];
|
|
|
|
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
@end // SDLApplication
|
|
|
|
|
|
|
|
/* setAppleMenu disappeared from the headers in 10.4 */
|
|
|
|
@interface NSApplication(NSAppleMenu)
|
|
|
|
- (void)setAppleMenu:(NSMenu *)menu;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface SDLAppDelegate : NSObject <NSApplicationDelegate> {
|
|
|
|
@public
|
|
|
|
BOOL seenFirstActivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init;
|
2020-05-03 23:27:29 -07:00
|
|
|
- (void)localeDidChange:(NSNotification *)notification;
|
2015-06-21 08:33:46 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation SDLAppDelegate : NSObject
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
|
|
|
|
|
|
seenFirstActivate = NO;
|
|
|
|
|
|
|
|
[center addObserver:self
|
|
|
|
selector:@selector(windowWillClose:)
|
|
|
|
name:NSWindowWillCloseNotification
|
|
|
|
object:nil];
|
|
|
|
|
|
|
|
[center addObserver:self
|
|
|
|
selector:@selector(focusSomeWindow:)
|
|
|
|
name:NSApplicationDidBecomeActiveNotification
|
|
|
|
object:nil];
|
2020-05-03 23:27:29 -07:00
|
|
|
|
|
|
|
[center addObserver:self
|
|
|
|
selector:@selector(localeDidChange:)
|
|
|
|
name:NSCurrentLocaleDidChangeNotification
|
|
|
|
object:nil];
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
|
|
|
|
|
|
[center removeObserver:self name:NSWindowWillCloseNotification object:nil];
|
|
|
|
[center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:nil];
|
2020-05-03 23:27:29 -07:00
|
|
|
[center removeObserver:self name:NSCurrentLocaleDidChangeNotification object:nil];
|
2015-06-21 08:33:46 -07:00
|
|
|
|
2021-04-12 11:25:44 -07:00
|
|
|
/* Remove our URL event handler only if we set it */
|
|
|
|
if ([NSApp delegate] == self) {
|
|
|
|
[[NSAppleEventManager sharedAppleEventManager]
|
|
|
|
removeEventHandlerForEventClass:kInternetEventClass
|
|
|
|
andEventID:kAEGetURL];
|
|
|
|
}
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillClose:(NSNotification *)notification;
|
|
|
|
{
|
|
|
|
NSWindow *win = (NSWindow*)[notification object];
|
|
|
|
|
|
|
|
if (![win isKeyWindow]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:25:44 -07:00
|
|
|
/* Don't do anything if this was not an SDL window that was closed */
|
|
|
|
if (FindSDLWindowForNSWindow(win) == NULL)
|
|
|
|
return;
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
/* HACK: Make the next window in the z-order key when the key window is
|
|
|
|
* closed. The custom event loop and/or windowing code we have seems to
|
|
|
|
* prevent the normal behavior: https://bugzilla.libsdl.org/show_bug.cgi?id=1825
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* +[NSApp orderedWindows] never includes the 'About' window, but we still
|
|
|
|
* want to try its list first since the behavior in other apps is to only
|
|
|
|
* make the 'About' window key if no other windows are on-screen.
|
|
|
|
*/
|
|
|
|
for (NSWindow *window in [NSApp orderedWindows]) {
|
|
|
|
if (window != win && [window canBecomeKeyWindow]) {
|
2016-05-20 20:20:52 -07:00
|
|
|
if (![window isOnActiveSpace]) {
|
|
|
|
continue;
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 20:20:52 -07:00
|
|
|
/* If a window wasn't found above, iterate through all visible windows in
|
|
|
|
* the active Space in z-order (including the 'About' window, if it's shown)
|
|
|
|
* and make the first one key.
|
2015-06-21 08:33:46 -07:00
|
|
|
*/
|
2016-05-20 20:20:52 -07:00
|
|
|
for (NSNumber *num in [NSWindow windowNumbersWithOptions:0]) {
|
|
|
|
NSWindow *window = [NSApp windowWithWindowNumber:[num integerValue]];
|
|
|
|
if (window && window != win && [window canBecomeKeyWindow]) {
|
|
|
|
[window makeKeyAndOrderFront:self];
|
|
|
|
return;
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)focusSomeWindow:(NSNotification *)aNotification
|
|
|
|
{
|
|
|
|
/* HACK: Ignore the first call. The application gets a
|
|
|
|
* applicationDidBecomeActive: a little bit after the first window is
|
|
|
|
* created, and if we don't ignore it, a window that has been created with
|
|
|
|
* SDL_WINDOW_MINIMIZED will ~immediately be restored.
|
|
|
|
*/
|
|
|
|
if (!seenFirstActivate) {
|
|
|
|
seenFirstActivate = YES;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:25:44 -07:00
|
|
|
/* Don't do anything if the application already has a key window
|
|
|
|
* that is not an SDL window.
|
|
|
|
*/
|
|
|
|
if ([NSApp keyWindow] && FindSDLWindowForNSWindow([NSApp keyWindow]) == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
SDL_VideoDevice *device = SDL_GetVideoDevice();
|
|
|
|
if (device && device->windows) {
|
|
|
|
SDL_Window *window = device->windows;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < device->num_displays; ++i) {
|
|
|
|
SDL_Window *fullscreen_window = device->displays[i].fullscreen_window;
|
|
|
|
if (fullscreen_window) {
|
|
|
|
if (fullscreen_window->flags & SDL_WINDOW_MINIMIZED) {
|
|
|
|
SDL_RestoreWindow(fullscreen_window);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window->flags & SDL_WINDOW_MINIMIZED) {
|
|
|
|
SDL_RestoreWindow(window);
|
|
|
|
} else {
|
|
|
|
SDL_RaiseWindow(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:27:29 -07:00
|
|
|
- (void)localeDidChange:(NSNotification *)notification;
|
|
|
|
{
|
|
|
|
SDL_SendLocaleChangedEvent();
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
|
|
|
|
{
|
2016-01-04 22:42:00 -08:00
|
|
|
return (BOOL)SDL_SendDropFile(NULL, [filename UTF8String]) && SDL_SendDropComplete(NULL);
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
2017-09-25 16:49:31 -07:00
|
|
|
|
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
/* The menu bar of SDL apps which don't have the typical .app bundle
|
|
|
|
* structure fails to work the first time a window is created (until it's
|
|
|
|
* de-focused and re-focused), if this call is in Cocoa_RegisterApp instead
|
|
|
|
* of here. https://bugzilla.libsdl.org/show_bug.cgi?id=3051
|
|
|
|
*/
|
|
|
|
if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
|
2020-01-27 14:15:12 -08:00
|
|
|
/* Get more aggressive for Catalina: activate the Dock first so we definitely reset all activation state. */
|
|
|
|
for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) {
|
|
|
|
[i activateWithOptions:NSApplicationActivateIgnoringOtherApps];
|
|
|
|
break;
|
|
|
|
}
|
2020-02-12 10:08:29 -08:00
|
|
|
SDL_Delay(300); /* !!! FIXME: this isn't right. */
|
2017-09-25 16:49:31 -07:00
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
|
|
|
}
|
2017-11-09 18:24:09 -08:00
|
|
|
|
|
|
|
/* If we call this before NSApp activation, macOS might print a complaint
|
|
|
|
* about ApplePersistenceIgnoreState. */
|
|
|
|
[SDLApplication registerUserDefaults];
|
2017-09-25 16:49:31 -07:00
|
|
|
}
|
2020-04-05 10:47:58 -07:00
|
|
|
|
|
|
|
- (void)handleURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
|
|
|
|
{
|
|
|
|
NSString* path = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
|
|
|
|
SDL_SendDropFile(NULL, [path UTF8String]);
|
|
|
|
SDL_SendDropComplete(NULL);
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
static SDLAppDelegate *appDelegate = nil;
|
|
|
|
|
|
|
|
static NSString *
|
|
|
|
GetApplicationName(void)
|
|
|
|
{
|
|
|
|
NSString *appName;
|
|
|
|
|
|
|
|
/* Determine the application name */
|
|
|
|
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
|
|
|
|
if (!appName) {
|
|
|
|
appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![appName length]) {
|
|
|
|
appName = [[NSProcessInfo processInfo] processName];
|
|
|
|
}
|
|
|
|
|
|
|
|
return appName;
|
|
|
|
}
|
|
|
|
|
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd
Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup.
SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus.
This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically.
Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to:
* In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false.
* If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before.
* Otherwise, we're done, and using the menus from the nib/xib file!
I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
2019-12-03 07:12:55 -08:00
|
|
|
static bool
|
|
|
|
LoadMainMenuNibIfAvailable(void)
|
|
|
|
{
|
|
|
|
NSDictionary *infoDict;
|
|
|
|
NSString *mainNibFileName;
|
|
|
|
bool success = false;
|
2020-10-08 18:00:00 -07:00
|
|
|
|
2020-10-08 18:11:09 -07:00
|
|
|
if (floor(NSAppKitVersionNumber) < NSAppKitVersionNumber10_8) {
|
2020-10-08 18:00:00 -07:00
|
|
|
return false;
|
|
|
|
}
|
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd
Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup.
SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus.
This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically.
Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to:
* In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false.
* If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before.
* Otherwise, we're done, and using the menus from the nib/xib file!
I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
2019-12-03 07:12:55 -08:00
|
|
|
infoDict = [[NSBundle mainBundle] infoDictionary];
|
|
|
|
if (infoDict) {
|
|
|
|
mainNibFileName = [infoDict valueForKey:@"NSMainNibFile"];
|
|
|
|
|
|
|
|
if (mainNibFileName) {
|
|
|
|
success = [[NSBundle mainBundle] loadNibNamed:mainNibFileName owner:[NSApplication sharedApplication] topLevelObjects:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
static void
|
|
|
|
CreateApplicationMenus(void)
|
|
|
|
{
|
|
|
|
NSString *appName;
|
|
|
|
NSString *title;
|
|
|
|
NSMenu *appleMenu;
|
|
|
|
NSMenu *serviceMenu;
|
|
|
|
NSMenu *windowMenu;
|
|
|
|
NSMenuItem *menuItem;
|
|
|
|
NSMenu *mainMenu;
|
|
|
|
|
|
|
|
if (NSApp == nil) {
|
|
|
|
return;
|
|
|
|
}
|
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd
Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup.
SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus.
This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically.
Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to:
* In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false.
* If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before.
* Otherwise, we're done, and using the menus from the nib/xib file!
I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
2019-12-03 07:12:55 -08:00
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
mainMenu = [[NSMenu alloc] init];
|
|
|
|
|
|
|
|
/* Create the main menu bar */
|
|
|
|
[NSApp setMainMenu:mainMenu];
|
|
|
|
|
|
|
|
/* Create the application menu */
|
|
|
|
appName = GetApplicationName();
|
|
|
|
appleMenu = [[NSMenu alloc] initWithTitle:@""];
|
|
|
|
|
|
|
|
/* Add menu items */
|
|
|
|
title = [@"About " stringByAppendingString:appName];
|
|
|
|
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
|
|
|
|
|
|
|
|
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
[appleMenu addItemWithTitle:@"Preferences…" action:nil keyEquivalent:@","];
|
|
|
|
|
|
|
|
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
serviceMenu = [[NSMenu alloc] initWithTitle:@""];
|
|
|
|
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
|
|
|
|
[menuItem setSubmenu:serviceMenu];
|
|
|
|
|
|
|
|
[NSApp setServicesMenu:serviceMenu];
|
|
|
|
|
|
|
|
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
title = [@"Hide " stringByAppendingString:appName];
|
|
|
|
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
|
|
|
|
|
|
|
|
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
|
2017-07-13 18:59:02 -07:00
|
|
|
[menuItem setKeyEquivalentModifierMask:(NSEventModifierFlagOption|NSEventModifierFlagCommand)];
|
2015-06-21 08:33:46 -07:00
|
|
|
|
|
|
|
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
|
|
|
|
|
|
|
|
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
title = [@"Quit " stringByAppendingString:appName];
|
|
|
|
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
|
|
|
|
|
|
|
|
/* Put menu into the menubar */
|
|
|
|
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
|
|
|
|
[menuItem setSubmenu:appleMenu];
|
|
|
|
[[NSApp mainMenu] addItem:menuItem];
|
|
|
|
|
|
|
|
/* Tell the application object that this is now the application menu */
|
|
|
|
[NSApp setAppleMenu:appleMenu];
|
|
|
|
|
|
|
|
/* Create the window menu */
|
|
|
|
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
|
|
|
|
|
|
|
|
/* Add menu items */
|
2018-12-08 11:06:40 -08:00
|
|
|
[windowMenu addItemWithTitle:@"Close" action:@selector(performClose:) keyEquivalent:@"w"];
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
[windowMenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
|
|
|
|
|
|
|
|
[windowMenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
|
2018-12-08 11:06:40 -08:00
|
|
|
|
|
|
|
/* Add the fullscreen toggle menu option, if supported */
|
|
|
|
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) {
|
|
|
|
/* Cocoa should update the title to Enter or Exit Full Screen automatically.
|
|
|
|
* But if not, then just fallback to Toggle Full Screen.
|
|
|
|
*/
|
|
|
|
menuItem = [[NSMenuItem alloc] initWithTitle:@"Toggle Full Screen" action:@selector(toggleFullScreen:) keyEquivalent:@"f"];
|
|
|
|
[menuItem setKeyEquivalentModifierMask:NSEventModifierFlagControl | NSEventModifierFlagCommand];
|
|
|
|
[windowMenu addItem:menuItem];
|
|
|
|
}
|
2015-06-21 08:33:46 -07:00
|
|
|
|
|
|
|
/* Put menu into the menubar */
|
|
|
|
menuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
|
|
|
|
[menuItem setSubmenu:windowMenu];
|
|
|
|
[[NSApp mainMenu] addItem:menuItem];
|
|
|
|
|
|
|
|
/* Tell the application object that this is now the window menu */
|
|
|
|
[NSApp setWindowsMenu:windowMenu];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cocoa_RegisterApp(void)
|
|
|
|
{ @autoreleasepool
|
|
|
|
{
|
|
|
|
/* This can get called more than once! Be careful what you initialize! */
|
|
|
|
|
|
|
|
if (NSApp == nil) {
|
|
|
|
[SDLApplication sharedApplication];
|
|
|
|
SDL_assert(NSApp != nil);
|
|
|
|
|
2016-10-14 08:15:39 -07:00
|
|
|
s_bShouldHandleEventsInSDLApplication = SDL_TRUE;
|
|
|
|
|
2016-10-07 23:40:44 -07:00
|
|
|
if (!SDL_GetHintBoolean(SDL_HINT_MAC_BACKGROUND_APP, SDL_FALSE)) {
|
2016-05-20 20:20:52 -07:00
|
|
|
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
2018-09-24 11:49:25 -07:00
|
|
|
}
|
|
|
|
|
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd
Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup.
SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus.
This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically.
Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to:
* In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false.
* If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before.
* Otherwise, we're done, and using the menus from the nib/xib file!
I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
2019-12-03 07:12:55 -08:00
|
|
|
/* If there aren't already menus in place, look to see if there's
|
|
|
|
* a nib we should use. If not, then manually create the basic
|
|
|
|
* menus we meed.
|
|
|
|
*/
|
2015-06-21 08:33:46 -07:00
|
|
|
if ([NSApp mainMenu] == nil) {
|
Fixed bug 4877 - Add support for loading menus from a nib/xib instead of building a hardcoded minimum set
Eric Shepherd
Currently, SDL on Cocoa macOS creates a rudimentary menu bar programmatically if none is already present when the app is registered during setup.
SDL could be much more easily and flexibly used on macOS if upon finding that no menus are currently in place, it first looked for the application's main menu nib or xib file and, if found, loaded that instead of programmatically building the menus.
This would then let developers simply drop in a nib file with a menu bar defined in it and it would be installed and used automatically.
Attached is a patch that does just this. It changes the SDL_cocoaevents.m file to:
* In Cocoa_RegisterApp(), before calling CreateApplicationMenus(), it calls a new function, LoadMainMenuNibIfAvailable(), which attempts to load and install the main menu nib file, using the nib name fetched from the Info.plist file. If that succeeds, LoadMainMenuNibIfAvailable() returns true; otherwise false.
* If LMMNIA() returns false, CreateApplicationMenus() is called to programmatically build the menus as before.
* Otherwise, we're done, and using the menus from the nib/xib file!
I made these changes to support a project I'm working on, and felt they were useful enough to be worth offering them for uplift. They should have zero impact on existing projects' behavior, but make Cocoa SDL development miles easier.
2019-12-03 07:12:55 -08:00
|
|
|
bool nibLoaded;
|
|
|
|
|
|
|
|
nibLoaded = LoadMainMenuNibIfAvailable();
|
|
|
|
if (!nibLoaded) {
|
|
|
|
CreateApplicationMenus();
|
|
|
|
}
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
[NSApp finishLaunching];
|
2017-11-09 18:24:09 -08:00
|
|
|
if ([NSApp delegate]) {
|
|
|
|
/* The SDL app delegate calls this in didFinishLaunching if it's
|
|
|
|
* attached to the NSApp, otherwise we need to call it manually.
|
|
|
|
*/
|
|
|
|
[SDLApplication registerUserDefaults];
|
|
|
|
}
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
if (NSApp && !appDelegate) {
|
|
|
|
appDelegate = [[SDLAppDelegate alloc] init];
|
|
|
|
|
|
|
|
/* If someone else has an app delegate, it means we can't turn a
|
|
|
|
* termination into SDL_Quit, and we can't handle application:openFile:
|
|
|
|
*/
|
|
|
|
if (![NSApp delegate]) {
|
2021-04-12 11:25:44 -07:00
|
|
|
/* Only register the URL event handler if we are being set as the
|
|
|
|
* app delegate to avoid replacing any existing event handler.
|
|
|
|
*/
|
|
|
|
[[NSAppleEventManager sharedAppleEventManager]
|
|
|
|
setEventHandler:appDelegate
|
|
|
|
andSelector:@selector(handleURLEvent:withReplyEvent:)
|
|
|
|
forEventClass:kInternetEventClass
|
|
|
|
andEventID:kAEGetURL];
|
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
[(NSApplication *)NSApp setDelegate:appDelegate];
|
|
|
|
} else {
|
|
|
|
appDelegate->seenFirstActivate = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2021-03-12 12:58:20 -08:00
|
|
|
int
|
|
|
|
Cocoa_PumpEventsUntilDate(_THIS, NSDate *expiration, bool accumulate)
|
2015-06-21 08:33:46 -07:00
|
|
|
{
|
2017-07-13 19:09:37 -07:00
|
|
|
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
|
2016-12-01 11:52:47 -08:00
|
|
|
/* Update activity every 30 seconds to prevent screensaver */
|
|
|
|
SDL_VideoData *data = (SDL_VideoData *)_this->driverdata;
|
|
|
|
if (_this->suspend_screensaver && !data->screensaver_use_iopm) {
|
|
|
|
Uint32 now = SDL_GetTicks();
|
|
|
|
if (!data->screensaver_activity ||
|
|
|
|
SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
|
|
|
|
UpdateSystemActivity(UsrActivity);
|
|
|
|
data->screensaver_activity = now;
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 19:09:37 -07:00
|
|
|
#endif
|
2016-12-01 11:52:47 -08:00
|
|
|
|
2015-06-21 08:33:46 -07:00
|
|
|
for ( ; ; ) {
|
2021-03-12 12:58:20 -08:00
|
|
|
NSEvent *event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:expiration inMode:NSDefaultRunLoopMode dequeue:YES ];
|
2015-06-21 08:33:46 -07:00
|
|
|
if ( event == nil ) {
|
2021-03-12 12:58:20 -08:00
|
|
|
return 0;
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
|
2016-10-14 08:15:39 -07:00
|
|
|
if (!s_bShouldHandleEventsInSDLApplication) {
|
|
|
|
Cocoa_DispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-10-01 14:34:52 -07:00
|
|
|
// Pass events down to SDLApplication to be handled in sendEvent:
|
2015-06-21 08:33:46 -07:00
|
|
|
[NSApp sendEvent:event];
|
2021-03-12 12:58:20 -08:00
|
|
|
if ( !accumulate) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Cocoa_WaitEventTimeout(_THIS, int timeout)
|
|
|
|
{ @autoreleasepool
|
|
|
|
{
|
|
|
|
if (timeout > 0) {
|
|
|
|
NSDate *limitDate = [NSDate dateWithTimeIntervalSinceNow: (double) timeout / 1000.0];
|
|
|
|
return Cocoa_PumpEventsUntilDate(_this, limitDate, false);
|
|
|
|
} else if (timeout == 0) {
|
|
|
|
return Cocoa_PumpEventsUntilDate(_this, [NSDate distantPast], false);
|
|
|
|
} else {
|
|
|
|
while (Cocoa_PumpEventsUntilDate(_this, [NSDate distantFuture], false) == 0) {
|
|
|
|
}
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
2021-03-12 12:58:20 -08:00
|
|
|
return 1;
|
|
|
|
}}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cocoa_PumpEvents(_THIS)
|
|
|
|
{ @autoreleasepool
|
|
|
|
{
|
|
|
|
Cocoa_PumpEventsUntilDate(_this, [NSDate distantPast], true);
|
|
|
|
}}
|
|
|
|
|
|
|
|
void Cocoa_SendWakeupEvent(_THIS, SDL_Window *window)
|
|
|
|
{ @autoreleasepool
|
|
|
|
{
|
2022-05-07 17:57:23 -07:00
|
|
|
NSWindow *nswindow = ((__bridge SDL_WindowData *) window->driverdata).nswindow;
|
2021-03-12 12:58:20 -08:00
|
|
|
|
|
|
|
NSEvent* event = [NSEvent otherEventWithType: NSEventTypeApplicationDefined
|
|
|
|
location: NSMakePoint(0,0)
|
|
|
|
modifierFlags: 0
|
|
|
|
timestamp: 0.0
|
|
|
|
windowNumber: nswindow.windowNumber
|
|
|
|
context: nil
|
|
|
|
subtype: 0
|
|
|
|
data1: 0
|
|
|
|
data2: 0];
|
|
|
|
|
|
|
|
[NSApp postEvent: event atStart: YES];
|
2015-06-21 08:33:46 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
void
|
|
|
|
Cocoa_SuspendScreenSaver(_THIS)
|
|
|
|
{ @autoreleasepool
|
|
|
|
{
|
2022-05-07 17:57:23 -07:00
|
|
|
SDL_VideoData *data = (__bridge SDL_VideoData *)_this->driverdata;
|
2015-06-21 08:33:46 -07:00
|
|
|
|
2022-05-07 17:57:23 -07:00
|
|
|
if (!data.screensaver_use_iopm) {
|
2016-12-01 11:52:47 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-07 17:57:23 -07:00
|
|
|
if (data.screensaver_assertion) {
|
|
|
|
IOPMAssertionRelease(data.screensaver_assertion);
|
|
|
|
data.screensaver_assertion = kIOPMNullAssertionID;
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_this->suspend_screensaver) {
|
|
|
|
/* FIXME: this should ideally describe the real reason why the game
|
|
|
|
* called SDL_DisableScreenSaver. Note that the name is only meant to be
|
|
|
|
* seen by OS X power users. there's an additional optional human-readable
|
|
|
|
* (localized) reason parameter which we don't set.
|
|
|
|
*/
|
2022-05-07 17:57:23 -07:00
|
|
|
IOPMAssertionID assertion = kIOPMNullAssertionID;
|
2015-06-21 08:33:46 -07:00
|
|
|
NSString *name = [GetApplicationName() stringByAppendingString:@" using SDL_DisableScreenSaver"];
|
|
|
|
IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep,
|
2022-05-07 17:57:23 -07:00
|
|
|
(__bridge CFStringRef) name,
|
2015-06-21 08:33:46 -07:00
|
|
|
NULL, NULL, NULL, 0, NULL,
|
2022-05-07 17:57:23 -07:00
|
|
|
&assertion);
|
|
|
|
data.screensaver_assertion = assertion;
|
2015-06-21 08:33:46 -07:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
|
|
|
#endif /* SDL_VIDEO_DRIVER_COCOA */
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|