home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / CONSOLE.C < prev    next >
Text File  |  1994-01-24  |  6KB  |  295 lines

  1. /* ----------- console.c ---------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. /* ----- table of alt keys for finding shortcut keys ----- */
  6. static int altconvert[] = {
  7.     ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
  8.     ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
  9.     ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
  10.     ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
  11.     ALT_6,ALT_7,ALT_8,ALT_9
  12. };
  13.  
  14. unsigned video_mode;
  15. unsigned video_page;
  16.  
  17. static int near cursorpos[MAXSAVES];
  18. static int near cursorshape[MAXSAVES];
  19. static int cs;
  20.  
  21. static union REGS regs;
  22.  
  23. /* ------------- clear the screen -------------- */
  24. void clearscreen(void)
  25. {
  26.     int ht = SCREENHEIGHT;
  27.     int wd = SCREENWIDTH;
  28.     cursor(0, 0);
  29.     regs.h.al = ' ';
  30.     regs.h.ah = 9;
  31.     regs.x.bx = 7;
  32.     regs.x.cx = ht * wd;
  33.     int86(VIDEO, ®s, ®s);
  34. }
  35.  
  36. void SwapCursorStack(void)
  37. {
  38.     if (cs > 1)    {
  39.         swap(cursorpos[cs-2], cursorpos[cs-1]);
  40.         swap(cursorshape[cs-2], cursorshape[cs-1]);
  41.     }
  42. }
  43.  
  44. #ifndef MSC
  45. #ifndef WATCOM
  46. #define ZEROFLAG 0x40
  47. /* ---- Test for keystroke ---- */
  48. BOOL keyhit(void)
  49. {
  50.     _AH = 1;
  51.     geninterrupt(KEYBRD);
  52.     return (_FLAGS & ZEROFLAG) == 0;
  53. }
  54. #endif
  55. #endif
  56.  
  57. /* ---- Read a keystroke ---- */
  58. int getkey(void)
  59. {
  60.     int c;
  61.     while (keyhit() == FALSE)
  62.         ;
  63.     if (((c = bioskey(0)) & 0xff) == 0)
  64.         c = (c >> 8) | 0x1080;
  65.     else
  66.         c &= 0xff;
  67.     return c & 0x10ff;
  68. }
  69.  
  70. /* ---------- read the keyboard shift status --------- */
  71. int getshift(void)
  72. {
  73.     regs.h.ah = 2;
  74.     int86(KEYBRD, ®s, ®s);
  75.     return regs.h.al;
  76. }
  77.  
  78. static int far *clk = MK_FP(0x40,0x6c);
  79. /* ------- macro to wait one clock tick -------- */
  80. #define wait()          \
  81. {                       \
  82.     int now = *clk;     \
  83.     while (now == *clk) \
  84.         ;               \
  85. }
  86.  
  87. /* -------- sound a buzz tone ---------- */
  88. void beep(void)
  89. {
  90.     wait();
  91.     outp(0x43, 0xb6);               /* program the frequency */
  92.     outp(0x42, (int) (COUNT % 256));
  93.     outp(0x42, (int) (COUNT / 256));
  94.     outp(0x61, inp(0x61) | 3);      /* start the sound */
  95.     wait();
  96.     outp(0x61, inp(0x61) & ~3);     /* stop the sound  */
  97. }
  98.  
  99. /* -------- get the video mode and page from BIOS -------- */
  100. void videomode(void)
  101. {
  102.     regs.h.ah = 15;
  103.     int86(VIDEO, ®s, ®s);
  104.     video_mode = regs.h.al;
  105.     video_page = regs.x.bx;
  106.     video_page &= 0xff00;
  107.     video_mode &= 0x7f;
  108. }
  109.  
  110. /* ------ position the cursor ------ */
  111. void cursor(int x, int y)
  112. {
  113.     videomode();
  114.     regs.x.dx = ((y << 8) & 0xff00) + x;
  115.     regs.h.ah = SETCURSOR;
  116.     regs.x.bx = video_page;
  117.     int86(VIDEO, ®s, ®s);
  118. }
  119.  
  120. /* ------ get cursor shape and position ------ */
  121. static void near getcursor(void)
  122. {
  123.     videomode();
  124.     regs.h.ah = READCURSOR;
  125.     regs.x.bx = video_page;
  126.     int86(VIDEO, ®s, ®s);
  127. }
  128.  
  129. /* ------- get the current cursor position ------- */
  130. void curr_cursor(int *x, int *y)
  131. {
  132.     getcursor();
  133.     *x = regs.h.dl;
  134.     *y = regs.h.dh;
  135. }
  136.  
  137. /* ------ save the current cursor configuration ------ */
  138. void savecursor(void)
  139. {
  140.     if (cs < MAXSAVES)    {
  141.         getcursor();
  142.         cursorshape[cs] = regs.x.cx;
  143.         cursorpos[cs] = regs.x.dx;
  144.         cs++;
  145.     }
  146. }
  147.  
  148. /* ---- restore the saved cursor configuration ---- */
  149. void restorecursor(void)
  150. {
  151.     if (cs)    {
  152.         --cs;
  153.         videomode();
  154.         regs.x.dx = cursorpos[cs];
  155.         regs.h.ah = SETCURSOR;
  156.         regs.x.bx = video_page;
  157.         int86(VIDEO, ®s, ®s);
  158.         set_cursor_type(cursorshape[cs]);
  159.     }
  160. }
  161.  
  162. /* ------ make a normal cursor ------ */
  163. void normalcursor(void)
  164. {
  165.     set_cursor_type(0x0607);
  166. }
  167.  
  168. /* ------ hide the cursor ------ */
  169. void hidecursor(void)
  170. {
  171.     getcursor();
  172.     regs.h.ch |= HIDECURSOR;
  173.     regs.h.ah = SETCURSORTYPE;
  174.     int86(VIDEO, ®s, ®s);
  175. }
  176.  
  177. /* ------ unhide the cursor ------ */
  178. void unhidecursor(void)
  179. {
  180.     getcursor();
  181.     regs.h.ch &= ~HIDECURSOR;
  182.     regs.h.ah = SETCURSORTYPE;
  183.     int86(VIDEO, ®s, ®s);
  184. }
  185.  
  186. /* ---- use BIOS to set the cursor type ---- */
  187. void set_cursor_type(unsigned t)
  188. {
  189.     videomode();
  190.     regs.h.ah = SETCURSORTYPE;
  191.     regs.x.bx = video_page;
  192.     regs.x.cx = t;
  193.     int86(VIDEO, ®s, ®s);
  194. }
  195.  
  196. /* ---- test for EGA -------- */
  197. BOOL isEGA(void)
  198. {
  199.     if (isVGA())
  200.         return FALSE;
  201.     regs.h.ah = 0x12;
  202.     regs.h.bl = 0x10;
  203.     int86(VIDEO, ®s, ®s);
  204.     return regs.h.bl != 0x10;
  205. }
  206.  
  207. /* ---- test for VGA -------- */
  208. BOOL isVGA(void)
  209. {
  210.     regs.x.ax = 0x1a00;
  211.     int86(VIDEO, ®s, ®s);
  212.     return regs.h.al == 0x1a && regs.h.bl > 6;
  213. }
  214.  
  215. static void Scan350(void)
  216. {
  217.     regs.x.ax = 0x1201;
  218.     regs.h.bl = 0x30;
  219.     int86(VIDEO, ®s, ®s);
  220.     regs.h.ah = 0x0f;
  221.     int86(VIDEO, ®s, ®s);
  222.     regs.h.ah = 0x00;
  223.     int86(VIDEO, ®s, ®s);
  224. }
  225.  
  226. static void Scan400(void)
  227. {
  228.     regs.x.ax = 0x1202;
  229.     regs.h.bl = 0x30;
  230.     int86(VIDEO, ®s, ®s);
  231.     regs.h.ah = 0x0f;
  232.     int86(VIDEO, ®s, ®s);
  233.     regs.h.ah = 0x00;
  234.     int86(VIDEO, ®s, ®s);
  235. }
  236.  
  237. /* ---------- set 25 line mode ------- */
  238. void Set25(void)
  239. {
  240.     if (isVGA())    {
  241.         Scan400();
  242.         regs.x.ax = 0x1114;
  243.     }
  244.     else
  245.         regs.x.ax = 0x1111;
  246.     regs.h.bl = 0;
  247.     int86(VIDEO, ®s, ®s);
  248. }
  249.  
  250. /* ---------- set 43 line mode ------- */
  251. void Set43(void)
  252. {
  253.     if (isVGA())
  254.         Scan350();
  255.     regs.x.ax = 0x1112;
  256.     regs.h.bl = 0;
  257.     int86(VIDEO, ®s, ®s);
  258. }
  259.  
  260. /* ---------- set 50 line mode ------- */
  261. void Set50(void)
  262. {
  263.     if (isVGA())
  264.         Scan400();
  265.     regs.x.ax = 0x1112;
  266.     regs.h.bl = 0;
  267.     int86(VIDEO, ®s, ®s);
  268. }
  269.  
  270. /* ------ convert an Alt+ key to its letter equivalent ----- */
  271. int AltConvert(int c)
  272. {
  273.     int i, a = 0;
  274.     for (i = 0; i < 36; i++)
  275.         if (c == altconvert[i])
  276.             break;
  277.     if (i < 26)
  278.         a = 'a' + i;
  279.     else if (i < 36)
  280.         a = '0' + i - 26;
  281.     return a;
  282. }
  283.  
  284. #if MSC | WATCOM
  285. int getdisk(void)
  286. {
  287.     unsigned int cd;
  288.     _dos_getdrive(&cd);
  289.     cd -= 1;
  290.     return cd;
  291. }
  292. #endif
  293.  
  294. 
  295.