1 //  This may look like C code, but it is really -*- C++ -*-
2 
3 //  ------------------------------------------------------------------
4 //  The Goldware Library
5 //  Copyright (C) 1990-1999 Odinn Sorensen
6 //  Copyright (C) 1999-2000 Alexander S. Aganichev
7 //  ------------------------------------------------------------------
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Library General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Library General Public License for more details.
17 //
18 //  You should have received a copy of the GNU Library General Public
19 //  License along with this program; if not, write to the Free
20 //  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 //  MA 02111-1307, USA
22 //  ------------------------------------------------------------------
23 //  $Id: gvidall.h,v 1.1 2011/02/18 19:46:01 stas_degteff Exp $
24 //  ------------------------------------------------------------------
25 //  GCUI: Golded+ Character-oriented User Interface.
26 //  Screen/video functions.
27 //  Based on CXL by Mike Smedley.
28 //  ------------------------------------------------------------------
29 
30 #ifndef __gvidall_h
31 #define __gvidall_h
32 
33 
34 //  ------------------------------------------------------------------
35 
36 #include <gdefs.h>
37 #if defined(__USE_NCURSES__)
38 #include <gcurses.h>
39 #endif
40 #if defined(__WIN32__)
41 #include <windows.h>
42 #endif
43 
44 #if defined(_MSC_VER)
45 #pragma warning(disable: 4200)
46 #endif
47 
48 
49 //  ------------------------------------------------------------------
50 
51 #ifdef BLINK
52 #undef BLINK
53 #endif
54 #define BLINK       128
55 
56 #ifdef INTENSE
57 #undef INTENSE
58 #endif
59 #define INTENSE     8
60 
61 #if defined(__UNIX__) && !defined(__USE_NCURSES__)
62 #define ACSET       BLINK
63 #else
64 #define ACSET       0
65 #endif
66 
67 
68 //  ------------------------------------------------------------------
69 
70 #if defined(__USE_NCURSES__)
71 typedef chtype vchar;     // Type of characters on-screen
72 typedef int    vattr;     // Type of screen attributes
73 typedef chtype vatch;     // Type of character-attribute groups
74 #elif defined(__WIN32__)
75 typedef char vchar;       // Type of characters on-screen
76 typedef int  vattr;       // Type of screen attributes
77 typedef CHAR_INFO vatch;  // Type of character-attribute groups
78 #else
79 typedef char vchar;       // Type of characters on-screen
80 typedef int  vattr;       // Type of screen attributes
81 typedef word vatch;       // Type of character-attribute groups
82 #endif
83 
84 
85 //  ------------------------------------------------------------------
86 //  Attribute codes for functions that use them
87 
88 const vattr DEFATTR   = -1;
89 
90 const vattr BLACK_    = 0;
91 const vattr BLUE_     = 1;
92 const vattr GREEN_    = 2;
93 const vattr CYAN_     = 3;
94 const vattr RED_      = 4;
95 const vattr MAGENTA_  = 5;
96 const vattr BROWN_    = 6;
97 const vattr LGREY_    = 7;
98 const vattr DGREY_    = 8;
99 const vattr LBLUE_    = 9;
100 const vattr LGREEN_   = 10;
101 const vattr LCYAN_    = 11;
102 const vattr LRED_     = 12;
103 const vattr LMAGENTA_ = 13;
104 const vattr YELLOW_   = 14;
105 const vattr WHITE_    = 15;
106 
107 const vattr _BLACK    = (BLACK_   << 4);
108 const vattr _BLUE     = (BLUE_    << 4);
109 const vattr _GREEN    = (GREEN_   << 4);
110 const vattr _CYAN     = (CYAN_    << 4);
111 const vattr _RED      = (RED_     << 4);
112 const vattr _MAGENTA  = (MAGENTA_ << 4);
113 const vattr _BROWN    = (BROWN_   << 4);
114 const vattr _LGREY    = (LGREY_   << 4);
115 const vattr _DGREY    = (DGREY_   << 4);
116 const vattr _LBLUE    = (LBLUE_   << 4);
117 const vattr _LGREEN   = (LGREEN_  << 4);
118 const vattr _LCYAN    = (LCYAN_   << 4);
119 const vattr _LRED     = (LRED_    << 4);
120 const vattr _LMAGENTA = (LMAGENTA_<< 4);
121 const vattr _YELLOW   = (YELLOW_  << 4);
122 const vattr _WHITE    = (WHITE_   << 4);
123 
124 
125 //  ------------------------------------------------------------------
126 //  Additional monochrome color values
127 
128 const vattr UNDERLINE = 1;
129 const vattr NORMAL    = 7;
130 const vattr HIGHLIGHT = 15;
131 const vattr REVERSE   = 112;
132 
133 
134 //  ------------------------------------------------------------------
135 //  Display adapter types returned from vidtype()
136 //  If bit 0 is set, the adapter was detected in monochrome mode
137 
138 #define GV_NONE     0x0000
139 #define V_MONO      0x0001
140 #define V_MDA       0x0011
141 #define V_HGC       0x0021
142 #define V_HGCPLUS   0x0041
143 #define V_INCOLOR   0x0080
144 #define V_CGA       0x0100
145 #define V_EGA       0x0200
146 #define V_EGAMONO   0x0201
147 #define V_MCGA      0x0400
148 #define V_MCGAMONO  0x0401
149 #define V_VGA       0x0800
150 #define V_VGAMONO   0x0801
151 
152 
153 //  ------------------------------------------------------------------
154 //  Video parameter settings
155 
156 #define VP_DMA      0   // direct screen writes
157 #define VP_CGA      1   // direct screen writes, eliminate CGA snow
158 #define VP_BIOS     2   // BIOS screen writes
159 #define VP_MONO     3   // monochrome attribute translation on
160 #define VP_COLOR    4   // monochrome attribute translation off
161 #define VP_OS2VIO   6   // OS/2 vio screen writes
162 #define VP_W32CON   7   // WIN32 console screen writes
163 #define VP_CURSES   8   // Curses screen writes
164 
165 
166 //  ------------------------------------------------------------------
167 //  Video devices
168 
169 #define GVID_DMA  0
170 #define GVID_CGA  1
171 #define GVID_BIO  2
172 #define GVID_OS2  4
173 #define GVID_W32  5
174 #define GVID_CUR  6
175 
176 
177 //  ------------------------------------------------------------------
178 //  Useful defines for video (0x10) interrupt function numbers
179 
180 #if defined(__MSDOS__)
181 #define V_SET_MODE              0x00
182 #define V_SET_CURSOR_POS        0x02
183 #define V_GET_CURSOR_POS        0x03
184 #define V_SCROLL_UP             0x06
185 #define V_SCROLL_DOWN           0x07
186 #define V_RD_CHAR_ATTR          0x08
187 #define V_WR_CHAR_ATTR          0x09
188 #define V_WR_CHAR               0x0A
189 #define V_WR_TTY                0x0E
190 #define V_GET_MODE              0x0F
191 #define V_GET_FONT_INFO         0x1130
192 #endif
193 
194 
195 //  ------------------------------------------------------------------
196 
197 #if defined(__MSDOS__)
198 struct __int10_ah1b_statebuf {
199                         // Offset  Size    Description
200   dword statfunctable;  //  00h    DWORD   address of static funtionality table (see below)
201   byte  videomode;      //  04h    BYTE    video mode in effect
202   word  columns;        //  05h    WORD    number of columns
203   word  regenbuflen;    //  07h    WORD    length of regen buffer in bytes
204   word  regenbufstart;  //  09h    WORD    starting address of regen buffer
205   word  cursorpos0;     //  0Bh    WORD    cursor position for page 0
206   word  cursorpos1;     //  0Dh    WORD    cursor position for page 1
207   word  cursorpos2;     //  0Fh    WORD    cursor position for page 2
208   word  cursorpos3;     //  11h    WORD    cursor position for page 3
209   word  cursorpos4;     //  13h    WORD    cursor position for page 4
210   word  cursorpos5;     //  15h    WORD    cursor position for page 5
211   word  cursorpos6;     //  17h    WORD    cursor position for page 6
212   word  cursorpos7;     //  19h    WORD    cursor position for page 7
213   word  cursortype;     //  1Bh    WORD    cursor type
214   byte  activepage;     //  1Dh    BYTE    active display page
215   word  crctportaddr;   //  1Eh    WORD    CRTC port address
216   byte  curr_reg_3x8;   //  20h    BYTE    current setting of register (3?8)
217   byte  curr_reg_3x9;   //  21h    BYTE    current setting of register (3?9)
218   byte  rows;           //  22h    BYTE    number of rows
219   word  bytesperchar;   //  23h    WORD    bytes/character
220   byte  dispcombcode;   //  25h    BYTE    display combination code of active display
221   byte  dcc;            //  26h    BYTE    DCC of alternate display
222   word  numcolors;      //  27h    WORD    number of colors supported in current mode
223   byte  numpages;       //  29h    BYTE    number of pages supported in current mode
224   byte  numscanlines;   //  2Ah    BYTE    number of scan lines active (0,1,2,3) = (200,350,400,480) Tseng ET3000: (4,5,6 = 512,600,768)
225   byte  primcharblock;  //  2Bh    BYTE    primary character block
226   byte  seccharblock;   //  2Ch    BYTE    secondary character block
227   byte  miscflags;      //  2Dh    BYTE    miscellaneous flags (see below)
228   byte  reserved1[3];   //  2Eh  3 BYTEs   reserved (00h)
229   byte  videomem;       //  31h    BYTE    video memory available 00h = 64K, 01h = 128K, 02h = 192K, 03h = 256K
230   byte  stateflags;     //  32h    BYTE    save pointer state flags (see below)
231   byte  reserved2[13];  //  33h 13 BYTEs   reserved (00h)
232 };
233 #endif
234 
235 
236 //  ------------------------------------------------------------------
237 //  Border types
238 
239 #define BT_SINGLE     0
240 #define BT_DOUBLE     1
241 #define BT_SINGLETOP  2
242 #define BT_DOUBLETOP  3
243 #define BT_BLANKS     4
244 #define BT_NONE       5
245 #define BT_BLOCKS     6
246 #define BT_ASCII      7
247 
248 
249 //  ------------------------------------------------------------------
250 //  Video information structure
251 
252 struct GVidInfo {
253 
254   // Screen info
255   struct _screen
256   {
257     int  mode;          // Video mode
258     int  rows;          // Number of rows
259     int  columns;       // Number of columns
260     int  cheight;       // Character height
261     int  cwidth;        // Character width
262   } screen;
263 
264   // Cursor info
265   struct _cursor
266   {
267     int  column;        // Cursor column
268     int  row;           // Cursor row
269     int  start;         // Cursor start line
270     int  end;           // Cursor end line
271     word attr;          // Cursor attribute. Hidden if attr == 0xFFFF
272   } cursor;
273 
274   // Colors
275   struct _color
276   {
277     vattr textattr;     // Text attribute
278     vattr overscan;     // Overscan color
279     int   intensity;    // Background color state (intense or blinking)
280     int   palette[16];  // Palette state
281   } color;
282 };
283 
284 
285 //  ------------------------------------------------------------------
286 
287 #ifdef __DJGPP__
288 typedef uint32_t gdma; // Video DMA linear address
289 #else
290 typedef word*    gdma; // Video DMA pointer
291 #endif
292 
293 //  ------------------------------------------------------------------
294 //  Video information record
295 
296 class GVid {
297 
298 public:
299 
300   int      adapter;    // Video adapter type
301 
302   GVidInfo orig;       // Original video info
303   GVidInfo curr;       // Current video info
304 
305   int      device;     // Video device type
306 
307   gdma     dmadir;     // Video DMA pointer (direct)
308   gdma     dmaptr;     // Video DMA pointer (direct or buffered)
309 
310   vchar*   bufchr;     // Video line char buffer (char only)
311   vatch*   bufwrd;     // Video line word buffer (char+attr)
312   vchar*   bufansi;    // Video line ANSI buffer (11*numcols)
313 
314   int      currow;     // Current cursor row
315   int      curcol;     // Current cursor column
316 
317   size_t   numrows;    // number of displayed rows
318   size_t   numcols;    // number of displayed columns
319 
320   word     videoseg;   // video buffer segment address
321 
322 public:
323 
324   GVid();
325   ~GVid();
326 
327 public:
328 
329   void init();
330 
331   int  detectadapter  ();
332   void detectinfo     (GVidInfo* _info);
333   static
334   void setcolorpairs  (bool enabletransparent=false);
335 
336   void resetcurr      ();
337 
338   void setdevice      (int _device);
339 
340   void setmode        (int _mode);
341   void setrows        (int _rows);
342 
343   void setoverscan    (vattr _overscan);
344   void setintensity   (int _intensity);
345 
346   void getpalette     (int* _palette);
347   void setpalette     (int* _palette);
348 
isdma()349   bool isdma()  { return device == GVID_DMA; }
iscga()350   bool iscga()  { return device == GVID_CGA; }
isbios()351   bool isbios() { return device == GVID_BIO; }
352 
353   void restore_cursor();
354 
355   void resize_screen(int columns, int rows);
356 
357 };
358 
359 extern GVid *gvid;
360 
361 
362 //  ------------------------------------------------------------------
363 //  Box characters table
364 
365 #if !defined(__USE_NCURSES__)
366 
367 extern char* __box_table[];
368 #define _box_table(i,j) (__box_table[i][j])
369 
370 #else
371 
372 chtype _box_table(int type, int c);
373 
374 #endif
375 
376 
377 //  ------------------------------------------------------------------
378 //  Prototypes
379 
380 int setvparam  (int setting);
381 
382 vattr mapattr  (vattr attr);
383 vattr revsattr (vattr attr);
384 
attrib(int f,int b,int i,int bl)385 inline vattr attrib(int f, int b, int i, int bl) { return (int)((b<<4)|(f)|(i<<3)|(bl<<7)); }
386 
387 void vputw      (int row, int col, vatch chat);
388 void vputws     (int row, int col, vatch* buf, uint len);
389 void vputc      (int row, int col, vattr atr, vchar chr);
390 void vputvs     (int row, int col, vattr atr, const vchar* str);
391 void vputs      (int row, int col, vattr atr, const char* str);
392 void vputs_box  (int row, int col, vattr atr, const char* str);
393 void vputns     (int row, int col, vattr atr, const char* str, uint len);
394 void vputx      (int row, int col, vattr atr, vchar chr, uint len);
395 void vputy      (int row, int col, vattr atr, vchar chr, uint len);
396 
397 vatch vgetw     (int row, int col);
398 void vgetc      (int row, int col, vattr* atr, vchar* chr);
399 
400 void vscroll    (int srow, int scol, int erow, int ecol, vattr atr, int lines);
401 
402 void vposget    (int* row, int* col);
403 void vposset    (int row, int col);
404 
405 void vclrscr    ();
406 void vclrscr    (vattr atr);     // Overloaded
407 
408 typedef struct _vsavebuf {
409   int top, left, right, bottom;
410   __extension__ vatch data[0];
411 } vsavebuf;
412 vsavebuf* vsave (int srow=-1, int scol=-1, int erow=-1, int ecol=-1);
413 void vrestore   (vsavebuf* buf, int srow=-1, int scol=-1, int erow=-1, int ecol=-1);
414 
415 void vcurget    (int* sline, int* eline);
416 void vcurset    (int sline, int eline);
417 
418 void vcurhide   ();
419 void vcurshow   ();
420 bool vcurhidden ();
421 
422 void vcurlarge  ();
423 void vcursmall  ();
424 
425 void vbox       (int srow, int scol, int erow, int ecol, int box, vattr hiattr, vattr loattr = DEFATTR);
426 void vfill      (int srow, int scol, int erow, int ecol, vchar chr, vattr atr);
427 
428 
429 #if defined(__USE_NCURSES__)
430 
431 int gvid_dosattrcalc (int ourattr);
432 int gvid_attrcalc (int dosattr);
433 
vgchar(vatch chat)434 inline vchar vgchar (vatch chat) { return chat & (A_CHARTEXT | A_ALTCHARSET); }
vgattr(vatch chat)435 inline vattr vgattr (vatch chat) { return gvid_dosattrcalc(chat & ~(A_CHARTEXT | A_ALTCHARSET)); }
vschar(vatch chat,vchar chr)436 inline vatch vschar (vatch chat, vchar chr) { return (chr & (A_CHARTEXT | A_ALTCHARSET)) | (chat & ~(A_CHARTEXT | A_ALTCHARSET)); }
vsattr(vatch chat,vattr atr)437 inline vatch vsattr (vatch chat, vattr atr) { return (chat & (A_CHARTEXT | A_ALTCHARSET)) | gvid_attrcalc(atr); }
vcatch(vchar chr,vattr atr)438 inline vatch vcatch (vchar chr, vattr atr)  { return chr | gvid_attrcalc(atr); }
439 
440 #elif defined(__WIN32__)
441 
vgchar(vatch chat)442 inline vchar vgchar (vatch chat) { return chat.Char.AsciiChar; }
vgattr(vatch chat)443 inline vattr vgattr (vatch chat) { return chat.Attributes; }
vschar(vatch chat,vchar chr)444 inline vatch vschar (vatch chat, vchar chr) { chat.Char.UnicodeChar = 0; chat.Char.AsciiChar = chr; return chat; }
vsattr(vatch chat,vattr atr)445 inline vatch vsattr (vatch chat, vattr atr) { chat.Attributes = WORD(atr); return chat; }
vcatch(vchar chr,vattr atr)446 inline vatch vcatch (vchar chr, vattr atr)  { vatch chat; chat.Char.UnicodeChar = 0; chat.Char.AsciiChar = chr; chat.Attributes = WORD(atr); return chat; }
447 
448 #else
449 
vgchar(vatch chat)450 inline vchar vgchar (vatch chat) { return chat & 0xff; }
vgattr(vatch chat)451 inline vattr vgattr (vatch chat) { return (chat >> 8) & 0xff; }
vschar(vatch chat,vchar chr)452 inline vatch vschar (vatch chat, vchar chr) { return (chat & 0xff00) | chr; }
vsattr(vatch chat,vattr atr)453 inline vatch vsattr (vatch chat, vattr atr) { return (chat & 0xff) | (atr << 8); }
vcatch(vchar chr,vattr atr)454 inline vatch vcatch (vchar chr, vattr atr)  { return (chr & 0xff) | ((atr << 8) & 0xff00); }
455 
456 #endif
457 
vgetc(int row,int col)458 inline vchar vgetc (int row, int col) { return vgchar(vgetw(row, col)); }
459 
460 typedef void (*VidPutStrCP)(int,int,int,const char*);
461 
462 void gvid_boxcvt(char* s);
463 
464 
465 //  ------------------------------------------------------------------
466 
467 #endif
468 
469 //  ------------------------------------------------------------------
470