/*
	name:	platform.c

	Misc. Amiga specific code
	-------------------------


    This source-code is part of the RayLab 1.1 package, and it is provided
    for your compiling pleasure.  You may use it, change it, re-compile it
    etc., as long as nobody else but you receive the changes/compilations
    that you have made!

    You may not use any part(s) of this source-code for your own productions
    without the permission from the author of RayLab. Please read the legal
    information found in the users documentation for RayLab for more details.

*/


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

#include  <exec/libraries.h>		/* AmigaOS specific includes */
#include  <libraries/locale.h>
#include  <dos/dosextens.h>

#include  <proto/exec.h>		/* AmigaOS prototypes */
#include  <proto/locale.h>
#include  <proto/dos.h>

#include  "defs.h"
#include  "amidefs.h"
#include  "extern.h"



#define MIN_STACK_SIZE 40000L
#define TBUF_SIZE      16

void GetLanguage(void);
void BreakHandler(int SigNum);
void FPErrHandler(int SigNum);


static char *Version = "$VER: RayLab 1.1 (c)1995-1996 by Marcus Geelnard";

/*
   Locale language support (requires AmigaOS 2.1 or higher)

   I chosed to keep the "catalog" inside the source-code, so that
   external modules are not needed... Will perhaps change this(?)
   It was only for fun, really, anyway. Feel free to add any language
   of your choise.
*/

	char *MyCatalog_eng[] = {
		"RayLab 1.1 Display",
		"RayLab - Chose display type",
		" 0 - No display [default]\n",
		" 1 - Select from requester\n",
		" 2 - Grey (16 colors)    Hires Interlaced\n",
		" 3 - HAM6 (4096 colors)  Lores\n",
		" 4 - HAM6 (4096 colors)  Lores Interlaced\n",
		" 5 - HAM8 (16M colors)   Lores\n",
		" 6 - HAM8 (16M colors)   Lores Interlaced\n",
		" 7 - HAM8 (16M colors)   Hires\n",
		" 8 - HAM8 (16M colors)   Hires Interlaced\n",
		"Warning: Could not open intuition.library (v36+); no display possible\n\n",
		"Warning: Could not open graphics.library (v36+); no display possible\n\n",
		"Warning: Unsupported display type (%ld), no display opened\n\n",
		"\n*** RayLab was interrupted. Closing down...\n",
		"\n*** Floating point error. Closing down...\n",
		"Warning: Could not open display window\n\n",
		"Warning: Could not open display screen\n\n",
		"Warning: True-color displays not yet supported\n\n",
		"Menu",
		" About",
		" Close",
		"Available display-modes:\n-----------------------\n\n",
		"CON:10/50/620/140/RayLab 1.1 output window",
		"Error: Stack is too small! Use at least %ld bytes for stack\n",
		"\n** Press return to exit RayLab **"
	};

	char *MyCatalog_sve[] = {
		"RayLab 1.1 Bildskärm",
		"RayLab - Välj skärmläge",
		" 0 - Ingen bildskärm [standard]\n",
		" 1 - Välj skärmläge med dialogruta\n",
		" 2 - Grå  (16 färger)    Högupplösning Interlace\n",
		" 3 - HAM6 (4096 färger)  Lågupplösning\n",
		" 4 - HAM6 (4096 färger)  Lågupplösning Interlace\n",
		" 5 - HAM8 (16M färger)   Lågupplösning\n",
		" 6 - HAM8 (16M färger)   Lågupplösning Interlace\n",
		" 7 - HAM8 (16M färger)   Högupplösning\n",
		" 8 - HAM8 (16M färger)   Högupplösning Interlace\n",
		"Varning: Kunde inte öppna intuition.library (v36+); ingen bildskärm möjlig\n\n",
		"Varning: Kunde inte öppna graphics.library (v36+); ingen bildskärm möjlig\n\n",
		"Varning: Bildskärms-typ ej understödd (%ld), ingen bildskärm öppnad\n\n",
		"\n*** RayLab blev avbruten. Stänger ned...\n",
		"\n*** Flyttals fel. Stänger ned...\n",
		"Varning: Kunde inte öppna bildskärms-fönster\n\n",
		"Varning: Kunde inte öppna bildskärm\n\n",
		"Varning: Fullfärgs skärmar stöds ej av RayLab än\n\n",
		"Meny",
		" Om",
		" Stäng",
		"Tillgängliga bildskärms-lägen:\n------------------------------\n\n",
		"CON:10/50/620/140/RayLab 1.1 utskrifts fönster",
		"Fel: Stacken är för liten! Använd minst %ld byte stack\n",
		"\n** Tryck på vagn-retur för att avsluta RayLab **"
	};

	char **MyCatalogs[] = {
		MyCatalog_eng,
		MyCatalog_sve
	};


