home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / lib_examples / divert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  7.0 KB  |  185 lines

  1. ;/* divert.c - commodity to monitor user inactivity - compiled with SASC 5.10
  2. LC -b0 -cfist -v -j73 divert.c
  3. Blink FROM LIB:c.o,divert.o TO divert LIBRARY LIB:LC.lib,LIB:Amiga.lib NODEBUG SC SD
  4. quit; */
  5. #include <exec/libraries.h>
  6. #include <libraries/commodities.h>
  7. #include <dos/dos.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/alib_protos.h>
  10. #include <clib/alib_stdio_protos.h>
  11. #include <clib/commodities_protos.h>
  12. #include <devices/inputevent.h>
  13.  
  14. #ifdef LATTICE
  15. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  16. int chkabort(void) { return(0); }
  17. #endif
  18.  
  19. #define TIMER_CLICKS 100
  20.  
  21. void main(int, char **);
  22. void ProcessMsg(void);
  23. void CxFunction(CxMsg *, CxObj *);
  24.  
  25. struct Library *CxBase, *IconBase;
  26. struct MsgPort *broker_mp;
  27. CxObj *broker, *cocustom, *cosignal;
  28.  
  29. struct NewBroker newbroker =
  30. {
  31.     NB_VERSION,
  32.     "Divert",           /* string to identify this broker */
  33.     "Divert",
  34.     "show divert",
  35.     NBU_UNIQUE | NBU_NOTIFY,  /* Don't want any new commodities starting with this name. */
  36.     0, 0, 0, 0                /* If someone tries it, let me know                        */
  37. };
  38.  
  39. struct Task *task;
  40. ULONG cxsigflag, signal, cxobjsignal;
  41.  
  42. void main(int argc, char **argv)
  43. {
  44.     UBYTE **ttypes;
  45.     CxMsg *msg;
  46.  
  47.     if (CxBase = OpenLibrary("commodities.library", 37L))
  48.     {
  49.         /* open the icon.library for support library functions, ArgArrayInit() and ArgArrayDone() */
  50.         if (IconBase = OpenLibrary("icon.library", 36L))
  51.         {
  52.             if (broker_mp = CreateMsgPort())
  53.             {
  54.                 newbroker.nb_Port = broker_mp;
  55.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  56.  
  57.                 /* ArgArrayInit() is a support library function (in the 2.0 version of amiga.lib) */
  58.                 /* that makes it easy to read arguments from either a CLI or from Workbench's     */
  59.                 /* ToolTypes. Because it uses icon.library, the library has to be open before     */
  60.                 /* before calling this function.  ArgArrayDone() cleans up after this function.   */
  61.                 ttypes = ArgArrayInit(argc, argv);
  62.  
  63.                 /* ArgInt() (in amiga.lib) searches through the array set up by ArgArrayInit()    */
  64.                 /* for a specific ToolType.  If it finds one, it returns the numeric value of the */
  65.                 /* number that followed the ToolType (i.e., CX_PRIORITY=7).  If it  doesn't find  */
  66.                 /* the ToolType, it returns the default value (the third argument)                */
  67.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  68.  
  69.                 if (broker = CxBroker(&newbroker, NULL))
  70.                 {
  71.                     /* CxCustom() takes two arguments, a pointer to the custom function           */
  72.                     /* and an ID. Commodities Exchange will assign that ID to any CxMsg           */
  73.                     /* passed to the custom  function.                                            */
  74.                     if (cocustom = CxCustom(CxFunction, 0L))
  75.                     {
  76.                         AttachCxObj(broker, cocustom);
  77.  
  78.                         /* Allocate a signal bit for the signal CxObj */
  79.                         if ( (signal = (ULONG)AllocSignal(-1L)) != -1)
  80.                         {
  81.                             /* set up the signal mask */
  82.                             cxobjsignal = 1L << signal;
  83.                             cxsigflag |= cxobjsignal;
  84.  
  85.                             /* CxSignal takes two arguments, a pointer to the task to signal      */
  86.                             /* (normally the commodity) and the number of the signal bit the      */
  87.                             /* commodity acquired to signal with.                                 */
  88.                             task = FindTask(NULL);
  89.                             if (cosignal = CxSignal(task, signal))
  90.                             {
  91.                                 AttachCxObj(cocustom, cosignal);
  92.                                 ActivateCxObj(broker, 1L);
  93.                                 ProcessMsg();
  94.                             }
  95.                             FreeSignal(signal);
  96.                         }
  97.                     }
  98.                     /* DeleteCxObjAll() is a commodities.library function that not only deletes   */
  99.                     /* the CxObject pointed to in its argument, but it deletes all of the         */
  100.                     /* CxObjects that are attached to it.                                         */
  101.                     DeleteCxObjAll(broker);
  102.  
  103.                     /* Empty the port of all CxMsgs */
  104.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  105.                         ReplyMsg((struct Message *)msg);
  106.                 }
  107.                 DeletePort(broker_mp);
  108.             }
  109.             ArgArrayDone();   /* this amiga.lib function cleans up after ArgArrayInit()           */
  110.             CloseLibrary(IconBase);
  111.         }
  112.         CloseLibrary(CxBase);
  113.     }
  114. }
  115.  
  116. void ProcessMsg(void)
  117. {
  118.     extern struct MsgPort *broker_mp;
  119.     extern CxObj *broker;
  120.     extern ULONG cxsigflag;
  121.     CxMsg *msg;
  122.     ULONG sigrcvd, msgid;
  123.     LONG returnvalue = 1L;
  124.  
  125.     while (returnvalue)
  126.     {
  127.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  128.  
  129.         while(msg = (CxMsg *)GetMsg(broker_mp))
  130.         {
  131.             msgid = CxMsgID(msg);
  132.             ReplyMsg((struct Message *)msg);
  133.  
  134.             switch(msgid)
  135.             {
  136.                 case CXCMD_DISABLE:
  137.                     ActivateCxObj(broker, 0L);
  138.                     break;
  139.                 case CXCMD_ENABLE:
  140.                     ActivateCxObj(broker, 1L);
  141.                     break;
  142.                 case CXCMD_KILL:
  143.                     returnvalue = 0L;
  144.                     break;
  145.                 case CXCMD_UNIQUE:
  146.                     returnvalue = 0L;
  147.                     break;
  148.             }
  149.         }
  150.  
  151.         if (sigrcvd & SIGBREAKF_CTRL_C) returnvalue = 0L;
  152.  
  153.         /* Check to see if the signal CxObj signalled us. */
  154.         if (sigrcvd & cxobjsignal) printf("Got Signal\n");
  155.     }
  156. }
  157.  
  158. /* The custom function for the custom CxObject.  Any code for a custom CxObj must be short        */
  159. /* and sweet because it runs as part of the input.device task.                                    */
  160. void CxFunction(register CxMsg *cxm, CxObj *co)
  161. {
  162.     struct InputEvent *ie;
  163.     static ULONG time = 0L;
  164.  
  165.     /* Get the struct InputEvent associated with this CxMsg. Unlike the InputEvent                */
  166.     /* extracted from a CxSender's CxMsg, this is a *REAL* input event, be careful with it.       */
  167.     ie = (struct InputEvent *)CxMsgData(cxm);
  168.  
  169.     /* This custom function counts the number of timer events that go by while no other input     */
  170.     /* events occur.  If it counts more than a certain amount of timer events, it clears the      */
  171.     /* count and diverts the timer event CxMsg to the custom object's personal                    */
  172.     /* list.  If an event besides a timer event passes by, the timer event count is reset.        */
  173.     if (ie->ie_Class == IECLASS_TIMER)
  174.     {
  175.         time++;
  176.         if (time >= TIMER_CLICKS)
  177.         {
  178.             time = 0L;
  179.             DivertCxMsg(cxm, co, co);
  180.         }
  181.     }
  182.     else
  183.         time = 0L;
  184. }
  185.