home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Demos / Tools / QC™ 1.1.3 / QCAPI / BadAPPL / BadAPPL src / BadGestalt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-21  |  5.3 KB  |  215 lines  |  [TEXT/R*ch]

  1. /*________________________________________________________________________________
  2.  
  3.     BadGestalt.c
  4.  
  5.     Copyright © 1993-1995 Onyx Technology - All rights reserved
  6.  
  7.     The routines in this file gather information about the environment
  8.     we're running in.  Whenever possible they use Gestalt as the first
  9.     choice and SysEnvirons as a last resort.
  10.  
  11.     IMPORTANT NOTE: Since development environments like MPW and THINK provide
  12.     glue code for Gestalt, and since it's been around for so long now, we don't
  13.     even bother checking for it anymore and just use it instead of reverting
  14.     back to _SysEnvirons calls.  Since QC itself won't run on systems that are
  15.     so old they don't have _Gestalt, it's pretty safe to say we wouldn't be
  16.     running this app anyway.  If that turns out to be the case, then #define
  17.     _CHK_FOR_GESTALT_ to use code that will check for _Gestalt and use
  18.     SysEnvirons if necessary.
  19.  
  20. ________________________________________________________________________________*/
  21.  
  22. #ifndef    _H_BadGestalt
  23. #include "BadGestalt.h"
  24. #endif
  25. #ifndef    _H_BadGlobs
  26. #include "BadGlobs.h"
  27. #endif
  28.  
  29. //#define _CHK_FOR_GESTALT_            // define this for code that checks for _Gestalt
  30.                                     // by default we let glue code figure it out for us
  31.  
  32. /*________________________________________________________________________________
  33.  
  34.     GetSystemVers()
  35.  
  36.     info:    Get the system version, try Gestalt first, then SysEnvirons
  37.  
  38.     return:    long sysVersion; -1 if failed
  39.  
  40. ________________________________________________________________________________*/
  41. long    GetSystemVers(void)
  42. {
  43.     short        myErr;
  44.     long        sysVersion;
  45. #ifdef    _CHK_FOR_GESTALT_
  46.     SysEnvRec    theWorld;
  47. #endif
  48.  
  49.     if ( HasGestalt() )
  50.         {
  51.         myErr = Gestalt(gestaltSystemVersion, &sysVersion);
  52.         if (myErr)
  53.             sysVersion    = -1;
  54.         }
  55. #ifdef    _CHK_FOR_GESTALT_
  56.     else
  57.         {
  58.         if (!SysEnvirons(2, &theWorld))
  59.             sysVersion    =    theWorld.systemVersion;
  60.         else
  61.             sysVersion    = -1;
  62.         }
  63. #endif    //_CHK_FOR_GESTALT_
  64.  
  65.     return(sysVersion);
  66. }
  67.  
  68. /*________________________________________________________________________________
  69.  
  70.     HasGestalt()
  71.  
  72.     info:    Determine if Gestalt is available or not.
  73.  
  74.     return:    true if available, false if not.
  75.  
  76. ________________________________________________________________________________*/
  77. short    HasGestalt(void)
  78. {
  79. #ifdef    _CHK_FOR_GESTALT_
  80. long    int        GestaltAddr = -1;
  81.         short    hasGestalt    =    false;
  82.  
  83.     GestaltAddr    = TrapAvailable(_GestaltDispatch);
  84.  
  85.     if (GestaltAddr>0)                                    // we have it, yeah!
  86.         hasGestalt = true;                                // set this flag
  87.  
  88.     return(hasGestalt);
  89. #else
  90.     return(true);
  91. #endif    //_CHK_FOR_GESTALT_
  92. }
  93.  
  94.  
  95. /*________________________________________________________________________________
  96.  
  97.     GetQDVers()
  98.  
  99.     info:    Get the QuickDraw version, try Gestalt first, then SysEnvirons
  100.  
  101.     return:    long sysVersion; -1 if failed
  102.  
  103. ________________________________________________________________________________*/
  104. short    GetQDVers(void)
  105. {
  106.     short        myErr;
  107.     long        QDvers;
  108. #ifdef    _CHK_FOR_GESTALT_
  109.     SysEnvRec    theWorld;
  110. #endif
  111.  
  112.     if (HasGestalt())
  113.         {
  114.         myErr = Gestalt(gestaltQuickdrawVersion, &QDvers);
  115.         if (myErr)
  116.             QDvers = gestaltOriginalQD;             // default to original
  117.         }
  118. #ifdef    _CHK_FOR_GESTALT_
  119.     else
  120.         {
  121.         if (!SysEnvirons(2, &theWorld))
  122.             QDvers    =    theWorld.hasColorQD;
  123.         else
  124.             QDvers    =    gestaltOriginalQD;            // default back to original
  125.         }
  126. #endif    //_CHK_FOR_GESTALT_
  127.  
  128.     return(QDvers);
  129. }
  130.  
  131. /*________________________________________________________________________________
  132.  
  133.     ColorQDIsPresent
  134.  
  135.     info:    Check whether Color QuickDraw is present.
  136.  
  137.     return:    true if Color QD is available, false if not.  Call GetQDVers if you
  138.             want the actual version of Color QuickDraw in use.
  139. _____________________________________________________________________ */
  140.  
  141. Boolean    ColorQDIsPresent(void)
  142. {
  143.     Boolean        hasColorQD    =    false;
  144.     
  145.     if (GetQDVers() > gestaltOriginalQD)        // if greater than 1, yes
  146.         hasColorQD    =    true;
  147.  
  148.     return (hasColorQD);
  149. }
  150.  
  151. /*________________________________________________________________________________
  152.  
  153.     TrapAvailable()
  154.  
  155.     info:    Determine if a particular trap is available.
  156.  
  157.     entry:    theTrap    -    the trap number to test for.
  158.  
  159.     return:    true if the trap is available, false if not.
  160. ________________________________________________________________________________*/
  161. short    TrapAvailable(short theTrap)
  162. {
  163.     short        theType;
  164.     short        trimTest;
  165.  
  166.     theType        =    GetTrapType(theTrap);
  167.  
  168.     if(theType    ==    ToolBoxTrap)
  169.         {
  170.         trimTest =    BitAnd(theTrap,0x07FF);
  171.         if(trimTest > NumToolboxTraps() )
  172.             return(false);
  173.         }
  174.  
  175.     return( (NGetTrapAddress(theTrap,theType) != NGetTrapAddress(_Unimplemented,ToolTrap)) );
  176. }
  177.  
  178. /*________________________________________________________________________________
  179.  
  180.     NumToolboxTraps()
  181.  
  182.     info:    Determine the number of toolbox traps available.
  183.  
  184.     return:    short number of traps available.
  185.  
  186. ________________________________________________________________________________*/
  187. short    NumToolboxTraps(void)
  188. {
  189.     if(NGetTrapAddress(InitGrafTrap,ToolTrap)==NGetTrapAddress(0xaa6e,ToolTrap))
  190.         return(0x200);
  191.     else
  192.         return(0x400);
  193. }
  194.  
  195. /*________________________________________________________________________________
  196.     GetTrapType()
  197.  
  198.     info:    Given a trap address, determine if trap is OS or ToolBox trap.
  199.     
  200.     entry:    theTrap    -    the trap number
  201.  
  202.     return:    short    1 = OSTrap; 2 = ToolBoxTrap
  203. ________________________________________________________________________________*/
  204. short    GetTrapType(short theTrap)
  205. {
  206.     short    trapType    =    0;
  207.  
  208.     if( BitAnd(theTrap,TrapMask) > 0)
  209.         trapType        =    ToolBoxTrap;
  210.     else
  211.         trapType        =    OSTrap;
  212.     
  213.     return(trapType);
  214. }
  215.