home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
049.lha
/
PtrOff.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-11-20
|
8KB
|
390 lines
/*
* This program is an INTUITIONized version of MouseOff by Denny Jenkins.
* I 'borrowed' 10%-20% codes from that program.
*
* 'PtrOff' will cause the mouse pointer to disappear if the mouse
* is inactive in a specified period and reappear if movement is
* detected.
*
* 'PtrOff' can be activated through either CLI or WorkBench.
* The time delay amount might be modified by clicking on the
* window and then clicking on the gadget to modify the value.
* The value should be in the range of 1 to 999. Wrong value
* will not be accepted.
*
* 'PtrOff' was compiled and linked with Aztec C 3.4a. However,
* there shouldn't be any problem compiling it with Lattice.
*
* The minimum stack size for the program should be 2000 bytes
*
* This program is public domain.
*
* KNOWN BUG: this program doesn't like Preferences to be changed
* while it's running. Actually, what will happen is
* that the new Preferences will be IGNORED.
*
* written by Giao Vu
* BIX: giaovan
*/
#include <exec/types.h>
#include <devices/timer.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#define PORTNAME "PtrOff.port"
#define INIT_TIME 10
#define INIT_STRING "10"
#define INT_PER_SECOND 2 /* # of timer alarm per second */
struct IntuitionBase *IntuitionBase;
struct NewWindow nw = {
80,0,149,10,
0,1,
CLOSEWINDOW | ACTIVEWINDOW | INACTIVEWINDOW | GADGETUP,
WINDOWCLOSE | SIMPLE_REFRESH | WINDOWDRAG | WINDOWDEPTH | NOCAREREFRESH,
NULL, NULL,
(UBYTE*) "PtrOff",
NULL, NULL,
149,10,149,56,
WBENCHSCREEN
};
SHORT coord[] = {
0, 0,
0, 10,
36, 10,
36, 0,
0, 0,
2, 11,
37, 11,
37, 1,
38, 11,
38, 1,
};
struct Border Border[] = {
{ -2, -2, 1, 0, JAM1, 5, coord, &Border[1] },
{ -2, -2, 1, 0, JAM1, 3, &coord[10], &Border[2] },
{ -2, -2, 1, 0, JAM1, 2, &coord[16], NULL },
};
struct IntuiText timetext =
{ 3, 0, JAM1, -90, 0, NULL, (UBYTE *)"Delay Time", NULL };
char str_buffer[4];
char undo_buffer[4];
struct StringInfo str_info = {
(UBYTE *)str_buffer,
(UBYTE *)undo_buffer,
0,
4,
0,
0, 0, 0, 0, 0,
NULL,
0,
NULL
};
struct Gadget timegadget = {
NULL,
100, 19, 32, 10,
GADGHCOMP,
LONGINT | TOGGLESELECT | RELVERIFY,
STRGADGET,
(APTR)&Border[0],
NULL,
&timetext,
NULL,
(APTR)&str_info,
0,
NULL
};
struct Preferences MyCopy;
struct Preferences OrigCopy;
extern struct Library *OpenLibrary();
extern struct Window *OpenWindow();
extern struct MsgPort *CreatePort();
extern struct IOStdReq *CreateStdIO();
extern struct IOStdReq *CreateExtIO();
extern struct MsgPort *FindPort();
extern struct Message *GetMsg();
extern ULONG CheckIO();
extern ULONG Wait();
_main()
{
struct Window *w;
struct timerequest *tr;
struct timerequest *OpenTimer();
IntuitionBase = (struct IntuitionBase *)
OpenLibrary("intuition.library", 32L);
if (IntuitionBase)
{
if (tr = OpenTimer())
{
if (w = OpenWindow(&nw))
{
MouseOff(w, tr);
CloseWindow(w);
}
CloseTimer(tr);
}
CloseLibrary(IntuitionBase);
}
}
MouseOff(w, tr)
register struct Window *w;
struct timerequest *tr;
{
register struct Screen *s = w->WScreen;
register int i;
ULONG WaitBits, WinBits, TimerBits, WakeBits;
ULONG MsgCode;
struct IntuiMessage *message;
struct MsgPort *TimerPort;
char *src, *dst;
SHORT count;
SHORT PointerOn;
SHORT timer_on;
SHORT LastX, LastY;
SHORT done;
SHORT timelimit;
/*
* Default delay time before blanking the pointer
*/
timelimit = INIT_TIME * INT_PER_SECOND;
strcpy(str_buffer, INIT_STRING);
/*
* Setup wait masks
*/
WinBits = 1L << w->UserPort->mp_SigBit;
TimerBits = 1L << tr->tr_node.io_Message.mn_ReplyPort->mp_SigBit;
WaitBits = WinBits | TimerBits | SIGBREAKF_CTRL_C;
/*
* Get original pointer
*/
GetPrefs (&OrigCopy, (long) sizeof(OrigCopy));
/*
* Copy to another buffer
*/
src = (char *)&OrigCopy;
dst = (char *)&MyCopy;
for (i=0; i<sizeof(OrigCopy); i++)
*dst++ = *src++;
/*
* Clear pointer's image in buffer
*/
for (i=0; i<POINTERSIZE; i++)
MyCopy.PointerMatrix[i] = NULL;
LastX = s->MouseX;
LastY = s->MouseY;
count = 0;
PointerOn = TRUE;
done = FALSE;
timer_on = FALSE;
TimerPort = tr->tr_node.io_Message.mn_ReplyPort;
while (done == FALSE)
{
if (timer_on == FALSE)
{
/*
* Starts timer every half second
*/
tr->tr_node.io_Command = TR_ADDREQUEST;
tr->tr_time.tv_secs = 0;
tr->tr_time.tv_micro = 1000000 / INT_PER_SECOND;
SendIO(tr);
timer_on = TRUE;
}
/*
* Waits for timer signal or close window signal
*/
WakeBits = Wait(WaitBits);
/*
* did the timer sets the signal?
*/
if (WakeBits & TimerBits)
{
/*
* clears timer and message port
*/
timer_on = FALSE;
while (GetMsg(TimerPort))
;
/*
* determines if the mouse has moved in the last period
*/
if ((LastX == s->MouseX) && (LastY == s->MouseY))
{
/*
* determines if mouse has been immobile for awhile
*/
if (++count > timelimit)
{
count = 0;
/*
* has the pointer been invisible?
*/
if (PointerOn)
{
/*
* Poof! Mouse is gone
*/
SetPrefs(&MyCopy, (long) sizeof(MyCopy));
PointerOn = FALSE;
}
}
}
else
{
count = 0;
LastX = s->MouseX;
LastY = s->MouseY;
/*
* has the mouse been invisible?
*/
if (!(PointerOn))
{
/*
* makes mouse visible again
*/
SetPrefs(&OrigCopy, (long) sizeof(OrigCopy));
PointerOn = TRUE;
}
}
}
/*
* determines if the user wants to terminate the program
*/
if (WakeBits & WinBits)
{
count = 0;
/*
* cleans up message port
*/
while (message = (struct IntuiMessage *)GetMsg(w->UserPort))
{
MsgCode = message->Class;
ReplyMsg(message);
switch(MsgCode)
{
case CLOSEWINDOW:
/*
* resets mouse pointer
*/
done = TRUE;
break;
case GADGETUP:
/*
* Did user enter bad number
*/
if (str_info.LongInt < 1)
{
/*
* Force user to reenter amount
*/
DisplayBeep(s);
ActivateGadget(&timegadget, w, 0L);
}
else
timelimit = str_info.LongInt * INT_PER_SECOND;
break;
case ACTIVEWINDOW:
/*
* Make window active by bringing it to foreground
* and the size bigger
*/
WindowToFront(w);
if (w->Height <= 10)
{
SizeWindow(w, 0L, 28L);
AddGadget(w, &timegadget, -1L);
}
break;
case INACTIVEWINDOW:
/*
* Deactivate window and make it smaller
*/
RemoveGadget(w, &timegadget);
if (w->Height > 10)
SizeWindow(w, 0L, -28L);
break;
}
}
}
/*
* user wants to exit program
*/
if (WakeBits & SIGBREAKF_CTRL_C)
done = TRUE;
}
if (timer_on == TRUE && CheckIO(tr) == NULL)
AbortIO(tr);
if (!(PointerOn))
SetPrefs(&OrigCopy, (long) sizeof(OrigCopy));
}
struct timerequest *
OpenTimer()
{
struct timerequest *tr;
struct MsgPort *tp;
/*
* if the port already exists, just quits
* avoids collision when running this program too many times
*/
if (FindPort(PORTNAME))
return ((struct timerequest *) 0);
if (tp = CreatePort(PORTNAME, 0L))
{
tr = (struct timerequest *)
CreateExtIO(tp, (LONG)sizeof(struct timerequest));
if (tr)
{
if (OpenDevice(TIMERNAME, UNIT_VBLANK, tr, 0L))
DeleteExtIO(tr, (LONG)sizeof(struct timerequest));
else return tr;
}
DeletePort(tp);
}
return ((struct timerequest *) 0);
}
CloseTimer(tr)
struct timerequest *tr;
{
struct MsgPort *tp;
tp = tr->tr_node.io_Message.mn_ReplyPort;
CloseDevice(tr);
DeleteExtIO(tr, (LONG)sizeof(struct timerequest));
DeletePort(tp);
}