termios_speed
[svn42.git] / go / termios / termios.go
index 11325c9..f3f0077 100644 (file)
@@ -1,7 +1,6 @@
 package termios
 
 import (
-    "fmt";
     "os";
     "syscall";
     "unsafe"
@@ -10,26 +9,60 @@ import (
 
 // termios types
 type cc_t byte
-type speed_t uint
-type tcflag_t uint
+type speed_t uint32
+type tcflag_t uint32
 
 // termios constants
 const (
-    BRKINT = tcflag_t (0000002);
-    ICRNL = tcflag_t (0000400);
-    INPCK = tcflag_t (0000020);
-    ISTRIP = tcflag_t (0000040);
-    IXON = tcflag_t (0002000);
-    OPOST = tcflag_t (0000001);
-    CS8 = tcflag_t (0000060);
-    ECHO = tcflag_t (0000010);
-    ICANON = tcflag_t (0000002);
-    IEXTEN = tcflag_t (0100000);
-    ISIG = tcflag_t (0000001);
-    VTIME = tcflag_t (5);
+    BRKINT = tcflag_t (0000002)
+    ICRNL = tcflag_t (0000400)
+    INPCK = tcflag_t (0000020)
+    ISTRIP = tcflag_t (0000040)
+    IXON = tcflag_t (0002000)
+    OPOST = tcflag_t (0000001)
+    CS8 = tcflag_t (0000060)
+    ECHO = tcflag_t (0000010)
+    ICANON = tcflag_t (0000002)
+    IEXTEN = tcflag_t (0100000)
+    ISIG = tcflag_t (0000001)
+    VTIME = tcflag_t (5)
     VMIN = tcflag_t (6)
 )
 
+const (
+    B0 = speed_t(0000000)         /* hang up */
+    B50 = speed_t(0000001)
+    B75 = speed_t(0000002)
+    B110 = speed_t(0000003)
+    B134 = speed_t(0000004)
+    B150 = speed_t(0000005)
+    B200 = speed_t(0000006)
+    B300 = speed_t(0000007)
+    B600 = speed_t(0000010)
+    B1200 = speed_t(0000011)
+    B1800 = speed_t(0000012)
+    B2400 = speed_t(0000013)
+    B4800 = speed_t(0000014)
+    B9600 = speed_t(0000015)
+    B19200 = speed_t(0000016)
+    B38400 = speed_t(0000017)
+    B57600 = speed_t(0010001)
+    B115200 = speed_t(0010002)
+    B230400 = speed_t(0010003)
+    B460800 = speed_t(0010004)
+    B500000 = speed_t(0010005)
+    B576000 = speed_t(0010006)
+    B921600 = speed_t(0010007)
+    B1000000 = speed_t(0010010)
+    B1152000 = speed_t(0010011)
+    B1500000 = speed_t(0010012)
+    B2000000 = speed_t(0010013)
+    B2500000 = speed_t(0010014)
+    B3000000 = speed_t(0010015)
+    B3500000 = speed_t(0010016)
+    B4000000 = speed_t(0010017)
+)
+
 const NCCS = 32
 type termios struct {
     c_iflag, c_oflag, c_cflag, c_lflag tcflag_t;
@@ -40,7 +73,7 @@ type termios struct {
 
 // ioctl constants
 const (
-    TCGETS = 0x5401;
+    TCGETS = 0x5401
     TCSETS = 0x5402
 )
 
@@ -65,7 +98,6 @@ func getTermios (dst *termios) error {
     if r1 != 0 {
     //    return errors.New("Error")
     }
-
     return nil
 }
 
@@ -79,43 +111,28 @@ func setTermios (src *termios) error {
     }
 
     if r1 != 0 {
-        return errors.New ("Error")
+        return errors.New ("Error during ioctl tcsets syscall")
     }
-
     return nil
 }
 
-func tty_raw () error {
-    raw := orig_termios;
-
-    raw.c_iflag &= ^(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
-    raw.c_oflag &= ^(OPOST);
-    raw.c_cflag |= (CS8);
-    raw.c_lflag &= ^(ECHO | ICANON | IEXTEN | ISIG);
+func SetRaw () (error) {
+    if err := getTermios (&orig_termios); err != nil { return err}
 
-    raw.c_cc[VMIN] = 1;
-    raw.c_cc[VTIME] = 0;
+    orig_termios.c_iflag &= ^(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+    orig_termios.c_oflag &= ^(OPOST);
+    orig_termios.c_cflag |= (CS8);
+    orig_termios.c_lflag &= ^(ECHO | ICANON | IEXTEN | ISIG);
 
-    if err := setTermios (&raw); err != nil { return err }
+    orig_termios.c_cc[VMIN] = 1;
+    orig_termios.c_cc[VTIME] = 0;
 
-    return nil
+    return setTermios(&orig_termios)
 }
 
-func SetRaw () {
-    var (
-        err error
-    )
-
-    defer func () {
-        if err != nil { fmt.Printf ("SetRaw Error: %v\n",err) }
-    } ();
-
-    if err = getTermios (&orig_termios); err != nil { return }
-
-//    defer func () {
-//        err = setTermios (&orig_termios)
-//    } ();
-
-    if err = tty_raw (); err != nil { return }
-    //if err = screenio (); err != nil { return }
+func SetSpeed (speed speed_t) (error) {
+    if err := getTermios (&orig_termios); err != nil { return err }
+    orig_termios.c_ispeed = speed
+    orig_termios.c_ospeed = speed
+    return setTermios(&orig_termios)
 }