| 1 |
/**************************************************************************/ |
| 2 |
/* godot_application_delegate.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "godot_application_delegate.h" |
| 32 |
|
| 33 |
#import "display_server_macos.h" |
| 34 |
#import "godot_menu_item.h" |
| 35 |
#import "key_mapping_macos.h" |
| 36 |
#import "native_menu_macos.h" |
| 37 |
#import "os_macos.h" |
| 38 |
|
| 39 |
#import "core/os/main_loop.h" |
| 40 |
#import "main/main.h" |
| 41 |
|
| 42 |
#import <Carbon/Carbon.h> |
| 43 |
|
| 44 |
@interface GodotApplicationDelegate () |
| 45 |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; |
| 46 |
- (void)accessibilityDisplayOptionsChange:(NSNotification *)notification; |
| 47 |
@end |
| 48 |
|
| 49 |
@implementation GodotApplicationDelegate { |
| 50 |
bool high_contrast; |
| 51 |
bool reduce_motion; |
| 52 |
bool reduce_transparency; |
| 53 |
bool voice_over; |
| 54 |
OS_MacOS_NSApp *os_mac; |
| 55 |
} |
| 56 |
|
| 57 |
- (GodotApplicationDelegate *)initWithOS:(OS_MacOS_NSApp *)os { |
| 58 |
self = [super init]; |
| 59 |
if (self) { |
| 60 |
os_mac = os; |
| 61 |
} |
| 62 |
|
| 63 |
[[NSWorkspace sharedWorkspace] addObserver:self forKeyPath:@"voiceOverEnabled" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:(void *)godot_ac_ctx]; |
| 64 |
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(accessibilityDisplayOptionsChange:) name:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification object:nil]; |
| 65 |
high_contrast = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast]; |
| 66 |
reduce_motion = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceMotion]; |
| 67 |
reduce_transparency = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceTransparency]; |
| 68 |
voice_over = [[NSWorkspace sharedWorkspace] isVoiceOverEnabled]; |
| 69 |
|
| 70 |
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(system_theme_changed:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; |
| 71 |
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(system_theme_changed:) name:@"AppleColorPreferencesChangedNotification" object:nil]; |
| 72 |
|
| 73 |
return self; |
| 74 |
} |
| 75 |
|
| 76 |
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app { |
| 77 |
return YES; |
| 78 |
} |
| 79 |
|
| 80 |
- (NSArray<NSString *> *)localizedTitlesForItem:(id)item { |
| 81 |
NSArray *item_name = @[ item[1] ]; |
| 82 |
return item_name; |
| 83 |
} |
| 84 |
|
| 85 |
- (void)searchForItemsWithSearchString:(NSString *)searchString resultLimit:(NSInteger)resultLimit matchedItemHandler:(void (^)(NSArray *items))handleMatchedItems { |
| 86 |
NSMutableArray *found_items = [[NSMutableArray alloc] init]; |
| 87 |
|
| 88 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 89 |
if (ds && ds->_help_get_search_callback().is_valid()) { |
| 90 |
Callable cb = ds->_help_get_search_callback(); |
| 91 |
|
| 92 |
Variant ret; |
| 93 |
Variant search_string = String::utf8([searchString UTF8String]); |
| 94 |
Variant result_limit = (uint64_t)resultLimit; |
| 95 |
Callable::CallError ce; |
| 96 |
const Variant *args[2] = { &search_string, &result_limit }; |
| 97 |
|
| 98 |
cb.callp(args, 2, ret, ce); |
| 99 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 100 |
ERR_PRINT(vformat(RTR("Failed to execute help search callback: %s."), Variant::get_callable_error_text(cb, args, 2, ce))); |
| 101 |
} |
| 102 |
Dictionary results = ret; |
| 103 |
for (const Variant *E = results.next(); E; E = results.next(E)) { |
| 104 |
const String &key = *E; |
| 105 |
const String &value = results[*E]; |
| 106 |
if (key.length() > 0 && value.length() > 0) { |
| 107 |
NSArray *item = @[ [NSString stringWithUTF8String:key.utf8().get_data()], [NSString stringWithUTF8String:value.utf8().get_data()] ]; |
| 108 |
[found_items addObject:item]; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
handleMatchedItems(found_items); |
| 114 |
} |
| 115 |
|
| 116 |
- (void)performActionForItem:(id)item { |
| 117 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 118 |
if (ds && ds->_help_get_action_callback().is_valid()) { |
| 119 |
Callable cb = ds->_help_get_action_callback(); |
| 120 |
|
| 121 |
Variant ret; |
| 122 |
Variant item_string = String::utf8([item[0] UTF8String]); |
| 123 |
Callable::CallError ce; |
| 124 |
const Variant *args[1] = { &item_string }; |
| 125 |
|
| 126 |
cb.callp(args, 1, ret, ce); |
| 127 |
if (ce.error != Callable::CallError::CALL_OK) { |
| 128 |
ERR_PRINT(vformat(RTR("Failed to execute help action callback: %s."), Variant::get_callable_error_text(cb, args, 1, ce))); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
- (void)system_theme_changed:(NSNotification *)notification { |
| 134 |
DisplayServerMacOSBase *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 135 |
if (ds) { |
| 136 |
ds->emit_system_theme_changed(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
- (void)applicationDidFinishLaunching:(NSNotification *)notification { |
| 141 |
os_mac->start_main(); |
| 142 |
} |
| 143 |
|
| 144 |
static const char *godot_ac_ctx = "gd_accessibility_observer_ctx"; |
| 145 |
|
| 146 |
- (void)dealloc { |
| 147 |
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:@"AppleInterfaceThemeChangedNotification" object:nil]; |
| 148 |
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:@"AppleColorPreferencesChangedNotification" object:nil]; |
| 149 |
[[NSWorkspace sharedWorkspace] removeObserver:self forKeyPath:@"voiceOverEnabled" context:(void *)godot_ac_ctx]; |
| 150 |
} |
| 151 |
|
| 152 |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { |
| 153 |
if (context == (void *)godot_ac_ctx) { |
| 154 |
voice_over = [[NSWorkspace sharedWorkspace] isVoiceOverEnabled]; |
| 155 |
} else { |
| 156 |
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
- (void)accessibilityDisplayOptionsChange:(NSNotification *)notification { |
| 161 |
high_contrast = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast]; |
| 162 |
reduce_motion = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceMotion]; |
| 163 |
reduce_transparency = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldReduceTransparency]; |
| 164 |
} |
| 165 |
|
| 166 |
- (bool)getHighContrast { |
| 167 |
return high_contrast; |
| 168 |
} |
| 169 |
|
| 170 |
- (bool)getReduceMotion { |
| 171 |
return reduce_motion; |
| 172 |
} |
| 173 |
|
| 174 |
- (bool)getReduceTransparency { |
| 175 |
return reduce_transparency; |
| 176 |
} |
| 177 |
|
| 178 |
- (bool)getVoiceOver { |
| 179 |
return voice_over; |
| 180 |
} |
| 181 |
|
| 182 |
- (void)application:(NSApplication *)application openURLs:(NSArray<NSURL *> *)urls { |
| 183 |
List<String> args; |
| 184 |
for (NSURL *url in urls) { |
| 185 |
if ([url isFileURL]) { |
| 186 |
args.push_back(String::utf8([url.path UTF8String])); |
| 187 |
} else { |
| 188 |
args.push_back(vformat("--uri=\"%s\"", String::utf8([url.absoluteString UTF8String]))); |
| 189 |
} |
| 190 |
} |
| 191 |
if (!args.is_empty()) { |
| 192 |
if (os_mac->get_main_loop()) { |
| 193 |
// Application is already running, open a new instance with the URL/files as command line arguments. |
| 194 |
os_mac->create_instance(args); |
| 195 |
} else if (os_mac->get_cmd_argc() == 0) { |
| 196 |
// Application is just started, add to the list of command line arguments and continue. |
| 197 |
os_mac->set_cmdline_platform_args(args); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
- (void)application:(NSApplication *)sender openFiles:(NSArray<NSString *> *)filenames { |
| 203 |
List<String> args; |
| 204 |
for (NSString *filename in filenames) { |
| 205 |
NSURL *url = [NSURL URLWithString:filename]; |
| 206 |
args.push_back(String::utf8([url.path UTF8String])); |
| 207 |
} |
| 208 |
if (!args.is_empty()) { |
| 209 |
if (os_mac->get_main_loop()) { |
| 210 |
// Application is already running, open a new instance with the URL/files as command line arguments. |
| 211 |
os_mac->create_instance(args); |
| 212 |
} else if (os_mac->get_cmd_argc() == 0) { |
| 213 |
// Application is just started, add to the list of command line arguments and continue. |
| 214 |
os_mac->set_cmdline_platform_args(args); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
- (void)applicationDidResignActive:(NSNotification *)notification { |
| 220 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 221 |
if (ds) { |
| 222 |
ds->mouse_process_popups(true); |
| 223 |
} |
| 224 |
if (os_mac->get_main_loop()) { |
| 225 |
os_mac->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
static const CGKeyCode modifiers[8] = { |
| 230 |
kVK_Command, |
| 231 |
kVK_RightCommand, |
| 232 |
kVK_Shift, |
| 233 |
kVK_RightShift, |
| 234 |
kVK_Option, |
| 235 |
kVK_RightOption, |
| 236 |
kVK_Control, |
| 237 |
kVK_RightControl, |
| 238 |
}; |
| 239 |
|
| 240 |
// The list of modifier flags we care about for raising pressed events when the application becomes active. |
| 241 |
constexpr static NSEventModifierFlags FLAGS = NSEventModifierFlagCommand | NSEventModifierFlagShift | NSEventModifierFlagOption | NSEventModifierFlagControl; |
| 242 |
|
| 243 |
- (void)applicationDidBecomeActive:(NSNotification *)notification { |
| 244 |
if (os_mac->get_main_loop()) { |
| 245 |
os_mac->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN); |
| 246 |
} |
| 247 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 248 |
if (!ds) { |
| 249 |
return; |
| 250 |
} |
| 251 |
Input *input = Input::get_singleton(); |
| 252 |
if (!input) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
// Poll the modifier keys and submit pressed events if they are down when the application becomes active. |
| 257 |
int mod = NSEvent.modifierFlags; |
| 258 |
if ((mod & FLAGS) == 0) { |
| 259 |
// No flags we care about. |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
DisplayServer::WindowID window_id = ds->get_focused_window(); |
| 264 |
NSEventModifierFlags flags = static_cast<NSEventModifierFlags>(mod); |
| 265 |
|
| 266 |
for (const CGKeyCode key : modifiers) { |
| 267 |
bool is_down = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, key); |
| 268 |
if (likely(!is_down)) { |
| 269 |
continue; |
| 270 |
} |
| 271 |
Ref<InputEventKey> ke; |
| 272 |
ke.instantiate(); |
| 273 |
|
| 274 |
ke->set_window_id(window_id); |
| 275 |
ke->set_echo(false); |
| 276 |
ke->set_pressed(true); |
| 277 |
ds->get_key_modifier_state(flags, ke); |
| 278 |
ke->set_keycode(KeyMappingMacOS::remap_key(key, mod, false)); |
| 279 |
ke->set_physical_keycode(KeyMappingMacOS::translate_key(key)); |
| 280 |
ke->set_key_label(KeyMappingMacOS::remap_key(key, mod, true)); |
| 281 |
ke->set_location(KeyMappingMacOS::translate_location(key)); |
| 282 |
input->parse_input_event(ke); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
- (BOOL)validateMenuItem:(NSMenuItem *)item { |
| 287 |
if (item) { |
| 288 |
GodotMenuItem *value = [item representedObject]; |
| 289 |
if (value) { |
| 290 |
return value->enabled; |
| 291 |
} |
| 292 |
} |
| 293 |
return YES; |
| 294 |
} |
| 295 |
|
| 296 |
- (void)globalMenuCallback:(id)sender { |
| 297 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 298 |
if (ds) { |
| 299 |
return ds->menu_callback(sender); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
- (NSMenu *)applicationDockMenu:(NSApplication *)sender { |
| 304 |
if (NativeMenu::get_singleton()) { |
| 305 |
NativeMenuMacOS *nmenu = (NativeMenuMacOS *)NativeMenu::get_singleton(); |
| 306 |
return nmenu->_get_dock_menu(); |
| 307 |
} else { |
| 308 |
return nullptr; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
- (void)applicationWillTerminate:(NSNotification *)notification { |
| 313 |
os_mac->cleanup(); |
| 314 |
exit(os_mac->get_exit_code()); |
| 315 |
} |
| 316 |
|
| 317 |
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { |
| 318 |
if (os_mac->os_should_terminate()) { |
| 319 |
return NSTerminateNow; |
| 320 |
} |
| 321 |
|
| 322 |
DisplayServerMacOS *ds = Object::cast_to<DisplayServerMacOS>(DisplayServer::get_singleton()); |
| 323 |
if (ds && ds->has_window(DisplayServerMacOS::MAIN_WINDOW_ID)) { |
| 324 |
ds->send_window_event(ds->get_window(DisplayServerMacOS::MAIN_WINDOW_ID), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST); |
| 325 |
} |
| 326 |
|
| 327 |
return NSTerminateCancel; |
| 328 |
} |
| 329 |
|
| 330 |
- (void)showAbout:(id)sender { |
| 331 |
if (os_mac->get_main_loop()) { |
| 332 |
os_mac->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_ABOUT); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
@end |