/*
**      $VER: CheckForUAE.c 1.00 (28.12.97)
**
**      Check, whether running in an UAE environment w/ P96
**
**      (C) Copyright 1997 Andreas R. Kleinert
**      Freeware. All Rights Reserved.
*/

#define __USE_SYSBASE

#include <exec/types.h>
#include <exec/memory.h>

#include <intuition/intuition.h>
#include <graphics/gfxbase.h>
#include <graphics/displayinfo.h>
#include <graphics/modeid.h>

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#ifndef N
#define N (NULL)
#endif /* N */


char ver_text [] = "\0$VER: CheckForUAE 1.00 (28.12.97)";


/* *************************************************** */
/* *                                                 * */
/* * Additional Base Declarations                    * */
/* *                                                 * */
/* *************************************************** */

extern struct ExecBase *SysBase;

struct IntuitionBase *IntuitionBase = N;
struct GfxBase       *GfxBase       = N;


/* *************************************************** */
/* *                                                 * */
/* * MAIN                                            * */
/* *                                                 * */
/* *************************************************** */

void __regargs __chkabort(void) { }
void __regargs _CXBRK(void)     { }


ULONG RunningUnderUAE(void);

void main(long argc, char **argv)
{
 ULONG error = 0;

 IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
 if(IntuitionBase)
  {
   GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 37);
   if(GfxBase)
    {
     if(RunningUnderUAE()) printf("\n We are running under UAE with P96\n\n");
      else                 printf("\n We are probably not running under UAE with P96\n\n");
    }else error = 20;

   CloseLibrary((APTR) IntuitionBase);

  }else error = 20;

 exit(error);
}

ULONG IsUAEModeID(ULONG mode_id);

ULONG RunningUnderUAE(void)
{
 ULONG mode_id  = INVALID_ID;
 ULONG retval   = FALSE;

 for(;;)
  {
   mode_id = NextDisplayInfo(mode_id);
   if(mode_id == INVALID_ID) break;

   retval = IsUAEModeID(mode_id);
   if(retval) break;
  }

 return(retval);
}

ULONG IsUAEModeID(ULONG mode_id)
{
 APTR                   mode_handle  = N;
 struct DimensionInfo  *mode_diminfo = N;
 struct NameInfo       *mode_naminfo = N;
 long                   mode_dimsize = sizeof(struct DimensionInfo);
 long                   mode_namsize = sizeof(struct NameInfo);
 ULONG                  mode_result  = N;
 ULONG                  retval       = FALSE;

 mode_handle = FindDisplayInfo(mode_id);
 if(mode_handle)
  {
   mode_diminfo = (APTR) AllocVec(mode_dimsize, (MEMF_CLEAR|MEMF_PUBLIC));
   mode_naminfo = (APTR) AllocVec(mode_namsize, (MEMF_CLEAR|MEMF_PUBLIC));

   if(mode_diminfo && mode_naminfo)
    {
     mode_result = GetDisplayInfoData(mode_handle, (UBYTE *) mode_diminfo, mode_dimsize, DTAG_DIMS, N);
     mode_result = GetDisplayInfoData(mode_handle, (UBYTE *) mode_naminfo, mode_namsize, DTAG_NAME, N);

     if(mode_result && (!ModeNotAvailable(mode_id)))
      {
       if(!strnicmp(mode_naminfo->Name, "UAEGFX", 6)) retval = TRUE;

       /* further checks could be added here, when necessary */
      }
    }

   if(mode_diminfo) FreeVec(mode_diminfo);
   if(mode_naminfo) FreeVec(mode_naminfo);
  }

 return(retval);
}
