home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / commodities / divert.e < prev    next >
Text File  |  1977-12-31  |  6KB  |  164 lines

  1. -> divert.e - commodity to monitor user inactivity
  2.  
  3. OPT PREPROCESS  -> E-Note: we are using the CxXXX creation macros
  4.  
  5. MODULE 'icon',
  6.        'commodities',
  7.        'amigalib/argarray',
  8.        'other/ecode',
  9.        'devices/inputevent',
  10.        'dos/dos',
  11.        'exec/libraries',
  12.        'exec/ports',
  13.        'libraries/commodities'
  14.  
  15. ENUM ERR_NONE, ERR_ARGS, ERR_BRKR, ERR_CRCX, ERR_CXERR, ERR_ECODE, ERR_LIB,
  16.      ERR_PORT, ERR_SIG
  17.  
  18. RAISE ERR_BRKR IF CxBroker()=NIL,
  19.       ERR_CRCX IF CreateCxObj()=NIL,  -> E-Note: the CxXXX macros use this
  20.       ERR_LIB  IF OpenLibrary()=NIL,
  21.       ERR_PORT IF CreateMsgPort()=NIL,
  22.       ERR_SIG  IF AllocSignal()=-1
  23.     
  24. CONST TIMER_CLICKS=100
  25.  
  26. DEF broker_mp=NIL:PTR TO mp, broker=NIL, cocustom=NIL, cosignal=NIL,
  27.     task, cxsigflag, signal=-1, cxobjsignal
  28.   
  29. -> E-Note: the C static used in the custom function is just a global variable
  30. DEF time=0
  31.  
  32. PROC main() HANDLE
  33.   DEF ttypes=NIL, msg, cxfunc
  34.   cxbase:=OpenLibrary('commodities.library', 37)
  35.   -> Open the icon.library for support functions, argXXX()
  36.   iconbase:=OpenLibrary('icon.library', 36)
  37.   broker_mp:=CreateMsgPort()
  38.   cxsigflag:=Shl(1, broker_mp.sigbit)
  39.  
  40.   -> argArrayInit() is a support function (in the 2.0 version of amigalib)
  41.   -> that makes it easy to read arguments from either a CLI or from
  42.   -> Workbench's ToolTypes.  Because it uses icon.library, the library has
  43.   -> to be open before before calling this function.  argArrayDone() cleans
  44.   -> up after this function.
  45.   -> E-Note: uses global "wbmessage" and "arg" so it needs no arguments
  46.   ttypes:=argArrayInit()
  47.  
  48.   broker:=CxBroker([NB_VERSION, 0, 'Divert',  -> String to identify this broker
  49.                     'Divert', 'Show divert',
  50.                     -> Don't want any new commodities starting with this name.
  51.                     -> If someone tries it, let me know
  52.                     NBU_UNIQUE OR NBU_NOTIFY, 0,
  53.   -> argInt() (in amigalib) searches through the array set up by argArrayInit()
  54.   -> for a specific ToolType.  If it finds one, it returns the numeric value of
  55.   -> the number that followed the ToolType (e.g., CX_PRIORITY=7).  If it doesn't
  56.   -> find the ToolType, it returns the default value (the third argument).
  57.                     argInt(ttypes, 'CX_PRIORITY', 0),
  58.                     0, broker_mp, 0]:newbroker, NIL)
  59.  
  60.   -> CxCustom() takes two arguments, a pointer to the custom function and an
  61.   -> ID.  Commodities Exchange will assign that ID to any CxMsg passed to the
  62.   -> custom function.
  63.   -> E-Note: eCodeCxCustom() protects an E function so you can use it as a
  64.   ->         CX custom function
  65.   IF NIL=(cxfunc:=eCodeCxCustom({cxFunction})) THEN Raise(ERR_ECODE)
  66.   cocustom:=CxCustom(cxfunc, 0)
  67.   AttachCxObj(broker, cocustom)
  68.  
  69.   -> Allocate a signal bit for the signal CxObj
  70.   signal:=AllocSignal(-1)
  71.   -> Set up the signal mask
  72.   cxobjsignal:=Shl(1, signal)
  73.   cxsigflag:=cxsigflag OR cxobjsignal
  74.  
  75.   -> CxSignal takes two arguments, a pointer to the task to signal (normally
  76.   -> the commodity) and the number of the signal bit the commodity acquired
  77.   -> to signal with.
  78.   task:=FindTask(NIL)
  79.   cosignal:=CxSignal(task, signal)
  80.   AttachCxObj(cocustom, cosignal)
  81.   ActivateCxObj(broker, TRUE)
  82.   processMsg()
  83.  
  84. EXCEPT DO
  85.   IF signal<>-1 THEN FreeSignal(signal)
  86.   -> DeleteCxObjAll() is a commodities.library function that not only deletes
  87.   -> the CxObject pointed to in its argument, but it deletes all of the
  88.   -> CxObjects that are attached to it.
  89.   IF broker THEN DeleteCxObjAll(broker)
  90.   -> This amigalib function cleans up after argArrayInit()
  91.   IF ttypes THEN argArrayDone()
  92.   IF broker_mp
  93.     -> Empty the port of all CxMsgs
  94.     WHILE msg:=GetMsg(broker_mp) DO ReplyMsg(msg)
  95.     DeleteMsgPort(broker_mp) -> E-Note: C version incorrectly uses DeletePort()
  96.   ENDIF
  97.   IF iconbase THEN CloseLibrary(iconbase)
  98.   IF cxbase THEN CloseLibrary(cxbase)
  99.   SELECT exception
  100.   CASE ERR_ARGS;  WriteF('Error: Could not parse tooltypes/arguments\n')
  101.   CASE ERR_BRKR;  WriteF('Error: Could not create broker\n')
  102.   CASE ERR_CRCX;  WriteF('Error: Could not create CX object\n')
  103.   CASE ERR_CXERR; WriteF('Error: Could not activate broker\n')
  104.   CASE ERR_ECODE; WriteF('Error: Ran out of memory in eCodeCxCustom()\n')
  105.   CASE ERR_LIB;   WriteF('Error: Could not open commodities.library\n')
  106.   CASE ERR_PORT;  WriteF('Error: Could not create message port\n')
  107.   CASE ERR_SIG;   WriteF('Error: Could not allocate signal\n')
  108.   ENDSELECT
  109. ENDPROC
  110.  
  111. PROC processMsg()
  112.   DEF msg, sigrcvd, msgid, done=FALSE
  113.   REPEAT
  114.     sigrcvd:=Wait(SIGBREAKF_CTRL_C OR cxsigflag)
  115.  
  116.     WHILE msg:=GetMsg(broker_mp)
  117.       msgid:=CxMsgID(msg)
  118.       ReplyMsg(msg)
  119.  
  120.       SELECT msgid
  121.       CASE CXCMD_DISABLE
  122.         ActivateCxObj(broker, FALSE)
  123.       CASE CXCMD_ENABLE
  124.         ActivateCxObj(broker, TRUE)
  125.       CASE CXCMD_KILL
  126.         done:=TRUE
  127.       CASE CXCMD_UNIQUE
  128.         done:=TRUE
  129.       ENDSELECT
  130.     ENDWHILE
  131.  
  132.     IF sigrcvd AND SIGBREAKF_CTRL_C THEN done:=TRUE
  133.  
  134.     -> Check to see if the signal CxObj signalled us.
  135.     IF sigrcvd AND cxobjsignal THEN WriteF('Got Signal\n')
  136.   UNTIL done
  137. ENDPROC
  138.  
  139. -> The custom function for the custom CxObject.  Any code for a custom CxObj
  140. -> must be short and sweet because it runs as part of the input.device task.
  141. PROC cxFunction(cxm, co)
  142.   DEF ie:PTR TO inputevent
  143.  
  144.   -> Get the struct InputEvent associated with this CxMsg.  Unlike the
  145.   -> InputEvent extracted from a CxSender's CxMsg, this is a *REAL* input
  146.   -> event, be careful with it.
  147.   ie:=CxMsgData(cxm)
  148.  
  149.   -> This custom function counts the number of timer events that go by while
  150.   -> no other input events occur.  If it counts more than a certain amount of
  151.   -> timer events, it clears the count and diverts the timer event CxMsg to the
  152.   -> custom object's personal list.  If an event besides a timer event passes
  153.   -> by, the timer event count is reset.
  154.   IF ie.class=IECLASS_TIMER
  155.     time++
  156.     IF time>=TIMER_CLICKS
  157.       time:=0
  158.       DivertCxMsg(cxm, co, co)
  159.     ENDIF
  160.   ELSE
  161.     time:=0
  162.   ENDIF
  163. ENDPROC
  164.