home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / xi-1 / hotkey.c < prev   
C/C++ Source or Header  |  1996-01-30  |  10KB  |  277 lines

  1. ;/* HotKey.c - Simple hot key commodity.  Compiled with SAS/C 6.56
  2. sc DATA=FAR NMINC STRMERGE NOSTKCHK IGNORE=73 hotkey.c
  3. slink FROM LIB:c.o,hotkey.o TO hotkey LIBRARY LIB:scnb.lib,LIB:amiga.lib
  4. quit
  5.  */
  6. /*
  7. Copyright (c) 1991 Commodore-Amiga, Inc.
  8.  
  9. This example is provided in electronic form by Commodore-Amiga,
  10. Inc. for use with the Amiga Mail Volume II technical publication.
  11. Amiga Mail Volume II contains additional information on the correct
  12. usage of the techniques and operating system functions presented in
  13. these examples.  The source and executable code of these examples may
  14. only be distributed in free electronic form, via bulletin board or
  15. as part of a fully non-commercial and freely redistributable
  16. diskette.  Both the source and executable code (including comments)
  17. must be included, without modification, in any copy.  This example
  18. may not be published in printed form or distributed with any
  19. commercial product. However, the programming techniques and support
  20. routines set forth in these examples may be used in the development
  21. of original executable software products for Commodore Amiga
  22. computers.
  23.  
  24. All other rights reserved.
  25.  
  26. This example is provided "as-is" and is subject to change; no
  27. warranties are made.  All use is at your own risk. No liability or
  28. responsibility is assumed.
  29. */
  30.  
  31. #include <exec/libraries.h>
  32. #include <libraries/commodities.h>
  33. #include <dos/dos.h>
  34. #include <clib/exec_protos.h>
  35. #include <clib/alib_protos.h>
  36. #include <clib/alib_stdio_protos.h>
  37. #include <clib/commodities_protos.h>
  38.  
  39. #ifdef LATTICE
  40. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  41. int chkabort(void) { return(0); }
  42. #endif
  43.  
  44. #define EVT_HOTKEY 1L
  45.  
  46. struct Library *CxBase, *IconBase;
  47.  
  48. void main(int, char **);
  49. LONG ProcessMsg(void);
  50.  
  51. struct MsgPort *broker_mp;
  52.  
  53. CxObj *broker, *filter, *sender, *translate;
  54.  
  55. struct NewBroker newbroker = {
  56.     NB_VERSION,
  57.     "AmigaMail HotKey",           /* string to identify this broker */
  58.     "A Simple HotKey",
  59.     "A simple hot key commodity",
  60.     NBU_UNIQUE | NBU_NOTIFY,      /* Don't want any new commodities
  61.                                    * starting with this name.  If someone
  62.                                    * tries it, let me know */
  63.     0,
  64.     0,
  65.     0,
  66.     0
  67. };
  68.  
  69.  
  70. ULONG cxsigflag;
  71.  
  72. void main(int argc, char **argv)
  73. {
  74.     char *hotkey, **ttypes;
  75.  
  76.     if (CxBase = OpenLibrary("commodities.library", 37L))
  77.     {
  78.         /* open the icon.library for the support library
  79.          * functions, ArgArrayInit() and ArgArrayDone()
  80.          */
  81.         if (IconBase = OpenLibrary("icon.library", 36L))
  82.         {
  83.             if (broker_mp = CreateMsgPort())
  84.             {
  85.                 newbroker.nb_Port = broker_mp;
  86.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  87.  
  88.                 /* ArgArrayInit() is a support library function
  89.                  * (from the 2.0 version of amiga.lib) that makes it
  90.                  * easy to read arguments from either a CLI or from
  91.                  * the Workbench's ToolTypes.  Because it uses
  92.                  * icon.library, the library has to be open before
  93.                  * calling this function.  ArgArrayDone() cleans up
  94.                  * after this function.
  95.                  */
  96.                 ttypes = ArgArrayInit(argc, argv);
  97.  
  98.                 /* ArgInt() (also from amiga.lib) searches through the
  99.                  * array set up by ArgArrayInit() for a specific
  100.                  * ToolType.  If it finds one, it returns the numeric
  101.                  * value of the number that followed the ToolType
  102.                  * (i.e. CX_PRIORITY=7).  If it doesn't find the ToolType,
  103.                  * it returns the default value (the third argument)
  104.                  */
  105.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  106.  
  107.                 /* ArgString() works just like ArgInt(), except it
  108.                  * returns a pointer to a string rather than an integer.
  109.                  * In the example below, if there is no ToolType "HOTKEY",
  110.                  * the function returns a pointer to "rawkey control esc".
  111.                  */
  112.                 hotkey = ArgString(ttypes, "HOTKEY", "rawkey control esc");
  113.  
  114.                 if (broker = CxBroker(&newbroker, NULL))
  115.                 {
  116.                     /* CxFilter() is a macro that creates a filter
  117.                      * CxObject.  This filter passes input events that
  118.                      * match the string pointed to by hotkey.
  119.                      */
  120.                     if (filter = CxFilter(hotkey))
  121.                     {
  122.                         /* Add a CxObject to another's personal list */
  123.                         AttachCxObj(broker, filter);
  124.  
  125.                         /* CxSender() creates a sender CxObject.  Every
  126.                          * time a sender gets a CxMessage, it sends a new
  127.                          * CxMessage to the port pointed to in the first
  128.                          * argument.  CxSender()'s second argument will be
  129.                          * the ID of any CxMessages the sender sends to
  130.                          * the port.  The data pointer associated with the
  131.                          * CxMessage will point to a *COPY* of the
  132.                          * InputEvent structure associated with the orginal
  133.                          * CxMessage.
  134.                          */
  135.                         if (sender = CxSender(broker_mp, EVT_HOTKEY))
  136.                         {
  137.                             AttachCxObj(filter, sender);
  138.  
  139.                             /* CxTranslate() creates a translate CxObject.
  140.                              * When a translate CxObject gets a CxMessage,
  141.                              * it deletes the original CxMessage and adds
  142.                              * a new input event to the input.device's
  143.                              * input stream after the Commodities input
  144.                              * handler.  CxTranslate's argument points
  145.                              * to an InputEvent structure from which to
  146.                              * create the new input event.  In this example,
  147.                              * the pointer is NULL, meaning no new event
  148.                              * should be introduced.
  149.                              */
  150.                             if (translate = (CxTranslate(NULL)))
  151.                             {
  152.                                 AttachCxObj(filter, translate);
  153.  
  154.                                 /* CxObjError() is a commodities.library
  155.                                  * function that returns the internal
  156.                                  * accumulated error code of a CxObject.
  157.                                  */
  158.                                 if (! CxObjError(filter))
  159.                                 {
  160.                                     ActivateCxObj(broker, 1L);
  161.                                     while (ProcessMsg());
  162.                                 }
  163.                             }
  164.                         }
  165.                     }
  166.                     /* DeleteCxObjAll() is a commodities.library function
  167.                      * that not only deletes the CxObject pointed to in
  168.                      * its argument, but it deletes all of the CxObjects
  169.                      * that are attached to it.
  170.                      */
  171.                     DeleteCxObjAll(broker);
  172.                 }
  173.                 DeletePort(broker_mp);
  174.             }
  175.  
  176.             /* this amiga.lib function cleans up after ArgArrayInit() */
  177.             ArgArrayDone();
  178.             CloseLibrary(IconBase);
  179.         }
  180.         CloseLibrary(CxBase);
  181.     }
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. LONG ProcessMsg(void)
  189. {
  190.     extern struct MsgPort *broker_mp;
  191.     extern CxObj *broker;
  192.  
  193.     extern ULONG cxsigflag;
  194.  
  195.     CxMsg *msg;
  196.  
  197.     ULONG sigrcvd, msgid, msgtype;
  198.     LONG returnvalue = 1L;
  199.  
  200.     sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  201.  
  202.     while(msg = (CxMsg *)GetMsg(broker_mp))
  203.     {
  204.         msgid = CxMsgID(msg);
  205.         msgtype = CxMsgType(msg);
  206.         ReplyMsg((struct Message *)msg);
  207.  
  208.         switch(msgtype)
  209.         {
  210.             case CXM_IEVENT:
  211.                 printf("A CXM_EVENT, ");
  212.                 switch(msgid)
  213.                 {
  214.                     case EVT_HOTKEY:
  215.                     /* We got the message from the sender CxObject */
  216.                         printf("You hit the HotKey.\n");
  217.                         break;
  218.  
  219.                     default:
  220.                         printf("unknown.\n");
  221.                         break;
  222.                 }
  223.                 break;
  224.  
  225.             case CXM_COMMAND:
  226.                 printf("A command: ");
  227.                 switch(msgid)
  228.                 {
  229.                     case CXCMD_DISABLE:
  230.                         printf("CXCMD_DISABLE\n");
  231.                         ActivateCxObj(broker, 0L);
  232.                         break;
  233.  
  234.                     case CXCMD_ENABLE:
  235.                         printf("CXCMD_ENABLE\n");
  236.                         ActivateCxObj(broker, 1L);
  237.                         break;
  238.  
  239.                     case CXCMD_KILL:
  240.                         printf("CXCMD_KILL\n");
  241.                         returnvalue = 0L;
  242.                         break;
  243.  
  244.                     case CXCMD_UNIQUE:
  245.                     /* Commodities Exchange can be told not
  246.                      * only to refuse to launch a commodity with
  247.                      * a name already in use but also can notify
  248.                      * the already running commodity that it happened.
  249.                      * It does this by sending a CXM_COMMAND with the
  250.                      * ID set to CXMCMD_UNIQUE.  If the user tries
  251.                      * to run a windowless commodity that is already
  252.                      * running, the user wants the commodity to shut down.
  253.                      */
  254.                         printf("CXCMD_UNIQUE\n");
  255.                         returnvalue = 0L;
  256.                         break;
  257.                     default:
  258.                         printf("Unknown msgid\n");
  259.                         break;
  260.                 }
  261.                 break;
  262.             default:
  263.                 printf("Unknown msgtype\n");
  264.                 break;
  265.         }
  266.     }
  267.  
  268.  
  269.     if (sigrcvd & SIGBREAKF_CTRL_C)
  270.     {
  271.         returnvalue = 0L;
  272.         printf("CTRL C signal break\n");
  273.     }
  274.  
  275.     return(returnvalue);
  276. }
  277.