home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / PopShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  7.0 KB  |  185 lines

  1. ;/* PopShell.c - Simple hot key commodity compiled with SASC 5.10
  2. LC -b0 -cfist -v -j73 popshell.c
  3. Blink FROM LIB:c.o,popshell.o TO popshell 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.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/alib_stdio_protos.h>
  13. #include <clib/commodities_protos.h>
  14.  
  15. #ifdef LATTICE
  16. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  17. int chkabort(void) { return(0); }
  18. #endif
  19.  
  20.  
  21. void main(int, char **);
  22. void ProcessMsg(void);
  23.  
  24. #define EVT_HOTKEY 1L
  25. struct Library *CxBase, *IconBase;
  26. struct MsgPort *broker_mp;
  27. CxObj *broker, *filter, *sender, *translate;
  28.  
  29. struct NewBroker newbroker =
  30. {
  31.     NB_VERSION,
  32.     "RKM PopShell",           /* string to identify this broker */
  33.     "A Simple PopShell",
  34.     "A simple PopShell commodity",
  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. UBYTE *newshell = "\rllehswen";  /* "newshell" spelled backwards */
  40. struct InputEvent *ie;
  41. ULONG cxsigflag;
  42.  
  43.  
  44. void main(int argc, char **argv)
  45. {
  46.     UBYTE *hotkey, **ttypes;
  47.     CxMsg *msg;
  48.  
  49.     if (CxBase = OpenLibrary("commodities.library", 37L))
  50.     {
  51.         if (IconBase = OpenLibrary("icon.library", 36L))
  52.         {
  53.             if (broker_mp = CreateMsgPort())
  54.             {
  55.                 newbroker.nb_Port = broker_mp;
  56.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  57.                 ttypes = ArgArrayInit(argc, argv);
  58.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  59.                 hotkey = ArgString(ttypes, "HOTKEY", "rawkey control esc");
  60.  
  61.                 if (broker = CxBroker(&newbroker, NULL))
  62.                 {
  63.                     /* HotKey() is an amiga.lib function that creates a filter, sender */
  64.                     /* and translate CxObject and connects them to report a hot key    */
  65.                     /* press and delete its input event. */
  66.                     if (filter = HotKey(hotkey, broker_mp, EVT_HOTKEY))
  67.                     {
  68.                         AttachCxObj(broker, filter); /* Add a CxObject to another's personal list */
  69.  
  70.                         if (! CxObjError(filter))
  71.                         {
  72.                             /* InvertString() is an amiga.lib function that creates a linked */
  73.                             /* list of input events which would translate into the string    */
  74.                             /* passed to it.  Note that it puts the input events in the      */
  75.                             /* opposite order in which the corresponding letters appear in   */
  76.                             /* the string.  A translate CxObject expects them backwards.     */
  77.                             if (ie = InvertString(newshell, NULL))
  78.                             {
  79.                                 ActivateCxObj(broker, 1L);
  80.                                 ProcessMsg();
  81.                                 /* we have to release the memory allocated by InvertString.       */
  82.                                 FreeIEvents(ie);
  83.                             }
  84.                         }
  85.                     }
  86.                     /* DeleteCxObjAll() is a commodities.library function that not only      */
  87.                     /* deletes the CxObject pointed to in its argument, but deletes all of   */
  88.                     /* the CxObjects attached to it.                                         */
  89.                     DeleteCxObjAll(broker);
  90.  
  91.                     /* Empty the port of all CxMsgs */
  92.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  93.                         ReplyMsg((struct Message *)msg);
  94.                 }
  95.                 DeletePort(broker_mp);
  96.             }
  97.             ArgArrayDone();  /* this amiga.lib function cleans up after ArgArrayInit() */
  98.             CloseLibrary(IconBase);
  99.         }
  100.         CloseLibrary(CxBase);
  101.     }
  102. }
  103.  
  104. void ProcessMsg(void)
  105. {
  106.     extern struct MsgPort *broker_mp;
  107.     extern CxObj *broker;
  108.     extern ULONG cxsigflag;
  109.     CxMsg *msg;
  110.     ULONG sigrcvd, msgid, msgtype;
  111.     LONG returnvalue = 1L;
  112.  
  113.     while (returnvalue)
  114.     {
  115.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  116.  
  117.         while(msg = (CxMsg *)GetMsg(broker_mp))
  118.         {
  119.             msgid = CxMsgID(msg);
  120.             msgtype = CxMsgType(msg);
  121.             ReplyMsg((struct Message *)msg);
  122.  
  123.             switch(msgtype)
  124.             {
  125.                 case CXM_IEVENT:
  126.                     printf("A CXM_EVENT, ");
  127.                     switch(msgid)
  128.                     {
  129.                         case EVT_HOTKEY:
  130.                             /* We got the message from the sender CxObject */
  131.                             printf("You hit the HotKey.\n");
  132.                             /* Add the string "newshell" to input * stream.  If a shell       */
  133.                             /* gets it, it'll open a new shell.                               */
  134.                             AddIEvents(ie);
  135.                             break;
  136.                         default:
  137.                             printf("unknown.\n");
  138.                             break;
  139.                     }
  140.                     break;
  141.                 case CXM_COMMAND:
  142.                     printf("A command: ");
  143.                     switch(msgid)
  144.                     {
  145.                         case CXCMD_DISABLE:
  146.                             printf("CXCMD_DISABLE\n");
  147.                             ActivateCxObj(broker, 0L);
  148.                             break;
  149.                         case CXCMD_ENABLE:
  150.                             printf("CXCMD_ENABLE\n");
  151.                             ActivateCxObj(broker, 1L);
  152.                             break;
  153.                         case CXCMD_KILL:
  154.                             printf("CXCMD_KILL\n");
  155.                             returnvalue = 0L;
  156.                             break;
  157.                         case CXCMD_UNIQUE:
  158.                         /* Commodities Exchange can be told not only to refuse to launch a    */
  159.                         /* commodity with a name already in use but also can notify the       */
  160.                         /* already running commodity that it happened.  It does this by       */
  161.                         /* sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE.  If the    */
  162.                         /* user tries to run a windowless commodity that is already running,  */
  163.                         /* the user wants the commodity to shut down.                         */
  164.                             printf("CXCMD_UNIQUE\n");
  165.                             returnvalue = 0L;
  166.                             break;
  167.                         default:
  168.                             printf("Unknown msgid\n");
  169.                             break;
  170.                     }
  171.                     break;
  172.                 default:
  173.                     printf("Unknown msgtype\n");
  174.                     break;
  175.             }
  176.         }
  177.  
  178.         if (sigrcvd & SIGBREAKF_CTRL_C)
  179.         {
  180.             returnvalue = 0L;
  181.             printf("CTRL C signal break\n");
  182.         }
  183.     }
  184. }
  185.