2014-10-23 23:44:57 +00:00
|
|
|
// +build windows
|
|
|
|
package term
|
|
|
|
|
2015-03-06 00:41:48 +00:00
|
|
|
import (
|
|
|
|
"io"
|
2015-03-23 18:38:43 +00:00
|
|
|
"os"
|
2015-03-22 16:55:21 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/term/winconsole"
|
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-01-24 01:33:49 +00:00
|
|
|
// GetWinsize gets the window size of the given terminal
|
2014-10-23 23:44:57 +00:00
|
|
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
|
|
|
ws := &Winsize{}
|
2015-03-06 00:41:48 +00:00
|
|
|
var info *winconsole.CONSOLE_SCREEN_BUFFER_INFO
|
|
|
|
info, err := winconsole.GetConsoleScreenBufferInfo(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-24 01:33:49 +00:00
|
|
|
|
|
|
|
ws.Width = uint16(info.Window.Right - info.Window.Left + 1)
|
|
|
|
ws.Height = uint16(info.Window.Bottom - info.Window.Top + 1)
|
2014-10-23 23:44:57 +00:00
|
|
|
|
|
|
|
ws.x = 0 // todo azlinux -- this is the pixel size of the Window, and not currently used by any caller
|
|
|
|
ws.y = 0
|
|
|
|
|
|
|
|
return ws, nil
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// SetWinsize sets the terminal connected to the given file descriptor to a
|
|
|
|
// given size.
|
2014-10-23 23:44:57 +00:00
|
|
|
func SetWinsize(fd uintptr, ws *Winsize) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
|
|
|
func IsTerminal(fd uintptr) bool {
|
2015-03-06 00:41:48 +00:00
|
|
|
_, e := winconsole.GetConsoleMode(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
return e == nil
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor to a
|
2014-10-23 23:44:57 +00:00
|
|
|
// previous state.
|
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
2015-03-06 00:41:48 +00:00
|
|
|
return winconsole.SetConsoleMode(fd, state.mode)
|
2014-10-23 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// SaveState saves the state of the given console
|
2014-10-23 23:44:57 +00:00
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
2015-03-06 00:41:48 +00:00
|
|
|
mode, e := winconsole.GetConsoleMode(fd)
|
2014-10-23 23:44:57 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
return &State{mode}, nil
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// DisableEcho disbales the echo for given file descriptor and returns previous state
|
2014-10-23 23:44:57 +00:00
|
|
|
// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx for these flag settings
|
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
2015-03-06 00:41:48 +00:00
|
|
|
state.mode &^= (winconsole.ENABLE_ECHO_INPUT)
|
|
|
|
state.mode |= (winconsole.ENABLE_PROCESSED_INPUT | winconsole.ENABLE_LINE_INPUT)
|
|
|
|
return winconsole.SetConsoleMode(fd, state.mode)
|
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
|
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
2014-10-23 23:44:57 +00:00
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
|
|
|
oldState, err := MakeRaw(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO (azlinux): implement handling interrupt and restore state of terminal
|
|
|
|
return oldState, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeRaw puts the terminal connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be
|
|
|
|
// restored.
|
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
|
|
|
var state *State
|
|
|
|
state, err := SaveState(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
|
|
|
// All three input modes, along with processed output mode, are designed to work together.
|
|
|
|
// It is best to either enable or disable all of these modes as a group.
|
|
|
|
// When all are enabled, the application is said to be in "cooked" mode, which means that most of the processing is handled for the application.
|
|
|
|
// When all are disabled, the application is in "raw" mode, which means that input is unfiltered and any processing is left to the application.
|
|
|
|
state.mode = 0
|
2015-03-06 00:41:48 +00:00
|
|
|
err = winconsole.SetConsoleMode(fd, state.mode)
|
2014-10-23 23:44:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2015-03-06 00:41:48 +00:00
|
|
|
|
2015-03-07 01:04:35 +00:00
|
|
|
// GetFdInfo returns file descriptor and bool indicating whether the file is a terminal
|
|
|
|
func GetFdInfo(in interface{}) (uintptr, bool) {
|
2015-03-06 00:41:48 +00:00
|
|
|
return winconsole.GetHandleInfo(in)
|
|
|
|
}
|
|
|
|
|
2015-03-22 16:55:21 +00:00
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
2015-03-23 18:38:43 +00:00
|
|
|
var shouldEmulateANSI bool
|
|
|
|
switch {
|
|
|
|
case os.Getenv("ConEmuANSI") == "ON":
|
|
|
|
// ConEmu shell, ansi emulated by default and ConEmu does an extensively
|
|
|
|
// good emulation.
|
|
|
|
shouldEmulateANSI = false
|
|
|
|
case os.Getenv("MSYSTEM") != "":
|
|
|
|
// MSYS (mingw) cannot fully emulate well and still shows escape characters
|
|
|
|
// mostly because it's still running on cmd.exe window.
|
|
|
|
shouldEmulateANSI = true
|
|
|
|
default:
|
|
|
|
shouldEmulateANSI = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldEmulateANSI {
|
|
|
|
return winconsole.StdStreams()
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Stdin, os.Stdout, os.Stderr
|
2015-03-06 00:41:48 +00:00
|
|
|
}
|