From 8c328d3eac0f43880ad7a28468248074ed190fb8 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 17 Jun 2017 19:25:35 -0500 Subject: [PATCH] handle signals --- bg/background.go | 11 +++++++++++ events/background_event.go | 2 ++ ui/ui_darwin.go | 15 +++++++++++++-- ui/ui_darwin.h | 5 +++-- ui/ui_darwin.m | 8 ++++++-- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/bg/background.go b/bg/background.go index 70ba8c2..f55757f 100644 --- a/bg/background.go +++ b/bg/background.go @@ -1,6 +1,9 @@ package bg import ( + "os" + "os/signal" + "github.com/jordanorelli/dws/events" ) @@ -10,6 +13,7 @@ func Run(out chan events.BackgroundEvent, in chan events.UserEvent) { out: out, server: newServer(), } + go bg.handleSignals() go bg.listen() bg.run() } @@ -28,3 +32,10 @@ func (bg *background) run() { } } } + +func (bg *background) handleSignals() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + bg.out <- events.SigIntEvent{} +} diff --git a/events/background_event.go b/events/background_event.go index d70f0e3..e18c66e 100644 --- a/events/background_event.go +++ b/events/background_event.go @@ -8,3 +8,5 @@ type BackgroundEvent interface { type backgroundEvent struct{ event } func (e backgroundEvent) isBackgroundEvent() {} + +type SigIntEvent struct{ backgroundEvent } diff --git a/ui/ui_darwin.go b/ui/ui_darwin.go index 1ba570e..f647d62 100644 --- a/ui/ui_darwin.go +++ b/ui/ui_darwin.go @@ -21,7 +21,7 @@ func Desktop() UI { } log.Println("Creating new Cocoa UI") - C.Initialize() + C.initialize() desktopUI = new(cocoaUI) return desktopUI } @@ -35,10 +35,21 @@ func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent log.Println("Running Desktop UI") ui.in = in ui.out = out - C.Run() + go ui.forwardEvents() + C.run() return nil } +func (ui *cocoaUI) forwardEvents() { + for e := range ui.in { + switch e.(type) { + case events.SigIntEvent: + log.Println("Cocoa UI sees sig int, forwarding to NSApp") + C.shutdown() + } + } +} + //export selectDirectory func selectDirectory(cpath *C.char) { path := C.GoString(cpath) diff --git a/ui/ui_darwin.h b/ui/ui_darwin.h index 21275c4..48a1c96 100644 --- a/ui/ui_darwin.h +++ b/ui/ui_darwin.h @@ -1,2 +1,3 @@ -void Initialize(void); -int Run(void); +void initialize(); +int run(); +void shutdown(); diff --git a/ui/ui_darwin.m b/ui/ui_darwin.m index 5ffd0d7..1989b8e 100644 --- a/ui/ui_darwin.m +++ b/ui/ui_darwin.m @@ -4,15 +4,19 @@ id defaultAutoreleasePool; id appDelegate; -void Initialize(void) { +void initialize() { defaultAutoreleasePool = [NSAutoreleasePool new]; [NSApplication sharedApplication]; [NSApp setDelegate: [[AppDelegate new] autorelease]]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; } -int Run(void) { +int run() { [NSApp run]; [defaultAutoreleasePool drain]; return 0; } + +void shutdown() { + [[NSApplication sharedApplication] terminate:nil]; +}