home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 083.lha / twm / twmClient.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  3KB  |  123 lines

  1. /* twmClient.c
  2.  
  3.    This module should be compiled and linked with applications that wish to
  4.    be clients of twm when it is present in the system. Briefly, the client
  5.    calls the function twmInit() to set up, afterwards calls PostMe() whenever
  6.    he wishes to go to sleep, then finally calls twmCleanUP() just before
  7.    exiting. Full details are in the prefatory comments to twm.c.
  8.    
  9.    Update history:
  10.       Nov 12/87:  Forbid()/Permit() added to PostMe() and UnPostMe()
  11. */
  12.  
  13. #include "header/twm.h"
  14.  
  15. #define TWM_MSGSIZE ((long)sizeof(struct twmMessage))
  16.  
  17. extern VOID *CreatePort(), *FindPort();
  18. extern VOID *GetMsg(),     *AllocMem();
  19.  
  20. struct MsgPort      *mp       = NULL;  /* reply port for our msgs    */
  21. struct MsgPort      *twmport  = NULL;  /* points to twm's port       */
  22. struct twmMessage   *Addmsg   = NULL;  /* TWM_ACTION_ADD message     */
  23. struct twmMessage   *Delmsg   = NULL;  /* TWM_ACTION_DELETE message  */
  24.  
  25. int twmReady = FALSE;   /* TRUE means ports are allocated & initialized */
  26.  
  27.  
  28. PostMe (clientName)
  29. register char *clientName;
  30. {
  31.    /* trying not to pass junk to twm... */
  32.    if (clientName == NULL || *clientName == '\0')
  33.       return FALSE;
  34.    
  35.    /* set up our message telling twm to add its gadget   */
  36.    Addmsg->tmName   = clientName;
  37.    Addmsg->tmAction = TWM_ACTION_ADD;
  38.    
  39.    /* check we're initialized and that twm exists in systemh and send message
  40.    */
  41.    
  42.    Forbid();                  /* to be on the safe side */
  43.    
  44.    if (!twmReady || (twmport = FindPort(PORTNAME)) == NULL)
  45.       return FALSE;
  46.    
  47.    PutMsg(twmport, Addmsg);
  48.    
  49.    Permit();
  50.    
  51.    WaitPort(mp);
  52.    
  53.    Addmsg = GetMsg(mp);
  54.    
  55.    /* anything other than E_OK return code is bad news... forget about twm */
  56.    return (Addmsg->tmAction == E_OK);
  57. }   
  58.    
  59.  
  60. UnPostMe ()
  61. {
  62.    Delmsg->tmAction = TWM_ACTION_DELETE;
  63.  
  64.    if (twmReady)
  65.    {
  66.       Forbid();
  67.       
  68.       if ((twmport = FindPort(PORTNAME)) != NULL)
  69.          PutMsg(twmport, Delmsg);
  70.          
  71.       Permit();
  72.       
  73.       /* twm will reply the original (ADD) message before replying this 
  74.          one if it's going to reply it at all... hence loop exit condition
  75.       */
  76.       if (twmport != NULL)
  77.          do
  78.          {
  79.             WaitPort(mp);
  80.          } while (GetMsg(mp) != Delmsg);
  81.    }
  82. }        
  83.  
  84.  
  85. twmInit ()
  86. {
  87.    /* don't re-initialize */
  88.    if (twmReady)
  89.       return TRUE;
  90.       
  91.    /* set up our messages, allocate a port */
  92.    if ((mp = CreatePort(NULL, 0L)) == NULL
  93.        || (Delmsg = AllocMem(TWM_MSGSIZE, MEMF_CLEAR)) == NULL
  94.        || (Addmsg = AllocMem(TWM_MSGSIZE, MEMF_CLEAR)) == NULL
  95.       )
  96.    {
  97.       twmCleanUp();
  98.       return FALSE;
  99.    }
  100.    else
  101.    {
  102.       Delmsg->tmMessage.mn_ReplyPort = mp;
  103.       Addmsg->tmMessage.mn_ReplyPort = mp;
  104.       
  105.       Delmsg->tmMessage.mn_Node.ln_Type = NT_MESSAGE;
  106.       Addmsg->tmMessage.mn_Node.ln_Type = NT_MESSAGE;
  107.       
  108.       return (twmReady = TRUE);
  109.    }
  110. }
  111.  
  112.  
  113. twmCleanUp ()
  114. {
  115.    twmReady = FALSE;
  116.    
  117.    if (mp)        DeletePort(mp);
  118.    if (Delmsg)    FreeMem(Delmsg, TWM_MSGSIZE);
  119.    if (Addmsg)    FreeMem(Addmsg, TWM_MSGSIZE);
  120. }
  121.  
  122.  
  123.