2
0

ncrt.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. {---------------------------------------------------------------------------
  2. CncWare
  3. (c) Copyright 1999-2000
  4. Portions copyright the FreePascal Team
  5. ---------------------------------------------------------------------------
  6. Filename..: ncrt.inc
  7. Programmer: Ken J. Wright, [email protected]
  8. Date......: 03/01/99
  9. Purpose - Code that is common to nCrt and oCrt.
  10. -------------------------------<< REVISIONS >>--------------------------------
  11. Ver | Date | Prog| Description
  12. -------+----------+-----+-----------------------------------------------------
  13. 2.00 | 12/13/99 | kjw | Initial Release.
  14. 2.02 | 12/15/99 | kjw | Removed use of endwin. Replaced with tcget/setattr.
  15. 2.03 | 12/16/99 | kjw | 1) Added shifted f-keys to nReadkey.
  16. | 2) Added raw & scrollok to StartCurses.
  17. | 3) Added alt'd keyset support.
  18. 2.04 | 01/04/00 | kjw | keypressed changed back to method of using getch
  19. | rather than select.
  20. 2.05 | 01/06/00 | kjw | 1) StartCurses now defaults to echo. Readkey sets to
  21. | noecho. This allows nCrt to handle echoing in the
  22. | default manor, but allows oCrt to control echoing
  23. | in the app with nEcho. Note: Read(ln) will always
  24. | echo as normal, regardless of any setting by nEcho.
  25. | Also set DoRefresh to true.
  26. | 2) nDelWindow now checks for stdscr or curscr and
  27. | makes sure that ActiveWn is not nil.
  28. | 3) Window() now moves to 1,1 and does not do a
  29. | clrscr.
  30. 2.06 | 01/11/00 | kjw | 1) Oops! 2.04 change went back to stdscr vs. ActiveWn.
  31. | Keypressed works correctly with windows again.
  32. | 2) ClrEol works correctly now with color.
  33. 2.07 | 01/31/00 | kjw | 1) Added NCRT_VERSION constants.
  34. | 2) Added prev_textattr to detect a change in
  35. | TextAttr value so current color gets updated.
  36. | 3) See ocrt.pp
  37. 2.08 | 06/09/00 | kjw | See ocrt.pp
  38. 2.08.01 | 06/11/2000 | kjw | See ocrt.pp
  39. 2.09.00 | 06/16/2000 | kjw | See ocrt.pp
  40. 2.10.00 | 06/16/2000 | kjw | See ocrt.pp
  41. 2.11.00 | 06/27/2000 | kjw
  42. | 1) See ocrt.pp
  43. | 2) Now uses ncurses for CrtRead so console control characters
  44. | work correctly (i.e., <ctrl/h>, <backspace>, etc.).
  45. 2.12.00 | 06/29/2000 | kjw | See ocrt.pp
  46. 2.13.00 | 06/30/2000 | kjw
  47. | Added nStop and nStart procedures.
  48. 2.14.00 | 07/05/2000 | kjw
  49. | 1) Added nCursor and nEscDelay functions.
  50. | 2) Added nInit and moved code from ncrt.pp & ocrt.pp to it.
  51. | 3) KEY_ALTMINUS & KEYALTEQUAL were reversed, but mapping ended
  52. | up correct.
  53. ------------------------------------------------------------------------------
  54. }
  55. Procedure AssignCrt(var F: Text);
  56. Procedure ClrEol;
  57. Procedure ClrScr;
  58. Procedure ClrBot;
  59. Procedure Delay(DTime: Word);
  60. Procedure DelLine;
  61. Procedure GotoXY(x,y : integer);
  62. Procedure HighVideo;
  63. Procedure InsLine;
  64. Function Keypressed : boolean;
  65. Procedure LowVideo;
  66. Procedure NormVideo;
  67. Procedure NoSound;
  68. Function Readkey : char;
  69. Procedure Sound(hz : word);
  70. Procedure TextBackground(att : byte);
  71. Procedure TextColor(att : byte);
  72. Procedure TextMode(mode : word);
  73. Function WhereX : integer;
  74. Function WhereY : integer;
  75. Procedure Window(x,y,x1,y1 : integer);
  76. Procedure nStop;
  77. Procedure nStart;
  78. Function nCursor(c : integer) : integer;
  79. Function nEscDelay(d : longint) : longint;
  80. Const
  81. NCRT_VERSION_MAJOR = 2;
  82. NCRT_VERSION_MINOR = 14;
  83. NCRT_VERSION_PATCH = 0;
  84. NCRT_VERSION = '2.14.00';
  85. { CRT modes }
  86. BW40 = 0; { 40x25 B/W on Color Adapter }
  87. CO40 = 1; { 40x25 Color on Color Adapter }
  88. BW80 = 2; { 80x25 B/W on Color Adapter }
  89. CO80 = 3; { 80x25 Color on Color Adapter }
  90. Mono = 7; { 80x25 on Monochrome Adapter }
  91. Font8x8 = 256; { Add-in for ROM font }
  92. { Mode constants for 3.0 compatibility }
  93. C40 = CO40;
  94. C80 = CO80;
  95. Black = 0;
  96. Blue = 1;
  97. Green = 2;
  98. Cyan = 3;
  99. Red = 4;
  100. Magenta = 5;
  101. Brown = 6;
  102. LightGray = 7;
  103. DarkGray = 8;
  104. LightBlue = 9;
  105. LightGreen = 10;
  106. LightCyan = 11;
  107. LightRed = 12;
  108. LightMagenta = 13;
  109. Yellow = 14;
  110. White = 15;
  111. Blink = 128;
  112. TextAttr : Byte = $07;
  113. LastMode : Word = 3;
  114. WindMin : Word = $0;
  115. WindMax : Word = $184f;
  116. { support for the alt'd characters }
  117. { these get initialized by StartCurses }
  118. KEY_ALTA = 465; { alt/a }
  119. KEY_ALTB = 466;
  120. KEY_ALTC = 467;
  121. KEY_ALTD = 468;
  122. KEY_ALTE = 469;
  123. KEY_ALTF = 470;
  124. KEY_ALTG = 471;
  125. KEY_ALTH = 472;
  126. KEY_ALTI = 473;
  127. KEY_ALTJ = 474;
  128. KEY_ALTK = 475;
  129. KEY_ALTL = 476;
  130. KEY_ALTM = 477;
  131. KEY_ALTN = 478;
  132. KEY_ALTO = 479;
  133. KEY_ALTP = 480;
  134. KEY_ALTQ = 481;
  135. KEY_ALTR = 482;
  136. KEY_ALTS = 483;
  137. KEY_ALTT = 484;
  138. KEY_ALTU = 485;
  139. KEY_ALTV = 486;
  140. KEY_ALTW = 487;
  141. KEY_ALTX = 488;
  142. KEY_ALTY = 489;
  143. KEY_ALTZ = 490; { alt/z }
  144. KEY_ALT1 = 491; { alt/1 }
  145. KEY_ALT2 = 492; { alt/2 }
  146. KEY_ALT3 = 493; { alt/3 }
  147. KEY_ALT4 = 494; { alt/4 }
  148. KEY_ALT5 = 495; { alt/5 }
  149. KEY_ALT6 = 496; { alt/6 }
  150. KEY_ALT7 = 497; { alt/7 }
  151. KEY_ALT8 = 498; { alt/8 }
  152. KEY_ALT9 = 499; { alt/9 }
  153. KEY_ALT0 = 500; { alt/0 }
  154. KEY_ALTMINUS = 501; { alt/- }
  155. KEY_ALTEQUAL = 502; { alt/= }
  156. KEY_ALTTAB = 503; { alt/tab }
  157. { cursor type }
  158. cOFF = 0; { invisible cursor }
  159. cON = 1; { normal cursor }
  160. cBIG = 2; { very visible cursor }
  161. var
  162. CheckBreak,
  163. CheckEOF,
  164. CheckSnow,
  165. DirectVideo: Boolean;
  166. Implementation
  167. uses strings;
  168. Const
  169. { standard file descriptors }
  170. STDIN = 0;
  171. STDOUT = 1;
  172. STDERR = 2;
  173. Var
  174. ExitSave : pointer; { pointer to original exit proc }
  175. fg,bg : integer; { foreground & background }
  176. cp : array [0..7,0..7] of integer; { color pair array }
  177. ps : array [0..255] of char; { for use with pchars }
  178. doRefresh : boolean; { immediate refresh toggle }
  179. SubWn, { window created from window() }
  180. ActiveWn : pwindow; { current active window for stdout }
  181. tmp_b : boolean;
  182. isEcho : boolean; { keeps track of echo status }
  183. MaxRows, { set at startup to terminal values }
  184. MaxCols : longint; { for columns and rows }
  185. tios : TermIOS; { saves the term settings at startup }
  186. prev_textattr : integer; { detect change in TextAttr }
  187. {==========================================================================
  188. This code chunk is from the FPC source tree in rtl/inc/textrec.inc.
  189. It is the internal format of a variable of type "Text" as defined and
  190. described in the Borland Pascal docs.
  191. ==========================================================================}
  192. const
  193. TextRecNameLength = 256;
  194. TextRecBufSize = 256;
  195. type
  196. TextBuf = array[0..TextRecBufSize-1] of char;
  197. TextRec = Packed Record
  198. Handle,
  199. Mode,
  200. bufsize,
  201. _private,
  202. bufpos,
  203. bufend : longint;
  204. bufptr : ^textbuf;
  205. openfunc,
  206. inoutfunc,
  207. flushfunc,
  208. closefunc : pointer;
  209. UserData : array[1..16] of byte;
  210. name : array[0..textrecnamelength-1] of char;
  211. buffer : textbuf;
  212. End;
  213. {==========================================================================}
  214. {--------------------------------------------
  215. initialize ncurses screen & keyboard, and
  216. return a pointer to stdscr.
  217. NOTE: This is done at unit initialization.
  218. --------------------------------------------}
  219. Function StartCurses(var win : pWindow) : Boolean;
  220. Var
  221. i : integer;
  222. s : string[3];
  223. Begin
  224. { save the current terminal settings }
  225. tcGetAttr(STDIN,tios);
  226. if initscr=Nil then Begin
  227. StartCurses := false;
  228. Exit;
  229. End Else Begin
  230. StartCurses := true;
  231. start_color;
  232. cbreak; { disable keyboard buffering }
  233. raw; { disable flow control, etc. }
  234. echo; { echo keypresses }
  235. nonl; { don't process cr in newline }
  236. intrflush(stdscr,bool(false));
  237. keypad(stdscr,bool(true));
  238. scrollok(stdscr,bool(true));
  239. win := stdscr;
  240. isEcho := true;
  241. doRefresh := true;
  242. getmaxyx(stdscr,MaxRows,MaxCols);
  243. { define the the alt'd keysets for ncurses }
  244. { alt/a .. atl/z }
  245. for i := ord('a') to ord('z') do Begin
  246. s := #27+chr(i)+#0;
  247. define_key(@s[1],(KEY_ALTA-97)+i);
  248. End;
  249. { alt/1 .. alt/9 }
  250. for i := 1 to 9 do Begin
  251. s := #27+chr(i)+#0;
  252. define_key(@s[1],(KEY_ALT1-1)+i);
  253. End;
  254. s := #27+'0'+#0; define_key(@s[1],KEY_ALT0); { alt/0 }
  255. s := #27+'-'+#0; define_key(@s[1],KEY_ALTMINUS); { alt/- }
  256. s := #27+'='+#0; define_key(@s[1],KEY_ALTEQUAL); { alt/= }
  257. s := #27+#9+#0; define_key(@s[1],KEY_ALTTAB); { alt/tab }
  258. End;
  259. End;
  260. {----------------------------------
  261. Shutdown ncurses.
  262. NOTE: This is done via ExitProc.
  263. ----------------------------------}
  264. Procedure EndCurses;
  265. Begin
  266. { restore the original terminal settings }
  267. { and leave the screen how the app left it }
  268. tcSetAttr(STDIN,TCSANOW,tios);
  269. End;
  270. {--------------------------------------------------------
  271. This disables any curses activity until a refresh.
  272. Use this BEFORE any shelling (shell,exec,execv,etc)
  273. to put the terminal temporarily back into cooked mode.
  274. --------------------------------------------------------}
  275. Procedure nStop;
  276. Begin
  277. endwin;
  278. End;
  279. {---------------------------------------------
  280. Simply a refresh to re-establish the curses
  281. terminal settings following an nStop.
  282. ---------------------------------------------}
  283. Procedure nStart;
  284. Begin
  285. refresh;
  286. End;
  287. { see if the specified attribute is high intensity }
  288. Function IsBold(att : integer) : boolean;
  289. Begin
  290. bg := att div 16;
  291. fg := att - (bg * 16);
  292. isbold := (fg > 7);
  293. End;
  294. { initialize a color pair }
  295. Function SetColorPair(att : integer) : integer;
  296. var
  297. i : integer;
  298. { ncurses constants
  299. COLOR_BLACK = 0;
  300. COLOR_RED = 1;
  301. COLOR_GREEN = 2;
  302. COLOR_YELLOW = 3;
  303. COLOR_BLUE = 4;
  304. COLOR_MAGENTA = 5;
  305. COLOR_CYAN = 6;
  306. COLOR_WHITE = 7;
  307. }
  308. Begin
  309. bg := att div 16;
  310. fg := att - ((att div 16) * 16);
  311. While bg > 7 Do dec(bg,8);
  312. While fg > 7 Do dec(fg,8);
  313. { map to ncurses color values }
  314. case bg of
  315. 0 : bg := COLOR_BLACK;
  316. 1 : bg := COLOR_BLUE;
  317. 2 : bg := COLOR_GREEN;
  318. 3 : bg := COLOR_CYAN;
  319. 4 : bg := COLOR_RED;
  320. 5 : bg := COLOR_MAGENTA;
  321. 6 : bg := COLOR_YELLOW;
  322. 7 : bg := COLOR_WHITE;
  323. end;
  324. case fg of
  325. 0 : fg := COLOR_BLACK;
  326. 1 : fg := COLOR_BLUE;
  327. 2 : fg := COLOR_GREEN;
  328. 3 : fg := COLOR_CYAN;
  329. 4 : fg := COLOR_RED;
  330. 5 : fg := COLOR_MAGENTA;
  331. 6 : fg := COLOR_YELLOW;
  332. 7 : fg := COLOR_WHITE;
  333. end;
  334. i := cp[bg,fg];
  335. init_pair(i,fg,bg);
  336. SetColorPair := i;
  337. End;
  338. { map a standard color attribute to an ncurses attribute }
  339. Function CursesAtts(att : byte) : longint;
  340. Var
  341. atts : longint;
  342. Begin
  343. atts := COLOR_PAIR(SetColorPair(att));
  344. If IsBold(att) Then atts := atts or A_BOLD;
  345. If (att and $80) = $80 Then atts := atts or A_BLINK;
  346. CursesAtts := atts;
  347. End;
  348. {------------------------------------------------
  349. Delete a window.
  350. NOTE: This does not clear it from the display.
  351. ------------------------------------------------}
  352. Procedure nDelWindow(var win : pWindow);
  353. Begin
  354. If (win = stdscr) or (win = curscr) Then Exit;
  355. If win <> Nil Then delwin(win);
  356. win := Nil;
  357. If ActiveWn = Nil Then ActiveWn := stdscr;
  358. End;
  359. {-----------------------------------------
  360. Set the current text color of a window,
  361. delayed until next refresh.
  362. -----------------------------------------}
  363. Procedure nWinColor(win : pWindow; att : integer);
  364. Begin
  365. wattr_set(win,CursesAtts(att));
  366. prev_textattr := att;
  367. End;
  368. { clear the specified window }
  369. procedure nClrScr(win : pWindow; att : integer);
  370. Begin
  371. wbkgd(win,CursesAtts(att));
  372. TouchWin(win);
  373. werase(win);
  374. If doRefresh Then wrefresh(win);
  375. End;
  376. { clear from the cursor to the end of line in a window }
  377. Procedure nClrEol(win : pWindow);
  378. Var
  379. tmp : pwindow;
  380. x,y,
  381. xb,yb,
  382. xm,ym : longint;
  383. Begin
  384. {--------------------------------------------------------
  385. In order to have the correct color, we must define and
  386. clear a temporary window. ncurses wclrtoeol() uses the
  387. window background color rather that the current color
  388. attribute ;-(
  389. --------------------------------------------------------}
  390. getyx(win,y,x);
  391. getbegyx(win,yb,xb);
  392. getmaxyx(win,ym,xm);
  393. tmp := subwin(win,1,xm-x,yb+y,xb+x);
  394. If tmp = nil then Exit;
  395. wbkgd(tmp,CursesAtts(TextAttr));
  396. werase(tmp);
  397. { wclrtoeol(win);}
  398. If doRefresh Then wrefresh(tmp);
  399. delwin(tmp);
  400. End;
  401. { clear from the cursor to the bottom in a window }
  402. Procedure nClrBot(win : pWindow);
  403. Begin
  404. wclrtobot(win);
  405. If doRefresh Then wrefresh(win);
  406. End;
  407. { insert a line at the cursor line in a window }
  408. Procedure nInsLine(win : pWindow);
  409. Begin
  410. winsertln(win);
  411. If doRefresh Then wrefresh(win);
  412. End;
  413. { delete line at the cursor in a window }
  414. Procedure nDelLine(win : pWindow);
  415. Begin
  416. wdeleteln(win);
  417. If doRefresh Then wrefresh(win);
  418. End;
  419. { position cursor in a window }
  420. Procedure nGotoXY(win : pWindow; x,y : integer);
  421. Begin
  422. wmove(win,y-1,x-1);
  423. touchwin(win);
  424. If doRefresh Then wrefresh(win);
  425. End;
  426. { find cursor x position in a window }
  427. Function nWhereX(win : pWindow) : integer;
  428. var x,y : longint;
  429. Begin
  430. getyx(win,y,x);
  431. nWhereX := x+1;
  432. End;
  433. { find cursor y position in a window }
  434. Function nWhereY(win : pWindow) : integer;
  435. var x,y : longint;
  436. Begin
  437. getyx(win,y,x);
  438. nWhereY := y+1;
  439. End;
  440. {---------------------------------------------------------------------
  441. read a keystroke from a window, including function keys and extended
  442. keys (arrows, etc.)
  443. Note: Make sure that keypad(win,true) has been issued prior to use.
  444. ( nWindow does this )
  445. ---------------------------------------------------------------------}
  446. Function nReadkey(win : pWindow) : char;
  447. var
  448. c : char;
  449. l : longint;
  450. xtnded : boolean;
  451. Begin
  452. l := wgetch(win);
  453. { if it's an extended key, then map to the IBM values }
  454. if l > 255 then begin
  455. xtnded := true;
  456. c := #27;
  457. Case l of
  458. KEY_BREAK : Begin xtnded := false; c := #3; End;
  459. KEY_BACKSPACE : Begin xtnded := false; c := #8; End;
  460. KEY_IC : c := #82; { insert }
  461. KEY_DC : c := #83; { delete }
  462. KEY_HOME : c := #71; { home }
  463. KEY_END : c := #79; { end }
  464. KEY_UP : c := #72; { up arrow }
  465. KEY_DOWN : c := #80; { down arrow }
  466. KEY_LEFT : c := #75; { left arrow }
  467. KEY_RIGHT : c := #77; { right arrow }
  468. KEY_NPAGE : c := #81; { page down }
  469. KEY_PPAGE : c := #73; { page up }
  470. KEY_ALTA : c := #30; { alt/a }
  471. KEY_ALTB : c := #48;
  472. KEY_ALTC : c := #46;
  473. KEY_ALTD : c := #32;
  474. KEY_ALTE : c := #18;
  475. KEY_ALTF : c := #33;
  476. KEY_ALTG : c := #34;
  477. KEY_ALTH : c := #35;
  478. KEY_ALTI : c := #23;
  479. KEY_ALTJ : c := #36;
  480. KEY_ALTK : c := #37;
  481. KEY_ALTL : c := #38;
  482. KEY_ALTM : c := #50;
  483. KEY_ALTN : c := #49;
  484. KEY_ALTO : c := #24;
  485. KEY_ALTP : c := #25;
  486. KEY_ALTQ : c := #16;
  487. KEY_ALTR : c := #19;
  488. KEY_ALTS : c := #31;
  489. KEY_ALTT : c := #20;
  490. KEY_ALTU : c := #22;
  491. KEY_ALTV : c := #47;
  492. KEY_ALTW : c := #17;
  493. KEY_ALTX : c := #45;
  494. KEY_ALTY : c := #21;
  495. KEY_ALTZ : c := #44; { alt/z }
  496. KEY_ALT1 : c := #120; { alt/1 }
  497. KEY_ALT2 : c := #121; { alt/2 }
  498. KEY_ALT3 : c := #122; { alt/3 }
  499. KEY_ALT4 : c := #123; { alt/4 }
  500. KEY_ALT5 : c := #124; { alt/5 }
  501. KEY_ALT6 : c := #125; { alt/6 }
  502. KEY_ALT7 : c := #126; { alt/7 }
  503. KEY_ALT8 : c := #127; { alt/8 }
  504. KEY_ALT9 : c := #128; { alt/9 }
  505. KEY_ALT0 : c := #129; { alt/0 }
  506. KEY_ALTMINUS : c := #130; { alt/- }
  507. KEY_ALTEQUAL : c := #131; { alt/= }
  508. KEY_ALTTAB : c := #15; { alt/tab }
  509. Else
  510. Begin
  511. If l = Key_f(1) Then c := #59 Else
  512. If l = Key_f(2) Then c := #60 Else
  513. If l = Key_f(3) Then c := #61 Else
  514. If l = Key_f(4) Then c := #62 Else
  515. If l = Key_f(5) Then c := #63 Else
  516. If l = Key_f(6) Then c := #64 Else
  517. If l = Key_f(7) Then c := #65 Else
  518. If l = Key_f(8) Then c := #66 Else
  519. If l = Key_f(9) Then c := #67 Else
  520. If l = Key_f(10) Then c := #68 Else
  521. If l = Key_f(11) Then c := #84 Else
  522. If l = Key_f(12) Then c := #85 Else
  523. If l = Key_f(13) Then c := #86 Else
  524. If l = Key_f(14) Then c := #87 Else
  525. If l = Key_f(15) Then c := #88 Else
  526. If l = Key_f(16) Then c := #89 Else
  527. If l = Key_f(17) Then c := #90 Else
  528. If l = Key_f(18) Then c := #91 Else
  529. If l = Key_f(19) Then c := #92 Else
  530. If l = Key_f(20) Then c := #93;
  531. End;
  532. End;
  533. If xtnded Then Begin
  534. nReadKey := #0;
  535. ungetch(ord(c));
  536. Exit;
  537. End Else
  538. nReadkey := c;
  539. End Else
  540. nReadkey := chr(ord(l));
  541. End;
  542. { write a string to a window at the current cursor position }
  543. Procedure nWrite(win : pWindow; s : string);
  544. Begin
  545. If TextAttr <> prev_textattr Then
  546. nWinColor(win,TextAttr);
  547. waddstr(win,StrPCopy(ps,s));
  548. If doRefresh Then wrefresh(win);
  549. End;
  550. {=========================================================================
  551. CrtWrite, CrtRead, CrtReturn, CrtClose, CrtOpen, AssignCrt.
  552. These functions come from the FPC distribution rtl/linux/crt unit.
  553. These are the hooks into the input/output stream needed for write(ln)
  554. and read(ln).
  555. =========================================================================}
  556. { used by CrtWrite }
  557. Procedure DoWrite(temp : string);
  558. Begin
  559. nWrite(ActiveWn,temp);
  560. End;
  561. Function CrtWrite(Var F: TextRec): Integer;
  562. {
  563. Top level write function for CRT
  564. }
  565. Var
  566. Temp : String;
  567. idx,i : Longint;
  568. { oldflush : boolean;}
  569. Begin
  570. { oldflush:=ttySetFlush(Flushing);}
  571. idx:=0;
  572. while (F.BufPos>0) do
  573. begin
  574. i:=F.BufPos;
  575. if i>255 then
  576. i:=255;
  577. system.Move(F.BufPTR^[idx],Temp[1],F.BufPos);
  578. Temp[0]:=Chr(i);
  579. DoWrite(Temp);
  580. dec(F.BufPos,i);
  581. inc(idx,i);
  582. end;
  583. { ttySetFlush(oldFLush);}
  584. CrtWrite:=0;
  585. End;
  586. Function CrtRead(Var F: TextRec): Integer;
  587. {
  588. Read from CRT associated file.
  589. }
  590. Begin
  591. { let's use ncurses instead! }
  592. FillChar(F.BufPtr^, F.BufSize, #0);
  593. wgetnstr(ActiveWn,F.BufPtr^, F.BufSize-1);
  594. F.BufEnd := Length(StrPas(F.BufPtr^))+1;
  595. F.BufPtr^[F.BufEnd-1] := #10;
  596. F.BufPos:=0;
  597. { CrtWrite(F);}
  598. CrtRead:=0;
  599. End;
  600. Function CrtReturn(Var F:TextRec):Integer;
  601. Begin
  602. F.BufEnd := 0;
  603. F.BufPos:= 0;
  604. CrtReturn:=0;
  605. end;
  606. Function CrtClose(Var F: TextRec): Integer;
  607. {
  608. Close CRT associated file.
  609. }
  610. Begin
  611. F.Mode:=fmClosed;
  612. CrtClose:=0;
  613. End;
  614. Function CrtOpen(Var F: TextRec): Integer;
  615. {
  616. Open CRT associated file.
  617. }
  618. Begin
  619. If F.Mode=fmOutput Then
  620. begin
  621. TextRec(F).InOutFunc:=@CrtWrite;
  622. TextRec(F).FlushFunc:=@CrtWrite;
  623. end
  624. Else
  625. begin
  626. F.Mode:=fmInput;
  627. TextRec(F).InOutFunc:=@CrtRead;
  628. TextRec(F).FlushFunc:=@CrtReturn;
  629. end;
  630. TextRec(F).CloseFunc:=@CrtClose;
  631. CrtOpen:=0;
  632. End;
  633. procedure AssignCrt(var F: Text);
  634. {
  635. Assign a file to the console. All output on file goes to console instead.
  636. }
  637. begin
  638. Assign(F,'');
  639. TextRec(F).OpenFunc:=@CrtOpen;
  640. end;
  641. {==========================================================================
  642. Standard crt unit replacements
  643. ==========================================================================}
  644. { set the text background color }
  645. Procedure TextBackground(att : byte);
  646. Begin
  647. TextAttr:=
  648. ((att shl 4) and ($f0 and not Blink)) or (TextAttr and ($0f OR Blink) );
  649. nWinColor(ActiveWn,TextAttr);
  650. End;
  651. { set the text foreground color }
  652. Procedure TextColor(att : byte);
  653. Begin
  654. TextAttr := (att and $8f) or (TextAttr and $70);
  655. nWinColor(ActiveWn,TextAttr);
  656. End;
  657. { set to high intensity }
  658. Procedure HighVideo;
  659. Begin
  660. TextColor(TextAttr Or $08);
  661. End;
  662. { set to low intensity }
  663. Procedure LowVideo;
  664. Begin
  665. TextColor(TextAttr And $77);
  666. End;
  667. { set to normal display colors }
  668. Procedure NormVideo;
  669. Begin
  670. TextColor(7);
  671. TextBackGround(0);
  672. End;
  673. { clear stdscr }
  674. Procedure ClrScr;
  675. Begin
  676. nClrScr(ActiveWn,TextAttr);
  677. End;
  678. { clear from the cursor to the end of line in stdscr }
  679. Procedure ClrEol;
  680. Begin
  681. nClrEol(ActiveWn);
  682. End;
  683. { clear from the cursor to the bottom of stdscr }
  684. Procedure ClrBot;
  685. Begin
  686. nClrBot(ActiveWn);
  687. End;
  688. { insert a line at the cursor line in stdscr }
  689. Procedure InsLine;
  690. Begin
  691. nInsLine(ActiveWn);
  692. End;
  693. { delete line at the cursor in stdscr }
  694. Procedure DelLine;
  695. Begin
  696. nDelLine(ActiveWn);
  697. End;
  698. { position cursor in stdscr }
  699. Procedure GotoXY(x,y : integer);
  700. Begin
  701. nGotoXY(ActiveWn,x,y);
  702. End;
  703. { find cursor x position in stdscr }
  704. Function WhereX : integer;
  705. Begin
  706. WhereX := nWhereX(ActiveWn);
  707. End;
  708. { find cursor y position in stdscr }
  709. Function WhereY : integer;
  710. Begin
  711. WhereY := nWhereY(ActiveWn);
  712. End;
  713. { Wait for DTime milliseconds }
  714. Procedure Delay(DTime: Word);
  715. Begin
  716. Select(0,nil,nil,nil,DTime);
  717. End;
  718. { create a new subwindow of stdscr }
  719. Procedure Window(x,y,x1,y1 : integer);
  720. Begin
  721. nDelWindow(SubWn);
  722. SubWn := subwin(stdscr,y1-y+1,x1-x+1,y-1,x-1);
  723. If SubWn = nil then Exit;
  724. intrflush(SubWn,bool(false));
  725. keypad(SubWn,bool(true));
  726. scrollok(SubWn,bool(true));
  727. ActiveWn := SubWn;
  728. GotoXY(1,1);
  729. End;
  730. {------------------------------------------------------
  731. Check if a key has been pressed.
  732. Note: this is best used along with select() on STDIN,
  733. as it can suck up lots of cpu time.
  734. Better yet, use nKeypressed instead if you don't need
  735. to include file descriptors other than STDIN.
  736. ------------------------------------------------------}
  737. function Keypressed : boolean;
  738. var
  739. l : longint;
  740. { fd : fdSet;}
  741. Begin
  742. Keypressed := FALSE;
  743. nodelay(ActiveWn,bool(TRUE));
  744. l := wgetch(ActiveWn);
  745. If l <> ERR Then Begin { ERR = -(1) from unit ncurses }
  746. ungetch(l);
  747. Keypressed := TRUE;
  748. End;
  749. nodelay(ActiveWn,bool(FALSE));
  750. { Below is more efficient code, but does not work well with
  751. nReadkey & extended keys because nReadkey's ungetch does not
  752. force a change in STDIN. So, a "while keypressed" block does
  753. not produce the expected results when trapping for char(0)
  754. followed by a second scan code.
  755. FD_Zero(fd);
  756. fd_Set(STDIN,fd);
  757. Keypressed := (Select(STDIN+1,@fd,nil,nil,0) > 0);
  758. }
  759. End;
  760. { silently read a key from stdscr }
  761. Function Readkey : char;
  762. Begin
  763. tmp_b := IsEcho;
  764. noecho;
  765. Readkey := nReadkey(ActiveWn);
  766. If tmp_b Then echo;
  767. End;
  768. { a cheap replacement! }
  769. Procedure Sound(hz : word);
  770. Begin
  771. Beep;
  772. wrefresh(ActiveWn);
  773. End;
  774. Procedure NoSound;
  775. Begin
  776. End;
  777. Procedure TextMode(mode : word);
  778. Begin
  779. nDelWindow(SubWn);
  780. ActiveWn := stdscr;
  781. NormVideo;
  782. LastMode := mode;
  783. DirectVideo := true;
  784. CheckSnow := true;
  785. NormVideo;
  786. ClrScr;
  787. End;
  788. { Set the cursor visibility. Returns the previous value }
  789. { or (-1) if value c is not supported by the terminal. }
  790. Function nCursor(c : integer) : integer;
  791. Begin
  792. nCursor := curs_set(c);
  793. End;
  794. { Set the <esc> key delay time in milliseconds. }
  795. { Use d=(-1) to return current value without updating. }
  796. Function nEscDelay(d : longint) : longint;
  797. Begin
  798. nEscDelay := ESCDELAY;
  799. If d >= 0 Then ESCDELAY := d;
  800. End;
  801. { unit initialization, following ncurses init }
  802. Procedure nInit;
  803. Begin
  804. SubWn := nil;
  805. TextMode(LastMode);
  806. { Redirect the standard output }
  807. assigncrt(Output);
  808. Rewrite(Output);
  809. TextRec(Output).Handle:=StdOutputHandle;
  810. { Redirect the standard input }
  811. assigncrt(Input);
  812. Reset(Input);
  813. TextRec(Input).Handle:=StdInputHandle;
  814. nEscDelay(500); { default is 1000 (1 second) }
  815. nCursor(cON); { normal cursor }
  816. End;
  817. { exit procedure to ensure curses is closed up cleanly }
  818. Procedure nExit;
  819. Begin
  820. ExitProc := ExitSave;
  821. EndCurses;
  822. End;