2021-09-10 07:50:44 +03:00
|
|
|
//go:build windows
|
2021-04-15 16:14:40 +03:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package twin
|
|
|
|
|
2021-04-17 08:03:52 +03:00
|
|
|
import (
|
|
|
|
"os"
|
2021-04-22 22:03:22 +03:00
|
|
|
"syscall"
|
2021-04-17 08:03:52 +03:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
2021-04-15 20:09:04 +03:00
|
|
|
|
2021-04-15 16:14:40 +03:00
|
|
|
func (screen *UnixScreen) setupSigwinchNotification() {
|
|
|
|
screen.sigwinch = make(chan int, 1)
|
|
|
|
screen.sigwinch <- 0 // Trigger initial screen size query
|
|
|
|
|
|
|
|
// No SIGWINCH handling on Windows for now, contributions welcome, see
|
|
|
|
// sigwinch.go for inspiration.
|
|
|
|
}
|
2021-04-15 20:09:04 +03:00
|
|
|
|
2021-04-18 14:15:34 +03:00
|
|
|
func (screen *UnixScreen) setupTtyInTtyOut() error {
|
2021-04-22 22:03:22 +03:00
|
|
|
in, err := syscall.Open("CONIN$", syscall.O_RDWR, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-18 08:36:13 +03:00
|
|
|
}
|
|
|
|
|
2021-04-22 22:03:22 +03:00
|
|
|
screen.ttyIn = os.NewFile(uintptr(in), "/dev/tty")
|
2021-04-17 08:03:52 +03:00
|
|
|
|
|
|
|
// Set input stream to raw mode
|
2021-04-17 08:25:58 +03:00
|
|
|
stdin := windows.Handle(screen.ttyIn.Fd())
|
2021-04-17 08:44:24 +03:00
|
|
|
err = windows.GetConsoleMode(stdin, &screen.oldTtyInMode)
|
2021-04-17 08:25:58 +03:00
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
return err
|
2021-04-17 08:25:58 +03:00
|
|
|
}
|
2021-04-17 08:44:24 +03:00
|
|
|
err = windows.SetConsoleMode(stdin, screen.oldTtyInMode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT)
|
2021-04-17 08:25:58 +03:00
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
return err
|
2021-04-17 08:25:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-17 08:03:52 +03:00
|
|
|
screen.oldTerminalState, err = term.MakeRaw(int(screen.ttyIn.Fd()))
|
2021-04-15 21:14:06 +03:00
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
|
|
|
|
return err
|
2021-04-15 21:14:06 +03:00
|
|
|
}
|
2021-04-17 08:03:52 +03:00
|
|
|
|
|
|
|
screen.ttyOut = os.Stdout
|
|
|
|
|
|
|
|
// Enable console colors, from: https://stackoverflow.com/a/52579002
|
|
|
|
stdout := windows.Handle(screen.ttyOut.Fd())
|
2021-04-17 08:44:24 +03:00
|
|
|
err = windows.GetConsoleMode(stdout, &screen.oldTtyOutMode)
|
2021-04-17 08:25:58 +03:00
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
|
|
|
|
return err
|
2021-04-17 08:25:58 +03:00
|
|
|
}
|
2021-04-17 08:44:24 +03:00
|
|
|
err = windows.SetConsoleMode(stdout, screen.oldTtyOutMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
screen.restoreTtyInTtyOut() // Error intentionally ignored, report the first one only
|
|
|
|
return err
|
2021-04-17 08:44:24 +03:00
|
|
|
}
|
2021-04-18 14:15:34 +03:00
|
|
|
|
|
|
|
return nil
|
2021-04-17 08:44:24 +03:00
|
|
|
}
|
|
|
|
|
2021-04-18 14:15:34 +03:00
|
|
|
func (screen *UnixScreen) restoreTtyInTtyOut() error {
|
2021-04-17 08:44:24 +03:00
|
|
|
stdin := windows.Handle(screen.ttyIn.Fd())
|
|
|
|
err := windows.SetConsoleMode(stdin, screen.oldTtyInMode)
|
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
return err
|
2021-04-17 08:44:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stdout := windows.Handle(screen.ttyOut.Fd())
|
|
|
|
err = windows.SetConsoleMode(stdout, screen.oldTtyOutMode)
|
2021-04-17 08:25:58 +03:00
|
|
|
if err != nil {
|
2021-04-18 14:15:34 +03:00
|
|
|
return err
|
2021-04-17 08:25:58 +03:00
|
|
|
}
|
2021-04-18 14:15:34 +03:00
|
|
|
|
|
|
|
return nil
|
2021-04-15 20:09:04 +03:00
|
|
|
}
|