term/termios_linux.go
745edc49
 package term
08ac1d0b
 
b00ff479
 import (
50bee2f8
 	"syscall"
 	"unsafe"
b00ff479
 )
08ac1d0b
 
 const (
 	getTermios = syscall.TCGETS
 	setTermios = syscall.TCSETS
 )
b00ff479
 
a70dd659
 type Termios struct {
 	Iflag  uint32
 	Oflag  uint32
 	Cflag  uint32
 	Lflag  uint32
 	Cc     [20]byte
 	Ispeed uint32
 	Ospeed uint32
 }
 
b00ff479
 // 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.
31eb01ae
 func MakeRaw(fd uintptr) (*State, error) {
50bee2f8
 	var oldState State
31eb01ae
 	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
50bee2f8
 		return nil, err
 	}
 
64f34677
 	newState := oldState.termios
50bee2f8
 
64f34677
 	newState.Iflag &^= (syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK | syscall.ISTRIP | syscall.INLCR | syscall.IGNCR | syscall.ICRNL | syscall.IXON)
 	newState.Oflag &^= syscall.OPOST
 	newState.Lflag &^= (syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.ISIG | syscall.IEXTEN)
 	newState.Cflag &^= (syscall.CSIZE | syscall.PARENB)
 	newState.Cflag |= syscall.CS8
50bee2f8
 
31eb01ae
 	if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
64f34677
 		return nil, err
 	}
 	return &oldState, nil
50bee2f8
 }