//============================================================
//============================================================
//==														==
//==				Environ-Checking Routines				==
//==														==
//============================================================
//============================================================


//========================================================  Includes  

#include "Globals.h"
#include "UnivUtilities.h"
#include "Environ.h"


//========================================================  Functions  
//========================================================  TrapExists  

Boolean TrapExists (short trapNumber)

{
	return ((NGetTrapAddress(trapNumber, ToolTrap) !=
		NGetTrapAddress(kUnimpTrap, ToolTrap)));
		
}


//========================================================  DoWeHaveGestalt  

Boolean DoWeHaveGestalt (void)

{
	return (TrapExists(kGestaltTrap));
}


//========================================================  DoWeHaveWNE  

Boolean DoWeHaveWNE (void)

{
	return (TrapExists(kWNETrap));
}


//========================================================  DoWeHaveColor  

Boolean DoWeHaveColor (void)

{
	SysEnvRec		thisWorld;
	
	SysEnvirons(2, &thisWorld);
	return (thisWorld.hasColorQD);
}


//========================================================  DoWeHaveSystem602  

Boolean DoWeHaveSystem602 (void)

{
	SysEnvRec		thisWorld;
	Boolean			haveIt;
	
	SysEnvirons(2, &thisWorld);
	if (thisWorld.systemVersion >= 0x0602)
		haveIt = TRUE;
	else
		haveIt = FALSE;
	return (haveIt);
}


//========================================================  DoWeHaveSystem605  

Boolean DoWeHaveSystem605 (void)

{
	SysEnvRec		thisWorld;
	Boolean			haveIt;
	
	SysEnvirons(2, &thisWorld);
	if (thisWorld.systemVersion >= 0x0605)
		haveIt = TRUE;
	else
		haveIt = FALSE;
	return (haveIt);
}


//========================================================  DoWeHaveSystem7  

Boolean DoWeHaveSystem7 (void)

{
	SysEnvRec		thisWorld;
	Boolean			haveIt;
	
	SysEnvirons(2, &thisWorld);
	if (thisWorld.systemVersion >= 0x0700)
		haveIt = TRUE;
	else
		haveIt = FALSE;
	return (haveIt);
}


//========================================================  HowWillWeDisplay  

short HowWillWeDisplay (Boolean commandKeyHeld)

{
	short			displayAs, isWide, isTall;
	
	isWide = screenBits.bounds.right - screenBits.bounds.left;
	isTall = screenBits.bounds.bottom - screenBits.bounds.top;
	
	if (commandKeyHeld)
	{
		if ((isWide >= 512) && (isTall >= 384) && isColor)
			displayAs = kDisplay12Inch;
		else
			displayAs = kDisplay9Inch;
	}
	else
	{
		if ((isWide >= 640) && (isTall >= 480))
			displayAs = kDisplay13Inch;
		else
		{
			if ((isWide >= 512) && (isTall >= 384) && isColor)
				displayAs = kDisplay12Inch;
			else
				displayAs = kDisplay9Inch;
		}
	}
	
	return(displayAs);
}


//========================================================  WhatsOurDepth  

short WhatsOurDepth (void)

{
	GDHandle		theDevice;
	short			thisDepth;
	char			wasState;
	
	if (thisMac.hasColor)
	{
		theDevice = GetMainDevice();
		if (theDevice != kNilPointer)
		{
			wasState = HGetState((Handle)theDevice);
			HLock((Handle)theDevice);
			thisDepth = (**(**theDevice).gdPMap).pixelSize;
			HSetState((Handle)theDevice, wasState);
		}
		else
			DeathError(kErrNoDevice);
	}
	else
		thisDepth = 1;
	
	return (thisDepth);
}


//========================================================  CanWeDisplay4Bit  

Boolean CanWeDisplay4Bit (void)

