
#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/displayinfo.h>

#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>

#define max(a,b)    ((a)>(b)?(a):(b))
#define min(a,b)    ((a)<=(b)?(a):(b))

LONG cleanExit ( LONG );

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;

main()
{
    if ( !( IntuitionBase = OpenLibrary ( "intuition.library", 37L ) ) )
        cleanExit ( 20 );
    if ( !( GfxBase = OpenLibrary ( "graphics.library", 37L ) ) )
        cleanExit ( 20 );

    ULONG screen_modeID;
    LONG width = 640, height = 200, top = 0, left = 0;

    struct Screen *DefPubScreen;
    struct Rectangle rect;

    Forbid();

    if ( DefPubScreen = LockPubScreen ( NULL ) )
    {
        screen_modeID = GetVPModeID ( &DefPubScreen->ViewPort );
        if ( screen_modeID != INVALID_ID )
        {
            if ( QueryOverscan ( screen_modeID, &rect, OSCAN_TEXT ) )
            {
                left   = max(0, -DefPubScreen->LeftEdge );
                top    = max(0, -DefPubScreen->TopEdge );

                width  = rect.MaxX - rect.MinX + 1;
                height = rect.MaxY - rect.MinY + 1;

                if ( DefPubScreen->TopEdge > 0 )
                    height -= DefPubScreen->TopEdge;

                width  = min(width,  DefPubScreen->Width);
                height = min(height, DefPubScreen->Height);
            }
        }
        printf ( "The default Public Screen's details are as follows!\n" );
        printf ( "Screen Left Edge is %d\n", DefPubScreen->LeftEdge );
        printf ( "Screen Top Edge is %d\n", DefPubScreen->TopEdge );
        printf ( "Screen Width is %d\n", DefPubScreen->Width );
        printf ( "Screen Height is %d\n", DefPubScreen->Height );
        printf ( "Screen title is \"%s\"\n", DefPubScreen->Title );
        printf ( "Screen default title is \"%s\"\n", DefPubScreen->DefaultTitle );
        printf ( "Screen default font is \"%s\"\n", DefPubScreen->Font->ta_Name );
        printf ( "\n" );
        printf ( "Mouse is at co-ords (%d,%d)\n", DefPubScreen->MouseX, DefPubScreen->MouseY );
        printf ( "\n" );
        printf ( "Displaying co-ords from (%d,%d) - (%d,%d)\n", rect.MinX + left, rect.MinY + top, rect.MaxX + left, rect.MaxY + top );
        printf ( "The screens viewable height is %d, while its width is %d\n", height, width );
        UnlockPubScreen ( NULL, DefPubScreen );
    }
    Permit();
    cleanExit ( 0 );
}

LONG cleanExit ( LONG returnValue )
{
    if ( GfxBase )              CloseLibrary ( GfxBase );
    if ( IntuitionBase )        CloseLibrary ( IntuitionBase );

    exit ( returnValue );
}

