home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ddkx86v5.zip / DDKX86 / SRC / VDH / VDHPALET.C < prev    next >
C/C++ Source or Header  |  1995-04-14  |  33KB  |  724 lines

  1. /*DDK*************************************************************************/
  2. /*                                                                           */
  3. /* COPYRIGHT (C) Microsoft Corporation, 1989                                 */
  4. /* COPYRIGHT    Copyright (C) 1995 IBM Corporation                           */
  5. /*                                                                           */
  6. /*    The following IBM OS/2 WARP source code is provided to you solely for  */
  7. /*    the purpose of assisting you in your development of OS/2 WARP device   */
  8. /*    drivers. You may use this code in accordance with the IBM License      */
  9. /*    Agreement provided in the IBM Device Driver Source Kit for OS/2. This  */
  10. /*    Copyright statement may not be removed.                                */
  11. /*                                                                           */
  12. /*****************************************************************************/
  13. /*****************************************************************************
  14.  *
  15.  * SOURCE FILE NAME = VDHPALET.c
  16.  *
  17.  * DESCRIPTIVE NAME = Base video device handlers - Color Lookup/Palette 
  18.  *
  19.  *
  20.  * VERSION      V2.0
  21.  *
  22.  * DATE         
  23.  *
  24.  * DESCRIPTION  This source file contains VDH entry points which             
  25.  *              get/set the color lookup table and palette registers.
  26.  *              and get/set palette registers.
  27.  *
  28.  *  NOTE:       These routines execute as ring 2 conforming           
  29.  *
  30.  * FUNCTIONS    GetColorLookup,  SetColorLookup                       
  31.  *              GetPaletteReg,   SetPaletteReg                        
  32.  *
  33.  * NOTES        NONE
  34.  *
  35.  * STRUCTURES   NONE
  36.  *
  37.  * EXTERNAL REFERENCES
  38.  *              SaveRegs, RestoreRegs          
  39.  *              AccessHardware, AccessCLUT      
  40.  *              PhysToUVirt, FreePhysToUVirt    
  41.  *
  42.  * EXTERNAL FUNCTIONS
  43.  *
  44.  *              NONE
  45.  *
  46. */
  47.  
  48. /*
  49. **  Include files                                                            
  50. */
  51.  
  52. #define  INCL_BASE                     /* ALL of OS/2 Base                  */
  53. #define  INCL_OS2STD                   /* Needed for NULL definition in     */
  54.                                        /* OS2STD.H                          */
  55. #include <os2.h>
  56. #include "vdhctl.h"                    /* Conditional compilation control   */
  57. #include "vdh.h"                       /* Type definitions                  */
  58.  
  59. /*
  60. **  Externally defined global variables                                      
  61. */
  62.  
  63. extern VIDEOMODE Modes[];              /* Supported modes                   */
  64. extern MEMORYMAPS MemoryMaps[];        /* Memory map info for each mode     */
  65. extern UCHAR LCDGrayLUT[];             /* LCD gray scale lookup table       */
  66. extern USHORT OEMFlags;                /* OEM specific features             */
  67. extern SHORT HugeShift;                /* Huge shift value                  */
  68. extern UCHAR READABLE;
  69. extern ULONG PartialSaveSize;          /* Room required to save entire PVB  */
  70.                                        /* in the popup mode                 */
  71. extern USHORT VGA_PRESENT;             /* TRUE if VGA VDH has been          */
  72.                                        /* installed                         */
  73. extern USHORT(APIENTRY *ChainedCallVectorTable[MaxFn])();/*             @T24*/
  74.  
  75. /*
  76. **  Parameters to ring 2 routines                                            
  77. */
  78.  
  79. extern UCHAR CRTReg1;
  80. extern UCHAR CRTReg2;
  81. extern UCHAR PelHeightFixup;
  82. extern UCHAR ScreenRemainder;
  83. extern CLUTDATA far ColorCLUT;
  84. extern CLUTDATA far MonoCLUT;
  85. extern CLUTDATA far SumCLUT;
  86.  
  87. #if      VDHVGA        || VDH8514A     /* Start of               @S24CHANGES*/
  88.                                        /* MS00                              */
  89.  
  90. /*****************************************************************************
  91.  *                                                                          
  92.  *  SUBROUTINE NAME: GetColorLookup                                         
  93.  *                                                                          
  94.  *  DESCRIPTIVE NAME: Get color lookup table                                
  95.  *                                                                          
  96.  *  FUNCTION: GetColorLookup is called by BVS to return a specified         
  97.  *            portion of the current color lookup table settings            
  98.  *                                                                          
  99.  *  ENTRY POINT: GetColorLookup                                             
  100.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 263 )       
  101.  *                                                                          
  102.  *  INPUT: (Passed on stack)                                                
  103.  *             FAR *Environment ( Environment buffer for the session )      
  104.  *             FAR *ParmBlock                                               
  105.  *                     USHORT Length = length of this packet                
  106.  *                     USHORT Flags  = 0 - Environment buffer only          
  107.  *                                     1 - Hardware also                    
  108.  *                     CLUTDATA far *LookupTable ( 0x00 - 0xFF )            
  109.  *                                     UCHAR Red                            
  110.  *                                     UCHAR Green                          
  111.  *                                     UCHAR Blue                           
  112.  *                     USHORT   FirstEntry                                  
  113.  *                     USHORT   NumEntries                                  
  114.  *             ULONG Function ( Call vector table entry = 263 )             
  115.  *                                                                          
  116.  *  EXIT-NORMAL: AX = 0                                                     
  117.  *               Portion of CLUT requested is returned                      
  118.  *                                                                          
  119.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                                
  120.  *                                                                          
  121.  *  EFFECTS: If hardware specified and hardware is readable, the            
  122.  *           environment buffer is updated, if passed.                      
  123.  *                                                                          
  124.  *  INTERNAL REFERENCES:                                                    
  125.  *    ROUTINES: NONE                                                        
  126.  *                                                                          
  127.  *  EXTERNAL REFERENCES:                                                    
  128.  *    ROUTINES: AccessCLUT                                                  
  129.  *                                                                          
  130.  ****************************************************************************/
  131.  
  132. USHORT EXPENTRY GetColorLookup(Environment,ParmBlock,Function)
  133.  
  134.   ENVIRONMENT far *Environment;
  135.   VDH_CLUT far *ParmBlock;
  136.   ULONG Function;
  137.  
  138. {
  139.   USHORT rc,EnvBufferPassed;
  140.   int i,j;
  141.  
  142.   rc = ERROR_VIO_INVALID_PARMS;        /* Initialize to error               */
  143.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  144.  
  145.   #if      VDH8514A                    /*  MS00                             */
  146.  
  147.   if (EnvBufferPassed)
  148.   {
  149.  
  150.     if (!(Environment->ModeData.fbType & NATIVE))
  151.     {                                  /* Passthru VGA modes                */
  152.       rc = (*ChainedCallVectorTable[Function-FnTextBufferUpdate])((char far *)
  153.             &Environment->VGAEnvironment, ParmBlock, Function);
  154.       return (rc);
  155.     } 
  156.   } 
  157.  
  158.   else
  159.   {
  160.     return (rc);
  161.   } 
  162.  
  163.   #endif        /*          VDH8514A,  MS00                                 */
  164.  
  165.   if ((Function == FnGetColorLookupTable) && /* Valid function request      */
  166.      (ParmBlock->Length >= sizeof(VDH_CLUT)) && /* Valid packet length      */
  167.      (ParmBlock->Flags <= 1) &&        /* Valid flags                       */
  168.      (ParmBlock->NumEntries) &&        /* Non-zero amount                   */
  169.      (ParmBlock->FirstEntry <= 0xFF) && /* Valid first entry                */
  170.      (ParmBlock->FirstEntry + ParmBlock->NumEntries <= 0xFF+1) &&
  171.      (EnvBufferPassed || (ParmBlock->Flags & UPDATE_HARDWARE)))
  172.   {
  173.  
  174. /*
  175. **  If requesting application is running in foreground,           
  176. **    - Return hardware value                                     
  177. **    - Otherwise return environment buffer value                 
  178. **  If requesting application is running in background,           
  179. **    - Return environment buffer value                           
  180. **     ( report error if environment buffer was not passed )      
  181. */
  182.  
  183.     rc = NO_ERROR;                     /* Initialize no error               */
  184.  
  185. /*
  186. **  Get CLUT values directly from the hardware                              
  187. */
  188.  
  189.     if (ParmBlock->Flags & UPDATE_HARDWARE)
  190.     {
  191.       AccessCLUT(GET, &ParmBlock->LookupTable);/* ring 2 callgate           */
  192.  
  193.   #if      VDHVGA                      /*  MS00                             */
  194.  
  195. /*
  196. **  Copy CLUT values from caller's parmeter area to the env buffer 
  197. */
  198.  
  199.       if (EnvBufferPassed)
  200.       {
  201.  
  202.         for (i = ParmBlock->FirstEntry, j = 0; j < ParmBlock->NumEntries;
  203.              i++, j++)
  204.         {
  205.           Environment->LookupTable[i] = ParmBlock->LookupTable[j]; /*  MS20 */
  206.         } 
  207.       } 
  208.     } 
  209.  
  210.     else
  211.     {                                  /* Caller in background              */
  212.  
  213. /*
  214. **  Copy CLUT values from env buffer to caller's parameter area   
  215. */
  216.  
  217.       for (i = ParmBlock->FirstEntry, j = 0; j < ParmBlock->NumEntries; 
  218.            i++, j ++)
  219.       {
  220.         ParmBlock->LookupTable[j] = Environment->LookupTable[i]; /*  MS20   */
  221.       } 
  222.  
  223.   #endif        /*        VDH8514A,  MS00                                   */
  224.     } 
  225.   } 
  226.  
  227.   return (rc);
  228.  
  229. /*****************************************************************************
  230.  *                                                                        
  231.  *  SUBROUTINE NAME: SetColorLookup                                       
  232.  *                                                                        
  233.  *  DESCRIPTIVE NAME: Set color lookup table                              
  234.  *                                                                        
  235.  *  FUNCTION: SetColorLookup is called by BVS to set a specified          
  236.  *            portion of the color lookup table settings                  
  237.  *                                                                        
  238.  *  ENTRY POINT: SetColorLookup                                           
  239.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 264 )     
  240.  *                                                                        
  241.  *  INPUT: (Passed on stack)                                              
  242.  *             FAR *Environment ( Environment buffer for the session )    
  243.  *             FAR *ParmBlock                                             
  244.  *                     USHORT Length = length of this packet              
  245.  *                     USHORT Flags  = 0 - Environment buffer only        
  246.  *                                     1 - Hardware also                  
  247.  *                     CLUTDATA far *LookupTable ( 0x00 - 0xFF )          
  248.  *                                     UCHAR Red                          
  249.  *                                     UCHAR Green                        
  250.  *                                     UCHAR Blue                         
  251.  *                     USHORT   FirstEntry                                
  252.  *                     USHORT   NumEntries                                
  253.  *             ULONG Function ( Call vector table entry = 264 )           
  254.  *                                                                        
  255.  *  EXIT-NORMAL: AX = 0                                                   
  256.  *               Portion of CLUT requested is set                         
  257.  *                                                                        
  258.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                              
  259.  *                                                                        
  260.  *  INTERNAL REFERENCES:                                                  
  261.  *    ROUTINES: NONE                                                      
  262.  *                                                                        
  263.  *  EXTERNAL REFERENCES:                                                  
  264.  *    ROUTINES: AccessCLUT                                                
  265.  *                                                                        
  266.  ****************************************************************************/
  267.  
  268. USHORT EXPENTRY SetColorLookup(Environment,ParmBlock,Function)
  269.  
  270.   ENVIRONMENT far *Environment;
  271.   VDH_CLUT far *ParmBlock;
  272.   ULONG Function;
  273.  
  274. {
  275.  
  276.   USHORT rc,i,j,EnvBufferPassed;
  277.   rc = ERROR_VIO_INVALID_PARMS;        /* Initialize to error               */
  278.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  279.  
  280.   #if      VDH8514A                    /*  MS00                             */
  281.  
  282.   if (EnvBufferPassed)
  283.   {
  284.  
  285.     if (!(Environment->ModeData.fbType&NATIVE))
  286.     {                                  /* Passthru VGA modes                */
  287.       rc = (*ChainedCallVectorTable[Function-FnTextBufferUpdate])((char far *)
  288.          &Environment->VGAEnvironment, ParmBlock, Function);
  289.       return (rc);
  290.     } 
  291.   } 
  292.  
  293.   else
  294.   {
  295.     return (rc);
  296.   } 
  297.   #endif        /*        VDH8514A,  MS00                                   */
  298.  
  299.   if ((Function == FnSetColorLookupTable) && /* Valid function request      */
  300.      (ParmBlock->Length >= sizeof(VDH_CLUT)) && /* Valid packet length      */
  301.      (ParmBlock->Flags <= 1) &&        /* Valid flags                       */
  302.      (ParmBlock->NumEntries) &&        /* Non-zero amount                   */
  303.      (ParmBlock->FirstEntry <= 0xFF) && /* Valid first entry                */
  304.      (ParmBlock->FirstEntry + ParmBlock->NumEntries <= 0xFF+1) &&
  305.      (EnvBufferPassed || (ParmBlock->Flags & UPDATE_HARDWARE)))
  306.   {
  307.  
  308. /*
  309. **  Parameters are valid - Update environment buffer if it was passed  
  310. **                       - Update hardware if in foreground            
  311. */
  312.  
  313.     rc = NO_ERROR;                     /* Initialize no error               */
  314.  
  315.   #if      VDHVGA                      /*  MS00                             */
  316.  
  317. /*
  318. **  Copy CLUT values from caller's parmeter area to the env buffer       
  319. */
  320.  
  321.     if (EnvBufferPassed)
  322.     {
  323.  
  324.       for (i = ParmBlock->FirstEntry, j = 0; j < ParmBlock->NumEntries; 
  325.            i++, j++)
  326.       {
  327.         Environment->LookupTable[i] = ParmBlock->LookupTable[j]; /*  MS20   */
  328.       } 
  329.  
  330.     } 
  331.  
  332.   #endif        /*        VDHVGA,  MS00                                     */
  333.  
  334. /*
  335. **  Set CLUT registers directly on the hardware                      
  336. */
  337.  
  338.     if (ParmBlock->Flags & UPDATE_HARDWARE)
  339.     {
  340.       AccessCLUT(SET, &ParmBlock->LookupTable);/* ring 2 callgate           */
  341.     } 
  342.   } 
  343.   return (rc);
  344.  
  345. #endif              /*  VDHVGA || VDH8514A    End of @S24 changes           */
  346.  
  347. #if      PALETTE_SUPPORT                   /* MS00                          */
  348.  
  349. /*****************************************************************************
  350.  *                                                                        
  351.  *  SUBROUTINE NAME: GetPaletteReg                                        
  352.  *                                                                        
  353.  *  DESCRIPTIVE NAME: Get palette registers                               
  354.  *                                                                        
  355.  *  FUNCTION: GetPaletteReg is called by BVS to return the specified      
  356.  *            portion of the palette registers.  If the request           
  357.  *            specifies hardware and the hardware is readable, the        
  358.  *            actual hardware setting will be read and returned.          
  359.  *            Otherwise the returned information will be taken from       
  360.  *            the environment buffer, if it has been passed.              
  361.  *                                                                        
  362.  *  ENTRY POINT: GetPaletteReg                                            
  363.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 273 )     
  364.  *                                                                        
  365.  *  INPUT: (Passed on stack)                                              
  366.  *             FAR *Environment ( Environment buffer for the session )    
  367.  *             FAR *ParmBlock                                             
  368.  *                     USHORT Length = length of this packet              
  369.  *                     USHORT Flags  = 0 - Environment buffer only        
  370.  *                                     1 - Hardware also                  
  371.  *                     FAR *PaletteBuffer                                 
  372.  *                     USHORT   FirstEntry                                
  373.  *                     USHORT   NumEntries                                
  374.  *             ULONG Function ( Call vector table entry = 273 )           
  375.  *                                                                        
  376.  *  EXIT-NORMAL: AX = 0                                                   
  377.  *               Requested palette registers are returned                 
  378.  *                                                                        
  379.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                              
  380.  *                                                                        
  381.  *  EFFECTS: If hardware specified and hardware is readable, the          
  382.  *           environment buffer is updated, if passed.                    
  383.  *                                                                        
  384.  *  INTERNAL REFERENCES:                                                  
  385.  *    ROUTINES: NONE                                                      
  386.  *                                                                        
  387.  *  EXTERNAL REFERENCES:                                                  
  388.  *    ROUTINES: AccessHardware                                            
  389.  *                                                                        
  390.  ****************************************************************************/
  391.  
  392. USHORT EXPENTRY GetPaletteReg(Environment,ParmBlock,Function)
  393.  
  394.   ENVIRONMENT far *Environment;
  395.   VDH_PALETTE far *ParmBlock;
  396.   ULONG Function;
  397.  
  398. {
  399.  
  400.   USHORT rc,ColorMode,                 /*                               @C21*/
  401.          VideoOn,                      /*                               @C21*/
  402.          EnvBufferPassed;
  403.   int i,j;
  404.   REGADDRESS RegAddress;
  405.  
  406.  
  407.   rc = ERROR_VIO_INVALID_PARMS;        /* Initialize to error               */
  408.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  409.  
  410.   if ((Function == FnGetPaletteRegisters) && /* Valid function request      */
  411.      (ParmBlock->Length >= sizeof(VDH_PALETTE)) && /* Valid packet length   */
  412.      (ParmBlock->Flags <= 1) &&        /* Valid flags                       */
  413.      (EnvBufferPassed || (ParmBlock->Flags & UPDATE_HARDWARE)))
  414.  
  415.     if ((ParmBlock->PaletteBuffer.NumEntries) && /* Non-zero amount         */
  416.        (ParmBlock->PaletteBuffer.FirstEntry <= 0x0F) && /* Valid first      */
  417.                                                         /*  entry           */
  418.        (ParmBlock->PaletteBuffer.FirstEntry +
  419.         ParmBlock->PaletteBuffer.NumEntries <= 0x0F+1))
  420.     {
  421.       rc = NO_ERROR;                   /* Initialize no error               */
  422.  
  423. /*
  424. **  If requesting application is running in foreground,                  
  425. **    - Return hardware value if hardware can be read                    
  426. **    - Otherwise return environment buffer value                        
  427. **     ( report error if write-only hardware and no env buffer passed )  
  428. **  If requesting application is running in background,                  
  429. **    - Return environment buffer value                                  
  430. **     ( report error if environment buffer was not passed )             
  431. */
  432.  
  433.       if ((ParmBlock->Flags&UPDATE_HARDWARE) && READABLE)
  434.       {
  435.  
  436.  #if      VDHVGA                        /* Read/write hardware              */
  437.                                         /*  MS00                            */
  438.  
  439. /*
  440. **  Get palette values directly from the hardware                     
  441. **  Accessing the Attribute regs resets the VideoEnable status so have
  442. **  TO RESET IT IF IT WAS ON BEFORE THE CALL                          
  443. */
  444.  
  445.         ColorMode = HardwareColor();   /* color/mono mode ( hardware )  @C21*/
  446.  
  447.         if (EnvBufferPassed)           /*                               @C21*/
  448.           VideoOn = Environment->VideoEnable;/*                         @C21*/
  449.  
  450.         else                           /*                               @C21*/
  451.           AccessVideoEnable(ColorMode, GET, &VideoOn);/*                @C21*/
  452.         RegAddress.AddressPort = AttAddressPort;
  453.         RegAddress.DataPort = AttDataReadPort;
  454.         RegAddress.ColorAdjust = NONE;
  455.         RegAddress.Flags = Attributes_CMD;
  456.         AccessHardware(&RegAddress, WORDS, HardwareColor(), GET, 
  457.                       (REGDATA far *)&ParmBlock->PaletteBuffer);
  458.  
  459.         if (VideoOn)                   /*                               @C21*/
  460.           AccessVideoEnable(ColorMode, SET, &VideoOn);/*                @C21*/
  461.  
  462.         if (EnvBufferPassed)
  463.  
  464. /*
  465. **  Copy palette values from caller's parmeter area to the env buffer        
  466. */
  467.  
  468.           for (i = ParmBlock->PaletteBuffer.FirstEntry, j = 0; 
  469.                j < ParmBlock->PaletteBuffer.NumEntries; i++, j++)
  470.  
  471.             Environment->Hardware.Attributes.Regs.Palettes[i] = 
  472.                     (UCHAR) ParmBlock->PaletteBuffer.DataArea[j];
  473.  
  474.   #endif        /*        VDHVGA,  MS00                                     */
  475.       } 
  476.  
  477.       else
  478.       {
  479.  
  480. /*
  481. **  Requesting application is running in the background                      
  482. */
  483.  
  484.         if (EnvBufferPassed)
  485.  
  486. /*
  487. **  Copy palette values from env buffer to caller's parameter area           
  488. */
  489.  
  490.           for (i = ParmBlock->PaletteBuffer.FirstEntry, j = 0;
  491.                j < ParmBlock->PaletteBuffer.NumEntries; i++, j++)
  492.  
  493.             ParmBlock->PaletteBuffer.DataArea[j] = (USHORT)
  494.                Environment->Hardware.Attributes.Regs.Palettes[i];
  495.  
  496.         else
  497.           rc = ERROR_VIO_INVALID_PARMS;/* Not FG OR write-only, no env      */
  498.                                        /* buffer                            */
  499.       } 
  500.     } 
  501.  
  502.  
  503.   return (rc);
  504.  
  505. /*****************************************************************************
  506.  *                                                                         
  507.  *  SUBROUTINE NAME: SetPaletteReg                                         
  508.  *                                                                         
  509.  *  DESCRIPTIVE NAME: Set palette registers                                
  510.  *                                                                         
  511.  *  FUNCTION: SetPaletteReg is called by BVS to set the specified          
  512.  *            portion of the palette registers.  If the request            
  513.  *            specifies hardware the hardware and the environment          
  514.  *            buffer, if passed, will be updated.  Otherwise just the      
  515.  *            environment buffer, if passed, will be updated.              
  516.  *                                                                         
  517.  *  ENTRY POINT: SetPaletteReg                                             
  518.  *    LINKAGE:   CALL FAR ( via BVS-DDI call vector table entry 274 )      
  519.  *                                                                         
  520.  *  INPUT: (Passed on stack)                                               
  521.  *             FAR *Environment ( Environment buffer for the session )     
  522.  *             FAR *ParmBlock                                              
  523.  *                     USHORT Length = length of this packet               
  524.  *                     USHORT Flags  = 0 - Environment buffer only         
  525.  *                                     1 - Hardware also                   
  526.  *                     FAR *PaletteBuffer                                  
  527.  *                     USHORT   FirstEntry                                 
  528.  *                     USHORT   NumEntries                                 
  529.  *             ULONG Function ( Call vector table entry = 274 )            
  530.  *                                                                         
  531.  *  EXIT-NORMAL: AX = 0                                                    
  532.  *               Requested palette registers are set                       
  533.  *                                                                         
  534.  *  EXIT-ERROR: AX = ERROR_VIO_INVALID_PARMS                               
  535.  *                                                                         
  536.  *  INTERNAL REFERENCES:                                                   
  537.  *    ROUTINES: NONE                                                       
  538.  *                                                                         
  539.  *  EXTERNAL REFERENCES:                                                   
  540.  *    ROUTINES: AccessHardware                                             
  541.  *                                                                         
  542.  ****************************************************************************/
  543.  
  544. USHORT EXPENTRY SetPaletteReg(Environment,ParmBlock,Function)
  545.  
  546.   ENVIRONMENT far *Environment;
  547.   VDH_PALETTE far *ParmBlock;
  548.   ULONG Function;
  549.  
  550. {
  551.  
  552.   USHORT rc,i,j,ColorMode,VideoOn,     /*                               @C21*/
  553.          EnvBufferPassed;
  554.   REGADDRESS RegAddress;
  555.  
  556.  
  557.   rc = ERROR_VIO_INVALID_PARMS;        /* Init to error                     */
  558.   EnvBufferPassed = SEG(Environment);  /* Non-zero = TRUE                   */
  559.  
  560.   if ((Function == FnSetPaletteRegisters) && /* Valid function request      */
  561.      (ParmBlock->Length >= sizeof(VDH_PALETTE)) && /* Valid packet length   */
  562.      (ParmBlock->Flags <= 1) &&        /* Valid flags                       */
  563.      (ParmBlock->PaletteBuffer.FirstEntry <= 0x0F) && /*     @@AVALID1STNTRY*/
  564.      (EnvBufferPassed || (ParmBlock->Flags&UPDATE_HARDWARE)))
  565.  
  566.     if ((ParmBlock->PaletteBuffer.NumEntries) && /* Non-zero amount         */
  567.        (ParmBlock->PaletteBuffer.FirstEntry +
  568.         ParmBlock->PaletteBuffer.NumEntries <= 0x0F+1))
  569.     {
  570.       rc = NO_ERROR;                   /* Initialize no error               */
  571.  
  572. /*
  573. **  Parameters are valid - Update environment buffer if it was passed       
  574. **                       - Update hardware if in foreground                  
  575. */
  576.  
  577.       if (EnvBufferPassed)
  578.       {
  579.  
  580. /*
  581. **  Copy palette values from caller's parmeter area to the env buffer        
  582. */
  583.  
  584.         for (i = ParmBlock->PaletteBuffer.FirstEntry, j = 0; 
  585.              j < ParmBlock->PaletteBuffer.NumEntries; i++, j++)
  586.  
  587.           Environment->Hardware.Attributes.Regs.Palettes[i] = 
  588.                 (UCHAR) ParmBlock->PaletteBuffer.DataArea[j];
  589.  
  590.       } 
  591.  
  592.       if (ParmBlock->Flags&UPDATE_HARDWARE)
  593.       {
  594.  
  595. /*
  596. **  Set palette values directly from the hardware                            
  597. **  Accessing the Attribute regs resets the VideoEnable status so have
  598. **  TO RESET IT IF IT WAS ON BEFORE THE CALL                          
  599. */
  600.  
  601.         RegAddress.AddressPort = AttAddressPort;
  602.         RegAddress.DataPort = AttDataWritePort;
  603.         RegAddress.ColorAdjust = NONE;
  604.         RegAddress.Flags = Attributes_CMD;
  605.  
  606.  #if      VDHVGA                       /* Read/write hardware               */
  607.                                        /*  MS00                             */
  608.         ColorMode = HardwareColor();   /* color/mono mode ( hardware )      */
  609.  
  610.   #else                                /* Write-only hardware               */
  611.         ColorMode = Environment->ModeData.fbType & NOT_MONO;/* color/mono   */
  612.                                        /* mode (env buffer)                 */
  613.  
  614.   #endif        /*        VDHVGA,  MS00                                     */
  615.  
  616.         if (EnvBufferPassed)           /*                               @C21*/
  617.           VideoOn = Environment->VideoEnable;/*                         @C21*/
  618.  
  619.         else                           /*                               @C21*/
  620.  
  621.   #if      VDHVGA                      /*                          @C21,MS00*/
  622.   AccessVideoEnable(ColorMode, GET, &VideoOn);/*                        @C21*/
  623.  
  624.   #else                                /*                          @C21,MS00*/
  625.   VideoOn = 1;                         /*                               @C21*/
  626.   #endif                               /*   VDHVGA,   @C21,MS00             */
  627.  
  628.         AccessHardware(&RegAddress, WORDS, ColorMode, SET, 
  629.                       (REGDATA far *) &ParmBlock->PaletteBuffer);
  630.  
  631.         if (VideoOn)                   /*                               @C21*/
  632.           AccessVideoEnable(ColorMode, SET, &VideoOn);/*                @C21*/
  633.       } 
  634.     } 
  635.  
  636.   return (rc);
  637.  
  638. #endif        /*        PALETTE_SUPPORT,  MS00                              */
  639.  
  640. #if      VDHVGA                         /*  MS21 - BEGIN                    */
  641.  
  642. /***************************************************************************
  643.  *
  644.  * FUNCTION NAME = SetTextGrays(Environment)
  645.  *
  646.  * DESCRIPTION   = 
  647.  *                                                                      
  648.  *     When running on an LCD in text mode, this programs the gray scale
  649.  *     so that foreground/background combinations have nearly the same  
  650.  *     contrast as the standard colors do on a color monitor.           
  651.  *                                                                      
  652.  * INPUT         = *Environment;
  653.  * OUTPUT        = NONE
  654.  *
  655.  * RETURN-NORMAL = NONE
  656.  * RETURN-ERROR  = NONE
  657.  *
  658.  **************************************************************************/
  659.  
  660. void PASCAL near SetTextGrays(Environment)
  661.  
  662.   ENVIRONMENT far *Environment;        /*  Session environment buffer       */
  663.  
  664. {
  665.   REGADDRESS RegAddress;
  666.   REGDATA RegData;
  667.   UCHAR DataArea;
  668.   USHORT i;
  669.  
  670.  
  671.   if ((OEMFlags&(PRISM_VGA|CRYSTAL_VGA)) /*  LCD adapter                    */
  672.      && (Environment->ModeData.fbType == NOT_MONO))
  673.   {                                    /*  & Color text mode                */
  674.                                        /*  = Set gray scale                 */
  675.     /*
  676.     ** Open access to extended VGA registers
  677.     */
  678.     RegAddress.AddressPort = GraphAddressPort;
  679.     RegAddress.DataPort = GraphDataPort;
  680.     RegAddress.Flags = NONE;
  681.     RegData.DataArea = &DataArea;
  682.     RegData.FirstEntry = VGA_ENV_REG;
  683.     RegData.NumEntries = 1;
  684.     DataArea = VGA_UNLOCK;
  685.     AccessHardware(&RegAddress, BYTES, NONE, SET, &RegData);
  686.  
  687.     /*
  688.     ** Map 16 colors onto gray levels.
  689.     */
  690.     for (i = 0; i < 16; i++)
  691.     {
  692.  
  693.       /*
  694.       ** Use color's palette value as index into gray scale LUT...
  695.       */
  696.       RegData.FirstEntry = LCD_GRAYSCALE_ADDR;
  697.       DataArea = Environment->Hardware.Attributes.Regs.Palettes[i];
  698.       AccessHardware(&RegAddress, BYTES, NONE, SET, &RegData);
  699.  
  700.       /*
  701.       ** then set that entry to a gray level.
  702.       */
  703.       RegData.FirstEntry = LCD_GRAYSCALE_DATA;
  704.       DataArea = (OEMFlags&CRYSTAL_VGA)?LCDGrayLUT[i] /*  range 0..31       */
  705.          : LCDGrayLUT[i] >> 1;                         /*  range 0..15       */
  706.       AccessHardware(&RegAddress, BYTES, NONE, SET, &RegData);
  707.     } 
  708.  
  709.     /*
  710.     ** Close access to extended VGA registers
  711.     */
  712.     RegData.FirstEntry = VGA_ENV_REG;
  713.     DataArea = VGA_LOCK;
  714.     AccessHardware(&RegAddress, BYTES, NONE, SET, &RegData);
  715.   } 
  716.  
  717. #endif            /*    VDHVGA,  @MS21 - END                                */
  718.  
  719.