home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / commodities / popshell.c < prev   
C/C++ Source or Header  |  1992-09-03  |  8KB  |  212 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.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. #include <exec/libraries.h>
  34. #include <libraries/commodities.h>
  35. #include <dos/dos.h>
  36.  
  37. #include <clib/exec_protos.h>
  38. #include <clib/alib_protos.h>
  39. #include <clib/alib_stdio_protos.h>
  40. #include <clib/commodities_protos.h>
  41.  
  42. #ifdef LATTICE
  43. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  44. int chkabort(void) { return(0); }
  45. #endif
  46.  
  47.  
  48. void main(int, char **);
  49. void ProcessMsg(void);
  50.  
  51. #define EVT_HOTKEY 1L
  52. struct Library *CxBase, *IconBase;
  53. struct MsgPort *broker_mp;
  54. CxObj *broker, *filter, *sender, *translate;
  55.  
  56. struct NewBroker newbroker =
  57. {
  58.     NB_VERSION,
  59.     "RKM PopShell",           /* string to identify this broker */
  60.     "A Simple PopShell",
  61.     "A simple PopShell commodity",
  62.     NBU_UNIQUE | NBU_NOTIFY,      /* Don't want any new commodities starting with this name. */
  63.     0, 0, 0, 0                    /* If someone tries it, let me know */
  64. };
  65.  
  66. UBYTE *newshell = "\rllehswen";  /* "newshell" spelled backwards */
  67. struct InputEvent *ie;
  68. ULONG cxsigflag;
  69.  
  70.  
  71. void main(int argc, char **argv)
  72. {
  73.     UBYTE *hotkey, **ttypes;
  74.     CxMsg *msg;
  75.  
  76.     if (CxBase = OpenLibrary("commodities.library", 37L))
  77.     {
  78.         if (IconBase = OpenLibrary("icon.library", 36L))
  79.         {
  80.             if (broker_mp = CreateMsgPort())
  81.             {
  82.                 newbroker.nb_Port = broker_mp;
  83.                 cxsigflag = 1L << broker_mp->mp_SigBit;
  84.                 ttypes = ArgArrayInit(argc, argv);
  85.                 newbroker.nb_Pri = (BYTE)ArgInt(ttypes, "CX_PRIORITY", 0);
  86.                 hotkey = ArgString(ttypes, "HOTKEY", "rawkey control esc");
  87.  
  88.                 if (broker = CxBroker(&newbroker, NULL))
  89.                 {
  90.                     /* HotKey() is an amiga.lib function that creates a filter, sender */
  91.                     /* and translate CxObject and connects them to report a hot key    */
  92.                     /* press and delete its input event. */
  93.                     if (filter = HotKey(hotkey, broker_mp, EVT_HOTKEY))
  94.                     {
  95.                         AttachCxObj(broker, filter); /* Add a CxObject to another's personal list */
  96.  
  97.                         if (! CxObjError(filter))
  98.                         {
  99.                             /* InvertString() is an amiga.lib function that creates a linked */
  100.                             /* list of input events which would translate into the string    */
  101.                             /* passed to it.  Note that it puts the input events in the      */
  102.                             /* opposite order in which the corresponding letters appear in   */
  103.                             /* the string.  A translate CxObject expects them backwards.     */
  104.                             if (ie = InvertString(newshell, NULL))
  105.                             {
  106.                                 ActivateCxObj(broker, 1L);
  107.                                 ProcessMsg();
  108.                                 /* we have to release the memory allocated by InvertString.       */
  109.                                 FreeIEvents(ie);
  110.                             }
  111.                         }
  112.                     }
  113.                     /* DeleteCxObjAll() is a commodities.library function that not only      */
  114.                     /* deletes the CxObject pointed to in its argument, but deletes all of   */
  115.                     /* the CxObjects attached to it.                                         */
  116.                     DeleteCxObjAll(broker);
  117.  
  118.                     /* Empty the port of all CxMsgs */
  119.                     while(msg = (CxMsg *)GetMsg(broker_mp))
  120.                         ReplyMsg((struct Message *)msg);
  121.                 }
  122.                 DeletePort(broker_mp);
  123.             }
  124.             ArgArrayDone();  /* this amiga.lib function cleans up after ArgArrayInit() */
  125.             CloseLibrary(IconBase);
  126.         }
  127.         CloseLibrary(CxBase);
  128.     }
  129. }
  130.  
  131. void ProcessMsg(void)
  132. {
  133.     extern struct MsgPort *broker_mp;
  134.     extern CxObj *broker;
  135.     extern ULONG cxsigflag;
  136.     CxMsg *msg;
  137.     ULONG sigrcvd, msgid, msgtype;
  138.     LONG returnvalue = 1L;
  139.  
  140.     while (returnvalue)
  141.     {
  142.         sigrcvd = Wait(SIGBREAKF_CTRL_C | cxsigflag);
  143.  
  144.         while(msg = (CxMsg *)GetMsg(broker_mp))
  145.         {
  146.             msgid = CxMsgID(msg);
  147.             msgtype = CxMsgType(msg);
  148.             ReplyMsg((struct Message *)msg);
  149.  
  150.             switch(msgtype)
  151.             {
  152.                 case CXM_IEVENT:
  153.                     printf("A CXM_EVENT, ");
  154.                     switch(msgid)
  155.                     {
  156.                         case EVT_HOTKEY:
  157.                             /* We got the message from the sender CxObject */
  158.                             printf("You hit the HotKey.\n");
  159.                             /* Add the string "newshell" to input * stream.  If a shell       */
  160.                             /* gets it, it'll open a new shell.                               */
  161.                             AddIEvents(ie);
  162.                             break;
  163.                         default:
  164.                             printf("unknown.\n");
  165.                             break;
  166.                     }
  167.                     break;
  168.                 case CXM_COMMAND:
  169.                     printf("A command: ");
  170.                     switch(msgid)
  171.                     {
  172.                         case CXCMD_DISABLE:
  173.                             printf("CXCMD_DISABLE\n");
  174.                             ActivateCxObj(broker, 0L);
  175.                             break;
  176.                         case CXCMD_ENABLE:
  177.                             printf("CXCMD_ENABLE\n");
  178.                             ActivateCxObj(broker, 1L);
  179.                             break;
  180.                         case CXCMD_KILL:
  181.                             printf("CXCMD_KILL\n");
  182.                             returnvalue = 0L;
  183.                             break;
  184.                         case CXCMD_UNIQUE:
  185.                         /* Commodities Exchange can be told not only to refuse to launch a    */
  186.                         /* commodity with a name already in use but also can notify the       */
  187.                         /* already running commodity that it happened.  It does this by       */
  188.                         /* sending a CXM_COMMAND with the ID set to CXMCMD_UNIQUE.  If the    */
  189.                         /* user tries to run a windowless commodity that is already running,  */
  190.                         /* the user wants the commodity to shut down.                         */
  191.                             printf("CXCMD_UNIQUE\n");
  192.                             returnvalue = 0L;
  193.                             break;
  194.                         default:
  195.                             printf("Unknown msgid\n");
  196.                             break;
  197.                     }
  198.                     break;
  199.                 default:
  200.                     printf("Unknown msgtype\n");
  201.                     break;
  202.             }
  203.         }
  204.  
  205.