crt.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. procedure GetScreenCursor (var X, Y: dword);inline;
  108. (* Return current cursor postion - 0-based. *)
  109. var
  110. X0, Y0: word;
  111. begin
  112. X := 0;
  113. Y := 0;
  114. if VioGetCurPos (Y0, X0, VioHandle) = 0 then
  115. begin
  116. X := X0;
  117. Y := Y0;
  118. end;
  119. end;
  120. procedure SetScreenCursor (X, Y: dword); inline;
  121. (* Set current cursor postion - 0-based. *)
  122. begin
  123. VioSetCurPos (Y, X, VioHandle);
  124. end;
  125. procedure RemoveLines (Row: dword; Cnt: dword); inline;
  126. (* Remove Cnt lines from screen starting with (0-based) Row. *)
  127. var
  128. ScrEl: word;
  129. begin
  130. ScrEl := $20 or (TextAttr shl 8);
  131. VioScrollUp (Row + WindMinY, WindMinX, WindMaxY, WindMaxX, Cnt, ScrEl,
  132. VioHandle);
  133. end;
  134. procedure ClearCells (X, Y, Cnt: dword); inline;
  135. (* Clear Cnt cells in line Y (0-based) starting with position X (0-based). *)
  136. var
  137. ScrEl: word;
  138. begin
  139. ScrEl := $20 or (TextAttr shl 8);
  140. VioScrollRight (Y, X, Y, X + Pred (Cnt), Cnt, ScrEl, VioHandle);
  141. end;
  142. procedure InsLine;
  143. (* Inserts a line at cursor position. *)
  144. var
  145. ScrEl: word;
  146. begin
  147. ScrEl := $20 or (TextAttr shl 8);
  148. VioScrollDn (Pred (WhereY32) + WindMinY, WindMinX, WindMaxY, WindMaxX, 1,
  149. ScrEl, VioHandle);
  150. end;
  151. procedure SetScreenMode (Mode: word);
  152. var
  153. NewMode: VioModeInfo;
  154. begin
  155. NewMode.cb := 8;
  156. VioGetMode (NewMode, VioHandle);
  157. NewMode.fbType := 1; {Non graphics colour mode.}
  158. NewMode.Color := 4; {We want 16 colours, 2^4=16 - requests for BW ignored.}
  159. case Mode and $FF of
  160. BW40, CO40: NewMode.Col := 40;
  161. BW80, CO80: NewMode.Col := 80;
  162. else
  163. begin
  164. (* Keep current amount of columns! *)
  165. end;
  166. end;
  167. case Mode and $100 of
  168. 0: NewMode.Row := 25;
  169. $100: NewMode.Row := 50
  170. else
  171. begin
  172. (* Keep current amount of rows! *)
  173. end;
  174. end;
  175. VioSetMode (NewMode, VioHandle);
  176. ScreenWidth := NewMode.Col;
  177. ScreenHeight := NewMode.Row;
  178. end;
  179. procedure Delay (Ms: word);
  180. {Waits ms milliseconds.}
  181. begin
  182. DosSleep (Ms)
  183. end;
  184. procedure WriteNormal (C: char; X, Y: dword); inline;
  185. (* Write C to console at X, Y (0-based). *)
  186. begin
  187. VioWrtCharStrAtt (@C, 1, Y, X, TextAttr, VioHandle);
  188. end;
  189. procedure WriteBell; inline;
  190. (* Write character #7 - beep. *)
  191. begin
  192. DosBeep (800, 250);
  193. end;
  194. {****************************************************************************
  195. Extra Crt Functions
  196. ****************************************************************************}
  197. procedure CursorOn;
  198. var
  199. I: TVioCursorInfo;
  200. begin
  201. VioGetCurType (I, VioHandle);
  202. with I do
  203. begin
  204. yStartInt := -90;
  205. cEndInt := -100;
  206. Attr := 15;
  207. end;
  208. VioSetCurType (I, VioHandle);
  209. end;
  210. procedure CursorOff;
  211. var
  212. I: TVioCursorInfo;
  213. begin
  214. VioGetCurType (I, VioHandle);
  215. I.AttrInt := -1;
  216. VioSetCurType (I, VioHandle);
  217. end;
  218. procedure CursorBig;
  219. var
  220. I: TVioCursorInfo;
  221. begin
  222. VioGetCurType (I, VioHandle);
  223. with I do
  224. begin
  225. yStart := 0;
  226. cEndInt := -100;
  227. Attr := 15;
  228. end;
  229. VioSetCurType (I, VioHandle);
  230. end;
  231. (* Include common, platform independent part. *)
  232. {$I crt.inc}
  233. function KeyPressed: boolean;
  234. {Checks if a key is pressed.}
  235. var
  236. AKeyRec: TKbdKeyinfo;
  237. begin
  238. if SpecialKey or (ScanCode <> 0) then
  239. KeyPressed := true
  240. else
  241. KeyPressed := (KbdPeek (AKeyRec, 0) = 0)
  242. and ((AKeyRec.fbStatus and $40) <> 0);
  243. end;
  244. function ReadKey: char;
  245. {Reads the next character from the keyboard.}
  246. var
  247. AKeyRec: TKbdKeyInfo;
  248. C, S: char;
  249. begin
  250. if SpecialKey then
  251. begin
  252. SpecialKey := false;
  253. ReadKey := char (ScanCode);
  254. ScanCode := 0;
  255. end
  256. else
  257. if ScanCode <> 0 then
  258. begin
  259. ReadKey := char (ScanCode);
  260. ScanCode := 0;
  261. end
  262. else
  263. begin
  264. while ((KbdCharIn (AKeyRec, 1, 0) <> 0)
  265. or (AKeyRec.fbStatus and $41 <> $40)) and (ScanCode = 0) do
  266. DosSleep (5);
  267. if ScanCode = 0 then
  268. begin
  269. C := AKeyRec.CharCode;
  270. S := AKeyRec.ScanCode;
  271. if (C = #224) and (S <> #0) then
  272. C := #0;
  273. if C = #0 then
  274. begin
  275. SpecialKey := true;
  276. ScanCode := byte (S);
  277. end;
  278. ReadKey := C;
  279. end
  280. else
  281. begin
  282. ReadKey := char (ScanCode);
  283. ScanCode := 0;
  284. end;
  285. end;
  286. end;
  287. {Initialization.}
  288. var
  289. CurMode: VioModeInfo;
  290. begin
  291. if not (IsConsole) then
  292. VioCreatePS (VioHandle, 25, 80, 1, 1, 0);
  293. { InitVideo;}
  294. CurMode.cb := SizeOf (CurMode);
  295. VioGetMode (CurMode, VioHandle);
  296. ScreenWidth := CurMode.Col;
  297. ScreenHeight := CurMode.Row;
  298. LastMode := 0;
  299. case ScreenWidth of
  300. 40: LastMode := CO40;
  301. 80: LastMode := CO80
  302. else
  303. LastMode := 255
  304. end;
  305. case ScreenHeight of
  306. 50: LastMode := LastMode + $100
  307. else
  308. LastMode := LastMode + $FF00;
  309. end;
  310. CrtInit;
  311. end.