macMain.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <Cocoa/Cocoa.h>
  23. #include "app/mainLoop.h"
  24. #include "platform/platformInput.h"
  25. #include <IOKit/ps/IOPowerSources.h>
  26. #include <IOKit/ps/IOPSKeys.h>
  27. #include "console/console.h"
  28. #include "platform/threads/thread.h"
  29. // TODO: let the mainLoop's sleep time happen via rescheduling the timer every run-through.
  30. extern S32 sgTimeManagerProcessInterval;
  31. @interface MainLoopTimerHandler : NSObject
  32. {
  33. U32 argc;
  34. const char** argv;
  35. NSTimeInterval interval;
  36. }
  37. +(id)startTimerWithintervalMs:(U32)intervalMs argc:(U32)_argc argv:(const char**)_argv;
  38. -(void)firstFire:(NSTimer*)theTimer;
  39. -(void)fireTimer:(NSTimer*)theTimer;
  40. @end
  41. @implementation MainLoopTimerHandler
  42. -(void)firstFire:(NSTimer*)theTimer
  43. {
  44. StandardMainLoop::init();
  45. StandardMainLoop::handleCommandLine(argc, argv);
  46. [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
  47. }
  48. -(void)fireTimer:(NSTimer*)theTimer
  49. {
  50. if(!StandardMainLoop::doMainLoop())
  51. {
  52. StandardMainLoop::shutdown();
  53. [theTimer invalidate];
  54. [NSApp setDelegate:nil];
  55. [NSApp terminate:self];
  56. }
  57. // if(!mainLoop || !mainLoop->mainLoop())
  58. // {
  59. // // stop the timer from firing again
  60. // if(mainLoop)
  61. // mainLoop->shutdown();
  62. //
  63. // [theTimer invalidate];
  64. // [NSApp setDelegate:nil];
  65. // [NSApp terminate:self];
  66. // }
  67. }
  68. +(id)startTimerWithintervalMs:(U32)intervalMs argc:(U32)_argc argv:(const char**)_argv
  69. {
  70. MainLoopTimerHandler* handler = [[[MainLoopTimerHandler alloc] init] autorelease];
  71. handler->argc = _argc;
  72. handler->argv = _argv;
  73. handler->interval = intervalMs / 1000.0; // interval in milliseconds
  74. [NSTimer scheduledTimerWithTimeInterval:handler->interval target:handler selector:@selector(firstFire:) userInfo:nil repeats:NO];
  75. return handler;
  76. }
  77. @end
  78. #pragma mark -
  79. #ifndef TORQUE_SHARED
  80. //-----------------------------------------------------------------------------
  81. // main() - the real one - this is the actual program entry point.
  82. //-----------------------------------------------------------------------------
  83. S32 main(S32 argc, const char **argv)
  84. {
  85. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  86. // get command line and text file args, filter them
  87. // now, we prepare to hand off execution to torque & macosx.
  88. U32 appReturn = 0;
  89. printf("installing torque main loop timer\n");
  90. [MainLoopTimerHandler startTimerWithintervalMs:1 argc:argc argv:argv];
  91. printf("starting NSApplicationMain\n");
  92. appReturn = NSApplicationMain(argc, argv);
  93. printf("NSApplicationMain exited\n");
  94. // shut down the engine
  95. [pool release];
  96. return appReturn;
  97. }
  98. #endif
  99. static NSApplication *app = NULL;
  100. static NSAutoreleasePool* pool = NULL;
  101. void torque_mac_engineinit(S32 argc, const char **argv)
  102. {
  103. if (!Platform::getWebDeployment())
  104. {
  105. pool = [[NSAutoreleasePool alloc] init];
  106. app = [NSApplication sharedApplication];
  107. }
  108. }
  109. void torque_mac_enginetick()
  110. {
  111. if (!Platform::getWebDeployment())
  112. {
  113. NSEvent *e = [app nextEventMatchingMask: NSAnyEventMask
  114. untilDate: [NSDate distantPast]
  115. inMode: NSDefaultRunLoopMode
  116. dequeue: YES];
  117. if (e)
  118. [app sendEvent: e];
  119. }
  120. }
  121. void torque_mac_engineshutdown()
  122. {
  123. if (!Platform::getWebDeployment())
  124. {
  125. [pool release];
  126. }
  127. }
  128. extern "C" {
  129. //-----------------------------------------------------------------------------
  130. // torque_macmain() - entry point for application using bundle
  131. //-----------------------------------------------------------------------------
  132. S32 torque_macmain(S32 argc, const char **argv)
  133. {
  134. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  135. // get command line and text file args, filter them
  136. // now, we prepare to hand off execution to torque & macosx.
  137. U32 appReturn = 0;
  138. printf("installing torque main loop timer\n");
  139. [MainLoopTimerHandler startTimerWithintervalMs:1 argc:argc argv:argv];
  140. printf("starting NSApplicationMain\n");
  141. appReturn = NSApplicationMain(argc, argv);
  142. printf("NSApplicationMain exited\n");
  143. // shut down the engine
  144. [pool release];
  145. return appReturn;
  146. }
  147. } // extern "C"
  148. #pragma mark ---- Init funcs ----
  149. //------------------------------------------------------------------------------
  150. void Platform::init()
  151. {
  152. // Set the platform variable for the scripts
  153. Con::setVariable( "$platform", "macos" );
  154. Input::init();
  155. }
  156. //------------------------------------------------------------------------------
  157. void Platform::shutdown()
  158. {
  159. Input::destroy();
  160. }