home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / SCCHGDEV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  5.0 KB  |  160 lines

  1. /**
  2. *
  3. * Name        scchgdev -- Switch to color or monochrome adapter
  4. *
  5. * Synopsis    ercode = scchgdev(device);
  6. *
  7. *        int ercode      Returned error code:
  8. *                  1 if device absent or if it must first
  9. *                    be reset with SCNEWDEV;
  10. *                  0 if ok.
  11. *        int device      The device to select (SC_MONO or
  12. *                  SC_COLOR).
  13. *
  14. * Description    SCCHGDEV selects a video adapter for future I/O without
  15. *        resetting it.  This includes testing for the presence of
  16. *        the proper device, setting the BIOS equipment word,
  17. *        restoring the BIOS video state variables, and setting
  18. *        the global variables b_device and b_curpage, to restore
  19. *        the saved state of the new device.
  20. *
  21. *        If the requested device is not the current device, then
  22. *        information from the b_adap_state table is used to
  23. *        restore the BIOS video state information.  (BSCREENS.H
  24. *        declares b_adap_state.)  This information is available
  25. *        only if the BIOS state variables for the requested
  26. *        device have been previously stored by SCCHGDEV or
  27. *        SCNEWDEV.  This implies that
  28. *
  29. *            (a) The device that is current when the program is
  30. *            started can be selected at any time by SCCHGDEV
  31. *            or SCNEWDEV.
  32. *            (b) The other device (if present) must first be
  33. *            reset at least once by SCNEWDEV before it is
  34. *            selected by SCCHGDEV.  Otherwise SCCHGDEV will
  35. *            report an error.
  36. *
  37. * Returns    ercode          Returned error code:
  38. *                  1 if device absent or if it must first
  39. *                    be reset with SCNEWDEV;
  40. *                  0 if ok.
  41. *        b_device       Adapter used (SC_COLOR or SC_MONO).
  42. *
  43. * Version    6.00 (C)Copyright Blaise Computing Inc.  1986-1989
  44. *
  45. **/
  46.  
  47. #include <dos.h>
  48.  
  49. #include <bscreens.h>
  50.  
  51. extern int b_vidcpy(int,int);          /* Internal (see SCNEWDEV.C).   */
  52.  
  53. #define  FROM_BIOS    0          /* Direction flags for copying  */
  54. #define  TO_BIOS    1          /* BIOS video state variables.  */
  55.  
  56. #define  VIDEO_FIELD  0x0030          /* The bits to be modified in   */
  57.                       /* the BIOS equipment word.     */
  58. #define  MONO_FLAG    0x0030
  59. #define  COLOR_80     0x0020
  60. #define  COLOR_40     0x0010
  61.  
  62.                   /* Address of BIOS equipment word.      */
  63. #define  EQUIP_WORD_LOC  (uttofar(0x0040,0x0010,unsigned int))
  64.  
  65. int scchgdev(device)
  66. int device;
  67. {
  68.     int old_mode,old_columns,old_act_page;  /* Previous state          */
  69.     int old_dev;
  70.     union REGS regs;
  71.  
  72. #define our_word    (regs.x.ax)       /* Our copy of equipment word.  */
  73.  
  74.     int  old_flag,new_flag;          /* Old and new values of video  */
  75.                       /* device bit field.          */
  76.  
  77.     if (device != SC_MONO && device != SC_COLOR)
  78.     return 1;              /* Unknown device.          */
  79.  
  80.     /* Note current state of BIOS.                      */
  81.  
  82.     old_dev = scmode(&old_mode,&old_columns,&old_act_page);
  83.  
  84.     if (device == old_dev)
  85.     return 0;              /* Nothing to do.           */
  86.  
  87.     /* Record current BIOS variables in case we ever need to change   */
  88.     /* back to the old device.                          */
  89.  
  90.     if (b_vidcpy(FROM_BIOS,old_dev))
  91.     return 1;
  92.  
  93.     if (!b_adap_state[device].known)  /* Abort if we've never         */
  94.     return 1;              /* recorded the BIOS state      */
  95.                       /* variables for new device.    */
  96.  
  97.     scequip();                  /* Sense which hardware is      */
  98.                       /* present.              */
  99.  
  100.     switch (device)
  101.     {
  102.     case SC_COLOR:
  103.         if (   b_cga != SC_COLOR  && b_pcmodel != IBM_JR
  104.         && b_ega != SC_COLOR  && b_pgc       != SC_COLOR
  105.         && b_vga == SC_ABSENT && b_mcga    == SC_ABSENT)
  106.         return 1;          /* Color/Graphics Adapter       */
  107.                       /* functions are not available. */
  108.         new_flag = COLOR_80;
  109.         break;
  110.     case SC_MONO:
  111.         if (   b_mdpa != SC_MONO
  112.         && b_ega  != SC_MONO
  113.         && b_vga  == SC_ABSENT)
  114.         return 1;          /* Monochrome Adapter functions */
  115.                       /* are not available.          */
  116.         new_flag = MONO_FLAG;
  117.         break;
  118.     }
  119.  
  120.     b_device = device;
  121.  
  122.     /* If we are switching VGA from color to monochrome or vice       */
  123.     /* versa, note the change in our global variables.              */
  124.  
  125.     if (   device != b_mdpa && device != b_cga && device != b_ega
  126.     && device != b_pgc)
  127.     {
  128.     if (b_vga == old_dev)
  129.         b_vga = device;
  130.     }
  131.  
  132.     /* The BIOS keeps track of the color vs. monochrome distinction   */
  133.     /* by two bits in the equipment word, so we must poke those bits  */
  134.     /* to signal the change in device.                      */
  135.  
  136.     int86(17,®s,®s);          /* Retrieve equipment word.     */
  137.  
  138.     old_flag = (our_word & VIDEO_FIELD);
  139.     if (new_flag != old_flag          /* There is a change.          */
  140.         && (old_flag != COLOR_40 || new_flag != COLOR_80))
  141.                       /* (Skip the change if 40-col   */
  142.                       /*   mode is set and we're      */
  143.                       /*   setting color.)          */
  144.     {
  145.                       /* Install modified equipment   */
  146.                       /* word.                  */
  147.     utpokeb(EQUIP_WORD_LOC,
  148.         (unsigned char) ((our_word & ~VIDEO_FIELD) | new_flag));
  149.     }
  150.  
  151.     /* Restore BIOS variables that were in effect when we last quit   */
  152.     /* this device.                              */
  153.  
  154.     b_vidcpy(TO_BIOS,b_device);
  155.  
  156.     scpage(b_adap_state[device].curpage);   /* Restore current page.  */
  157.  
  158.     return 0;
  159. }
  160.