TweetFollow Us on Twitter

ShiftMod
Volume Number:3
Issue Number:9
Column Tag:Technical Note

ShiftMod Patch

By Mike Scanlin, San Diego, CA

Last week we were talking with an IBM freak about the differences between our Macs and his footrest, that is, PC. One of his complaints about the Mac was that you get upper case letters when both the shift key and caps lock key are down at the same time. He wanted to be able to type a lower case letter if the caps lock key was down by pressing the shift key. Then he could type sentences like “i WISH i HAD A mACINTOSH” without having to hold down the shift key the whole time. Well, Mac fans, fear not. Now the Mac can do this too. Say hello to Mr. ShiftMod.

All that is really needed is a tail patch to _GetNextEvent that checks if a keyDown or autoKey event occured while the caps lock and shift keys were held down. The only problem with implementing it is that because _GetNextEvent is a stack based trap (it gets its parameters from the stack) the patch requires a bit of self modifying code to get it to set things up correctly. While this example is trivial, the technique used here can be used to patch any stack based trap routine.

A tail patch is something that first calls the original trap and then does some post-processing before returning to the caller. If the original trap is not stack based, we can do a JSR OriginalTrap at the beginning of our patch, do our post-processing, and end with an RTS. However, if the original trap is stack based it will expect the stack to look a certain way when it gets called. By doing a JSR to it, we put a return address on top of the existing stack which, in effect, shifts all of the parameters by 4 bytes (relative to the top of the stack). The routine being called does not know about the extra return address and will use the wrong bytes as parameters.

The code listed here is an application, but it can be easily (via ResEdit) be made into an INIT resource and pasted into your system file. It installs itself in the system heap and hangs around as long as you don’t turn your Mac off. This code is useful if you want to install any event processing stuff that is global for all applications. For instance, you could call _Eject every time you detect a diskEvt and count how many times someone tries to insert a disk in a minute. Or you could do something semi-useful like adding a keyclick after keyDowns (how about a mouseclick on mouseDowns?) for debugging.

Now how about an IBM hacker writing a routine to make his machine return upper case letters when both the shift key and caps lock key are down? Is it even possible?

/* shiftMod.c    2 July 1987
 * 
 * by Mike Scanlin and Andy Voelker
 *
 * This program installs a tail patch on _GetNextEvent so
 * that the shift key toggles between upper and lower case
 * letters if the caps lock key is down.
 *
 * No toggle is done if the option and/or command key was
 * held down.
 */
 
#include“Asm.h”
#include“EventMgr.h”

#define whatOFFSET(EventRecord,what)
#define message  OFFSET(EventRecord,message)
#define modifiersOFFSET(EventRecord,modifiers)

#define GetNextEvent 0xA970
#define JMP 0x4EF9
#define memFullErr -108

