#include <exec/types.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <libraries/asl.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/utility.h>
#include <string.h>

#include "mymacros.h"

static void             PatchIt( void );
static BOOL SAVEDS ASM  MyAslRequest( REG( a0 ) APTR requester, REG( a1 ) struct TagItem *tags );

static TEXT             Version[] = "$VER: AslSizePatch 1.0 (15.9.99) ©Simone Tellini";
static UWORD            Width = 50, Height = 90, Top, Left;
static BOOL             Center;
static struct Library  *AslBase;
static ULONG ASM        ( *OldAslRequest )( REG( a0 ) APTR, REG( a1 ) struct TagItem *, REG( a6 ) struct Library * );

int main( int argc, char *argv[] )
{
    struct RDArgs  *rdargs;
    struct {
            ULONG  *Width;
            ULONG  *Height;
            ULONG   Center;
    }               Args;
    int             ret = 0;

    memset( &Args, 0, sizeof( Args ));

    if( rdargs = ReadArgs( "WIDTH/N,HEIGHT/N,CENTER/S", (LONG *)&Args, NULL )) {

        if( Args.Width )
            Width = *Args.Width;

        if( Args.Height )
            Height = *Args.Height;

        Center = Args.Center;

        PatchIt();

        FreeArgs( rdargs );

    } else {

        PrintFault( IoErr(), argv[0] );

        ret = 20;
    }

    return( ret );
}

static void PatchIt( void )
{
    struct Screen  *scr;

    if( scr = LockPubScreen( NULL )) {

        Width  = ( scr->Width  * Width  ) / 100;
        Height = ( scr->Height * Height ) / 100;
        Left   = ( scr->Width  - Width  ) / 2;
        Top    = ( scr->Height - Height ) / 2;

        UnlockPubScreen( NULL, scr );

        if( AslBase = OpenLibrary( "asl.library", 0 )) {

            (APTR)OldAslRequest = SetFunction( AslBase, -60, (APTR) MyAslRequest );

            Wait( SIGBREAKF_CTRL_C );

            SetFunction( AslBase, -60, (APTR)OldAslRequest );

            CloseLibrary( AslBase );
        }
    }
}

static BOOL SAVEDS ASM MyAslRequest( REG( a0 ) APTR requester, REG( a1 ) struct TagItem *tags )
{
    ULONG           CenterTags[] = {
                                ASLFR_InitialLeftEdge,  Left,
                                ASLFR_InitialTopEdge,   Top,
                                TAG_MORE,               (ULONG) tags
                    };
    ULONG           ExtraTags[] = {
                                ASLFR_InitialWidth,     Width,
                                ASLFR_InitialHeight,    Height,
                                TAG_MORE,               Center ? (ULONG)CenterTags : (ULONG)tags
                    };
    struct TagItem *oldleft = NULL, *oldtop = NULL, *oldwidth, *oldheight;
    BOOL            ret;

    oldwidth  = FindTagItem( ASLFR_InitialWidth,  tags );
    oldheight = FindTagItem( ASLFR_InitialHeight, tags );

    if( Center ) {
        oldleft = FindTagItem( ASLFR_InitialLeftEdge, tags );
        oldtop  = FindTagItem( ASLFR_InitialTopEdge,  tags );
    }

    if( oldleft )
        oldleft->ti_Tag = TAG_IGNORE;

    if( oldtop )
        oldtop->ti_Tag = TAG_IGNORE;

    if( oldwidth )
        oldwidth->ti_Tag = TAG_IGNORE;

    if( oldheight )
        oldheight->ti_Tag = TAG_IGNORE;

    ret = ( *OldAslRequest )( requester, (struct TagItem *) &ExtraTags, AslBase );

    if( oldleft )
        oldleft->ti_Tag = ASLFR_InitialLeftEdge;

    if( oldtop )
        oldtop->ti_Tag = ASLFR_InitialTopEdge;

    if( oldwidth )
        oldwidth->ti_Tag = ASLFR_InitialWidth;

    if( oldheight )
        oldheight->ti_Tag = ASLFR_InitialHeight;

    return( ret );
}
