Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix idle wakeup problem #130

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type tScreen struct {
sigwinch chan os.Signal
quit chan struct{}
indoneq chan struct{}
inputchan chan InputPacket
keyexist map[Key]bool
keycodes map[string]*tKeyCode
cx int
Expand All @@ -102,6 +103,12 @@ type tScreen struct {
sync.Mutex
}

type InputPacket struct {
n int
e error
chunk []byte
}

func (t *tScreen) Init() error {
t.evch = make(chan Event, 10)
t.indoneq = make(chan struct{})
Expand Down Expand Up @@ -386,6 +393,7 @@ func (t *tScreen) Fini() {
t.curstyle = Style(-1)
t.clear = false
t.fini = true
close(t.inputchan)
t.Unlock()

if t.quit != nil {
Expand Down Expand Up @@ -1235,9 +1243,16 @@ func (t *tScreen) scanInput(buf *bytes.Buffer, expire bool) {
}

func (t *tScreen) inputLoop() {
t.inputchan = make(chan InputPacket)
buf := &bytes.Buffer{}

chunk := make([]byte, 128)
go func() {
chunk := make([]byte, 128)
for {
n, e := t.in.Read(chunk)
t.inputchan <- InputPacket{n, e, chunk}
}
}()
for {
select {
case <-t.quit:
Expand All @@ -1254,7 +1269,14 @@ func (t *tScreen) inputLoop() {
continue
default:
}
n, e := t.in.Read(chunk)

in, ok := <-t.inputchan
if !ok {
close(t.indoneq)
return
}
n, e, chunk := in.n, in.e, in.chunk

switch e {
case io.EOF:
// If we timeout waiting for more bytes, then it's
Expand Down
7 changes: 2 additions & 5 deletions tscreen_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (t *tScreen) termioInit() error {
// close file descriptors on systems like Darwin, where close does
// cause a wakeup. (Probably we could reasonably increase this to
// something like 1 sec or 500 msec.)
newtios.Cc[syscall.VMIN] = 0
newtios.Cc[syscall.VTIME] = 1
newtios.Cc[syscall.VMIN] = 1
newtios.Cc[syscall.VTIME] = 0
tios = uintptr(unsafe.Pointer(&newtios))

ioc = uintptr(syscall.TIOCSETA)
Expand Down Expand Up @@ -107,9 +107,6 @@ func (t *tScreen) termioFini() {
syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0)
t.out.Close()
}
if t.in != nil {
t.in.Close()
}
}

func (t *tScreen) getWinSize() (int, int, error) {
Expand Down
7 changes: 2 additions & 5 deletions tscreen_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func (t *tScreen) termioInit() error {
// close file descriptors on systems like Darwin, where close does
// cause a wakeup. (Probably we could reasonably increase this to
// something like 1 sec or 500 msec.)
newtios.Cc[syscall.VMIN] = 0
newtios.Cc[syscall.VTIME] = 1
newtios.Cc[syscall.VMIN] = 1
newtios.Cc[syscall.VTIME] = 0
tios = uintptr(unsafe.Pointer(&newtios))

// Well this kind of sucks, because we don't have TCSETSF, but only
Expand Down Expand Up @@ -110,9 +110,6 @@ func (t *tScreen) termioFini() {
syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0)
t.out.Close()
}
if t.in != nil {
t.in.Close()
}
}

func (t *tScreen) getWinSize() (int, int, error) {
Expand Down
7 changes: 2 additions & 5 deletions tscreen_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func (t *tScreen) termioInit() error {
// close file descriptors on systems like Darwin, where close does
// cause a wakeup. (Probably we could reasonably increase this to
// something like 1 sec or 500 msec.)
newtios.c_cc[C.VMIN] = 0
newtios.c_cc[C.VTIME] = 1
newtios.c_cc[C.VMIN] = 1
newtios.c_cc[C.VTIME] = 0

if rv, e = C.tcsetattr(fd, C.TCSANOW|C.TCSAFLUSH, &newtios); rv != 0 {
goto failed
Expand Down Expand Up @@ -193,9 +193,6 @@ func (t *tScreen) termioFini() {
C.tcsetattr(fd, C.TCSANOW|C.TCSAFLUSH, &t.tiosp.tios)
t.out.Close()
}
if t.in != nil {
t.in.Close()
}
}

func (t *tScreen) getWinSize() (int, int, error) {
Expand Down