godot_application_delegate.mm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "godot_application_delegate.h"
  31. #include "display_server_macos.h"
  32. #include "os_macos.h"
  33. @implementation GodotApplicationDelegate
  34. - (void)forceUnbundledWindowActivationHackStep1 {
  35. // Step 1: Switch focus to macOS SystemUIServer process.
  36. // Required to perform step 2, TransformProcessType will fail if app is already the in focus.
  37. for (NSRunningApplication *app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.systemuiserver"]) {
  38. [app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  39. break;
  40. }
  41. [self performSelector:@selector(forceUnbundledWindowActivationHackStep2)
  42. withObject:nil
  43. afterDelay:0.02];
  44. }
  45. - (void)forceUnbundledWindowActivationHackStep2 {
  46. // Step 2: Register app as foreground process.
  47. ProcessSerialNumber psn = { 0, kCurrentProcess };
  48. (void)TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  49. [self performSelector:@selector(forceUnbundledWindowActivationHackStep3) withObject:nil afterDelay:0.02];
  50. }
  51. - (void)forceUnbundledWindowActivationHackStep3 {
  52. // Step 3: Switch focus back to app window.
  53. [[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
  54. }
  55. - (void)applicationDidFinishLaunching:(NSNotification *)notice {
  56. NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  57. NSString *nsbundleid_env = [NSString stringWithUTF8String:getenv("__CFBundleIdentifier")];
  58. NSString *nsbundleid = [[NSBundle mainBundle] bundleIdentifier];
  59. if (nsappname == nil || isatty(STDOUT_FILENO) || isatty(STDIN_FILENO) || isatty(STDERR_FILENO) || ![nsbundleid isEqualToString:nsbundleid_env]) {
  60. // 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).
  61. [self performSelector:@selector(forceUnbundledWindowActivationHackStep1) withObject:nil afterDelay:0.02];
  62. }
  63. }
  64. - (id)init {
  65. self = [super init];
  66. NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager];
  67. [aem setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
  68. [aem setEventHandler:self andSelector:@selector(handleAppleEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEOpenDocuments];
  69. return self;
  70. }
  71. - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
  72. OS_MacOS *os = (OS_MacOS *)OS::get_singleton();
  73. if (!event || !os) {
  74. return;
  75. }
  76. List<String> args;
  77. if (([event eventClass] == kInternetEventClass) && ([event eventID] == kAEGetURL)) {
  78. // Opening URL scheme.
  79. NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
  80. args.push_back(vformat("--uri=\"%s\"", String::utf8([url UTF8String])));
  81. }
  82. if (([event eventClass] == kCoreEventClass) && ([event eventID] == kAEOpenDocuments)) {
  83. // Opening file association.
  84. NSAppleEventDescriptor *files = [event paramDescriptorForKeyword:keyDirectObject];
  85. if (files) {
  86. NSInteger count = [files numberOfItems];
  87. for (NSInteger i = 1; i <= count; i++) {
  88. NSURL *url = [NSURL URLWithString:[[files descriptorAtIndex:i] stringValue]];
  89. args.push_back(String::utf8([url.path UTF8String]));
  90. }
  91. }
  92. }
  93. if (!args.is_empty()) {
  94. if (os->get_main_loop()) {
  95. // Application is already running, open a new instance with the URL/files as command line arguments.
  96. os->create_instance(args);
  97. } else {
  98. // Application is just started, add to the list of command line arguments and continue.
  99. os->set_cmdline_platform_args(args);
  100. }
  101. }
  102. }
  103. - (void)applicationDidResignActive:(NSNotification *)notification {
  104. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  105. if (ds) {
  106. ds->mouse_process_popups(true);
  107. }
  108. if (OS::get_singleton()->get_main_loop()) {
  109. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  110. }
  111. }
  112. - (void)applicationDidBecomeActive:(NSNotification *)notification {
  113. if (OS::get_singleton()->get_main_loop()) {
  114. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  115. }
  116. }
  117. - (void)globalMenuCallback:(id)sender {
  118. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  119. if (ds) {
  120. return ds->menu_callback(sender);
  121. }
  122. }
  123. - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
  124. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  125. if (ds) {
  126. return ds->get_dock_menu();
  127. } else {
  128. return nullptr;
  129. }
  130. }
  131. - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
  132. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  133. if (ds) {
  134. ds->send_window_event(ds->get_window(DisplayServerMacOS::MAIN_WINDOW_ID), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  135. }
  136. return NSTerminateCancel;
  137. }
  138. - (void)showAbout:(id)sender {
  139. OS_MacOS *os = (OS_MacOS *)OS::get_singleton();
  140. if (os && os->get_main_loop()) {
  141. os->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_ABOUT);
  142. }
  143. }
  144. @end