tty_unix.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2021 The TCell Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use file except in compliance with the License.
  5. // You may obtain a copy of the license at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
  15. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  16. package tcell
  17. import (
  18. "errors"
  19. "fmt"
  20. "os"
  21. "os/signal"
  22. "strconv"
  23. "sync"
  24. "syscall"
  25. "time"
  26. "golang.org/x/term"
  27. )
  28. // devTty is an implementation of the Tty API based upon /dev/tty.
  29. type devTty struct {
  30. fd int
  31. f *os.File
  32. of *os.File // the first open of /dev/tty
  33. saved *term.State
  34. sig chan os.Signal
  35. cb func()
  36. stopQ chan struct{}
  37. dev string
  38. wg sync.WaitGroup
  39. l sync.Mutex
  40. }
  41. func (tty *devTty) Read(b []byte) (int, error) {
  42. return tty.f.Read(b)
  43. }
  44. func (tty *devTty) Write(b []byte) (int, error) {
  45. return tty.f.Write(b)
  46. }
  47. func (tty *devTty) Close() error {
  48. return tty.f.Close()
  49. }
  50. func (tty *devTty) Start() error {
  51. tty.l.Lock()
  52. defer tty.l.Unlock()
  53. // We open another copy of /dev/tty. This is a workaround for unusual behavior
  54. // observed in macOS, apparently caused when a subshell (for example) closes our
  55. // own tty device (when it exits for example). Getting a fresh new one seems to
  56. // resolve the problem. (We believe this is a bug in the macOS tty driver that
  57. // fails to account for dup() references to the same file before applying close()
  58. // related behaviors to the tty.) We're also holding the original copy we opened
  59. // since closing that might have deleterious effects as well. The upshot is that
  60. // we will have up to two separate file handles open on /dev/tty. (Note that when
  61. // using stdin/stdout instead of /dev/tty this problem is not observed.)
  62. var err error
  63. if tty.f, err = os.OpenFile(tty.dev, os.O_RDWR, 0); err != nil {
  64. return err
  65. }
  66. if !term.IsTerminal(tty.fd) {
  67. return errors.New("device is not a terminal")
  68. }
  69. _ = tty.f.SetReadDeadline(time.Time{})
  70. saved, err := term.MakeRaw(tty.fd) // also sets vMin and vTime
  71. if err != nil {
  72. return err
  73. }
  74. tty.saved = saved
  75. tty.stopQ = make(chan struct{})
  76. tty.wg.Add(1)
  77. go func(stopQ chan struct{}) {
  78. defer tty.wg.Done()
  79. for {
  80. select {
  81. case <-tty.sig:
  82. tty.l.Lock()
  83. cb := tty.cb
  84. tty.l.Unlock()
  85. if cb != nil {
  86. cb()
  87. }
  88. case <-stopQ:
  89. return
  90. }
  91. }
  92. }(tty.stopQ)
  93. signal.Notify(tty.sig, syscall.SIGWINCH)
  94. return nil
  95. }
  96. func (tty *devTty) Drain() error {
  97. _ = tty.f.SetReadDeadline(time.Now())
  98. if err := tcSetBufParams(tty.fd, 0, 0); err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func (tty *devTty) Stop() error {
  104. tty.l.Lock()
  105. if err := term.Restore(tty.fd, tty.saved); err != nil {
  106. tty.l.Unlock()
  107. return err
  108. }
  109. _ = tty.f.SetReadDeadline(time.Now())
  110. signal.Stop(tty.sig)
  111. close(tty.stopQ)
  112. tty.l.Unlock()
  113. tty.wg.Wait()
  114. // close our tty device -- we'll get another one if we Start again later.
  115. _ = tty.f.Close()
  116. return nil
  117. }
  118. func (tty *devTty) WindowSize() (int, int, error) {
  119. w, h, err := term.GetSize(tty.fd)
  120. if err != nil {
  121. return 0, 0, err
  122. }
  123. if w == 0 {
  124. w, _ = strconv.Atoi(os.Getenv("COLUMNS"))
  125. }
  126. if w == 0 {
  127. w = 80 // default
  128. }
  129. if h == 0 {
  130. h, _ = strconv.Atoi(os.Getenv("LINES"))
  131. }
  132. if h == 0 {
  133. h = 25 // default
  134. }
  135. return w, h, nil
  136. }
  137. func (tty *devTty) NotifyResize(cb func()) {
  138. tty.l.Lock()
  139. tty.cb = cb
  140. tty.l.Unlock()
  141. }
  142. // NewDevTty opens a /dev/tty based Tty.
  143. func NewDevTty() (Tty, error) {
  144. return NewDevTtyFromDev("/dev/tty")
  145. }
  146. // NewDevTtyFromDev opens a tty device given a path. This can be useful to bind to other nodes.
  147. func NewDevTtyFromDev(dev string) (Tty, error) {
  148. tty := &devTty{
  149. dev: dev,
  150. sig: make(chan os.Signal),
  151. }
  152. var err error
  153. if tty.of, err = os.OpenFile(dev, os.O_RDWR, 0); err != nil {
  154. return nil, err
  155. }
  156. tty.fd = int(tty.of.Fd())
  157. if !term.IsTerminal(tty.fd) {
  158. _ = tty.f.Close()
  159. return nil, errors.New("not a terminal")
  160. }
  161. if tty.saved, err = term.GetState(tty.fd); err != nil {
  162. _ = tty.f.Close()
  163. return nil, fmt.Errorf("failed to get state: %w", err)
  164. }
  165. return tty, nil
  166. }