home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / dflt14.zip / CONSOLE.C < prev    next >
Text File  |  1992-08-07  |  6KB  |  273 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. void SwapCursorStack(void)
  24. {
  25.     if (cs > 1)    {
  26.         swap(cursorpos[cs-2], cursorpos[cs-1]);
  27.         swap(cursorshape[cs-2], cursorshape[cs-1]);
  28.     }
  29. }
  30.  
  31. #ifndef MSC
  32. #ifndef WATCOM
  33. #define ZEROFLAG 0x40
  34. /* ---- Test for keystroke ---- */
  35. BOOL keyhit(void)
  36. {
  37.     _AH = 1;
  38.     geninterrupt(KEYBRD);
  39.     return (_FLAGS & ZEROFLAG) == 0;
  40. }
  41. #endif
  42. #endif
  43.  
  44. /* ---- Read a keystroke ---- */
  45. int getkey(void)
  46. {
  47.     int c;
  48.     while (keyhit() == FALSE)
  49.         ;
  50.     if (((c = bioskey(0)) & 0xff) == 0)
  51.         c = (c >> 8) | 0x1080;
  52.     else
  53.         c &= 0xff;
  54.     return c & 0x10ff;
  55. }
  56.  
  57. /* ---------- read the keyboard shift status --------- */
  58. int getshift(void)
  59. {
  60.     regs.h.ah = 2;
  61.     int86(KEYBRD, ®s, ®s);
  62.     return regs.h.al;
  63. }
  64.  
  65. static int far *clk = MK_FP(0x40,0x6c);
  66. /* ------- macro to wait one clock tick -------- */
  67. #define wait()          \
  68. {                       \
  69.     int now = *clk;     \
  70.     while (now == *clk) \
  71.         ;               \
  72. }
  73.  
  74. /* -------- sound a buzz tone ---------- */
  75. void beep(void)
  76. {
  77.     wait();
  78.     outp(0x43, 0xb6);               /* program the frequency */
  79.     outp(0x42, (int) (COUNT % 256));
  80.     outp(0x42, (int) (COUNT / 256));
  81.     outp(0x61, inp(0x61) | 3);      /* start the sound */
  82.     wait();
  83.     outp(0x61, inp(0x61) & ~3);     /* stop the sound  */
  84. }
  85.  
  86. /* -------- get the video mode and page from BIOS -------- */
  87. void videomode(void)
  88. {
  89.     regs.h.ah = 15;
  90.     int86(VIDEO, ®s, ®s);
  91.     video_mode = regs.h.al;
  92.     video_page = regs.x.bx;
  93.     video_page &= 0xff00;
  94.     video_mode &= 0x7f;
  95. }
  96.  
  97. /* ------ position the cursor ------ */
  98. void cursor(int x, int y)
  99. {
  100.     videomode();
  101.     regs.x.dx = ((y << 8) & 0xff00) + x;
  102.     regs.h.ah = SETCURSOR;
  103.     regs.x.bx = video_page;
  104.     int86(VIDEO, ®s, ®s);
  105. }
  106.  
  107. /* ------ get cursor shape and position ------ */
  108. static void near getcursor(void)
  109. {
  110.     videomode();
  111.     regs.h.ah = READCURSOR;
  112.     regs.x.bx = video_page;
  113.     int86(VIDEO, ®s, ®s);
  114. }
  115.  
  116. /* ------- get the current cursor position ------- */
  117. void curr_cursor(int *x, int *y)
  118. {
  119.     getcursor();
  120.     *x = regs.h.dl;
  121.     *y = regs.h.dh;
  122. }
  123.  
  124. /* ------ save the current cursor configuration ------ */
  125. void savecursor(void)
  126. {
  127.     if (cs < MAXSAVES)    {
  128.         getcursor();
  129.         cursorshape[cs] = regs.x.cx;
  130.         cursorpos[cs] = regs.x.dx;
  131.         cs++;
  132.     }
  133. }
  134.  
  135. /* ---- restore the saved cursor configuration ---- */
  136. void restorecursor(void)
  137. {
  138.     if (cs)    {
  139.         --cs;
  140.         videomode();
  141.         regs.x.dx = cursorpos[cs];
  142.         regs.h.ah = SETCURSOR;
  143.         regs.x.bx = video_page;
  144.         int86(VIDEO, ®s, ®s);
  145.         set_cursor_type(cursorshape[cs]);
  146.     }
  147. }
  148.  
  149. /* ------ make a normal cursor ------ */
  150. void normalcursor(void)
  151. {
  152.     set_cursor_type(0x0607);
  153. }
  154.  
  155. /* ------ hide the cursor ------ */
  156. void hidecursor(void)
  157. {
  158.     getcursor();
  159.     regs.h.ch |= HIDECURSOR;
  160.     regs.h.ah = SETCURSORTYPE;
  161.     int86(VIDEO, ®s, ®s);
  162. }
  163.  
  164. /* ------ unhide the cursor ------ */
  165. void unhidecursor(void)
  166. {
  167.     getcursor();
  168.     regs.h.ch &= ~HIDECURSOR;
  169.     regs.h.ah = SETCURSORTYPE;
  170.     int86(VIDEO, ®s, ®s);
  171. }
  172.  
  173. /* ---- use BIOS to set the cursor type ---- */
  174. void set_cursor_type(unsigned t)
  175. {
  176.     videomode();
  177.     regs.h.ah = SETCURSORTYPE;
  178.     regs.x.bx = video_page;
  179.     regs.x.cx = t;
  180.     int86(VIDEO, ®s, ®s);
  181. }
  182.  
  183. /* ---- test for EGA -------- */
  184. BOOL isEGA(void)
  185. {
  186.     if (isVGA())
  187.         return FALSE;
  188.     regs.h.ah = 0x12;
  189.     regs.h.bl = 0x10;
  190.     int86(VIDEO, ®s, ®s);
  191.     return regs.h.bl != 0x10;
  192. }
  193.  
  194. /* ---- test for VGA -------- */
  195. BOOL isVGA(void)
  196. {
  197.     regs.x.ax = 0x1a00;
  198.     int86(VIDEO, ®s, ®s);
  199.     return regs.h.al == 0x1a && regs.h.bl > 6;
  200. }
  201.  
  202. static void Scan350(void)
  203. {
  204.     regs.x.ax = 0x1201;
  205.     regs.h.bl = 0x30;
  206.     int86(VIDEO, ®s, ®s);
  207. }
  208.  
  209. static void Scan400(void)
  210. {
  211.     regs.x.ax = 0x1202;
  212.     regs.h.bl = 0x30;
  213.     int86(VIDEO, ®s, ®s);
  214. }
  215.  
  216. /* ---------- set 25 line mode ------- */
  217. void Set25(void)
  218. {
  219.     if (isVGA())    {
  220.         Scan400();
  221.         regs.x.ax = 0x1114;
  222.     }
  223.     else
  224.         regs.x.ax = 0x1111;
  225.     regs.x.bx = 0;
  226.     int86(VIDEO, ®s, ®s);
  227. }
  228.  
  229. /* ---------- set 43 line mode ------- */
  230. void Set43(void)
  231. {
  232.     if (isVGA())
  233.         Scan350();
  234.     regs.x.ax = 0x1112;
  235.     regs.x.bx = 0;
  236.     int86(VIDEO, ®s, ®s);
  237. }
  238.  
  239. /* ---------- set 50 line mode ------- */
  240. void Set50(void)
  241. {
  242.     if (isVGA())
  243.         Scan400();
  244.     regs.x.ax = 0x1112;
  245.     regs.x.bx = 0;
  246.     int86(VIDEO, ®s, ®s);
  247. }
  248.  
  249. /* ------ convert an Alt+ key to its letter equivalent ----- */
  250. int AltConvert(int c)
  251. {
  252.     int i, a = 0;
  253.     for (i = 0; i < 36; i++)
  254.         if (c == altconvert[i])
  255.             break;
  256.     if (i < 26)
  257.         a = 'a' + i;
  258.     else if (i < 36)
  259.         a = '0' + i - 26;
  260.     return a;
  261. }
  262.  
  263. #if MSC | WATCOM
  264. int getdisk(void)
  265. {
  266.     unsigned int cd;
  267.     _dos_getdrive(&cd);
  268.     cd -= 1;
  269.     return cd;
  270. }
  271. #endif
  272.  
  273.