2015-04-16 17:01:37 -04:00
|
|
|
// +build windows
|
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package windowsconsole // import "github.com/docker/docker/pkg/term/windows"
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-06-22 19:34:01 -04:00
|
|
|
"io"
|
2015-04-16 17:01:37 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
|
2015-09-03 16:01:56 -04:00
|
|
|
ansiterm "github.com/Azure/go-ansiterm"
|
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
2015-04-16 17:01:37 -04:00
|
|
|
)
|
|
|
|
|
2015-11-11 22:53:58 -05:00
|
|
|
const (
|
|
|
|
escapeSequence = ansiterm.KEY_ESC_CSI
|
|
|
|
)
|
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
// ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation.
|
|
|
|
type ansiReader struct {
|
|
|
|
file *os.File
|
|
|
|
fd uintptr
|
|
|
|
buffer []byte
|
|
|
|
cbBuffer int
|
|
|
|
command []byte
|
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
// NewAnsiReader returns an io.ReadCloser that provides VT100 terminal emulation on top of a
|
|
|
|
// Windows console input handle.
|
|
|
|
func NewAnsiReader(nFile int) io.ReadCloser {
|
2016-05-24 16:03:13 -04:00
|
|
|
initLogger()
|
2015-09-03 16:01:56 -04:00
|
|
|
file, fd := winterm.GetStdFile(nFile)
|
2015-04-16 17:01:37 -04:00
|
|
|
return &ansiReader{
|
2015-11-11 22:53:58 -05:00
|
|
|
file: file,
|
|
|
|
fd: fd,
|
|
|
|
command: make([]byte, 0, ansiterm.ANSI_MAX_CMD_LENGTH),
|
|
|
|
buffer: make([]byte, 0),
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the wrapped file.
|
|
|
|
func (ar *ansiReader) Close() (err error) {
|
|
|
|
return ar.file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fd returns the file descriptor of the wrapped file.
|
|
|
|
func (ar *ansiReader) Fd() uintptr {
|
|
|
|
return ar.fd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads up to len(p) bytes of translated input events into p.
|
|
|
|
func (ar *ansiReader) Read(p []byte) (int, error) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Previously read bytes exist, read as much as we can and return
|
|
|
|
if len(ar.buffer) > 0 {
|
|
|
|
logger.Debugf("Reading previously cached bytes")
|
|
|
|
|
|
|
|
originalLength := len(ar.buffer)
|
|
|
|
copiedLength := copy(p, ar.buffer)
|
|
|
|
|
|
|
|
if copiedLength == originalLength {
|
|
|
|
ar.buffer = make([]byte, 0, len(p))
|
|
|
|
} else {
|
|
|
|
ar.buffer = ar.buffer[copiedLength:]
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("Read from cache p[%d]: % x", copiedLength, p)
|
|
|
|
return copiedLength, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and translate key events
|
|
|
|
events, err := readInputEvents(ar.fd, len(p))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else if len(events) == 0 {
|
|
|
|
logger.Debug("No input events detected")
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:53:58 -05:00
|
|
|
keyBytes := translateKeyEvents(events, []byte(escapeSequence))
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
// Save excess bytes and right-size keyBytes
|
|
|
|
if len(keyBytes) > len(p) {
|
|
|
|
logger.Debugf("Received %d keyBytes, only room for %d bytes", len(keyBytes), len(p))
|
|
|
|
ar.buffer = keyBytes[len(p):]
|
|
|
|
keyBytes = keyBytes[:len(p)]
|
|
|
|
} else if len(keyBytes) == 0 {
|
2015-08-07 18:24:18 -04:00
|
|
|
logger.Debug("No key bytes returned from the translator")
|
2015-04-16 17:01:37 -04:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
copiedLength := copy(p, keyBytes)
|
|
|
|
if copiedLength != len(keyBytes) {
|
2016-11-22 01:53:11 -05:00
|
|
|
return 0, errors.New("unexpected copy length encountered")
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debugf("Read p[%d]: % x", copiedLength, p)
|
|
|
|
logger.Debugf("Read keyBytes[%d]: % x", copiedLength, keyBytes)
|
|
|
|
return copiedLength, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readInputEvents polls until at least one event is available.
|
2015-09-03 16:01:56 -04:00
|
|
|
func readInputEvents(fd uintptr, maxBytes int) ([]winterm.INPUT_RECORD, error) {
|
2015-04-16 17:01:37 -04:00
|
|
|
// Determine the maximum number of records to retrieve
|
|
|
|
// -- Cast around the type system to obtain the size of a single INPUT_RECORD.
|
|
|
|
// unsafe.Sizeof requires an expression vs. a type-reference; the casting
|
|
|
|
// tricks the type system into believing it has such an expression.
|
2015-09-03 16:01:56 -04:00
|
|
|
recordSize := int(unsafe.Sizeof(*((*winterm.INPUT_RECORD)(unsafe.Pointer(&maxBytes)))))
|
2015-04-16 17:01:37 -04:00
|
|
|
countRecords := maxBytes / recordSize
|
2015-09-03 16:01:56 -04:00
|
|
|
if countRecords > ansiterm.MAX_INPUT_EVENTS {
|
|
|
|
countRecords = ansiterm.MAX_INPUT_EVENTS
|
2016-12-09 13:02:44 -05:00
|
|
|
} else if countRecords == 0 {
|
|
|
|
countRecords = 1
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
logger.Debugf("[windows] readInputEvents: Reading %v records (buffer size %v, record size %v)", countRecords, maxBytes, recordSize)
|
|
|
|
|
|
|
|
// Wait for and read input events
|
2015-09-03 16:01:56 -04:00
|
|
|
events := make([]winterm.INPUT_RECORD, countRecords)
|
2015-04-16 17:01:37 -04:00
|
|
|
nEvents := uint32(0)
|
2015-09-03 16:01:56 -04:00
|
|
|
eventsExist, err := winterm.WaitForSingleObject(fd, winterm.WAIT_INFINITE)
|
2015-04-16 17:01:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if eventsExist {
|
2015-09-03 16:01:56 -04:00
|
|
|
err = winterm.ReadConsoleInput(fd, events, &nEvents)
|
2015-04-16 17:01:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a slice restricted to the number of returned records
|
|
|
|
logger.Debugf("[windows] readInputEvents: Read %v events", nEvents)
|
|
|
|
return events[:nEvents], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeyEvent Translation Helpers
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
var arrowKeyMapPrefix = map[uint16]string{
|
2015-09-03 16:01:56 -04:00
|
|
|
winterm.VK_UP: "%s%sA",
|
|
|
|
winterm.VK_DOWN: "%s%sB",
|
|
|
|
winterm.VK_RIGHT: "%s%sC",
|
|
|
|
winterm.VK_LEFT: "%s%sD",
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
var keyMapPrefix = map[uint16]string{
|
2015-09-03 16:01:56 -04:00
|
|
|
winterm.VK_UP: "\x1B[%sA",
|
|
|
|
winterm.VK_DOWN: "\x1B[%sB",
|
|
|
|
winterm.VK_RIGHT: "\x1B[%sC",
|
|
|
|
winterm.VK_LEFT: "\x1B[%sD",
|
|
|
|
winterm.VK_HOME: "\x1B[1%s~", // showkey shows ^[[1
|
|
|
|
winterm.VK_END: "\x1B[4%s~", // showkey shows ^[[4
|
|
|
|
winterm.VK_INSERT: "\x1B[2%s~",
|
|
|
|
winterm.VK_DELETE: "\x1B[3%s~",
|
|
|
|
winterm.VK_PRIOR: "\x1B[5%s~",
|
|
|
|
winterm.VK_NEXT: "\x1B[6%s~",
|
|
|
|
winterm.VK_F1: "",
|
|
|
|
winterm.VK_F2: "",
|
|
|
|
winterm.VK_F3: "\x1B[13%s~",
|
|
|
|
winterm.VK_F4: "\x1B[14%s~",
|
|
|
|
winterm.VK_F5: "\x1B[15%s~",
|
|
|
|
winterm.VK_F6: "\x1B[17%s~",
|
|
|
|
winterm.VK_F7: "\x1B[18%s~",
|
|
|
|
winterm.VK_F8: "\x1B[19%s~",
|
|
|
|
winterm.VK_F9: "\x1B[20%s~",
|
|
|
|
winterm.VK_F10: "\x1B[21%s~",
|
|
|
|
winterm.VK_F11: "\x1B[23%s~",
|
|
|
|
winterm.VK_F12: "\x1B[24%s~",
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// translateKeyEvents converts the input events into the appropriate ANSI string.
|
2015-09-03 16:01:56 -04:00
|
|
|
func translateKeyEvents(events []winterm.INPUT_RECORD, escapeSequence []byte) []byte {
|
2015-04-16 17:01:37 -04:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
for _, event := range events {
|
2015-09-03 16:01:56 -04:00
|
|
|
if event.EventType == winterm.KEY_EVENT && event.KeyEvent.KeyDown != 0 {
|
2015-04-16 17:01:37 -04:00
|
|
|
buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyToString maps the given input event record to the corresponding string.
|
2015-09-03 16:01:56 -04:00
|
|
|
func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) string {
|
2015-04-16 17:01:37 -04:00
|
|
|
if keyEvent.UnicodeChar == 0 {
|
|
|
|
return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, alt, control := getControlKeys(keyEvent.ControlKeyState)
|
|
|
|
if control {
|
|
|
|
// TODO(azlinux): Implement following control sequences
|
|
|
|
// <Ctrl>-D Signals the end of input from the keyboard; also exits current shell.
|
|
|
|
// <Ctrl>-H Deletes the first character to the left of the cursor. Also called the ERASE key.
|
|
|
|
// <Ctrl>-Q Restarts printing after it has been stopped with <Ctrl>-s.
|
|
|
|
// <Ctrl>-S Suspends printing on the screen (does not stop the program).
|
|
|
|
// <Ctrl>-U Deletes all characters on the current line. Also called the KILL key.
|
|
|
|
// <Ctrl>-E Quits current command and creates a core
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// <Alt>+Key generates ESC N Key
|
|
|
|
if !control && alt {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_ESC_N + strings.ToLower(string(keyEvent.UnicodeChar))
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(keyEvent.UnicodeChar)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string.
|
2016-04-25 18:55:12 -04:00
|
|
|
func formatVirtualKey(key uint16, controlState uint32, escapeSequence []byte) string {
|
2015-04-16 17:01:37 -04:00
|
|
|
shift, alt, control := getControlKeys(controlState)
|
2015-09-15 16:20:10 -04:00
|
|
|
modifier := getControlKeysModifier(shift, alt, control)
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
if format, ok := arrowKeyMapPrefix[key]; ok {
|
|
|
|
return fmt.Sprintf(format, escapeSequence, modifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
if format, ok := keyMapPrefix[key]; ok {
|
|
|
|
return fmt.Sprintf(format, modifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// getControlKeys extracts the shift, alt, and ctrl key states.
|
2016-04-25 18:55:12 -04:00
|
|
|
func getControlKeys(controlState uint32) (shift, alt, control bool) {
|
2015-09-03 16:01:56 -04:00
|
|
|
shift = 0 != (controlState & winterm.SHIFT_PRESSED)
|
|
|
|
alt = 0 != (controlState & (winterm.LEFT_ALT_PRESSED | winterm.RIGHT_ALT_PRESSED))
|
|
|
|
control = 0 != (controlState & (winterm.LEFT_CTRL_PRESSED | winterm.RIGHT_CTRL_PRESSED))
|
2015-04-16 17:01:37 -04:00
|
|
|
return shift, alt, control
|
|
|
|
}
|
|
|
|
|
|
|
|
// getControlKeysModifier returns the ANSI modifier for the given combination of control keys.
|
2015-09-15 16:20:10 -04:00
|
|
|
func getControlKeysModifier(shift, alt, control bool) string {
|
2015-04-16 17:01:37 -04:00
|
|
|
if shift && alt && control {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_8
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if alt && control {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_7
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if shift && control {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_6
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if control {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_5
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if shift && alt {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_4
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if alt {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_3
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
if shift {
|
2015-09-03 16:01:56 -04:00
|
|
|
return ansiterm.KEY_CONTROL_PARAM_2
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|