--- tty.py-orig Sun Dec 19 15:14:41 2004 +++ tty.py Sun Dec 19 15:21:11 2004 @@ -3,8 +3,9 @@ # Author: Steen Lumholt. from termios import * +from copy import deepcopy -__all__ = ["setraw", "setcbreak"] +__all__ = ["setraw", "setcbreak", "setsane"] # Indexes for termios list. IFLAG = 0 @@ -15,9 +16,13 @@ OSPEED = 5 CC = 6 +_mode = None + def setraw(fd, when=TCSAFLUSH): """Put terminal into a raw mode.""" + global _mode mode = tcgetattr(fd) + _mode = deepcopy(mode) mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON) mode[OFLAG] = mode[OFLAG] & ~(OPOST) mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB) @@ -29,8 +34,17 @@ def setcbreak(fd, when=TCSAFLUSH): """Put terminal into a cbreak mode.""" + global _mode mode = tcgetattr(fd) + _mode = deepcopy(mode) mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON) mode[CC][VMIN] = 1 mode[CC][VTIME] = 0 tcsetattr(fd, when, mode) + +def setsane(fd, when=TCSAFLUSH): + """Put terminal in a sane mode""" + global _mode + if _mode: + tcsetattr(fd, when, _mode) + _mode = None