kanjitalk755 wrote: ↑Mon Nov 08, 2021 5:41 am
Unfortunately the samples are not suitable.
It should continue to receive null events until the OS shuts down.
Here is some old code I managed to dig up. I don't exactly understand how this all works, being that it's been twenty years since I wrote this code, but it looks like it is an INIT that can patch both "SysTask()" and "GetNextEvent()". I don't quite understand how the GNE trap works, and I suspect some code is missing, but the code for trapping "SysTask()" seems to all be here.
Code: Select all
/************************************************************
EventTrap.c
Copyright Marcio Luis Teixeira, 1992.
All rights reserved
************************************************************/
#define _EVENT_TRAP_
#define D0MASK 0x0000FF00L /* To check for a non-null event */
#include <Pascal.h>
#include <SystemInfo.h>
#include <FileInfo.h>
#ifndef _NO_DIALOGS_
#include <Place Dialog.h>
#endif
#ifndef _NO_ERROR_
#include <Universal Error.h>
#endif
typedef pascal Boolean (*GNEPtr)(int,EventRecord*);
typedef pascal (*STaskPtr)(void);
pascal void ShowINIT(short iconID, short moveX);
void main(void);
void InstallMe(void);
void InstallSysTaskTrap();
void SysTaskFilter();
void EventFilter(void);
int EventTrap(Boolean IsEvent, EventRecord *TheEvent);
int EventInit(Boolean *ShowIcon);
void GetCurResFileName(char *TheName,int *TheVol);
void SetUpResFile();
void RestoreResFile();
Boolean IsRegistered;
ProcPtr OldFilter;
ProcPtr OldSysTask;
char *ResFileName;
int ResFileVol;
int SavedVolume;
int SavedResFile;
GrafPtr SavedGrafPort;
int ResRefNum;
Boolean InstallTrap;
Boolean InstallSysTask;
THz SavedZone;
CGrafPtr port;
CGrafPort gp;
typedef struct
{
char filler[76]; /* internal QD storage */
long randSeed;
BitMap screenBits;
Cursor arrow;
Pattern dkGray;
Pattern ltGray;
Pattern gray;
Pattern black;
Pattern white;
GrafPtr thePort;
} QDStorage;
static QDStorage QDGlobals;
/* Initialization_____________________________________________________________________*/
void main()
{
Handle MyHandle;
int theIcon = OkIconID;
Boolean ShowIcon;
/* save all system registers */
asm {
movem.l a0-a5/d0-d7, -(SP)
/* set up A4 to point to top of resource to access global */
LEA main, a4 ; put top address in A4
/* self-preservation */
LEA main, a0 ; put address of resource in parameter
_RecoverHandle
move.l a0, MyHandle ; who am i?
/* set up QuickDraw */
/*link a5,#(-300) ; space for QuickDraw globals
move.l a5,CurrentA5 ; we now have our own A5 world
pea -4(a5)
_InitGraf
move.l CurrentA5,d0;
sub.l #206,d0;
move.l d0,QDGlobals;*/
}
InitGraf(&QDGlobals.thePort);
#ifndef _NO_DIALOGS_
#ifdef _INIT_FONTS_AND_WINDOWS_
InitFonts();
InitWindows();
fontsDefined=TRUE;
#else
fontsDefined=FALSE;
#endif
toolboxDefined=FALSE;
#endif
/* Get Resource File Info */
ResFileName="\0 ";
GetCurResFileName(ResFileName,&ResFileVol);
#ifndef _NO_ERROR_
InitUnivErr(univErrID);
#endif
InitSystemInfo();
InitFileInfo();
/* open our port */
#ifndef _ScreenChooser_
if (hasColorQD)
OpenCPort(&gp);
else
OpenPort((GrafPtr)&gp);
port = &gp;
#else
if (hasColorQD)
GetCWMgrPort(&port);
else
GetWMgrPort(&port);
#endif
ShowIcon=TRUE;
InstallTrap=TRUE;
InstallSysTask=FALSE;
HLock(MyHandle);
DetachResource(MyHandle);
/* check for mouse button (to disable) */
if(Button())
theIcon = BadIconID;
else {
OSErr TheError;
char *TempChar;
if(EventInit(&ShowIcon))
{
if(InstallTrap) InstallMe();
if(InstallSysTask) InstallSysTaskTrap();
}
else
theIcon=BadIconID;
}
if(ShowIcon) ShowIconFamily(theIcon);
Exit:
/* Restore state & exit */
asm {
movem.l (SP)+, a0-a5/d0-d7 ; restore registers
}
}
/* Trap Instalation___________________________________________________________________*/
void InstallMe()
{
OldFilter = JGNEFilter;
JGNEFilter = (ProcPtr)EventFilter;
}
void InstallSysTaskTrap()
{
OldSysTask=(ProcPtr)GetTrapAddress(0xA9BA);
SetTrapAddress((long)SysTaskFilter,0xA9BA);
}
/* Trap Filters_______________________________________________________________________*/
void SysTaskFilter()
{
long myD0;
Boolean TempBool;
asm {
movem.l a1-a4/d1-d7, -(SP) ; save all registers
LEA main, A4 ; set up globals
move.l D0, myD0 ; save off D0
}
TempBool=EventTrap(FALSE,NULL);
asm {
move.l OldSysTask, A0; ; Get old proc address
move.l myD0, D0;
movem.l (SP)+, a1-a4/d1-d7 ; restore registers
UNLK A6 ; Wipe local variables
move.l a0,-(sp) ; Push SystemTask address
RTS ; Jump back to where we were
}
}
void EventFilter()
{
long myD0;
EventRecord *TheEvent;
Boolean IsEvent;
Ptr OldFxn;
asm {
movem.l a1-a4/d1-d7, -(SP) ; save all registers
LEA main, A4 ; set up globals
move.l A1, TheEvent ; TheEvent -> Event Record
move.l D0, myD0 ; save off D0
}
IsEvent=(Boolean)((myD0 & D0MASK)?1:0); /* is there an event? */
if (!EventTrap(IsEvent,TheEvent))
{
if (IsEvent) { /* fake a null event */
myD0 = 0L;
asm {
clr.b 8(A6) ; and set the pascal return value too
}
}
}
asm {
move.l OldFilter, A0; ; Get old GNE filter
move.l myD0, D0;
movem.l (SP)+, a1-a4/d1-d7 ; restore registers
UNLK A6 ; Wipe local variables
cmp.l #0, a0 ; Check to see if there is another filter
beq.s @1
move.l a0,-(sp) ; if so, push the next filter's address
@1
RTS ; Jump back to where we were
}
}
/* Utilities________________________________________________________________________*/
void GetCurResFileName(char *TheName,int *TheVol)
{
FCBPBRec TheParam;
OSErr TheError;
long gestaltResult;
int tempInt;
TheParam.ioCompletion=NULL;
TheParam.ioNamePtr=(StringPtr)TheName;
TheParam.ioVRefNum=0;
TheParam.ioRefNum=CurResFile();
TheParam.ioFCBIndx=0;
TheError=PBGetFCBInfo(&TheParam,FALSE);
TheError=GetVol(NULL,TheVol);
}
void SetSysZone(Boolean SetSys)
{
if(SetSys)
{
SavedZone=GetZone();
SetZone(SystemZone());
}
else
SetZone(SavedZone);
}
void SetUpResFile()
{
OSErr TheError;
TheError=GetVol(NULL,&SavedVolume);
SavedResFile=CurResFile();
TheError=SetVol(NULL,ResFileVol);
ResRefNum=OpenResFile(ResFileName);
GetPort(&SavedGrafPort);
}
void RestoreResFile()
{
OSErr TheError;
CloseResFile(ResRefNum);
TheError=SetVol(NULL,SavedVolume);
UseResFile(SavedResFile);
SetPort(SavedGrafPort);
}
void PutUpMessage(int ListID,int ErrorNum)
{
GrafPort TempPort;
Rect TempRect;
int MessLenght;
Str255 errorStr;
char *msg;
if(ErrorNum==-1)
NumToString((long)ListID,errorStr);
else
GetIndString(errorStr,ListID,ErrorNum);
if(errorStr[0]==0)
msg="\pCan't find needed resource! This file is damaged!";
else
msg=(char *)errorStr;
OpenPort(&TempPort);
TextSize(9);
TextFont(geneva);
MessLenght=StringWidth(msg)+10;
SetRect(&TempRect,42,42,42+MessLenght,59);
PaintRect(&TempRect);
OffsetRect(&TempRect,-2,-2);
EraseRect(&TempRect);
FrameRect(&TempRect);
MoveTo(45,52);
DrawString(msg);
}