consolei.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. Constructor TPTCConsole.Create;
  18. Var
  19. I : Integer;
  20. {$IFDEF UNIX}
  21. s : AnsiString;
  22. {$ENDIF UNIX}
  23. Begin
  24. Inherited Create;
  25. console := Nil;
  26. hacky_option_console_flag := False;
  27. FillChar(m_modes, SizeOf(m_modes), 0);
  28. For I := Low(m_modes) To High(m_modes) Do
  29. m_modes[I] := TPTCMode.Create;
  30. {$IFDEF UNIX}
  31. configure('/usr/share/ptc/ptc.conf');
  32. s := fpgetenv('HOME');
  33. If s = '' Then
  34. s := '/';
  35. If s[Length(s)] <> '/' Then
  36. s := s + '/';
  37. s := s + '.ptc.conf';
  38. configure(s);
  39. {$ELSE UNIX}
  40. configure('ptc.cfg');
  41. {$ENDIF UNIX}
  42. End;
  43. Destructor TPTCConsole.Destroy;
  44. Var
  45. I : Integer;
  46. Begin
  47. close;
  48. console.Free;
  49. For I := Low(m_modes) To High(m_modes) Do
  50. m_modes[I].Free;
  51. Inherited Destroy;
  52. End;
  53. Procedure TPTCConsole.configure(Const _file : String);
  54. Var
  55. F : Text;
  56. S : String;
  57. Begin
  58. ASSignFile(F, _file);
  59. {$I-}
  60. Reset(F);
  61. {$I+}
  62. If IOResult <> 0 Then
  63. Exit;
  64. While Not EoF(F) Do
  65. Begin
  66. {$I-}
  67. Readln(F, S);
  68. {$I+}
  69. If IOResult <> 0 Then
  70. Break;
  71. option(S);
  72. End;
  73. CloseFile(F);
  74. End;
  75. Function TPTCConsole.option(Const _option : String) : Boolean;
  76. Begin
  77. {$IFDEF PTC_LOGGING}
  78. If _option = 'enable logging' Then
  79. Begin
  80. LOG_enabled := True;
  81. option := True;
  82. Exit;
  83. End;
  84. If _option = 'disable logging' Then
  85. Begin
  86. LOG_enabled := False;
  87. option := True;
  88. Exit;
  89. End;
  90. {$ENDIF PTC_LOGGING}
  91. If Assigned(console) Then
  92. option := console.option(_option)
  93. Else
  94. Begin
  95. console := ConsoleCreate(_option);
  96. If Assigned(console) Then
  97. Begin
  98. hacky_option_console_flag := True;
  99. option := True;
  100. End
  101. Else
  102. option := False;
  103. End;
  104. End;
  105. Function TPTCConsole.modes : PPTCMode;
  106. Var
  107. _console : TPTCBaseConsole;
  108. index, mode : Integer;
  109. local : Integer;
  110. _modes : PPTCMode;
  111. tmp : TPTCMode;
  112. Begin
  113. If Assigned(console) Then
  114. modes := console.modes
  115. Else
  116. Begin
  117. _console := Nil;
  118. index := -1;
  119. mode := 0;
  120. Try
  121. Repeat
  122. Inc(index);
  123. Try
  124. _console := ConsoleCreate(index);
  125. Except
  126. On TPTCError Do Begin
  127. FreeAndNil(_console);
  128. Continue;
  129. End;
  130. End;
  131. If _console = Nil Then
  132. Break;
  133. _modes := _console.modes;
  134. local := 0;
  135. While _modes[local].valid Do
  136. Begin
  137. m_modes[mode].ASSign(_modes[local]);
  138. Inc(local);
  139. Inc(mode);
  140. End;
  141. FreeAndNil(_console);
  142. Until False;
  143. Finally
  144. _console.Free;
  145. End;
  146. { todo: strip duplicate modes from list? }
  147. tmp := TPTCMode.Create;
  148. Try
  149. m_modes[mode].ASSign(tmp);
  150. Finally
  151. tmp.Free;
  152. End;
  153. modes := m_modes;
  154. End;
  155. End;
  156. Procedure TPTCConsole.open(Const _title : String; _pages : Integer);{ Overload;}
  157. Var
  158. composite, tmp : TPTCError;
  159. index : Integer;
  160. success : Boolean;
  161. Begin
  162. If Assigned(console) Then
  163. Begin
  164. Try
  165. console.open(_title, _pages);
  166. Exit;
  167. Except
  168. On error : TPTCError Do Begin
  169. FreeAndNil(console);
  170. If hacky_option_console_flag Then
  171. Begin
  172. hacky_option_console_flag := False;
  173. Raise TPTCError.Create('could not open console', error);
  174. End;
  175. End;
  176. End;
  177. End;
  178. index := -1;
  179. composite := TPTCError.Create;
  180. success := False;
  181. Try
  182. Repeat
  183. Inc(index);
  184. Try
  185. console := ConsoleCreate(index);
  186. If console = Nil Then
  187. Break;
  188. console.open(_title, _pages);
  189. success := True;
  190. Exit;
  191. Except
  192. On error : TPTCError Do Begin
  193. tmp := TPTCError.Create(error.message, composite);
  194. Try
  195. composite.ASSign(tmp);
  196. Finally
  197. tmp.Free;
  198. End;
  199. FreeAndNil(console);
  200. Continue;
  201. End;
  202. End;
  203. Until False;
  204. console := Nil;
  205. Raise TPTCError.Create(composite);
  206. Finally
  207. composite.Free;
  208. If Not success Then
  209. FreeAndNil(console);
  210. End;
  211. End;
  212. Procedure TPTCConsole.open(Const _title : String; Const _format : TPTCFormat;
  213. _pages : Integer);{ Overload;}
  214. Var
  215. composite, tmp : TPTCError;
  216. index : Integer;
  217. success : Boolean;
  218. Begin
  219. If Assigned(console) Then
  220. Begin
  221. Try
  222. console.open(_title, _format, _pages);
  223. Exit;
  224. Except
  225. On error : TPTCError Do Begin
  226. FreeAndNil(console);
  227. If hacky_option_console_flag Then
  228. Begin
  229. hacky_option_console_flag := False;
  230. Raise TPTCError.Create('could not open console', error);
  231. End;
  232. End;
  233. End;
  234. End;
  235. index := -1;
  236. composite := TPTCError.Create;
  237. success := False;
  238. Try
  239. Repeat
  240. Inc(index);
  241. Try
  242. console := ConsoleCreate(index);
  243. If console = Nil Then
  244. Break;
  245. console.open(_title, _format, _pages);
  246. success := True;
  247. Exit;
  248. Except
  249. On error : TPTCError Do Begin
  250. tmp := TPTCError.Create(error.message, composite);
  251. Try
  252. composite.ASSign(tmp);
  253. Finally
  254. tmp.Free;
  255. End;
  256. FreeAndNil(console);
  257. Continue;
  258. End;
  259. End;
  260. Until False;
  261. console := Nil;
  262. Raise TPTCError.Create(composite);
  263. Finally
  264. composite.Free;
  265. If Not success Then
  266. FreeAndNil(console);
  267. End;
  268. End;
  269. Procedure TPTCConsole.open(Const _title : String; _width, _height : Integer;
  270. Const _format : TPTCFormat; _pages : Integer);{ Overload;}
  271. Var
  272. composite, tmp : TPTCError;
  273. index : Integer;
  274. success : Boolean;
  275. Begin
  276. If Assigned(console) Then
  277. Begin
  278. Try
  279. console.open(_title, _width, _height, _format, _pages);
  280. Exit;
  281. Except
  282. On error : TPTCError Do Begin
  283. FreeAndNil(console);
  284. If hacky_option_console_flag Then
  285. Begin
  286. hacky_option_console_flag := False;
  287. Raise TPTCError.Create('could not open console', error);
  288. End;
  289. End;
  290. End;
  291. End;
  292. index := -1;
  293. composite := TPTCError.Create;
  294. success := False;
  295. Try
  296. Repeat
  297. Inc(index);
  298. Try
  299. console := ConsoleCreate(index);
  300. If console = Nil Then
  301. Break;
  302. console.open(_title, _width, _height, _format, _pages);
  303. success := True;
  304. Exit;
  305. Except
  306. On error : TPTCError Do Begin
  307. tmp := TPTCError.Create(error.message, composite);
  308. Try
  309. composite.ASSign(tmp);
  310. Finally
  311. tmp.Free;
  312. End;
  313. FreeAndNil(console);
  314. Continue;
  315. End;
  316. End;
  317. Until False;
  318. console := Nil;
  319. Raise TPTCError.Create(composite);
  320. Finally
  321. composite.Free;
  322. If Not success Then
  323. FreeAndNil(console);
  324. End;
  325. End;
  326. Procedure TPTCConsole.open(Const _title : String; Const _mode : TPTCMode;
  327. _pages : Integer);{ Overload;}
  328. Var
  329. composite, tmp : TPTCError;
  330. index : Integer;
  331. success : Boolean;
  332. Begin
  333. If Assigned(console) Then
  334. Begin
  335. Try
  336. console.open(_title, _mode, _pages);
  337. Exit;
  338. Except
  339. On error : TPTCError Do Begin
  340. FreeAndNil(console);
  341. If hacky_option_console_flag Then
  342. Begin
  343. hacky_option_console_flag := False;
  344. Raise TPTCError.Create('could not open console', error);
  345. End;
  346. End;
  347. End;
  348. End;
  349. index := -1;
  350. composite := TPTCError.Create;
  351. success := False;
  352. Try
  353. Repeat
  354. Inc(index);
  355. Try
  356. console := ConsoleCreate(index);
  357. If console = Nil Then
  358. Break;
  359. console.open(_title, _mode, _pages);
  360. success := True;
  361. Exit;
  362. Except
  363. On error : TPTCError Do Begin
  364. tmp := TPTCError.Create(error.message, composite);
  365. Try
  366. composite.ASSign(tmp);
  367. Finally
  368. tmp.Free;
  369. End;
  370. FreeAndNil(console);
  371. Continue;
  372. End;
  373. End;
  374. Until False;
  375. console := Nil;
  376. Raise TPTCError.Create(composite);
  377. Finally
  378. composite.Free;
  379. If Not success Then
  380. FreeAndNil(console);
  381. End;
  382. End;
  383. Procedure TPTCConsole.close;
  384. Begin
  385. If Assigned(console) Then
  386. console.close;
  387. hacky_option_console_flag := False;
  388. End;
  389. Procedure TPTCConsole.flush;
  390. Begin
  391. check;
  392. console.flush;
  393. End;
  394. Procedure TPTCConsole.finish;
  395. Begin
  396. check;
  397. console.finish;
  398. End;
  399. Procedure TPTCConsole.update;
  400. Begin
  401. check;
  402. console.update;
  403. End;
  404. Procedure TPTCConsole.update(Const _area : TPTCArea);
  405. Begin
  406. check;
  407. console.update(_area);
  408. End;
  409. Procedure TPTCConsole.internal_ReadKey(k : TPTCKey);
  410. Begin
  411. check;
  412. console.internal_ReadKey(k);
  413. End;
  414. Function TPTCConsole.internal_PeekKey(k : TPTCKey) : Boolean;
  415. Begin
  416. check;
  417. Result := console.internal_PeekKey(k);
  418. End;
  419. Procedure TPTCConsole.copy(Var surface : TPTCBaseSurface);
  420. Begin
  421. check;
  422. console.copy(surface);
  423. End;
  424. Procedure TPTCConsole.copy(Var surface : TPTCBaseSurface;
  425. Const source, destination : TPTCArea);
  426. Begin
  427. check;
  428. console.copy(surface, source, destination);
  429. End;
  430. Function TPTCConsole.lock : Pointer;
  431. Begin
  432. check;
  433. lock := console.lock;
  434. End;
  435. Procedure TPTCConsole.unlock;
  436. Begin
  437. check;
  438. console.unlock;
  439. End;
  440. Procedure TPTCConsole.load(Const pixels : Pointer;
  441. _width, _height, _pitch : Integer;
  442. Const _format : TPTCFormat;
  443. Const _palette : TPTCPalette);
  444. Begin
  445. check;
  446. console.load(pixels, _width, _height, _pitch, _format, _palette);
  447. End;
  448. Procedure TPTCConsole.load(Const pixels : Pointer;
  449. _width, _height, _pitch : Integer;
  450. Const _format : TPTCFormat;
  451. Const _palette : TPTCPalette;
  452. Const source, destination : TPTCArea);
  453. Begin
  454. check;
  455. console.load(pixels, _width, _height, _pitch, _format, _palette,
  456. source, destination);
  457. End;
  458. Procedure TPTCConsole.save(pixels : Pointer;
  459. _width, _height, _pitch : Integer;
  460. Const _format : TPTCFormat;
  461. Const _palette : TPTCPalette);
  462. Begin
  463. check;
  464. console.save(pixels, _width, _height, _pitch, _format, _palette);
  465. End;
  466. Procedure TPTCConsole.save(pixels : Pointer;
  467. _width, _height, _pitch : Integer;
  468. Const _format : TPTCFormat;
  469. Const _palette : TPTCPalette;
  470. Const source, destination : TPTCArea);
  471. Begin
  472. check;
  473. console.save(pixels, _width, _height, _pitch, _format, _palette,
  474. source, destination);
  475. End;
  476. Procedure TPTCConsole.clear;
  477. Begin
  478. check;
  479. console.clear;
  480. End;
  481. Procedure TPTCConsole.clear(Const color : TPTCColor);
  482. Begin
  483. check;
  484. console.clear(color);
  485. End;
  486. Procedure TPTCConsole.clear(Const color : TPTCColor;
  487. Const _area : TPTCArea);
  488. Begin
  489. check;
  490. console.clear(color, _area);
  491. End;
  492. Procedure TPTCConsole.palette(Const _palette : TPTCPalette);
  493. Begin
  494. check;
  495. console.palette(_palette);
  496. End;
  497. Function TPTCConsole.palette : TPTCPalette;
  498. Begin
  499. check;
  500. palette := console.palette;
  501. End;
  502. Procedure TPTCConsole.clip(Const _area : TPTCArea);
  503. Begin
  504. check;
  505. console.clip(_area);
  506. End;
  507. Function TPTCConsole.width : Integer;
  508. Begin
  509. check;
  510. width := console.width;
  511. End;
  512. Function TPTCConsole.height : Integer;
  513. Begin
  514. check;
  515. height := console.height;
  516. End;
  517. Function TPTCConsole.pitch : Integer;
  518. Begin
  519. check;
  520. pitch := console.pitch;
  521. End;
  522. Function TPTCConsole.pages : Integer;
  523. Begin
  524. check;
  525. pages := console.pages;
  526. End;
  527. Function TPTCConsole.area : TPTCArea;
  528. Begin
  529. check;
  530. area := console.area;
  531. End;
  532. Function TPTCConsole.clip : TPTCArea;
  533. Begin
  534. check;
  535. clip := console.clip;
  536. End;
  537. Function TPTCConsole.format : TPTCFormat;
  538. Begin
  539. check;
  540. format := console.format;
  541. End;
  542. Function TPTCConsole.name : String;
  543. Begin
  544. name := '';
  545. If Assigned(console) Then
  546. name := console.name
  547. Else
  548. {$IFDEF GO32V2}
  549. name := 'DOS';
  550. {$ENDIF GO32V2}
  551. {$IFDEF WIN32}
  552. name := 'Win32';
  553. {$ENDIF WIN32}
  554. {$IFDEF LINUX}
  555. name := 'Linux';
  556. {$ENDIF LINUX}
  557. End;
  558. Function TPTCConsole.title : String;
  559. Begin
  560. check;
  561. title := console.title;
  562. End;
  563. Function TPTCConsole.information : String;
  564. Begin
  565. check;
  566. information := console.information;
  567. End;
  568. Function TPTCConsole.ConsoleCreate(index : Integer) : TPTCBaseConsole;
  569. Begin
  570. {$IFDEF GO32V2}
  571. Case index Of
  572. 0 : ConsoleCreate := VESAConsole.Create;
  573. 1 : ConsoleCreate := VGAConsole.Create;
  574. 2 : ConsoleCreate := CGAConsole.Create;
  575. 3 : ConsoleCreate := TEXTFX2Console.Create;
  576. Else
  577. ConsoleCreate := Nil;
  578. End;
  579. {$ENDIF GO32V2}
  580. {$IFDEF WIN32}
  581. Case index Of
  582. 0 : ConsoleCreate := TDirectXConsole.Create;
  583. Else
  584. ConsoleCreate := Nil;
  585. End;
  586. {$ENDIF WIN32}
  587. {$IFDEF UNIX}
  588. Case index Of
  589. 0 : ConsoleCreate := TX11Console.Create;
  590. Else
  591. ConsoleCreate := Nil;
  592. End;
  593. {$ENDIF UNIX}
  594. If ConsoleCreate <> Nil Then
  595. ConsoleCreate.KeyReleaseEnabled := KeyReleaseEnabled;
  596. End;
  597. Function TPTCConsole.ConsoleCreate(Const _name : String) : TPTCBaseConsole;
  598. Begin
  599. ConsoleCreate := Nil;
  600. {$IFDEF GO32V2}
  601. If _name = 'VESA' Then
  602. ConsoleCreate := VESAConsole.Create;
  603. If (_name = 'VGA') Or (_name = 'Fakemode') Then
  604. ConsoleCreate := VGAConsole.Create;
  605. If (_name = 'TEXTFX2') Or (_name = 'Text') Then
  606. ConsoleCreate := TEXTFX2Console.Create;
  607. If _name = 'CGA' Then
  608. ConsoleCreate := CGAConsole.Create;
  609. {$ENDIF GO32V2}
  610. {$IFDEF WIN32}
  611. If _name = 'DirectX' Then
  612. ConsoleCreate := TDirectXConsole.Create;
  613. {$ENDIF WIN32}
  614. {$IFDEF UNIX}
  615. If _name = 'X11' Then
  616. ConsoleCreate := TX11Console.Create;
  617. {$ENDIF UNIX}
  618. If ConsoleCreate <> Nil Then
  619. ConsoleCreate.KeyReleaseEnabled := KeyReleaseEnabled;
  620. End;
  621. Procedure TPTCConsole.check;
  622. Begin
  623. {$IFDEF DEBUG}
  624. If console = Nil Then
  625. Raise TPTCError.Create('console is not open (core)');
  626. {$ENDIF DEBUG}
  627. End;
  628. {$WARNING this should be removed for fpc 1.1}
  629. {pages=0}
  630. Procedure TPTCConsole.open(Const _title : String);
  631. Begin
  632. open(_title, 0);
  633. End;
  634. Procedure TPTCConsole.open(Const _title : String; Const _format : TPTCFormat);
  635. Begin
  636. open(_title, _format, 0);
  637. End;
  638. Procedure TPTCConsole.open(Const _title : String; _width, _height : Integer;
  639. Const _format : TPTCFormat);
  640. Begin
  641. open(_title, _width, _height, _format, 0);
  642. End;
  643. Procedure TPTCConsole.open(Const _title : String; Const _mode : TPTCMode);
  644. Begin
  645. open(_title, _mode, 0);
  646. End;
  647. {/pages=0}