/*
======================================================================
config.c

Ernie Wright  17 May 05
MSVC 4.0

Load and save the QV config file.
====================================================================== */

#include "qv.h"

#define CFGNAME "qv4.cfg"

#define ID_QVCF LWID_( 'Q','V','C','F' )
#define ID_VERS LWID_( 'V','E','R','S' )
#define ID_TXOP LWID_( 'T','X','O','P' )
#define ID_HOTP LWID_( 'H','O','T','P' )
#define ID_SAFP LWID_( 'S','A','F','P' )
#define ID_FONT LWID_( 'F','O','N','T' )
#define ID_COLR LWID_( 'C','O','L','R' )
#define ID_POSI LWID_( 'P','O','S','I' )
#define ID_TEXT LWID_( 'T','E','X','T' )
#define ID_SHAD LWID_( 'S','H','A','D' )
#define ID_LEVL LWID_( 'L','E','V','L' )
#define ID_PTYP LWID_( 'P','T','Y','P' )
#define ID_CLRS LWID_( 'C','L','R','S' )

static LWFileIOFuncs *fiof;

static LWBlockIdent idcontainer[] = {
   ID_QVCF, "QVConfig",
   0
};

static LWBlockIdent idroot[] = {
   ID_VERS, "Version",
   ID_TXOP, "TextOptions",
   ID_HOTP, "HotParams",
   ID_SAFP, "SafeParams",
   ID_LEVL, "BlackWhite",
   ID_PTYP, "PixelDisplayType",
   ID_CLRS, "CloseRenderStatus",
   0
};

static LWBlockIdent idtext[] = {
   ID_FONT, "Font",
   ID_COLR, "Color",
   ID_POSI, "Position",
   ID_TEXT, "Text",
   ID_SHAD, "ShadowOptions",
   0
};


int initConfigIO( GlobalFunc *global )
{
   fiof = global( LWFILEIOFUNCS_GLOBAL, GFUSE_TRANSIENT );
   return fiof != NULL;
}


static void default_qvconfig( QVConfig *config )
{
   strcpy( config->filename, CFGNAME );

   config->text.lf.lfHeight    = -24;
   config->text.lf.lfWeight    = 400;
   strcpy( config->text.lf.lfFaceName, "Arial" );

   config->text.text[ 0 ]   = 0;
   config->text.color       = RGB( 255, 255, 255 );
   config->text.x           = 10;
   config->text.y           = 30;
   config->text.lcr         = LCR_LEFT;
   config->text.dropshadow  = TRUE;
   config->text.dx          =
   config->text.dy          = 2;

   config->hot.encoding     = 0;
   config->hot.pedestal     = 7.5f;
   config->hot.limComposite = 110.0f;
   config->hot.limChroma    = 50.0f;

   config->safe.aspect      = 1.85f;
   config->safe.margin      = 0.02f;
   config->safe.types       = SAFE_VIDEO;
   config->safe.style       = SAFE_OUTLINE;

   config->black            = 0.0f;
   config->white            = 1.0f;
   config->pixelDisplayType = SHOWPEL_BYTE;
   config->closeRS          = TRUE;
}


