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 (
|
2023-08-14 20:47:42 +03:00
|
|
|
"fmt"
|
2024-07-14 12:37:36 +03:00
|
|
|
"io"
|
2021-04-17 08:03:52 +03:00
|
|
|
"os"
|
2024-07-14 12:37:36 +03:00
|
|
|
"sync/atomic"
|
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
|
|
|
|
2024-07-17 01:38:31 +03:00
|
|
|
// NOTE: Karma points for replacing TestInterruptableReader_blockedOnRead() with
|
|
|
|
// TestInterruptableReader_blockedOnReadImmediate() and fixing the Windows
|
|
|
|
// implementation here so that the tests pass.
|
|
|
|
|
2024-07-14 12:37:36 +03:00
|
|
|
type interruptableReaderImpl struct {
|
2024-07-14 13:07:30 +03:00
|
|
|
base *os.File
|
2024-07-14 12:37:36 +03:00
|
|
|
shutdownRequested atomic.Bool
|
|
|
|
}
|
|
|
|
|
2024-07-14 13:25:17 +03:00
|
|
|
// NOTE: To work properly, this Read() should return immediately after somebody
|
|
|
|
// calls Interrupt(), *without first reading any bytes from the base reader*.
|
|
|
|
//
|
|
|
|
// This implementation doesn't do that. If you want to fix this, the not-Windows
|
|
|
|
// implementation in screen-setup.go may or may not work as inspiration.
|
2024-07-14 12:37:36 +03:00
|
|
|
func (r *interruptableReaderImpl) Read(p []byte) (n int, err error) {
|
2024-07-14 13:14:59 +03:00
|
|
|
if r.shutdownRequested.Load() {
|
|
|
|
err = io.EOF
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-14 12:37:36 +03:00
|
|
|
n, err = r.base.Read(p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.shutdownRequested.Load() {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *interruptableReaderImpl) Interrupt() {
|
|
|
|
// Previously we used to close the screen.ttyIn file descriptor here, but:
|
|
|
|
// * That didn't interrupt the blocking read() in the main loop
|
|
|
|
// * It may or may not have caused shutdown issues on Windows
|
|
|
|
//
|
|
|
|
// Setting this flag doesn't interrupt the blocking read() either, but it
|
|
|
|
// should at least not cause any shutdown issues on Windows.
|
|
|
|
//
|
|
|
|
// Ref:
|
|
|
|
// * https://github.com/walles/moar/issues/217
|
|
|
|
// * https://github.com/walles/moar/issues/221
|
|
|
|
r.shutdownRequested.Store(true)
|
|
|
|
}
|
|
|
|
|
2024-07-14 13:07:30 +03:00
|
|
|
func newInterruptableReader(base *os.File) (interruptableReader, error) {
|
|
|
|
return &interruptableReaderImpl{base: base}, nil
|
2024-07-14 12:37:36 +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 {
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to open CONIN$: %w", 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 {
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to get stdin console mode: %w", 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 {
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to set stdin console mode: %w", 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
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to set raw mode: %w", 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
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to get stdout console mode: %w", 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
|
2023-08-14 20:47:42 +03:00
|
|
|
return fmt.Errorf("failed to set stdout console mode: %w", 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 {
|
2024-06-29 14:31:21 +03:00
|
|
|
errors := []error{}
|
|
|
|
|
2021-04-17 08:44:24 +03:00
|
|
|
stdin := windows.Handle(screen.ttyIn.Fd())
|
|
|
|
err := windows.SetConsoleMode(stdin, screen.oldTtyInMode)
|
|
|
|
if err != nil {
|
2024-06-29 14:31:21 +03:00
|
|
|
errors = append(errors, fmt.Errorf("failed to restore stdin console mode: %w", 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 {
|
2024-06-29 14:31:21 +03:00
|
|
|
errors = append(errors, fmt.Errorf("failed to restore stdout console mode: %w", err))
|
2021-04-17 08:25:58 +03:00
|
|
|
}
|
2021-04-18 14:15:34 +03:00
|
|
|
|
2024-06-29 14:31:21 +03:00
|
|
|
if len(errors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to restore terminal state: %v", errors)
|
2021-04-15 20:09:04 +03:00
|
|
|
}
|