/*

  Select Mode (C) 1995 Dominic Clifton - Aka Hydra/LSD - Deluxe Software Ltd!

  Defines a screenmode for HBBS to use!

*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <time.h>

#include <exec/types.h>
#include <exec/memory.h>

#include <libraries/reqtools.h>

#include <clib/exec_protos.h>
#include <clib/reqtools_protos.h>

#include <HBBS/ANSI_Codes.h>
#include <HBBS/Defines.h>
#include <HBBS/types.h>
#include <HBBS/structures.h>
#include <HBBS/hbbscommon_protos.h>
#include <HBBS/hbbscommon_pragmas.h>

struct Library *HBBSCommonBase=NULL;
struct ReqToolsBase *ReqToolsBase;

UBYTE *OPT_BBS_ScrModeID           = "ScrModeID",
      *OPT_BBS_ScrWidth            = "ScrWidth",
      *OPT_BBS_ScrHeight           = "ScrHeight",
      *OPT_BBS_ScrDepth            = "ScrDepth";

UBYTE *FILE_SCRMODEPREFS           = "HBBS:System/Data/CtrlScrPrefs.CFG";

static VOID cleanup(ULONG num)
{
  if (ReqToolsBase)
    CloseLibrary ((struct Library *)ReqToolsBase);

  if (HBBSCommonBase)
  {
    HBBS_CleanUpCommon();
    CloseLibrary (HBBSCommonBase);
  }


  if (num) printf("Door Error = %d\n",num);

  exit(0);
}

static VOID init( void )
{
  if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  {
    cleanup(1);
  }

  if (!(HBBS_InitCommon()))
  {
    cleanup(2);
  }

  if (!(ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION)))
  {
    cleanup(3);
  }

}

BOOL PickScreen( void )
{
  BOOL retval=FALSE;
  struct rtScreenModeRequester *scrmodereq;
  struct CfgFileData *ScrCfg;
  char params[30];

  if (scrmodereq = rtAllocRequestA (RT_SCREENMODEREQ, NULL))
  {
    if (rtScreenModeRequest (scrmodereq, "Select Screen mode:",RTSC_Flags,SCREQF_GUIMODES | SCREQF_DEPTHGAD | SCREQF_SIZEGADS,
                                                               RTSC_MinHeight,200,
                                                               RTSC_MinWidth,640,
                                                               RTSC_MinDepth,2,
                                                               RTSC_MaxDepth,4,

                                                               TAG_END))
    {

      if (ScrCfg=HBBS_CreateConfig((FILE_SCRMODEPREFS)))
      {
        sprintf(params,"%d",scrmodereq->DisplayID);
        HBBS_AddCfgItem(ScrCfg,OPT_BBS_ScrModeID,params);
        sprintf(params,"%d",scrmodereq->DisplayHeight);
        HBBS_AddCfgItem(ScrCfg,OPT_BBS_ScrHeight,params);
        sprintf(params,"%d",scrmodereq->DisplayWidth);
        HBBS_AddCfgItem(ScrCfg,OPT_BBS_ScrWidth,params);
        sprintf(params,"%d",scrmodereq->DisplayDepth);
        HBBS_AddCfgItem(ScrCfg,OPT_BBS_ScrDepth,params);

        if (!HBBS_SaveConfig(ScrCfg))
        {
          HBBS_rterror("Error Saving Screenmode Prefs!");
        }
        else
        {
          retval=TRUE;
        }
        HBBS_FlushConfig(ScrCfg);
      }
    }
    rtFreeRequest(scrmodereq);
  }
  return(retval);
}

void main( void )
{
  init();

  PickScreen();

  cleanup(0);
}
