TweetFollow Us on Twitter

Cursor Wrap
Volume Number:5
Issue Number:0
Column Tag:Pascal Procedures

Related Info: Control Panel OS Utilities

Writing INITs in Pascal

By Steve Kiene, Lincoln, NE

Why Inits: Trap Patching, etc

INITs are pretty hot items these days. I have at least six in my System Folder and I know people that have fifteen or more. INITs do anything from making it easier to move through SFGetFiles (SFScrollINIT by Andy Hertzfeld) to creating large virtual desktops (SteppingOUT II). INITs are used to customize the Macintosh. They are very simple to install and remove (dragging them into or out of the System Folder).

The majority of INITs trap patches. Patch trapping is simply the rewriting of the Macintosh’s internal routines or the modification of these routines for special situations. This is done by using the traps GetTrapAddress,SetTrapAddress, NSetTrapAddress, and NGetTrapAddress. Usually the programmer calls GetTrapAddress and saves the address for later use; the programmer then puts this address somewhere,usually in his/her code so that his/her code can check for the special situation, do whatever-taken that it is the special situation, and then call the original trap address. All trap patches should call the original trap address; this allows traps to be patched more than one time and is the best bet for compatibility insurance.

I would guess that 90% of all INITs are written in assembly. I’m sure they probably say that INITs are much easier to write in assembly. Well, it’s probably true, if you know assembly, but, if you don’t know assembly, I would say that it isn’t all that easy. This article, since it is in the Pascal section, demonstrates how to write an INIT in Pascal, includes full source to an INIT that allows the cursor to wrap around the screen, and gives a couple of hints on writing INITs in general.

Having the INIT install our patch.

INITs must load their code into the system heap so that the code will stay around as application’s are launched (the application heap is flushed every launch or return to the Finder). The INIT part of the code should put the trap patch or whatever code into the system heap. In CursorWrap, the included INIT, we create a pointer in the system heap that is four bytes greater than the size of our VBL task code; we then BlockMove the code into the pointer starting at the location of the pointer plus four. We store the pointer to our VBL task in the four bytes before our code so that we have easy access to it. The code is written in MPW Pascal, but should be easy to convert to any other development system.

{1}

Unit CursorWrap;
Interface
Uses
 {$LOAD MAC.Dump}
 MemTypes,QuickDraw,OSIntf,Toolintf,PackIntf,Script;
Procedure SetUpVBL;
Procedure Wrap;

Implementation
Procedure SetUpVBL;
var
 theVBL : VBLTask;
 myQElem: QElemPtr;
 myErr  : OSErr;
 SaveZone : THz;
 SizeNeeded : Longint;
 PatchPtr : Ptr;
 theCode: Handle;
 thePtr : ^LongInt;
 dummyErr : OSErr;
Begin
 { * Get handle to our code *}
 theCode:=Get1Resource(‘INIT’,1);
 { * Use the system’s Heap * }
 SaveZone:=GetZone;
 SetZone(SystemZone);
 { * Get size of our patch code and our QElem ptr * }
 SizeNeeded:=SizeResource(theCode)-(LongInt(@SetUpVBL)-LongInt(theCode^))+sizeof(QElem);
 ResrvMem(SizeNeeded);
 If MemError<>NoErr then
 begin
 { * If not enough room in system heap then get out * }
 SysBeep(1);
 SetZone(SaveZone);
 exit(SetUpVBL);
 end;
 { * Get new ptr for our code * }
 PatchPtr:=NewPtr(SizeNeeded+4);
 { * blockmove our code in * }
 BlockMove(@Wrap,Pointer(Ord(PatchPtr)+4),SizeNeeded);
 { * get new ptr for our VBL task * }
 myQElem:=QElemPtr(NewPtr(sizeof(QElem)));
 { * restore zone * }
 SetZone(SaveZone);
 { * put our vbl task ptr’s address into the ptr where our patch code 
will be * }
 thePtr:=Pointer(PatchPtr);
 thePtr^:=LongInt(myQElem);
 { * set up VBL and install * }
 with theVBL do
 begin
 qType:=Ord(vType);
 vblAddr:=Pointer(Ord(PatchPtr)+4);
 vblCount:=6;
 vblPhase:=0;
 end;
 myQElem^.vblQElem:=theVBL;
 dummyErr:=VInstall(myQElem);
