handle signals

master
Jordan Orelli 8 years ago
parent a68425d1fb
commit 8c328d3eac

@ -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{}
}

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

@ -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)

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

@ -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];
}

Loading…
Cancel
Save