TweetFollow Us on Twitter

Button DA
Volume Number:2
Issue Number:5
Column Tag:Assembly Language Lab

Panic Button DA Shows Screen Blanking

By Ed Ludwig, Garfield, NJ

Panic Button DA

It is very difficult these days to find a computing magazine that isn't 98% fluff (and meaningless advertisments) and 2% useful information. MacTutor is the only magazine that I actually look forward to reading each month from cover to cover!

Here is a little gem, just to show my appreciation for all the hard work the MacTutor staff puts in each month, and one I hope proves useful to our readers as well. Here is a desk accessory (very tiny!) that I wrote using the MDS Assembler. It's function is very simple; it's a Panic Button! There are probably others that are more sophisticated than this one, but sometimes less is more.

Fig. 1 The Panic Button Exposed!

When you invoke it, you are given a tiny dialog box which you can move and hide in a corner of the screen. When you are in the middle of your favorite program (doodling in MacPaint for example...) and your boss comes along, what do you do? HIT THE PANIC BOTTON! When you hit the button, the whole screen goes dark as if the Mac is turned off and you are save! After the boss goes away, click the mouse, and everything is just as you left it.

There are only two things to remember: First, move the mouse away from the spot where you pressed the panic button when you click to restore the screen, because if you click inside the button again, the whole screen will flash dark again. Second, the program grabs about 22K of storage to save away the screen buffer while the screen is dark (and then restores it again) so it may not work with large programs that don't leave 22K of memory in the Mac to spare; however, a simple Alert box will be triggered with the message "Not Enough Memory' to inform you of that fact if a memory problem develops.

Program Details

Desk accessories were covered in detail last month in both C and assembly, but this little DA is very nicely done and easy to follow. In fact, it will serve as a better introduction than the more complicated DA published last month if you are new to DA programming. As with all DA's, we begin with a table of word length constants that tell the desk manager where the five entry points of the DA are. These five entry points are standard for any type driver application, which is what a desk accessory is. The entry points are for opening the DA, closing the DA and control of the DA. The other two entry points are normally associated with device drivers that require a status check or a prime routine for set-up. Immediately after this table of entry points is the string title of our desk accessory.

The three routines for open, close and control then follow. In general, the desk manager will pass control to either one of these three routines from the currently running application depending on circumstances. When the user first selects the DA from the Apple menu, the open entry will be invoked. When a control in the DA's window is pressed, the control entry will be invoked. When the user no longer requires the DA and presses the close box, then the close entry will be invoked. We must be prepared for the DA to begin execution at any one of these three entry points.

Open and Close

When we open or close the DA, we first save the application's graph port. For some reason, the desk manager does not do this for us. Care must be taken to save all the registers and especially to protect the contents of A0 and A1, which contain information vital to the DA. It is very easy to forget and make a trap call and find out the DCE pointer is messed up because the A1 register was not protected from alteration. When the DA is called, register A0 contains a pointer to the IO Parameter Block and A1 contains the pointer to the Device Control Entry Record. These two structures contain all the information the DA needs to find out what to do. In the IO Parameter block is a field called csCode which we use to branch on to handle the various events passed to the DA. This event processing is done in the Control entry of our DA. In the Open and Close entry portions, we make use of the Device Control Entry Record to set up any global storage space required by the DA. The field dCtlStorage is used to hold a handle to any global variable space required by the DA. Both the DCE and the IO Parameter block were shown in detail in last months's issue of MacTutor, and are documented in Inside Macintosh.

Resource ID

One thing we need to do when we open the DA is to establish the base ID for our resources. In the top of our DA source code, we tell the linker that the code that follows is a resource of type DRVR and we give it a resource Id number as shown:

RESOURCE 'DRVR' 25 'Panic Button'

In fact, this entire source file is all resources! After the code resource, we have our 'traditional' resources. Anyway, the number 25 indicates the Id number for this DRVR resource, and hence for our DA. All other resources must be a function of this number, because the Font/DA Mover will change the DA number when it inserts this DA into the system file so that no two DA's have the same Id. The formula for calculating the RMaker resource ID number from this DA reference number is as follows:

RMaker ID = (DArefNum X 32) + Index - 16384

The index is used for owned resources such as Alert items, where the resource "owns" other resources. Otherwise, it is normally zero. Since our DA reference number is 25, we calculate:

RMaker ID = 25 X 32 +0 -16384 = -15584