main()
{
 asm  {
 move.l D3,-(SP)
 
/* set up the JMP instruction at the end of the patch */
 lea    @patchExit,A0
 move #JMP,(A0)

/* get the old trap address */
 move #GetNextEvent,D0
 _GetTrapAddress
 
/* set up the JMP instruction that calls the original trap */
 lea    @origTrap,A1
 move #JMP,(A1)+
 move.l A0,(A1)

/* get some space in the system heap */
 lea    @last,A0
 lea    @first,A1
 suba.l A1,A0
 move.l A0,D0
 move.l D0,D3  /* save for _BlockMove */
 _NewPtrSYS
 cmpi #memFullErr,D0
 beq.s  @noPatch
 move.l A0,-(SP) /* save for _BlockMove */

/* set the trap address to the space we just got in the
 * system heap
 */
 move #GetNextEvent,D0
 _SetTrapAddress
 
/* now move it into place */
 lea    @first,A0
 move.l (SP)+,A1
 move.l D3,D0
 _BlockMove
 
@noPatchmove.l (SP)+,D3
 rts

/* Here’s the new _GetNextEvent.  It calls the existing
 * _GetNextEvent and then checks if a keyDown or autoKey
 * event is being reported.
 */

 /* save original return address */
@first  lea @exitAddress,A0
 move.l (SP)+,(A0)

 /* save ptr to event record */
 lea    @eventPtr,A0
 move.l (SP),(A0)

 /* return to our routine */
 pea    @tailPatch

@origTrap nop    /* JMP to original trap */
 nop
 nop
 
@tailPatchlea    @eventPtr,A0
 move.l (A0),A0

 /* Is it a keyDown or autoKey? */
 move what(A0),D0
 cmpi #keyDown,D0
 beq.s  @isKeyDown
 cmpi #autoKey,D0
 bne.s  @patchExit

@isKeyDownmove modifiers(A0),D0
 andi   #shiftKey+alphaLock+optionKey
 +cmdKey,D0
 eori   #shiftKey+alphaLock,D0
 bne.s  @patchExit

 move.l message(A0),D0
 cmpi.b #’A’,D0
 bmi.s  @patchExit
 cmpi.b #’Z’,D0
 bgt.s  @patchExit
 addi.b #’a’-’A’,D0
 move.l D0,message(A0)

 /* return to original caller via long JMP */
@patchExitnop
@exitAddressnop
 nop


@eventPtr dc.l 0 /* storage for event record ptr */

@last
 }
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Tunnelblick 3.8.2a - GUI for OpenVPN.
Tunnelblick is a free, open source graphic user interface for OpenVPN on OS X. It provides easy control of OpenVPN client and/or server connections. It comes as a ready-to-use application with all... Read more
calibre 4.17.0 - Complete e-book library...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital librarian... Read more
MacPilot 11.1.4 - $15.96
MacPilot gives you the power of UNIX and the simplicity of Macintosh, which means a phenomenal amount of untapped power in your hands! Use MacPilot to unlock over 1,200 features, and access them all... Read more
Transmission 3.00 - Popular BitTorrent c...
Transmission is a fast, easy, and free multi-platform BitTorrent client. Transmission sets initial preferences so things "just work", while advanced features like watch directories, bad peer blocking... Read more
Doom 3 1.3.1 - First-person shooter acti...
A massive demonic invasion has overwhelmed the Union Aerospace Corporation's (UAC) Mars Research Facility, leaving only chaos and horror in its wake. As one of only a few survivors, you must fight... Read more
Box Sync 4.0.8004 - Online synchronizati...
Box Sync gives you a hard-drive in the Cloud for online storage. Note: You must first sign up to use Box. What if the files you need are on your laptop -- but you're on the road with your iPhone? No... Read more
LibreOffice 6.4.4.2 - Free, open-source...
LibreOffice is an office suite (word processor, spreadsheet, presentations, drawing tool) compatible with other major office suites. The Document Foundation is coordinating development and... Read more
Day One 4.14 - Maintain a daily journal.
Day One is an easy, great-looking way to use a journal / diary / text-logging application. Day One is well designed and extremely focused to encourage you to write more through quick Menu Bar entry,... Read more
MenuMeters 2.0.7 - CPU, memory, disk, an...
MenuMeters is a set of CPU, memory, disk, and network monitoring tools for Mac OS X. Although there are numerous other programs which do the same thing, none had quite the feature set I was looking... Read more
Doxie 2.12.2 - Scan, share, and store do...
Introducing Doxie, the new, modern paper scanner that's so simple, it'll revolutionize the way you think about sharing and storing docs and photos forever. Doxie is ultra-portable, fully automatic,... Read more

Latest Forum Discussions

See All

SINoALICE, Yoko Taro and Pokelabo's...
Yoko Taro and developer Pokelabo's SINoALICE has now opened for pre-registration over on the App Store. It's already amassed 1.5 million Android pre-registrations, and it's currently slated to launch on July 1st. [Read more] | Read more »
Masketeers: Idle Has Fallen's lates...
Masketeers: Idle Has Fallen is the latest endeavour from Appxplore, the folks behind Crab War, Thor: War of Tapnarok and Light A Way. It's an idle RPG that's currently available for Android in Early Access and will head to iOS at a later date. [... | Read more »
Evil Hunter Tycoon celebrates 2 million...
Evil Hunter Tycoon has proved to be quite the hit since launching back in March, with its most recent milestone being 2 million downloads. To celebrate the achievement, developer Super Planet has released a new updated called Darkness' Front Yard... | Read more »
Peak's Edge is an intriguing roguel...
Peak's Edge is an upcoming roguelike puzzle game from developer Kenny Sun that's heading for both iOS and Android on June 4th as a free-to-play title. It will see players rolling a pyramid shape through a variety of different levels. [Read more] | Read more »
Clash Royale: The Road to Legendary Aren...
Supercell recently celebrated its 10th anniversary and their best title, Clash Royale, is as good as it's ever been. Even for lapsed players, returning to the game is as easy as can be. If you want to join us in picking the game back up, we've put... | Read more »
The Magic Gladiator class arrives in MU...
The Magic Gladiator class is now available in MU Origin 2 following the most recent patch. It also marks the start of Abyss Season 11 and the introduction of Couple Skills and Couple Dungeons. [Read more] | Read more »
The 5 Best Racing Games
With KartRider Rush+ making a splash this past week, we figured it was high time we updated our list of the best mobile racing games out there. From realistic racing sims to futuristic arcade racers (and even racing management games!), check out... | Read more »
KartRider Rush+ Guide - Tips for new rac...
KartRider Rush+ continues to be a surprisingly refreshing and fun kart racer that's entirely free-to-play. The main reason for this is just how high its skill ceiling is. Check out the video above if you're curious to know what top level play looks... | Read more »
KartRider Rush+ might be good, actually?
It's hard to find good racing games on mobile. Most of them are free-to-play, and free-to-play racers generally suck. Even Nintendo couldn't put together a competent Mario Kart game, opting instead for a weird score chaser that resembles--but feels... | Read more »
LifeAfter, NetEase's popular surviv...
A new map will be making its way into NetEase's popular survival game LifeAfter. The map is set to arrive on May 28th and will introduce a volcano that's teetering on the verge of eruption, bringing a host of added challenges to the game. [Read... | Read more »

Price Scanner via MacPrices.net

Memorial Day Weekend Sale: Take $300 off thes...
Apple resellers are offering $300 discounts on select 16″ MacBook Pros as part of their Memorial Day Weekend 2020 sales. Prices start at $2099: – 16″ 2.6GHz 6-Core Space Gray MacBook Pro: $2099 at... Read more
Best Memorial Day Weekend 2020 Apple AirPods...
Apple resellers are offering discounts ranging up to $50 off MSRP on AirPods as part of their Memorial Day Weekend 2020 sales. These are the best deals today on various AirPods models. See our... Read more
Memorial Day Weekend Sale: 10″ Apple iPads fo...
Amazon is offering new 10.2″ iPads for $80-$100 off Apple’s MSRP as part of their Memorial Day Weekend 2020 sale, with prices starting at only $249. These are the same iPads sold by Apple in their... Read more
Memorial Day Weekend Sale: 2020 Apple iPhone...
Sprint is offering Apple’s new 2020 64GB iPhone SE for $0 per month for 18 months as part of their Memorial Day Weekend 2020 sale. New line of service and trade-in required. Offer is valid from 5/22/... Read more
Amazon’s popular $100 Apple Watch Series 5 di...
Amazon has Apple Watch Series 5 GPS + Cellular models on sale for up to $100 off Apple’s MSRP today. Shipping is free. These are the same Apple Watch models sold by Apple in their retail and online... Read more
2020 13″ 4-Core MacBook Air on sale for $949,...
Apple reseller Adorama has the new 2020 13″ 1.1GHz 4-Core Space Gray MacBook Air on sale today for $949 shipped. Their price is $50 off Apple’s MSRP, and it’s the lowest price currently available for... Read more
Apple Retail Chief Announces Staggered Reopen...
NEWS: 05.20.20 – In the midst of a global pandemic, after its retail outlets were shuttered temporarily in mid-March as a mitigation measure enacted by Apple during the peak of the spread of COVID-19... Read more
Apple’s Pro Display XDR models in stock today...
Abt Electronics has Apple’s new 32″ Pro Display XDR models in stock and on sale today for up to $305 off MSRP. Shipping is free. Their prices are currently the lowest available for these new models... Read more
Apple restocks refurbished iPhone 8 for $339
Apple has restocked Apple Certified Refurbished 64GB iPhone 8 models for only $339. Apple dropped their price on this phone last month by $160, from $499 to $339. Each refurbished iPhone comes with a... Read more
New at AT&T: 50% off iPhone 11 for new cu...
AT&T is offering a 50% off the 64GB iPhone 11 for new customers who switch to AT&T and open a new line of service. Discount applied over a 30 month period The fine print: “iPhone 11 64GB for... Read more

Jobs Board

Cub Foods - *Apple* Valley - Now Hiring Par...
Cub Foods - Apple Valley - Now Hiring Part Time! United States of America, Minnesota, Apple Valley New Retail Operations Post Date 5 days ago Requisition # Read more
Senior Data Engineer - *Apple* - Theorem, L...
Job Summary Apple is seeking an experienced, detail-minded data engineeringconsultant to join our worldwide business development and strategy team. If you are Read more
Cub Foods - *Apple* Valley - Now Hiring Par...
Cub Foods - Apple Valley - Now Hiring Part Time! United States of America, Minnesota, Apple Valley New Retail Operations Post Date 4 days ago Requisition # Read more
Medical Screener - *Apple* Hill - Evenings...
Medical Screener - Apple Hill - Evenings Tracking Code D-MS-AH-E Job Description Medical Screener - Temporary We Are Hiring: WellSpan Health has a temporary Medical Read more
*Apple* Systems Administrator - Solidus Tech...
Solidus is searching for an Apple Systems Engineer. The engineer will be responsible for desktop and server infrastructure. This includes support for operating Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.