home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 620.lha / CloneCommandKeys_v1.0 / CloneCommandKeys.c < prev    next >
C/C++ Source or Header  |  1992-02-14  |  5KB  |  227 lines

  1. ;/* Execute me to Compile and link
  2. shelltimer start verbose
  3. ; For debugging, use the following command:
  4. ;lc -. -b1 -cfistq -j73 -v -Lit -d3 -isrc:memlib -dMWDEBUG=1 CloneCommandKeys.c
  5. ; For develpoment, use the following commands:
  6. lc -. -O -ms -b1 -cfistq -j73 -v CloneCommandKeys.c
  7. blink define __main=__tinymain MAP mapfile NODEBUG SMALLCODE SMALLDATA FROM lib:c.o,CloneCommandKeys.o to CloneCommandKeys LIBRARY LIB:lc.lib,LIB:amiga.lib
  8. ; For production, use the following command:
  9. ;lc -. -O -ms -b0 -cfistq -j73 -Lit -tr CloneCommandKeys.c
  10. echo "Time to compile and link: " NOLINE
  11. shelltimer stop
  12. quit
  13. */
  14.  
  15. /***
  16. ****    CLONECOMMANDKEYS.C
  17. ****
  18. ****    Creation:        John Lindwall
  19. ****                    12 Feb 1992
  20. ****
  21. ****    Description:    CloneCommandKeys is a Commodity that maps the
  22. ****        AmigaDOS 2.04 Shell's CUT and PASTE  commands  to  any keys.
  23. ****        By default,  CloneCommandKey  will make LEFT-amiga-c also
  24. ****        operate as COPY, and LEFT-amiga-v  will  also  act  as PASTE.
  25. ****        Alternately, you can specify any key-mapping you like by
  26. ****        using ToolTypes.
  27. ****
  28. ****        This program requires AmigaDOS 2.04.
  29. ****
  30. ****        CloneCommandKeys is released into the Public Domain by the
  31. ****        author, John Lindwall.  I ask that the archive be kept intact
  32. ****        with the docs and the source code.
  33. ****        Bug reports should be sent to me at johnl@crash.cts.com.
  34. ****
  35. ****        The code looked nicer before I optimized for space...
  36. ****
  37. ****    Overhauls:
  38. ***/
  39.  
  40. #include <exec/libraries.h>
  41. #include <libraries/commodities.h>
  42. #include <dos/dos.h>
  43. #include <devices/inputevent.h>
  44.  
  45. #include <clib/exec_protos.h>
  46. #include <clib/alib_protos.h>
  47. #include <clib/commodities_protos.h>
  48. #include <clib/dos_protos.h>
  49.  
  50. #include <stdlib.h>
  51.  
  52. void main(int, char **);
  53. LONG ProcessMessages(void);
  54. BOOL SetupBroker(int argc, char *argv[], char **copyKey, char **pasteKey);
  55. BOOL CloneCommandKey(char *copyKey, struct InputEvent *event);
  56.  
  57. /* Error codes */
  58. #define ERR_OPENLIBFAIL        20
  59. #define ERR_BROKERFAIL        0    /* Uniqueness violation */
  60. #define ERR_CLONEFAIL        10
  61.  
  62. #define OPENLIBRARIES()     \
  63.     (((CxBase = OpenLibrary("commodities.library", 37L))!=NULL) &&  \
  64.     ((IconBase = OpenLibrary("icon.library", 36L))!=NULL) )
  65. #define CLOSELIBRARIES()    \
  66.     if( IconBase ) CloseLibrary(IconBase); \
  67.     if( CxBase ) CloseLibrary(CxBase);
  68. #define SHUTDOWNBROKER()    \
  69.     if( broker ) DeleteCxObjAll(broker); \
  70.     if( newbroker.nb_Port != NULL )    DeleteMsgPort(newbroker.nb_Port); \
  71.     ArgArrayDone();
  72.  
  73. UBYTE versionTag[] = "\0$VER: CloneCommandKeys 1.0 (14.02.92)";
  74.  
  75. struct Library *CxBase, *IconBase;
  76. CxObj *broker;
  77. ULONG cxSignal;
  78.  
  79. struct NewBroker newbroker =
  80. {
  81.     NB_VERSION,
  82.     "CloneCommandKeys",
  83.     &versionTag[7],
  84.     "Clones the COPY and PASTE command keys",
  85.     NBU_UNIQUE | NBU_NOTIFY,
  86.     0, 0, 0, 0
  87. };
  88.  
  89. /*
  90.  * Here are the InputEvents that our translate CxObjects jam into the
  91.  * event stream.  The copyEvent is the "rcommand c" keycode; the pasteEvent
  92.  * is the "rcommand v" keycode.
  93.  */
  94. struct InputEvent copyEvent =
  95. {
  96.     NULL, IECLASS_RAWKEY, IESUBCLASS_COMPATIBLE, 0x33, IEQUALIFIER_RCOMMAND
  97. };
  98.  
  99.  
  100. struct InputEvent pasteEvent =
  101. {
  102.     NULL, IECLASS_RAWKEY, IESUBCLASS_COMPATIBLE, 0x34, IEQUALIFIER_RCOMMAND
  103. };
  104.  
  105.  
  106. void
  107. main(int argc, char **argv)
  108. {
  109.     char *copyKey, *pasteKey;
  110.     int status;
  111.  
  112.     status = 0;
  113.     if( ! OPENLIBRARIES() )
  114.     {
  115.         status = ERR_OPENLIBFAIL;
  116.         if( argc != 0 )
  117.         {
  118.             Write(Output(), "Error opening libraries\n", 24);
  119.         }
  120.         goto exit;
  121.     }
  122.     if( SetupBroker(argc, argv, ©Key, &pasteKey) == FALSE )
  123.     {
  124.         status = ERR_BROKERFAIL;
  125.         goto exit;
  126.     }
  127.     if( CloneCommandKey(copyKey, ©Event) == FALSE ||
  128.         CloneCommandKey(pasteKey, &pasteEvent) == FALSE )
  129.     {
  130.         status = ERR_CLONEFAIL;
  131.         goto exit;
  132.     }
  133.     ActivateCxObj(broker, 1L);
  134.     while( ProcessMessages() )
  135.     {
  136.         ;    /* Empty */
  137.     }
  138.  
  139. exit:
  140.     SHUTDOWNBROKER();
  141.     CLOSELIBRARIES();
  142.     exit(status);
  143. }
  144.  
  145.  
  146. BOOL
  147. SetupBroker(int argc, char *argv[], char **copyKey, char **pasteKey)
  148. {
  149.     struct MsgPort *brokerPort;
  150.     char **toolTypes;
  151.  
  152.     if( brokerPort = CreateMsgPort() )
  153.     {
  154.         newbroker.nb_Port = brokerPort;
  155.         cxSignal = 1L << brokerPort->mp_SigBit;
  156.         toolTypes = ArgArrayInit(argc, argv);
  157.         newbroker.nb_Pri = (BYTE)ArgInt(toolTypes, "CX_PRIORITY", 0);
  158.         *copyKey = ArgString(toolTypes, "COPYKEY", "lcommand c");
  159.         *pasteKey = ArgString(toolTypes, "PASTEKEY", "lcommand v");
  160.         if( broker = CxBroker(&newbroker, NULL) )
  161.         {
  162.             return(TRUE);
  163.         }
  164.     }
  165.     return(FALSE);
  166. }
  167.  
  168.  
  169. BOOL
  170. CloneCommandKey(char *key, struct InputEvent *event)
  171. {
  172.     CxObj *filter, *translate;
  173.  
  174.     if( filter = CxFilter(key) )
  175.     {
  176.         AttachCxObj(broker, filter);
  177.         if( translate = CxTranslate(event) )
  178.         {
  179.             AttachCxObj(filter, translate);
  180.         }
  181.     }
  182.     if( ! CxObjError(filter) )
  183.     {
  184.         return(TRUE);
  185.     }
  186.     return(FALSE);
  187. }
  188.  
  189. LONG
  190. ProcessMessages(void)
  191. {
  192.     CxMsg *msg;
  193.     ULONG signalReceived;
  194.     LONG returnValue = 1L;
  195.  
  196.     signalReceived = Wait( SIGBREAKF_CTRL_C | cxSignal);
  197.  
  198.     while( msg = (CxMsg *) GetMsg(newbroker.nb_Port))
  199.     {
  200.         switch( CxMsgType(msg) )
  201.         {
  202.             case CXM_COMMAND:
  203.                 switch( CxMsgID(msg) )
  204.                 {
  205.                     case CXCMD_DISABLE:
  206.                         ActivateCxObj(broker, 0L);
  207.                         break;
  208.                     case CXCMD_ENABLE:
  209.                         ActivateCxObj(broker, 1L);
  210.                         break;
  211.                     case CXCMD_KILL:
  212.                     case CXCMD_UNIQUE:
  213.                         returnValue = 0L;
  214.                         break;
  215.                 }
  216.                 break;
  217.         }
  218.         ReplyMsg((struct Message *) msg);
  219.     }
  220.  
  221.     if( signalReceived & SIGBREAKF_CTRL_C )
  222.     {
  223.         returnValue = 0L;
  224.     }
  225.     return(returnValue);
  226. }
  227.