github.com/pmorton/docker@v1.5.0/pkg/term/console_windows.go (about)

     1  // +build windows
     2  
     3  package term
     4  
     5  import (
     6  	"syscall"
     7  	"unsafe"
     8  )
     9  
    10  const (
    11  	// Consts for Get/SetConsoleMode function
    12  	// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx
    13  	ENABLE_ECHO_INPUT      = 0x0004
    14  	ENABLE_INSERT_MODE     = 0x0020
    15  	ENABLE_LINE_INPUT      = 0x0002
    16  	ENABLE_MOUSE_INPUT     = 0x0010
    17  	ENABLE_PROCESSED_INPUT = 0x0001
    18  	ENABLE_QUICK_EDIT_MODE = 0x0040
    19  	ENABLE_WINDOW_INPUT    = 0x0008
    20  	// If parameter is a screen buffer handle, additional values
    21  	ENABLE_PROCESSED_OUTPUT   = 0x0001
    22  	ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002
    23  )
    24  
    25  var kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
    26  
    27  var (
    28  	setConsoleModeProc             = kernel32DLL.NewProc("SetConsoleMode")
    29  	getConsoleScreenBufferInfoProc = kernel32DLL.NewProc("GetConsoleScreenBufferInfo")
    30  )
    31  
    32  func GetConsoleMode(fileDesc uintptr) (uint32, error) {
    33  	var mode uint32
    34  	err := syscall.GetConsoleMode(syscall.Handle(fileDesc), &mode)
    35  	return mode, err
    36  }
    37  
    38  func SetConsoleMode(fileDesc uintptr, mode uint32) error {
    39  	r, _, err := setConsoleModeProc.Call(fileDesc, uintptr(mode), 0)
    40  	if r == 0 {
    41  		if err != nil {
    42  			return err
    43  		}
    44  		return syscall.EINVAL
    45  	}
    46  	return nil
    47  }
    48  
    49  // types for calling GetConsoleScreenBufferInfo
    50  // see http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093(v=vs.85).aspx
    51  type (
    52  	SHORT int16
    53  
    54  	SMALL_RECT struct {
    55  		Left   SHORT
    56  		Top    SHORT
    57  		Right  SHORT
    58  		Bottom SHORT
    59  	}
    60  
    61  	COORD struct {
    62  		X SHORT
    63  		Y SHORT
    64  	}
    65  
    66  	WORD uint16
    67  
    68  	CONSOLE_SCREEN_BUFFER_INFO struct {
    69  		dwSize              COORD
    70  		dwCursorPosition    COORD
    71  		wAttributes         WORD
    72  		srWindow            SMALL_RECT
    73  		dwMaximumWindowSize COORD
    74  	}
    75  )
    76  
    77  func GetConsoleScreenBufferInfo(fileDesc uintptr) (*CONSOLE_SCREEN_BUFFER_INFO, error) {
    78  	var info CONSOLE_SCREEN_BUFFER_INFO
    79  	r, _, err := getConsoleScreenBufferInfoProc.Call(uintptr(fileDesc), uintptr(unsafe.Pointer(&info)), 0)
    80  	if r == 0 {
    81  		if err != nil {
    82  			return nil, err
    83  		}
    84  		return nil, syscall.EINVAL
    85  	}
    86  	return &info, nil
    87  }