Skip to content

Commit

Permalink
manually handle the SIGINT and quit
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed May 25, 2016
1 parent 86d80b3 commit ca1644f
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions damnedlife.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"log"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"

gc "github.com/rthornton128/goncurses"
Expand Down Expand Up @@ -114,7 +116,7 @@ func generationTimer(world *game.World, newGeneration chan<- bool, quit <-chan b
}
}

func keyPresses(field *gc.Window, originMoved chan<- game.Point, quit chan<- bool) {
func keyPresses(field *gc.Window, originMoved chan<- game.Point, quit chan bool) {
origin := game.Point{0, 0}
keys:
for {
Expand All @@ -123,37 +125,37 @@ keys:
// a key
switch field.GetChar() {
case 'h':
gc.FlushInput()
origin.X--
originMoved <- origin
continue keys
case 'j':
gc.FlushInput()
origin.Y++
originMoved <- origin
continue keys
case 'k':
gc.FlushInput()
origin.Y--
originMoved <- origin
continue keys
case 'l':
gc.FlushInput()
origin.X++
originMoved <- origin
continue keys
case '0':
gc.FlushInput()
origin.X, origin.Y = 0, 0
originMoved <- origin
continue keys
case 'q':
gc.FlushInput()
quit <- true
close(originMoved)
close(quit)
log.Println("[KP] leaving keyPresses, closed the channels")
return
case 0:
select {
case <-time.After(time.Millisecond * 1):
continue keys
case <-quit:
return
}
}
}
}
Expand Down Expand Up @@ -198,6 +200,19 @@ redraw:

}

func catchSigInt(quit chan bool) {
var c chan os.Signal
c = make(chan os.Signal)
gc.CBreak(true)
signal.Notify(c, syscall.SIGINT)
for {
<-c
log.Println("trying to quit")
close(quit)
return
}
}

/* want the following
┌────────────────────┐
Expand Down Expand Up @@ -300,6 +315,7 @@ func main() {
var quit chan bool = make(chan bool)
var worldLocker *sync.RWMutex = &sync.RWMutex{}

go catchSigInt(quit)
go keyPresses(field, originMoved, quit) // producter of origini change commands, and quit. closes originMoved when done
go generationTimer(world, newGeneration, quit, worldLocker) // produces ticks on the newGeneration channel, waits for a quit command to end. closes newGeneration when done
redrawConsumer(title, gameBoard, footer, world, originMoved, newGeneration, worldLocker) // listens to orignMoved and newGeneration, quits when both are done.
Expand Down

0 comments on commit ca1644f

Please sign in to comment.