2014-10-23 23:44:57 +00:00
|
|
|
// +build windows
|
2015-07-01 20:16:16 +00:00
|
|
|
|
2014-10-23 23:44:57 +00:00
|
|
|
package term
|
|
|
|
|
2015-03-06 00:41:48 +00:00
|
|
|
import (
|
2015-04-16 21:01:37 +00:00
|
|
|
"fmt"
|
2015-03-06 00:41:48 +00:00
|
|
|
"io"
|
2015-03-23 18:38:43 +00:00
|
|
|
"os"
|
2015-04-16 21:01:37 +00:00
|
|
|
"os/signal"
|
2015-03-22 16:55:21 +00:00
|
|
|
|
2015-04-16 21:01:37 +00:00
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
2015-04-10 22:43:35 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-16 21:01:37 +00:00
|
|
|
"github.com/docker/docker/pkg/term/windows"
|
2015-03-06 00:41:48 +00:00
|
|
|
)
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// State holds the console mode for the terminal.
|
2014-10-23 23:44:57 +00:00
|
|
|
type State struct {
|
|
|
|
mode uint32
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// Winsize is used for window size.
|
2014-10-23 23:44:57 +00:00
|
|
|
type Winsize struct {
|
|
|
|
Height uint16
|
|
|
|
Width uint16
|
|
|
|
x uint16
|
|
|
|
y uint16
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// StdStreams returns the standard streams (stdin, stdout, stedrr).
|
2015-04-02 16:35:13 +00:00
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
|
|
|
switch {
|
|
|
|
case os.Getenv("ConEmuANSI") == "ON":
|
|
|
|
// The ConEmu shell emulates ANSI well by default.
|
|
|
|
return os.Stdin, os.Stdout, os.Stderr
|
|
|
|
case os.Getenv("MSYSTEM") != "":
|
|
|
|
// MSYS (mingw) does not emulate ANSI well.
|
2015-04-16 21:01:37 +00:00
|
|
|
return windows.ConsoleStreams()
|
2015-04-02 16:35:13 +00:00
|
|
|
default:
|
2015-04-16 21:01:37 +00:00
|
|
|
return windows.ConsoleStreams()
|
2015-04-02 16:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
2015-04-02 16:35:13 +00:00
|
|
|
func GetFdInfo(in interface{}) (uintptr, bool) {
|
2015-04-16 21:01:37 +00:00
|
|
|
return windows.GetHandleInfo(in)
|
2015-04-02 16:35:13 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// GetWinsize returns the window size based on the specified file descriptor.
|
2014-10-23 23:44:57 +00:00
|
|
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
2015-04-16 21:01:37 +00:00
|
|
|
|
|
|
|
info, err := winterm.GetConsoleScreenBufferInfo(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-24 01:33:49 +00:00
|
|
|
|
2015-04-16 21:01:37 +00:00
|
|
|
winsize := &Winsize{
|
2015-04-06 21:31:42 +00:00
|
|
|
Width: uint16(info.Window.Right - info.Window.Left + 1),
|
|
|
|
Height: uint16(info.Window.Bottom - info.Window.Top + 1),
|
2015-04-02 16:35:13 +00:00
|
|
|
x: 0,
|
2015-04-16 21:01:37 +00:00
|
|
|
y: 0}
|
|
|
|
|
|
|
|
// Note: GetWinsize is called frequently -- uncomment only for excessive details
|
|
|
|
// logrus.Debugf("[windows] GetWinsize: Console(%v)", info.String())
|
|
|
|
// logrus.Debugf("[windows] GetWinsize: Width(%v), Height(%v), x(%v), y(%v)", winsize.Width, winsize.Height, winsize.x, winsize.y)
|
|
|
|
return winsize, nil
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// SetWinsize tries to set the specified window size for the specified file descriptor.
|
2014-10-23 23:44:57 +00:00
|
|
|
func SetWinsize(fd uintptr, ws *Winsize) error {
|
2015-04-16 21:01:37 +00:00
|
|
|
|
|
|
|
// Ensure the requested dimensions are no larger than the maximum window size
|
|
|
|
info, err := winterm.GetConsoleScreenBufferInfo(fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ws.Width == 0 || ws.Height == 0 || ws.Width > uint16(info.MaximumWindowSize.X) || ws.Height > uint16(info.MaximumWindowSize.Y) {
|
|
|
|
return fmt.Errorf("Illegal window size: (%v,%v) -- Maximum allow: (%v,%v)",
|
|
|
|
ws.Width, ws.Height, info.MaximumWindowSize.X, info.MaximumWindowSize.Y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Narrow the sizes to that used by Windows
|
2015-07-28 00:40:49 +00:00
|
|
|
width := winterm.SHORT(ws.Width)
|
|
|
|
height := winterm.SHORT(ws.Height)
|
2015-04-16 21:01:37 +00:00
|
|
|
|
|
|
|
// Set the dimensions while ensuring they remain within the bounds of the backing console buffer
|
|
|
|
// -- Shrinking will always succeed. Growing may push the edges past the buffer boundary. When that occurs,
|
|
|
|
// shift the upper left just enough to keep the new window within the buffer.
|
|
|
|
rect := info.Window
|
|
|
|
if width < rect.Right-rect.Left+1 {
|
|
|
|
rect.Right = rect.Left + width - 1
|
|
|
|
} else if width > rect.Right-rect.Left+1 {
|
|
|
|
rect.Right = rect.Left + width - 1
|
|
|
|
if rect.Right >= info.Size.X {
|
|
|
|
rect.Left = info.Size.X - width
|
|
|
|
rect.Right = info.Size.X - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if height < rect.Bottom-rect.Top+1 {
|
|
|
|
rect.Bottom = rect.Top + height - 1
|
|
|
|
} else if height > rect.Bottom-rect.Top+1 {
|
|
|
|
rect.Bottom = rect.Top + height - 1
|
|
|
|
if rect.Bottom >= info.Size.Y {
|
|
|
|
rect.Top = info.Size.Y - height
|
|
|
|
rect.Bottom = info.Size.Y - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logrus.Debugf("[windows] SetWinsize: Requested((%v,%v)) Actual(%v)", ws.Width, ws.Height, rect)
|
|
|
|
|
|
|
|
return winterm.SetConsoleWindowInfo(fd, true, rect)
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
|
|
|
func IsTerminal(fd uintptr) bool {
|
2015-04-16 21:01:37 +00:00
|
|
|
return windows.IsConsole(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
|
|
|
// to a previous state.
|
2014-10-23 23:44:57 +00:00
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
2015-04-16 21:01:37 +00:00
|
|
|
return winterm.SetConsoleMode(fd, state.mode)
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 16:35:13 +00:00
|
|
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
2014-10-23 23:44:57 +00:00
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
2015-04-16 21:01:37 +00:00
|
|
|
mode, e := winterm.GetConsoleMode(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return &State{mode}, nil
|
|
|
|
}
|
|
|
|
|
2015-04-02 16:35:13 +00:00
|
|
|
// DisableEcho disables echo for the terminal connected to the given file descriptor.
|
2015-04-16 21:01:37 +00:00
|
|
|
// -- See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
2014-10-23 23:44:57 +00:00
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
2015-04-02 16:35:13 +00:00
|
|
|
mode := state.mode
|
2015-04-16 21:01:37 +00:00
|
|
|
mode &^= winterm.ENABLE_ECHO_INPUT
|
|
|
|
mode |= winterm.ENABLE_PROCESSED_INPUT | winterm.ENABLE_LINE_INPUT
|
|
|
|
|
|
|
|
err := winterm.SetConsoleMode(fd, mode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
|
|
|
return nil
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// SetRawTerminal puts the terminal connected to the given file descriptor into raw
|
2015-07-25 08:35:07 +00:00
|
|
|
// mode and returns the previous state.
|
2014-10-23 23:44:57 +00:00
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
2015-04-02 16:35:13 +00:00
|
|
|
state, err := MakeRaw(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-16 21:01:37 +00:00
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
2015-04-02 16:35:13 +00:00
|
|
|
return state, err
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 21:01:37 +00:00
|
|
|
// MakeRaw puts the terminal (Windows Console) connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be restored.
|
2014-10-23 23:44:57 +00:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
|
|
|
state, err := SaveState(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-02 16:35:13 +00:00
|
|
|
// See
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
|
|
|
mode := state.mode
|
|
|
|
|
|
|
|
// Disable these modes
|
2015-04-16 21:01:37 +00:00
|
|
|
mode &^= winterm.ENABLE_ECHO_INPUT
|
|
|
|
mode &^= winterm.ENABLE_LINE_INPUT
|
|
|
|
mode &^= winterm.ENABLE_MOUSE_INPUT
|
|
|
|
mode &^= winterm.ENABLE_WINDOW_INPUT
|
|
|
|
mode &^= winterm.ENABLE_PROCESSED_INPUT
|
2015-04-02 16:35:13 +00:00
|
|
|
|
|
|
|
// Enable these modes
|
2015-04-16 21:01:37 +00:00
|
|
|
mode |= winterm.ENABLE_EXTENDED_FLAGS
|
|
|
|
mode |= winterm.ENABLE_INSERT_MODE
|
|
|
|
mode |= winterm.ENABLE_QUICK_EDIT_MODE
|
2015-04-02 16:35:13 +00:00
|
|
|
|
2015-04-16 21:01:37 +00:00
|
|
|
err = winterm.SetConsoleMode(fd, mode)
|
2014-10-23 23:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2015-04-16 21:01:37 +00:00
|
|
|
|
|
|
|
func restoreAtInterrupt(fd uintptr, state *State) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = <-sigchan
|
|
|
|
RestoreTerminal(fd, state)
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|