IDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. namespace Terminal.Gui.Drivers;
  2. /// <summary>Base interface for Terminal.Gui Driver implementations.</summary>
  3. /// <remarks>
  4. /// There are currently four implementations: UnixDriver, WindowsDriver, DotNetDriver, and FakeDriver
  5. /// </remarks>
  6. public interface IDriver
  7. {
  8. /// <summary>
  9. /// Gets the name of the driver implementation.
  10. /// </summary>
  11. string? GetName ();
  12. /// <summary>
  13. /// Class responsible for processing native driver input objects
  14. /// e.g. <see cref="ConsoleKeyInfo"/> into <see cref="Key"/> events
  15. /// and detecting and processing ansi escape sequences.
  16. /// </summary>
  17. IInputProcessor InputProcessor { get; }
  18. /// <summary>
  19. /// Describes the desired screen state. Data source for <see cref="IOutput"/>.
  20. /// </summary>
  21. IOutputBuffer OutputBuffer { get; }
  22. /// <summary>
  23. /// Interface for classes responsible for reporting the current
  24. /// size of the terminal window.
  25. /// </summary>
  26. ISizeMonitor SizeMonitor { get; }
  27. /// <summary>Get the operating system clipboard.</summary>
  28. ///
  29. IClipboard? Clipboard { get; }
  30. /// <summary>Gets the location and size of the terminal screen.</summary>
  31. Rectangle Screen { get; }
  32. /// <summary>
  33. /// Sets the screen size for testing purposes. Only supported by FakeDriver.
  34. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  35. /// </summary>
  36. /// <param name="width">The new width in columns.</param>
  37. /// <param name="height">The new height in rows.</param>
  38. /// <exception cref="NotSupportedException">Thrown when called on non-FakeDriver instances.</exception>
  39. void SetScreenSize (int width, int height);
  40. /// <summary>
  41. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  42. /// to.
  43. /// </summary>
  44. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  45. Region? Clip { get; set; }
  46. /// <summary>
  47. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  48. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  49. /// </summary>
  50. int Col { get; }
  51. /// <summary>The number of columns visible in the terminal.</summary>
  52. int Cols { get; set; }
  53. // BUGBUG: This should not be publicly settable.
  54. /// <summary>
  55. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  56. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  57. /// </summary>
  58. Cell [,]? Contents { get; set; }
  59. /// <summary>The leftmost column in the terminal.</summary>
  60. int Left { get; set; }
  61. /// <summary>
  62. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  63. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  64. /// </summary>
  65. int Row { get; }
  66. /// <summary>The number of rows visible in the terminal.</summary>
  67. int Rows { get; set; }
  68. /// <summary>The topmost row in the terminal.</summary>
  69. int Top { get; set; }
  70. /// <summary>Gets whether the <see cref="IDriver"/> supports TrueColor output.</summary>
  71. bool SupportsTrueColor { get; }
  72. /// <summary>
  73. /// Gets or sets whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  74. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  75. /// </summary>
  76. /// <remarks>
  77. /// <para>
  78. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  79. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  80. /// </para>
  81. /// </remarks>
  82. bool Force16Colors { get; set; }
  83. /// <summary>
  84. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  85. /// call.
  86. /// </summary>
  87. Attribute CurrentAttribute { get; set; }
  88. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  89. /// <returns></returns>
  90. string GetVersionInfo ();
  91. /// <summary>
  92. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  93. /// </summary>
  94. /// <param name="ansi"></param>
  95. void WriteRaw (string ansi);
  96. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  97. /// <param name="rune"></param>
  98. /// <returns>
  99. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  100. /// support displaying this rune.
  101. /// </returns>
  102. bool IsRuneSupported (Rune rune);
  103. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Rune.</summary>
  104. /// <param name="rune">Used to determine if one or two columns are required.</param>
  105. /// <param name="col">The column.</param>
  106. /// <param name="row">The row.</param>
  107. /// <returns>
  108. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  109. /// <see cref="IDriver.Clip"/>.
  110. /// <see langword="true"/> otherwise.
  111. /// </returns>
  112. bool IsValidLocation (Rune rune, int col, int row);
  113. /// <summary>
  114. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  115. /// <see cref="IDriver.Contents"/>.
  116. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  117. /// where to add content.
  118. /// </summary>
  119. /// <remarks>
  120. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  121. /// <para>
  122. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  123. /// and
  124. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  125. /// </para>
  126. /// </remarks>
  127. /// <param name="col">Column to move to.</param>
  128. /// <param name="row">Row to move to.</param>
  129. void Move (int col, int row);
  130. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  131. /// <remarks>
  132. /// <para>
  133. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  134. /// <paramref name="rune"/> required, even if the new column value is outside of the
  135. /// <see cref="IDriver.Clip"/> or screen
  136. /// dimensions defined by <see cref="IDriver.Cols"/>.
  137. /// </para>
  138. /// <para>
  139. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  140. /// of columns
  141. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  142. /// character (U+FFFD)
  143. /// will be added instead.
  144. /// </para>
  145. /// </remarks>
  146. /// <param name="rune">Rune to add.</param>
  147. void AddRune (Rune rune);
  148. /// <summary>
  149. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  150. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  151. /// constructor.
  152. /// </summary>
  153. /// <param name="c">Character to add.</param>
  154. void AddRune (char c);
  155. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  156. /// <remarks>
  157. /// <para>
  158. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  159. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  160. /// or screen
  161. /// dimensions defined by <see cref="IDriver.Cols"/>.
  162. /// </para>
  163. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  164. /// </remarks>
  165. /// <param name="str">String.</param>
  166. void AddStr (string str);
  167. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  168. void ClearContents ();
  169. /// <summary>
  170. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  171. /// </summary>
  172. event EventHandler<EventArgs> ClearedContents;
  173. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  174. /// <remarks>
  175. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  176. /// drawn.
  177. /// </remarks>
  178. /// <param name="rect">The Screen-relative rectangle.</param>
  179. /// <param name="rune">The Rune used to fill the rectangle</param>
  180. void FillRect (Rectangle rect, Rune rune = default);
  181. /// <summary>
  182. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  183. /// that calls <see cref="IDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  184. /// </summary>
  185. /// <param name="rect"></param>
  186. /// <param name="c"></param>
  187. void FillRect (Rectangle rect, char c);
  188. /// <summary>Gets the terminal cursor visibility.</summary>
  189. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  190. /// <returns><see langword="true"/> upon success</returns>
  191. bool GetCursorVisibility (out CursorVisibility visibility);
  192. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  193. void Refresh ();
  194. /// <summary>Sets the terminal cursor visibility.</summary>
  195. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  196. /// <returns><see langword="true"/> upon success</returns>
  197. bool SetCursorVisibility (CursorVisibility visibility);
  198. /// <summary>
  199. /// The event fired when the screen changes (size, position, etc.).
  200. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  201. /// </summary>
  202. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  203. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  204. /// <remarks>This is only implemented in UnixDriver.</remarks>
  205. void Suspend ();
  206. /// <summary>
  207. /// Sets the position of the terminal cursor to <see cref="IDriver.Col"/> and
  208. /// <see cref="IDriver.Row"/>.
  209. /// </summary>
  210. void UpdateCursor ();
  211. /// <summary>Initializes the driver</summary>
  212. void Init ();
  213. /// <summary>Ends the execution of the console driver.</summary>
  214. void End ();
  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>Event fired when a mouse event occurs.</summary>
  223. event EventHandler<MouseEventArgs>? MouseEvent;
  224. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="IDriver.KeyUp"/>.</summary>
  225. event EventHandler<Key>? KeyDown;
  226. /// <summary>Event fired when a key is released.</summary>
  227. /// <remarks>
  228. /// Drivers that do not support key release events will fire this event after <see cref="IDriver.KeyDown"/>
  229. /// processing is
  230. /// complete.
  231. /// </remarks>
  232. event EventHandler<Key>? KeyUp;
  233. /// <summary>
  234. /// Enqueues a key input event to the driver. For unit tests.
  235. /// </summary>
  236. /// <param name="key"></param>
  237. void EnqueueKeyEvent (Key key);
  238. /// <summary>
  239. /// Queues the given <paramref name="request"/> for execution
  240. /// </summary>
  241. /// <param name="request"></param>
  242. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  243. /// <summary>
  244. /// Gets the <see cref="AnsiRequestScheduler"/> for the driver
  245. /// </summary>
  246. /// <returns></returns>
  247. public AnsiRequestScheduler GetRequestScheduler ();
  248. /// <summary>
  249. /// Gets a string representation of <see cref="Contents"/>.
  250. /// </summary>
  251. /// <returns></returns>
  252. public string ToString ();
  253. /// <summary>
  254. /// Gets an ANSI escape sequence representation of <see cref="Contents"/>. This is the
  255. /// same output as would be written to the terminal to recreate the current screen contents.
  256. /// </summary>
  257. /// <returns></returns>
  258. public string ToAnsi ();
  259. }