From 10f5461cd868547cd82fcffaf0ce442136ae33f0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 30 Jan 2019 14:50:04 +0530 Subject: [PATCH] Nicer signal handling in cocoa for the notification run loop --- kitty/cocoa_window.m | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 41bc9c3ef..5c704349f 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -177,11 +177,6 @@ - (void) userNotificationCenter:(NSUserNotificationCenter *)center Py_RETURN_NONE; } -static void -SIGTERM_handler(int signum UNUSED) { - exit(EXIT_FAILURE); -} - static void call_timer_callback(PyObject *timer_callback) { PyObject *ret = PyObject_CallObject(timer_callback, NULL); @@ -194,12 +189,22 @@ - (void) userNotificationCenter:(NSUserNotificationCenter *)center PyObject *timer_callback; double timeout; if (!PyArg_ParseTuple(args, "OOd", ¬ification_activated_callback, &timer_callback, &timeout)) return NULL; - signal(SIGTERM, SIGTERM_handler); - signal(SIGINT, SIGTERM_handler); NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSApplication * application = [NSApplication sharedApplication]; // prevent icon in dock [application setActivationPolicy:NSApplicationActivationPolicyAccessory]; + signal(SIGTERM, SIG_IGN); + signal(SIGINT, SIG_IGN); + dispatch_source_t sigint = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, dispatch_get_main_queue()); + dispatch_source_set_event_handler(sigint, ^{ + [application terminate:nil]; + }); + dispatch_resume(sigint); + dispatch_source_t sigterm = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, dispatch_get_main_queue()); + dispatch_source_set_event_handler(sigterm, ^{ + [application terminate:nil]; + }); + dispatch_resume(sigterm); // timer will fire after timeout, so fire it once at the start call_timer_callback(timer_callback); [NSTimer scheduledTimerWithTimeInterval:timeout