TweetFollow Us on Twitter

Dialog Quake
Volume Number:6
Issue Number:4
Column Tag:Assembly Lab

Dialog Quake For April Fools

By Mike Scanlin, Mountain View, CA

It started off much like any other day. By 11 AM it was obvious that today would be little different. Our accountant, who is annoying to begin with, was being especially antagonistic today. There I was peacefully coding away when all of a sudden about 210 little 3-hole punch droppings come floating down to land on my computer, on my desk, in my open drawers, etc. I turn around and see our accountant standing on tippy-toes peering over my five foot divider with a manly swoosh and a silly grin on his face saying “Gotcha, Prophead! Ha, ha, ha ” So I say to myself “Looks like Goobs’ immaturity is flaring up this morning. Hope it goes away by noon.” I manage to get in a couple of serene hours of blissful programming before Goobs’ maturity level nose dives to the 3rd grade for the second time that day. Just as I was typing in an especially efficient arithmetic shift right instruction about 29 triangular pieces of yellow paper sift through the air and land on their smaller round brothers. And I hear the maniacal laughter of an obviously sexually frustrated person behind me “Gotcha again, Dweeb. Heh, heh, heh ” I look up the number for Rent-An-Accountant but the line is busy. I brush the yellow triangles off my keyboard and make an optimization that has to do with the number three. Like any child not getting the attention it wants, Goobs resorts to more direct methods of assault. If you’ve ever been in the middle of a tricky piece of self-modifying, recursive, threaded code while having soccer balls and volleyballs bounced off your head then you can appreciate the kind of nuisance a bored accountant can be. I was counting the microseconds until 5 PM. It came. Goobs left. I laughed. I laughed again. Little did Goobs, the living embodiment of a feeble minded user, realize that the one thing you don’t want to do in a software company is mess with a hacker.

DIALOG QUAKE

I needed to do something to his system that would be annoying but not render it useless. I considered installing TMON on his system and turning on heap scramble but he works with multi-megabyte data files over a network and I figured that would make his system too unusable. Besides, he’s slow enough anyways that he wouldn’t realize I had done anything. It had to be more obvious. I thought about remapping his keyboard but that would definitely make it unusable. After experimenting with patches to _NewControl and _SizeControl that made all of his scroll bars 6 pixels wide I decided to go with a patch to _ModalDialog instead (there were problems with some applications in resizing their controls behind their back -- it did look cool in the Finder, though). The code that follows is an INIT written in Lightspeed C 3.0 (actually, Lightspeed Asm 3.0) that patches _ModalDialog. The effect of this patch is that all modal dialogs will shake around a bit. This patch does not make modal dialogs useless, but it does make them somewhat difficult to use (How’s your mouse coordination, Goobs?).

The _ModalDialog patch is a pre-patch and a tail patch. Once the patch has been installed, a call to _ModalDialog will temporarily patch _GetNextEvent, set up the tail patch, call the real _ModalDialog, return to the tail patch, remove the _GetNextEvent patch and exit normally (if anyone can use the word “patch” more times in a sentence, please let me know). All of the real work is done by the _GetNextEvent patch.

The new _GetNextEvent calls the existing _GetNextEvent and then does some additional work. It keeps track of a counter so that a random amount of time passes between calls to _MoveWindow (to give the movement more of a jagged, quake-like feel). The window is moved by a random amount in both directions in the range -7 to +7. There is a mildly interesting problem in generating this range of random numbers. It would be easier to generate the range -8 to +7 because that is the range that can be expressed with 4 bit signed number. However, I needed a mean of zero in my randomness so that the window wouldn’t have a tendency to shake itself off the screen. One solution would be to generate the range 0 to 14 (with a DIVU instruction) and subtract 7 but like most conscientious programmers, I stay as far away as possible from 150 cycle instructions. Here’s what I ended up doing (say there are random bits in D0):

;1

;preserve the sign bit and the low 3 bits
 andi #0x8007,D0
;if it’s positive, then we’re done (0..+7)
 bpl.s  @1
;clear the sign bit
 bclr #15,D0
