consolei.inc 15 KB

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