TweetFollow Us on Twitter

Monkey CDEV
Volume Number:3
Issue Number:9
Column Tag:C Workshop

CDEV Extends Control Panel

By Jan Eugenides, Assembly Corner, Maynard, MA

The new control panel that comes with System 4.1 is extensible. You can write a code resource called a ‘cdev’ to add functions to the control panel. The process is not difficult, and it offers a number of interesting possibilities.

What I had in mind for this project was to write a fairly simple cdev, just to test the waters. Having just acquired a Macintosh II, I also wanted to explore some of the sound generation capabilities. The result is Monkey, an amusing addition to the control panel. Although this particular cdev will only work on a Macintosh II (because it calls the Mac II’s sound routines), the techniques shown here will work for more generalized cdevs as well.

The Control Panel

Figure 1 shows the new Control Panel with the General panel displayed. The panel allows choosing the Desktop pattern, setting the rate of insertion point blinking, setting the time, and more. Some other standard panels allow setting keyboard options, sound options, and display options.

By writing your own cdev, you can add a new icon to the scrollable list that appears on the left edge of the Control Panel. In Figure 1, you can see the icon for the Monkey cdev at the bottom. Clicking on an icon selects it, and the Control Panel displays various controls, depending on the item list contained in the corresponding cdev. Figure 2 shows how the Control Panel appears when Monkey is selected. Clicking on the picture of a monkey causes the Macintosh II to generate the a monkey screech sound.

How It Works

When the user opens the Control Panel, it scans the System Folder for all resource files of type ‘cdev’. When it finds one, it loads the icon and the file’s name and adds it to the icon list. After all the cdevs are loaded, the General icon is selected and the standard controls are displayed. The Control Panel then calls the cdev with various event messages, indicating which items the user has clicked or dragged, enabling the cdev to track its controls.

A cdev file must contain eight resources:

• cdev - a code resource

• mach - some data that indicates which machines the cdev is compatible with

• DITL - a standard Dialog item list

• nrct - some data that defines the number of rectangles needed for the cdev’s display, and their sizes.

• ICN# - a standard icon with mask

• BNDL - a standard bundle resource

• FREF - a standard file reference resource

• An owner resource, that must be a unique four-character identifier, just like the ones used for applications. The Monkey cdev uses the type ‘mOnk’.

With the exception of the owner resource, all the resources should have the same ID number, -4064. The owner resource ID depends on how the BNDL is structured, and is usually zero.

Fig. 1 Adding our own control functions

The nrct and mach Resources

These two resources are unique to cdevs, and require some more detailed explanation. The nrct type is a list of rectangles. The first word of the resource is the number of rectangles in the list. It is followed by eight bytes for each rectangle, which describe the top, left, bottom, and right boundaries. For each rectangle defined in the nrct, the Control Panel clears out some white space in its window, and draws a frame around it. The nrct, along with the DITL, define the look of the cdev panel.

The mach resource type consists of two words. The first is called the Softmask, and is compared to the global variable ROM85 to determine which toolbox features are available (such as Color Quickdraw). The second word is called the Hardmask, and it is used to determine which hardware features are available. The values of these masks determines on which machines the cdev will appear. Table 1 shows some possible settings.

The cdev code

Listing 1 shows the code used in the Monkey cdev, written in LightspeedC. The main function is declared as:

pascal  Handle main(message,item,numItems,CPanelID,ep,cdevStorage, CPDialog)
intmessage,item,numItems,CPanelID;
EventRecord *ep;
Handle  cdevStorage;
DialogPtr CPDialog;

Message is a number that tells the cdev what event just took place. Monkey only responds to three types of messages: initDev, which means “do your initialization”; closeDev, which means “dispose of any allocated storage and close up shop”; and hitDev, which means “the user clicked on an item”.

For initDev, Monkey just allocates 16 bytes of storage, and returns the handle in cdevStorage. Monkey does not use this storage, but the Control Panel wants to see a valid handle. For closeDev, Monkey releases the 16 bytes of storage, and clears the cdevStorage variable. For hitDev, Monkey loads the ‘snd ’ resource with ID# 4 (the monkey screech),and then calls the Macintosh II Sound Manager routine SndPlay to generate the sound. Since there is only one item, the DoHit routine does not have to determine which item was clicked on. If there were more items, the DoHit routine would have to subtract numItems from item to determine which item in its DITL was clicked. This is because the cdev’s DITL is appended to the Control Panel’s DITL.

