Skip to content

Commit 274357b

Browse files
committed
Use GCD for SIGTERM signal handling.
1 parent daf2c88 commit 274357b

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

NativeDisplayBrightness/AppDelegate.m

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ @interface AppDelegate ()
5959

6060
@property (weak) IBOutlet NSWindow *window;
6161
@property (nonatomic) float brightness;
62+
@property (strong, nonatomic) dispatch_source_t signalHandlerSource;
6263
@end
6364

6465
@implementation AppDelegate
@@ -152,7 +153,6 @@ - (void)_loadBrightness
152153

153154
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
154155
{
155-
delegateInstance = self;
156156
if (![self _loadBezelServices])
157157
{
158158
[self _loadOSDFramework];
@@ -164,16 +164,21 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
164164
[self _registerSignalHandling];
165165
}
166166

167-
__weak AppDelegate *delegateInstance = nil;
168-
169-
void shutdownSignalHandler(int signal) {
170-
NSLog(@"Caught SIGTERM");
171-
[delegateInstance _willTerminate];
172-
exit(0);
167+
void shutdownSignalHandler(int signal)
168+
{
169+
//Don't do anything
173170
}
174171

175172
- (void)_registerSignalHandling
176173
{
174+
//Register signal callback that will gracefully shut the application down
175+
self.signalHandlerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGTERM, 0, dispatch_get_main_queue());
176+
dispatch_source_set_event_handler(self.signalHandlerSource, ^{
177+
NSLog(@"Caught SIGTERM");
178+
[[NSApplication sharedApplication] terminate:self];
179+
});
180+
dispatch_resume(self.signalHandlerSource);
181+
//Register signal handler that will prevent the app from being killed
177182
signal(SIGTERM, shutdownSignalHandler);
178183
}
179184

0 commit comments

Comments
 (0)