home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / term23_2.lha / Source_Code / termSource / termHotkeys.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  7KB  |  372 lines

  1. /*
  2. **    $Id: termHotkeys.c,v 1.4 92/08/15 20:14:18 olsen Sta Locker: olsen $
  3. **    $Revision: 1.4 $
  4. **    $Date: 92/08/15 20:14:18 $
  5. **
  6. **    Hotkey support routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14. enum    {    CX_TERMSCREENTOFRONT,CX_BUFFERSCREENTOFRONT,CX_SKIPDIALENTRY,CX_ABORTAREXX };
  15.  
  16.     /* Asynchronous hotkey task. */
  17.  
  18. STATIC struct Process *CxProcess;
  19.  
  20.     /* Hotkey(STRPTR Code,struct MsgPort *Port,LONG ID):
  21.      *
  22.      *    A custom version of the amiga.lib supplied code.
  23.      */
  24.  
  25. STATIC CxObj * __regargs
  26. CustomHotKey(STRPTR Code,struct MsgPort *Port,LONG ID)
  27. {
  28.     CxObj *Filter;
  29.  
  30.     if(Filter = CxFilter(Code))
  31.     {
  32.         CxObj *Sender;
  33.  
  34.         if(Sender = CxSender(Port,ID))
  35.         {
  36.             CxObj *Translator;
  37.  
  38.             AttachCxObj(Filter,Sender);
  39.  
  40.             if(Translator = CxTranslate(NULL))
  41.             {
  42.                 AttachCxObj(Filter,Translator);
  43.  
  44.                 if(!CxObjError(Filter))
  45.                     return(Filter);
  46.             }
  47.         }
  48.  
  49.         DeleteCxObjAll(Filter);
  50.     }
  51.  
  52.     return(NULL);
  53. }
  54.  
  55.     /* CreateBroker(struct MsgPort *CxPort):
  56.      *
  57.      *    Set up a CxObj commodity broker.
  58.      */
  59.  
  60. STATIC CxObj *
  61. CreateBroker(struct MsgPort *CxPort)
  62. {
  63.     CxObj *Broker;
  64.  
  65.         /* Set the commodity priority. */
  66.  
  67.     NewTermBroker . nb_Pri = Hotkeys . CommodityPriority;
  68.  
  69.         /* Create the broker. */
  70.  
  71.     if(Broker = CxBroker(&NewTermBroker,NULL))
  72.     {
  73.             /* Add the hotkeys. */
  74.  
  75.         AttachCxObj(Broker,CustomHotKey(Hotkeys . termScreenToFront,    CxPort,CX_TERMSCREENTOFRONT));
  76.         AttachCxObj(Broker,CustomHotKey(Hotkeys . BufferScreenToFront,    CxPort,CX_BUFFERSCREENTOFRONT));
  77.         AttachCxObj(Broker,CustomHotKey(Hotkeys . SkipDialEntry,    CxPort,CX_SKIPDIALENTRY));
  78.         AttachCxObj(Broker,CustomHotKey(Hotkeys . AbortARexx,        CxPort,CX_ABORTAREXX));
  79.  
  80.             /* Did an error show up? */
  81.  
  82.         if(!CxObjError(Broker))
  83.         {
  84.                 /* Broker has been added, now activate it. */
  85.  
  86.             ActivateCxObj(Broker,Hotkeys . HotkeysEnabled);
  87.  
  88.             return(Broker);
  89.         }
  90.  
  91.         DeleteCxObjAll(Broker);
  92.     }
  93.  
  94.     return(NULL);
  95. }
  96.  
  97.     /* TermCxServer():
  98.      *
  99.      *    Asynchronous hotkey server.
  100.      */
  101.  
  102. STATIC VOID __saveds
  103. TermCxServer()
  104. {
  105.     CxObj        *Broker;
  106.     struct MsgPort    *CxPort;
  107.     CxMsg        *Message;
  108.  
  109.         /* Create a reply port. */
  110.  
  111.     if(CxPort = CreateMsgPort())
  112.     {
  113.             /* Add the port to the public list. */
  114.  
  115.         CxPort -> mp_Node . ln_Name = NewTermBroker . nb_Name;
  116.  
  117.         AddPort(CxPort);
  118.  
  119.             /* Install the port. */
  120.  
  121.         NewTermBroker . nb_Port    = CxPort;
  122.  
  123.             /* Create the broker. */
  124.  
  125.         if(Broker = CreateBroker(CxPort))
  126.         {
  127.             ULONG    SignalSet;
  128.             BYTE    Terminated = FALSE;
  129.  
  130.                 /* Signal father task that we're done. */
  131.  
  132.             Signal(ThisProcess,SIGBREAKF_CTRL_C);
  133.  
  134.                 /* Loop and loop... */
  135.  
  136.             while(!Terminated)
  137.             {
  138.                     /* Wait for some signal. */
  139.  
  140.                 SignalSet = Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | (1 << CxPort -> mp_SigBit));
  141.  
  142.                     /* ^C aborts. */
  143.  
  144.                 if(SignalSet & SIGBREAKF_CTRL_C)
  145.                     Terminated = TRUE;
  146.  
  147.                     /* ^D removes the broker and
  148.                      * creates a new one.
  149.                      */
  150.  
  151.                 if(SignalSet & SIGBREAKF_CTRL_D)
  152.                 {
  153.                     DeleteCxObjAll(Broker);
  154.  
  155.                     Broker = CreateBroker(CxPort);
  156.                 }
  157.  
  158.                     /* A commodity message. */
  159.  
  160.                 if(SignalSet & (1 << CxPort -> mp_SigBit))
  161.                 {
  162.                     ULONG MessageType,MessageID;
  163.  
  164.                         /* Remove all messages. */
  165.  
  166.                     while(Message = (CxMsg *)GetMsg(CxPort))
  167.                     {
  168.                             /* Extract type and ID. */
  169.  
  170.                         MessageType    = CxMsgID(Message);
  171.                         MessageID    = CxMsgType(Message);
  172.  
  173.                         ReplyMsg((struct Message *)Message);
  174.  
  175.                             /* Take a look at the type... */
  176.  
  177.                         switch(MessageID)
  178.                         {
  179.                                 /* A hotkey was pressed. */
  180.  
  181.                             case CXM_IEVENT:    switch(MessageType)
  182.                                         {
  183.                                             case CX_TERMSCREENTOFRONT:    BumpWindow(TopWindow);
  184.                                                             break;
  185.  
  186.                                             case CX_BUFFERSCREENTOFRONT:    LaunchBuffer();
  187.                                                             break;
  188.  
  189.                                             case CX_SKIPDIALENTRY:        Signal(ThisProcess,SIGBREAKF_CTRL_F);
  190.                                                             break;
  191.  
  192.                                             case CX_ABORTAREXX:        if(RexxBackgroundProcess)
  193.                                                                 Signal(RexxBackgroundProcess,SIGBREAKF_CTRL_C);
  194.  
  195.                                                             break;
  196.                                         }
  197.  
  198.                                         break;
  199.  
  200.                                 /* An internal commodity command. */
  201.  
  202.                             case CXM_COMMAND:    switch(MessageType)
  203.                                         {
  204.                                             case CXCMD_DISABLE:    ActivateCxObj(Broker,Hotkeys . HotkeysEnabled = FALSE);
  205.                                                         break;
  206.  
  207.                                             case CXCMD_ENABLE:    ActivateCxObj(Broker,Hotkeys . HotkeysEnabled = TRUE);
  208.                                                         break;
  209.  
  210.                                             default:        break;
  211.                                         }
  212.  
  213.                                         break;
  214.                         }
  215.                     }
  216.                 }
  217.             }
  218.  
  219.                 /* Remove the broker. */
  220.  
  221.             DeleteCxObjAll(Broker);
  222.         }
  223.  
  224.             /* Remove the port from the public list. */
  225.  
  226.         RemPort(CxPort);
  227.  
  228.             /* Remove all pendig messages. */
  229.  
  230.         while(Message = (CxMsg *)GetMsg(CxPort))
  231.             ReplyMsg((struct Message *)Message);
  232.  
  233.             /* Delete the reply port. */
  234.  
  235.         DeleteMsgPort(CxPort);
  236.     }
  237.  
  238.     Forbid();
  239.  
  240.         /* Clear the task ID. */
  241.  
  242.     CxProcess = NULL;
  243.  
  244.         /* Signal father process that we're done. */
  245.  
  246.     Signal(ThisProcess,SIGBREAKF_CTRL_C);
  247. }
  248.  
  249.     /* ShutdownCx():
  250.      *
  251.      *    Remove the hotkey task.
  252.      */
  253.  
  254. VOID
  255. ShutdownCx()
  256. {
  257.     if(CxProcess)
  258.     {
  259.         Signal(CxProcess,SIGBREAKF_CTRL_C);
  260.  
  261.         Wait(SIGBREAKF_CTRL_C);
  262.     }
  263. }
  264.  
  265.     /* SetupCx():
  266.      *
  267.      *    Create the hotkey task.
  268.      */
  269.  
  270. BYTE
  271. SetupCx()
  272. {
  273.         /* If the task is already running, tell it to
  274.          * update the hotkey settings.
  275.          */
  276.  
  277.     if(CxProcess)
  278.     {
  279.         Signal(CxProcess,SIGBREAKF_CTRL_D);
  280.  
  281.         return(TRUE);
  282.     }
  283.     else
  284.     {
  285.         if(CxProcess = (struct Process *)CreateNewProcTags(
  286.             NP_Entry,    TermCxServer,
  287.             NP_Name,    "term hotkey process",
  288.             NP_Priority,    0,
  289.             NP_StackSize,    8192,
  290.             NP_WindowPtr,    -1,
  291.         TAG_END))
  292.         {
  293.             Wait(SIGBREAKF_CTRL_C);
  294.  
  295.             if(CxProcess)
  296.                 return(TRUE);
  297.         }
  298.     }
  299.  
  300.     return(FALSE);
  301. }
  302.  
  303. BYTE
  304. LoadHotkeys(STRPTR Name,struct Hotkeys *Keys)
  305. {
  306.     struct IFFHandle    *Handle;
  307.     BYTE             Success = FALSE;
  308.     struct StoredProperty    *Prop;
  309.     struct TermInfo        *TermInfo;
  310.  
  311.     if(Handle = AllocIFF())
  312.     {
  313.         if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
  314.         {
  315.             InitIFFasDOS(Handle);
  316.  
  317.             if(!OpenIFF(Handle,IFFF_READ))
  318.             {
  319.                 /* Collect version number ID if
  320.                  * available.
  321.                  */
  322.  
  323.                 if(!PropChunks(Handle,&VersionProps[0],1))
  324.                 {
  325.                     /* The following line tells iffparse to stop at the
  326.                      * very beginning of a `Type' chunk contained in a
  327.                      * `TERM' FORM chunk.
  328.                      */
  329.  
  330.                     if(!StopChunk(Handle,'TERM','HOTK'))
  331.                     {
  332.                         /* Parse the file... */
  333.  
  334.                         if(!ParseIFF(Handle,IFFPARSE_SCAN))
  335.                         {
  336.                             /* Did we get a version ID? */
  337.  
  338.                             if(Prop = FindProp(Handle,'TERM','VERS'))
  339.                             {
  340.                                 TermInfo = (struct TermInfo *)Prop -> sp_Data;
  341.  
  342.                                 if((TermInfo -> Version < TermVersion) || (TermInfo -> Version == TermVersion && TermInfo -> Revision < TermRevision))
  343.                                 {
  344.                                     if(ReadChunkBytes(Handle,Keys,sizeof(struct HotkeysOld)) == sizeof(struct HotkeysOld))
  345.                                     {
  346.                                         strcpy(Keys -> AbortARexx,"lshift rshift escape");
  347.  
  348.                                         Success = TRUE;
  349.                                     }
  350.                                 }
  351.                                 else
  352.                                 {
  353.                                     if(ReadChunkBytes(Handle,Keys,sizeof(struct Hotkeys)) == sizeof(struct Hotkeys))
  354.                                         Success = TRUE;
  355.                                 }
  356.                             }
  357.                         }
  358.                     }
  359.                 }
  360.  
  361.                 CloseIFF(Handle);
  362.             }
  363.  
  364.             Close(Handle -> iff_Stream);
  365.         }
  366.  
  367.         FreeIFF(Handle);
  368.     }
  369.  
  370.     return(Success);
  371. }
  372.