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

  1. ;/* HotKey.c - Simple hot key commodity compiled with SASC 5.10
  2. LC -b0 -cfist -v -j73 hotkey.c
  3. Blink FROM LIB:c.o,hotkey.o TO hotkey LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  */
  6. #include <exec/libraries.h>
  7. #include <libraries/commodities.h>
  8. #include <dos/dos.h>
  9. #include <clib/exec_protos.h>
  10. #include <clib/alib_protos.h>
  11. #include <clib/alib_stdio_protos.h>
  12. #include <clib/commodities_protos.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 EVT_HOTKEY 1L
  20.  
  21. void main(int, char **);
  22. void ProcessMsg(void);
  23.  
  24. struct Library *CxBase, *IconBase;
  25. struct MsgPort *broker_mp;
  26. CxObj *broker, *filter, *sender, *translate;
  27.  
  28. struct NewBroker newbroker = {
  29.     NB_VERSION,
  30.     "RKM HotKey",           /* string to identify this broker */
  31.     "A Simple HotKey",
  32.     "A simple hot key commodity",
  33.     NBU_UNIQUE | NBU_NOTIFY,    /* Don't want any new commodities starting with this name. */
  34.     0, 0, 0, 0                  /* If someone tries it, let me know */
  35. };
  36.  
  37. ULONG cxsigflag;
  38.  
  39. void main(int argc, char **argv)
  40. {
  41.     UBYTE *hotkey, **ttypes;
  42.     CxMsg *msg;
  43.  
  44.     if (CxBase = OpenLibrary("commodities.library", 37L))
  45.     {
  46.         /* open the icon.library for the support library */
  47.         /* functions, ArgArrayInit() and ArgArrayDone()  */
  48.         if (IconBase = OpenLibrary("icon.library", 36L))
  49.         {
  50.             if (broker_mp = CreateMsgPort())
  51.             {
  52.                 newbroker.nb_Port = broker_mp;
  53.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  54.  
  55.                 /* ArgArrayInit() is a support library function (from the 2.0 version
  56.                  * of amiga.lib) that makes it easy to read arguments from either a
  57.                  * CLI or from Workbench's ToolTypes.  Because it uses icon.library,
  58.                  * the library has to be open before calling this function.
  59.                  * ArgArrayDone() cleans up after this function.
  60.                  */
  61.                 ttypes = ArgArrayInit(argc, argv);
  62.  
  63.                 /* ArgInt() (also from amiga.lib) searches through the array set up
  64.                  * by ArgArrayInit() for a specific ToolType.  If it finds one, it
  65.                  * returns the numeric value of the number that followed the
  66.                  * ToolType (i.e., CX_PRIORITY=7). If it doesn't find the ToolType,
  67.                  * it returns the default value (the third argument)
  68.                  */
  69.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  70.  
  71.                 /* ArgString() works just like ArgInt(), except it returns a pointer to a string
  72.                  * rather than an integer. In the example below, if there is no ToolType
  73.                  * "HOTKEY", the function returns a pointer to "rawkey control esc".
  74.                  */
  75.                 hotkey = ArgString(ttypes, "HOTKEY", "rawkey control esc");
  76.  
  77.                 if (broker = CxBroker(&newbroker, NULL))
  78.                 {
  79.                     /* CxFilter() is a macro that creates a filter CxObject.  This filter
  80.                      * passes input events that match the string pointed to by hotkey.
  81.                      */
  82.                     if (filter = CxFilter(hotkey))
  83.                     {
  84.                         /* Add a CxObject to another's personal list */
  85.                         AttachCxObj(broker, filter);
  86.  
  87.                         /* CxSender() creates a sender CxObject.  Every time a sender gets
  88.                          * a CxMessage, it sends a new CxMessage to the port pointed to in
  89.                          * the first argument. CxSender()'s second argument will be the ID
  90.                          * of any CxMessages the sender sends to the port.  The data pointer
  91.                          * associated with the CxMessage will point to a *COPY* of the
  92.                          * InputEvent structure associated with the orginal CxMessage.
  93.                          */
  94.                         if (sender = CxSender(broker_mp, EVT_HOTKEY))
  95.                         {
  96.                             AttachCxObj(filter, sender);
  97.  
  98.                             /* CxTranslate() creates a translate CxObject. When a translate
  99.                              * CxObject gets a CxMessage, it deletes the original CxMessage
  100.                              * and adds a new input event to the input.device's input stream
  101.                              * after the Commodities input handler. CxTranslate's argument
  102.                              * points to an InputEvent structure from which to create the new
  103.                              * input event.  In this example, the pointer is NULL, meaning no
  104.                              * new event should be introduced, which causes any event that
  105.                              * reaches this object to disappear from the input stream.
  106.                              */
  107.                             if (translate = (CxTranslate(NULL)))
  108.                             {
  109.                                 AttachCxObj(filter, translate);
  110.  
  111.                                 /* CxObjError() is a commodities.library function that returns
  112.                                  * the internal accumulated error code of a CxObject.
  113.                                  */
  114.                                 if (! CxObjError(filter))
  115.                                 {
  116.                                     ActivateCxObj(broker, 1L);
  117.                                     ProcessMsg();
  118.                                 }
  119.                             }
  120.                         }
  121.                     }
  122.                     /* DeleteCxObjAll() is a commodities.library function that not only
  123.                      * deletes the CxObject pointed to in its argument, but it deletes
  124.                      * all of the CxObjects that are attached to it.
  125.                      */
  126.                     DeleteCxObjAll(broker);
  127.  
  128.                     /* Empty the port of all CxMsgs */
  129.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  130.                         ReplyMsg((struct Message *)msg);
  131.                 }
  132.                 DeletePort(broker_mp);
  133.             }
  134.             /* this amiga.lib function cleans up after ArgArrayInit() */
  135.             ArgArrayDone();
  136.             CloseLibrary(IconBase);
  137.         }
  138.         CloseLibrary(CxBase);
  139.     }
  140. }
  141. void ProcessMsg(void)
  142. {
  143.     extern struct MsgPort *broker_mp;
  144.     extern CxObj *broker;
  145.     extern ULONG cxsigflag;
  146.     CxMsg *msg;
  147.     ULONG sigrcvd, msgid, msgtype;
  148.     LONG returnvalue = 1L;
  149.  
  150.     while(returnvalue)
  151.     {
  152.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  153.  
  154.         while(msg = (CxMsg *)GetMsg(broker_mp))
  155.         {
  156.             msgid = CxMsgID(msg);
  157.             msgtype = CxMsgType(msg);
  158.             ReplyMsg((struct Message *)msg);
  159.  
  160.             switch(msgtype)
  161.             {
  162.                 case CXM_IEVENT:
  163.                     printf("A CXM_EVENT, ");
  164.                     switch(msgid)
  165.                     {
  166.                         case EVT_HOTKEY: /* We got the message from the sender CxObject */
  167.                             printf("You hit the HotKey.\n");
  168.                             break;
  169.                         default:
  170.                             printf("unknown.\n");
  171.                             break;
  172.                     }
  173.                     break;
  174.                 case CXM_COMMAND:
  175.                     printf("A command: ");
  176.                     switch(msgid)
  177.                     {
  178.                         case CXCMD_DISABLE:
  179.                             printf("CXCMD_DISABLE\n");
  180.                             ActivateCxObj(broker, 0L);
  181.                             break;
  182.                         case CXCMD_ENABLE:
  183.                             printf("CXCMD_ENABLE\n");
  184.                             ActivateCxObj(broker, 1L);
  185.                             break;
  186.                         case CXCMD_KILL:
  187.                             printf("CXCMD_KILL\n");
  188.                             returnvalue = 0L;
  189.                             break;
  190.                         case CXCMD_UNIQUE:
  191.                         /* Commodities Exchange can be told not only to refuse to launch a
  192.                          * commodity with a name already in use but also can notify the
  193.                          * already running commodity that it happened. It does this by
  194.                          * sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE.  If the
  195.                          * user tries to run a windowless commodity that is already running,
  196.                          * the user wants the commodity to shut down. */
  197.                             printf("CXCMD_UNIQUE\n");
  198.                             returnvalue = 0L;
  199.                             break;
  200.                         default:
  201.                             printf("Unknown msgid\n");
  202.                             break;
  203.                     }
  204.                     break;
  205.                 default:
  206.                     printf("Unknown msgtype\n");
  207.                     break;
  208.             }
  209.         }
  210.         if (sigrcvd & SIGBREAKF_CTRL_C)
  211.         {
  212.             returnvalue = 0L;
  213.             printf("CTRL C signal break\n");
  214.         }
  215.     }
  216. }
  217.