UnixOutput.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 (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. base.Write (output);
  58. try
  59. {
  60. byte [] utf8 = Encoding.UTF8.GetBytes (output.ToString ());
  61. // Write to stdout (fd 1)
  62. write (STDOUT_FILENO, utf8, utf8.Length);
  63. }
  64. catch
  65. {
  66. // ignore for unit tests
  67. }
  68. }
  69. private Point? _lastCursorPosition;
  70. /// <inheritdoc />
  71. protected override bool SetCursorPositionImpl (int screenPositionX, int screenPositionY)
  72. {
  73. if (_lastCursorPosition is { } && _lastCursorPosition.Value.X == screenPositionX && _lastCursorPosition.Value.Y == screenPositionY)
  74. {
  75. return true;
  76. }
  77. _lastCursorPosition = new (screenPositionX, screenPositionY);
  78. try
  79. {
  80. using TextWriter? writer = CreateUnixStdoutWriter ();
  81. // + 1 is needed because Unix is based on 1 instead of 0 and
  82. EscSeqUtils.CSI_WriteCursorPosition (writer!, screenPositionY + 1, screenPositionX + 1);
  83. }
  84. catch
  85. {
  86. // ignore
  87. }
  88. return true;
  89. }
  90. private TextWriter? CreateUnixStdoutWriter ()
  91. {
  92. // duplicate stdout so we don't mess with Console.Out's FD
  93. int fdCopy = dup (STDOUT_FILENO);
  94. if (fdCopy == -1)
  95. {
  96. // Log but don't throw - we're likely running without a TTY (CI/CD, tests, etc.)
  97. var errno = Marshal.GetLastWin32Error ();
  98. Logging.Warning ($"Failed to dup STDOUT_FILENO, errno={errno}. Running without TTY support.");
  99. return null; // Return null instead of throwing
  100. }
  101. try
  102. {
  103. // wrap the raw fd into a SafeFileHandle
  104. SafeFileHandle handle = new SafeFileHandle (fdCopy, ownsHandle: true);
  105. // create FileStream from the safe handle
  106. FileStream stream = new FileStream (handle, FileAccess.Write);
  107. return new StreamWriter (stream)
  108. {
  109. AutoFlush = true
  110. };
  111. }
  112. catch (Exception ex)
  113. {
  114. Logging.Warning ($"Failed to create TextWriter from dup'd STDOUT: {ex.Message}");
  115. return null;
  116. }
  117. }
  118. /// <inheritdoc />
  119. public void Write (ReadOnlySpan<char> text)
  120. {
  121. try
  122. {
  123. byte [] utf8 = Encoding.UTF8.GetBytes (text.ToArray ());
  124. // Write to stdout (fd 1)
  125. write (STDOUT_FILENO, utf8, utf8.Length);
  126. }
  127. catch
  128. {
  129. // ignore for unit tests
  130. }
  131. }
  132. /// <inheritdoc />
  133. public Size GetSize ()
  134. {
  135. try
  136. {
  137. if (ioctl (1, TIOCGWINSZ, out WinSize ws) == 0)
  138. {
  139. if (ws.ws_col > 0 && ws.ws_row > 0)
  140. {
  141. return new (ws.ws_col, ws.ws_row);
  142. }
  143. }
  144. }
  145. catch
  146. {
  147. // ignore
  148. }
  149. return new (80, 25); // fallback
  150. }
  151. private EscSeqUtils.DECSCUSR_Style? _currentDecscusrStyle;
  152. /// <inheritdoc cref="IOutput.SetCursorVisibility"/>
  153. public override void SetCursorVisibility (CursorVisibility visibility)
  154. {
  155. try
  156. {
  157. if (visibility != CursorVisibility.Invisible)
  158. {
  159. if (_currentDecscusrStyle is null || _currentDecscusrStyle != (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF))
  160. {
  161. _currentDecscusrStyle = (EscSeqUtils.DECSCUSR_Style)(((int)visibility >> 24) & 0xFF);
  162. Write (EscSeqUtils.CSI_SetCursorStyle ((EscSeqUtils.DECSCUSR_Style)_currentDecscusrStyle));
  163. }
  164. Write (EscSeqUtils.CSI_ShowCursor);
  165. }
  166. else
  167. {
  168. Write (EscSeqUtils.CSI_HideCursor);
  169. }
  170. }
  171. catch
  172. {
  173. // ignore
  174. }
  175. }
  176. /// <inheritdoc />
  177. public Point GetCursorPosition ()
  178. {
  179. return _lastCursorPosition ?? Point.Empty;
  180. }
  181. /// <inheritdoc />
  182. public void SetCursorPosition (int col, int row)
  183. {
  184. SetCursorPositionImpl (col, row);
  185. }
  186. /// <inheritdoc />
  187. public void SetSize (int width, int height)
  188. {
  189. // Do nothing
  190. }
  191. /// <inheritdoc />
  192. public void Dispose ()
  193. {
  194. }
  195. }