home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 635.lha / hold_1.43b / hold.c < prev    next >
C/C++ Source or Header  |  1992-01-18  |  5KB  |  216 lines

  1. /*
  2.  * Hold.c - Holds the right mouse button while making menu selections
  3.  *
  4.  * Bruno Costa - 14 Sep 89 - 19 Jan 91
  5.  *
  6.  * ( Based on PopCLI III by John Toebes )
  7.  *
  8.  *  Compile under Lattice C 5.02 with options -cus -v
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/interrupts.h>
  13. #include <devices/inputevent.h>
  14. #include <devices/input.h>
  15. #include <devices/timer.h>
  16. #include <intuition/intuitionbase.h>
  17. #include <libraries/dos.h>
  18. #include <proto/exec.h>
  19. #include <proto/dos.h>
  20. #include <proto/intuition.h>
  21. #include <string.h>
  22. #include "hold.h"
  23.  
  24. #if !DETACH
  25. BPTR _Backstdout;         /* standard output */
  26. #else
  27. extern BPTR _Backstdout;     /* standard output when run in background */
  28. #endif
  29. long _BackGroundIO = 1;        /* Flag to tell it if we want to do I/O   */
  30. long _stack = 4000;        /* Amount of stack space our task needs   */
  31. char *_procname = TASKNAME;    /* The name of the task to create         */
  32. long _priority = 5;        /* The priority to run us at              */
  33.  
  34. extern struct DosLibrary *DosBase;
  35. struct IntuitionBase *IntuitionBase = NULL;
  36. struct Library *LayersBase = NULL;
  37.  
  38. /* input handler */
  39. extern void handler (void);
  40.  
  41.  
  42. void putmsg (char *msg)
  43. {
  44.  if (_Backstdout)
  45.    Write (_Backstdout, msg, strlen (msg));
  46. }
  47.  
  48.  
  49. #define loop for(;;)
  50.  
  51.  
  52. void main (int argc, char *argv[])
  53. {
  54.  int stay = FALSE;
  55.  ULONG sigmask;
  56.  struct MsgPort *inputDevPort = NULL;
  57.  struct MsgPort *myport = NULL, *port = NULL;
  58.  struct IOStdReq *inputRequestBlock = NULL;
  59.  struct Interrupt handlerStuff;
  60.  struct handlerdata hd;
  61.  
  62. #if DEBUG
  63.  _Backstdout = Output();
  64. #endif
  65.  
  66.  /* test if hold is already installed */
  67.  if ((port = FindPort(PORTNAME)) == NULL)
  68.  {
  69.    if ((port = myport = CreatePort(PORTNAME, NULL)) == NULL)
  70.      goto abort;
  71.    stay = TRUE;
  72.  }
  73.  
  74.  hd.activate = ON;
  75.  hd.hold = ON;
  76.  
  77.  while (argc-- > 1)
  78.  {
  79.    if (argv[argc][0] == '-')
  80.    {
  81.      switch (argv[argc][1])
  82.      {
  83.        case 'q':
  84.          if (!stay)
  85.          {
  86.            putmsg (KILLMSG);
  87.            Signal (port->mp_SigTask, 1L << port->mp_SigBit);
  88.          }
  89.          else
  90.            putmsg (NOKILLMSG);
  91.  
  92.          stay = FALSE;
  93.          break;
  94.  
  95.        case 'a':
  96.          hd.activate = OFF;
  97.          break;
  98.  
  99.        case 'h':
  100.          hd.hold = OFF;
  101.          break;
  102.      }
  103.    }
  104.    else
  105.    {
  106.      putmsg (USAGE);
  107.      stay = FALSE;
  108.    }
  109.  }
  110.  
  111.  if (stay)
  112.    putmsg (BANNER);
  113.  
  114.  /* Let the original window go away */
  115.  if (_Backstdout)
  116.  {
  117.    Close(_Backstdout);
  118.    _Backstdout = NULL;
  119.  }
  120.  
  121.  if (!stay)
  122.    goto abort;
  123.  
  124.  IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",0);
  125.  LayersBase = (struct Library *) OpenLibrary ("layers.library",0);
  126.  
  127.  if ((inputDevPort = CreatePort (NULL, 0)) == NULL)
  128.    goto abort;
  129.  
  130.  if ((inputRequestBlock = CreateExtIO (inputDevPort,
  131.                                        sizeof (struct IOStdReq))) == NULL)
  132.    goto abort;
  133.  
  134.  if (OpenDevice ("input.device", 0, inputRequestBlock, 0))
  135.    goto abort;
  136.  
  137.  hd.ignore = 0;
  138.  hd.actbit = AllocSignal (-1);
  139.  hd.actmask = (1L << hd.actbit);
  140.  hd.task = FindTask (NULL);
  141.  hd.count = 0;
  142.  hd.activated = FALSE;
  143.  hd.maxtime = MAXTIME;
  144.  
  145.  handlerStuff.is_Data = (APTR) &hd;
  146.  handlerStuff.is_Code = handler;
  147.  handlerStuff.is_Node.ln_Pri = 55;    /* ahead of PopCLI and Intuition */
  148.  
  149.  inputRequestBlock->io_Command = IND_ADDHANDLER;
  150.  inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  151.  
  152.  DoIO ((struct IORequest *)inputRequestBlock);
  153.  
  154.  loop
  155.  {
  156.    sigmask = Wait (SIGBREAKF_CTRL_C | (1L << myport->mp_SigBit) | hd.actmask);
  157.  
  158.    if (sigmask & hd.actmask)
  159.    {
  160.      ULONG lock;
  161.      struct Window *wd;
  162.  
  163. #if !DEBUG
  164.      lock = LockIBase (0);
  165. #endif
  166.      wd = WhichWindow ();
  167.      if (wd == IntuitionBase->ActiveWindow)
  168.        wd = NULL;
  169. #if !DEBUG
  170.      UnlockIBase (lock);
  171. #endif
  172.  
  173. #if 0
  174.      if (wd && !(IntuitionBase->ActiveWindow->Flags & WBENCHWINDOW))
  175. #else
  176.      if (wd)
  177. #endif
  178.        ActivateWindow (wd);
  179.    }
  180.  
  181.    if (sigmask & ((1L << myport->mp_SigBit) | SIGBREAKF_CTRL_C))
  182.      break;
  183.  }
  184.  
  185. abort:
  186.  if (stay)
  187.    FreeSignal (hd.actbit);
  188.  
  189.  if (inputRequestBlock)
  190.  {
  191.    if (inputRequestBlock->io_Device)
  192.    {
  193.      inputRequestBlock->io_Command = IND_REMHANDLER;
  194.      inputRequestBlock->io_Data = (APTR) &handlerStuff;
  195.      DoIO ((struct IORequest *) inputRequestBlock);
  196.      CloseDevice ((struct IORequest *)inputRequestBlock);
  197.    }
  198.    DeleteExtIO (inputRequestBlock);
  199.  }
  200.  if (inputDevPort)
  201.    DeletePort (inputDevPort);
  202.  
  203.  if (myport)
  204.    DeletePort (myport);
  205.  
  206. #if !DEBUG
  207.  if (_Backstdout)
  208.    Close (_Backstdout);
  209. #endif
  210.  
  211.  if (IntuitionBase)
  212.    CloseLibrary ((struct Library *) IntuitionBase);
  213.  if (LayersBase)
  214.    CloseLibrary ((struct Library *)LayersBase);
  215. }
  216.