Fig. 2 Our Monkey Squeek!

Compiling the cdev in LightspeedC

Creating the Monkey cdev is fairly straightforward in LightspeedC. First use RMaker to compile the necessary resources, as given in Listing 3. Create a new project called MonkeyCdevP, and set the project type to a code resource of type cdev. Enter Listing 2, and save it as SoundMgr.h, in the same folder with your other Macintosh header files. Add the MacTraps library to the project. Enter Listing 1, save it as MonkeyCdev.c and add it to the project. Then build the code resource (select Build Code Resource from the Project menu). Save it as Monkey. Finally, use ResEdit to set the creator type to mOnk, and set the bundle bit. Now drag the finished cdev into the System Folder, open the Control Panel, and make the monkey screech!

A Note About the Resources

I didn’t actually use RMaker to create the resources for the Monkey cdev. I created them with ResEdit, and then used a shareware resource decompiler called ResDecomp, by Robert Comer, to create the RMaker source code for publication. To save yourself some typing, you may want to create your own ICN# and PICT resources using ResEdit, rather than enter all the hex code for them. Just be sure to give them the correct ID numbers, and make them purgeable.

By the way, the monkey picture was originally taken from a clip art disk called WetPaint, put out by Dubl-Clik software. I modified it some, but I like to give credit for these things.

Table 1: Sample mach Settings

Softmask Hardmask Function

$0000 $FFFF call cdev to determine whether it should show up

$FFFF $0000 show up on all machines

$7FFF $0400 show up on machines with an Apple Desktop Bus only

$3FFF $0000 show up on the Macintosh II only

Listing 1.
/* Monkey CDEV
 * by Jan Eugenides
 * 6/6/87
 *
 * An example of adding a function to the
 * Control Panel, and using the Macintosh II’s
 * SndPlay routine
 */
#include <MacTypes.h>
#include <pascal.h>
#include <MemoryMgr.h>
#include <OSUtil.h>
#include <ToolboxUtil.h>
#include <DialogMgr.h>
#include <EventMgr.h>
#include <SoundMgr.h>
/*First define some needed constants*/
enum{
 initDev,
 hitDev,
 closeDev,
 nulDev,
 updateDev,
 deActivDev,
 keyEvtDev,
 macDev
 };
 
 Handle InitStorage();
 
 /*This is main routine, entry point for the CDEV*/
 pascal Handle main(message,item,numItems,CPanelID,ep,cdevStorage,CPDialog)
 int    message,item,numItems,CPanelID;
 EventRecord*ep;
 Handle cdevStorage;
 DialogPtrCPDialog;
 {
 if(message == macDev)return((Handle)1);
 if(cdevStorage)
 {
 switch(message)
 {
 case initDev: /*Init message received, 
 allocate some storage*/
 cdevStorage = InitStorage();
 if(cdevStorage)DoHit(1+numItems, numItems,CPDialog);
 break;
 case closeDev: /*Close message received, dispose of           
 storage*/
 DisposeStorage(cdevStorage);
 cdevStorage = (Handle)0L;
 break;
 case hitDev: /*User clicked an item, handle it*/
 DoHit(item,numItems,CPDialog);
 break;
 } /*end switch*/
 } /*end else if*/ 
return(cdevStorage);
 }
  Handle  InitStorage()  /*ultra-simple storage allocation*/
 { /*The storage is not used in this example*/
 return(NewHandle(16L));
 }
 
 DisposeStorage(h) /*Release our storage area*/
 Handle h;
 {
 DisposHandle(h);
 }
 
 DoHit(item,numItems,CPDialog)/*Handle a click on one of       
 our items*/
 int  item,numItems;   /*This example has one item, so it’s*/
 DialogPtrCPDialog;  /*very simple*/
 {
 Handle soundH;
 
soundH = GetResource(‘snd ‘,4);/*there is a space after snd */
if(soundH)
 SndPlay(0L,soundH,TRUE);   /*play the monkey screech*/
}

* Resources for Monkey cdev

MonkeyCdevP.rsrc
\00\00\00\00\00\00\00\00

Type ICN# = GNRL
 ,-4064 (32) ;; attributes -> Purgeable