{
	GDHandle		theDevice;
	short			canDepth;
	short			GDTypeFlag = 1, colorFlag = 1;
	Boolean			canDo;
	
	canDo = FALSE;
	if (thisMac.hasColor && thisMac.canSwitch)
	{
		theDevice = GetMainDevice();
		if (theDevice != kNilPointer)
		{
			HLock((Handle)theDevice);
			canDepth = HasDepth(theDevice, 4, GDTypeFlag, colorFlag);
			HUnlock((Handle)theDevice);
			if (canDepth != 0)
				canDo = TRUE;
		}
		else
			DeathError(kErrNoDevice);
	}
	
	return (canDo);
}


//========================================================  SwitchDepthOrAbort  

void SwitchDepthOrAbort (void)

{
	GDHandle		theDevice;
	AlertTHndl		alertHandle;
	Rect			alertRect;
	OSErr			theErr;
	short			usersDecision, dummyInt;
	short			GDTypeFlag = 1, colorFlag = 1;
	char			tagByte;
	
	if (thisMac.canSwitch)
	{
		alertHandle = (AlertTHndl)GetResource('ALRT', rSwitchDepthAlert);
		if (alertHandle != kNilPointer)
		{
			HNoPurge((Handle)alertHandle);
			alertRect = (**alertHandle).boundsRect;
			OffsetRect(&alertRect, -alertRect.left, -alertRect.top);
			dummyInt = (screenBits.bounds.right - alertRect.right) / 2;
			OffsetRect(&alertRect, dummyInt, 0);
			dummyInt = (screenBits.bounds.bottom - alertRect.bottom) / 3;
			OffsetRect(&alertRect, 0, dummyInt);
			(**alertHandle).boundsRect = alertRect;
			HPurge((Handle)alertHandle);
		}
		else
		{
		}
		usersDecision = Alert(rSwitchDepthAlert, kNilPointer);
		switch (usersDecision)
		{
			case 1:
				theDevice = GetMainDevice();
				if (theDevice != kNilPointer)
				{
					tagByte = HGetState((Handle)theDevice);
					HLock((Handle)theDevice);
					theErr = SetDepth(theDevice, 4, GDTypeFlag, colorFlag);
					HSetState((Handle)theDevice, tagByte);
					if (theErr != noErr)
						DeathError(kErrFailedSwitch);
					else
						thisMac.isDepth = 4;
				}
				else
					DeathError(kErrNoDevice);
				break;
			case 2:
				theDevice = GetMainDevice();
				if (theDevice != kNilPointer)
				{
					tagByte = HGetState((Handle)theDevice);
					HLock((Handle)theDevice);
					theErr = SetDepth(theDevice, 1, GDTypeFlag, colorFlag);
					HSetState((Handle)theDevice, tagByte);
					if (theErr != noErr)
						DeathError(kErrFailedSwitch);
					else
						thisMac.isDepth = 1;
				}
				else
					DeathError(kErrNoDevice);
				break;
			case 3:
				ExitToShell();
				break;
		}
	}
	else
		DeathError(kErr2or16Colors);
}


//========================================================  CheckOurEnvirons  

void CheckOurEnvirons (void)

{
	Boolean			commandKeyPressed;
	
	commandKeyPressed = CommandKeyIsDown();
	
	thisMac.hasGestalt = DoWeHaveGestalt();
	thisMac.hasWNE = DoWeHaveWNE();
	thisMac.hasColor = DoWeHaveColor();
	knowsColor = thisMac.hasColor;
	if (!(DoWeHaveSystem602()))
	{
		DeathError(kErrNeedSystem602);
	}
	thisMac.canSwitch = DoWeHaveSystem605();
	thisMac.hasSystem7 = DoWeHaveSystem7();
	thisMac.wasDepth = WhatsOurDepth();
	thisMac.canColor = CanWeDisplay4Bit();
	if ((thisMac.wasDepth != 4) && (thisMac.wasDepth != 1) && (thisMac.canColor))
	{
		SwitchDepthOrAbort();
	}
	else
	{
		thisMac.isDepth = thisMac.wasDepth;
	}
	isColor = (thisMac.isDepth != kDisplay1Bit);
	displayMode = HowWillWeDisplay(commandKeyPressed);
}


