
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <clib/dos_protos.h>
#include <stdio.h>
#include <string.h>
#include <exec/lists.h>

#include <libraries/reqtools.h>

#include <functions.h>

static char version_string[] = "$VER: rtScreenModeRequest 2.3 (16.11.94)";

char MyExtHelp[] =
"\n"
"Usage: rtScreenModeRequest\n"
"\n"
" TITLE,OVERSCANGAD/S,AUTOSCROLLGAD/S,SIZEGADS/S,DEPTHGAD/S,NONSTDMODES/S,\n"
" GUIMODES/S,HEIGHT/N/K,OKTEXT/K,MINWIDTH/N/K,MAXWIDTH/N/K,MINHEIGHT/N/K,\n"
" MAXHEIGHT/N/K,MINDEPTH/N/K,MAXDEPTH/N/K,PROPERTYFLAGS/N/K,PROPERTYMASK/N/K,\n"
" POSITION=POS/K,TOPOFFSET=TOP/N/K,LEFTOFFSET=LEFT/N/K,PUBSCREEN/K\n" 
"\n"
"  TITLE        <text> - Title text for requester window.\n"
"  OVERSCANGAD         - Add an overscan gadget to the requester.\n"
"  AUTOSCROLLGAD       - Add an autoscroll checkbox gadget to the requester.\n"
"  SIZEGADS            - Add width and height gadgets to the requester.\n"
"  DEPTHGAD            - Add a depth slider gadget to the requester.\n"
"  NONSTDMODES         - Include all modes. Unless set, rtScreenModeRequest\n"
"                        will exclude nonstandard modes. (presently HAM and\n"
"                        EHB (ExtraHalfBrite))\n"
"  GUIMODES            - Get screen mode to open a user interface screen in.\n"
"  HEIGHT        <num> - Suggested height of screenmode requester window.\n"
"  OKTEXT       <text> - Replacement text for \"Ok\" gadget, max 6 chars.\n"
"  MINWIDTH      <num> - The minimum display width allowed.\n"
"  MAXWIDTH      <num> - The maximum display width allowed.\n"
"  MINHEIGHT     <num> - The minimum display height allowed.\n"
"  MAXHEIGHT     <num> - The maximum display height allowed.\n"
"  MINDEPTH      <num> - The minimum display depth allowed.\n"
"  MAXDEPTH      <num> - The maximum display depth allowed.\n"
"  PROPERTYFLAGS <num> - A mode must have these property flags to be included.\n"
"  PROPERTYMASK  <num> - Mask to apply to PropertyFlags to determine which bits\n"
"                        to consider.\n"
"  POSITION   <pos> - Open requester window at <pos>.  Permissible values are:\n"
" >or POS             CENTERSCR, TOPLEFTSCR, CENTERWIN, TOPLEFTWIN, POINTER.\n"
"  TOPOFFSET  <num> - Offset position relative to above.\n"
" >or TOP\n"
"  LEFFTOFFSET <num>- Offset position relative to above.\n"
" >or LEFT\n"
"  PUBSCREEN <screen> - Public screen name.\n";

#define TEMPLATE "TITLE/A,OVERSCANGAD/S,AUTOSCROLLGAD/S,SIZEGADS/S,DEPTHGAD/S,NONSTDMODES/S,GUIMODES/S,HEIGHT/N/K,OKTEXT/K,MINWIDTH/N/K,MAXWIDTH/N/K,MINHEIGHT/N/K,MAXHEIGHT/N/K,MINDEPTH/N/K,MAXDEPTH/N/K,PROPERTYFLAGS/N/K,PROPERTYMASK/N/K,POSITION=POS/K,TOPOFFSET=TOP/N/K,LEFTOFFSET=LEFT/N/K,PUBSCREEN/K"


#define OPT_TITLE 0
#define OPT_OVERSCANGAD 1
#define OPT_AUTOSCROLLGAD 2
#define OPT_SIZEGADS 3
#define OPT_DEPTHGAD 4
#define OPT_NONSTDMODES 5
#define OPT_GUIMODES 6
#define OPT_HEIGHT 7
#define OPT_OKTEXT 8
#define OPT_MINWIDTH 9
#define OPT_MAXWIDTH 10
#define OPT_MINHEIGHT 11
#define OPT_MAXHEIGHT 12
#define OPT_MINDEPTH 13
#define OPT_MAXDEPTH 14
#define OPT_PROPERTYFLAGS 15
#define OPT_PROPERTYMASK 16
#define OPT_POSITION 17
#define OPT_TOPOFFSET 18
#define OPT_LEFTOFFSET 19
#define OPT_PUBSCREEN 20
#define OPT_COUNT 21

struct Library *ReqToolsBase;

