2013-03-13 03:29:40 -04:00
|
|
|
package term
|
2013-01-28 20:06:46 -05:00
|
|
|
|
2013-03-22 07:24:03 -04:00
|
|
|
import (
|
2013-03-25 21:43:25 -04:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2013-03-22 07:24:03 -04:00
|
|
|
)
|
2013-01-28 20:06:46 -05:00
|
|
|
|
2013-03-25 21:43:25 -04:00
|
|
|
// #include <termios.h>
|
|
|
|
// #include <sys/ioctl.h>
|
|
|
|
/*
|
2013-03-25 22:31:20 -04:00
|
|
|
void MakeRaw(int fd) {
|
2013-03-25 21:43:25 -04:00
|
|
|
struct termios t;
|
|
|
|
|
|
|
|
// FIXME: Handle errors?
|
2013-03-25 22:31:20 -04:00
|
|
|
ioctl(fd, TCGETS, &t);
|
2013-03-25 21:43:25 -04:00
|
|
|
|
|
|
|
t.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
2013-04-04 15:54:53 -04:00
|
|
|
t.c_oflag &= ~OPOST;
|
|
|
|
t.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
|
2013-03-25 21:43:25 -04:00
|
|
|
t.c_cflag &= ~(CSIZE | PARENB);
|
|
|
|
t.c_cflag |= CS8;
|
|
|
|
|
2013-03-25 22:31:20 -04:00
|
|
|
ioctl(fd, TCSETS, &t);
|
2013-03-25 21:43:25 -04:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2013-01-28 20:06:46 -05:00
|
|
|
const (
|
|
|
|
getTermios = syscall.TCGETS
|
|
|
|
setTermios = syscall.TCSETS
|
|
|
|
)
|
2013-03-22 07:24:03 -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 int) (*State, error) {
|
2013-03-25 21:43:25 -04:00
|
|
|
var oldState State
|
|
|
|
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(fd), syscall.TCGETS, uintptr(unsafe.Pointer(&oldState.termios)), 0, 0, 0); err != 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-03-25 22:31:20 -04:00
|
|
|
C.MakeRaw(C.int(fd))
|
2013-03-25 21:43:25 -04:00
|
|
|
return &oldState, nil
|
|
|
|
|
|
|
|
// FIXME: post on goland issues this: very same as the C function bug non-working
|
|
|
|
|
|
|
|
// newState := oldState.termios
|
|
|
|
|
|
|
|
// newState.Iflag &^= (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON)
|
|
|
|
// newState.Oflag &^= OPOST
|
|
|
|
// newState.Lflag &^= (ECHO | syscall.ECHONL | ICANON | ISIG | IEXTEN)
|
|
|
|
// newState.Cflag &^= (CSIZE | syscall.PARENB)
|
|
|
|
// newState.Cflag |= CS8
|
|
|
|
|
|
|
|
// if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.TCSETS, uintptr(unsafe.Pointer(&newState))); err != 0 {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// return &oldState, nil
|
|
|
|
}
|