.H
00000000 0000FE00 0003FF80 000F01E0
0F9C007F 1FF9C7BF 3AF7FFDA 7FEE3C6F
6AEC992E 7FCDDBB7 6ACD99B6 7FCC0037
6AC60066 3FC4FF27 3BC1FF87 1FE3D7CF
0F67FFED 00363C78 003C003D 001DFFB0
000DC3B5 000C4230 00077E75 001F00E0
003DFFD5 0078FF08 00F57555 01E2B000
03D57555 07E8F080 0DF5F555 1FFFB000
00000000 0000FE00 0003FF80 000FFFE0
0F9FFFFF 1FFFFFFF 3FFFFFFE 7FFFFFFF
7FFFFFFE 7FFFFFFF 7FFFFFFE 7FFFFFFF
7FFFFFFE 3FFFFFFF 3FFFFFFF 1FFFFFFF
0F7FFFFD 003FFFF8 003FFFFD 001FFFF0
000FFFF5 000FFFF0 0007FFF5 001FFFE0
003FFFD5 007FFF08 00FFF555 01FFF000
03FFF555 07FFF080 0FFFF555 1FFFB000

Type FREF
 ,-4064 (32) ;; attributes -> Purgeable
cdev 0 

Type BNDL
 ,-4064 (32) ;; attributes -> Purgeable
mOnk 0
ICN#
 0 -4064 
FREF
 0 -4064 

Type DITL
 ,-4064 (32) ;; attributes -> Purgeable
2

PicItem
1 128 76 194
-4064

StatText Disabled
77 133 93 206
Click Me!

Type PICT = GNRL
 ,-4064 (32) ;; attributes -> Purgeable
.H
03850000 0000004B 00421101 01000A00
00000000 4B004298 000A0031 0066007C
00A80031 0066007C 00A80000 0000004B
00420000 08FC0002 01FFF0FF 0008FC00
0203FFFC FF0008FC 000206AA AAFF0008
FC00020F F7F7FF00 08FC0002 0AAAABFF
0008FC00 020FFFFF FF0008FC 00040BFF
AA800008 FC000406 00FF8000 06FA0002
2A800006 FA00023F 800006FA 00021AC0
000A0200 00FEFD00 0217C000 0B030003
FF80FE00 021AC000 0B03000F 01E0FE00
021FC000 0B090F9C 007FC000 001AC000
0B091FF9 C7BFF000 001F4000 0B093AF7
FFDAB800 001AC000 0B097FEE 3C6FFC00
001FC000 0B096AEC 992EAC00 001AC000
0B097FCD DBB7FC00 0017C000 0B096ACD
99B6AC00 001AC000 0B097FCC 0037FC00
001FC000 0B096AC6 0066B800 001AC000
0B093FC4 FF27F800 001F4000 0B093BC1
FF87FFC0 001AC000 0B091FE3 D7CFFFF8
001FC000 0B090F67 FFED557F 001AC000
0B090036 3C78080F C0178000 0B09003C
003D5555 F01A8000 0B09001D FFB00000
7C3F8000 0B09000D C3B55555 5E2A8000
0B09000C 42308080 87BF8000 0B030007
7E75FE55 02EA8000 0B03001F 00E0FE00
02FF8000 0B03003D FFD5FE55 027A8000
0A020078 FFFD0802 3F80000A 0200F575
FD55025A 80000A02 01E2B0FD 00021F80
000A0203 D575FD55 025E8000 0A0207E8
F0FD8002 8F80000A 020DF5F5 FD55025F
00000A02 1FFFB0FD 00020700 000A023A
AF35FD55 02570000 0A027FFE 78FD0802
0F00000A 02777C75 FD550257 00000A02
FFF860FD 00020300 000A02EA B075FD55
02570000 0B09FFE0 6080FE80 80830000
0B09DDC0 7557FFD5 55570000 0B09FF80
C0073BC0 00030000 0B09EF00 D55E35F5
55570000 0B097E00 C81C38F8 080B0000
0B073800 D558357D 5557FF00 0B070000
C03032BC 0003FF00 0B070001 D570755D
5557FF00 0B070001 80E0688E 8083FF00
0B070001 D560755F 5557FF00 0B070003
80C06A3B 0003FF00 0B070003 D5C0D55B
5557FF00 0B070003 0980C8B1 880BFF00
0B070007 5580D571 D557FF00 0B070006
0300E2E0 C003FF00 0B070007 5700D560
D557FF00 0B07000E 860188C0 6083FF00
0B07000F 5601D5C0 7556FF00 0B07001C
0C01AB80 3006FF00 0B07001F FC01FF80
1FFEFF00 0B07003F F803FF00 1FFEFF00
0B090077 78037700 0F760000 0B0901FF
F807FF00 0FFE0000 0B097FEA B9FEAB0F
FEAE0000 0B09FFFF FBFFFF1F FFFE0000
0B09DDDD FBDDDF1D DDDE0000 0B09FFFF
F3FFFF1F FFFE0000 0B097FFF E1FFFE0F
FFFC0000 FF

