home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Util / Quit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  3.4 KB  |  170 lines

  1. /*****
  2.  *
  3.  *    Quit.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompilingForWSAPI
  18. #include <WSAPI.h>
  19. #endif
  20.  
  21. #include "globals.h"
  22.  
  23. #include "CustomHandlers.h"
  24. #include "EventUtil.h"
  25. #include "LogUtil.h"
  26. #include "MemoryUtil.h"
  27. #include "ProcessUtil.h"
  28. #include "WindowInt.h"
  29.  
  30. #include "Quit.h"
  31.  
  32.  
  33. /***  INTERFACE  ***/
  34.  
  35. /* Handle an interface or scripting call to quit the applciation */
  36. void
  37. doQuitApp ( void )
  38. {
  39.     /* set application to quit */
  40.     gQuit = true;
  41. } /* doQuitApp */
  42.  
  43.  
  44. /***  FUNCTIONS  ***/
  45. #pragma mark -
  46.  
  47. #if kCompilingForWSAPI
  48. long
  49. QuitWSAPI ( WSAPI_CommandPBPtr commandPtr )
  50. {
  51.     #pragma unused(commandPtr)
  52.     
  53.     Boolean    success;
  54.     
  55.     success = CustomQuit ( false );
  56.     
  57.     return noErr;
  58. } /* QuitWSAPI */
  59. #endif
  60.  
  61. /* Confirm that the application can quit, prompt the user if necessary.
  62.     Clean up, if the application can quit.
  63.     Return true if application is set to quit, false if it can't quit. */
  64. Boolean
  65. QuitPrepare ( Boolean allowUserInteract )
  66. {
  67.     Boolean            memoryLow;
  68.     Boolean            gotEvent;
  69.     EventRecord        theEvent;
  70.     Boolean            success;
  71.     
  72.     gQuit = true;
  73.     
  74.     /* check for emergency memory store. If it isn't available, memory is low */
  75.     memoryLow = !IsEmergencyMemAvail ();
  76.     
  77.     /* Should add check for outstanding events and handle them as appropriate.
  78.         Especially important is to check for any queued high level events. */
  79.     if ( !memoryLow )
  80.     {
  81.         /* check for outstanding events and handle them as appropriate */
  82.         do
  83.         {
  84.             gotEvent = WaitNextEvent ( highLevelEventMask, &theEvent, 0, NULL );
  85.             if ( gotEvent )
  86.             {
  87.                 /* • Need to figure out a good way to distinguish between idle time
  88.                     quits and others. So, if quitting on idle and an event is
  89.                     found gQuit will be reset to false. Otherwise, if quitting
  90.                     normally and an event is found gQuit will be left alone. */
  91.                 switch ( theEvent.what )
  92.                 {
  93.                     case kHighLevelEvent :
  94.                         doHighLevelEvent ( &theEvent );
  95.                         break;
  96.                 }
  97.             }
  98.         } while ( gotEvent == true );
  99.     }
  100.         
  101.     #if kCompileWithForeground
  102.     /* get rid of document windows */
  103.     success = WindowCloseAll ( (!memoryLow) && allowUserInteract );
  104.     
  105.     if ( success || !allowUserInteract || memoryLow )
  106.     #endif    /* kCompileWithForeground */
  107.     {
  108.         #if kStartupThreadsPreallocate > nil
  109.         #if kCompileWithThreadsOptional
  110.         if ( gHasThreadMgr )
  111.         {
  112.         #endif
  113.         
  114.             /* while there remain other threads, let them finish before quitting */
  115.             ThreadFinishAllSubThreads ();
  116.         
  117.         #if kCompileWithThreadsOptional
  118.         }
  119.         #endif
  120.         #endif
  121.         
  122.         success = CustomQuit ( allowUserInteract );
  123.         
  124.         /* close up the log file */
  125.         LogQuit ();
  126.         
  127.         /* can quit if successful or user is not allowed to interact */
  128.         gQuit = ( success || !allowUserInteract || memoryLow );
  129.     }
  130.     #if kCompileWithForeground
  131.     else
  132.     {
  133.         gQuit = false;
  134.     }
  135.     #endif
  136.     
  137.     return gQuit;
  138. } /* QuitPrepare */
  139.  
  140.  
  141. /* force the application to quit, doing necessary cleanup. */
  142. p_export
  143. void
  144. ForceQuit ( void )
  145. {
  146.     /* quit with no user interaction */
  147.     QuitPrepare ( false );
  148.     
  149.     ExitToShell ();
  150. } /* ForceQuit */
  151.  
  152.  
  153. /**  Utilities  **/
  154. #pragma mark -
  155.  
  156. #if kCompileWithQuitOnLongIdle
  157. p_export
  158. void
  159. ResetQuitIdleTimer ( void )
  160. {
  161.     if ( gDoIdleQuit )
  162.     {
  163.         gTimeLastAction = TickCount ();
  164.     }
  165. } /* ResetQuitIdleTimer */
  166. #endif
  167.  
  168.  
  169. /*****  EOF  *****/
  170.