/*=========================================================================*

 GetModeIDByName.c - given a display name, return its mode ID of INVALID_ID

		   Placed in the public domain by Sylvan Technical Arts
                   		   Use at your own risk.
 *=========================================================================*/

#include <graphics/displayinfo.h>

extern struct Library	*GfxBase;

/*	This is a list of mode names that are acceptable, although they may
	not be in the display database.											*/

struct ExtraModes {
	LONG	ModeID;
	char	*Name;
};

struct ExtraModes extra_modes[] = {
	LORES_KEY,		"Lores",
	HIRES_KEY,		"Hires",
	SUPER_KEY,		"SuperHires",
	LORESLACE_KEY,	"Lores-Interlaced",
	HIRESLACE_KEY,	"Hires-Interlaced",
	SUPERLACE_KEY,	"SuperHires-Interlaced"
};
	
/*	This is routne that does the work...									*/

LONG GetModeIDByName(char *name)
{
	DisplayInfoHandle	handle;
	struct NameInfo		nameinfo;
	struct DisplayInfo	dispinfo;
	LONG				theID = INVALID_ID;
	WORD				i;

	while ( (theID = NextDisplayInfo(theID)) != INVALID_ID )
	{
		if (handle = FindDisplayInfo(theID))
		{
			if ( GetDisplayInfoData(handle, (void *)&nameinfo, sizeof nameinfo,
				DTAG_NAME, NULL) )
			{
				if (!str_cmpi(nameinfo.Name,name))
				{
					if ( GetDisplayInfoData(handle,(void *)&dispinfo,
						sizeof dispinfo,DTAG_DISP, NULL) )
					{
						return (dispinfo.NotAvailable ? INVALID_ID : theID);
					}
				}
			}
		}
	}

	for (i=0;i<sizeof extra_modes/sizeof (struct ExtraModes);i++)
	{
		if (!str_cmpi(extra_modes[i].Name,name))
		{
			if (handle = FindDisplayInfo(theID = extra_modes[i].ModeID))
			{
				if ( GetDisplayInfoData(handle,(void *)&dispinfo,
					sizeof dispinfo,DTAG_DISP, NULL) )
				{
					return (dispinfo.NotAvailable ? INVALID_ID : theID);
				}
			}
		}
	}

	return INVALID_ID;
}

/*	Here a test program for the above...									*/

#if 0
void main(int argc,char *argv[])
{	LONG	id;

	if ( (GfxBase = OpenLibrary("graphics.library",36L)) == NULL ) exit(0);

	if (argc > 1)
	{
		id = GetModeIDByName(argv[1]);
		if (id == INVALID_ID) printf("The ID is invalid on this system.\n");
		else printf("The ID for this mode is %08lx.\n",id);
	}

	CloseLibrary(GfxBase);

	exit (0);
}
#endif
