Skip to content

Commit

Permalink
handle channel killing
Browse files Browse the repository at this point in the history
  • Loading branch information
lyubchev committed Sep 11, 2020
1 parent 56499c2 commit 9e9cfc2
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"image"
"image/color"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/JamesHovious/w32"
Expand Down Expand Up @@ -101,7 +103,7 @@ func main() {

if ev.Message == types.WM_KEYUP && ev.VKCode == types.VK_3 {
if !isRunning {
go Play(qc, levelsToPass, c.x0, c.y0, c.x1, c.y1)
go Play(kbC, qc, levelsToPass, c.x0, c.y0, c.x1, c.y1)
isRunning = true
}
}
Expand Down Expand Up @@ -145,7 +147,7 @@ func RegKbHook() (chan types.KeyboardEvent, error) {
return keyboardChan, nil
}

func Play(qc chan struct{}, levelsToPass, x0, y0, x1, y1 int) {
func Play(kbC chan types.KeyboardEvent, qc chan struct{}, levelsToPass, x0, y0, x1, y1 int) {
bounds := image.Rect(x0, y0, x1, y1)
img, err := screenshot.CaptureRect(bounds)
if err != nil {
Expand Down Expand Up @@ -194,8 +196,15 @@ func Play(qc chan struct{}, levelsToPass, x0, y0, x1, y1 int) {
}
}

termC := make(chan os.Signal, 100)
signal.Notify(termC, syscall.SIGINT, syscall.SIGTERM)

select {
case <-termC:
close(kbC)
return
case <-qc:
close(kbC)
return
default:
for y := screenshotBounds.Min.Y; y < height; y += 10 {
Expand Down

0 comments on commit 9e9cfc2

Please sign in to comment.