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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4.  *  VMGREMX.C; VidMgr module for the EMX GNU C/C++ compiler.  Release 1.2.
  5.  *
  6.  *  This module written in April 1996 by Andrew Clarke and released to the
  7.  *  public domain.  Last modified in June 1996.
  8.  */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <dos.h>
  13. #include <sys/video.h>
  14.  
  15. #define INCL_KBD
  16. #define INCL_VIO
  17. #define INCL_DOSPROCESS
  18.  
  19. #include <os2.h>
  20.  
  21. #include "vidmgr.h"
  22. #include "opsys.h"
  23.  
  24. static int vm_iscolorcard(void);
  25. static void vm_setcursorsize(char start, char end);
  26. static void vm_getcursorsize(char *start, char *end);
  27. static void vm_getkey(unsigned char *chScan, unsigned char *chChar);
  28.  
  29. static int video_init = 0;
  30.  
  31. #define vi_init()  { if (!video_init) video_init = v_init(); }
  32. #define vi_done()  { video_init = 0; }
  33.  
  34. void vm_init(void)
  35. {
  36.     vi_init();
  37.     vm_getinfo(&vm_startup);
  38.     vm_setattr(vm_startup.attr);
  39.     if (_osmode == DOS_MODE)
  40.     {
  41.         opsysDetect();
  42.     }
  43. }
  44.  
  45. void vm_done(void)
  46. {
  47.     vm_setcursorsize(vm_startup.cur_start, vm_startup.cur_end);
  48.     vi_done();
  49. }
  50.  
  51. void vm_getinfo(struct vm_info *v)
  52. {
  53.     v->ypos = vm_wherey();
  54.     v->xpos = vm_wherex();
  55.     v->attr = vm_getattrxy(v->xpos, v->ypos);
  56.     v->height = vm_getscreenheight();
  57.     v->width = vm_getscreenwidth();
  58.     vm_getcursorsize(&v->cur_start, &v->cur_end);
  59. }
  60.  
  61. char vm_getscreenwidth(void)
  62. {
  63.     if (_osmode == DOS_MODE)
  64.     {
  65.         int width, height;
  66.         vi_init();
  67.         v_dimen(&width, &height);
  68.         return (char)width;
  69.     }
  70.     else
  71.     {
  72.         VIOMODEINFO vi;
  73.         vi.cb = sizeof(VIOMODEINFO);
  74.         VioGetMode(&vi, 0);
  75.         return vi.col;
  76.     }
  77. }
  78.  
  79. char vm_getscreenheight(void)
  80. {
  81.     if (_osmode == DOS_MODE)
  82.     {
  83.         int width, height;
  84.         vi_init();
  85.         v_dimen(&width, &height);
  86.         return (char)height;
  87.     }
  88.     else
  89.     {
  90.         VIOMODEINFO vi;
  91.         vi.cb = sizeof(VIOMODEINFO);
  92.         VioGetMode(&vi, 0);
  93.         return vi.row;
  94.     }
  95. }
  96.  
  97. short vm_getscreensize(void)
  98. {
  99.     return (short)(vm_getscreenwidth() * vm_getscreenheight() * 2);
  100. }
  101.  
  102. char vm_wherex(void)
  103. {
  104.     if (_osmode == DOS_MODE)
  105.     {
  106.         int row, col;
  107.         vi_init();
  108.         v_getxy(&col, &row);
  109.         return (char)(col + 1);
  110.     }
  111.     else
  112.     {
  113.         USHORT row, col;
  114.         VioGetCurPos(&row, &col, 0);
  115.         return (char)(col + 1);
  116.     }
  117. }
  118.  
  119. char vm_wherey(void)
  120. {
  121.     if (_osmode == DOS_MODE)
  122.     {
  123.         int row, col;
  124.         vi_init();
  125.         v_getxy(&col, &row);
  126.         return (char)(row + 1);
  127.     }
  128.     else
  129.     {
  130.         USHORT row, col;
  131.         VioGetCurPos(&row, &col, 0);
  132.         return (char)(row + 1);
  133.     }
  134. }
  135.  
  136. static int vm_iscolorcard(void)
  137. {
  138.     int hw = v_hardware();
  139.     return hw == V_COLOR_8 || hw == V_COLOR_12 ? 1 : 0;
  140. }
  141.  
  142. void vm_gotoxy(char x, char y)
  143. {
  144.     if (_osmode == DOS_MODE)
  145.     {
  146.         vi_init();
  147.         v_gotoxy((int)(x - 1), (int)(y - 1));
  148.     }
  149.     else
  150.     {
  151.         VioSetCurPos((USHORT) (y - 1), (USHORT) (x - 1), 0);
  152.     }
  153. }
  154.  
  155. static void vm_setcursorsize(char start, char end)
  156. {
  157.     if (_osmode == DOS_MODE)
  158.     {
  159.         vi_init();
  160.         v_ctype(start, end);
  161.     }
  162.     else
  163.     {
  164.         VIOCURSORINFO vi;
  165.         vi.yStart = start;
  166.         vi.cEnd = end;
  167.         vi.cx = 0;
  168.         vi.attr = 0;
  169.         VioSetCurType(&vi, 0);
  170.     }
  171. }
  172.  
  173. static void vm_getcursorsize(char *start, char *end)
  174. {
  175.     if (_osmode == DOS_MODE)
  176.     {
  177.         int cstart, cend;
  178.         vi_init();
  179.         v_getctype(&cstart, &cend);
  180.         *start = (char)cstart;
  181.         *end = (char)cend;
  182.     }
  183.     else
  184.     {
  185.         VIOCURSORINFO vi;
  186.         VioGetCurType(&vi, 0);
  187.         *start = vi.yStart;
  188.         *end = vi.cEnd;
  189.     }
  190. }
  191.  
  192. static void vm_getkey(unsigned char *chScan, unsigned char *chChar)
  193. {
  194.     union REGS regs;
  195.     regs.h.ah = 0x00;
  196.     _int86(0x16, ®s, ®s);
  197.     *chScan = regs.h.ah;
  198.     *chChar = regs.h.al;
  199. }
  200.  
  201. int vm_kbhit(void)
  202. {
  203.     if (_osmode == DOS_MODE)
  204.     {
  205.         union REGS regs;
  206.         static unsigned short counter = 0;
  207.         if (counter % 10 == 0)
  208.         {
  209.             opsysTimeSlice();
  210.         }
  211.         counter++;
  212.         regs.h.ah = 0x01;
  213.         _int86(0x16, ®s, ®s);
  214.         return !(regs.x.flags & 0x40);
  215.     }
  216.     else
  217.     {
  218.         KBDKEYINFO ki;
  219.         ki.fbStatus = 0;
  220.         KbdPeek(&ki, 0);
  221.         return ki.fbStatus & KBDTRF_FINAL_CHAR_IN;
  222.     }
  223. }
  224.  
  225. int vm_getch(void)
  226. {
  227.     if (_osmode == DOS_MODE)
  228.     {
  229.         unsigned char chChar, chScan;
  230.  
  231.         while (!vm_kbhit())
  232.         {
  233.             /* nada */
  234.         }
  235.  
  236.         vm_getkey(&chScan, &chChar);
  237.         if (chChar == 0xe0)
  238.         {
  239.             if (chScan)
  240.             {
  241.                 chChar = 0;     /* force scan return */
  242.             }
  243.             else
  244.             {                   /* get next block */
  245.                 chChar = 0;
  246.  
  247.                 vm_getkey(&chScan, &chChar);
  248.                 if (!chScan)
  249.                 {               /* still no scan? */
  250.                     chScan = chChar;  /* move new char over */
  251.                     chChar = 0; /* force its return */
  252.                 }
  253.                 else
  254.                 {
  255.                     chChar = 0; /* force new scan */
  256.                 }
  257.             }
  258.         }
  259.         if (chScan == 0xe0)
  260.         {
  261.             if (!chChar)
  262.             {
  263.                 chScan = 0;
  264.  
  265.                 vm_getkey(&chScan, &chChar);
  266.                 if (!chScan)
  267.                 {               /* still no scan? */
  268.                     chScan = chChar;  /* move new char over */
  269.                     chChar = 0; /* force its return */
  270.                 }
  271.                 else
  272.                 {
  273.                     chChar = 0; /* force new scan */
  274.                 }
  275.             }
  276.             else
  277.             {
  278.                 chScan = 0;     /* handle 0xe00d case */
  279.             }
  280.         }
  281.         if (chChar)
  282.         {
  283.             chScan = 0;
  284.         }
  285.  
  286.         return (int)((chScan << 8) + (chChar));
  287.     }
  288.     else
  289.     {
  290.         KBDKEYINFO ki;
  291.  
  292.         ki.chChar = 0;
  293.         ki.chScan = 0;
  294.         KbdCharIn(&ki, IO_WAIT, 0);
  295.  
  296.         if (ki.chChar == 0xe0)
  297.         {
  298.             if (ki.chScan)
  299.             {
  300.                 ki.chChar = 0;  /* force scan return */
  301.             }
  302.             else
  303.             {                   /* get next block */
  304.                 ki.chChar = 0;
  305.                 KbdCharIn(&ki, IO_WAIT, 0);
  306.                 if (!ki.chScan)
  307.                 {               /* still no scan? */
  308.                     ki.chScan = ki.chChar;  /* move new char over */
  309.                     ki.chChar = 0;  /* force its return */
  310.                 }
  311.                 else
  312.                 {
  313.                     ki.chChar = 0;  /* force new scan */
  314.                 }
  315.             }
  316.         }
  317.         if (ki.chScan == 0xe0)
  318.         {
  319.             if (!ki.chChar)
  320.             {
  321.                 ki.chScan = 0;
  322.                 KbdCharIn(&ki, IO_WAIT, 0);
  323.                 if (!ki.chScan)
  324.                 {               /* still no scan? */
  325.                     ki.chScan = ki.chChar;  /* move new char over */
  326.                     ki.chChar = 0;  /* force its return */
  327.                 }
  328.                 else
  329.                 {
  330.                     ki.chChar = 0;  /* force new scan */
  331.                 }
  332.             }
  333.             else
  334.             {
  335.                 ki.chScan = 0;  /* handle 0xe00d case */
  336.             }
  337.         }
  338.         if (ki.chChar)
  339.         {
  340.             ki.chScan = 0;
  341.         }
  342.  
  343.         return (int)((ki.chScan << 8) + (ki.chChar));
  344.     }
  345. }
  346.  
  347. char vm_getchxy(char x, char y)
  348. {
  349.     if (_osmode == DOS_MODE)
  350.     {
  351.         char cell[2];
  352.         vi_init();
  353.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  354.         return *cell;
  355.     }
  356.     else
  357.     {
  358.         char cell[2];
  359.         USHORT len = sizeof cell;
  360.         VioReadCharStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  361.         return *cell;
  362.     }
  363. }
  364.  
  365. char vm_getattrxy(char x, char y)
  366. {
  367.     if (_osmode == DOS_MODE)
  368.     {
  369.         char cell[2];
  370.         vi_init();
  371.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  372.         return *(cell + 1);
  373.     }
  374.     else
  375.     {
  376.         char cell[2];
  377.         USHORT len = sizeof cell;
  378.         VioReadCellStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  379.         return *(cell + 1);
  380.     }
  381. }
  382.  
  383. void vm_xgetchxy(char x, char y, char *attr, char *ch)
  384. {
  385.     if (_osmode == DOS_MODE)
  386.     {
  387.         char cell[2];
  388.         vi_init();
  389.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  390.         *ch = *cell;
  391.         *attr = *(cell + 1);
  392.     }
  393.     else
  394.     {
  395.         char cell[2];
  396.         USHORT len = sizeof cell;
  397.         VioReadCellStr(cell, &len, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  398.         *ch = *cell;
  399.         *attr = *(cell + 1);
  400.     }
  401. }
  402.  
  403. void vm_setcursorstyle(int style)
  404. {
  405.     if (_osmode == DOS_MODE)
  406.     {
  407.         if (vm_iscolorcard())
  408.         {
  409.             switch (style)
  410.             {
  411.             case CURSORHALF:
  412.                 vm_setcursorsize(4, 7);
  413.                 break;
  414.             case CURSORFULL:
  415.                 vm_setcursorsize(0, 7);
  416.                 break;
  417.             case CURSORNORM:
  418.                 vm_setcursorsize(7, 8);
  419.                 break;
  420.             case CURSORHIDE:
  421.                 v_hidecursor();
  422.                 break;
  423.             default:
  424.                 break;
  425.             }
  426.         }
  427.         else
  428.         {
  429.             switch (style)
  430.             {
  431.             case CURSORHALF:
  432.                 vm_setcursorsize(8, 13);
  433.                 break;
  434.             case CURSORFULL:
  435.                 vm_setcursorsize(0, 13);
  436.                 break;
  437.             case CURSORNORM:
  438.                 vm_setcursorsize(11, 13);
  439.                 break;
  440.             case CURSORHIDE:
  441.                 vm_setcursorsize(32, 32);
  442.                 break;
  443.             default:
  444.                 break;
  445.             }
  446.         }
  447.     }
  448.     else
  449.     {
  450.         VIOCURSORINFO vi;
  451.  
  452.         switch (style)
  453.         {
  454.         case CURSORHALF:
  455.             vi.yStart = -50;
  456.             vi.cEnd = -100;
  457.             vi.cx = 0;
  458.             vi.attr = 0;
  459.             VioSetCurType(&vi, 0);
  460.             break;
  461.         case CURSORFULL:
  462.             vi.yStart = 0;
  463.             vi.cEnd = -100;
  464.             vi.cx = 0;
  465.             vi.attr = 0;
  466.             VioSetCurType(&vi, 0);
  467.             break;
  468.         case CURSORNORM:
  469.             vi.yStart = -90;
  470.             vi.cEnd = -100;
  471.             vi.cx = 0;
  472.             vi.attr = 0;
  473.             VioSetCurType(&vi, 0);
  474.             break;
  475.         case CURSORHIDE:
  476.             vi.yStart = -90;
  477.             vi.cEnd = -100;
  478.             vi.cx = 0;
  479.             vi.attr = -1;
  480.             VioSetCurType(&vi, 0);
  481.             break;
  482.         default:
  483.             break;
  484.         }
  485.     }
  486. }
  487.  
  488. void vm_putch(char x, char y, char ch)
  489. {
  490.     if (_osmode == DOS_MODE)
  491.     {
  492.         char cell[2];
  493.         vi_init();
  494.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  495.         *cell = ch;
  496.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  497.     }
  498.     else
  499.     {
  500.         VioWrtCharStr(&ch, 1, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  501.     }
  502. }
  503.  
  504. void vm_puts(char x, char y, char *str)
  505. {
  506.     if (_osmode == DOS_MODE)
  507.     {
  508.         vi_init();
  509.         while (*str)
  510.         {
  511.             vm_putch(x, y, *str);
  512.             str++;
  513.             x++;
  514.         }
  515.     }
  516.     else
  517.     {
  518.         VioWrtCharStr(str, (USHORT) strlen(str), (USHORT) (y - 1), (USHORT) (x - 1), 0);
  519.     }
  520. }
  521.  
  522. void vm_xputch(char x, char y, char attr, char ch)
  523. {
  524.     if (_osmode == DOS_MODE)
  525.     {
  526.         char cell[2];
  527.         vi_init();
  528.         *cell = ch;
  529.         *(cell + 1) = attr;
  530.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  531.     }
  532.     else
  533.     {
  534.         VioWrtCharStrAtt(&ch, 1, (USHORT) (y - 1), (USHORT) (x - 1), (PBYTE) &attr, 0);
  535.     }
  536. }
  537.  
  538. void vm_xputs(char x, char y, char attr, char *str)
  539. {
  540.     if (_osmode == DOS_MODE)
  541.     {
  542.         char *p, *cell, *pcell;
  543.         cell = malloc(strlen(str) * 2);
  544.         if (cell)
  545.         {
  546.             pcell = cell;
  547.             p = str;
  548.             while (*p)
  549.             {
  550.                 *pcell++ = *p++;
  551.                 *pcell++ = attr;
  552.             }
  553.             vi_init();
  554.             v_putline(cell, (int)(x - 1), (int)(y - 1), strlen(str));
  555.             free(cell);
  556.         }
  557.     }
  558.     else
  559.     {
  560.         VioWrtCharStrAtt(str, (USHORT) strlen(str), (USHORT) (y - 1), (USHORT) (x - 1), (PBYTE) &attr, 0);
  561.     }
  562. }
  563.  
  564. void vm_putattr(char x, char y, char attr)
  565. {
  566.     if (_osmode == DOS_MODE)
  567.     {
  568.         char cell[2];
  569.         vi_init();
  570.         v_getline(cell, (int)(x - 1), (int)(y - 1), 1);
  571.         *(cell + 1) = attr;
  572.         v_putline(cell, (int)(x - 1), (int)(y - 1), 1);
  573.     }
  574.     else
  575.     {
  576.         VioWrtNAttr((PBYTE) &attr, 1, (USHORT) (y - 1), (USHORT) (x - 1), 0);
  577.     }
  578. }
  579.  
  580. void vm_paintclearbox(char x1, char y1, char x2, char y2, char attr)
  581. {
  582.     if (_osmode == DOS_MODE)
  583.     {
  584.         char x, y;
  585.         for (y = y1; y <= y2; y++)
  586.         {
  587.             for (x = x1; x <= x2; x++)
  588.             {
  589.                 vm_xputch(x, y, attr, ' ');
  590.             }
  591.         }
  592.     }
  593.     else
  594.     {
  595.         char y, cell[2];
  596.         cell[0] = ' ';
  597.         cell[1] = attr;
  598.         for (y = y1; y <= y2; y++)
  599.         {
  600.             VioWrtNCell((PBYTE) &cell, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  601.         }
  602.     }
  603. }
  604.  
  605. void vm_paintbox(char x1, char y1, char x2, char y2, char attr)
  606. {
  607.     if (_osmode == DOS_MODE)
  608.     {
  609.         char x, y;
  610.         for (y = y1; y <= y2; y++)
  611.         {
  612.             for (x = x1; x <= x2; x++)
  613.             {
  614.                 vm_putattr(x, y, attr);
  615.             }
  616.         }
  617.     }
  618.     else
  619.     {
  620.         char y;
  621.         for (y = y1; y <= y2; y++)
  622.         {
  623.             VioWrtNAttr((PBYTE) &attr, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  624.         }
  625.     }
  626. }
  627.  
  628. void vm_clearbox(char x1, char y1, char x2, char y2)
  629. {
  630.     if (_osmode == DOS_MODE)
  631.     {
  632.         char x, y;
  633.         for (y = y1; y <= y2; y++)
  634.         {
  635.             for (x = x1; x <= x2; x++)
  636.             {
  637.                 vm_putch(x, y, ' ');
  638.             }
  639.         }
  640.     }
  641.     else
  642.     {
  643.         char y, ch;
  644.         ch = ' ';
  645.         for (y = y1; y <= y2; y++)
  646.         {
  647.             VioWrtNChar((PBYTE) &ch, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  648.         }
  649.     }
  650. }
  651.  
  652. void vm_fillbox(char x1, char y1, char x2, char y2, char ch)
  653. {
  654.     if (_osmode == DOS_MODE)
  655.     {
  656.         char x, y;
  657.         for (y = y1; y <= y2; y++)
  658.         {
  659.             for (x = x1; x <= x2; x++)
  660.             {
  661.                 vm_putch(x, y, ch);
  662.             }
  663.         }
  664.     }
  665.     else
  666.     {
  667.         char y;
  668.         for (y = y1; y <= y2; y++)
  669.         {
  670.             VioWrtNChar((PBYTE) &ch, (USHORT) (x2 - x1 + 1), (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  671.         }
  672.     }
  673. }
  674.  
  675. void vm_gettext(char x1, char y1, char x2, char y2, char *dest)
  676. {
  677.     if (_osmode == DOS_MODE)
  678.     {
  679.         char x, y;
  680.         for (y = y1; y <= y2; y++)
  681.         {
  682.             for (x = x1; x <= x2; x++)
  683.             {
  684.                 vm_xgetchxy(x, y, dest + 1, dest);
  685.                 dest += 2;
  686.             }
  687.         }
  688.     }
  689.     else
  690.     {
  691.         USHORT width;
  692.         char y;
  693.         width = (USHORT) ((x2 - x1 + 1) * 2);
  694.         for (y = y1; y <= y2; y++)
  695.         {
  696.             VioReadCellStr((PBYTE) dest, &width, (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  697.             dest += width;
  698.         }
  699.     }
  700. }
  701.  
  702. void vm_puttext(char x1, char y1, char x2, char y2, char *srce)
  703. {
  704.     if (_osmode == DOS_MODE)
  705.     {
  706.         char x, y;
  707.         for (y = y1; y <= y2; y++)
  708.         {
  709.             for (x = x1; x <= x2; x++)
  710.             {
  711.                 vm_xputch(x, y, *(srce + 1), *srce);
  712.                 srce += 2;
  713.             }
  714.         }
  715.     }
  716.     else
  717.     {
  718.         USHORT width;
  719.         char y;
  720.         width = (USHORT) ((x2 - x1 + 1) * 2);
  721.         for (y = y1; y <= y2; y++)
  722.         {
  723.             VioWrtCellStr((PBYTE) srce, width, (USHORT) (y - 1), (USHORT) (x1 - 1), 0);
  724.             srce += width;
  725.         }
  726.     }
  727. }
  728.  
  729. void vm_horizline(char x1, char x2, char row, char attr, char ch)
  730. {
  731.     if (_osmode == DOS_MODE)
  732.     {
  733.         char x;
  734.         for (x = x1; x <= x2; x++)
  735.         {
  736.             vm_xputch(x, row, attr, ch);
  737.         }
  738.     }
  739.     else
  740.     {
  741.         char cell[2];
  742.         cell[0] = ch;
  743.         cell[1] = attr;
  744.         VioWrtNCell((PBYTE) &cell, (USHORT) (x2 - x1 + 1), (USHORT) (row - 1), (USHORT) (x1 - 1), 0);
  745.     }
  746. }
  747.