home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / VMGRDJGP.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  9KB  |  421 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.  *  VMGRDJGP.C; VidMgr module for the DJGPP GNU C/C++ compiler.  Release 1.2.
  5.  *
  6.  *  This module written in May 1996 by Andrew Clarke and released to the
  7.  *  public domain.
  8.  */
  9.  
  10. #include <dos.h>
  11. #include <go32.h>
  12. #include <sys/farptr.h>
  13. #include "vidmgr.h"
  14. #include "opsys.h"
  15.  
  16. static int vm_iscolorcard(void);
  17. static char vm_getscreenmode(void);
  18. static void vm_setscreenmode(char mode);
  19. static void vm_setcursorsize(char start, char end);
  20. static void vm_getcursorsize(char *start, char *end);
  21. static void vm_getkey(unsigned char *chScan, unsigned char *chChar);
  22. static unsigned long vm_screenaddress(char x, char y);
  23.  
  24. void vm_init(void)
  25. {
  26.     vm_getinfo(&vm_startup);
  27.     vm_setattr(vm_startup.attr);
  28.     opsysDetect();
  29. }
  30.  
  31. void vm_done(void)
  32. {
  33.     if (vm_getscreenmode() != vm_startup.mode)
  34.     {
  35.         vm_setscreenmode(vm_startup.mode);
  36.     }
  37.     vm_setcursorsize(vm_startup.cur_start, vm_startup.cur_end);
  38. }
  39.  
  40. void vm_getinfo(struct vm_info *v)
  41. {
  42.     v->ypos = vm_wherey();
  43.     v->xpos = vm_wherex();
  44.     v->attr = vm_getattrxy(v->xpos, v->ypos);
  45.     v->mode = vm_getscreenmode();
  46.     v->height = vm_getscreenheight();
  47.     v->width = vm_getscreenwidth();
  48.     vm_getcursorsize(&v->cur_start, &v->cur_end);
  49. }
  50.  
  51. static int vm_iscolorcard(void)
  52. {
  53.     return vm_getscreenmode() != 7;
  54. }
  55.  
  56. static char vm_getscreenmode(void)
  57. {
  58.     return (char)_farpeekb(_go32_conventional_mem_selector(), 0x0449);
  59. }
  60.  
  61. static void vm_setscreenmode(char mode)
  62. {
  63.     union REGS regs;
  64.     regs.h.ah = 0x00;
  65.     regs.h.al = mode;
  66.     int86(0x10, ®s, ®s);
  67. }
  68.  
  69. char vm_getscreenwidth(void)
  70. {
  71.     return (char)_farpeekb(_go32_conventional_mem_selector(), 0x044a);
  72. }
  73.  
  74. char vm_getscreenheight(void)
  75. {
  76.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0484) + 1);
  77. }
  78.  
  79. short vm_getscreensize(void)
  80. {
  81.     return (short)_farpeekw(_go32_conventional_mem_selector(), 0x044c);
  82. }
  83.  
  84. void vm_gotoxy(char x, char y)
  85. {
  86.     union REGS regs;
  87.     regs.h.ah = 0x02;
  88.     regs.h.bh = 0;
  89.     regs.h.dh = (unsigned char)(y - 1);
  90.     regs.h.dl = (unsigned char)(x - 1);
  91.     int86(0x10, ®s, ®s);
  92. }
  93.  
  94. char vm_wherex(void)
  95. {
  96.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0450) + 1);
  97. }
  98.  
  99. char vm_wherey(void)
  100. {
  101.     return (char)(_farpeekb(_go32_conventional_mem_selector(), 0x0451) + 1);
  102. }
  103.  
  104. static void vm_setcursorsize(char start, char end)
  105. {
  106.     union REGS regs;
  107.     regs.h.ah = 0x01;
  108.     regs.h.ch = start;
  109.     regs.h.cl = end;
  110.     int86(0x10, ®s, ®s);
  111. }
  112.  
  113. static void vm_getcursorsize(char *start, char *end)
  114. {
  115.     union REGS regs;
  116.     regs.h.ah = 0x03;
  117.     regs.h.bh = 0;
  118.     int86(0x10, ®s, ®s);
  119.     *start = regs.h.ch;
  120.     *end = regs.h.cl;
  121. }
  122.  
  123. int vm_kbhit(void)
  124. {
  125.     union REGS regs;
  126.     static unsigned short counter = 0;
  127.     if (counter % 10 == 0)
  128.     {
  129.         opsysTimeSlice();
  130.     }
  131.     counter++;
  132.     regs.h.ah = 0x01;
  133.     int86(0x16, ®s, ®s);
  134.     return !(regs.x.flags & 0x40);
  135. }
  136.  
  137. static void vm_getkey(unsigned char *chScan, unsigned char *chChar)
  138. {
  139.     union REGS regs;
  140.     regs.h.ah = 0x00;
  141.     int86(0x16, ®s, ®s);
  142.     *chScan = regs.h.ah;
  143.     *chChar = regs.h.al;
  144. }
  145.  
  146. int vm_getch(void)
  147. {
  148.     unsigned char chChar, chScan;
  149.  
  150.     while (!vm_kbhit())
  151.     {
  152.         /* nada */
  153.     }
  154.  
  155.     vm_getkey(&chScan, &chChar);
  156.     if (chChar == 0xe0)
  157.     {
  158.         if (chScan)
  159.         {
  160.             chChar = 0;         /* force scan return */
  161.         }
  162.         else
  163.         {                       /* get next block */
  164.             chChar = 0;
  165.  
  166.             vm_getkey(&chScan, &chChar);
  167.             if (!chScan)
  168.             {                   /* still no scan? */
  169.                 chScan = chChar;  /* move new char over */
  170.                 chChar = 0;     /* force its return */
  171.             }
  172.             else
  173.             {
  174.                 chChar = 0;     /* force new scan */
  175.             }
  176.         }
  177.     }
  178.     if (chScan == 0xe0)
  179.     {
  180.         if (!chChar)
  181.         {
  182.             chScan = 0;
  183.  
  184.             vm_getkey(&chScan, &chChar);
  185.             if (!chScan)
  186.             {                   /* still no scan? */
  187.                 chScan = chChar;  /* move new char over */
  188.                 chChar = 0;     /* force its return */
  189.             }
  190.             else
  191.             {
  192.                 chChar = 0;     /* force new scan */
  193.             }
  194.         }
  195.         else
  196.         {
  197.             chScan = 0;         /* handle 0xe00d case */
  198.         }
  199.     }
  200.     if (chChar)
  201.     {
  202.         chScan = 0;
  203.     }
  204.  
  205.     return (int)((chScan << 8) + (chChar));
  206. }
  207.  
  208. void vm_setcursorstyle(int style)
  209. {
  210.     if (vm_iscolorcard())
  211.     {
  212.         switch (style)
  213.         {
  214.         case CURSORHALF:
  215.             vm_setcursorsize(4, 7);
  216.             break;
  217.         case CURSORFULL:
  218.             vm_setcursorsize(0, 7);
  219.             break;
  220.         case CURSORNORM:
  221.             vm_setcursorsize(7, 8);
  222.             break;
  223.         case CURSORHIDE:
  224.             vm_setcursorsize(32, 32);
  225.             break;
  226.         default:
  227.             break;
  228.         }
  229.     }
  230.     else
  231.     {
  232.         switch (style)
  233.         {
  234.         case CURSORHALF:
  235.             vm_setcursorsize(8, 13);
  236.             break;
  237.         case CURSORFULL:
  238.             vm_setcursorsize(0, 13);
  239.             break;
  240.         case CURSORNORM:
  241.             vm_setcursorsize(11, 13);
  242.             break;
  243.         case CURSORHIDE:
  244.             vm_setcursorsize(32, 32);
  245.             break;
  246.         default:
  247.             break;
  248.         }
  249.     }
  250. }
  251.  
  252. static unsigned long vm_screenaddress(char x, char y)
  253. {
  254.     unsigned short base;
  255.     if (vm_iscolorcard())
  256.     {
  257.         base = opsysGetVideoSeg(0xB800);
  258.     }
  259.     else
  260.     {
  261.         base = opsysGetVideoSeg(0xB000);
  262.     }
  263.     return (unsigned long)((base << 4) + ((y - 1) * vm_getscreenwidth() * 2) + ((x - 1) * 2));
  264. }
  265.  
  266. char vm_getchxy(char x, char y)
  267. {
  268.     unsigned long address;
  269.     address = vm_screenaddress(x, y);
  270.     return (char)_farpeekb(_go32_conventional_mem_selector(), address);
  271. }
  272.  
  273. char vm_getattrxy(char x, char y)
  274. {
  275.     unsigned long address;
  276.     address = vm_screenaddress(x, y);
  277.     return (char)_farpeekb(_go32_conventional_mem_selector(), address + 1L);
  278. }
  279.  
  280. void vm_xgetchxy(char x, char y, char *attr, char *ch)
  281. {
  282.     unsigned long address;
  283.     address = vm_screenaddress(x, y);
  284.     *ch = (char)_farpeekb(_go32_conventional_mem_selector(), address);
  285.     *attr = (char)_farpeekb(_go32_conventional_mem_selector(), address + 1L);
  286. }
  287.  
  288. void vm_putch(char x, char y, char ch)
  289. {
  290.     unsigned long address;
  291.     address = vm_screenaddress(x, y);
  292.     _farpokeb(_go32_conventional_mem_selector(), address, ch);
  293. }
  294.  
  295. void vm_puts(char x, char y, char *str)
  296. {
  297.     char ofs;
  298.     ofs = 0;
  299.     while (*str)
  300.     {
  301.         unsigned long address;
  302.         address = vm_screenaddress(x + ofs, y);
  303.         _farpokeb(_go32_conventional_mem_selector(), address, *str);
  304.         str++;
  305.         ofs++;
  306.     }
  307. }
  308.  
  309. void vm_xputch(char x, char y, char attr, char ch)
  310. {
  311.     unsigned long address;
  312.     address = vm_screenaddress(x, y);
  313.     _farpokeb(_go32_conventional_mem_selector(), address, ch);
  314.     _farpokeb(_go32_conventional_mem_selector(), address + 1L, attr);
  315. }
  316.  
  317. void vm_xputs(char x, char y, char attr, char *str)
  318. {
  319.     char ofs;
  320.     ofs = 0;
  321.     while (*str)
  322.     {
  323.         unsigned long address;
  324.         address = vm_screenaddress(x + ofs, y);
  325.         _farnspokeb(address, *str);
  326.         _farnspokeb(address + 1L, attr);
  327.         str++;
  328.         ofs++;
  329.     }
  330. }
  331.  
  332. void vm_putattr(char x, char y, char attr)
  333. {
  334.     unsigned long address;
  335.     address = vm_screenaddress(x, y);
  336.     _farpokeb(_go32_conventional_mem_selector(), address + 1L, attr);
  337. }
  338.  
  339. void vm_paintclearbox(char x1, char y1, char x2, char y2, char attr)
  340. {
  341.     char x, y;
  342.     for (y = y1; y <= y2; y++)
  343.     {
  344.         for (x = x1; x <= x2; x++)
  345.         {
  346.             vm_xputch(x, y, attr, ' ');
  347.         }
  348.     }
  349. }
  350.  
  351. void vm_paintbox(char x1, char y1, char x2, char y2, char attr)
  352. {
  353.     char x, y;
  354.     for (y = y1; y <= y2; y++)
  355.     {
  356.         for (x = x1; x <= x2; x++)
  357.         {
  358.             vm_putattr(x, y, attr);
  359.         }
  360.     }
  361. }
  362.  
  363. void vm_clearbox(char x1, char y1, char x2, char y2)
  364. {
  365.     char x, y;
  366.     for (y = y1; y <= y2; y++)
  367.     {
  368.         for (x = x1; x <= x2; x++)
  369.         {
  370.             vm_putch(x, y, ' ');
  371.         }
  372.     }
  373. }
  374.  
  375. void vm_fillbox(char x1, char y1, char x2, char y2, char ch)
  376. {
  377.     char x, y;
  378.     for (y = y1; y <= y2; y++)
  379.     {
  380.         for (x = x1; x <= x2; x++)
  381.         {
  382.             vm_putch(x, y, ch);
  383.         }
  384.     }
  385. }
  386.  
  387. void vm_gettext(char x1, char y1, char x2, char y2, char *dest)
  388. {
  389.     char x, y;
  390.     for (y = y1; y <= y2; y++)
  391.     {
  392.         for (x = x1; x <= x2; x++)
  393.         {
  394.             vm_xgetchxy(x, y, dest + 1, dest);
  395.             dest += 2;
  396.         }
  397.     }
  398. }
  399.  
  400. void vm_puttext(char x1, char y1, char x2, char y2, char *srce)
  401. {
  402.     char x, y;
  403.     for (y = y1; y <= y2; y++)
  404.     {
  405.         for (x = x1; x <= x2; x++)
  406.         {
  407.             vm_xputch(x, y, *(srce + 1), *srce);
  408.             srce += 2;
  409.         }
  410.     }
  411. }
  412.  
  413. void vm_horizline(char x1, char x2, char row, char attr, char ch)
  414. {
  415.     char x;
  416.     for (x = x1; x <= x2; x++)
  417.     {
  418.         vm_xputch(x, row, attr, ch);
  419.     }
  420. }
  421.