int
main (int argc, char **argv)
{
  int retval = 0;

  if (argc)
    {
      if ((argc == 2) && (stricmp (argv[1], "HELP") == 0))
	{
	  fprintf (stdout, "%s", MyExtHelp);
	}
      else
	{
	  /* My custom RDArgs */
	  struct RDArgs *myrda;

	  /* Need to ask DOS for a RDArgs structure */
	  if (myrda = (struct RDArgs *) AllocDosObject (DOS_RDARGS, NULL))
	    {
	      /*
	       * The array of LONGs where ReadArgs() will store the data from
	       * the command line arguments.  C guarantees that all the array
	       * entries will be set to zero.
	       */
	      LONG *result[OPT_COUNT] =
	      {NULL};

	      /* parse my command line */
	      if (ReadArgs (TEMPLATE, result, myrda))
		{
		  if (ReqToolsBase = (struct Library *) OpenLibrary ("reqtools.library", LIBRARY_MINIMUM))
		    {
		      struct rtReqInfo *requester;

		      if (requester = (struct rtReqInfo *) rtAllocRequest (RT_SCREENMODEREQ, NULL))
			{
			  ULONG myRTSC_Flags = 0;
			  /*
			   * Take care of requester positioning -
			   * These flags common to most rt requesters
			   */
			  if (stricmp (result[OPT_POSITION], "CENTERSCR") == 0)
			    requester->ReqPos = REQPOS_CENTERSCR;
			  else if (stricmp (result[OPT_POSITION], "TOPLEFTSCR") == 0)
			    requester->ReqPos = REQPOS_TOPLEFTSCR;
			  else if (stricmp (result[OPT_POSITION], "CENTERWIN") == 0)
			    requester->ReqPos = REQPOS_CENTERWIN;
			  else if (stricmp (result[OPT_POSITION], "TOPLEFTWIN") == 0)
			    requester->ReqPos = REQPOS_TOPLEFTWIN;
			  else
			    requester->ReqPos = REQPOS_POINTER;

			  myRTSC_Flags =
			    ((result[OPT_OVERSCANGAD]) ? SCREQF_OVERSCANGAD : NULL) |
			    ((result[OPT_AUTOSCROLLGAD]) ? SCREQF_AUTOSCROLLGAD : NULL) |
			    ((result[OPT_SIZEGADS]) ? SCREQF_SIZEGADS : NULL) |
			    ((result[OPT_DEPTHGAD]) ? SCREQF_DEPTHGAD : NULL) |
			    ((result[OPT_NONSTDMODES]) ? SCREQF_NONSTDMODES : NULL) |
			    ((result[OPT_GUIMODES]) ? SCREQF_GUIMODES : NULL);

			  {
			    struct Process *process = (struct Process *) FindTask (NULL);
			    APTR windowptr = process->pr_WindowPtr;

			    /*
			     * Tags will be used to take care ofattributes not directly
			     * settable in the rtReqInfo structure
			     */

			    retval = rtScreenModeRequest (requester, (result[OPT_TITLE]) ? result[OPT_TITLE] : NULL,

				   (myRTSC_Flags) ? RTSC_Flags : TAG_IGNORE,
							  myRTSC_Flags,

			    (result[OPT_HEIGHT]) ? RTSC_Height : TAG_IGNORE,
							*result[OPT_HEIGHT],
			    (result[OPT_OKTEXT]) ? RTSC_OkText : TAG_IGNORE,
							  result[OPT_OKTEXT],
							  (result[OPT_MINWIDTH]) ? RTSC_MinWidth : TAG_IGNORE,
						      *result[OPT_MINWIDTH],
							  (result[OPT_MAXWIDTH]) ? RTSC_MaxWidth : TAG_IGNORE,
						      *result[OPT_MAXWIDTH],
							  (result[OPT_MINHEIGHT]) ? RTSC_MinHeight : TAG_IGNORE,
						     *result[OPT_MINHEIGHT],
							  (result[OPT_MAXHEIGHT]) ? RTSC_MaxHeight : TAG_IGNORE,
						     *result[OPT_MAXHEIGHT],
							  (result[OPT_MINDEPTH]) ? RTSC_MinDepth : TAG_IGNORE,
						      *result[OPT_MINDEPTH],
							  (result[OPT_MAXDEPTH]) ? RTSC_MaxDepth : TAG_IGNORE,
						      *result[OPT_MAXDEPTH],
							  (result[OPT_PROPERTYFLAGS]) ? RTSC_PropertyFlags : TAG_IGNORE,
						 *result[OPT_PROPERTYFLAGS],
							  (result[OPT_PROPERTYMASK]) ? RTSC_PropertyMask : TAG_IGNORE,
						  *result[OPT_PROPERTYMASK],


			    /*
			     * Finally,
			     * set some more general tags shared by most requesters
			     */
							  RT_Underscore, '_',
							  (result[OPT_TOPOFFSET]) ? RT_TopOffset : TAG_IGNORE,
						     *result[OPT_TOPOFFSET],
							  (result[OPT_LEFTOFFSET]) ? RT_LeftOffset : TAG_IGNORE,
						    *result[OPT_LEFTOFFSET],
			    (windowptr) ? RT_Window : TAG_IGNORE,
			    windowptr,
							  (result[OPT_PUBSCREEN]) ? RT_PubScrName : TAG_IGNORE,
						      result[OPT_PUBSCREEN],
							  TAG_END);

			  }
			  rtFreeRequest (requester);
			}
		      else
			{
			  /* Unable to allocate request structure */
			  retval = 20;
			}
		      CloseLibrary (ReqToolsBase);
		    }
		  else
		    {
		      /* Unable to open reqtools.library */
		      fprintf (stderr, "rtScreenModeRequest: Unable to open reqtools.library.\n");
		      retval = 20;
		    }
		  FreeArgs (myrda);
		}
	      else
		{
		  /*
		   * Something went wrong in the parse
		   */

		  if (result[OPT_TITLE] == NULL)
		    {
		      fprintf (stderr, "rtScreenModeRequest: Required argument missing.\n");
		    }
		  else
		    {
		      /* something else is wrong */
		      fprintf (stderr, "rtScreenModeRequest: Command syntax error.\n");
		    }

		  fprintf (stderr, "Try - rtScreenModeRequest help.\n");

		  retval = 20;
		}
	      FreeDosObject (DOS_RDARGS, myrda);
	    }
	  else
	    {
	      /* Unable to alloc readargs structure */
	      retval = 20;
	    }
	}
    }
  else
    {
      /* launched from workbench */
    }

  return (retval);
}