void loadQVConfig( QVConfig *config, const LWLoadState *ls )
{
   LWID id;
   int ver = 0;

   id = LWLOAD_FIND( ls, idcontainer );
   if ( id != ID_QVCF )
      return;

   while ( id = LWLOAD_FIND( ls, idroot )) {
      switch ( id )
      {
         case ID_VERS:
            LWLOAD_I4( ls, &ver, 1 );
            break;

         case ID_TXOP:
            while ( id = LWLOAD_FIND( ls, idtext )) {
               switch ( id )
               {
                  case ID_FONT:
                     LWLOAD_STR( ls, config->text.lf.lfFaceName,
                        sizeof( config->text.lf.lfFaceName ));
                     LWLOAD_I4( ls, &config->text.lf.lfHeight, 5 );
                     LWLOAD_I1( ls, &config->text.lf.lfItalic, 8 );
                     break;

                  case ID_COLR:
                     LWLOAD_U1( ls, ( unsigned char * ) &config->text.color, 4 );
                     break;

                  case ID_POSI:
                     LWLOAD_I4( ls, &config->text.x, 4 );
                     break;

                  case ID_TEXT:
                     LWLOAD_STR( ls, config->text.text,
                        sizeof( config->text.text ));
                     break;

                  case ID_SHAD:
                     LWLOAD_I4( ls, &config->text.dropshadow, 3 );
                     break;
               }
               LWLOAD_END( ls );
            }
            break;

         case ID_HOTP:
            LWLOAD_FP( ls, &config->hot.pedestal, 3 );
            LWLOAD_I4( ls, &config->hot.encoding, 1 );
            break;

         case ID_SAFP:
            LWLOAD_FP( ls, &config->safe.aspect, 2 );
            LWLOAD_I4( ls, &config->safe.types, 2 );
            break;

         case ID_LEVL:
            LWLOAD_FP( ls, &config->black, 2 );
            break;

         case ID_PTYP:
            LWLOAD_I4( ls, &config->pixelDisplayType, 1 );
            break;

         case ID_CLRS:
            LWLOAD_I4( ls, &config->closeRS, 1 );
            break;
      }
      LWLOAD_END( ls );
   }
   LWLOAD_END( ls );
}


void loadQVConfigFile( HINSTANCE hinst, QVConfig *config )
{
   LWLoadState *ls;


   default_qvconfig( config );

   if ( findConfigFile( hinst, CFGNAME, config->filename,
         sizeof( config->filename ))) {
      if ( ls = fiof->openLoad( config->filename, LWIO_ASCII )) {
         loadQVConfig( config, ls );
         fiof->closeLoad( ls );
      }
   }
}


void saveQVConfig( QVConfig *config, const LWSaveState *ss )
{
   int ver = 4;

   LWSAVE_BEGIN( ss, idcontainer, 0 );
      LWSAVE_BEGIN( ss, &idroot[ 0 ], 1 );
         LWSAVE_I4( ss, &ver, 1 );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 1 ], 0 );
         LWSAVE_BEGIN( ss, &idtext[ 0 ], 1 );
            LWSAVE_STR( ss, config->text.lf.lfFaceName );
            LWSAVE_I4( ss, &config->text.lf.lfHeight, 5 );
            LWSAVE_I1( ss, &config->text.lf.lfItalic, 8 );
         LWSAVE_END( ss );

         LWSAVE_BEGIN( ss, &idtext[ 1 ], 1 );
            LWSAVE_U1( ss, ( unsigned char * ) &config->text.color, 4 );
         LWSAVE_END( ss );

         LWSAVE_BEGIN( ss, &idtext[ 2 ], 1 );
            LWSAVE_I4( ss, &config->text.x, 4 );
         LWSAVE_END( ss );

         LWSAVE_BEGIN( ss, &idtext[ 3 ], 1 );
            LWSAVE_STR( ss, config->text.text );
         LWSAVE_END( ss );

         LWSAVE_BEGIN( ss, &idtext[ 4 ], 1 );
            LWSAVE_I4( ss, &config->text.dropshadow, 3 );
         LWSAVE_END( ss );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 2 ], 1 );
         LWSAVE_FP( ss, &config->hot.pedestal, 3 );
         LWSAVE_I4( ss, &config->hot.encoding, 1 );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 3 ], 1 );
         LWSAVE_FP( ss, &config->safe.aspect, 2 );
         LWSAVE_I4( ss, &config->safe.types, 2 );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 4 ], 1 );
         LWSAVE_FP( ss, &config->black, 2 );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 5 ], 1 );
         LWSAVE_I4( ss, &config->pixelDisplayType, 1 );
      LWSAVE_END( ss );

      LWSAVE_BEGIN( ss, &idroot[ 6 ], 1 );
         LWSAVE_I4( ss, &config->closeRS, 1 );
      LWSAVE_END( ss );
   LWSAVE_END( ss );
}


void saveQVConfigFile( QVConfig *config )
{
   LWSaveState *ss;

   if ( ss = fiof->openSave( config->filename, LWIO_ASCII )) {
      saveQVConfig( config, ss );
      fiof->closeSave( ss );
   }
}
