home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Tetris Light 1.0.3 / source / env.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  5.4 KB  |  171 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2. File: env.c
  3.  
  4. Purpose:    This module provides routines to determine the facilities
  5.             available on the running machine. These routines provide
  6.             glue to work on all Macintoshes (including the original
  7.             128K Mac). The implementation does not rely on any 
  8.             compiler supplied glues.
  9.             
  10. Tetris Light - a simple implementation of a Tetris game
  11. Copyright © 1993-1996 Hoylen Sue
  12.  
  13. Updated for CW9 by Paul Celestin
  14. Questions about this version should go to me at celestin@celestin.com
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program; see the file COPYING.  If not, write to the
  28. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  29. ---------------------------------------------------------------------- */
  30.  
  31. #include "local.h"
  32.  
  33. #include <GestaltEqu.h>
  34. #include <Traps.h>
  35.  
  36. #include "env.h"
  37.  
  38. /*--------------------------------------------------------------------*/
  39.  
  40. /* Local globals - initialized to worst case assumption, but will be
  41.    set up correctly to reflect the machine when `env_init' is called. */
  42.  
  43. static Boolean env_64k_rom = TRUE;
  44. static Boolean env_gestalt_available = FALSE;
  45.  
  46. /*--------------------------------------------------------------------*/
  47.  
  48. static void env_init(void)
  49. /* Set up local globals which indicate whether the machine has 64K ROMs
  50.    and if gestalt is available.  The routines which need this information
  51.    must call this routine before accessing that information.  Calculation
  52.    of these variables is done only once, but this approach simplifies the
  53.    interface to this module, because it requries no explicit
  54.    initialization.  Failure to call this module may result in those
  55.    variables containing default values, which will indicate a very
  56.    limited machine. */
  57. {
  58.     static Boolean initialized = FALSE;
  59.     OSErr myErr;
  60.     INTEGER        rom_version = 0;
  61.     SysEnvRec    the_environ;
  62.     
  63.     if (initialized)
  64.         return;
  65.     initialized = TRUE;
  66.     
  67.     /* Check for 64K ROMS. Use Environs since it is available in ALL
  68.        Macintoshes, although it has been replaced by Gestalt. Note that
  69.        this code does not rely on any compiler glues. */
  70.     
  71.     SysEnvirons(rom_version, &the_environ);
  72.     if (rom_version >= 0x75)
  73.         env_64k_rom = FALSE;
  74.     
  75.     /* Check for Gestalt facility. Gestalt is never available on
  76.        original 64K ROM machines! */
  77.     
  78.     if (env_64k_rom)
  79.         env_gestalt_available = FALSE;
  80.     else
  81.         env_gestalt_available = env_trap_available(0xA1AD);
  82. }
  83.  
  84. /*--------------------------------------------------------------------*/
  85.  
  86. static INTEGER num_toolbox_traps(void)
  87. /* Returns the size of the toolbox trap dispatch table.  Code from
  88.    Inside Macintosh VI p. 3-8.  InitGraf (0xA86E) is always implemented.
  89.    If the trap dispatch table has more than 0x200 entries, then 0xAA6E
  90.    always points to either unimplemented or something else, but never
  91.    to InitGraf. The 64K roms do not have NGetTrapAddress, but always
  92.    have small dispatch tables.
  93.    Internal routines which call this one must have ensured that the
  94.    `env_64k_rom' variable has been set up correctly. */
  95. {
  96.     if (env_64k_rom ||
  97.         (NGetTrapAddress(_InitGraf, ToolTrap) ==
  98.          NGetTrapAddress(0xAA6E, ToolTrap)))
  99.         return 0x0200;
  100.     else
  101.         return 0x0400;
  102. }
  103.  
  104. Boolean env_trap_available(INTEGER the_trap)
  105. /* Returns TRUE if the given trap is available. Based on code from
  106.    Inside Macintosh VI.  This routine does not rely on any glue, and
  107.    will work on all Macintoshes from the original 128K mac upwards.
  108.    Short circuit calls to this routine by checking for 64K roms first. */
  109. {
  110.     register TrapType tType;
  111.  
  112.     env_init();
  113.     
  114.     if (the_trap & 0x0800) {
  115.         tType = ToolTrap;
  116.         the_trap &= 0x07FF;
  117.         if (the_trap >= num_toolbox_traps())
  118.             the_trap = _Unimplemented;
  119.     }
  120.     else
  121.         tType = OSTrap;
  122.     
  123.     if (env_64k_rom)
  124.         return (GetTrapAddress(the_trap) !=
  125.                 GetTrapAddress(_Unimplemented));
  126.     else
  127.         return (NGetTrapAddress(the_trap, tType) != 
  128.                 NGetTrapAddress(_Unimplemented, ToolTrap));
  129. }
  130.  
  131. /*--------------------------------------------------------------------*/
  132.  
  133. Boolean env_WaitNextEvent_available(void)
  134. /* Determines if `WaitNextEvent' trap is available.  Returns TRUE if it
  135.    is, FALSE if it is not available. */
  136. {
  137.     return env_trap_available(_WaitNextEvent);
  138. }
  139.  
  140. /*--------------------------------------------------------------------*/
  141.  
  142. Boolean env_FindFolder_available(void)
  143. /* Determines if the `Gestalt' trap is available.  Returns TRUE if it
  144.    is, FALSE if it is not available. */
  145. {    
  146.     env_init();
  147.  
  148.     if (env_gestalt_available) {
  149.         LONGINT result;
  150.         
  151.         Gestalt(gestaltFindFolderAttr, &result);
  152.         if (result & (0x0001 << gestaltFindFolderPresent))
  153.             return TRUE;
  154.     }
  155.  
  156.     return FALSE;
  157. }
  158.  
  159. /*--------------------------------------------------------------------*/
  160.  
  161. Boolean env_SndPlay_available(void)
  162. /* Determines if 'SndPlay' is available.  Returns TRUE if it is, FALSE
  163.    if it is not. */
  164. {
  165.     env_init();
  166.     
  167.     return env_trap_available(_SndPlay);
  168. }
  169.  
  170. /*--------------------------------------------------------------------*/
  171.