crt.pas 10 KB

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