2020-04-16 05:23:37 -04:00
|
|
|
package term
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-10 18:41:46 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-05-10 18:41:46 -04:00
|
|
|
getTermios = unix.TCGETS
|
|
|
|
setTermios = unix.TCSETS
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Termios is the Unix API for terminal I/O.
|
2017-05-10 18:41:46 -04:00
|
|
|
type Termios unix.Termios
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// MakeRaw put the terminal connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
2017-08-07 05:52:40 -04:00
|
|
|
termios, err := unix.IoctlGetTermios(int(fd), getTermios)
|
|
|
|
if err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
var oldState State
|
|
|
|
oldState.termios = Termios(*termios)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-08-07 05:52:40 -04: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
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2017-08-07 05:52:40 -04:00
|
|
|
if err := unix.IoctlSetTermios(int(fd), setTermios, termios); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &oldState, nil
|
|
|
|
}
|