graph.pp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Florian Klaempfl
  5. This file implements the linux GGI support for the graph unit
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit Graph;
  13. interface
  14. {$i graphh.inc}
  15. Const
  16. { Supported modes }
  17. {(sg) GTEXT deactivated because we need mode #0 as default mode}
  18. {GTEXT = 0; Compatible with VGAlib v1.2 }
  19. G320x200x16 = 1;
  20. G640x200x16 = 2;
  21. G640x350x16 = 3;
  22. G640x480x16 = 4;
  23. G320x200x256 = 5;
  24. G320x240x256 = 6;
  25. G320x400x256 = 7;
  26. G360x480x256 = 8;
  27. G640x480x2 = 9;
  28. G640x480x256 = 10;
  29. G800x600x256 = 11;
  30. G1024x768x256 = 12;
  31. G1280x1024x256 = 13; { Additional modes. }
  32. G320x200x32K = 14;
  33. G320x200x64K = 15;
  34. G320x200x16M = 16;
  35. G640x480x32K = 17;
  36. G640x480x64K = 18;
  37. G640x480x16M = 19;
  38. G800x600x32K = 20;
  39. G800x600x64K = 21;
  40. G800x600x16M = 22;
  41. G1024x768x32K = 23;
  42. G1024x768x64K = 24;
  43. G1024x768x16M = 25;
  44. G1280x1024x32K = 26;
  45. G1280x1024x64K = 27;
  46. G1280x1024x16M = 28;
  47. G800x600x16 = 29;
  48. G1024x768x16 = 30;
  49. G1280x1024x16 = 31;
  50. G720x348x2 = 32; { Hercules emulation mode }
  51. G320x200x16M32 = 33; { 32-bit per pixel modes. }
  52. G640x480x16M32 = 34;
  53. G800x600x16M32 = 35;
  54. G1024x768x16M32 = 36;
  55. G1280x1024x16M32 = 37;
  56. { additional resolutions }
  57. G1152x864x16 = 38;
  58. G1152x864x256 = 39;
  59. G1152x864x32K = 40;
  60. G1152x864x64K = 41;
  61. G1152x864x16M = 42;
  62. G1152x864x16M32 = 43;
  63. G1600x1200x16 = 44;
  64. G1600x1200x256 = 45;
  65. G1600x1200x32K = 46;
  66. G1600x1200x64K = 47;
  67. G1600x1200x16M = 48;
  68. G1600x1200x16M32 = 49;
  69. implementation
  70. uses
  71. linux;
  72. const
  73. InternalDriverName = 'LinuxVGA';
  74. {$i graph.inc}
  75. type
  76. PByte = ^Byte;
  77. PLongInt = ^LongInt;
  78. PByteArray = ^TByteArray;
  79. TByteArray = array [0..MAXINT - 1] of Byte;
  80. { ---------------------------------------------------------------------
  81. SVGA bindings.
  82. ---------------------------------------------------------------------}
  83. { Link with VGA, gl and c libraries }
  84. {$linklib vga}
  85. {$linklib c}
  86. Const
  87. { Text }
  88. WRITEMODE_OVERWRITE = 0;
  89. WRITEMODE_MASKED = 1;
  90. FONT_EXPANDED = 0;
  91. FONT_COMPRESSED = 2;
  92. { Types }
  93. type
  94. pvga_modeinfo = ^vga_modeinfo;
  95. vga_modeinfo = record
  96. width,
  97. height,
  98. bytesperpixel,
  99. colors,
  100. linewidth, { scanline width in bytes }
  101. maxlogicalwidth, { maximum logical scanline width }
  102. startaddressrange, { changeable bits set }
  103. maxpixels, { video memory / bytesperpixel }
  104. haveblit, { mask of blit functions available }
  105. flags: Longint; { other flags }
  106. { Extended fields: }
  107. chiptype, { Chiptype detected }
  108. memory, { videomemory in KB }
  109. linewidth_unit: Longint; { Use only a multiple of this as parameter for set_displaystart }
  110. linear_aperture: PChar; { points to mmap secondary mem aperture of card }
  111. aperture_size: Longint; { size of aperture in KB if size>=videomemory.}
  112. set_aperture_page: procedure (page: Longint);
  113. { if aperture_size<videomemory select a memory page }
  114. extensions: Pointer; { points to copy of eeprom for mach32 }
  115. { depends from actual driver/chiptype.. etc. }
  116. end;
  117. PGraphicsContext = ^TGraphicsContext;
  118. TGraphicsContext = record
  119. ModeType: Byte;
  120. ModeFlags: Byte;
  121. Dummy: Byte;
  122. FlipPage: Byte;
  123. Width: LongInt;
  124. Height: LongInt;
  125. BytesPerPixel: LongInt;
  126. Colors: LongInt;
  127. BitsPerPixel: LongInt;
  128. ByteWidth: LongInt;
  129. VBuf: pointer;
  130. Clip: LongInt;
  131. ClipX1: LongInt;
  132. ClipY1: LongInt;
  133. ClipX2: LongInt;
  134. ClipY2: LongInt;
  135. ff: pointer;
  136. end;
  137. Const
  138. GLASTMODE = 49;
  139. ModeNames : Array[0..GLastMode] of string [18] =
  140. ('Text',
  141. 'G320x200x16',
  142. 'G640x200x16',
  143. 'G640x350x16',
  144. 'G640x480x16',
  145. 'G320x200x256',
  146. 'G320x240x256',
  147. 'G320x400x256',
  148. 'G360x480x256',
  149. 'G640x480x2',
  150. 'G640x480x256',
  151. 'G800x600x256',
  152. 'G1024x768x256',
  153. 'G1280x1024x256',
  154. 'G320x200x32K',
  155. 'G320x200x64K',
  156. 'G320x200x16M',
  157. 'G640x480x32K',
  158. 'G640x480x64K',
  159. 'G640x480x16M',
  160. 'G800x600x32K',
  161. 'G800x600x64K',
  162. 'G800x600x16M',
  163. 'G1024x768x32K',
  164. 'G1024x768x64K',
  165. 'G1024x768x16M',
  166. 'G1280x1024x32K',
  167. 'G1280x1024x64K',
  168. 'G1280x1024x16M',
  169. 'G800x600x16',
  170. '1024x768x16',
  171. '1280x1024x16',
  172. 'G720x348x2',
  173. 'G320x200x16M32',
  174. 'G640x480x16M32',
  175. 'G800x600x16M32',
  176. 'G1024x768x16M32',
  177. 'G1280x1024x16M32',
  178. 'G1152x864x16',
  179. 'G1152x864x256',
  180. 'G1152x864x32K',
  181. 'G1152x864x64K',
  182. 'G1152x864x16M',
  183. 'G1152x864x16M32',
  184. 'G1600x1200x16',
  185. 'G1600x1200x256',
  186. 'G1600x1200x32K',
  187. 'G1600x1200x64K',
  188. 'G1600x1200x16M',
  189. 'G1600x1200x16M32');
  190. {var
  191. PhysicalScreen: PGraphicsContext; }
  192. { vga functions }
  193. Function vga_init: Longint; Cdecl; External;
  194. Function vga_hasmode(mode: Longint): Boolean; Cdecl; External;
  195. Function vga_getmodeinfo(mode: Longint): pvga_modeinfo; Cdecl; External;
  196. Function vga_setmode(mode: Longint): Longint; Cdecl; External;
  197. Function vga_getcolors: Longint; cdecl;external;
  198. Function vga_setpalette(index: Longint; red: Longint; green: Longint; blue: Longint) : longint; cdecl;external;
  199. Function vga_getpalette(index: Longint; var red: Longint; var green: Longint; var blue: Longint): Longint; cdecl;external;
  200. Function vga_setegacolor(Color: Longint) : longint; cdecl;external;
  201. Function vga_setcolor(color: Longint): Longint; cdecl;external;
  202. Function vga_drawpixel(x, y: Longint): Longint; cdecl;external;
  203. Function vga_getpixel(x, y: Longint): Longint; cdecl;external;
  204. Function vga_drawline(x1, y1, x2, y2: Longint): Longint; cdecl;external;
  205. function vga_screenoff: Longint; Cdecl; External;
  206. function vga_screenon: Longint; Cdecl; External;
  207. function vga_getgraphmem: PByteArray; cdecl; external;
  208. var
  209. OldIO : TermIos;
  210. Procedure SetRawMode(b:boolean);
  211. Var
  212. Tio : Termios;
  213. Begin
  214. if b then
  215. begin
  216. TCGetAttr(1,Tio);
  217. OldIO:=Tio;
  218. CFMakeRaw(Tio);
  219. end
  220. else
  221. Tio:=OldIO;
  222. TCSetAttr(1,TCSANOW,Tio);
  223. End;
  224. { ---------------------------------------------------------------------
  225. Required procedures
  226. ---------------------------------------------------------------------}
  227. {$INCLUDE vgagraph16.inc} // Include graphic functions for 16 colours modes
  228. var
  229. LastColor: Integer; {Cache the last set color to improve speed}
  230. procedure SetEGAColor(color: Integer);
  231. begin
  232. if color <> LastColor then begin
  233. LastColor := color;
  234. vga_setegacolor(color);
  235. end;
  236. end;
  237. procedure libvga_savevideostate;
  238. begin
  239. end;
  240. procedure libvga_restorevideostate;
  241. begin
  242. vga_setmode(0);
  243. end;
  244. const
  245. BgiColors: array[0..15] of LongInt
  246. = ($000000, $000020, $002000, $002020,
  247. $200000, $200020, $202000, $303030,
  248. $202020, $00003F, $003F00, $003F3F,
  249. $3F0000, $3F003F, $3F3F00, $3F3F3F);
  250. procedure InitColors;
  251. var
  252. i: Integer;
  253. begin
  254. for i:=0 to 15 do
  255. vga_setpalette(I,BgiColors[i] shr 16,
  256. (BgiColors[i] shr 8) and 255,
  257. BgiColors[i] and 255)
  258. end;
  259. procedure libvga_initmodeproc;
  260. Var Nrcolors : Longint;
  261. begin
  262. vga_setmode(IntCurrentMode);
  263. vga_screenon;
  264. VidMem := vga_getgraphmem;
  265. nrColors:=vga_getcolors;
  266. if (nrColors=16) or (nrcolors=256) then
  267. InitColors;
  268. end;
  269. Function ClipCoords (Var X,Y : Integer) : Boolean;
  270. { Adapt to viewport, return TRUE if still in viewport,
  271. false if outside viewport}
  272. begin
  273. X:= X + StartXViewPort;
  274. Y:= Y + StartYViewPort;
  275. ClipCoords:=Not ClipPixels;
  276. if ClipPixels then
  277. Begin
  278. ClipCoords:=(X < StartXViewPort) or (X > (StartXViewPort + ViewWidth));
  279. ClipCoords:=ClipCoords or
  280. ((Y < StartYViewPort) or (Y > (StartYViewPort + ViewHeight)));
  281. ClipCoords:=Not ClipCoords;
  282. end;
  283. end;
  284. procedure libvga_directpixelproc(X,Y: Integer);
  285. Var Color : Word;
  286. begin
  287. case CurrentWriteMode of
  288. XORPut:
  289. begin
  290. { getpixel wants local/relative coordinates }
  291. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  292. Color := CurrentColor Xor Color;
  293. end;
  294. OrPut:
  295. begin
  296. { getpixel wants local/relative coordinates }
  297. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  298. Color := CurrentColor Or Color;
  299. end;
  300. AndPut:
  301. begin
  302. { getpixel wants local/relative coordinates }
  303. Color := GetPixel(x-StartXViewPort,y-StartYViewPort);
  304. Color := CurrentColor And Color;
  305. end;
  306. NotPut:
  307. begin
  308. Color := Not Color;
  309. end
  310. else
  311. Color:=CurrentColor;
  312. end;
  313. SetEGAColor(Color);
  314. vga_drawpixel(x, y);
  315. end;
  316. procedure libvga_putpixelproc(X,Y: Integer; Color: Word);
  317. begin
  318. If Not ClipCoords(X,Y) Then exit;
  319. SetEGAColor(Color);
  320. vga_drawpixel(x, y);
  321. end;
  322. function libvga_getpixelproc (X,Y: Integer): word;
  323. begin
  324. ClipCoords(X,Y);
  325. libvga_getpixelproc:=vga_getpixel(x, y);
  326. end;
  327. procedure libvga_clrviewproc;
  328. Var I,Xmax : longint;
  329. begin
  330. SetEGAColor(CurrentBkColor);
  331. Xmax:=StartXViewPort+ViewWidth-1;
  332. For i:=StartYViewPort to StartYViewPort+ViewHeight-1 do
  333. vga_drawline(StartXViewPort,I,Xmax,I);
  334. end;
  335. { Bitmap utilities }
  336. {type
  337. PBitmap = ^TBitmap;
  338. TBitmap = record
  339. Width, Height: Integer;
  340. Data: record end;
  341. end;
  342. }
  343. procedure libvga_putimageproc (X,Y: Integer; var Bitmap; BitBlt: Word);
  344. begin
  345. {
  346. With TBitMap(BitMap) do
  347. gl_putbox(x, y, width, height, @Data);
  348. }
  349. end;
  350. procedure libvga_getimageproc (X1,Y1,X2,Y2: Integer; Var Bitmap);
  351. begin
  352. { with TBitmap(Bitmap) do
  353. begin
  354. Width := x2 - x1 + 1;
  355. Height := y2 - y1 + 1;
  356. gl_getbox(x1,y1, x2 - x1 + 1, y2 - y1 + 1, @Data);
  357. end;
  358. }
  359. end;
  360. {
  361. function libvga_imagesizeproc (X1,Y1,X2,Y2: Integer): longint;
  362. begin
  363. libvga_imagesizeproc := SizeOf(TBitmap) + (x2 - x1 + 1) * (y2 - y1 + 1) * PhysicalScreen^.BytesPerPixel;
  364. end;
  365. }
  366. procedure libvga_hlineproc (x, x2,y : integer);
  367. begin
  368. end;
  369. procedure libvga_vlineproc (x,y,y2: integer);
  370. begin
  371. end;
  372. procedure libvga_patternlineproc (x1,x2,y: integer);
  373. begin
  374. end;
  375. procedure libvga_ellipseproc (X,Y: Integer;XRadius: word;
  376. YRadius:word; stAngle,EndAngle: word; fp: PatternLineProc);
  377. begin
  378. end;
  379. procedure libvga_lineproc (X1, Y1, X2, Y2 : Integer);
  380. begin
  381. end;
  382. procedure libvga_getscanlineproc (X1,X2,Y : integer; var data);
  383. begin
  384. end;
  385. procedure libvga_setactivepageproc (page: word);
  386. begin
  387. end;
  388. procedure libvga_setvisualpageproc (page: word);
  389. begin
  390. end;
  391. procedure libvga_savestateproc;
  392. begin
  393. end;
  394. procedure libvga_restorestateproc;
  395. begin
  396. end;
  397. procedure libvga_setrgbpaletteproc(ColorNum, RedValue, GreenValue, BlueValue: Integer);
  398. begin
  399. vga_setpalette(ColorNum,RedValue shr 2,GreenValue shr 2,BlueValue shr 2);
  400. end;
  401. procedure libvga_getrgbpaletteproc (ColorNum: integer;
  402. var RedValue, GreenValue, BlueValue: Integer);
  403. Var R,G,B : longint;
  404. begin
  405. vga_getpalette(ColorNum,R,G,B);
  406. RedValue:=R * 255 div 63;
  407. GreenValue:=G * 255 div 63;
  408. BlueValue:=B * 255 div 63;
  409. end;
  410. {************************************************************************}
  411. {* General routines *}
  412. {************************************************************************}
  413. procedure CloseGraph;
  414. Begin
  415. If not isgraphmode then
  416. begin
  417. _graphresult := grnoinitgraph;
  418. exit
  419. end;
  420. RestoreVideoState;
  421. isgraphmode := false;
  422. end;
  423. function QueryAdapterInfo:PModeInfo;
  424. { This routine returns the head pointer to the list }
  425. { of supported graphics modes. }
  426. { Returns nil if no graphics mode supported. }
  427. { This list is READ ONLY! }
  428. var
  429. mode: TModeInfo;
  430. modeinfo : vga_modeinfo;
  431. i : longint;
  432. begin
  433. QueryAdapterInfo := ModeList;
  434. { If the mode listing already exists... }
  435. { simply return it, without changing }
  436. { anything... }
  437. if assigned(ModeList) then
  438. exit;
  439. SaveVideoState:=libvga_savevideostate;
  440. RestoreVideoState:=libvga_restorevideostate;
  441. vga_init;
  442. For I:=0 to GLastMode do
  443. begin
  444. If vga_hasmode(I) then
  445. begin
  446. ModeInfo:=vga_getmodeinfo(i)^;
  447. InitMode(Mode);
  448. With Mode do
  449. begin
  450. ModeNumber:=I;
  451. ModeName:=ModeNames[i];
  452. // Pretend we are VGA always.
  453. DriverNumber := VGA;
  454. // MaxX is number of pixels in X direction - 1
  455. MaxX:=ModeInfo.Width-1;
  456. // same for MaxY
  457. MaxY:=ModeInfo.Height-1;
  458. MaxColor := ModeInfo.colors;
  459. PaletteSize := MaxColor;
  460. HardwarePages := 0;
  461. // necessary hooks ...
  462. if (MaxColor = 16) and
  463. (LongInt(ModeInfo.Width) * LongInt(ModeInfo.Height) < 65536*4*2) then
  464. begin
  465. // Use optimized graphics routines for 4 bit EGA/VGA modes
  466. ScrWidth := ModeInfo.Width div 8;
  467. DirectPutPixel := @DirectPutPixel16;
  468. PutPixel := @PutPixel16;
  469. GetPixel := @GetPixel16;
  470. HLine := @HLine16;
  471. VLine := @VLine16;
  472. GetScanLine := @GetScanLine16;
  473. end
  474. else
  475. begin
  476. DirectPutPixel := @libvga_DirectPixelProc;
  477. GetPixel := @libvga_GetPixelProc;
  478. PutPixel := @libvga_PutPixelProc;
  479. { May be implemented later:
  480. HLine := @libvga_HLineProc;
  481. VLine := @libvga_VLineProc;
  482. GetScanLine := @libvga_GetScanLineProc;}
  483. ClearViewPort := @libvga_ClrViewProc;
  484. end;
  485. SetRGBPalette := @libvga_SetRGBPaletteProc;
  486. GetRGBPalette := @libvga_GetRGBPaletteProc;
  487. { These are not really implemented yet:
  488. PutImage := @libvga_PutImageProc;
  489. GetImage := @libvga_GetImageProc;}
  490. { If you use the default getimage/putimage, you also need the default
  491. imagesize! (JM)
  492. ImageSize := @libvga_ImageSizeProc; }
  493. { Add later maybe ?
  494. SetVisualPage := SetVisualPageProc;
  495. SetActivePage := SetActivePageProc;
  496. Line := @libvga_LineProc;
  497. InternalEllipse:= @libvga_EllipseProc;
  498. PatternLine := @libvga_PatternLineProc;
  499. }
  500. InitMode := @libvga_InitModeProc;
  501. end;
  502. AddMode(Mode);
  503. end;
  504. end;
  505. end;
  506. begin
  507. InitializeGraph;
  508. SetRawMode(True);
  509. end.
  510. {
  511. $Log$
  512. Revision 1.17 2000-06-30 22:14:44 peter
  513. * removed unused var
  514. Revision 1.16 2000/06/25 13:38:30 jonas
  515. * disabled libvga_imagesizeproc() because currently the default
  516. getimage and putimage are used (so the default imagesize should
  517. be used too)
  518. Revision 1.15 2000/04/16 21:19:19 sg
  519. * The terminal will now be set to raw mode directly on initialization,
  520. as the svgalib switches the terminal immediately after startup, and
  521. not after a switch to graphics mode!
  522. * Fixed a problem which occured after Florian's last patch: MaxX and MaxY
  523. are smaller by 1 now, which lead to a wrong scanline width calculation
  524. for the optimized routines for 16 colour modes.
  525. Revision 1.14 2000/04/13 16:01:22 sg
  526. * The new terminal started by svgalib after a switch to graphics mode is
  527. now set to raw mode, so that some functions of the CRT unit such as
  528. ReadKey can work correctly now.
  529. Revision 1.13 2000/03/25 19:12:00 florian
  530. * fixed values of MaxX and MaxY
  531. Revision 1.12 2000/03/19 11:20:14 peter
  532. * graph unit include is now independent and the dependent part
  533. is now in graph.pp
  534. * ggigraph unit for linux added
  535. }