video.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. %
  23. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  24. % The Video unit
  25. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  26. \chapter{The VIDEO unit}
  27. \FPCexampledir{videoex}
  28. The \file{Video} unit implements a screen access layer which is system
  29. independent. It can be used to write on the screen in a system-independent
  30. way, which should be optimal on all platforms for which the unit is
  31. implemented.
  32. The working of the \file{Video} is simple: After calling \seep{InitVideo},
  33. the array \var{VideoBuf} contains a representation of the video screen of
  34. size \var{ScreenWidth*ScreenHeight}, going from left to right and top to
  35. bottom when walking the array elements: \var{VideoBuf[0]} contains the
  36. character and color code of the top-left character on the screen.
  37. \var{VideoBuf[ScreenWidth]} contains the data for the character in the
  38. first columnd of the second row on the screen, and so on.
  39. The contents of the \var{VideoBuf} array may be modified: This is 'writing'
  40. to the screen. As soon as everything that needs to be written in the array
  41. is in the \var{VideoBuf} array, calling \var{UpdateScreen} will copy the
  42. contents of the array screen to the screen, in a manner that is as efficient
  43. as possible.
  44. The updating of the screen can be prohibited to optimize performance; To
  45. this end, the \seep{LockScreenUpdate} function can be used: This will
  46. increment an internal counter. As long as the counter different from zero,
  47. calling \seep{UpdateScreen} will not do anything. The counter can be
  48. lowered with \seep{UnLockscreenUpdate}. When it reaches zero, the next call
  49. to \seep{UpdateScreen} will actually update the screen. This is useful when
  50. having nested procedures that do a lot of screen writing.
  51. The video unit also presents an interface for custom screen drivers, thus
  52. it is possible to override the default screen driver with a custom screen
  53. driver, see the \seep{SetVideoDriver} call.
  54. \section{Constants, Type and variables }
  55. \subsection{Constants}
  56. The following constants describe colors that can be used as
  57. foreground and background colors.
  58. \begin{verbatim}
  59. Black = 0;
  60. Blue = 1;
  61. Green = 2;
  62. Cyan = 3;
  63. Red = 4;
  64. Magenta = 5;
  65. Brown = 6;
  66. LightGray = 7;
  67. \end{verbatim}
  68. The following color constants can be used only as foreground colors:
  69. \begin{verbatim}
  70. DarkGray = 8;
  71. LightBlue = 9;
  72. LightGreen = 10;
  73. LightCyan = 11;
  74. LightRed = 12;
  75. LightMagenta = 13;
  76. Yellow = 14;
  77. White = 15;
  78. \end{verbatim}
  79. The foreground color can be logically or-ed with the blink attribute:
  80. \begin{verbatim}
  81. Blink = 128;
  82. \end{verbatim}
  83. The following constants describe the capabilities of a certain video mode:
  84. \begin{verbatim}
  85. cpUnderLine = $0001;
  86. cpBlink = $0002;
  87. cpColor = $0004;
  88. cpChangeFont = $0008;
  89. cpChangeMode = $0010;
  90. cpChangeCursor = $0020;
  91. \end{verbatim}
  92. The following constants describe the various supported cursor modes:
  93. \begin{verbatim}
  94. crHidden = 0;
  95. crUnderLine = 1;
  96. crBlock = 2;
  97. crHalfBlock = 3;
  98. \end{verbatim}
  99. When a video function needs to report an error condition, the following
  100. constants are used:
  101. \begin{verbatim}
  102. vioOK = 0;
  103. errVioBase = 1000;
  104. errVioInit = errVioBase + 1; { Initialization error}
  105. errVioNotSupported = errVioBase + 2; { Unsupported function }
  106. errVioNoSuchMode = errVioBase + 3; { No such video mode }
  107. \end{verbatim}
  108. The following constants can be read to get some information about the
  109. current screen:
  110. \begin{verbatim}
  111. ScreenWidth : Word = 0;
  112. ScreenHeight : Word = 0;
  113. LowAscii : Boolean = true;
  114. NoExtendedFrame : Boolean = false;
  115. FVMaxWidth = 132;
  116. \end{verbatim}
  117. The error-handling code uses the following constants:
  118. \begin{verbatim}
  119. errOk = 0;
  120. ErrorCode: Longint = ErrOK;
  121. ErrorInfo: Pointer = nil;
  122. ErrorHandler: TErrorHandler = DefaultErrorHandler;
  123. \end{verbatim}
  124. The \var{ErrorHandler} variable can be set to a custom-error handling
  125. function. It is set by default to the \seef{DefaultErrorHandler} function.
  126. The \var{Modes} list contains the list of supported video modes:
  127. \begin{verbatim}
  128. Modes: PVideoModeList = nil;
  129. \end{verbatim}
  130. \subsection{Types}
  131. The \var{TVideoMode} record describes a videomode. Its fields are
  132. self-explaining. The \var{TVideoModeSelector} procedural variable
  133. is used to select a video mode.
  134. \begin{verbatim}
  135. PVideoMode = ^TVideoMode;
  136. TVideoMode = record
  137. Col,Row : Word;
  138. Color : Boolean;
  139. end;
  140. TVideoModeSelector = function (const VideoMode: TVideoMode; Params: Longint): Boolean;
  141. \end{verbatim}
  142. \var{TVideoCell} describes one character on the screen. The high byte
  143. contains the color attribute with which the character is drawn on the screen,
  144. and the low byte contains the ASCII code of the character to be drawn.
  145. \begin{verbatim}
  146. TVideoCell = Word;
  147. PVideoCell = ^TVideoCell;
  148. \end{verbatim}
  149. The \var{TVideoBuf} and \var{PVideoBuf} are two types used to represent the
  150. screen.
  151. \begin{verbatim}
  152. TVideoBuf = array[0..32759] of TVideoCell;
  153. PVideoBuf = ^TVideoBuf;
  154. \end{verbatim}
  155. When registering video modes, the following typed are used to store the
  156. registered modes:
  157. \begin{verbatim}
  158. PVideoModeList = ^TVideoModeList;
  159. TVideoModeList = record
  160. Col, Row: Word;
  161. Color: Boolean;
  162. VideoModeSelector: TVideoModeSelector;
  163. Params: Longint;
  164. Next: PVideoModeList;
  165. end;
  166. \end{verbatim}
  167. The following type is used when reporting error conditions:
  168. \begin{verbatim}
  169. TErrorHandlerReturnValue = (errRetry, errAbort, errContinue);
  170. \end{verbatim}
  171. Here, \var{errRetry} means retry the operation, \var{errAbort}
  172. abort and return error code and \var{errContinue} means abort
  173. without returning an errorcode.
  174. The \var{TErrorHandler} function is used to register an own error
  175. handling function. It should be used when installing a custom error
  176. handling function, and must return one of the above values.
  177. \begin{verbatim}
  178. TErrorHandler =
  179. function (Code: Longint; Info: Pointer): TErrorHandlerReturnValue;
  180. \end{verbatim}
  181. \var{Code} should contain the error code for the error condition,
  182. and the \var{Info} parameter may contain any data type specific to
  183. the error code passed to the function.
  184. \subsection{Variables}
  185. The following variables contain information about the current screen
  186. status:
  187. \begin{verbatim}
  188. ScreenColor : Boolean;
  189. CursorX, CursorY : Word;
  190. \end{verbatim}
  191. \var{ScreenColor} indicates whether the current screen supports colors.
  192. \var{CursorX,CursorY} contain the current cursor position.
  193. The following variables form the heart of the \file{Video} unit: The
  194. \var{VideoBuf} array represents the physical screen. Writing to this
  195. array and calling \seep{UpdateScreen} will write the actual characters
  196. to the screen. \var{VideoBufSize} contains the actual screen size, and is
  197. equal to the product of the number of columns times the number of lines
  198. on the screen (\var{ScreenWidth*ScreenHeight}).
  199. \begin{verbatim}
  200. VideoBuf : PVideoBuf;
  201. VideoBufSize : Longint;
  202. \end
  203. \end{verbatim}
  204. \section{Functions and Procedures}
  205. \begin{procedure}{ClearScreen}
  206. \Declaration
  207. procedure ClearScreen;
  208. \Description
  209. \var{ClearScreen} clears the entire screen, and calls \seep{UpdateScreen}
  210. after that.
  211. \Errors
  212. None.
  213. \SeeAlso
  214. \seep{InitVideo}, \seep{UpdateScreen}
  215. \end{procedure}
  216. \begin{procedure}{DefaultErrorHandler}
  217. \Declaration
  218. function DefaultErrorHandler(AErrorCode: Longint; AErrorInfo: Pointer): TErrorHandlerReturnValue;
  219. \Description
  220. \var{DefaultErrorHandler} is the default error handler used by the video
  221. driver. It simply sets the error code \var{AErrorCode} and \var{AErrorInfo}
  222. in the global variables \var{ErrorCode} and \var{ErrorInfo} and returns
  223. \var{errContinue}.
  224. \Errors
  225. None.
  226. \SeeAlso
  227. \end{procedure}
  228. \begin{procedure}{DefaultVideoModeSelector}
  229. \Declaration
  230. function DefaultVideoModeSelector(const VideoMode: TVideoMode; Params: Longint): Boolean;
  231. \Description
  232. Needs to be removed from the API.
  233. \Errors
  234. \SeeAlso
  235. \end{procedure}
  236. \begin{procedure}{DoneVideo}
  237. \Declaration
  238. procedure DoneVideo;
  239. \Description
  240. \var{DoneVideo} disables the Video driver if the video driver is active. If
  241. the videodriver was already disabled or not yet initialized, it does
  242. nothing. Disabling the driver means it will clean up any allocated
  243. resources, possibly restore the screen in the state it was before
  244. \var{InitVideo} was called.
  245. \Errors
  246. \SeeAlso
  247. \end{procedure}
  248. \begin{function}{GetCapabilities}
  249. \Declaration
  250. function GetCapabilities: Word;
  251. \Description
  252. { Return the capabilities of the current environment }
  253. \Errors
  254. \SeeAlso
  255. \end{function}
  256. \begin{function}{GetCursorType}
  257. \Declaration
  258. function GetCursorType: Word;
  259. \Description
  260. { Return the cursor type: Hidden, UnderLine or Block }
  261. \Errors
  262. \SeeAlso
  263. \end{function}
  264. \begin{procedure}{GetVideoMode}
  265. \Declaration
  266. procedure GetVideoMode(var Mode: TVideoMode);
  267. \Description
  268. { Return dimensions of the current video mode }
  269. \Errors
  270. \SeeAlso
  271. \end{procedure}
  272. \begin{procedure}{InitVideo}
  273. \Declaration
  274. procedure InitVideo;
  275. \Description
  276. { Initializes the video subsystem }
  277. \Errors
  278. \SeeAlso
  279. \end{procedure}
  280. \begin{procedure}{RegisterVideoMode}
  281. \Declaration
  282. procedure RegisterVideoMode(Col, Row: Word; Color: Boolean; VideoModeSelector: TVideoModeSelector; Params: Longint);
  283. \Description
  284. { Registers a video mode to be selectable by SetVideoMode }
  285. { moved to interface because we need a way to retrieve the modes }
  286. \Errors
  287. \SeeAlso
  288. \end{procedure}
  289. \begin{procedure}{SetCursorPos}
  290. \Declaration
  291. procedure SetCursorPos(NewCursorX, NewCursorY: Word);
  292. \Description
  293. { Position the cursor to the given position }
  294. \Errors
  295. \SeeAlso
  296. \end{procedure}
  297. \begin{procedure}{SetCursorType}
  298. \Declaration
  299. procedure SetCursorType(NewType: Word);
  300. \Description
  301. \var{SetCursorType} sets the cursor to the type specified in \var{NewType}.
  302. \begin{description}
  303. \item[crHidden] the cursor is not visible.
  304. \item[crUnderLine] the cursor is a small underline character (usually
  305. denoting insert mode).
  306. \item[crBlock] the cursor is a block the size of a screen cell (usually
  307. denoting overwrite mode).
  308. \item[crHalfBlock] the cursor is a block half the size of a screen cell.
  309. \end{description}
  310. \Errors
  311. None.
  312. \SeeAlso
  313. \seep{SetCursorPos}
  314. \end{procedure}
  315. \begin{procedure}{SetVideoMode}
  316. \Declaration
  317. procedure SetVideoMode(Mode: TVideoMode);
  318. \Description
  319. \var{SetVideoMode} sets the video mode to the mode specified in \var{Mode}:
  320. \begin{verbatim}
  321. TVideoMode = record
  322. Col,Row : Word;
  323. Color : Boolean;
  324. end;
  325. \end{verbatim}
  326. If the call was succesful, then the screen will have \var{Col} columns and
  327. \var{Row} rows, and will be displaying in color if \var{Color} is
  328. \var{True}.
  329. Note that the video mode may not always be set. E.g. a console on Linux
  330. or a telnet session cannot always set the mode. It is important to check
  331. the error value returned by this function if it was not succesful.
  332. \Errors
  333. If the specified mode cannot be set, then \var{errVioNoSuchMode} may be set
  334. in \var{ErrorCode}
  335. \SeeAlso
  336. \end{procedure}
  337. \begin{procedure}{UpdateScreen}
  338. \Declaration
  339. procedure UpdateScreen(Force: Boolean);
  340. \Description
  341. \var{UpdateScreen} synchronizes the actual screen with the contents
  342. of the \var{VideoBuf} internal buffer. The parameter \var{Force}
  343. specifies whether the whole screen has to be redrawn (\var{Force=True})
  344. or only parts that have changed since the last update of the screen.
  345. The \var{Video} unit keeps an internal copy of the screen as it last
  346. wrote it to the screen. The current contents of \var{VideoBuf} are examined
  347. to see what locations on the screen need to be updated. On slow terminals
  348. (e.g. a \linux telnet session) this mechanism can speed up the screen redraw
  349. considerably.
  350. \Errors
  351. None.
  352. \SeeAlso
  353. \seep{ClearScreen}
  354. \end{procedure}