home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / screen / shdwmstr.lha / source / savers / utilitymain.h < prev   
C/C++ Source or Header  |  1991-12-29  |  4KB  |  150 lines

  1. /*
  2.  * To obey the protocol, we must use NP_ExitCode to set up code that clears
  3.  * the proc pointer, indicating that we ain't got a blanker any more.
  4.  * In addition, This code sends a CTRL-C signal to the utility, so that it
  5.  * will exit, so we need a pointer to our task. Finally, proc must be
  6.  * protected by a semaphore, so that's out here as well.
  7.  *
  8.  * Copyright 1991, Mike Meyer
  9.  * All Rights Reserved
  10.  *
  11.  * See the file "ShadowMaster:Distribution" for information on distribution.
  12.  */
  13.  
  14. static struct Process *proc = NULL ;
  15. static struct Task *me = NULL ;
  16. static struct SignalSemaphore guard ;
  17. #ifdef    LOOPS
  18. #include <graphics/displayinfo.h>
  19. #include <proto/intuition.h>
  20. struct IntuitionBase *IntuitionBase ;
  21. #endif
  22.  
  23. #define FIRST    1
  24. #define REST    2
  25. #define CLEANUP    3
  26.  
  27. /*
  28.  * This is the starting point. It sets up the world, and calls your
  29.  * utility routine (doutility()), then cleans up after itself when you
  30.  * return. Nothing in here should need changing.
  31.  *
  32.  * This gets included into the utility file at the appropriate place.
  33.  */
  34. #define done(x)    do { status = x; goto out; } while (0) ;
  35.  
  36. int __saveds
  37. start(void) {
  38.     int status = RETURN_OK ;
  39. #ifdef    LOOPS
  40.     struct Screen    *screen = NULL ;
  41.     static struct ColorSpec colors[] = {{0, 0, 0, 0}, {1, 0, 0, 0}, {-1} } ;
  42.     static struct TagItem screen_tags[] = {
  43.         {SA_Depth, 1},
  44.         {SA_DisplayID, LORES_KEY},
  45.         {SA_Quiet, TRUE},
  46.         {SA_Colors, &colors},
  47.         {TAG_END, 0}
  48.         } ;
  49. #endif
  50. #ifdef    TEMPLATE
  51.     struct RDArgs *args = NULL ;
  52. #endif
  53.     int tripno = FIRST ;
  54.  
  55.     proctags[0].ti_Data = 0 ;
  56.     SysBase = *((struct ExecBase **) 4);
  57.     if ((DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37)) == NULL)
  58.         done(RETURN_FAIL) ;
  59. #ifdef    LOOPS
  60.     if ((IntuitionBase = (struct IntuitionBase *)
  61.         OpenLibrary("intuition.library", 37)) == NULL)
  62.         done(RETURN_FAIL) ;
  63. #endif
  64.     InitSemaphore(&guard) ;
  65.     me = FindTask(0L) ;
  66.  
  67.     /* Do the magic that you do so well */
  68. #ifdef    TEMPLATE
  69.     if ((args = ReadArgs(TEMPLATE, opts, NULL)) == NULL)
  70.         done(RETURN_ERROR) ;
  71. #endif
  72.  
  73.     /* Let's get the show on the road... */
  74. #ifdef    LOOPS
  75.     for (;;) {
  76. #endif
  77.     proctags[1].ti_Data = (long) doutility(tripno) ;
  78.  
  79. #ifdef    LOOPS
  80.     if (tripno != FIRST && proctags[1].ti_Data)
  81.         screen = OpenScreenTagList(NULL, screen_tags) ;
  82.     ObtainSemaphore(&guard) ;
  83.     while (proc) {
  84.         Signal((struct Task *) proc, SIGBREAKF_CTRL_C) ;
  85.         ReleaseSemaphore(&guard) ;
  86.         Wait(SIGBREAKF_CTRL_C) ;    /* From procend */
  87.         ObtainSemaphore(&guard) ;
  88.         }
  89.     ReleaseSemaphore(&guard) ;
  90. #endif
  91.  
  92.     if (proctags[1].ti_Data == NULL
  93.     || ((proctags[0].ti_Data = (long) LoadSeg((char *) proctags[1].ti_Data)) == NULL)
  94.     /* At this point, proc isn't running, so we don't need a guard... */
  95.     || ((proc = CreateNewProc(proctags)) == NULL)) {
  96. #ifdef    LOOPS
  97.         doutility(CLEANUP) ;        /* So they can clean up */
  98. #endif
  99.         done(RETURN_FAIL) ;
  100.         }
  101.  
  102.     proctags[0].ti_Data = NULL ;    /* The process will unload itself */
  103.  
  104. #ifdef    LOOPS
  105.     if (screen) {
  106.         Delay(1) ;        /* Force a task switch */
  107.         CloseScreen(screen) ;
  108.         }
  109.     screen = NULL ;
  110.     tripno = REST ;
  111.     }
  112. #else
  113.     Wait(SIGBREAKF_CTRL_C) ;    /* Someone says die */
  114.  
  115.     ObtainSemaphore(&guard) ;
  116.     while (proc) {
  117.         Signal((struct Task *) proc, SIGBREAKF_CTRL_C) ;
  118.         ReleaseSemaphore(&guard) ;
  119.         Wait(SIGBREAKF_CTRL_C) ;    /* From procend */
  120.         ObtainSemaphore(&guard) ;
  121.         }
  122.     ReleaseSemaphore(&guard) ;
  123. #endif
  124.  
  125.     /* Here's how we exit this mess */
  126. out:
  127.     if (proctags[0].ti_Data) UnLoadSeg(proctags[1].ti_Data) ;
  128. #ifdef    LOOPS
  129.     if (screen) CloseScreen(screen) ;
  130. #endif
  131. #ifdef    TEMPLATE
  132.     if (args) FreeArgs(args) ;
  133. #endif
  134.     if (DOSBase) CloseLibrary((struct Library *) DOSBase) ;
  135.     return status ;
  136.     }
  137.  
  138. /* Make sure we don't screw up any following code... */
  139. #undef done
  140.  
  141. /* The routine that ends all procs */
  142. static void __saveds
  143. procend(void) {
  144.  
  145.     ObtainSemaphore(&guard) ;
  146.     proc = NULL ;
  147.     Signal(me, SIGBREAKF_CTRL_C) ;
  148.     ReleaseSemaphore(&guard) ;
  149.     }
  150.