home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 049.lha / PtrOff.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  8KB  |  390 lines

  1. /*
  2.  *    This program is an INTUITIONized version of MouseOff by Denny Jenkins.
  3.  *    I 'borrowed' 10%-20% codes from that program.
  4.  *
  5.  *    'PtrOff' will cause the mouse pointer to disappear if the mouse
  6.  *    is inactive in a specified period and reappear if movement is
  7.  *    detected.
  8.  *
  9.  *    'PtrOff' can be activated through either CLI or WorkBench.
  10.  *    The time delay amount might be modified by clicking on the
  11.  *    window and then clicking on the gadget to modify the value.
  12.  *    The value should be in the range of 1 to 999.  Wrong value
  13.  *    will not be accepted.
  14.  *
  15.  *    'PtrOff' was compiled and linked with Aztec C 3.4a.  However,
  16.  *    there shouldn't be any problem compiling it with Lattice.
  17.  *
  18.  *    The minimum stack size for the program should be 2000 bytes
  19.  *
  20.  *    This program is public domain.
  21.  *
  22.  *    KNOWN BUG: this program doesn't like Preferences to be changed
  23.  *           while it's running.  Actually, what will happen is
  24.  *           that the new Preferences will be IGNORED.
  25.  *
  26.  *    written by Giao Vu
  27.  *                 BIX: giaovan
  28.  */
  29. #include <exec/types.h>
  30. #include <devices/timer.h>
  31. #include <libraries/dos.h>
  32. #include <intuition/intuition.h>
  33.  
  34.  
  35. #define PORTNAME    "PtrOff.port"
  36.  
  37. #define INIT_TIME    10
  38. #define INIT_STRING    "10"
  39.  
  40. #define INT_PER_SECOND    2    /* # of timer alarm per second */
  41.  
  42. struct IntuitionBase *IntuitionBase;
  43.  
  44. struct NewWindow nw = {
  45.     80,0,149,10,
  46.     0,1,
  47.     CLOSEWINDOW | ACTIVEWINDOW | INACTIVEWINDOW | GADGETUP,
  48.     WINDOWCLOSE | SIMPLE_REFRESH | WINDOWDRAG | WINDOWDEPTH | NOCAREREFRESH,
  49.     NULL, NULL,
  50.     (UBYTE*) "PtrOff",
  51.     NULL, NULL,
  52.     149,10,149,56,
  53.     WBENCHSCREEN
  54. };
  55.  
  56. SHORT coord[] = {
  57.     0, 0,
  58.     0, 10,
  59.     36, 10,
  60.     36, 0,
  61.     0, 0,
  62.  
  63.     2, 11,
  64.     37, 11,
  65.     37, 1,
  66.  
  67.     38, 11,
  68.     38, 1,
  69.     };
  70.  
  71. struct Border Border[] = {
  72.     { -2, -2, 1, 0, JAM1, 5, coord, &Border[1] },
  73.     { -2, -2, 1, 0, JAM1, 3, &coord[10], &Border[2] },
  74.     { -2, -2, 1, 0, JAM1, 2, &coord[16], NULL },
  75.     };
  76.  
  77. struct IntuiText timetext =
  78.     { 3, 0, JAM1, -90, 0, NULL, (UBYTE *)"Delay Time", NULL };
  79.  
  80. char str_buffer[4];
  81. char undo_buffer[4];
  82.  
  83. struct StringInfo str_info = {
  84.     (UBYTE *)str_buffer,
  85.     (UBYTE *)undo_buffer,
  86.     0,
  87.     4,
  88.     0,
  89.     0, 0, 0, 0, 0,
  90.     NULL,
  91.     0,
  92.     NULL
  93.     };
  94.  
  95. struct Gadget timegadget = {
  96.     NULL,
  97.     100, 19, 32, 10,
  98.     GADGHCOMP,
  99.     LONGINT | TOGGLESELECT | RELVERIFY,
  100.     STRGADGET,
  101.     (APTR)&Border[0],
  102.     NULL,
  103.     &timetext,
  104.     NULL,
  105.     (APTR)&str_info,
  106.     0,
  107.     NULL
  108.     };
  109.  
  110. struct Preferences MyCopy;
  111. struct Preferences OrigCopy;
  112.  
  113. extern struct Library *OpenLibrary();
  114. extern struct Window *OpenWindow();
  115. extern struct MsgPort *CreatePort();
  116. extern struct IOStdReq *CreateStdIO();
  117. extern struct IOStdReq *CreateExtIO();
  118. extern struct MsgPort *FindPort();
  119. extern struct Message *GetMsg();
  120. extern ULONG CheckIO();
  121. extern ULONG Wait();
  122.  
  123. _main()
  124. {
  125.     struct Window *w;
  126.     struct timerequest *tr;
  127.     struct timerequest *OpenTimer();
  128.  
  129.     IntuitionBase = (struct IntuitionBase *)
  130.                OpenLibrary("intuition.library", 32L);
  131.     if (IntuitionBase)
  132.     {
  133.     if (tr = OpenTimer())
  134.         {
  135.         if (w = OpenWindow(&nw))
  136.         {
  137.         MouseOff(w, tr);
  138.         CloseWindow(w);
  139.         }
  140.         CloseTimer(tr);
  141.         }
  142.     CloseLibrary(IntuitionBase);
  143.     }
  144. }
  145.  
  146. MouseOff(w, tr)
  147. register struct Window *w;
  148. struct timerequest *tr;
  149. {
  150.     register struct Screen *s = w->WScreen;
  151.     register int i;
  152.     ULONG WaitBits, WinBits, TimerBits, WakeBits;
  153.     ULONG MsgCode;
  154.     struct IntuiMessage *message;
  155.     struct MsgPort *TimerPort;
  156.     char *src, *dst;
  157.     SHORT count;
  158.     SHORT PointerOn;
  159.     SHORT timer_on;
  160.     SHORT LastX, LastY;
  161.     SHORT done;
  162.     SHORT timelimit;
  163.  
  164.     /*
  165.      * Default delay time before blanking the pointer
  166.      */
  167.     timelimit = INIT_TIME * INT_PER_SECOND;
  168.     strcpy(str_buffer, INIT_STRING);
  169.  
  170.     /*
  171.      * Setup wait masks
  172.      */
  173.     WinBits = 1L << w->UserPort->mp_SigBit;
  174.     TimerBits = 1L << tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit;
  175.     WaitBits = WinBits | TimerBits | SIGBREAKF_CTRL_C;
  176.  
  177.     /*
  178.      * Get original pointer
  179.      */
  180.     GetPrefs (&OrigCopy, (long) sizeof(OrigCopy));
  181.  
  182.     /*
  183.      * Copy to another buffer
  184.      */
  185.     src = (char *)&OrigCopy;
  186.     dst = (char *)&MyCopy;
  187.     for (i=0; i<sizeof(OrigCopy); i++)
  188.         *dst++ = *src++;
  189.  
  190.     /*
  191.      * Clear pointer's image in buffer
  192.      */
  193.     for (i=0; i<POINTERSIZE; i++)
  194.     MyCopy.PointerMatrix[i] = NULL;
  195.  
  196.     LastX = s->MouseX;
  197.     LastY = s->MouseY;
  198.  
  199.     count = 0;
  200.     PointerOn = TRUE;
  201.     done = FALSE;
  202.     timer_on = FALSE;
  203.  
  204.     TimerPort = tr->tr_node.io_Message.mn_ReplyPort;
  205.  
  206.     while (done == FALSE) 
  207.     {
  208.     if (timer_on == FALSE)
  209.         {
  210.         /*
  211.          * Starts timer every half second
  212.          */
  213.         tr->tr_node.io_Command = TR_ADDREQUEST;
  214.         tr->tr_time.tv_secs = 0;
  215.         tr->tr_time.tv_micro = 1000000 / INT_PER_SECOND;
  216.         SendIO(tr);
  217.         timer_on = TRUE;
  218.         }
  219.  
  220.     /*
  221.      * Waits for timer signal or close window signal
  222.      */
  223.     WakeBits = Wait(WaitBits);
  224.  
  225.     /*
  226.      * did the timer sets the signal?
  227.      */
  228.     if (WakeBits & TimerBits)
  229.         {
  230.         /*
  231.          * clears timer and message port
  232.          */
  233.         timer_on = FALSE;
  234.         while (GetMsg(TimerPort))
  235.         ;
  236.  
  237.         /*
  238.          * determines if the mouse has moved in the last period
  239.          */
  240.         if ((LastX == s->MouseX) && (LastY == s->MouseY))
  241.         {
  242.         /*
  243.          * determines if mouse has been immobile for awhile
  244.          */
  245.         if (++count > timelimit)
  246.             {
  247.             count = 0;
  248.             /*
  249.              * has the pointer been invisible?
  250.              */
  251.             if (PointerOn)
  252.             {
  253.             /*
  254.              * Poof!  Mouse is gone
  255.              */
  256.             SetPrefs(&MyCopy, (long) sizeof(MyCopy));
  257.             PointerOn = FALSE;
  258.             }
  259.             }
  260.              }
  261.         else
  262.         {
  263.         count = 0;
  264.         LastX = s->MouseX;
  265.         LastY = s->MouseY;
  266.         /*
  267.          * has the mouse been invisible?
  268.          */
  269.         if (!(PointerOn))
  270.             {
  271.             /*
  272.              * makes mouse visible again
  273.              */
  274.             SetPrefs(&OrigCopy, (long) sizeof(OrigCopy));
  275.             PointerOn = TRUE;
  276.             }
  277.         }
  278.         }
  279.  
  280.     /*
  281.      * determines if the user wants to terminate the program
  282.      */
  283.     if (WakeBits & WinBits)
  284.         {
  285.         count = 0;
  286.         /*
  287.          * cleans up message port
  288.          */
  289.         while (message = (struct IntuiMessage *)GetMsg(w->UserPort))
  290.         {
  291.         MsgCode = message->Class;
  292.         ReplyMsg(message);
  293.         switch(MsgCode)
  294.             {
  295.             case CLOSEWINDOW:
  296.             /*
  297.              * resets mouse pointer
  298.              */
  299.             done = TRUE;
  300.             break;
  301.             case GADGETUP:
  302.                 /*
  303.              * Did user enter bad number
  304.              */
  305.             if (str_info.LongInt < 1)
  306.                 {
  307.                 /*
  308.                  * Force user to reenter amount
  309.                  */
  310.                 DisplayBeep(s);
  311.                 ActivateGadget(&timegadget, w, 0L);
  312.                 }
  313.             else
  314.                 timelimit = str_info.LongInt * INT_PER_SECOND;
  315.             break;
  316.             case ACTIVEWINDOW:
  317.                 /*
  318.              * Make window active by bringing it to foreground
  319.              * and the size bigger
  320.              */
  321.                 WindowToFront(w);
  322.             if (w->Height <= 10)
  323.                 {
  324.                 SizeWindow(w, 0L, 28L);
  325.                 AddGadget(w, &timegadget, -1L);
  326.                 }
  327.             break;
  328.             case INACTIVEWINDOW:
  329.                 /*
  330.              * Deactivate window and make it smaller
  331.              */
  332.             RemoveGadget(w, &timegadget);
  333.             if (w->Height > 10)
  334.                 SizeWindow(w, 0L, -28L);
  335.             break;
  336.             }
  337.         }
  338.         }
  339.  
  340.     /*
  341.      * user wants to exit program
  342.      */
  343.     if (WakeBits & SIGBREAKF_CTRL_C)
  344.         done = TRUE;
  345.     }
  346.     if (timer_on == TRUE && CheckIO(tr) == NULL)
  347.         AbortIO(tr);
  348.     if (!(PointerOn))
  349.     SetPrefs(&OrigCopy, (long) sizeof(OrigCopy));
  350. }
  351.  
  352. struct timerequest *
  353. OpenTimer()
  354. {
  355.     struct timerequest *tr;
  356.     struct MsgPort *tp;
  357.  
  358.     /*
  359.      * if the port already exists, just quits
  360.      * avoids collision when running this program too many times
  361.      */
  362.     if (FindPort(PORTNAME))
  363.         return ((struct timerequest *) 0);
  364.  
  365.     if (tp = CreatePort(PORTNAME, 0L))
  366.     {
  367.     tr = (struct timerequest *)
  368.         CreateExtIO(tp, (LONG)sizeof(struct timerequest));
  369.     if (tr)
  370.         {
  371.         if (OpenDevice(TIMERNAME, UNIT_VBLANK, tr, 0L))
  372.         DeleteExtIO(tr, (LONG)sizeof(struct timerequest));
  373.         else return tr;
  374.         }
  375.     DeletePort(tp);
  376.     }
  377.     return ((struct timerequest *) 0);
  378. }
  379.  
  380. CloseTimer(tr)
  381. struct timerequest *tr;
  382. {
  383.     struct MsgPort *tp;
  384.  
  385.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  386.     CloseDevice(tr);
  387.     DeleteExtIO(tr, (LONG)sizeof(struct timerequest));
  388.     DeletePort(tp);
  389. }
  390.