/****************/
/* Modes.c	*/
/****************/

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <clib/exec_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/macros.h>
#include "/includes/struct.h"

/****************/
/* Default mode */
/****************/

#define DEF_MODE      0
#define DEF_MODE_NAME "HighRes"

extern	UBYTE	flg_debug;

/* This function is only used in with wb1.3, to create	*/
/* a list of all availables modes			*/

#ifdef WB_13
add_mode(	struct List *ModeList,
		int mx,
		int my,
		int MX,
		int My,
		int depth,
		long id,
		char *p_name)
{
  register struct ModeNode *ModeNode;
  if (ModeNode=AllocMem(sizeof(struct ModeNode),MEMF_PUBLIC))
   {
    (void)strcpy(ModeNode->Name,p_name);
    ModeNode->mn_Node.ln_Name=ModeNode->Name;
    ModeNode->DisplayID=id;
    ModeNode->MaxX=mx;
    ModeNode->MaxY=my;
    ModeNode->MaxDepth=depth;

    AddTail (ModeList,&ModeNode->mn_Node);
   }
  else
   {
    return NULL;
   }
}
#endif

/********************************************************** get_modes() *****/

/********************************************************/
/* We get the current modes availables in this computer */
/********************************************************/

struct	List	*get_modes()
{
 struct List *ModeList;
 ULONG DisplayID;
 struct DimensionInfo DimInfo;
 struct NameInfo NameInfo;
 struct ModeNode *ModeNode;

 if (ModeList=AllocMem(sizeof(struct List),MEMF_PUBLIC)) NewList (ModeList);
 else return NULL;

#ifndef	WB_13
 DisplayID=INVALID_ID;
 while ((DisplayID=NextDisplayInfo(DisplayID))!=INVALID_ID){
  if ((DisplayID&MONITOR_ID_MASK)&&(ModeNotAvailable(DisplayID)==0L)){
   if (GetDisplayInfoData(NULL,(UBYTE *)&DimInfo,sizeof(struct DimensionInfo),
                          DTAG_DIMS,DisplayID)){
    if (DimInfo.MaxDepth>1){
     if (GetDisplayInfoData(NULL,(UBYTE *)&NameInfo,sizeof(struct NameInfo),
                            DTAG_NAME,DisplayID)){
      if (ModeNode=AllocMem(sizeof(struct ModeNode),MEMF_PUBLIC))
       {

	ModeNode->MaxDepth=DimInfo.MaxDepth;
	ModeNode->MinX=DimInfo.Nominal.MinX+1;
	ModeNode->MaxX=DimInfo.Nominal.MaxX+1;
	ModeNode->MinY=DimInfo.Nominal.MinY+1;
	ModeNode->MaxY=DimInfo.Nominal.MaxY+1;

        (void)strcpy(ModeNode->Name,
                     NameInfo.Name);
	ModeNode->mn_Node.ln_Name=ModeNode->Name;
        ModeNode->DisplayID=DisplayID;

	printd("%20s ",NameInfo.Name);
	printd("M.depth:%ld mx:%02ld my:%02ld MX:%03ld MY:%03ld ",DimInfo.MaxDepth,
				DimInfo.Nominal.MinX,
				DimInfo.Nominal.MinY,
				DimInfo.Nominal.MaxX,
				DimInfo.Nominal.MaxY);
	printd("Flags:%lx\n",ModeNode->DisplayID);

        AddTail (ModeList,&ModeNode->mn_Node);
      }
     }
    }
   }
  }
 }	
 if (ModeList->lh_Head->ln_Succ==NULL)
  if (ModeNode=AllocMem(sizeof(struct ModeNode),MEMF_PUBLIC))
   {
    (void)strcpy(ModeNode->Name,
                     DEF_MODE_NAME);
    ModeNode->mn_Node.ln_Name=ModeNode->Name;
    ModeNode->DisplayID=DEF_MODE;
    ModeNode->MaxX=640;
    ModeNode->MaxY=256;
    ModeNode->MaxDepth=4;

    AddTail (ModeList,&ModeNode->mn_Node);
   }
  else
   {
    FreeMem ((APTR)ModeList,sizeof(struct List));
    return NULL;
   }
#endif
#ifdef	WB_13
	add_mode(ModeList,0,0,320,256,5,LORES_KEY,"PAL Lores");
	add_mode(ModeList,0,0,640,256,4,HIRES_KEY,"PAL Hires");
/*	add_mode(ModeList,0,0,320,256,6,HAM_KEY,"PAL Ham");*/
	add_mode(ModeList,0,0,320,512,5,LORESLACE_KEY,"PAL LoresLace");
	add_mode(ModeList,0,0,640,512,4,HIRESLACE_KEY,"PAL HiresLace");
/*	add_mode(ModeList,0,0,320,512,6,HAMLACE_KEY,"PAL HamLace");*/
#endif
 return ModeList;
}

/************************************************************* free_modes() */


/***********************************************/
/* We delete the liste of the available modes */
/***********************************************/

void free_modes(struct List *ModeList)

{
	struct ModeNode *ModeNode;

	while (ModeList->lh_Head->ln_Succ){
		ModeNode=(struct ModeNode *)ModeList->lh_Head;
		RemHead (ModeList);
		FreeMem ((APTR)ModeNode,sizeof(struct ModeNode));
	}
	FreeMem ((APTR)ModeList,sizeof(struct List));
}

