2023-01-05 13:25:55 +01:00
/**************************************************************************/
/* godot_application_delegate.mm */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
2022-01-26 10:01:27 +01:00
#include "godot_application_delegate.h"
2022-07-20 08:28:22 +02:00
#include "display_server_macos.h"
#include "os_macos.h"
2022-01-26 10:01:27 +01:00
@implementation GodotApplicationDelegate
2024-02-07 09:59:35 +01:00
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}
2023-10-22 13:29:56 +02:00
- (NSArray<NSString *> *)localizedTitlesForItem:(id)item {
NSArray *item_name = @[ item[1] ];
return item_name;
}
- (void)searchForItemsWithSearchString:(NSString *)searchString resultLimit:(NSInteger)resultLimit matchedItemHandler:(void (^)(NSArray *items))handleMatchedItems {
NSMutableArray *found_items = [[NSMutableArray alloc] init];
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds && ds->_help_get_search_callback().is_valid()) {
Callable cb = ds->_help_get_search_callback();
Variant ret;
Variant search_string = String::utf8([searchString UTF8String]);
Variant result_limit = (uint64_t)resultLimit;
Callable::CallError ce;
const Variant *args[2] = { &search_string, &result_limit };
cb.callp(args, 2, ret, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_PRINT(vformat(RTR("Failed to execute help search callback: %s."), Variant::get_callable_error_text(cb, args, 2, ce)));
}
Dictionary results = ret;
for (const Variant *E = results.next(); E; E = results.next(E)) {
const String &key = *E;
const String &value = results[*E];
if (key.length() > 0 && value.length() > 0) {
NSArray *item = @[ [NSString stringWithUTF8String:key.utf8().get_data()], [NSString stringWithUTF8String:value.utf8().get_data()] ];
[found_items addObject:item];
}
}
}
handleMatchedItems(found_items);
}
- (void)performActionForItem:(id)item {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds && ds->_help_get_action_callback().is_valid()) {
Callable cb = ds->_help_get_action_callback();
Variant ret;
Variant item_string = String::utf8([item[0] UTF8String]);
Callable::CallError ce;
const Variant *args[1] = { &item_string };
cb.callp(args, 1, ret, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_PRINT(vformat(RTR("Failed to execute help action callback: %s."), Variant::get_callable_error_text(cb, args, 1, ce)));
}
}
}
2022-01-26 10:01:27 +01:00
- (void)forceUnbundledWindowActivationHackStep1 {
// Step 1: Switch focus to macOS SystemUIServer process.
// Required to perform step 2, TransformProcessType will fail if app is already the in focus.
for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.systemuiserver"]) {
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
break;
}
[self performSelector:@selector(forceUnbundledWindowActivationHackStep2)
withObject:nil
afterDelay:0.02];
}
- (void)forceUnbundledWindowActivationHackStep2 {
// Step 2: Register app as foreground process.
ProcessSerialNumber psn = { 0, kCurrentProcess };
(void)TransformProcessType(&psn, kProcessTransformToForegroundApplication);
[self performSelector:@selector(forceUnbundledWindowActivationHackStep3) withObject:nil afterDelay:0.02];
}
- (void)forceUnbundledWindowActivationHackStep3 {
// Step 3: Switch focus back to app window.
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
2024-01-19 19:46:26 +01:00
- (void)system_theme_changed:(NSNotification *)notification {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds) {
ds->emit_system_theme_changed();
}
}
2022-01-26 10:01:27 +01:00
- (void)applicationDidFinishLaunching:(NSNotification *)notice {
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
2024-02-23 07:20:02 +01:00
const char *bundled_id = getenv("__CFBundleIdentifier");
NSString *nsbundleid_env = [NSString stringWithUTF8String:(bundled_id != nullptr) ? bundled_id : ""];
2022-11-17 10:14:03 +01:00
NSString *nsbundleid = [[NSBundle mainBundle] bundleIdentifier];
if (nsappname == nil || isatty(STDOUT_FILENO) || isatty(STDIN_FILENO) || isatty(STDERR_FILENO) || ![nsbundleid isEqualToString:nsbundleid_env]) {
2022-01-26 10:01:27 +01:00
// If the executable is started from terminal or is not bundled, macOS WindowServer won't register and activate app window correctly (menu and title bar are grayed out and input ignored).
[self performSelector:@selector(forceUnbundledWindowActivationHackStep1) withObject:nil afterDelay:0.02];
}
2024-01-19 19:46:26 +01:00
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(system_theme_changed:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(system_theme_changed:) name:@"AppleColorPreferencesChangedNotification" object:nil];
2022-01-26 10:01:27 +01:00
}
2022-07-07 09:57:22 +02:00
- (id)init {
self = [super init];
NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager];
[aem setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
[aem setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEOpenDocuments];
return self;
}
2024-01-19 19:46:26 +01:00
- (void)dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:@"AppleInterfaceThemeChangedNotification" object:nil];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:@"AppleColorPreferencesChangedNotification" object:nil];
}
2022-07-07 09:57:22 +02:00
- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
2022-07-20 08:28:22 +02:00
OS_MacOS *os = (OS_MacOS *)OS::get_singleton();
2022-07-07 09:57:22 +02:00
if (!event || !os) {
return;
}
List<String> args;
if (([event eventClass] == kInternetEventClass) && ([event eventID] == kAEGetURL)) {
// Opening URL scheme.
NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
args.push_back(vformat("--uri=\"%s\"", String::utf8([url UTF8String])));
}
if (([event eventClass] == kCoreEventClass) && ([event eventID] == kAEOpenDocuments)) {
// Opening file association.
NSAppleEventDescriptor *files = [event paramDescriptorForKeyword:keyDirectObject];
if (files) {
NSInteger count = [files numberOfItems];
for (NSInteger i = 1; i <= count; i++) {
NSURL *url = [NSURL URLWithString:[[files descriptorAtIndex:i] stringValue]];
args.push_back(String::utf8([url.path UTF8String]));
}
}
}
if (!args.is_empty()) {
if (os->get_main_loop()) {
// Application is already running, open a new instance with the URL/files as command line arguments.
os->create_instance(args);
} else {
// Application is just started, add to the list of command line arguments and continue.
os->set_cmdline_platform_args(args);
}
}
}
2022-01-26 10:01:27 +01:00
- (void)applicationDidResignActive:(NSNotification *)notification {
2022-07-20 08:28:22 +02:00
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-02-24 10:21:23 +01:00
if (ds) {
ds->mouse_process_popups(true);
}
2022-01-26 10:01:27 +01:00
if (OS::get_singleton()->get_main_loop()) {
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
}
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
if (OS::get_singleton()->get_main_loop()) {
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
}
}
- (void)globalMenuCallback:(id)sender {
2022-07-20 08:28:22 +02:00
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-01-26 10:01:27 +01:00
if (ds) {
return ds->menu_callback(sender);
}
}
- (NSMenu *)applicationDockMenu:(NSApplication *)sender {
2022-07-20 08:28:22 +02:00
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-01-26 10:01:27 +01:00
if (ds) {
return ds->get_dock_menu();
} else {
return nullptr;
}
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
2022-07-20 08:28:22 +02:00
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
2022-01-26 10:01:27 +01:00
if (ds) {
2022-07-20 08:28:22 +02:00
ds->send_window_event(ds->get_window(DisplayServerMacOS::MAIN_WINDOW_ID), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
2022-01-26 10:01:27 +01:00
}
return NSTerminateCancel;
}
- (void)showAbout:(id)sender {
2022-07-20 08:28:22 +02:00
OS_MacOS *os = (OS_MacOS *)OS::get_singleton();
2022-01-26 10:01:27 +01:00
if (os && os->get_main_loop()) {
os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_ABOUT);
}
}
@end