Type mOnk = GNRL
 ,-4064 (32) ;; attributes -> Purgeable
.H
1C4D6F6E 6B657920 63646576 20627920
4A616E20 45756765 6E696465 73

Type nrct = GNRL
 ,-4064
.H
0001FFFF 00570066 00CE

Type mach = GNRL
 ,-4064 (32) ;; attributes -> Purgeable
.H
3FFF0000 
 
AAPL
$116.31
Apple Inc.
+1.64
MSFT
$48.70
Microsoft Corpora
+0.48
GOOG
$534.83
Google Inc.
-2.16

MacTech Search:
Community Search:

Software Updates via MacUpdate

Herald 5.0.1 - Notification plugin for M...
Note: Versions 2.1.3 (for OS X 10.7), 3.0.6 (for OS X 10.8), and 4.0.8 (for OS X 10.9) are no longer supported by the developer. Herald is a notification plugin for Mail.app, Apple's Mac OS X email... Read more
Firetask 3.7 - Innovative task managemen...
Firetask uniquely combines the advantages of classical priority-and-due-date-based task management with GTD. Stay focused and on top of your commitments - Firetask's "Today" view shows all relevant... Read more
TechTool Pro 7.0.6 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
PhotoDesk 3.0.1 - Instagram client for p...
PhotoDesk lets you view, like, comment, and download Instagram pictures/videos! (NO Uploads! / Image Posting! Instagram forbids that! AND you *need* an *existing* Instagram account). But you can do... Read more
SuperDuper! 2.7.3 - Advanced disk clonin...
SuperDuper! is an advanced, yet easy to use disk copying program. It can, of course, make a straight copy, or "clone" -- useful when you want to move all your data from one machine to another, or do... Read more
MacJournal 6.1.5 - Create, maintain, and...
MacJournal is the world's most popular journaling software for the Mac. MacJournal 6 adds a calendar mode that show entries from any journal, geolocation, word count, and progress tracking, as well... Read more
Skim 1.4.10 - PDF Reader and note-taker...
Skim is a PDF reader and note-taker for OS X. It is designed to help you read and annotate scientific papers in PDF, but is also great for viewing any PDF file. Skim includes many features and has a... Read more
FontExplorer X Pro 4.2.2 - Font manageme...
FontExplorer X Pro is optimized for professional use; it's the solution that gives you the power you need to manage all your fonts. Now you can more easily manage, activate and organize your... Read more
SoftRAID 5.0.5 - High-quality RAID manag...
SoftRAID allows you to create and manage disk arrays to increase performance and reliability. SoftRAID's intuitive interface and powerful feature set makes this utility a must have for any Mac OS X... Read more
DEVONthink Pro 2.8.2 - Knowledge base, i...
Save 10% with our exclusive coupon code: MACUPDATE10 DEVONthink Pro is your essential assistant for today's world, where almost everything is digital. From shopping receipts to important research... Read more

Latest Forum Discussions

See All

