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