;negate it (0..-7)
 neg  D0
@1

If anyone has a better way, please write in.

SATISFACTION

It was thoroughly satisfying to watch Goobs try and use his system the next day. Every time he went to print something he’d come over and whine that I had to fix his computer. I hadn’t yet admitted that I had done anything to his system and when I saw a shaking dialog on his screen I said “Looks like a virus to me. What’s it worth to you?” After his relentless whimpering I told him “You need to replace the Easy Access file in your System Folder with a newer version because the old version isn’t 32 bit clean or 100% compatible with Color Quickdraw and can cause problems with dialogs.” Wouldn’t you know it, that fixed the problem.

Hackers of the world, unite! You have nothing to loose but your sense of humor.

CREDITS

However much I would like to take all the credit for the ideas and the code presented here, I cannot. Were it not for my boss, Andy Jeffrey, none of this would have been possible. Thank you for having a sense of humor and maybe with a lot of hard work I can be just like you someday.


/* DialogQuakeINIT.c      5 June 89
 *
 * written by Mike Scanlin
 * inspiration by Andy Jeffrey
 * unwilling testing by Mr. “Goobs” Galvan
 *
 * INIT that installs a patch on _ModalDialog that
 * will, when _ModalDialog is called, install a
 * tail patch on _GetNextEvent that causes the
 * frontmost window to move around a bit (making
 * it hard to click on items with any real
 * accuracy). The patch to _GetNextEvent is
 * removed before _ModalDialog returns and the
 * patch to _ModalDialog can be removed by typing
 * cmd-option-shift-tab while a modal dialog is
 * frontmost.
 */
/* traps we patch */
#define ModalDialog0xA991
#define GetNextEvent 0xA970

/* uses a bit of self-modifying code */
#define JMP 0x4EF9

#define TAB_KEY  0x09
#define memFullErr -108

void  main(void);

