Skip to content

Commit

Permalink
tscreen_*.go: Use unix.Ioctl* on BSD systems, deduplicate code
Browse files Browse the repository at this point in the history
Replace Syscall with unix.Ioctl* in tscreen_{bsd,darwin}.go. Merge
tscreen_{bsd,solaris}.go with tscreen_linux.go and rename to
tscreen_unix.go to deduplicate code.
  • Loading branch information
niten94 committed Jan 1, 2025
1 parent 5802ee6 commit fd9a4c0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 279 deletions.
121 changes: 0 additions & 121 deletions tscreen_bsd.go

This file was deleted.

67 changes: 27 additions & 40 deletions tscreen_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ package tcell
import (
"os/signal"
"syscall"
"unsafe"

"github.com/zyedidia/poller"

"golang.org/x/sys/unix"
)

type termiosPrivate syscall.Termios
type termiosPrivate struct {
tio *unix.Termios
}

func (t *tScreen) termioInit() error {
var e error
var newtios termiosPrivate
var fd uintptr
var tios uintptr
var ioc uintptr
t.tiosp = &termiosPrivate{}
var raw unix.Termios
var tio *unix.Termios

if t.in, e = poller.Open("/dev/tty", poller.O_RO); e != nil {
goto failed
Expand All @@ -60,29 +60,24 @@ func (t *tScreen) termioInit() error {
goto failed
}

tios = uintptr(unsafe.Pointer(t.tiosp))
ioc = uintptr(syscall.TIOCGETA)
fd = uintptr(t.out.(*poller.FD).Sysfd())
if _, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0); e1 != 0 {
e = e1
tio, e = unix.IoctlGetTermios(int(t.out.(*poller.FD).Sysfd()), getTermios)
if e != nil {
goto failed
}

newtios = *t.tiosp
newtios.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK |
syscall.ISTRIP | syscall.INLCR | syscall.IGNCR |
syscall.ICRNL | syscall.IXON
newtios.Oflag &^= syscall.OPOST
newtios.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON |
syscall.ISIG | syscall.IEXTEN
newtios.Cflag &^= syscall.CSIZE | syscall.PARENB
newtios.Cflag |= syscall.CS8

tios = uintptr(unsafe.Pointer(&newtios))

ioc = uintptr(syscall.TIOCSETA)
if _, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0); e1 != 0 {
e = e1
t.tiosp = &termiosPrivate{tio: tio}

raw = *tio
raw.Iflag &^= (unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP |
unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON)
raw.Oflag &^= unix.OPOST
raw.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG |
unix.IEXTEN)
raw.Cflag &^= (unix.CSIZE | unix.PARENB)
raw.Cflag |= unix.CS8

e = unix.IoctlSetTermios(int(t.out.(*poller.FD).Sysfd()), setTermios, &raw)
if e != nil {
goto failed
}

Expand Down Expand Up @@ -110,11 +105,8 @@ func (t *tScreen) termioFini() {

<-t.indoneq

if t.out != nil {
fd := uintptr(t.out.(*poller.FD).Sysfd())
ioc := uintptr(syscall.TIOCSETAF)
tios := uintptr(unsafe.Pointer(t.tiosp))
syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0)
if t.out != nil && t.tiosp != nil {
unix.IoctlSetTermios(int(t.out.(*poller.FD).Sysfd()), flushTermios, t.tiosp.tio)
t.out.(*poller.FD).Close()
}

Expand All @@ -124,16 +116,11 @@ func (t *tScreen) termioFini() {
}

func (t *tScreen) getWinSize() (int, int, error) {

fd := uintptr(t.out.(*poller.FD).Sysfd())
dim := [4]uint16{}
dimp := uintptr(unsafe.Pointer(&dim))
ioc := uintptr(syscall.TIOCGWINSZ)
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL,
fd, ioc, dimp, 0, 0, 0); err != 0 {
wsz, err := unix.IoctlGetWinsize(int(t.out.(*poller.FD).Sysfd()), unix.TIOCGWINSZ)
if err != nil {
return -1, -1, err
}
return int(dim[1]), int(dim[0]), nil
return int(wsz.Col), int(wsz.Row), nil
}

func (t *tScreen) Beep() error {
Expand Down
114 changes: 0 additions & 114 deletions tscreen_solaris.go

This file was deleted.

8 changes: 4 additions & 4 deletions tscreen_linux.go → tscreen_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux
// +build linux solaris freebsd netbsd openbsd dragonfly

// Copyright 2019 The TCell Authors
//
Expand Down Expand Up @@ -41,7 +41,7 @@ func (t *tScreen) termioInit() error {
goto failed
}

tio, e = unix.IoctlGetTermios(int(t.out.(*os.File).Fd()), unix.TCGETS)
tio, e = unix.IoctlGetTermios(int(t.out.(*os.File).Fd()), getTermios)
if e != nil {
goto failed
}
Expand All @@ -64,7 +64,7 @@ func (t *tScreen) termioInit() error {
raw.Cc[unix.VMIN] = 1
raw.Cc[unix.VTIME] = 0

e = unix.IoctlSetTermios(int(t.out.(*os.File).Fd()), unix.TCSETS, &raw)
e = unix.IoctlSetTermios(int(t.out.(*os.File).Fd()), setTermios, &raw)
if e != nil {
goto failed
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (t *tScreen) termioFini() {
<-t.indoneq

if t.out != nil && t.tiosp != nil {
unix.IoctlSetTermios(int(t.out.(*os.File).Fd()), unix.TCSETSF, t.tiosp.tio)
unix.IoctlSetTermios(int(t.out.(*os.File).Fd()), flushTermios, t.tiosp.tio)
t.out.(*os.File).Close()
}

Expand Down
11 changes: 11 additions & 0 deletions tscreen_unix_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build freebsd netbsd openbsd dragonfly darwin

package tcell

import "golang.org/x/sys/unix"

const (
getTermios = unix.TIOCGETA
setTermios = unix.TIOCSETA
flushTermios = unix.TIOCSETAF
)
Loading

0 comments on commit fd9a4c0

Please sign in to comment.