IDriver.cs 14 KB

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