IDriver.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <see cref="IOutputBuffer"/> containing the buffered screen contents.
  56. /// </summary>
  57. /// <returns></returns>
  58. IOutputBuffer GetOutputBuffer ();
  59. /// <summary>
  60. /// Gets the <see cref="IOutput"/> responsible for writing to the terminal.
  61. /// </summary>
  62. IOutput GetOutput ();
  63. /// <summary>Get the operating system clipboard.</summary>
  64. IClipboard? Clipboard { get; }
  65. #endregion Driver Components
  66. #region Screen and Display
  67. /// <summary>Gets the location and size of the terminal screen.</summary>
  68. Rectangle Screen { get; }
  69. /// <summary>
  70. /// Sets the screen size. <see cref="Screen"/> is the source of truth for screen dimensions.
  71. /// </summary>
  72. /// <param name="width">The new width in columns.</param>
  73. /// <param name="height">The new height in rows.</param>
  74. void SetScreenSize (int width, int height);
  75. /// <summary>
  76. /// The event fired when the screen changes (size, position, etc.).
  77. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  78. /// </summary>
  79. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  80. /// <summary>The number of columns visible in the terminal.</summary>
  81. int Cols { get; set; }
  82. /// <summary>The number of rows visible in the terminal.</summary>
  83. int Rows { get; set; }
  84. /// <summary>The leftmost column in the terminal.</summary>
  85. int Left { get; set; }
  86. /// <summary>The topmost row in the terminal.</summary>
  87. int Top { get; set; }
  88. #endregion Screen and Display
  89. #region Color Support
  90. /// <summary>Gets whether the <see cref="IDriver"/> supports TrueColor output.</summary>
  91. bool SupportsTrueColor { get; }
  92. /// <summary>
  93. /// Gets or sets whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  94. /// </summary>
  95. /// <remarks>
  96. /// <para>
  97. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  98. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  99. /// </para>
  100. /// </remarks>
  101. /// <seealso cref="Driver.Force16Colors"/>
  102. bool Force16Colors { get; set; }
  103. #endregion Color Support
  104. #region Content Buffer
  105. // BUGBUG: This should not be publicly settable.
  106. /// <summary>
  107. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  108. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  109. /// </summary>
  110. Cell [,]? Contents { get; set; }
  111. /// <summary>
  112. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  113. /// to.
  114. /// </summary>
  115. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  116. Region? Clip { get; set; }
  117. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  118. void ClearContents ();
  119. /// <summary>
  120. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  121. /// </summary>
  122. event EventHandler<EventArgs> ClearedContents;
  123. #endregion Content Buffer
  124. #region Drawing and Rendering
  125. /// <summary>
  126. /// Gets the column 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 Col { get; }
  130. /// <summary>
  131. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  132. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  133. /// </summary>
  134. int Row { get; }
  135. /// <summary>
  136. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or
  137. /// <see cref="AddStr"/>
  138. /// call.
  139. /// </summary>
  140. Attribute CurrentAttribute { get; set; }
  141. /// <summary>
  142. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  143. /// <see cref="IDriver.Contents"/>.
  144. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  145. /// where to add content.
  146. /// </summary>
  147. /// <remarks>
  148. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  149. /// <para>
  150. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  151. /// and
  152. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  153. /// </para>
  154. /// </remarks>
  155. /// <param name="col">Column to move to.</param>
  156. /// <param name="row">Row to move to.</param>
  157. void Move (int col, int row);
  158. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  159. /// <param name="rune"></param>
  160. /// <returns>
  161. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  162. /// support displaying this rune.
  163. /// </returns>
  164. bool IsRuneSupported (Rune rune);
  165. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Text.</summary>
  166. /// <param name="text">Used to determine if one or two columns are required.</param>
  167. /// <param name="col">The column.</param>
  168. /// <param name="row">The row.</param>
  169. /// <returns>
  170. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  171. /// <see cref="IDriver.Clip"/>.
  172. /// <see langword="true"/> otherwise.
  173. /// </returns>
  174. bool IsValidLocation (string text, int col, int row);
  175. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  176. /// <remarks>
  177. /// <para>
  178. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  179. /// <paramref name="rune"/> required, even if the new column value is outside of the
  180. /// <see cref="IDriver.Clip"/> or screen
  181. /// dimensions defined by <see cref="IDriver.Cols"/>.
  182. /// </para>
  183. /// <para>
  184. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  185. /// of columns
  186. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  187. /// character (U+FFFD)
  188. /// will be added instead.
  189. /// </para>
  190. /// </remarks>
  191. /// <param name="rune">Rune to add.</param>
  192. void AddRune (Rune rune);
  193. /// <summary>
  194. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  195. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  196. /// constructor.
  197. /// </summary>
  198. /// <param name="c">Character to add.</param>
  199. void AddRune (char c);
  200. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  201. /// <remarks>
  202. /// <para>
  203. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  204. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  205. /// or screen
  206. /// dimensions defined by <see cref="IDriver.Cols"/>.
  207. /// </para>
  208. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  209. /// </remarks>
  210. /// <param name="str">String.</param>
  211. void AddStr (string str);
  212. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  213. /// <remarks>
  214. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  215. /// drawn.
  216. /// </remarks>
  217. /// <param name="rect">The Screen-relative rectangle.</param>
  218. /// <param name="rune">The Rune used to fill the rectangle</param>
  219. void FillRect (Rectangle rect, Rune rune = default);
  220. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  221. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  222. /// <param name="c">C.</param>
  223. Attribute SetAttribute (Attribute c);
  224. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  225. /// <returns>The current attribute.</returns>
  226. Attribute GetAttribute ();
  227. /// <summary>
  228. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  229. /// </summary>
  230. /// <param name="ansi"></param>
  231. void WriteRaw (string ansi);
  232. /// <summary>
  233. /// Gets the queue of sixel images to write out to screen when updating.
  234. /// If the terminal does not support Sixel, adding to this queue has no effect.
  235. /// </summary>
  236. ConcurrentQueue<SixelToRender> GetSixels ();
  237. /// <summary>
  238. /// Gets a string representation of <see cref="Contents"/>.
  239. /// </summary>
  240. /// <returns></returns>
  241. public string ToString ();
  242. /// <summary>
  243. /// Gets an ANSI escape sequence representation of <see cref="Contents"/>. This is the
  244. /// same output as would be written to the terminal to recreate the current screen contents.
  245. /// </summary>
  246. /// <returns></returns>
  247. public string ToAnsi ();
  248. #endregion Drawing and Rendering
  249. #region Cursor
  250. /// <summary>
  251. /// Sets the position of the terminal cursor to <see cref="IDriver.Col"/> and
  252. /// <see cref="IDriver.Row"/>.
  253. /// </summary>
  254. void UpdateCursor ();
  255. /// <summary>Gets the terminal cursor visibility.</summary>
  256. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  257. /// <returns><see langword="true"/> upon success</returns>
  258. bool GetCursorVisibility (out CursorVisibility visibility);
  259. /// <summary>Sets the terminal cursor visibility.</summary>
  260. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  261. /// <returns><see langword="true"/> upon success</returns>
  262. bool SetCursorVisibility (CursorVisibility visibility);
  263. #endregion Cursor
  264. #region Input Events
  265. /// <summary>Event fired when a mouse event occurs.</summary>
  266. event EventHandler<MouseEventArgs>? MouseEvent;
  267. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="IDriver.KeyUp"/>.</summary>
  268. event EventHandler<Key>? KeyDown;
  269. /// <summary>Event fired when a key is released.</summary>
  270. /// <remarks>
  271. /// Drivers that do not support key release events will fire this event after <see cref="IDriver.KeyDown"/>
  272. /// processing is
  273. /// complete.
  274. /// </remarks>
  275. event EventHandler<Key>? KeyUp;
  276. /// <summary>
  277. /// Enqueues a key input event to the driver. For unit tests.
  278. /// </summary>
  279. /// <param name="key"></param>
  280. void EnqueueKeyEvent (Key key);
  281. #endregion Input Events
  282. #region ANSI Escape Sequences
  283. /// <summary>
  284. /// Queues the given <paramref name="request"/> for execution
  285. /// </summary>
  286. /// <param name="request"></param>
  287. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  288. #endregion ANSI Escape Sequences
  289. }