Browse Source

unix: add ioctl functions to get/set terminal window size on Linux

Add IoctlGetWinsize and IoctlSetWinsize to retreive and manipulate
Winsize structures on Linux, akin to the already existing implementation
on Solaris.

Also remove the named result parameter for IoctlSetInt and
IoctlSetTermios, as they add no additional use.

Change-Id: Id349d1d6a21d5c9a05943f4dcc3a275613ccf7b8
Reviewed-on: https://go-review.googlesource.com/49231
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Tobias Klauser 8 years ago
parent
commit
cd2c276457

+ 3 - 0
unix/linux/types.go

@@ -28,6 +28,7 @@ package unix
 #include <stdio.h>
 #include <sys/epoll.h>
 #include <sys/inotify.h>
+#include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/mount.h>
 #include <sys/param.h>
@@ -543,3 +544,5 @@ const _SC_PAGESIZE = C._SC_PAGESIZE
 // Terminal handling
 
 type Termios C.termios_t
+
+type Winsize C.struct_winsize

+ 12 - 2
unix/syscall_linux.go

@@ -57,11 +57,15 @@ func Fchmodat(dirfd int, path string, mode uint32, flags int) (err error) {
 
 // IoctlSetInt performs an ioctl operation which sets an integer value
 // on fd, using the specified request number.
-func IoctlSetInt(fd int, req uint, value int) (err error) {
+func IoctlSetInt(fd int, req uint, value int) error {
 	return ioctl(fd, req, uintptr(value))
 }
 
-func IoctlSetTermios(fd int, req uint, value *Termios) (err error) {
+func IoctlSetWinsize(fd int, req uint, value *Winsize) error {
+	return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
+}
+
+func IoctlSetTermios(fd int, req uint, value *Termios) error {
 	return ioctl(fd, req, uintptr(unsafe.Pointer(value)))
 }
 
@@ -73,6 +77,12 @@ func IoctlGetInt(fd int, req uint) (int, error) {
 	return value, err
 }
 
+func IoctlGetWinsize(fd int, req uint) (*Winsize, error) {
+	var value Winsize
+	err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))
+	return &value, err
+}
+
 func IoctlGetTermios(fd int, req uint) (*Termios, error) {
 	var value Termios
 	err := ioctl(fd, req, uintptr(unsafe.Pointer(&value)))

+ 7 - 0
unix/ztypes_linux_386.go

@@ -676,3 +676,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_amd64.go

@@ -694,3 +694,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_arm.go

@@ -665,3 +665,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_arm64.go

@@ -673,3 +673,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_mips.go

@@ -670,3 +670,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_mips64.go

@@ -675,3 +675,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_mips64le.go

@@ -675,3 +675,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_mipsle.go

@@ -670,3 +670,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_ppc64.go

@@ -683,3 +683,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_ppc64le.go

@@ -683,3 +683,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}

+ 7 - 0
unix/ztypes_linux_s390x.go

@@ -700,3 +700,10 @@ type Termios struct {
 	Ispeed uint32
 	Ospeed uint32
 }
+
+type Winsize struct {
+	Row    uint16
+	Col    uint16
+	Xpixel uint16
+	Ypixel uint16
+}