godot_main_macos.mm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**************************************************************************/
  2. /* godot_main_macos.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. #import "os_macos.h"
  31. #import "godot_application.h"
  32. #include "main/main.h"
  33. #if defined(SANITIZERS_ENABLED)
  34. #include <sys/resource.h>
  35. #endif
  36. int main(int argc, char **argv) {
  37. #if defined(VULKAN_ENABLED)
  38. setenv("MVK_CONFIG_FULL_IMAGE_VIEW_SWIZZLE", "1", 1); // MoltenVK - enable full component swizzling support.
  39. setenv("MVK_CONFIG_SWAPCHAIN_MIN_MAG_FILTER_USE_NEAREST", "0", 1); // MoltenVK - use linear surface scaling. TODO: remove when full DPI scaling is implemented.
  40. #endif
  41. #if defined(SANITIZERS_ENABLED)
  42. // Note: Set stack size to be at least 30 MB (vs 8 MB default) to avoid overflow, address sanitizer can increase stack usage up to 3 times.
  43. struct rlimit stack_lim = { 0x1E00000, 0x1E00000 };
  44. setrlimit(RLIMIT_STACK, &stack_lim);
  45. #endif
  46. LocalVector<char *> args;
  47. args.resize(argc);
  48. uint32_t argsc = 0;
  49. int wait_for_debugger = 0; // wait 5 second by default
  50. bool is_embedded = false;
  51. for (int i = 0; i < argc; i++) {
  52. if (strcmp("-NSDocumentRevisionsDebugMode", argv[i]) == 0) {
  53. // remove "-NSDocumentRevisionsDebugMode" and the next argument
  54. continue;
  55. }
  56. if (strcmp("--os-debug", argv[i]) == 0) {
  57. i++;
  58. wait_for_debugger = 5000; // wait 5 seconds by default
  59. if (i < argc && strncmp(argv[i], "--", 2) != 0) {
  60. wait_for_debugger = atoi(argv[i]);
  61. }
  62. continue;
  63. }
  64. if (strcmp("--embedded", argv[i]) == 0) {
  65. is_embedded = true;
  66. continue;
  67. }
  68. args.ptr()[argsc] = argv[i];
  69. argsc++;
  70. }
  71. uint32_t remaining_args = argsc - 1;
  72. OS_MacOS *os = nullptr;
  73. if (is_embedded) {
  74. #ifdef DEBUG_ENABLED
  75. os = memnew(OS_MacOS_Embedded(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
  76. #else
  77. WARN_PRINT("Embedded mode is not supported in release builds.");
  78. return EXIT_FAILURE;
  79. #endif
  80. } else {
  81. os = memnew(OS_MacOS_NSApp(args[0], remaining_args, remaining_args > 0 ? &args[1] : nullptr));
  82. }
  83. #ifdef TOOLS_ENABLED
  84. if (wait_for_debugger > 0) {
  85. os->wait_for_debugger(wait_for_debugger);
  86. print_verbose("Continuing execution.");
  87. }
  88. #else
  89. if (wait_for_debugger > 0) {
  90. WARN_PRINT("--os-debug is not supported in release builds.");
  91. }
  92. #endif
  93. if (is_embedded) {
  94. // No dock icon for the embedded process, as it is hosted in the Godot editor.
  95. ProcessSerialNumber psn = { 0, kCurrentProcess };
  96. (void)TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
  97. }
  98. // We must override main when testing is enabled.
  99. TEST_MAIN_OVERRIDE
  100. os->run();
  101. memdelete(os);
  102. return os->get_exit_code();
  103. }