
#include <stdio.h>
#include <stdlib.h>
#include <utility/tagitem.h>
#include <exec/types.h>
#include <clib/exec_protos.h>
#include <workbench/startup.h>
#include <intuition/intuition.h>
#include <intuition/screens.h>
#include <clib/intuition_protos.h>
#include <clib/utility_protos.h>
#include <graphics/displayinfo.h>
#include <graphics/view.h>
#include <string.h>

int main(int argc, char *argv[]);
int wbmain(struct WBStartup *);
extern struct IntuitionBase *IntuitionBase;
extern int LVOOpenScreen;
struct Screen *MyOpenScreen(__A0 struct NewScreen *, __A6 struct IntuitionBase *);
extern stubMyOpenScreen(__A0 struct NewScreen *,__A6 struct IntuitionBase *);
struct Screen * (*oldOpenScreen)(__A0 struct ExtNewScreen *, __A6 struct IntuitionBase *);

__geta4 struct Screen *MyOpenScreen(__A0 struct NewScreen *ns, __A6 struct IntuitionBase *base)
 {
 struct ExtNewScreen ens;
 struct Screen *s;
 struct TagItem tag[] = {
    { SA_DisplayID, 0 },
    { TAG_DONE, 0 }
 };
 BOOL tagged = FALSE;

 if(!(ns->Type & NS_EXTENDED))
   {
   memcpy(&ens,ns,sizeof(struct NewScreen));
   if(ens.ViewModes & LACE)
     {
     if( ((LONG)ens.Height) >= 512 )
       {
       if( ens.ViewModes & HIRES )
         tag[0].ti_Data = PAL_MONITOR_ID | HIRESLACE_KEY;
       else
         tag[0].ti_Data = PAL_MONITOR_ID | LORESLACE_KEY;
       tagged = TRUE;
       }
     }
   else if ( ((LONG)ens.Height) >= 256 )
     {
     if( ens.ViewModes & HIRES )
       tag[0].ti_Data = PAL_MONITOR_ID | HIRES_KEY;
     else
       tag[0].ti_Data = PAL_MONITOR_ID | LORES_KEY;
     tagged = TRUE;
     }
  }

 if(tagged)
   {
   ens.Type |= NS_EXTENDED;
   ens.Extension = tag;
   s = oldOpenScreen(&ens,base);
   }
 else
   s= oldOpenScreen((struct ExtNewScreen *)ns,base);

 return(s);
 }

int wbmain(struct WBStartup *wb)
  {
  main(0,NULL);
  }

int main(int argc, char *argv[])
  {
  oldOpenScreen = SetFunction(IntuitionBase,-198,stubMyOpenScreen);
  Wait(0L);
  }

