handle signals

master
Jordan Orelli 8 years ago
parent a68425d1fb
commit 8c328d3eac

@ -1,6 +1,9 @@
package bg package bg
import ( import (
"os"
"os/signal"
"github.com/jordanorelli/dws/events" "github.com/jordanorelli/dws/events"
) )
@ -10,6 +13,7 @@ func Run(out chan events.BackgroundEvent, in chan events.UserEvent) {
out: out, out: out,
server: newServer(), server: newServer(),
} }
go bg.handleSignals()
go bg.listen() go bg.listen()
bg.run() 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{}
}

@ -8,3 +8,5 @@ type BackgroundEvent interface {
type backgroundEvent struct{ event } type backgroundEvent struct{ event }
func (e backgroundEvent) isBackgroundEvent() {} func (e backgroundEvent) isBackgroundEvent() {}
type SigIntEvent struct{ backgroundEvent }

@ -21,7 +21,7 @@ func Desktop() UI {
} }
log.Println("Creating new Cocoa UI") log.Println("Creating new Cocoa UI")
C.Initialize() C.initialize()
desktopUI = new(cocoaUI) desktopUI = new(cocoaUI)
return desktopUI return desktopUI
} }
@ -35,10 +35,21 @@ func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent
log.Println("Running Desktop UI") log.Println("Running Desktop UI")
ui.in = in ui.in = in
ui.out = out ui.out = out
C.Run() go ui.forwardEvents()
C.run()
return nil 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 //export selectDirectory
func selectDirectory(cpath *C.char) { func selectDirectory(cpath *C.char) {
path := C.GoString(cpath) path := C.GoString(cpath)

@ -1,2 +1,3 @@
void Initialize(void); void initialize();
int Run(void); int run();
void shutdown();

@ -4,15 +4,19 @@
id defaultAutoreleasePool; id defaultAutoreleasePool;
id appDelegate; id appDelegate;
void Initialize(void) { void initialize() {
defaultAutoreleasePool = [NSAutoreleasePool new]; defaultAutoreleasePool = [NSAutoreleasePool new];
[NSApplication sharedApplication]; [NSApplication sharedApplication];
[NSApp setDelegate: [[AppDelegate new] autorelease]]; [NSApp setDelegate: [[AppDelegate new] autorelease]];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
} }
int Run(void) { int run() {
[NSApp run]; [NSApp run];
[defaultAutoreleasePool drain]; [defaultAutoreleasePool drain];
return 0; return 0;
} }
void shutdown() {
[[NSApplication sharedApplication] terminate:nil];
}

Loading…
Cancel
Save