termios_linux.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package term
  2. import (
  3. "unsafe"
  4. "golang.org/x/sys/unix"
  5. )
  6. const (
  7. getTermios = unix.TCGETS
  8. setTermios = unix.TCSETS
  9. )
  10. // Termios is the Unix API for terminal I/O.
  11. type Termios unix.Termios
  12. // MakeRaw put the terminal connected to the given file descriptor into raw
  13. // mode and returns the previous state of the terminal so that it can be
  14. // restored.
  15. func MakeRaw(fd uintptr) (*State, error) {
  16. var oldState State
  17. if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, getTermios, uintptr(unsafe.Pointer(&oldState.termios))); err != 0 {
  18. return nil, err
  19. }
  20. newState := oldState.termios
  21. newState.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
  22. newState.Oflag |= unix.OPOST
  23. newState.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN)
  24. newState.Cflag &^= (unix.CSIZE | unix.PARENB)
  25. newState.Cflag |= unix.CS8
  26. if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, setTermios, uintptr(unsafe.Pointer(&newState))); err != 0 {
  27. return nil, err
  28. }
  29. return &oldState, nil
  30. }