void  main()
{
 asm  {
/* the next 20 or so lines are the only ones that
 * get executed during installation. They get some
 * space in the system heap for the patches and
 * then patch _ModalDialog. */
 
/* save register */
 move.l D4,-(SP)
 
/* get the old trap address */
 move #ModalDialog,D0
 _GetTrapAddress

/* set the address for the JMP instruction that
 * calls the original trap */
 lea  @origMD,A1
 move.l A0,(A1)

/* get some space in the system heap for our 
 * patches (note that this space is for both
 * patches) */
 lea  @last,A0
 lea  @modalDialogPatch,A1
 suba.l A1,A0
 move.l A0,D0
 move.l D0,D4
 _NewPtrSYS

/* if there’s not enough memory then abort installation */
 cmpi #memFullErr,D0
 beq.s  @noPatch
 
/* save address for _BlockMove */  
 move.l A0,-(SP)

/* set the trap address to the space we just got */
 move #ModalDialog,D0
 _SetTrapAddress

/* now move our patch into place */
 lea  @modalDialogPatch,A0
 move.l (SP)+,A1
 move.l D4,D0
 _BlockMove
 
@noPatch

/* restore register and exit installation code */
 move.l (SP)+,D4
 rts

/********************************************
 * Here’s the new _ModalDialog. It first installs
 * a tail patch on _GetNextEvent and then calls
 * the existing _ModalDialog. On exit this patch
 * will unpatch the _GetNextEvent patch.
 *******************************************/
@modalDialogPatch

/* save the original _GetNextEvent address */
 move #GetNextEvent,D0
 _GetTrapAddress

/* set the address for the JMP instruction that
 * calls the original trap */
 lea  @origGNE,A1
 move.l A0,(A1)

/* patch _GetNextEvent */
 lea  @getNextEventPatch,A0
 move #GetNextEvent,D0
 _SetTrapAddress

/* pop the original return address and save it */
 lea  @exitMD,A0
 move.l (SP)+,(A0)

/* set the return address to our patch */
 pea  @tailMDPatch
 
/* the nops get filled with the address of the
 * original _ModalDialog */
 dcJMP
@origMD nop
 nop

/* _ModalDialog returns here */

@tailMDPatch

/* remove the patch to _GetNextEvent */
 lea  @origGNE,A0
 move.l (A0),A0
 move #GetNextEvent,D0
 _SetTrapAddress

/* return to the place that called _ModalDialog */
 dcJMP
@exitMD nop
 nop
 
/********************************************
 * Here’s the new _GetNextEvent. If a random
 * amount of time has passed then call 
 * _MoveWindow to move the frontmost window
 * (a modal dialog) in a random direction.
 *******************************************/
@getNextEventPatch

/* pop the original return address and save it */
 lea  @exitGNE,A0
 move.l (SP)+,(A0)

/* save pointer to the event record so we can get
 * to it when the real _GetNextEvent returns */
 lea  @eventRecPtr,A0
 move.l (SP),(A0)

/* set the return address to our patch */
 pea  @tailGNEPatch
 
/* the nops get filled with the address of the
 * original _GetNextEvent */
 dcJMP
@origGNEnop
 nop

/* _GetNextEvent returns here */
@tailGNEPatch

/* save registers */
 movem.lD0-D2/A0-A2,-(SP)

/* if they don’t want us around, then exit */
 lea  @noMoreHassle,A0
 tst  (A0)
 bne  @goodBye

/* check if the event is a keyDown event */
 
 lea  @eventRecPtr,A0
 move.l (A0),A0
 move OFFSET(EventRecord,what)(A0),D0
 cmpi #keyDown,D0
 bne.s  @noKeyDown

/* it’s a keydown, but is it the special remove-us key? */
 move.l OFFSET(EventRecord,message)(A0),D0
 cmpi.b #TAB_KEY,D0
 bne.s  @noKeyDown
 move OFFSET(EventRecord,modifiers)(A0),D0
 andi #cmdKey + optionKey + shiftKey,D0
 eori #cmdKey + optionKey + shiftKey,D0
 bne.s  @noKeyDown

/* they don’t like us any more so remove ourself.
 * First beep to let them know that we got the
 * message to go away */
 move #1,-(SP)
 _SysBeep

/* set a flag so we know not to bother the nice
 * user any more */
 lea  @noMoreHassle,A0
 move #1,(A0)

/* move the frontmost window to a nice place in
 * case it was partially moved off the screen by
 * _MoveWindow */
 move.l WindowList,-(SP)
 move #30,-(SP)
 move #30,-(SP)
 bra.s  @moveToUpperLeft

@noKeyDown

/* has the timer expired? */
 lea  @timer,A0
 subi #1,(A0)
 bpl.s  @goodBye

/* reset the timer to wait a random amount of time
 * before expiring again */
 subq #2,SP
 _Random
 move (SP)+,D0

/* note: make the 0x3F smaller to move the window more often */
 andi #0x3F,D0
 lea  @timer,A0
 move D0,(A0)

/* push a WindowPtr for _MoveWindow. Note: this
 * does not check for Ghost Windows */
 move.l WindowList,A2
 move.l A2,-(SP)

/* get a couple of random numbers in the range [-7..+7] */
 subq #2,SP
 _Random
 move (SP)+,D1
 move D1,D2
 asr  #8,D1
 andi #0x8007,D1
 bpl.s  @2
 bclr #15,D1
 neg  D1
@2 ext  D2
 andi #0x8007,D2
 bpl.s  @3
 bclr #15,D2
 neg  D2
@3

/* push a random h coordinate */
 
 move OFFSET(GrafPort,portBits)+
 OFFSET(BitMap,bounds)+
 OFFSET(Rect,left)(A2),D0
 neg  D0
 add  D1,D0
 move D0,-(SP)
 
/* push a random v coordinate */
 
 move OFFSET(GrafPort,portBits)+
 OFFSET(BitMap,bounds)+
 OFFSET(Rect,top)(A2),D0
 neg  D0
 add  D2,D0
 move D0,-(SP)
 
@moveToUpperLeft
 sf-(SP)
 _MoveWindow
 
@goodBye

/* restore registers */
 movem.l(SP)+,D0-D2/A0-A2
 
/* return to _ModalDialog */
 dcJMP
@exitGNEnop
 nop

/* variables */
@noMoreHassle  dc0
@eventRecPtrdc.l 0
@timer  dc0

@last
 }
}

 
AAPL
$524.94
Apple Inc.
+5.93
MSFT
$40.01
Microsoft Corpora
-0.39
GOOG
$536.10
Google Inc.
-20.44

