/*************************************************************/
/* Includes and other common stuff for the MUI demo programs */
/*************************************************************/

/* MUI */
#include <libraries/mui.h>

/* System */
#include <dos.h>
#include <dos/dos.h>
#include <graphics/gfxmacros.h>
#include <workbench/workbench.h>

/* Prototypes */
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/icon_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/muimaster_protos.h>
#include <clib/asl_protos.h>

/* ANSI C */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>


/* Compiler specific stuff */

#ifdef _DCC

#define REG(x) __ ## x
#define ASM
#define SAVEDS __geta4

#else

#define REG(x) register __ ## x
#define ASM    __asm
#define SAVEDS __saveds

#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/graphics_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/gadtools_pragmas.h>
#include <pragmas/muimaster_pragmas.h>
#include <pragmas/asl_pragmas.h>

extern struct Library *IntuitionBase,*UtilityBase,*GfxBase,*DOSBase,*IconBase;
static struct Library *MUIMasterBase;

#endif

void shutdown_icon(void);


/*************************/
/* Init & Fail Functions */
/*************************/

static VOID fail(APTR app,char *str)
{
  int muierror = MUIE_OK;

  shutdown_icon();

  if (MUIMasterBase) muierror = MUI_Error();
  else printf("Can't open %s\n", MUIMASTER_NAME);

  if (app) {
    MUI_DisposeObject(app);
  }

#ifndef _DCC
	if (MUIMasterBase)
		CloseLibrary(MUIMasterBase);
#endif

  if (str) {
    switch (muierror) {
    case MUIE_OK:
      break;
    case MUIE_OutOfMemory:
      puts("I ran out of memory :(");
      break;
    case MUIE_OutOfGfxMemory:
      puts("I ran out of graphics memory :(");
      break;
    case MUIE_InvalidWindowObject:
      puts("Bad window object.  Please contact the author.");
      break;
    case MUIE_MissingLibrary:
      puts("Can't open a necessary library.");
      break;
    case MUIE_NoARexx:
      puts("Can't open an ARexx port!");
      break;
    case MUIE_SingleTask:
      puts("This program is already running!");
      break;
    }
    puts(str);
    exit(20);
  }
  exit(0);
}

static VOID init(int argc, char* argv[])
{
	extern void workbench_startup(char**);
	extern void shell_startup(void);
	extern struct DiskObject* AVM_DiskObject;

#ifndef _DCC
	if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
		fail(NULL,"Failed to open "MUIMASTER_NAME".");
#endif
	
	if (argc == 0) workbench_startup(argv);
	else shell_startup();
}

#ifndef __SASC
static VOID stccpy(char *dest,char *source,int len)
{
	strncpy(dest,source,len);
	dest[len-1]='\0';
}

int wbmain(struct WBStartup *wb_startup)
{
	extern int main(int argc, char *argv[]);
	return (main(0, NULL));
}
#endif

#ifdef __SASC
int CXBRK(void) { return(0); }
int _CXBRK(void) { return(0); }
void chkabort(void) { }
#endif

LONG __stack = 8192;

/* some macros */

#define StringDTMF(contents,maxlen)\
	StringObject,\
		StringFrame,\
		MUIA_String_MaxLen  , maxlen,\
		MUIA_String_Contents, contents,\
                MUIA_String_Accept, "0123456789#*", \
		End

#define StringTelNo(contents,maxlen)\
	StringObject,\
		StringFrame,\
		MUIA_String_MaxLen  , maxlen,\
		MUIA_String_Contents, contents,\
                MUIA_String_Accept, "0123456789#*,@Ww()-", \
		End

#define StringNoSpaces(contents,maxlen)\
	StringObject,\
		StringFrame,\
		MUIA_String_MaxLen  , maxlen,\
		MUIA_String_Contents, contents,\
                MUIA_String_Accept, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789", \
		End

