UnixOutput.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System.Runtime.InteropServices;
  2. using Microsoft.Win32.SafeHandles;
  3. // ReSharper disable IdentifierTypo
  4. // ReSharper disable InconsistentNaming
  5. namespace Terminal.Gui.Drivers;
  6. internal class UnixOutput : OutputBase, IOutput
  7. {
  8. [StructLayout (LayoutKind.Sequential)]
  9. private struct WinSize
  10. {
  11. public ushort ws_row;
  12. public ushort ws_col;
  13. public ushort ws_xpixel;
  14. public ushort ws_ypixel;
  15. }
  16. private static readonly uint TIOCGWINSZ =
  17. RuntimeInformation.IsOSPlatform (OSPlatform.OSX) ||
  18. RuntimeInformation.IsOSPlatform (OSPlatform.FreeBSD)
  19. ? 0x40087468u // Darwin/BSD
  20. : 0x5413u; // Linux
  21. [DllImport ("libc", SetLastError = true)]
  22. private static extern int ioctl (int fd, uint request, out WinSize ws);
  23. // File descriptor for stdout
  24. private const int STDOUT_FILENO = 1;
  25. [DllImport ("libc")]
  26. private static extern int write (int fd, byte [] buf, int n);
  27. [DllImport ("libc", SetLastError = true)]
  28. private static extern int dup (int fd);
  29. /// <inheritdoc />
  30. protected override void AppendOrWriteAttribute (StringBuilder output, Attribute attr, TextStyle redrawTextStyle)
  31. {
  32. if (Terminal.Gui.Drivers.Driver.Force16Colors)
  33. {
  34. output.Append (EscSeqUtils.CSI_SetForegroundColor (attr.Foreground.GetAnsiColorCode ()));
  35. output.Append (EscSeqUtils.CSI_SetBackgroundColor (attr.Background.GetAnsiColorCode ()));
  36. }
  37. else
  38. {
  39. EscSeqUtils.CSI_AppendForegroundColorRGB (
  40. output,
  41. attr.Foreground.R,
  42. attr.Foreground.G,
  43. attr.Foreground.B
  44. );
  45. EscSeqUtils.CSI_AppendBackgroundColorRGB (
  46. output,
  47. attr.Background.R,
  48. attr.Background.G,
  49. attr.Background.B
  50. );
  51. EscSeqUtils.CSI_AppendTextStyleChange (output, redrawTextStyle, attr.Style);
  52. }
  53. }
  54. /// <inheritdoc />
  55. protected override void Write (StringBuilder output)
  56. {
  57. try
  58. {
  59. byte [] utf8 = Encoding.UTF8.GetBytes (output.ToString ());
  60. // Write to stdout (fd 1)
  61. write (STDOUT_FILENO, utf8, utf8.Length);
  62. }
  63. catch
  64. {
  65. // ignore for unit tests
  66. }
  67. }
  68. private Point? _lastCursorPosition;
  69. /// <inheritdoc />
  70. protected override bool SetCursorPositionImpl (int screenPositionX, int screenPositionY)
  71. {
  72. if (_lastCursorPosition is { } && _lastCursorPosition.Value.X == screenPositionX && _lastCursorPosition.Value.Y == screenPositionY)
  73. {
  74. return true;
  75. }
  76. _lastCursorPosition = new (screenPositionX, screenPositionY);
  77. try
  78. {
  79. using TextWriter? writer = CreateUnixStdoutWriter ();
  80. // + 1 is needed because Unix is based on 1 instead of 0 and
  81. EscSeqUtils.CSI_WriteCursorPosition (writer!, screenPositionY + 1, screenPositionX + 1);
  82. }
  83. catch
  84. {
  85. // ignore
  86. }
  87. return true;
  88. }
  89. private TextWriter? CreateUnixStdoutWriter ()
  90. {
  91. // duplicate stdout so we don't mess with Console.Out's FD
  92. int fdCopy = dup (STDOUT_FILENO);
  93. if (fdCopy == -1)
  94. {
  95. // Log but don't throw - we're likely running without a TTY (CI/CD, tests, etc.)
  96. var errno = Marshal.GetLastWin32Error ();
  97. Logging.Warning ($"Failed to dup STDOUT_FILENO, errno={errno}. Running without TTY support.");
  98. return null; // Return null instead of throwing
  99. }
  100. try
  101. {
  102. // wrap the raw fd into a SafeFileHandle
  103. SafeFileHandle handle = new SafeFileHandle (fdCopy, ownsHandle: true);
  104. // create FileStream from the safe handle
  105. FileStream stream = new FileStream (handle, FileAccess.Write);
  106. return new StreamWriter (stream)
  107. {
  108. AutoFlush = true
  109. };
  110. }
  111. catch (Exception ex)
  112. {
  113. Logging.Warning ($"Failed to create TextWriter from dup'd STDOUT: {ex.Message}");
  114. return null;
  115. }
  116. }
  117. /// <inheritdoc />
  118. public void Write (ReadOnlySpan<char> text)
  119. {
  120. try
  121. {
  122. byte [] utf8 = Encoding.UTF8.GetBytes (text.ToArray ());
  123. // Write to stdout (fd 1)
  124. write (STDOUT_FILENO, utf8, utf8.Length);
  125. }
  126. catch
  127. {
  128. // ignore for unit tests
  129. }
  130. }
  131. /// <inheritdoc />
  132. public Size GetSize ()
  133. {
  134. try
  135. {
  136. if (ioctl (1, TIOCGWINSZ, out WinSize ws) == 0)
  137. {
  138. if (ws.ws_col > 0 && ws.ws_row > 0)
  139. {
  140. return new (ws.ws_col, ws.ws_row);
  141. }
  142. }
  143. }
  144. catch
  145. {
  146. // ignore
  147. }
  148. return new (80, 25); // fallback
  149. }
  150. private EscSeqUtils.DECSCUSR_Style? _currentDecscusrStyle;
  151. /// <inheritdoc cref="IOutput.SetCursorVisibility"/>
  152. public override void SetCursorVisibility (CursorVisibility visibility)
  153. {
  154. try
  155. {
  156. if (visibility != CursorVisibility.Invisible)
  157. {
  158. if (_currentDecscusrStyle is null || _currentDecscusrStyle != (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF))
  159. {
  160. _currentDecscusrStyle = (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF);
  161. Write (EscSeqUtils.CSI_SetCursorStyle ((EscSeqUtils.DECSCUSR_Style)_currentDecscusrStyle));
  162. }
  163. Write (EscSeqUtils.CSI_ShowCursor);
  164. }
  165. else
  166. {
  167. Write (EscSeqUtils.CSI_HideCursor);
  168. }
  169. }
  170. catch
  171. {
  172. // ignore
  173. }
  174. }
  175. /// <inheritdoc />
  176. public Point GetCursorPosition ()
  177. {
  178. return _lastCursorPosition ?? Point.Empty;
  179. }
  180. /// <inheritdoc />
  181. public void SetCursorPosition (int col, int row)
  182. {
  183. SetCursorPositionImpl (col, row);
  184. }
  185. /// <inheritdoc />
  186. public void SetSize (int width, int height)
  187. {
  188. // Do nothing
  189. }
  190. /// <inheritdoc />
  191. public void Dispose ()
  192. {
  193. }
  194. }