Skip to content

Commit

Permalink
Use GCD for SIGTERM signal handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bensge committed Feb 28, 2017
1 parent daf2c88 commit 274357b
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions NativeDisplayBrightness/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ @interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (nonatomic) float brightness;
@property (strong, nonatomic) dispatch_source_t signalHandlerSource;
@end

@implementation AppDelegate
Expand Down Expand Up @@ -152,7 +153,6 @@ - (void)_loadBrightness

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
delegateInstance = self;
if (![self _loadBezelServices])
{
[self _loadOSDFramework];
Expand All @@ -164,16 +164,21 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[self _registerSignalHandling];
}

__weak AppDelegate *delegateInstance = nil;

void shutdownSignalHandler(int signal) {
NSLog(@"Caught SIGTERM");
[delegateInstance _willTerminate];
exit(0);
void shutdownSignalHandler(int signal)
{
//Don't do anything
}

- (void)_registerSignalHandling
{
//Register signal callback that will gracefully shut the application down
self.signalHandlerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
dispatch_source_set_event_handler(self.signalHandlerSource, ^{
NSLog(@"Caught SIGTERM");
[[NSApplication sharedApplication] terminate:self];
});
dispatch_resume(self.signalHandlerSource);
//Register signal handler that will prevent the app from being killed
signal(SIGTERM, shutdownSignalHandler);
}

Expand Down

0 comments on commit 274357b

Please sign in to comment.