/* Some global variables */

	struct LocaleBase *LocaleBase = NULL;
	struct Library *DosBase = NULL;
	int	Language = LANG_ENGLISH;
	int	FromCLI = TRUE;
	char	*TextBuffer = NULL;

/******************************************************************
 *
 *  InitPlatform()
 *
 ******************************************************************/

void InitPlatform(void)
{
	struct Process *MyProc;

	/* Determine locale language, if possible */
	GetLanguage();


	/* Determine some things about our process */

/*
	MyProc = (struct Process *)FindTask(NULL);
	if((MyProc->pr_CLI) != NULL) FromCLI = TRUE;
	else FromCLI = FALSE;
*/

        if( Output() ) FromCLI = TRUE;
         else          FromCLI = FALSE;

	if(FromCLI == TRUE) {
	    textoutput = stderr;
	}
	else {
	    textoutput = fopen(MyCatalogs[Language][CTXT_WINNAME],"w+");
	}

        if(!FromCLI)

	/* Reduce text output stream buffer-size (for libnix) */
	TextBuffer = malloc((size_t)TBUF_SIZE);
	if(TextBuffer != NULL) {
	    fflush(textoutput);
	    setvbuf(textoutput,TextBuffer,_IOFBF,TBUF_SIZE);
	}

/*
	if((( MyProc->pr_Task.tc_SPUpper - MyProc->pr_Task.tc_SPLower) + 4) < MIN_STACK_SIZE) {
	    fprintf(textoutput,MyCatalogs[Language][CTXT_STACK],MIN_STACK_SIZE);
	    cleanup();
	    exit(1);
	}
*/

	/* Install break-handlers */
	(void) signal(SIGINT, (void *)BreakHandler);
	(void) signal(SIGTERM, (void *)BreakHandler);
	(void) signal(SIGFPE, (void *)FPErrHandler);
}


/******************************************************************
 *
 *  CloseDownPlatform()
 *
 ******************************************************************/

void CloseDownPlatform(void)
{
	if(FromCLI == FALSE) {
	    fprintf(textoutput,MyCatalogs[Language][CTXT_EXIT]);
	    (void)fgetc(textoutput);
	    fclose(textoutput);
	}
	if(TextBuffer != NULL) {
	    fflush(textoutput);
	    setvbuf(textoutput,NULL,_IONBF,NULL);
	    free(TextBuffer);
	    TextBuffer = NULL;
	}
}


/***************************************************************
 *
 *	GetLanguage() (yes! for localization under WB 2.1+)
 *
 ***************************************************************/

void GetLanguage(void)
{
	struct Locale *loc;

	Language = LANG_ENGLISH;	/* Default */

	LocaleBase = (struct LocaleBase *)OpenLibrary("locale.library",38L);
	if(LocaleBase!=NULL) {
	    loc = OpenLocale(NULL);
	    if(loc!=NULL) {
		if(strcmp(loc->loc_LanguageName,"english.language")==0)
			Language = LANG_ENGLISH;
		else if(strcmp(loc->loc_LanguageName,"svenska.language")==0)
			Language = LANG_SVENSKA;
	    }
	    CloseLibrary((struct Library *)LocaleBase);
	}
}


/******************************************************************

    Interrupt handlers

 ******************************************************************/

/* This routine is called if CTRL-C is issued */

void BreakHandler(int SigNum)
{
	fprintf(textoutput,MyCatalogs[Language][CTXT_CTRLC]);
	cleanup();
	exit(1);		/* Exit RayLab after this */
}

/* This routine is called in the case of a floating point error */

void FPErrHandler(int SigNum)
{
	fprintf(textoutput,MyCatalogs[Language][CTXT_FPERR]);
	cleanup();
	exit(1);		/* Exit RayLab after this */
}

