crt.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. {$i crth.inc}
  12. {cemodeset means that the procedure textmode has failed to set up a mode.}
  13. type
  14. cexxxx=(cenoerror,cemodeset);
  15. var
  16. crt_error:cexxxx; {Crt-status. RW}
  17. implementation
  18. {$i textrec.inc}
  19. const extkeycode:char=#0;
  20. var maxrows,maxcols:word;
  21. type Tkbdkeyinfo=record
  22. charcode,scancode:char;
  23. fbstatus,bnlsshift:byte;
  24. fsstate:word;
  25. time:longint;
  26. end;
  27. {if you have information on the folowing datastructure, please
  28. send them to me at [email protected]}
  29. {This datastructure is needed when we ask in what video mode we are,
  30. or we want to set up a new mode.}
  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. ! more info wanted !}
  42. attrib: byte; { number of attributes }
  43. buf_addr, { physical address of
  44. videobuffer, e.g. $0b800}
  45. buf_length, { length of a videopage (bytes)}
  46. full_length, { total video-memory on video-
  47. card (bytes)}
  48. partial_length:longint; { ????? info wanted !}
  49. ext_data_addr:pointer; { ????? info wanted !}
  50. end;
  51. {EMXWRAP.DLL has strange calling conventions: All parameters must have
  52. a 4 byte size.}
  53. function kbdcharin(var Akeyrec:Tkbdkeyinfo;wait,kbdhandle:longint):word; cdecl;
  54. external 'EMXWRAP' index 204;
  55. function kbdpeek(var Akeyrec:TkbdkeyInfo;kbdhandle:word):word; cdecl;
  56. external 'EMXWRAP' index 222;
  57. function dossleep(time:cardinal):word; cdecl;
  58. external 'DOSCALLS' index 229;
  59. function vioscrollup(top,left,bottom,right,lines:longint;
  60. var screl:word;viohandle:longint):word; cdecl;
  61. external 'EMXWRAP' index 107;
  62. function vioscrolldn(top,left,bottom,right,lines:longint;
  63. var screl:word;viohandle:longint):word; cdecl;
  64. external 'EMXWRAP' index 147;
  65. function viogetcurpos(var row,column:word;viohandle:longint):word; cdecl;
  66. external 'EMXWRAP' index 109;
  67. function viosetcurpos(row,column,viohandle:longint):word; cdecl;
  68. external 'EMXWRAP' index 115;
  69. function viowrtcharstratt(s:Pchar;len,row,col:longint;var attr:byte;
  70. viohandle:longint):word; cdecl;
  71. external 'EMXWRAP' index 148;
  72. function viogetmode(var Amodeinfo:viomodeinfo;viohandle:longint):word; cdecl;
  73. external 'EMXWRAP' index 121;
  74. function viosetmode(var Amodeinfo:viomodeinfo;viohandle:longint):word; cdecl;
  75. external 'EMXWRAP' index 122;
  76. procedure setscreenmode(mode:word);
  77. { This procedure sets a new videomode. Note that the constants passes to
  78. this procedure are different than in the dos mode.}
  79. const modecols:array[0..2] of word=(40,80,132);
  80. moderows:array[0..3] of word=(25,28,43,50);
  81. var newmode:viomodeinfo;
  82. begin
  83. newmode.cb:=8;
  84. newmode.fbtype:=1; {Non graphics colour mode.}
  85. newmode.color:=4; {We want 16 colours, 2^4=16.}
  86. newmode.col:=modecols[mode and 15];
  87. newmode.row:=moderows[mode shr 4];
  88. if viosetmode(newmode,0)=0 then
  89. crt_error:=cenoerror
  90. else
  91. crt_error:=cemodeset;
  92. maxcols:=newmode.col;
  93. maxrows:=newmode.row;
  94. end;
  95. procedure getcursor(var y,x:word);
  96. {Get the cursor position.}
  97. begin
  98. viogetcurpos(y,x,0)
  99. end;
  100. procedure setcursor(y,x:word);
  101. {Set the cursor position.}
  102. begin
  103. viosetcurpos(y,x,0)
  104. end;
  105. procedure scroll_up(top,left,bottom,right,lines:word;var screl:word);
  106. begin
  107. vioscrollup(top,left,bottom,right,lines,screl,0)
  108. end;
  109. procedure scroll_dn(top,left,bottom,right,lines:word;var screl:word);
  110. begin
  111. vioscrolldn(top,left,bottom,right,lines,screl,0)
  112. end;
  113. function keypressed:boolean;
  114. {Checks if a key is pressed.}
  115. var Akeyrec:Tkbdkeyinfo;
  116. begin
  117. kbdpeek(Akeyrec,0);
  118. keypressed:=(extkeycode<>#0) or ((Akeyrec.fbstatus and $40)<>0);
  119. end;
  120. function readkey:char;
  121. {Reads the next character from the keyboard.}
  122. var Akeyrec:Tkbdkeyinfo;
  123. c,s:char;
  124. begin
  125. if extkeycode<>#0 then
  126. begin
  127. readkey:=extkeycode;
  128. extkeycode:=#0
  129. end
  130. else
  131. begin
  132. kbdcharin(Akeyrec,0,0);
  133. c:=Akeyrec.charcode;
  134. s:=Akeyrec.scancode;
  135. if (c=#224) and (s<>#0) then
  136. c:=#0;
  137. if c=#0 then
  138. extkeycode:=s;
  139. readkey:=c;
  140. end;
  141. end;
  142. procedure clrscr;
  143. {Clears the current window.}
  144. var screl:word;
  145. begin
  146. screl:=$20+textattr shl 8;
  147. scroll_up(hi(windmin),lo(windmin),
  148. hi(windmax),lo(windmax),
  149. hi(windmax)-hi(windmin)+1,
  150. screl);
  151. gotoXY(1,1);
  152. end;
  153. procedure gotoXY(x,y:byte);
  154. {Positions the cursor on (x,y) relative to the window origin.}
  155. begin
  156. if x<1 then
  157. x:=1;
  158. if y<1 then
  159. y:=1;
  160. if y+hi(windmin)-2>=hi(windmax) then
  161. y:=hi(windmax)-hi(windmin)+1;
  162. if x+lo(windmin)-2>=lo(windmax) then
  163. x:=lo(windmax)-lo(windmin)+1;
  164. setcursor(y+hi(windmin)-1,x+lo(windmin)-1);
  165. end;
  166. function whereX:byte;
  167. {Returns the x position of the cursor.}
  168. var x,y:word;
  169. begin
  170. getcursor(y,x);
  171. whereX:=x-lo(windmin)+1;
  172. end;
  173. function whereY:byte;
  174. {Returns the y position of the cursor.}
  175. var x,y:word;
  176. begin
  177. getcursor(y,x);
  178. whereY:=y-hi(windmin)+1;
  179. end;
  180. procedure clreol;
  181. {Clear from current position to end of line.
  182. Contributed by Michail A. Baikov}
  183. var i:byte;
  184. begin
  185. {not fastest, but compatible}
  186. for i:=wherex to lo(windmax) do write(' ');
  187. gotoxy(1,wherey); {may be not}
  188. end;
  189. procedure delline;
  190. {Deletes the line at the cursor.}
  191. var row,left,right,bot:longint;
  192. fil:word;
  193. begin
  194. row:=whereY;
  195. left:=lo(windmin)+1;
  196. right:=lo(windmax)+1;
  197. bot:=hi(windmax)+1;
  198. fil:=$20 or (textattr shl 8);
  199. scroll_up(row+1,left,bot,right,1,fil);
  200. end;
  201. procedure insline;
  202. {Inserts a line at the cursor position.}
  203. var row,left,right,bot:longint;
  204. fil:word;
  205. begin
  206. row:=whereY;
  207. left:=lo(windmin)+1;
  208. right:=lo(windmax)+1;
  209. bot:=hi(windmax);
  210. fil:=$20 or (textattr shl 8);
  211. scroll_dn(row,left,bot-1,right,1,fil);
  212. end;
  213. procedure textmode(mode:integer);
  214. { Use this procedure to set-up a specific text-mode.}
  215. begin
  216. textattr:=$07;
  217. lastmode:=mode;
  218. mode:=mode and $ff;
  219. setscreenmode(mode);
  220. windmin:=0;
  221. windmax:=(maxcols-1) or ((maxrows-1) shl 8);
  222. clrscr;
  223. end;
  224. procedure textcolor(color:byte);
  225. {All text written after calling this will have color as foreground colour.}
  226. begin
  227. textattr:=(textattr and $70) or (color and $f)+color and 128;
  228. end;
  229. procedure textbackground(color:byte);
  230. {All text written after calling this will have colour as background colour.}
  231. begin
  232. textattr:=(textattr and $8f) or ((color and $7) shl 4);
  233. end;
  234. procedure normvideo;
  235. {Changes the text-background to black and the foreground to white.}
  236. begin
  237. textattr:=$7;
  238. end;
  239. procedure lowvideo;
  240. {All text written after this will have low intensity.}
  241. begin
  242. textattr:=textattr and $f7;
  243. end;
  244. procedure highvideo;
  245. {All text written after this will have high intensity.}
  246. begin
  247. textattr:=textattr or $8;
  248. end;
  249. procedure delay(ms:word);
  250. {Waits ms microseconds.}
  251. begin
  252. dossleep(ms)
  253. end;
  254. procedure window(X1,Y1,X2,Y2:byte);
  255. {Change the write window to the given coordinates.}
  256. begin
  257. if (X1<1) or
  258. (Y1<1) or
  259. (X2>maxcols) or
  260. (Y2>maxrows) or
  261. (X1>X2) or
  262. (Y1>Y2) then
  263. exit;
  264. windmin:=(X1-1) or ((Y1-1) shl 8);
  265. windmax:=(X2-1) or ((Y2-1) shl 8);
  266. gotoXY(1,1);
  267. end;
  268. procedure writePchar(s:Pchar;len:word);
  269. {Write a series of characters to the screen.
  270. Not very fast, but is just text-mode isn't it?}
  271. var
  272. x,y:word;
  273. i,n:integer;
  274. screl:word;
  275. ca:Pchar;
  276. begin
  277. i:=0;
  278. getcursor(y,x);
  279. while i<=len-1 do
  280. begin
  281. case s[i] of
  282. #8: x:=x-1;
  283. #9: x:=(x-lo(windmin)) and $fff8+8+lo(windmin);
  284. #10: ;
  285. #13: begin
  286. x:=lo(windmin);
  287. inc(y);
  288. end;
  289. else
  290. begin
  291. ca:=@s[i];
  292. n:=1;
  293. while not(s[i+1] in [#8,#9,#10,#13]) and
  294. (x+n<=lo(windmax)) and (i<len-1) do
  295. begin
  296. inc(n);
  297. inc(i);
  298. end;
  299. viowrtcharstratt(ca,n,y,x,textattr,0);
  300. x:=x+n;
  301. end;
  302. end;
  303. if x>lo(windmax) then
  304. begin
  305. x:=lo(windmin);
  306. inc(y);
  307. end;
  308. if y>hi(windmax) then
  309. begin
  310. screl:=$20+textattr shl 8;
  311. scroll_up(hi(windmin),lo(windmin),
  312. hi(windmax),lo(windmax),
  313. 1,screl);
  314. y:=hi(windmax);
  315. end;
  316. inc(i);
  317. end;
  318. setcursor(y,x);
  319. end;
  320. function crtread(var f:textrec):word;
  321. {Read a series of characters from the console.}
  322. var max,curpos:integer;
  323. c:char;
  324. clist:array[0..2] of char;
  325. begin
  326. max:=f.bufsize-2;
  327. curpos:=0;
  328. repeat
  329. c:=readkey;
  330. case c of
  331. #0:
  332. readkey;
  333. #8:
  334. if curpos>0 then
  335. begin
  336. clist:=#8' '#8;
  337. writePchar(@clist,3);
  338. dec(curpos);
  339. end;
  340. #13:
  341. begin
  342. f.bufptr^[curpos]:=#13;
  343. inc(curpos);
  344. f.bufptr^[curpos]:=#10;
  345. inc(curpos);
  346. f.bufpos:=0;
  347. f.bufend:=curpos;
  348. clist[0]:=#13;
  349. writePchar(@clist,1);
  350. break;
  351. end;
  352. #32..#255:
  353. if curpos<max then
  354. begin
  355. f.bufptr^[curpos]:=c;
  356. inc(curpos);
  357. writePchar(@c,1);
  358. end;
  359. end;
  360. until false;
  361. crtread:=0;
  362. end;
  363. function crtwrite(var f:textrec):word;
  364. {Write a series of characters to the console.}
  365. begin
  366. writePchar(Pchar(f.bufptr),f.bufpos);
  367. f.bufpos:=0;
  368. crtwrite:=0;
  369. end;
  370. function crtopen(var f:textrec):integer;
  371. begin
  372. if f.mode=fmoutput then
  373. crtopen:=0
  374. else
  375. crtopen:=5;
  376. end;
  377. function crtinout(var f:textrec):integer;
  378. begin
  379. case f.mode of
  380. fminput:
  381. crtinout:=crtread(f);
  382. fmoutput:
  383. crtinout:=crtwrite(f);
  384. end;
  385. end;
  386. function crtclose(var f:textrec):integer;
  387. begin
  388. f.mode:=fmclosed;
  389. crtclose:=0;
  390. end;
  391. procedure assigncrt(var f:text);
  392. {Assigns a file to the crt console.}
  393. begin
  394. textrec(f).mode:=fmclosed;
  395. textrec(f).bufsize:=128;
  396. textrec(f).bufptr:=@textrec(f).buffer;
  397. textrec(f).bufpos:=0;
  398. textrec(f).openfunc:=@crtopen;
  399. textrec(f).inoutfunc:=@crtinout;
  400. textrec(f).flushfunc:=@crtinout;
  401. textrec(f).closefunc:=@crtclose;
  402. textrec(f).name[0]:='.';
  403. textrec(f).name[0]:=#0;
  404. end;
  405. procedure sound(hz:word);
  406. {sound and nosound are not implemented because the OS/2 API supports a freq/
  407. duration procedure instead of start/stop procedures.}
  408. begin
  409. end;
  410. procedure nosound;
  411. begin
  412. end;
  413. {Extra Functions}
  414. procedure cursoron;
  415. begin
  416. end;
  417. procedure cursoroff;
  418. begin
  419. end;
  420. procedure cursorbig;
  421. begin
  422. end;
  423. {Initialization.}
  424. var
  425. curmode: viomodeinfo;
  426. begin
  427. textattr:=lightgray;
  428. curmode.cb:=sizeof(curmode);
  429. viogetmode(curmode,0);
  430. maxcols:=curmode.col;
  431. maxrows:=curmode.row;
  432. lastmode:=0;
  433. case maxcols of
  434. 40: lastmode:=0;
  435. 80: lastmode:=1;
  436. 132: lastmode:=2;
  437. end;
  438. case maxrows of
  439. 25:;
  440. 28: lastmode:=lastmode+16;
  441. 43: lastmode:=lastmode+32;
  442. 50: lastmode:=lastmode+48;
  443. end;
  444. windmin:=0;
  445. windmax:=((maxrows-1) shl 8) or (maxcols-1);
  446. crt_error:=cenoerror;
  447. assigncrt(input);
  448. textrec(input).mode:=fminput;
  449. assigncrt(output);
  450. textrec(output).mode:=fmoutput;
  451. end.
  452. {
  453. $Log$
  454. Revision 1.6 2004-02-08 16:22:20 michael
  455. + Moved CRT interface to common include file
  456. Revision 1.5 2003/10/18 16:53:21 hajny
  457. * longint2cardinal
  458. Revision 1.4 2003/09/24 12:30:08 yuri
  459. * Removed emx code from crt.pas
  460. - Removed doscalls.imp (not full and obsolete)
  461. Revision 1.3 2002/08/04 19:37:55 hajny
  462. * fix for bug 1998 (write in window) + removed warnings
  463. }