End;
{------------------------------------------------------}
Procedure Wrap;
{**
This code allows the cursor to seemly wrap around the screen when the 
<Option> key is held down.  It checks the low level interupt mouse location 
(mTemp) and if it is on one edge of the screen it moves it to the the 
other edge.  It puts the new cursor cooridinate into mTemp and then puts 
$FF into cursorNew--this tells the cursor routines that the cursor has 
moved 
**}
const
 CurrentA5= $904;
var
 myQElem: QElemPtr;
 thePtr : ^LongInt;
 CursorCoordPtr  : ^Point;
 ChangedPtr : ^Byte;
 changed: Boolean;
 theMap : KeyMap;
 currGDevice: GDHandle;
 mouseRect: Rect;
 myRectPtr: ^Rect;
 myPtr,myPtr2    : ^longint;
 myRect : Rect;
Begin
 { * Get the keymap and check if option down; if is not then do not allow 
wrap * }
 GetKeys(theMap);
 If theMap[58] then
 Begin
 { * set up ptr to low memory global of cursor location * }
 CursorCoordPtr:=Pointer($828);
 changed:=false;
 { * get rectangle of screen * }
 currGDevice:=GetGDevice;
 If currGDevice<>Nil then
 mouseRect:=currGDevice^^.gdRect
 else
 begin
 { * Use currentA5 to get offset to screenBits.bounds * }
 myPtr:=pointer(CurrentA5);
 myPtr2:=pointer(myPtr^);
 myRectPtr:=Pointer(myPtr2^-116);
 mouseRect:=myRectPtr^;
 end;
 InsetRect(mouseRect,1,1);
 { * check cursor location and change it if wrapping * }
 With CursorCoordPtr^,mouseRect do
 Begin
 if v<=top then
 Begin
 v:=bottom-1;
 changed:=true;
 End
 else if v>=bottom then
 Begin
 v:=top+1;
 changed:=true;
 End;
 if h<=left then
 Begin
 h:=right-1;
 changed:=true;
 End
 else if h>=right then
 Begin
 h:=left+1;
 changed:=true;
 End;
 End;
 { * if we changed the cursor location then set the low memory global 
cursorNew * }
 If changed then
 Begin
 changedPtr:=Pointer($8CE);
 { * this tells the cursor drawing routines that the cursor is moved 
* }
 changedPtr^:=$FF;
 End;
 End;
 { * get ptr to VBL taks from where we originally put it * }
 thePtr:=Pointer(Ord(@Wrap)-4);
 { * reset the vblcount so that we are executed again * }
 myQElem:=QElemPtr(thePtr^);
 myQElem^.vblQElem.vblCount:=6;
End;
End.

Another Way

There is another way to store variables. In our example INIT this would be the VBL pointer address. This way involves using a header file that is written in assembler. The header file does nothing but branch over instructions that take up space. The header would look something like the following in MPW assembly:

Header  MAIN EXPORT

Header1
 BRA.S  @1
 DC.L 0
 DC.L 0
 DC.L 0
@1
 END

This header would leave room for twelve bytes, three handles, or whatever. To use this you would have to link the Header.a.o in first and reference it in you pascal code. A little help:

{3}

Procedure Header; EXTERNAL;{must be after Implementation }
myPtr:=Ptr(Ord(@Header)+2); { myPtr points to the first byte of storage 
}

The above statement returns a pointer to the address of the header plus two bytes, so that we skip over the branch statement.

You would use this type of code when you are writing a WDEF, CDEF, etc. and you want to have some type of communication between your patches and your custom definition. We used this type of code when we wrote Tear-Off Menus and wanted our custom WDEF and MDEF to be able to access globals that our Menu and Window Manager traps used.

Added tips for coping with problems.

When writing INITs that are a little heavier than a ‘one trap patch’ INIT or a simple VBL installation, then you are most likely going to run into problems with certain programs. Maybe it will be because the guys at Microsoft didn’t quite follow all of the rules or perhaps it’s some other reason. Regardless the problem, the answer is to not install when you know you are not compatible. The easiest way to do this is to try and load the resource type of the creator type (ie. MSWD for Microsoft Word) when the application is launched.

{4}

theType:=Get1Resource(‘MSWD’,0);
If theType<>Nil then { the application exists }

Since some users can and do rename their applications, one can not just check for application names. Creator types should be unique (if they are registered with Apple) and no problems should be encountered this way.

Good luck with your INITs.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.