Hence this is the number we would use for our resource ID's if we were using the RMaker file to compile our resources. It turns out that RMaker has a problem opening a ".REL" file created by the Consulair C compiler for a DRVR Proc type resource. As a result of this bug, I was unable to use RMaker to complete the DA so I have translated all the RMaker resources into assembly language and have used the Consulair linker to create the DA. If your using the MDS assembler, simply assemble and link this file with the MDS linker. I have to use the Consulair compiler for my MDS assembler because I have a Mac Plus and Apple hasen't finished fixing the MDS system to run properly under HFS. Actually, the Consulair Compiler makes a great MDS alternative. But when the 16 bit value -15584 is used for the resource ID, the assembler converts this to a 32 bit value of FFFF C320 which does not work for a resource ID. So what we do is take -15584, convert it to hex with a hex calculator (included on the source code disk for this issue) to FFFF C320, take the last word, C320 and convert that back to decimal, which gives us 49952. This is the decimal value we use for our resource ID numbers when coding resources in assembly language.

The last thing we do in our open routine is to set up a dialog type window and to save the window pointer back in our DCE record and mark that window as a system window belonging to a DA by updating the windowkind field in our newly created window record. We then restore the users graph port and exit, clearing D0 to show the DA executed properly.

For the close routine, we do much the same thing only in reverse. This time we dispose of our window and release any global storage we might have referenced to dctlstorage field of our DCE record. In this DA, we don't use any storage except briefly during our control loop when we blank the screen.

The Control Loop

That brings us to the control loop, where the events processed by this DA are handled. The IO Parameter block entry cscode tells us which event was passed to us from the desk manager. In our case, the only event we will see is a dialog event when the user presses the panic button. The csparam field of the IO Parameter block contains the pointer to our event record, so we use that along with our window pointer to call _dialogselect, which returns the itemhit with the control item that caused the event. If we didn't get an event, then we take the control exit through JIODone and return to the current application.

If we did get an event, then that means the user pressed the panic button and our job is to clear the screen. Now the control entry code does it's work. We first get a handle to 22K of storage space so we can copy the current screen contents into a safe area on the heap. If this request becomes a problem for the Mac, then we check the error code returned in D0 to see if it's a memory full error, -108. If so, then we forget the screen blanking and exit because there is not enough memory available to lock up the necessary storage to blank the screen. However if it's some other type of error, the routine is executed with the assumption that the new handle was returned. This might cause a problem but I can't think under what circumstances it might happen. If the error is a memory full, we display an alert message using our alert resource, and then dispose our window and exit through JIODone. Since our simple DA doesn't need any attention, we return no error code and it's just as if we were never called.

Assuming no error was returned, we can then blank the screen. We do this by locking our handle after saving it in the dctlstorage field of the DCE record and then calling our blank screen subroutine. When we return, we unlock our handle and get rid of the storage space and exit.

Screen Save

