2019-11-11 00:11:59 -05:00
//============================================================================
//----------------------------------------------------------------------------
// Environ.c
//----------------------------------------------------------------------------
//============================================================================
2020-04-01 14:53:44 -04:00
# include "DisplayDeviceManager.h"
2019-11-11 00:11:59 -05:00
# include "Externs.h"
# include "Environ.h"
2020-04-01 14:53:44 -04:00
# include "MenuManager.h"
2020-01-20 05:14:35 -05:00
# include "IGpDisplayDriver.h"
2020-11-25 12:05:59 -05:00
# include "IGpSystemServices.h"
2020-04-01 14:53:44 -04:00
# include "WindowManager.h"
2019-11-11 00:11:59 -05:00
2020-11-25 12:05:59 -05:00
# include "PLDrivers.h"
# include "PLResources.h"
# include "PLPasStr.h"
2020-04-18 00:18:20 -04:00
# include <algorithm>
2019-11-11 00:11:59 -05:00
# define kSwitchDepthAlert 130
# define kSetMemoryAlert 180
# define kLowMemoryAlert 181
# define kWNETrap 0x60
# define kSetDepthTrap 0xA2
# define kUnimpTrap 0x9F
# define kGestaltTrap 0xAD
# define kDisplay9Inch 1
# define kDisplay12Inch 2
# define kDisplay13Inch 3
2020-07-03 13:53:10 -04:00
extern Boolean isAutoScale , isUseICCProfile ;
2020-06-20 01:33:26 -04:00
2019-11-11 00:11:59 -05:00
typedef struct
{
short flags ;
long mem1 ;
long mem2 ;
} sizeType ;
//short GetThisVolumeRefNum (void);
//long GetThisCurrentDirectoryID (void);
//Boolean TrapExists (short);
//Boolean DoWeHaveGestalt (void);
//Boolean DoWeHaveWNE (void);
//Boolean DoWeHaveColor (void);
//Boolean DoWeHaveSystem602 (void);
//Boolean DoWeHaveSystem605 (void);
//Boolean DoWeHaveSystem7 (void);
//Boolean DoWeHaveSoundManager3 (void);
Boolean DoWeHaveQuickTime ( void ) ;
Boolean DoWeHaveDragManager ( void ) ;
//Boolean CanWeDisplay4Bit (GDHandle);
//Boolean CanWeDisplay1Bit (GDHandle);
short HowManyUsableScreens ( Boolean , Boolean , Boolean ) ;
void GetDeviceRect ( Rect * ) ;
Boolean AreWeColorOrGrayscale ( void ) ;
void SwitchDepthOrAbort ( void ) ;
macEnviron thisMac ;
extern short isDepthPref ;
extern Boolean dontLoadMusic , dontLoadSounds ;
//============================================================== Functions
//-------------------------------------------------------------- GetThisVolumeRefNum
// Get a hard reference number for the current drive volume this app is on.
/*
short GetThisVolumeRefNum ( void )
{
2019-12-29 06:38:18 -05:00
PLError_t theErr ;
2019-11-11 00:11:59 -05:00
short vRef ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
theErr = GetVol ( nil , & vRef ) ;
return ( vRef ) ;
}
*/
//-------------------------------------------------------------- GetThisCurrentDirectoryID
// Get a hard ID number for the current directory volume this app is in.
/*
long GetThisCurrentDirectoryID ( void )
{
long dirID ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
dirID = LMGetCurDirStore ( ) ;
return ( dirID ) ;
}
*/
//-------------------------------------------------------------- TrapExists
// Returns whether or not a ToolBox trap exists for the users ROMs/System.
/*
Boolean TrapExists ( short trapNumber )
{
return ( ( NGetTrapAddress ( trapNumber , ToolTrap ) ! =
NGetTrapAddress ( kUnimpTrap , ToolTrap ) ) ) ;
}
*/
//-------------------------------------------------------------- DoWeHaveGestalt
// Specifically tests for the availablity of the Gestalt() trap.
/*
Boolean DoWeHaveGestalt ( void )
{
return ( TrapExists ( kGestaltTrap ) ) ;
}
*/
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- DoWeHaveWNE
2019-11-11 00:11:59 -05:00
// Specifically tests for the availablity of the WaitNextEvent() trap.
/*
Boolean DoWeHaveWNE ( void )
{
return ( TrapExists ( kWNETrap ) ) ;
}
*/
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- DoWeHaveColor
2019-11-11 00:11:59 -05:00
// Determines if ROMs support Color QuickDraw (monitor not neccessarily color).
/*
Boolean DoWeHaveColor ( void )
{
SysEnvRec thisWorld ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
SysEnvirons ( 2 , & thisWorld ) ;
return ( thisWorld . hasColorQD ) ;
}
*/
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- DoWeHaveSystem602
2019-11-11 00:11:59 -05:00
// Determines if the System version is at least 6.0.2 or more recent.
/*
Boolean DoWeHaveSystem602 ( void )
{
SysEnvRec thisWorld ;
Boolean haveIt ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
SysEnvirons ( 2 , & thisWorld ) ;
if ( thisWorld . systemVersion > = 0x0602 )
haveIt = true ;
else
haveIt = false ;
return ( haveIt ) ;
}
*/
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- DoWeHaveSystem605
2019-11-11 00:11:59 -05:00
// Determines if the System version is at least 6.0.5 or more recent.
/*
Boolean DoWeHaveSystem605 ( void )
{
SysEnvRec thisWorld ;
Boolean haveIt ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
SysEnvirons ( 2 , & thisWorld ) ;
if ( thisWorld . systemVersion > = 0x0605 )
haveIt = true ;
else
haveIt = false ;
return ( haveIt ) ;
}
2020-10-10 02:42:06 -04:00
*/
//-------------------------------------------------------------- DoWeHaveSystem7
2019-11-11 00:11:59 -05:00
// Determines if the System version is at least 7.0.0 or more recent.
2020-10-10 02:42:06 -04:00
/*
2019-11-11 00:11:59 -05:00
Boolean DoWeHaveSystem7 ( void )
{
SysEnvRec thisWorld ;
Boolean haveIt ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
SysEnvirons ( 2 , & thisWorld ) ;
if ( thisWorld . systemVersion > = 0x0700 )
haveIt = true ;
else
haveIt = false ;
return ( haveIt ) ;
}
2020-10-10 02:42:06 -04:00
*/
2019-11-11 00:11:59 -05:00
//-------------------------------------------------------------- DoWeHaveSoundManager3
// Determines if the Sound Manager version is at least 3.0.0 or more recent.
/*
Boolean DoWeHaveSoundManager3 ( void )
{
// NumVersion version;
Boolean hasIt ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
hasIt = true ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
version = SndSoundManagerVersion ( ) ;
hasIt = ( version . majorRev > = 3 ) ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
return hasIt ;
}
*/
//-------------------------------------------------------------- DoWeHaveQuickTime
Boolean DoWeHaveQuickTime ( void )
{
2020-02-20 03:13:15 -05:00
return true ;
2019-11-11 00:11:59 -05:00
}
//-------------------------------------------------------------- DoWeHaveDragManager
Boolean DoWeHaveDragManager ( void )
{
return true ;
}
void SwitchToDepth ( short , Boolean )
{
}
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- CanWeDisplay4Bit
2019-11-11 00:11:59 -05:00
// Determines if device (monitor) capable of supporting 4 bit (16 colors/grays).
/*
Boolean CanWeDisplay4Bit ( GDHandle theDevice )
{
short canDepth ;
Boolean canDo ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
canDo = false ;
canDepth = HasDepth ( theDevice , 4 , 1 , 0 ) ;
if ( canDepth ! = 0 )
canDo = true ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
return ( canDo ) ;
}
*/
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- CanWeDisplay1Bit
2019-11-11 00:11:59 -05:00
// Determines if device (monitor) capable of supporting 1 bit (black & white).
/*
Boolean CanWeDisplay1Bit ( GDHandle theDevice )
{
short canDepth ;
Boolean canDo ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
canDo = false ;
canDepth = HasDepth ( theDevice , 1 , 1 , 0 ) ;
if ( canDepth ! = 0 )
canDo = true ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
return ( canDo ) ;
}
*/
//-------------------------------------------------------------- HowManyUsableScreens
// Counts the number of monitors that meet the depth criteria passed in.
short HowManyUsableScreens ( Boolean use1Bit , Boolean use4Bit , Boolean use8Bit )
{
return 1 ;
}
2020-04-01 14:53:44 -04:00
//-------------------------------------------------------------- FlushResolutionChange
void FlushResolutionChange ( void )
{
if ( thisMac . isResolutionDirty )
{
2020-04-04 02:20:03 -04:00
GetDeviceRect ( & thisMac . fullScreen ) ;
thisMac . constrainedScreen = thisMac . fullScreen ;
if ( thisMac . constrainedScreen . Width ( ) > kMaxViewWidth )
{
thisMac . constrainedScreen . left = 0 ;
thisMac . constrainedScreen . right = kMaxViewWidth ;
}
if ( thisMac . constrainedScreen . Height ( ) > kMaxViewHeight )
{
thisMac . constrainedScreen . top = 0 ;
thisMac . constrainedScreen . bottom = kMaxViewHeight ;
}
thisMac . gray = thisMac . fullScreen ;
2020-04-01 14:53:44 -04:00
thisMac . gray . top = 20 ;
thisMac . isResolutionDirty = false ;
}
}
2020-10-10 02:42:06 -04:00
//-------------------------------------------------------------- CheckOurEnvirons
// Calls all the above functions in order to fill out a sort of "spec sheet"<22>
2019-11-11 00:11:59 -05:00
// for the current Mac.
void CheckOurEnvirons ( void )
2020-10-10 02:42:06 -04:00
{
2019-11-11 00:11:59 -05:00
thisMac . vRefNum = 0 ; // TEMP
thisMac . dirID = 0 ; // TEMP
thisMac . hasGestalt = true ; // TEMP
thisMac . hasWNE = true ; // TEMP
thisMac . hasColor = true ; // TEMP
thisMac . canSwitch = true ; // TEMP
thisMac . hasSystem7 = true ; // TEMP
thisMac . hasSM3 = true ; // TEMP
thisMac . hasQT = DoWeHaveQuickTime ( ) ;
thisMac . hasDrag = DoWeHaveDragManager ( ) ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
thisMac . can1Bit = true ;
thisMac . can4Bit = true ;
thisMac . can8Bit = true ;
thisMac . numScreens = HowManyUsableScreens ( false , true , true ) ;
2020-04-01 14:53:44 -04:00
thisMac . isResolutionDirty = true ;
2020-11-25 12:05:59 -05:00
thisMac . isTouchscreen = PLDrivers : : GetSystemServices ( ) - > IsTouchscreen ( ) ;
thisMac . isMouseTouchscreen = PLDrivers : : GetSystemServices ( ) - > IsUsingMouseAsTouch ( ) ;
2020-10-11 19:50:03 -04:00
2020-04-01 14:53:44 -04:00
FlushResolutionChange ( ) ;
}
//-------------------------------------------------------------- HandleResolutionChange
// Installs handler
void HandleResolutionChange ( uint32_t prevWidth , uint32_t prevHeight , uint32_t newWidth , uint32_t newHeight )
{
PortabilityLayer : : WindowManager : : GetInstance ( ) - > HandleScreenResolutionChange ( prevWidth , prevHeight , newWidth , newHeight ) ;
PortabilityLayer : : MenuManager : : GetInstance ( ) - > DrawMenuBar ( ) ;
thisMac . isResolutionDirty = true ; // Because of legacy code, we don't want to update thisMac.screen immediately, but rather, let the editor or game pick it up
}
class GpAppResolutionChangeHandler final : public PortabilityLayer : : DisplayDeviceManager : : IResolutionChangeHandler
{
public :
void OnResolutionChanged ( uint32_t prevWidth , uint32_t prevHeight , uint32_t newWidth , uint32_t newHeight ) override
{
HandleResolutionChange ( prevWidth , prevHeight , newWidth , newHeight ) ;
}
2020-04-18 00:18:20 -04:00
void AdjustRequestedResolution ( uint32_t & physicalWidth , uint32_t & physicalHeight , uint32_t & virtualWidth , uint32_t & virtualHeight , float & pixelScaleX , float & pixelScaleY ) override
2020-04-04 02:20:03 -04:00
{
2020-06-20 01:33:26 -04:00
double minMul = 1.0 ;
2020-10-24 10:03:22 -04:00
if ( isAutoScale | | physicalWidth < 640 | | physicalHeight < 480 )
2020-06-20 01:33:26 -04:00
{
double xMul = static_cast < double > ( physicalWidth ) / 640.0 ;
double yMul = static_cast < double > ( physicalHeight ) / 480.0 ;
2020-04-18 00:18:20 -04:00
2020-10-20 23:43:02 -04:00
minMul = std : : min ( xMul , yMul ) ;
if ( minMul > = 1.0 )
{
double granularity = 2.0 ;
2020-04-25 00:51:44 -04:00
2020-10-20 23:43:02 -04:00
xMul = floor ( xMul * granularity ) / granularity ;
yMul = floor ( yMul * granularity ) / granularity ;
2020-04-18 00:18:20 -04:00
2020-10-20 23:43:02 -04:00
minMul = std : : min ( xMul , yMul ) ;
}
2020-06-20 01:33:26 -04:00
}
2020-04-18 00:18:20 -04:00
virtualWidth = physicalWidth / minMul ;
virtualHeight = physicalHeight / minMul ;
pixelScaleX = static_cast < float > ( minMul ) ;
pixelScaleY = static_cast < float > ( minMul ) ;
2020-04-04 02:20:03 -04:00
}
2020-04-01 14:53:44 -04:00
static GpAppResolutionChangeHandler ms_instance ;
} ;
GpAppResolutionChangeHandler GpAppResolutionChangeHandler : : ms_instance ;
//-------------------------------------------------------------- InstallResolutionHandler
// Installs handler
void InstallResolutionHandler ( void )
{
PortabilityLayer : : DisplayDeviceManager : : GetInstance ( ) - > SetResolutionChangeHandler ( & GpAppResolutionChangeHandler : : ms_instance ) ;
2019-11-11 00:11:59 -05:00
}
//-------------------------------------------------------------- ReflectMonitor2Environs
// Tests second monitor (if available) for specific bit depth capabilities.
/*
void ReflectSecondMonitorEnvirons ( Boolean use1Bit , Boolean use4Bit , Boolean use8Bit )
{
GDHandle tempGDevice ;
2020-10-10 02:42:06 -04:00
2019-11-11 00:11:59 -05:00
tempGDevice = GetDeviceList ( ) ;
while ( tempGDevice ! = nil )
{
if ( TestDeviceAttribute ( tempGDevice , screenDevice ) )
2020-10-10 02:42:06 -04:00
if ( ( use1Bit & & CanWeDisplay1Bit ( tempGDevice ) ) | |
( use4Bit & & CanWeDisplay4Bit ( tempGDevice ) ) | |
2019-11-11 00:11:59 -05:00
( use8Bit & & CanWeDisplay8Bit ( tempGDevice ) ) )
if ( ! TestDeviceAttribute ( tempGDevice , mainScreen ) )
{
thisGDevice = tempGDevice ;
thisMac . can1Bit = CanWeDisplay1Bit ( thisGDevice ) ;
thisMac . can4Bit = CanWeDisplay4Bit ( thisGDevice ) ;
thisMac . can8Bit = CanWeDisplay8Bit ( thisGDevice ) ;
thisMac . wasDepth = WhatsOurDepth ( ) ;
thisMac . wasColorOrGray = AreWeColorOrGrayscale ( ) ;
GetDeviceRect ( & thisMac . screen ) ;
break ;
}
tempGDevice = GetNextDevice ( tempGDevice ) ;
}
}
*/
//-------------------------------------------------------------- HandleDepthSwitching
// Handles setting up a monitor's depth to play on.
void HandleDepthSwitching ( void )
{
2020-06-13 04:33:41 -04:00
switch ( isDepthPref )
2019-11-11 00:11:59 -05:00
{
2020-06-13 04:33:41 -04:00
case 32 :
PortabilityLayer : : DisplayDeviceManager : : GetInstance ( ) - > SetPixelFormat ( GpPixelFormats : : kRGB32 ) ;
break ;
case 8 :
PortabilityLayer : : DisplayDeviceManager : : GetInstance ( ) - > SetPixelFormat ( GpPixelFormats : : k8BitStandard ) ;
break ;
default :
isDepthPref = 8 ;
PortabilityLayer : : DisplayDeviceManager : : GetInstance ( ) - > SetPixelFormat ( GpPixelFormats : : k8BitStandard ) ;
break ;
2019-11-11 00:11:59 -05:00
}
2020-06-13 04:33:41 -04:00
thisMac . isDepth = isDepthPref ;
2020-07-03 13:53:10 -04:00
2020-11-25 12:05:59 -05:00
PLDrivers : : GetDisplayDriver ( ) - > SetUseICCProfile ( isUseICCProfile ! = 0 ) ;
2019-11-11 00:11:59 -05:00
}
void GetDeviceRect ( Rect * rect )
{
unsigned int width ;
unsigned int height ;
2020-11-25 12:05:59 -05:00
PLDrivers : : GetDisplayDriver ( ) - > GetDisplayResolution ( & width , & height ) ;
2019-11-11 00:11:59 -05:00
SetRect ( rect , 0 , 0 , static_cast < short > ( width ) , static_cast < short > ( height ) ) ;
}
Boolean AreWeColorOrGrayscale ( )
{
// ... As opposed to B&W
return true ;
}
void SwitchDepthOrAbort ( void )
{
}