console_windows.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package console
  14. import (
  15. "os"
  16. "github.com/pkg/errors"
  17. "golang.org/x/sys/windows"
  18. )
  19. var (
  20. vtInputSupported bool
  21. ErrNotImplemented = errors.New("not implemented")
  22. )
  23. func (m *master) init() {
  24. m.h = windows.Handle(m.f.Fd())
  25. if err := windows.GetConsoleMode(m.h, &m.mode); err == nil {
  26. if m.f == os.Stdin {
  27. // Validate that windows.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
  28. if err = windows.SetConsoleMode(m.h, m.mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err == nil {
  29. vtInputSupported = true
  30. }
  31. // Unconditionally set the console mode back even on failure because SetConsoleMode
  32. // remembers invalid bits on input handles.
  33. windows.SetConsoleMode(m.h, m.mode)
  34. } else if err := windows.SetConsoleMode(m.h, m.mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err == nil {
  35. m.mode |= windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING
  36. } else {
  37. windows.SetConsoleMode(m.h, m.mode)
  38. }
  39. }
  40. }
  41. type master struct {
  42. h windows.Handle
  43. mode uint32
  44. f *os.File
  45. }
  46. func (m *master) SetRaw() error {
  47. if m.f == os.Stdin {
  48. if err := makeInputRaw(m.h, m.mode); err != nil {
  49. return err
  50. }
  51. } else {
  52. // Set StdOut and StdErr to raw mode, we ignore failures since
  53. // windows.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this version of
  54. // Windows.
  55. windows.SetConsoleMode(m.h, m.mode|windows.DISABLE_NEWLINE_AUTO_RETURN)
  56. }
  57. return nil
  58. }
  59. func (m *master) Reset() error {
  60. if err := windows.SetConsoleMode(m.h, m.mode); err != nil {
  61. return errors.Wrap(err, "unable to restore console mode")
  62. }
  63. return nil
  64. }
  65. func (m *master) Size() (WinSize, error) {
  66. var info windows.ConsoleScreenBufferInfo
  67. err := windows.GetConsoleScreenBufferInfo(m.h, &info)
  68. if err != nil {
  69. return WinSize{}, errors.Wrap(err, "unable to get console info")
  70. }
  71. winsize := WinSize{
  72. Width: uint16(info.Window.Right - info.Window.Left + 1),
  73. Height: uint16(info.Window.Bottom - info.Window.Top + 1),
  74. }
  75. return winsize, nil
  76. }
  77. func (m *master) Resize(ws WinSize) error {
  78. return ErrNotImplemented
  79. }
  80. func (m *master) ResizeFrom(c Console) error {
  81. return ErrNotImplemented
  82. }
  83. func (m *master) DisableEcho() error {
  84. mode := m.mode &^ windows.ENABLE_ECHO_INPUT
  85. mode |= windows.ENABLE_PROCESSED_INPUT
  86. mode |= windows.ENABLE_LINE_INPUT
  87. if err := windows.SetConsoleMode(m.h, mode); err != nil {
  88. return errors.Wrap(err, "unable to set console to disable echo")
  89. }
  90. return nil
  91. }
  92. func (m *master) Close() error {
  93. return nil
  94. }
  95. func (m *master) Read(b []byte) (int, error) {
  96. return m.f.Read(b)
  97. }
  98. func (m *master) Write(b []byte) (int, error) {
  99. return m.f.Write(b)
  100. }
  101. func (m *master) Fd() uintptr {
  102. return uintptr(m.h)
  103. }
  104. // on windows, console can only be made from os.Std{in,out,err}, hence there
  105. // isnt a single name here we can use. Return a dummy "console" value in this
  106. // case should be sufficient.
  107. func (m *master) Name() string {
  108. return "console"
  109. }
  110. // makeInputRaw puts the terminal (Windows Console) connected to the given
  111. // file descriptor into raw mode
  112. func makeInputRaw(fd windows.Handle, mode uint32) error {
  113. // See
  114. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
  115. // -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
  116. // Disable these modes
  117. mode &^= windows.ENABLE_ECHO_INPUT
  118. mode &^= windows.ENABLE_LINE_INPUT
  119. mode &^= windows.ENABLE_MOUSE_INPUT
  120. mode &^= windows.ENABLE_WINDOW_INPUT
  121. mode &^= windows.ENABLE_PROCESSED_INPUT
  122. // Enable these modes
  123. mode |= windows.ENABLE_EXTENDED_FLAGS
  124. mode |= windows.ENABLE_INSERT_MODE
  125. mode |= windows.ENABLE_QUICK_EDIT_MODE
  126. if vtInputSupported {
  127. mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
  128. }
  129. if err := windows.SetConsoleMode(fd, mode); err != nil {
  130. return errors.Wrap(err, "unable to set console to raw mode")
  131. }
  132. return nil
  133. }
  134. func checkConsole(f *os.File) error {
  135. var mode uint32
  136. if err := windows.GetConsoleMode(windows.Handle(f.Fd()), &mode); err != nil {
  137. return err
  138. }
  139. return nil
  140. }
  141. func newMaster(f *os.File) (Console, error) {
  142. if f != os.Stdin && f != os.Stdout && f != os.Stderr {
  143. return nil, errors.New("creating a console from a file is not supported on windows")
  144. }
  145. m := &master{f: f}
  146. m.init()
  147. return m, nil
  148. }