application.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if defined(Hiro_Application)
  2. #include <IOKit/pwr_mgt/IOPMLib.h>
  3. @implementation CocoaDelegate : NSObject
  4. -(NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender {
  5. using hiro::Application;
  6. if(Application::state().cocoa.onQuit) Application::Cocoa::doQuit();
  7. else Application::quit();
  8. return NSTerminateCancel;
  9. }
  10. -(BOOL) applicationShouldHandleReopen:(NSApplication*)application hasVisibleWindows:(BOOL)flag {
  11. using hiro::Application;
  12. if(Application::state().cocoa.onActivate) Application::Cocoa::doActivate();
  13. return NO;
  14. }
  15. -(void) run:(NSTimer*)timer {
  16. using hiro::Application;
  17. if(Application::state().onMain) Application::doMain();
  18. }
  19. -(void) updateInDock:(NSTimer*)timer {
  20. NSArray* windows = [NSApp windows];
  21. for(uint n = 0; n < [windows count]; n++) {
  22. NSWindow* window = [windows objectAtIndex:n];
  23. if([window isMiniaturized]) {
  24. [window updateInDock];
  25. }
  26. }
  27. }
  28. @end
  29. CocoaDelegate* cocoaDelegate = nullptr;
  30. NSTimer* applicationTimer = nullptr;
  31. namespace hiro {
  32. auto pApplication::exit() -> void {
  33. quit();
  34. ::exit(EXIT_SUCCESS);
  35. }
  36. auto pApplication::modal() -> bool {
  37. return Application::state().modal > 0;
  38. }
  39. auto pApplication::run() -> void {
  40. //applicationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1667 target:cocoaDelegate selector:@selector(updateInDock:) userInfo:nil repeats:YES];
  41. if(Application::state().onMain) {
  42. applicationTimer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:cocoaDelegate selector:@selector(run:) userInfo:nil repeats:YES];
  43. //below line is needed to run application during window resize; however it has a large performance penalty on the resize smoothness
  44. //[[NSRunLoop currentRunLoop] addTimer:applicationTimer forMode:NSEventTrackingRunLoopMode];
  45. }
  46. @autoreleasepool {
  47. [NSApp run];
  48. }
  49. }
  50. auto pApplication::pendingEvents() -> bool {
  51. bool result = false;
  52. @autoreleasepool {
  53. NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:NO];
  54. if(event != nil) result = true;
  55. }
  56. return result;
  57. }
  58. auto pApplication::processEvents() -> void {
  59. @autoreleasepool {
  60. while(!Application::state().quit) {
  61. NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
  62. if(event == nil) break;
  63. [event retain];
  64. [NSApp sendEvent:event];
  65. [event release];
  66. }
  67. }
  68. }
  69. auto pApplication::quit() -> void {
  70. @autoreleasepool {
  71. [applicationTimer invalidate];
  72. [NSApp stop:nil];
  73. NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:NSMakePoint(0, 0) modifierFlags:0 timestamp:0.0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
  74. [NSApp postEvent:event atStart:true];
  75. }
  76. }
  77. auto pApplication::setScreenSaver(bool screenSaver) -> void {
  78. static IOPMAssertionID powerAssertion = kIOPMNullAssertionID;
  79. // do nothing if current already matches desired state
  80. bool current = powerAssertion == kIOPMNullAssertionID;
  81. if(current == screenSaver) return;
  82. @autoreleasepool {
  83. if(screenSaver) {
  84. IOPMAssertionRelease(powerAssertion);
  85. powerAssertion = kIOPMNullAssertionID;
  86. } else {
  87. string reason = {Application::state().name, " screensaver suppression"};
  88. NSString* assertionName = [NSString stringWithUTF8String:reason.data()];
  89. bool success = IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep,
  90. kIOPMAssertionLevelOn, (CFStringRef) assertionName, &powerAssertion) != kIOReturnSuccess;
  91. if(success) powerAssertion = kIOPMNullAssertionID;
  92. }
  93. }
  94. }
  95. auto pApplication::initialize() -> void {
  96. @autoreleasepool {
  97. [NSApplication sharedApplication];
  98. cocoaDelegate = [[CocoaDelegate alloc] init];
  99. [NSApp setDelegate:cocoaDelegate];
  100. }
  101. }
  102. }
  103. #endif