IDriver.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>Base interface for Terminal.Gui Driver implementations.</summary>
  4. /// <remarks>
  5. /// There are currently four implementations: UnixDriver, WindowsDriver, DotNetDriver, and FakeDriver
  6. /// </remarks>
  7. public interface IDriver : IDisposable
  8. {
  9. #region Driver Lifecycle
  10. /// <summary>Initializes the driver</summary>
  11. void Init ();
  12. /// <summary>
  13. /// INTERNAL: Updates the terminal with the current output buffer. Should not be used by applications. Drawing occurs
  14. /// once each Application main loop iteration.
  15. /// </summary>
  16. void Refresh ();
  17. /// <summary>
  18. /// Gets the name of the driver implementation.
  19. /// </summary>
  20. string? GetName ();
  21. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  22. /// <returns></returns>
  23. string GetVersionInfo ();
  24. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  25. /// <remarks>This is only implemented in UnixDriver.</remarks>
  26. void Suspend ();
  27. /// <summary>
  28. /// Gets whether the driver has detected the console requires legacy console API (Windows Console API without ANSI/VT
  29. /// support).
  30. /// Returns <see langword="true"/> for legacy consoles that don't support modern ANSI escape sequences (e.g. Windows
  31. /// conhost);
  32. /// <see langword="false"/> for modern terminals with ANSI/VT support.
  33. /// </summary>
  34. /// <remarks>
  35. /// <para>
  36. /// This property indicates whether the terminal supports modern ANSI escape sequences for input/output.
  37. /// On Windows, this maps to whether Virtual Terminal processing is enabled.
  38. /// On Unix-like systems, this is typically <see langword="false"/> as they support ANSI by default.
  39. /// </para>
  40. /// <para>
  41. /// When <see langword="true"/>, the driver must use legacy Windows Console API functions
  42. /// (e.g., WriteConsoleW, SetConsoleTextAttribute) instead of ANSI escape sequences.
  43. /// </para>
  44. /// </remarks>
  45. bool IsLegacyConsole { get; internal set; }
  46. #endregion Driver Lifecycle
  47. #region Driver Components
  48. /// <summary>
  49. /// Class responsible for processing native driver input objects
  50. /// e.g. <see cref="ConsoleKeyInfo"/> into <see cref="Key"/> events
  51. /// and detecting and processing ansi escape sequences.
  52. /// </summary>
  53. IInputProcessor GetInputProcessor ();
  54. /// <summary>
  55. /// Gets the output handler responsible for writing to the terminal.
  56. /// </summary>
  57. IOutput GetOutput ();
  58. /// <summary>Get the operating system clipboard.</summary>
  59. IClipboard? Clipboard { get; }
  60. #endregion Driver Components
  61. #region Screen and Display
  62. /// <summary>Gets the location and size of the terminal screen.</summary>
  63. Rectangle Screen { get; }
  64. /// <summary>
  65. /// Sets the screen size. <see cref="Screen"/> is the source of truth for screen dimensions.
  66. /// </summary>
  67. /// <param name="width">The new width in columns.</param>
  68. /// <param name="height">The new height in rows.</param>
  69. void SetScreenSize (int width, int height);
  70. /// <summary>
  71. /// The event fired when the screen changes (size, position, etc.).
  72. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  73. /// </summary>
  74. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  75. /// <summary>The number of columns visible in the terminal.</summary>
  76. int Cols { get; set; }
  77. /// <summary>The number of rows visible in the terminal.</summary>
  78. int Rows { get; set; }
  79. /// <summary>The leftmost column in the terminal.</summary>
  80. int Left { get; set; }
  81. /// <summary>The topmost row in the terminal.</summary>
  82. int Top { get; set; }
  83. #endregion Screen and Display
  84. #region Color Support
  85. /// <summary>Gets whether the <see cref="IDriver"/> supports TrueColor output.</summary>
  86. bool SupportsTrueColor { get; }
  87. /// <summary>
  88. /// Gets or sets whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  89. /// </summary>
  90. /// <remarks>
  91. /// <para>
  92. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  93. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  94. /// </para>
  95. /// </remarks>
  96. /// <seealso cref="Driver.Force16Colors"/>
  97. bool Force16Colors { get; set; }
  98. #endregion Color Support
  99. #region Content Buffer
  100. // BUGBUG: This should not be publicly settable.
  101. /// <summary>
  102. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  103. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  104. /// </summary>
  105. Cell [,]? Contents { get; set; }
  106. /// <summary>
  107. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  108. /// to.
  109. /// </summary>
  110. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  111. Region? Clip { get; set; }
  112. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  113. void ClearContents ();
  114. /// <summary>
  115. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  116. /// </summary>
  117. event EventHandler<EventArgs> ClearedContents;
  118. #endregion Content Buffer
  119. #region Drawing and Rendering
  120. /// <summary>
  121. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  122. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  123. /// </summary>
  124. int Col { get; }
  125. /// <summary>
  126. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  127. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  128. /// </summary>
  129. int Row { get; }
  130. /// <summary>
  131. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or
  132. /// <see cref="AddStr"/>
  133. /// call.
  134. /// </summary>
  135. Attribute CurrentAttribute { get; set; }
  136. /// <summary>
  137. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  138. /// <see cref="IDriver.Contents"/>.
  139. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  140. /// where to add content.
  141. /// </summary>
  142. /// <remarks>
  143. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  144. /// <para>
  145. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  146. /// and
  147. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  148. /// </para>
  149. /// </remarks>
  150. /// <param name="col">Column to move to.</param>
  151. /// <param name="row">Row to move to.</param>
  152. void Move (int col, int row);
  153. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  154. /// <param name="rune"></param>
  155. /// <returns>
  156. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  157. /// support displaying this rune.
  158. /// </returns>
  159. bool IsRuneSupported (Rune rune);
  160. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Text.</summary>
  161. /// <param name="text">Used to determine if one or two columns are required.</param>
  162. /// <param name="col">The column.</param>
  163. /// <param name="row">The row.</param>
  164. /// <returns>
  165. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  166. /// <see cref="IDriver.Clip"/>.
  167. /// <see langword="true"/> otherwise.
  168. /// </returns>
  169. bool IsValidLocation (string text, int col, int row);
  170. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  171. /// <remarks>
  172. /// <para>
  173. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  174. /// <paramref name="rune"/> required, even if the new column value is outside of the
  175. /// <see cref="IDriver.Clip"/> or screen
  176. /// dimensions defined by <see cref="IDriver.Cols"/>.
  177. /// </para>
  178. /// <para>
  179. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  180. /// of columns
  181. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  182. /// character (U+FFFD)
  183. /// will be added instead.
  184. /// </para>
  185. /// </remarks>
  186. /// <param name="rune">Rune to add.</param>
  187. void AddRune (Rune rune);
  188. /// <summary>
  189. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  190. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  191. /// constructor.
  192. /// </summary>
  193. /// <param name="c">Character to add.</param>
  194. void AddRune (char c);
  195. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  196. /// <remarks>
  197. /// <para>
  198. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  199. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  200. /// or screen
  201. /// dimensions defined by <see cref="IDriver.Cols"/>.
  202. /// </para>
  203. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  204. /// </remarks>
  205. /// <param name="str">String.</param>
  206. void AddStr (string str);
  207. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  208. /// <remarks>
  209. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  210. /// drawn.
  211. /// </remarks>
  212. /// <param name="rect">The Screen-relative rectangle.</param>
  213. /// <param name="rune">The Rune used to fill the rectangle</param>
  214. void FillRect (Rectangle rect, Rune rune = default);
  215. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  216. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  217. /// <param name="c">C.</param>
  218. Attribute SetAttribute (Attribute c);
  219. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  220. /// <returns>The current attribute.</returns>
  221. Attribute GetAttribute ();
  222. /// <summary>
  223. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  224. /// </summary>
  225. /// <param name="ansi"></param>
  226. void WriteRaw (string ansi);
  227. /// <summary>
  228. /// Gets the queue of sixel images to write out to screen when updating.
  229. /// If the terminal does not support Sixel, adding to this queue has no effect.
  230. /// </summary>
  231. ConcurrentQueue<SixelToRender> GetSixels ();
  232. /// <summary>
  233. /// Gets a string representation of <see cref="Contents"/>.
  234. /// </summary>
  235. /// <returns></returns>
  236. public string ToString ();
  237. /// <summary>
  238. /// Gets an ANSI escape sequence representation of <see cref="Contents"/>. This is the
  239. /// same output as would be written to the terminal to recreate the current screen contents.
  240. /// </summary>
  241. /// <returns></returns>
  242. public string ToAnsi ();
  243. #endregion Drawing and Rendering
  244. #region Cursor
  245. /// <summary>
  246. /// Sets the position of the terminal cursor to <see cref="IDriver.Col"/> and
  247. /// <see cref="IDriver.Row"/>.
  248. /// </summary>
  249. void UpdateCursor ();
  250. /// <summary>Gets the terminal cursor visibility.</summary>
  251. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  252. /// <returns><see langword="true"/> upon success</returns>
  253. bool GetCursorVisibility (out CursorVisibility visibility);
  254. /// <summary>Sets the terminal cursor visibility.</summary>
  255. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  256. /// <returns><see langword="true"/> upon success</returns>
  257. bool SetCursorVisibility (CursorVisibility visibility);
  258. #endregion Cursor
  259. #region Input Events
  260. /// <summary>Event fired when a mouse event occurs.</summary>
  261. event EventHandler<MouseEventArgs>? MouseEvent;
  262. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="IDriver.KeyUp"/>.</summary>
  263. event EventHandler<Key>? KeyDown;
  264. /// <summary>Event fired when a key is released.</summary>
  265. /// <remarks>
  266. /// Drivers that do not support key release events will fire this event after <see cref="IDriver.KeyDown"/>
  267. /// processing is
  268. /// complete.
  269. /// </remarks>
  270. event EventHandler<Key>? KeyUp;
  271. /// <summary>
  272. /// Enqueues a key input event to the driver. For unit tests.
  273. /// </summary>
  274. /// <param name="key"></param>
  275. void EnqueueKeyEvent (Key key);
  276. #endregion Input Events
  277. #region ANSI Escape Sequences
  278. /// <summary>
  279. /// Queues the given <paramref name="request"/> for execution
  280. /// </summary>
  281. /// <param name="request"></param>
  282. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  283. #endregion ANSI Escape Sequences
  284. }