crt.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. {****************************************************************************
  2. Standard CRT unit.
  3. Free Pascal runtime library for OS/2.
  4. Copyright (c) 1997 Daniel Mantione.
  5. This file may be reproduced and modified under the same conditions
  6. as all other Free Pascal source code.
  7. ****************************************************************************}
  8. unit crt;
  9. interface
  10. {$INLINE ON}
  11. {$i crth.inc}
  12. procedure Window32 (X1, Y1, X2, Y2: dword);
  13. procedure GotoXY32 (X, Y: dword);
  14. function WhereX32: dword;
  15. function WhereY32: dword;
  16. var
  17. ScreenHeight, ScreenWidth: dword;
  18. (* API *)
  19. implementation
  20. {uses keyboard, video;}
  21. {$i textrec.inc}
  22. const
  23. VioHandle: word = 0;
  24. type
  25. TKbdKeyInfo = record
  26. CharCode, ScanCode: char;
  27. fbStatus, bNlsShift: byte;
  28. fsState: word;
  29. Time: longint;
  30. end;
  31. VioModeInfo = record
  32. cb: word; { length of the entire data
  33. structure }
  34. fbType, { bit mask of mode being set}
  35. Color: byte; { number of colors (power of 2) }
  36. Col, { number of text columns }
  37. Row, { number of text rows }
  38. HRes, { horizontal resolution }
  39. VRes: word; { vertical resolution }
  40. fmt_ID, { attribute format }
  41. Attrib: byte; { number of attributes }
  42. Buf_Addr, { physical address of
  43. videobuffer, e.g. $0b800}
  44. Buf_Length, { length of a videopage (bytes)}
  45. Full_Length, { total video-memory on video-
  46. card (bytes)}
  47. Partial_Length: longint; { ????? info wanted !}
  48. Ext_Data_Addr: pointer; { ????? info wanted !}
  49. end;
  50. TVioCursorInfo=record
  51. case boolean of
  52. false: (
  53. yStart: word; {Cursor start (top) scan line (0-based)}
  54. cEnd: word; {Cursor end (bottom) scan line}
  55. cx: word; {Cursor width (0=default width)}
  56. Attr: word); {Cursor colour attribute (-1=hidden)}
  57. true:(
  58. yStartInt: integer; {integer variants can be used to specify negative}
  59. cEndInt: integer; {negative values (interpreted as percentage by OS/2)}
  60. cxInt: integer;
  61. AttrInt: integer);
  62. end;
  63. PVioCursorInfo = ^TVioCursorInfo;
  64. function KbdCharIn (var AKeyRec: TKbdKeyInfo; Wait, KbdHandle: longint):
  65. word; cdecl;
  66. external 'EMXWRAP' index 204;
  67. function KbdPeek (var AKeyRec: TKbdKeyInfo; KbdHandle: longint): word; cdecl;
  68. external 'EMXWRAP' index 222;
  69. function DosSleep (Time: cardinal): word; cdecl;
  70. external 'DOSCALLS' index 229;
  71. function VioScrollUp (Top, Left, Bottom, Right, Lines: longint;
  72. var ScrEl: word; VioHandle: word): word; cdecl;
  73. external 'EMXWRAP' index 107;
  74. {$WARNING ScrEl as word not DBCS safe!}
  75. function VioScrollDn (Top, Left, Bottom, Right, Lines: longint;
  76. var ScrEl: word; VioHandle: word): word; cdecl;
  77. external 'EMXWRAP' index 147;
  78. function VioScrollRight (Top, Left, Bottom, Right, Columns: word;
  79. var ScrEl: word; VioHandle: word): word; cdecl;
  80. external 'EMXWRAP' index 112;
  81. {external 'VIOCALLS' index 12;}
  82. function VioGetCurPos (var Row, Column: word; VioHandle: word): word; cdecl;
  83. external 'EMXWRAP' index 109;
  84. function VioSetCurPos (Row, Column, VioHandle: word): word; cdecl;
  85. external 'EMXWRAP' index 115;
  86. function VioWrtCharStrAtt (S: PChar; Len, Row, Col: longint; var Attr: byte;
  87. VioHandle: word): word; cdecl;
  88. external 'EMXWRAP' index 148;
  89. function VioGetMode (var AModeInfo: VioModeInfo; VioHandle: word): word; cdecl;
  90. external 'EMXWRAP' index 121;
  91. function VioSetMode (var AModeInfo: VioModeInfo; VioHandle: word): word; cdecl;
  92. external 'EMXWRAP' index 122;
  93. function VioSetCurType (var CurData: TVioCursorInfo; VioHandle: word): word;
  94. cdecl;
  95. external 'EMXWRAP' index 132;
  96. {external 'VIOCALLS' index 32;}
  97. function VioGetCurType (var CurData: TVioCursorInfo; VioHandle: word): word;
  98. cdecl;
  99. external 'EMXWRAP' index 127;
  100. {external 'VIOCALLS' index 27;}
  101. function VioCreatePS (var VPS: word; Depth, Width, Format, Attrs: integer;
  102. Reserved: word): word; cdecl;
  103. external 'EMXWRAP' index 156;
  104. {external 'VIOCALLS' index 56;}
  105. function DosBeep (Freq, MS: cardinal): cardinal; cdecl;
  106. external 'DOSCALLS' index 286;
  107. threadvar
  108. ExtKeyCode: char;
  109. function KeyPressed: boolean;
  110. {Checks if a key is pressed.}
  111. var
  112. AKeyRec: TKbdKeyinfo;
  113. begin
  114. if ExtKeyCode <> #0 then
  115. KeyPressed := true
  116. else
  117. KeyPressed := (KbdPeek (AKeyRec, 0) = 0)
  118. and ((AKeyRec.fbStatus and $40) <> 0);
  119. end;
  120. function ReadKey: char;
  121. {Reads the next character from the keyboard.}
  122. var
  123. AKeyRec: TKbdKeyInfo;
  124. C, S: char;
  125. begin
  126. if ExtKeyCode <> #0 then
  127. begin
  128. ReadKey := ExtKeyCode;
  129. ExtKeyCode := #0
  130. end
  131. else
  132. begin
  133. KbdCharIn (AKeyRec, 0, 0);
  134. C := AKeyRec.CharCode;
  135. S := AKeyRec.ScanCode;
  136. if (C = #224) and (S <> #0) then
  137. C := #0;
  138. if C = #0 then
  139. ExtKeyCode := S;
  140. ReadKey := C;
  141. end;
  142. end;
  143. procedure GetScreenCursor (var X, Y: dword);inline;
  144. (* Return current cursor postion - 0-based. *)
  145. var
  146. X0, Y0: word;
  147. begin
  148. X := 0;
  149. Y := 0;
  150. if VioGetCurPos (Y0, X0, VioHandle) = 0 then
  151. begin
  152. X := X0;
  153. Y := Y0;
  154. end;
  155. end;
  156. procedure SetScreenCursor (X, Y: dword); inline;
  157. (* Set current cursor postion - 0-based. *)
  158. begin
  159. VioSetCurPos (Y, X, VioHandle);
  160. end;
  161. procedure RemoveLines (Row: dword; Cnt: dword); inline;
  162. (* Remove Cnt lines from screen starting with (0-based) Row. *)
  163. var
  164. ScrEl: word;
  165. begin
  166. ScrEl := $20 or (TextAttr shl 8);
  167. VioScrollUp (Row + WindMinY, WindMinX, WindMaxY, WindMaxX, Cnt, ScrEl,
  168. VioHandle);
  169. end;
  170. procedure ClearCells (X, Y, Cnt: dword); inline;
  171. (* Clear Cnt cells in line Y (0-based) starting with position X (0-based). *)
  172. var
  173. ScrEl: word;
  174. begin
  175. ScrEl := $20 or (TextAttr shl 8);
  176. VioScrollRight (Y, X, Y, X + Pred (Cnt), Cnt, ScrEl, VioHandle);
  177. end;
  178. procedure InsLine;
  179. (* Inserts a line at cursor position. *)
  180. var
  181. ScrEl: word;
  182. begin
  183. ScrEl := $20 or (TextAttr shl 8);
  184. VioScrollDn (Pred (WhereY32) + WindMinY, WindMinX, WindMaxY, WindMaxX, 1,
  185. ScrEl, VioHandle);
  186. end;
  187. procedure SetScreenMode (Mode: word);
  188. var
  189. NewMode: VioModeInfo;
  190. begin
  191. NewMode.cb := 8;
  192. VioGetMode (NewMode, VioHandle);
  193. NewMode.fbType := 1; {Non graphics colour mode.}
  194. NewMode.Color := 4; {We want 16 colours, 2^4=16 - requests for BW ignored.}
  195. case Mode and $FF of
  196. BW40, CO40: NewMode.Col := 40;
  197. BW80, CO80: NewMode.Col := 80;
  198. else
  199. begin
  200. (* Keep current amount of columns! *)
  201. end;
  202. end;
  203. case Mode and $100 of
  204. 0: NewMode.Row := 25;
  205. $100: NewMode.Row := 50
  206. else
  207. begin
  208. (* Keep current amount of rows! *)
  209. end;
  210. end;
  211. VioSetMode (NewMode, VioHandle);
  212. ScreenWidth := NewMode.Col;
  213. ScreenHeight := NewMode.Row;
  214. end;
  215. procedure Delay (Ms: word);
  216. {Waits ms milliseconds.}
  217. begin
  218. DosSleep (Ms)
  219. end;
  220. procedure WriteNormal (C: char; X, Y: dword); inline;
  221. (* Write C to console at X, Y (0-based). *)
  222. begin
  223. VioWrtCharStrAtt (@C, 1, Y, X, TextAttr, VioHandle);
  224. end;
  225. procedure WriteBell; inline;
  226. (* Write character #7 - beep. *)
  227. begin
  228. DosBeep (800, 250);
  229. end;
  230. {****************************************************************************
  231. Extra Crt Functions
  232. ****************************************************************************}
  233. procedure CursorOn;
  234. var
  235. I: TVioCursorInfo;
  236. begin
  237. VioGetCurType (I, VioHandle);
  238. with I do
  239. begin
  240. yStartInt := -90;
  241. cEndInt := -100;
  242. Attr := 15;
  243. end;
  244. VioSetCurType (I, VioHandle);
  245. end;
  246. procedure CursorOff;
  247. var
  248. I: TVioCursorInfo;
  249. begin
  250. VioGetCurType (I, VioHandle);
  251. I.AttrInt := -1;
  252. VioSetCurType (I, VioHandle);
  253. end;
  254. procedure CursorBig;
  255. var
  256. I: TVioCursorInfo;
  257. begin
  258. VioGetCurType (I, VioHandle);
  259. with I do
  260. begin
  261. yStart := 0;
  262. cEndInt := -100;
  263. Attr := 15;
  264. end;
  265. VioSetCurType (I, VioHandle);
  266. end;
  267. (* Include common, platform independent part. *)
  268. {$I crt.inc}
  269. {Initialization.}
  270. var
  271. CurMode: VioModeInfo;
  272. begin
  273. if not (IsConsole) then
  274. VioCreatePS (VioHandle, 25, 80, 1, 1, 0);
  275. { InitVideo;}
  276. CurMode.cb := SizeOf (CurMode);
  277. VioGetMode (CurMode, VioHandle);
  278. ScreenWidth := CurMode.Col;
  279. ScreenHeight := CurMode.Row;
  280. LastMode := 0;
  281. case ScreenWidth of
  282. 40: LastMode := CO40;
  283. 80: LastMode := CO80
  284. else
  285. LastMode := 255
  286. end;
  287. case ScreenHeight of
  288. 50: LastMode := LastMode + $100
  289. else
  290. LastMode := LastMode + $FF00;
  291. end;
  292. CrtInit;
  293. end.