MacTech Search:
Community Search:

Software Updates via MacUpdate

VMware Fusion 6.0.3 - Run Windows apps a...
VMware Fusion allows you to create a Virtual Machine on your Mac and run Windows (including Windows 8.1) and Windows software on your Mac. Run your favorite Windows applications alongside Mac... Read more
Tweetbot 1.5.1 - Popular iOS twitter cli...
Tweetbot is a full-featured OS X Twitter client with a lot of personality. Whether it's the meticulously-crafted interface, sounds and animation, or features like multiple timelines and column views... Read more
Mac DVDRipper Pro 4.1.7 - Copy, backup,...
Mac DVDRipper Pro is the DVD backup solution that lets you protect your DVDs from scratches, save your batteries by reading your movies from your hard disk, manage your collection with just a few... Read more
PDFpenPro 6.2 - Advanced PDF toolkit for...
PDFpenPro allows users to edit PDF's easily. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Create... Read more
PDFpen 6.2 - Edit and annotate PDFs with...
PDFpen allows users to easily edit PDF's. Add text, images and signatures. Fill out PDF forms. Merge or split PDF documents. Reorder and delete pages. Even correct text and edit graphics! Features... Read more
Monolingual 1.5.9 - Remove unwanted OS X...
Monolingual is a program for removing unnecesary language resources from OS X, in order to reclaim several hundred megabytes of disk space. It requires a 64-bit capable Intel-based Mac and at least... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Starcraft II: Wings of Liberty 1.1.1.180...
Download the patch by launching the Starcraft II game and downloading it through the Battle.net connection within the app. Starcraft II: Wings of Liberty is a strategy game played in real-time. You... Read more
Sibelius 7.5.0 - Music notation solution...
Sibelius is the world's best-selling music notation software for Mac. It is as intuitive to use as a pen, yet so powerful that it does most things in less than the blink of an eye. The demo includes... Read more
Typinator 5.9 - Speedy and reliable text...
Typinator turbo-charges your typing productivity. Type a little. Typinator does the rest. We've all faced projects that require repetitive typing tasks. With Typinator, you can store commonly used... Read more

Latest Forum Discussions

See All

Have a Special Dead Trigger 2 Easter Bas...
Have a Special Dead Trigger 2 Easter Basket Full of Goodies, Courtesy of Madfinger Games Posted by Rob Rich on April 18th, 2014 [ permalink ] Dead Trigger 2 | Read more »
Almost All of Playdek’s Library is on Sa...
Almost All of Playdek’s Library is on Sale Right Now, and You Should Check it Out Posted by Rob Rich on April 18th, 2014 [ permalink ] Playdek has released quite a few great iOS ports of board and card games over the years, and now most of them... | Read more »
Zynga Launches Brand New Farmville Exper...
Zynga Launches Brand New Farmville Experience with Farmville 2: Country Escape Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
David. Review
David. Review By Cata Modorcea on April 18th, 2014 Our Rating: :: MINIMALISTIC IN A DIFFERENT WAYUniversal App - Designed for iPhone and iPad David is a minimalistic game wrapped inside of a soothing atmosphere in which the hero... | Read more »
Eyefi Unveils New Eyefi Cloud Service Th...
Eyefi Unveils New Eyefi Cloud Service That Allows Users to Share Media Across Personal Devices Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
Tales from the Dragon Mountain: The Lair...
Tales from the Dragon Mountain: The Lair Review By Jennifer Allen on April 18th, 2014 Our Rating: :: STEADY ADVENTURINGiPad Only App - Designed for the iPad Treading a safe path, Tales from the Dragon Mountain: The Lair is a... | Read more »
Yahoo Updates Flickr App with Advanced E...
Yahoo Updates Flickr App with Advanced Editing Features and More Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »
My Incredible Body - A Kid's App to...
My Incredible Body - A Kid's App to Learn about the Human Body 1.1.00 Device: iOS Universal Category: Education Price: $2.99, Version: 1.1.00 (iTunes) Description: Wouldn’t it be cool to look inside yourself and see what was going on... | Read more »
Trials Frontier Review
Trials Frontier Review By Carter Dotson on April 18th, 2014 Our Rating: :: A ROUGH LANDINGUniversal App - Designed for iPhone and iPad Trials Frontier finally brings the famed stunt racing franchise to mobile, but how much does its... | Read more »
Evernote Business Notebook by Moleskin I...
Evernote Business Notebook by Moleskin Introduced – Support Available in Evernote for iOS Posted by Tre Lawrence on April 18th, 2014 [ permalink ] | Read more »

