home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termKludge.c < prev    next >
C/C++ Source or Header  |  1993-02-13  |  4KB  |  200 lines

  1. /*
  2. **    termKludge.c
  3. **
  4. **    A particularly nasty kludge
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* Local data. */
  13.  
  14. STATIC struct Interrupt        *InputHandler;
  15. STATIC struct IOStdReq        *InputRequest;
  16. STATIC struct MsgPort        *InputPort;
  17. STATIC struct SignalSemaphore    *InputSemaphore;
  18.  
  19. STATIC BYTE              MouseMoveAllowed    = TRUE,
  20.                  GoingDown        = FALSE;
  21.  
  22.     /* EventHandler(register __a0 struct InputEvent *EventChain):
  23.      *
  24.      *    This is a kludge. Just in case you haven't seen that
  25.      *    many in your life, get a close look at this one. There
  26.      *    is much to be learned!
  27.      *
  28.      *    This input event handler actually does nothing but
  29.      *    check each incoming input event to see whether the
  30.      *    left mouse button is currently being pressed. At this
  31.      *    time of writing Workbench 3.0 has the rather unnerving
  32.      *    habit to release any currently selected icon as soon
  33.      *    as the status display area of the `term' Workbench
  34.      *    window is to be redrawn. This kludge prevents the
  35.      *    status display area from getting redrawn while the
  36.      *    the `term' Workbench window happens to be inactive
  37.      *    and the user is currently pressing the left mouse
  38.      *    button.
  39.      */
  40.  
  41. STATIC struct InputEvent * __saveds __asm
  42. EventHandler(register __a0 struct InputEvent *EventChain)
  43. {
  44.     register struct InputEvent    *Event        = EventChain;
  45.     register BYTE             PermitRefresh    = TRUE;
  46.  
  47.         /* Process the event chain... */
  48.  
  49.     do
  50.     {
  51.             /* Is any mouse button currently
  52.              * being pressed?
  53.              */
  54.  
  55.         if(Event -> ie_Qualifier & (IEQUALIFIER_LEFTBUTTON | IEQUALIFIER_RBUTTON))
  56.              PermitRefresh = FALSE;
  57.     }
  58.     while(Event = Event -> ie_NextEvent);
  59.  
  60.     if(!GoingDown)
  61.     {
  62.             /* Update the semaphore data. */
  63.  
  64.         if(PermitRefresh != MouseMoveAllowed)
  65.         {
  66.             ObtainSemaphore(InputSemaphore);
  67.  
  68.             MouseMoveAllowed = PermitRefresh;
  69.  
  70.             ReleaseSemaphore(InputSemaphore);
  71.         }
  72.     }
  73.  
  74.         /* Return the unchanged event chain. */
  75.  
  76.     return(EventChain);
  77. }
  78.  
  79.     /* RefreshAllowed():
  80.      *
  81.      *    Check whether the status display area is to be redrawn
  82.      *    or not.
  83.      */
  84.  
  85. BYTE
  86. RefreshAllowed()
  87. {
  88.     BYTE Result = TRUE;
  89.  
  90.     if(SysBase -> LibNode . lib_Version == 39)
  91.     {
  92.         ObtainSemaphore(InputSemaphore);
  93.  
  94.         Result = MouseMoveAllowed;
  95.  
  96.         ReleaseSemaphore(InputSemaphore);
  97.     }
  98.  
  99.     return(Result);
  100. }
  101.  
  102.     /* DeleteInputHandler():
  103.      *
  104.      *    Remove the input event handler from the chain.
  105.      */
  106.  
  107. VOID
  108. DeleteInputHandler()
  109. {
  110.     if(InputHandler)
  111.     {
  112.         GoingDown = TRUE;
  113.  
  114.         WaitTime(0,MILLION / 5);
  115.  
  116.         InputRequest -> io_Command    = IND_REMHANDLER;
  117.         InputRequest -> io_Data        = InputHandler;
  118.  
  119.         DoIO(InputRequest);
  120.  
  121.         FreeVec(InputHandler);
  122.  
  123.         InputHandler = NULL;
  124.     }
  125.  
  126.     if(InputSemaphore)
  127.     {
  128.         FreeVec(InputSemaphore);
  129.  
  130.         InputSemaphore = NULL;
  131.     }
  132.  
  133.     if(InputRequest)
  134.     {
  135.         if(InputRequest -> io_Device)
  136.             CloseDevice(InputRequest);
  137.  
  138.         DeleteIORequest(InputRequest);
  139.  
  140.         InputRequest = NULL;
  141.     }
  142.  
  143.     if(InputPort)
  144.     {
  145.         DeleteMsgPort(InputPort);
  146.  
  147.         InputPort = NULL;
  148.     }
  149. }
  150.  
  151.     /* CreateInputHandler():
  152.      *
  153.      *    Link a custom input event handler into the event
  154.      *    handler chain. Note: cannot use commodities objects
  155.      *    here since the raw, unmodified events are required.
  156.      */
  157.  
  158. BYTE
  159. CreateInputHandler()
  160. {
  161.     if(SysBase -> LibNode . lib_Version >= 39)
  162.     {
  163.         if(InputPort = CreateMsgPort())
  164.         {
  165.             if(InputRequest = (struct IOStdReq *)CreateIORequest(InputPort,sizeof(struct IOStdReq)))
  166.             {
  167.                 if(!OpenDevice("input.device",0,InputRequest,NULL))
  168.                 {
  169.                     if(InputSemaphore = (struct SignalSemaphore *)AllocVec(sizeof(struct SignalSemaphore),MEMF_ANY | MEMF_PUBLIC))
  170.                     {
  171.                         InitSemaphore(InputSemaphore);
  172.  
  173.                         if(InputHandler = (struct Interrupt *)AllocVec(sizeof(struct Interrupt),MEMF_ANY | MEMF_CLEAR | MEMF_PUBLIC))
  174.                         {
  175.                             GoingDown = FALSE;
  176.  
  177.                             InputHandler -> is_Node . ln_Name    = "term input kludge handler";
  178.                             InputHandler -> is_Node . ln_Pri    = 51;
  179.                             InputHandler -> is_Code            = (VOID (*)())EventHandler;
  180.  
  181.                             InputRequest -> io_Command        = IND_ADDHANDLER;
  182.                             InputRequest -> io_Data            = InputHandler;
  183.  
  184.                             DoIO(InputRequest);
  185.  
  186.                             return(TRUE);
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.  
  193.         DeleteInputHandler();
  194.  
  195.         return(FALSE);
  196.     }
  197.     else
  198.         return(TRUE);
  199. }
  200.