That leaves only the screen save and get id base subroutines to discuss. The screen save subroutine moves each byte of the screen buffer to our 22K storage space on the heap so it can be restored later, and fills the screen with $FFFF for black. A pointer to the base of the screen buffer area is contained in the system global location $824. We load the screen buffer pointer into A3 and the deferenced handle of our screen storage area into A2 and do an indirect move to move each word from the screen to the storage. Our loop counter is 10,943 words of memory or 21,886 bytes of memory. (Note that in the old 128K Macs, after the screen buffer, you have just over 100K, and with another 20K for various system things like heaps and stacks and such, the poor machine had only 80K or so for programs. Since most Mac programs take at least 60K, it's no wonder the skinny mac died of memory starvation.)

After setting the screen black, we loop on the buttonstate until it changes and we can then restore the screen and exit. The button state is stored in a system global at $172 and will be set when a button down event is registered by the system.

Our resources provide us with a dialog window containing a single control item, a button. The DLOG and first DITL resources specify our window and panic button. The next resource is an alert: a sorry button control and a string telling the user there is not enough memory. This alert is invoked when we detect a memory full error when we try to get a handle to 22K of heap space for saving the screen buffer. Note that the alert resource "owns it's DITL list and so the resource ID of it's DITL list is incremented accordingly.

That completes our simple, but fun little DA. Enjoy.

Assembly Language Source Code

;Panic Button DA
; By Ed Ludwig for MacTutor

RESOURCE 'DRVR' 25 'Panic Button'

;includes (probably not all are neccesary) 

Include MacTraps.d ; sys and toolbox traps
Include ToolEqu.d; Use toolbox equates
Include QuickEqu.d ; Use quickdraw equates
Include SysEqu.d ; Use system equates
include FSEqu.d  ;file system equates

;my equates

screenbaseequ  $824;pointer
buttonstate equ  $172;byte

; first comes the device control entry table

accentry:

 dc.w   $0400  ;control events only 
 dc.w   0 ;set only if acc. needs time
 dc.w   $0142  ;event mask
 dc.w   0 ;menu id (none at the moment)
 
 ;offsets into routines used by desk manager
 
 dc.w   accopen-accentry  ;open routine
 dc.w   accdone-accentry  ;prime (unused)
 dc.w   accctl-accentry ;control routine
 dc.w   accdone-accentry  ;status (unused)
 dc.w   accclose-accentry ;close routine
 
acctitle: ;name of accessory
 dc.b   @2-@1
@1 dc.b 'panic button - by Ed Ludwig'
@2
.align  2
 
;------------------------------open------------------------------------

;save application port and allocate window
 
accopen:
 movem.la0-a4/d0-d4,-(sp) ;save registers
 move.l a1,a4    ;move dce to safe place
 
 subq.l #4,sp    ;make room for the port
 move.l sp,-(sp) ;push a pointer to it
 _getport ;get it, now  on stack
 
 tst.l  dctlwindow(a4)  ;already have a window?    bne   standardreturn
 ;yes, non-zero so exit
 ;no, make new window
 bsr getidbase   ;get the base id number
 
 clr.l  -(sp)    ;space for window ptr
 move.w d0,-(sp) ;push basenum
 clr.l  -(sp)    ;nil for dstorage
 moveq  #-1,d0   ;get -1 into d0
 move.l d0,-(sp) ;push behind = -1
 _getnewdialog   ;display
 move.l (sp)+,a0 ;pop dialog pointer 
 
 move.l a0,dctlwindow(a4) ;save window pointer in dce          
 ;mark as system window
 move.w dctlrefnum(a4),windowkind(a0)

standardreturn:
 ;graph port on stack
 _setport ;make window current portmovem.l   (sp)+,a0-a4/d0-d4 ;restore 
registers
accdone:
 moveq  #0,d0    ;return no error
 rts
 
;-------------------------------close----------------------------------

accclose:

 movem.la0-a4/d0-d4,-(sp) ;save registers
 move.l a1,a4    ;move dce to safe place
 
 subq.l #4,sp    ;make room for the port
 move.l sp,-(sp) ;push a pointer to it
 _getport ;get it, now its on the stack
 
 tst.l  dctlwindow(a4)  ;do we have a window?
 beq  @1;no, go past disposal
 ;yes, prepare to trash it
 move.l dctlwindow(a4),-(sp);dispose window
 clr.l  dctlwindow(a4)  ;clear entry in dce
 _disposdialog   ;goodby window
@1 ;note port still on stack
 _setport ;make window current port
 movem.l(sp)+,a0-a4/d0-d4 ;restore registers
 moveq  #0,d0    ;return no error
 rts
;-------------------------------control------------------------------------

accctl:
 movem.la0-a4/d0-d4,-(sp) ;save registers
 move.l a1,a4    ;move dce to safe place
 moveq  #0,d0    ;clear d0

 move.w cscode(a0),d0;get control code
 cmpi.w #accevent,d0 ;is it an event?
 bne  done;no, all done
 ;yes, do event
 clr.w  -(sp)    ;clear result
 move.l csparam(a0),-(sp)   ;push ptr to event record
 pea  dctlwindow(a4) ;push address of dialog ptr
 pea  itemhit    ;push address of itemhit
 _dialogselect
 move.w (sp)+,d0 ;clear boolean 
 bne  whichevent ;yes handle it
done:
 movem.l(sp)+,a0-a4/d0-d4 ;restore registers
 moveq  #0,d0    ;return no error
 move.l jiodone,-(sp);jump to iodone
 rts
 
whichevent:
 move.w itemhit,d0
 subq.w #1,d0    ;was an item hit?
 bne  done;no, all done
 ;yes, so go to it!
 move.l #21888,d0;# of bytes on mac screen
 _newhandle ;get handle to 22K of store! 
 beq  @7;everything okay so branch
 cmpi.w #-108,d0 ;no, mem full error?
 bne  @7;no, go on to branch
 ;(could this happen?)
 
@6 ;yes, display error alert
 bsr getidbase   ;get base id num  in d0
 clr.w  -(sp)    ;save space
 move.w d0,-(sp) ;push basenum
 clr.l  -(sp)    ;nil for filter proc.
 _alert ;display alert
 move.w (sp)+,d0 ;pop result
 ;prepare to exit
 move.l dctlwindow(a4),-(sp)
 clr.l  dctlwindow(a4)  ;clear entry in dce
 _disposdialog   ;dump window
 bra  done;exit
 
@7 ;got handle ok
 move.l a0,dctlstorage(a4);save handle in dce
 move.l a0,a2    ;set up screen save
 _hlock ;lock handle
 bsr  screensave ;do the save and restore
 move.l dctlstorage(a4),a0
 _hunlock ;unlock storage
 move.l dctlstorage(a4),a0
 _disposhandle   ;dispose storage  (22k!)
 clr.l  dctlstorage(a4) ;clear storage bra   done        ;exit 
;------------------  SUBROUTINES  ----------------------------

screensave: ;clear and save screen
 ;enters with handle to storage
 _hidecursor;hide for proper restore
 move.l (a2),a2  ;derefence handle
 move.l #10943,d1;set loop index
 moveq  #0,d0    ;set screen index
 move.l screenbase,a3;base address of screen
@1 ;move word from screen to storage
 move.w 0(a3,d0.l),0(a2,d0.l) 
 move.w #$ffff,0(a3,d0.l) ;black out screen word
 addq.l #2,d0    ;inc screen index
 dbf  d1,@1 ;dec loop index
 moveq  #0,d0
@2
 move.b buttonstate,d0  ;user press button?
 bne  @2;no, wait until it is ;yes, restore screen
 move.l #10943,d1;set loop index moveq #0,d0       ;set screen index
@3 ;move word from storage to screen
 move.l 0(a2,d0.l),0(a3,d0.l) 
 addq.l #2,d0    ;inc screen index
 dbf  d1,@3 ;dec loop index
 _showcursor;restore cursor
 rts    ;return
 
getidbase:;exits with base id number in d0
 moveq  #0,d0    ;clear d0
 move.w dctlrefnum(a4),d0 ;get reference number
 addq.w #1,d0    ;and calc base number
 muls #-1,d0;for resource id 
 mulu #32,d0
 add.w  #$c000,d0
 rts
 
;--------------------------------data----------------------------------

itemhit dc.w0

;---------------------------- Resources ----------------------------

; resource ID numbers must be negative functions of 
; DA ref. number. The formula is: RMaker ID =
; (DArefNum X 32) + (index of owned resources) - 16384
; where index of owned resources is 0 on up. If our DA were
; 12 then 12 X 32 + 0 -16384 = -16000 which is the RMaker ID.
; But the asm converts -16000 to a 32 bit number FFFF C180
; which doesn't work. So, convert -16000 to hex and use the
; first word: -16000 = FFFF C180, so convert C180 to Decimal
; to get 49536, a 16 bit number, which is the assember 
; ID number for DA 12.
 
RESOURCE 'DLOG' 49952 'title'
 DC.W 295 ;TOP
 DC.W 399 ;LEFT
 DC.W 325 ;BOTTOM
 DC.W 492 ;RIGHT
 DC.W 4 ;window type
 DC.B 1 ;visible (T=1/F=0)
 DC.B 0 ;ignored (part of visible)
 DC.B 1 ;goaway box (1=yes, 0=none)
 DC.B 0 ;ignored (part of goaway)
 DC.L 0 ;refcon
 DC.W 49952 ;id of DITL list
 DC.B @2-@1 ;length byte
@1 DC.B 'Don',39,'t' ;window title
@2
.align 2
RESOURCE 'DITL' 49952 'Button'
 DC.W 0 ;number of items -1
 
 DC.L 0 ;handle holder
 DC.W 5 ;top
 DC.W 16;left
 DC.W 25;bottom
 DC.W 77;right
 DC.B 4+0 ;item type (cntrl button)
 DC.B @4-@3 ;item length (even)
@3 DC.B 'Panic!' ;item itself
@4
.ALIGN 2
RESOURCE 'ALRT' 49952 'error'
 DC.W 254 ;top
 DC.W 334 ;left
 DC.W 320 ;bottom
 DC.W 491 ;right
 DC.W $C321 ;resource ID of DITL list (49953)
 DC.W $4444 ;stages
.ALIGN 2
RESOURCE 'DITL' 49953 'msg'
 DC.W 1 ;number of items -1
 
 DC.L 0 ;handle holder
 DC.W 33;top
 DC.W 80;left
 DC.W 55;bottom
 DC.W 137 ;right
 DC.B 4+0 ;item type (cntrl button)
 DC.B @6-@5 ;item length (even)
@5 DC.B 'Sorry!' ;item itself
@6
 DC.L 0 ;handle holder
 DC.W 3 ;top
 DC.W 5 ;left
 DC.W 22;bottom
 DC.W 145 ;right
 DC.B 128+8 ;item type (disabled static text)
 DC.B @8-@7 ;item length (even)
@7 DC.B 'Not enough memory.';item itself
@8
END
; Link file for panic da

]
/Resources
 panic
/output  panic button
/Type 'DFIL' 'DMOV'
$
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
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
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer 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 MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more

Jobs Board

*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.