home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / dflat5 / console.c < prev    next >
Text File  |  1991-06-25  |  6KB  |  264 lines

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