termios_speed
[svn42.git] / go / termios / termios.go
1 package termios
2
3 import (
4     "os";
5     "syscall";
6     "unsafe"
7     "errors"
8 )
9
10 // termios types
11 type cc_t byte
12 type speed_t uint32
13 type tcflag_t uint32
14
15 // termios constants
16 const (
17     BRKINT = tcflag_t (0000002)
18     ICRNL = tcflag_t (0000400)
19     INPCK = tcflag_t (0000020)
20     ISTRIP = tcflag_t (0000040)
21     IXON = tcflag_t (0002000)
22     OPOST = tcflag_t (0000001)
23     CS8 = tcflag_t (0000060)
24     ECHO = tcflag_t (0000010)
25     ICANON = tcflag_t (0000002)
26     IEXTEN = tcflag_t (0100000)
27     ISIG = tcflag_t (0000001)
28     VTIME = tcflag_t (5)
29     VMIN = tcflag_t (6)
30 )
31
32 const (
33     B0 = speed_t(0000000)         /* hang up */
34     B50 = speed_t(0000001)
35     B75 = speed_t(0000002)
36     B110 = speed_t(0000003)
37     B134 = speed_t(0000004)
38     B150 = speed_t(0000005)
39     B200 = speed_t(0000006)
40     B300 = speed_t(0000007)
41     B600 = speed_t(0000010)
42     B1200 = speed_t(0000011)
43     B1800 = speed_t(0000012)
44     B2400 = speed_t(0000013)
45     B4800 = speed_t(0000014)
46     B9600 = speed_t(0000015)
47     B19200 = speed_t(0000016)
48     B38400 = speed_t(0000017)
49     B57600 = speed_t(0010001)
50     B115200 = speed_t(0010002)
51     B230400 = speed_t(0010003)
52     B460800 = speed_t(0010004)
53     B500000 = speed_t(0010005)
54     B576000 = speed_t(0010006)
55     B921600 = speed_t(0010007)
56     B1000000 = speed_t(0010010)
57     B1152000 = speed_t(0010011)
58     B1500000 = speed_t(0010012)
59     B2000000 = speed_t(0010013)
60     B2500000 = speed_t(0010014)
61     B3000000 = speed_t(0010015)
62     B3500000 = speed_t(0010016)
63     B4000000 = speed_t(0010017)
64 )
65
66 const NCCS = 32
67 type termios struct {
68     c_iflag, c_oflag, c_cflag, c_lflag tcflag_t;
69     c_line cc_t;
70     c_cc [NCCS]cc_t;
71     c_ispeed, c_ospeed speed_t
72 }
73
74 // ioctl constants
75 const (
76     TCGETS = 0x5401
77     TCSETS = 0x5402
78 )
79
80 var (
81     orig_termios termios;
82     ttyfd uintptr = 0 // STDIN_FILENO
83 )
84
85 func Ttyfd(fd uintptr) {
86   ttyfd=fd
87 }
88
89 func getTermios (dst *termios) error {
90     r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL,
91                                      uintptr (ttyfd), uintptr (TCGETS),
92                                      uintptr (unsafe.Pointer (dst)));
93
94     if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 && err != nil {
95         return err
96     }
97
98     if r1 != 0 {
99     //    return errors.New("Error")
100     }
101     return nil
102 }
103
104 func setTermios (src *termios) error {
105     r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL,
106                                      uintptr (ttyfd), uintptr (TCSETS),
107                                      uintptr (unsafe.Pointer (src)));
108
109     if err := os.NewSyscallError ("SYS_IOCTL", errno); errno!=0 &&err != nil {
110         return err
111     }
112
113     if r1 != 0 {
114         return errors.New ("Error during ioctl tcsets syscall")
115     }
116     return nil
117 }
118
119 func SetRaw () (error) {
120     if err := getTermios (&orig_termios); err != nil { return err}
121
122     orig_termios.c_iflag &= ^(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
123     orig_termios.c_oflag &= ^(OPOST);
124     orig_termios.c_cflag |= (CS8);
125     orig_termios.c_lflag &= ^(ECHO | ICANON | IEXTEN | ISIG);
126
127     orig_termios.c_cc[VMIN] = 1;
128     orig_termios.c_cc[VTIME] = 0;
129
130     return setTermios(&orig_termios)
131 }
132
133 func SetSpeed (speed speed_t) (error) {
134     if err := getTermios (&orig_termios); err != nil { return err }
135     orig_termios.c_ispeed = speed
136     orig_termios.c_ospeed = speed
137     return setTermios(&orig_termios)
138 }