home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emx / test / vio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  4.7 KB  |  215 lines

  1. /* vio.c (emx+gcc) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define INCL_DOSPROCESS
  7. #define INCL_VIO
  8. #define INCL_KBD
  9. #include <os2.h>
  10.  
  11. /* These variables must not cross a 64KB boundary */
  12. /* Making them auto variables is dangerous */
  13.  
  14. static VIOMODEINFO vmi;
  15. static VIOPHYSBUF vpb;
  16. static VIOMODEINFO graph_mode;
  17. static VIOCONFIGINFO config;
  18. static VIOMODEINFO org_mode;
  19. static VIOPHYSBUF phys;
  20. static KBDKEYINFO key;
  21. static char msg[] = "Hello world!\r\n";
  22.  
  23. static void usage (void)
  24. {
  25.   puts ("Usage: vio [-gmp]");
  26.   exit (1);
  27. }
  28.  
  29.  
  30. static void test_physbuf (void)
  31. {
  32.   USHORT rc;
  33.   unsigned char *p;
  34.   int i;
  35.  
  36.   vpb.pBuf = (PBYTE)0xb8000;
  37.   vpb.cb = 80*25*2;
  38.   rc = VioGetPhysBuf (&vpb, 0);
  39.   if (rc == ERROR_VIO_EXTENDED_SG)
  40.     {
  41.       fputs ("Must run in a full-screen session.\n", stderr);
  42.       exit (2);
  43.     }
  44.   if (rc != 0)
  45.     {
  46.       fprintf (stderr, "VioGetPhysBuf failed, rc=%u\n", (unsigned)rc);
  47.       exit (2);
  48.     }
  49.   p = MAKEP (vpb.asel[0], 0);
  50.   for (i = 0; i < 80*25; ++i)
  51.     {
  52.       *p ^= 1;
  53.       p += 2;
  54.     }
  55. }
  56.  
  57.  
  58. static void test_hello (void)
  59. {
  60.   VioWrtTTY (msg, strlen (msg), 0);
  61. }
  62.  
  63.  
  64. static void test_getmode (void)
  65. {
  66.   USHORT rc;
  67.  
  68.   vmi.cb = sizeof (vmi);
  69.   rc = VioGetMode (&vmi, 0);
  70.   if (rc != 0)
  71.     {
  72.       fprintf (stderr, "VioGetMode failed, rc=%u\n", (unsigned)rc);
  73.       exit (2);
  74.     }
  75.   printf ("Display: %s\n", (vmi.fbType == 0 ? "monochrome" : "color"));
  76.   printf ("Colors:  %d\n", 1 << vmi.color);
  77.   printf ("Columns: %d\n", vmi.col);
  78.   printf ("Rows:    %d\n", vmi.row);
  79.   printf ("Hres:    %d\n", vmi.hres);
  80.   printf ("Vres:    %d\n", vmi.vres);
  81. }
  82.  
  83.  
  84. static char *graph_phys;
  85. static char graph_save_buf[0x10000];
  86. static int graph_saved = 0;
  87. static int graph_on = 0;
  88.  
  89.  
  90. static void graph_restore (void)
  91. {
  92.   if (graph_saved)
  93.     {
  94.       VioSetMode (&graph_mode, 0);
  95.       memcpy (graph_phys, graph_save_buf, 0x10000);
  96.     }
  97. }
  98.  
  99.  
  100. static void redraw_thread (void *arg)
  101. {
  102.   USHORT notify;
  103.  
  104.   for (;;)
  105.     {
  106.       if (VioSavRedrawWait (VSRWI_SAVEANDREDRAW, ¬ify, 0) != 0)
  107.         _endthread ();
  108.       DosBeep ((notify == VSRWN_SAVE ? 200 : 400), 200);
  109.       if (graph_on)
  110.         {
  111.           if (notify == VSRWN_SAVE)
  112.             {
  113.               memcpy (graph_save_buf, graph_phys, 0x10000);
  114.               graph_saved = 1;
  115.             }
  116.           else 
  117.             graph_restore ();
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. static void mode_thread (void *arg)
  124. {
  125.   USHORT rc, notify;
  126.   
  127.   for (;;)
  128.     {
  129.       rc = VioModeWait (VMWR_POPUP, ¬ify, 0);
  130.       if (rc != 0)
  131.         _endthread ();
  132.       DosBeep (1000, 200);
  133.       if (graph_on)
  134.         graph_restore ();
  135.     }
  136. }
  137.  
  138.  
  139. static void test_graphics (void)
  140. {
  141.   USHORT rc;
  142.   BYTE not_locked;
  143.   BYTE *p;
  144.   int i, j;
  145.  
  146.   config.cb = sizeof (config);
  147.   rc = VioGetConfig (VIO_CONFIG_CURRENT, &config, 0);
  148.   if (rc != 0)
  149.     {
  150.       fprintf (stderr, "VioGetConfig failed, rc=%d\n", (unsigned)rc);
  151.       exit (2);
  152.     }
  153.   if (config.adapter != DISPLAY_VGA || config.adapter == MONITOR_MONOCHROME)
  154.     {
  155.       fputs ("Need VGA with color monitor\n", stderr);
  156.       exit (2);
  157.     }
  158.   org_mode.cb = sizeof (org_mode);
  159.   VioGetMode (&org_mode, 0);
  160.   memset (&graph_mode, 0, sizeof (graph_mode));
  161.   graph_mode.cb = sizeof (graph_mode);
  162.   graph_mode.fbType = VGMT_OTHER | VGMT_GRAPHICS;
  163.   graph_mode.color = 8;
  164.   graph_mode.col = 80;
  165.   graph_mode.row = 25;
  166.   graph_mode.hres = 320;
  167.   graph_mode.vres = 200;
  168.   rc = VioSetMode (&graph_mode, 0);
  169.   if (rc != 0)
  170.     {
  171.       fprintf (stderr, "VioSetMode failed, rc=%d\n", (unsigned)rc);
  172.       exit (2);
  173.     }
  174.   phys.pBuf = (PBYTE)0xa0000;
  175.   phys.cb = 0x10000;
  176.   rc = VioGetPhysBuf (&phys, 0);
  177.   if (rc != 0)
  178.     {
  179.       VioSetMode (&org_mode, 0);
  180.       fprintf (stderr, "VioGetPhysBuf failed, rc=%d\n", (unsigned)rc);
  181.       exit (2);
  182.     }
  183.   graph_phys = MAKEP (phys.asel[0], 0);
  184.   graph_on = 1;
  185.   _beginthread (redraw_thread, NULL, 0x4000, NULL);
  186.   _beginthread (mode_thread, NULL, 0x4000, NULL);
  187.   p = graph_phys;
  188.   for (j = 0;; ++j)
  189.     {
  190.       VioScrLock (LOCKIO_WAIT, ¬_locked, 0);
  191.       for (i = 0; i < 320*200; ++i)
  192.         p[i] = ((i+j) >> 8) & 0xff;
  193.       VioScrUnLock (0);
  194.       if (KbdCharIn (&key, IO_NOWAIT, 0) == 0 && key.fbStatus != 0)
  195.         break;
  196.     }
  197.   VioSetMode (&org_mode, 0);
  198. }
  199.  
  200.  
  201. int main (int argc, char *argv[])
  202. {
  203.   if (argc == 2 && strcmp (argv[1], "-p") == 0)
  204.     test_physbuf ();
  205.   else if (argc == 2 && strcmp (argv[1], "-g") == 0)
  206.     test_graphics ();
  207.   else if (argc == 2 && strcmp (argv[1], "-m") == 0)
  208.     test_getmode ();
  209.   else if (argc == 1)
  210.     test_hello ();
  211.   else
  212.     usage ();
  213.   return (0);
  214. }
  215.