Sago Mini Road Trip (Education)
Sago Mini Road Trip 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: Go for a fun-filled drive with Jinja the cat. Pick a destination, select a vehicle and hit the road. What will Jinja... | Read more »
New Tower Defense Game, Kingdom Rush: Or...
New Tower Defense Game, Kingdom Rush: Origins, is Available Today Posted by Jessica Fisher on November 20th, 2014 [ permalink ] iPad Only App - Designed for the iPad | Read more »
Sunburn! (Games)
Sunburn! 1.0.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.0 (iTunes) Description: Your ship is gone. Your crew is scattered. One option remains. Gather your crew... and jump into the sun. Reunite your... | Read more »
Tapventures Review
Tapventures Review By Jennifer Allen on November 20th, 2014 Our Rating: :: ODDLY COMPELLINGUniversal App - Designed for iPhone and iPad Tapventures is an increasingly hands-off one-tap RPG, but expect it to hook you despite your... | Read more »
Who Wore it Best? The Hunger Games: Girl...
With The Hunger Games: Mockingjay Part 1 out this weekend, Who Wore it Best? pits two Hunger Games tie-ins, Girl on Fire and Panem Run, against each other in a brutal and pointless fight to the death. I wonder where we got that idea from? | Read more »
Ironkill Review
Ironkill Review By Jennifer Allen on November 20th, 2014 Our Rating: :: LACKLUSTER PUNCHINGUniversal App - Designed for iPhone and iPad Ironkill is a freemium focused fighting game that doesn’t offer particularly thrilling fights... | Read more »
Real-Time Multiplayer Match-3 RPG Crusad...
Real-Time Multiplayer Match-3 RPG Crusaders Quest Set to Launch Next Month Posted by Ellis Spice on November 20th, 2014 [ permalink ] | Read more »
Checkpoint Champion Review
Checkpoint Champion Review By Jennifer Allen on November 20th, 2014 Our Rating: :: SPEEDY DRIFTINGUniversal App - Designed for iPhone and iPad Checkpoint Champion is a drift-focused racing game that’s ideal for short but fun gaming... | Read more »
MediaFire iOS 8 Native Update Brings New...
MediaFire iOS 8 Native Update Brings New “Power Upload” Feature to iPad and iPhone Posted by Jessica Fisher on November 20th, 2014 [ | Read more »
Mark of the Dragon Review
Mark of the Dragon Review By Nadia Oxford on November 20th, 2014 Our Rating: :: TRAIN YOUR DRAGON FOR WARUniversal App - Designed for iPhone and iPad Mark of the Dragon is a bit rough and scaly, but flying into battle on the backs... | Read more »

Price Scanner via MacPrices.net

64GB iPod touch on sale for $249, save $50
Best Buy has the 64GB iPod touch on sale for $249 on their online store for a limited time. Their price is $50 off MSRP. Choose free shipping or free local store pickup (if available). Sale price for... Read more
15″ 2.2GHz Retina MacBook Pro on sale for $17...
 B&H Photo has the 2014 15″ 2.2GHz Retina MacBook Pro on sale for $1799.99 for a limited time. Shipping is free, and B&H charges NY sales tax only. B&H will also include free copies of... Read more
New Logitech AnyAngle Case/Stand Brings Flexi...
Logitec has announced the newest addition to its suite of tablet products — the Logitech AnyAngle. A protective case with an any-angle stand for iPad Air 2 and all iPad mini models, AnyAngle is the... Read more
2013 15-inch 2.0GHz Retina MacBook Pro availa...
B&H Photo has leftover previous-generation 15″ 2.0GHz Retina MacBook Pros available for $1499 including free shipping plus NY sales tax only. Their price is $500 off original MSRP. B&H will... Read more
16GB Retina iPad mini on sale today for $199,...
 Staples has 2nd generation 16GB Retina iPad minis on sale for $199 on their online store for a limited time. Their price is $100 off MSRP. Choose free shipping or free local store pickup (if... Read more
Developers Start Designing Apps for Apple Wat...
Apple has announced the availability of WatchKit, software that gives developers a set of tools to easily create experiences designed specifically for Apple Watch. Apple’s developer community can now... Read more
C Spire Launches iPad Air 2 and iPad Mini 3 o...
C Spire has announced that iPad Air 2 with Wi-Fi + Cellular and iPad mini 3 with Wi-Fi + Cellular are now available on its 4G LTE network. C Spire offers both new iPads with a range of data plans... Read more
Are You On Your Last PC? – The ‘Book Mystique
Will your current PC be your last? Quite possibly so if you define “personal computer” as a traditional desktop or laptop form factor machine according to some commentators. So then, the upshot that... Read more
Save up to $180 on MacBook Airs with Apple re...
The Apple Store has Apple Certified Refurbished 2014 MacBook Airs available for up to $180 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is free.... Read more
16GB iPad mini available for $219, save $30
Walmart has 16GB iPad minis (1st generation) available for $219 on their online store. Their price is $30 off MSRP. Choose free shipping or free store pickup (if available). Price for online orders... Read more

Jobs Board

*Apple* Store Leader Program - College Gradu...
Job Description: Job Summary As an Apple Store Leader Program agent, you can continue your education as you major in the art of leadership at the Apple Store. You'll Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.