home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 354.lha / SmartIcon_v2.0 / src / main.c < prev    next >
C/C++ Source or Header  |  1990-02-27  |  7KB  |  246 lines

  1. #include <exec/types.h>
  2. #include <intuition/intuisup.h>
  3. #include <libraries/dos.h>
  4. #include "patch.h"
  5.  
  6. #ifndef DEBUG
  7. #include <proto/all.h>
  8. #endif
  9.  
  10. #define PROGRAM_NAME "SmartIcon 2.0"
  11. #define RCFILE       "s:.sirc"
  12.  
  13. struct   Window  *theEventWindow;
  14.  
  15. BOOL   Sleeping = FALSE;
  16. USHORT WindowX = 40, WindowY = 20;
  17. ULONG  PatchSignal = -1;
  18. struct WindowPatch   PList;
  19. struct HiddenWindow  HList;
  20. struct ListSupport   *DList;
  21. struct Library       *GfxBase, *IntuitionBase, *LayersBase;
  22. struct MsgPort       *DefaultPort, *WakeupPort, *IDCMPReplyPort;
  23. struct Task          *PatchTask;
  24.  
  25.  
  26. VOID
  27. ReadRCFile()
  28. {
  29.    USHORT x, y;
  30.    register struct FileHandle *file = (struct FileHandle *)Open(RCFILE, MODE_OLDFILE);
  31.  
  32.    if (file)
  33.       {
  34.       if (Read(file, &x, 2)==2 && Read(file, &y, 2)==2)
  35.          {
  36.          WindowX = x;
  37.          WindowY = y;
  38.          }
  39.       else DisplayBeep(NULL);
  40.       Close(file);
  41.       }
  42. }
  43.  
  44. VOID
  45. WriteRCFile()
  46. {
  47.    register struct FileHandle *file = (struct FileHandle *)Open(RCFILE, MODE_NEWFILE);
  48.  
  49.    if (file)
  50.       {
  51.       Write(file, &DList->LRWindow->LeftEdge, 2);
  52.       Write(file, &DList->LRWindow->TopEdge,  2);
  53.       Close(file);
  54.       }
  55.    else DisplayBeep(NULL);
  56. }
  57.  
  58. VOID
  59. Cleanup(code)
  60. register UBYTE code;
  61. {
  62.    register struct IntuiMessage *msg;
  63.  
  64.  
  65.    Forbid();
  66.    while (PList.NextPatch) UnPatchWindow(PList.NextPatch, TRUE);
  67.    Permit();
  68.  
  69.    if (DList) ReleaseList(DList);
  70.  
  71.    if (DefaultPort)
  72.       {
  73.       while(msg = (struct IntuiMessage *)GetMsg(DefaultPort)) ReplyMsg(msg);
  74.       DeletePort(DefaultPort);
  75.       }
  76.    if (WakeupPort)     DeletePort(WakeupPort);
  77.    if (IDCMPReplyPort) DeletePort(IDCMPReplyPort);
  78.  
  79.    if (PatchSignal!=-1)  FreeSignal(PatchSignal);
  80.  
  81.    if (GfxBase)         CloseLibrary(GfxBase);
  82.    if (IntuitionBase)   CloseLibrary(IntuitionBase);
  83.    if (LayersBase)      CloseLibrary(LayersBase);
  84.  
  85.    exit(code);
  86. }
  87.  
  88. VOID
  89. ControlLoop()
  90. {
  91.    BOOL     mousemoved;
  92.    SHORT    x, y, code, itemnum;
  93.    LONG     seconds, micros;
  94.    ULONG    class, mask,
  95.             PatchEvent   = 1 << PatchSignal,
  96.             DefPortEvent = 1 << DefaultPort->mp_SigBit,
  97.             WakeupEvent  = 1 << WakeupPort->mp_SigBit,
  98.             ReqEvent     = 1 << DList->LRWindow->UserPort->mp_SigBit;
  99.    register struct Window        *window;
  100.    register struct IntuiMessage  *msg;
  101.    register struct Gadget        *gadget;
  102.  
  103.    FOREVER
  104.    {
  105.    WAIT:
  106.    mask = Wait(SIGBREAKF_CTRL_C | DefPortEvent | PatchEvent | ReqEvent | WakeupEvent);
  107.  
  108.    if (mask & SIGBREAKF_CTRL_C) return;
  109.  
  110.    if (mask & DefPortEvent) while(msg=(struct IntuiMessage *)GetMsg(DefaultPort)) ReplyMsg(msg);
  111.  
  112.    if (mask & WakeupEvent)
  113.       {
  114. #ifdef DEBUG
  115. printf("wake up event received\n");
  116. #endif
  117.       /* A wake up signal here means that another SmartIcon task was launched */
  118.       /* and that it is telling us. Time to wake up if we were asleep.        */
  119.       if (Sleeping)
  120.          {
  121.          FixDisplay(FALSE);
  122.          if (!ls_StartRequester(DList, NULL, WindowX, WindowY)) DisplayBeep(NULL);
  123.          else Sleeping = FALSE;
  124.          }
  125.       }
  126.  
  127.    if (mask & PatchEvent)
  128.       {
  129.       Forbid();
  130.       window = theEventWindow;
  131.       Permit();
  132.  
  133. #ifdef DEBUG
  134. printf("patch event received: window [%s]\n", window->Title);
  135. #endif
  136.  
  137.       Iconify(window, !Sleeping);
  138.       }
  139.  
  140.    if (mask & ReqEvent)
  141.       {
  142.       mousemoved = FALSE;
  143.       while (msg = (struct IntuiMessage *)GetMsg(DList->LRWindow->UserPort))
  144.             {
  145.             gadget  = (struct Gadget *)msg->IAddress;
  146.             class   = msg->Class;
  147.             code    = msg->Code;
  148.             x       = msg->MouseX;
  149.             y       = msg->MouseY;
  150.             seconds = msg->Seconds;
  151.             micros  = msg->Micros;
  152.             ReplyMsg(msg);
  153.  
  154.             switch(class)
  155.                   {
  156.                   case MOUSEMOVE: mousemoved = TRUE; break;
  157.  
  158.                   case GADGETDOWN:
  159.                   case GADGETUP:
  160.                      if (ls_HandleGadget(DList, gadget, x, y, seconds, micros))
  161.                         UnIconify(DList->Pick, TRUE);
  162.                   break;
  163.  
  164.                   case CLOSEWINDOW:
  165.                      /* go to sleep... */
  166.                      Sleeping = TRUE;
  167.                      WindowX = DList->LRWindow->LeftEdge;
  168.                      WindowY = DList->LRWindow->TopEdge;
  169.                      ls_EndRequester(DList);
  170.                      goto WAIT;
  171.  
  172.                   case MENUPICK:
  173.                      while (code != MENUNULL && code != 252)
  174.                            {
  175.                            itemnum = ITEMNUM(code);
  176.  
  177.                            switch(itemnum)
  178.                                  {
  179.                                  case 3: WriteRCFile(); break;
  180.                                  case 5: return;
  181.                                  }
  182.                            code = ((struct MenuItem *)ItemAddress(DList->LRWindow->MenuStrip, code))->NextSelect;
  183.                            }
  184.                   break;
  185.                   }
  186.             }
  187.       if (mousemoved) ls_PropMouseMoves(DList);
  188.       }
  189.    }
  190. }
  191.  
  192. VOID
  193. main(argc, argv)
  194. UBYTE argc, **argv;
  195. {
  196.    extern struct Library *OpenLibrary();
  197.    extern struct MsgPort *CreatePort();
  198.    struct MsgPort *port;
  199.  
  200.    /* First check if we already have a port somewhere. If yes, then there is  */
  201.    /* already a task running, just signal that we saw it and exit.            */
  202.    if (port = (struct MsgPort *)FindPort("SM2.0"))
  203.       {
  204.       Signal(port->mp_SigTask, 1 << port->mp_SigBit);
  205.       exit(0);
  206.       }
  207.  
  208.    /* Still here? Just do our normal stuff.                                   */
  209.    if (  (GfxBase        = OpenLibrary("graphics.library", 0))
  210.       && (IntuitionBase  = OpenLibrary("intuition.library", 0))
  211.       && (LayersBase     = OpenLibrary("layers.library", 0))
  212.       && (WakeupPort     = CreatePort("SM2.0", 0))
  213.       && (DefaultPort    = CreatePort(0, 0))
  214.       && (IDCMPReplyPort = CreatePort(0, 0))
  215.       && ((PatchSignal   = AllocSignal(-1))!=-1)
  216.       && (DList          = GetListSupport(NULL, NULL, NULL))
  217.       )
  218.       {
  219.       ReadRCFile();
  220.  
  221.       PatchTask = (struct Task *)FindTask(0);
  222.       SetHooks();
  223.       PatchCurrentWindows();
  224.  
  225.       DList->ReqTitle = "Windows  ";
  226.       DList->MaxChars = 14;
  227.       DList->MaxLines = 5;
  228.  
  229.       if (ls_AllocateData(DList) && ls_StartRequester(DList, NULL, WindowX, WindowY))
  230.          {
  231.          ControlLoop();
  232.  
  233.          /* Put this here so we get display feedback */
  234.          while (HList.Next) UnIconify(HList.Next, !Sleeping);
  235.  
  236.          if (!Sleeping) ls_EndRequester(DList);
  237.          ls_DeleteData(DList);
  238.          }
  239.  
  240.       ClearHooks();
  241.       Cleanup(0);
  242.       }
  243.    Cleanup(10);
  244. }
  245.  
  246.