Price Scanner via MacPrices.net

13-inch 2.4GHz Retina MacBook Pro available f...
Abt has the 13″ 2.4GHz 128GB Retina MacBook Pro available for $1229 including free shipping. Their price is $70 off MSRP. Read more
iMacs on sale for up to $160 off MSRP this we...
Best Buy has iMacs on sale for up to $160 off MSRP for a limited time. Choose free home shipping or free instant local store pickup (if available). Prices are valid for online orders only, in-store... Read more
iPad Airs on sale this weekend for up to $100...
Best Buy has WiFi iPad Airs on sale for $50 off MSRP and WiFi + Cellular iPad Airs on sale for $100 off MSRP on their online store for a limited time, with prices now starting at $449. Choose free... Read more
Apple restocks refurbished Mac minis starting...
The Apple Store has restocked Apple Certified Refurbished Mac minis for up to $150 off the cost of new models. Apple’s one-year warranty is included with each mini, and shipping is free: - 2.5GHz Mac... Read more
Hyundai Brings Apple CarPlay To The 2015 Sona...
Hyundai Motor America has announced it will bring Apple CarPlay functionality to the 2015 Sonata. CarPlay is pitched as a smarter, safer and easier way to use iPhone in the car and gives iPhone users... Read more
Updated iPads Coming Sooner Than We Had Thoug...
MacRumors, cites KGI securities analyst Ming Chi Kuo, well-respected as an Apple product prognisticator, saying that Apple will introduce an upgraded iPad Air and iPad mini in 2014/Q3, meaning the... Read more
Toshiba Unveils New High And Low End Laptop M...
Toshiba has announced new laptop models covering both the high-end and low-end of the notebook computer spectrum. Toshiba 4K Ultra HD Laptop Toshiba’s new Satellite P55t features one of the world’s... Read more
Save up to $270 with Apple refurbished 13-inc...
The Apple Store has Apple Certified Refurbished October 2013 13″ Retina MacBook Pros available starting at $1099, with models up to $270 off MSRP. Apple’s one-year warranty is standard, and shipping... Read more
Apple now offering refurbished iPad mini with...
The Apple Store has Certified Refurbished 2nd generation iPad minis with Retina Displays now available starting at $339. Apple’s one-year warranty is included with each model, and shipping is free.... Read more
Microsoft Blinks – Drops Microsoft Office 365...
Microsoft has dropped the annual subscription fee for Microsoft Office 365 Personal – which is needed in order to create and edit documents in Microsoft Office for iPad. However, Apple’s iOS and OS X... Read more

Jobs Board

*Apple* Automotive Parts Department position...
Apple Automotive is one of the fastest growing dealer…and it shows. Consider making the switch to the Apple Automotive Group today! At Apple Automotive, we Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Retail - Manager - Holyoke - Apple I...
Job Summary Keeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you’re a master of them all. In the store’s fast-paced, Read more
*Apple* Retail - Manager - Apple (United Sta...
Job SummaryKeeping an Apple Store thriving requires a diverse set of leadership skills, and as a Manager, you're a master of them all. In the store's fast-paced, dynamic Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.