
/*
	
	$VER: listmodes.c 1.00 (07.03.96)

	Shows modeid's in gfx database

	(c) Copyright 1995-2001 Grzegorz Calkowski

	This file is part of SysPic.

	SysPic  is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	Bison is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Bison; see the file COPYING.  If not, write to
	the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/


#include <exec/types.h>
#include <exec/execbase.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <graphics/displayinfo.h>
#include <graphics/modeid.h>
#include <workbench/startup.h>

#include <other/vartools_protos.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>

#include <string.h>

void Out(STRPTR text);


extern struct ExecBase *SysBase;
struct DosLibrary       *DOSBase = NULL;
struct IntuitionBase    *IntuitionBase = NULL;
struct GfxBase          *GfxBase = NULL;

BPTR	 out;

STRPTR ver = "$VER: ListModes 1.00 (07.03.96)";


ULONG __saveds listmodes(void)
{
	struct Process	*prgproc;
	struct WBStartup *wbmsg = NULL;
	ULONG	 retval = RETURN_FAIL, id;
	BOOL	 win;
	struct NameInfo nameinfo;
	UBYTE	 buf[64];
	UWORD	 cnt = 0;

	SysBase = (*((struct ExecBase **) 4));
   prgproc = (struct Process *)FindTask(NULL);

	/* Check if we're running from CLI or not */
   if (!prgproc -> pr_CLI)
	{
		WaitPort(&prgproc->pr_MsgPort);
		wbmsg = (struct WBStartup *)GetMsg(&prgproc->pr_MsgPort);
	}

	if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 39))
	{
		if (DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 39))
		{
			out = Output();

			if (!out)
			{
				if (out = Open("RAW:120/20/380/160/ListModes Output", MODE_NEWFILE))
					win = TRUE;
			}
			else
				win = FALSE;

			if (out)
			{
				Out("\nAvailable modes:\n\n");

				id = INVALID_ID;
				while ((id = NextDisplayInfo(id)) != INVALID_ID)
					if (GetDisplayInfoData(NULL, (UBYTE *)&nameinfo, sizeof(nameinfo), DTAG_NAME, id))
					{
						SPrintF(buf, "0x%08lx: %s\n", nameinfo.Header.DisplayID, nameinfo.Name);
						Out(buf);
						++cnt;
					}
				Out(SPrintF(buf, "\n%lu modes listed.\n", cnt));

				if (win)
				{	
					UBYTE tmp;

					Out("\nPress any key to exit... ");
					Read(out, &tmp, 1);
					Close(out);
				}

				retval = RETURN_OK;
			}

			CloseLibrary((struct Library *)DOSBase);
		}
		CloseLibrary((struct Library *)GfxBase);
	}

	if (wbmsg)
	{
		Forbid();
		ReplyMsg(&wbmsg -> sm_Message);
	}

	return retval;
}


void Out(STRPTR text)
{
	Write(out, text, strlen(text));
}
