2019-02-27 11:01:02 -05:00
|
|
|
// +build !windows
|
|
|
|
// +build !plan9
|
|
|
|
|
|
|
|
package tty
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TTY struct {
|
|
|
|
in *os.File
|
|
|
|
bin *bufio.Reader
|
|
|
|
out *os.File
|
|
|
|
termios syscall.Termios
|
|
|
|
ss chan os.Signal
|
|
|
|
}
|
|
|
|
|
|
|
|
func open() (*TTY, error) {
|
|
|
|
tty := new(TTY)
|
|
|
|
|
|
|
|
in, err := os.Open("/dev/tty")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tty.in = in
|
|
|
|
tty.bin = bufio.NewReader(in)
|
|
|
|
|
|
|
|
out, err := os.OpenFile("/dev/tty", syscall.O_WRONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tty.out = out
|
|
|
|
|
|
|
|
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlReadTermios, uintptr(unsafe.Pointer(&tty.termios)), 0, 0, 0); err != 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newios := tty.termios
|
2019-11-12 13:14:37 -05:00
|
|
|
newios.Iflag &^= syscall.ISTRIP | syscall.INLCR | syscall.ICRNL | syscall.IGNCR | syscall.IXOFF
|
2019-02-27 11:01:02 -05:00
|
|
|
newios.Lflag &^= syscall.ECHO | syscall.ICANON /*| syscall.ISIG*/
|
|
|
|
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&newios)), 0, 0, 0); err != 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tty.ss = make(chan os.Signal, 1)
|
2019-04-18 07:20:41 -04:00
|
|
|
|
2019-02-27 11:01:02 -05:00
|
|
|
return tty, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) buffered() bool {
|
|
|
|
return tty.bin.Buffered() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) readRune() (rune, error) {
|
|
|
|
r, _, err := tty.bin.ReadRune()
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) close() error {
|
2019-04-02 04:15:29 -04:00
|
|
|
signal.Stop(tty.ss)
|
2019-02-27 11:01:02 -05:00
|
|
|
close(tty.ss)
|
|
|
|
_, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(tty.in.Fd()), ioctlWriteTermios, uintptr(unsafe.Pointer(&tty.termios)), 0, 0, 0)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) size() (int, int, error) {
|
2019-04-18 07:20:41 -04:00
|
|
|
x, y, _, _, err := tty.sizePixel()
|
|
|
|
return x, y, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) sizePixel() (int, int, int, int, error) {
|
2019-02-27 11:01:02 -05:00
|
|
|
var dim [4]uint16
|
|
|
|
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(tty.out.Fd()), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&dim)), 0, 0, 0); err != 0 {
|
2019-04-18 07:20:41 -04:00
|
|
|
return -1, -1, -1, -1, err
|
2019-02-27 11:01:02 -05:00
|
|
|
}
|
2019-04-18 07:20:41 -04:00
|
|
|
return int(dim[1]), int(dim[0]), int(dim[2]), int(dim[3]), nil
|
2019-02-27 11:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) input() *os.File {
|
|
|
|
return tty.in
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) output() *os.File {
|
|
|
|
return tty.out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tty *TTY) raw() (func() error, error) {
|
|
|
|
termios, err := unix.IoctlGetTermios(int(tty.in.Fd()), ioctlReadTermios)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-22 09:24:52 -04:00
|
|
|
backup := *termios
|
2019-02-27 11:01:02 -05:00
|
|
|
|
|
|
|
termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
|
|
|
|
termios.Oflag &^= unix.OPOST
|
|
|
|
termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
|
|
|
|
termios.Cflag &^= unix.CSIZE | unix.PARENB
|
|
|
|
termios.Cflag |= unix.CS8
|
|
|
|
termios.Cc[unix.VMIN] = 1
|
|
|
|
termios.Cc[unix.VTIME] = 0
|
|
|
|
if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, termios); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() error {
|
2019-03-22 09:24:52 -04:00
|
|
|
if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, &backup); err != nil {
|
2019-02-27 11:01:02 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-18 07:20:41 -04:00
|
|
|
func (tty *TTY) sigwinch() <-chan WINSIZE {
|
|
|
|
signal.Notify(tty.ss, syscall.SIGWINCH)
|
|
|
|
|
|
|
|
ws := make(chan WINSIZE)
|
|
|
|
go func() {
|
|
|
|
defer close(ws)
|
|
|
|
for sig := range tty.ss {
|
|
|
|
if sig != syscall.SIGWINCH {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h, err := tty.size()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// send but do not block for it
|
|
|
|
select {
|
|
|
|
case ws <- WINSIZE{W: w, H: h}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ws
|
2019-02-27 11:01:02 -05:00
|
|
|
}
|