oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

OS X Brings Unix Stability to the Mac
Pages: 1, 2

Carbon development

The classic Apple event handler was based on applications cooperatively using the processor time. The new event-handling scheme, Carbon, is the adopted method for handling preemptive multitasking applications. To develop applications for Mac OS X, we'll require a few tools. Apple has provided an SDK that's capable of running on both Mac OS 9 and developer release Mac OS X platforms.

Legacy PowerPC owners should be aware that only true G3 systems and newer are capable of running Mac OS X.

Legacy PowerPC owners should be aware that only true G3 systems and newer are capable of running Mac OS X. If you're using a PowerPC, this shouldn't hinder you from developing applications with Carbon. I used a Macintosh PowerPC 7500 with a G3 ZIF XLR8 carrier card for playing around with the Carbon SDK. That being said, I eventually migrated over to a G4 so I could load a Mac OS X developer release image.

The latest version of the Carbon SDK is available for free from Apple Developer Connection. You are required to become a member to download the kit, but membership is free. Since Mac OS X utilizes FreeBSD, you can be assured too that a GNU development system will surface either from Apple or from an open-source developer.

In addition to the Carbon SDK, you'll need a supported development environment as well. Apple supports the Metrowerks Code Warrior Release 4 development system. The libraries in the Carbon SDK were built using this compiler. If Code Warrior is out of your price range, Apple still has its MPW environment available for free. MPW requires some work to get up and running, but it is usable.

Classic events vs. Carbon events

I'm probably dating myself here, but I needed to refer to a heavily dog-eared book by Scott Knaster, Macintosh Programming Secrets, Second Edition. This has been my long-standing reference on Macintosh programming. To quote from my Holy doctrine on Mac programming, page 83: "You probably know that almost every Macintosh application has a routine called MainEventLoop, and it's usually the heart of the program." A classic Apple event loop looks like this:

void main (void)
OSErr err;
void MyEventLoop(void)
Boolean gotEvent;
EventRecord event;
gotEvent = WaitNextEvent(everyEvent,&event,0,nil);
if (gotEvent)

Basically, the event loop executes WaitNextEvent continuously and retrieves events as they occur. The Boolean variable bQuitApp is set when the user selects to quit the application from a pull-down menu or uses the Apple-Q command. This is the classic way of handling events. This will run under the Carbon environment, but remember, it's not optimized for Carbon event handling. A Carbon application looks like this:

#include <CarbonEvents.h>
void main(void)
WindowRef window;
OSStatus err;
EventHandlerRef ref;
EventTypeSpec list[] =
{ {kEventClassWindow, kEventWindowClose },
{ kEventClassWindow, kEventWindowDrawContent }

The function RunApplicationEventLoop is part of the Carbon API. We don't write our own event loop as we did with the classic event handling. The header file CarbonEvents.h defines several event classes, such as kEventClassWindow, along with event types for each class. KEventWindowClose is an event type for class kEventClassWindow. The events we are going to handle will be called out in an EventTypeSpec list. The list contains only Carbon events defined in the header file. The classic Apple events are handled in a different manner.

The following code snippet demonstrates how to handle an Apple event, such as waiting for an event from the Sound Manager:

EventTypeSpec eventList[] = { { kEventClassCommand,
kEventProcessCommand } };

InstallEventHandler( GetApplicationEventTarget(),
NewEventHandlerUPP( MySndEventHandler ),

We need to instruct Carbon to pass a high-level event such as an Apple event to our own event handler routine. If you're familiar with classic toolbox coding, you'll notice that I used the call NewEventHandlerUPP instead of NewRoutineDescriptor. A UPP is a Universal Procedure Pointer, and these do behave differently under Mac OS 9 than they do with Mac OS X. You should consult the Carbon Porting Guide for further details. An excellent and simple example of Carbon event handling is provided in the Sample Code folder of the SDK, BasicCarbEvents. This example demonstrates how to open a simple window using the Carbon event handlers.

Better late than never

For years I put off purchasing a new Mac as I waited for a better operating system. Had Sun Microsystems purchased Apple a few years back, I might have bought a new Macintosh then, anticipating some flavor of Unix running on that wonderful hardware.

Personally, I think the outdated OS has overshadowed Apple's otherwise sterling achievements. For example, when the G4 was introduced, I was salivating at the opportunity to own a 450 MHz RISC system with a 128-bit processor instruction bus. Apple's propaganda was enticing, too -- own your own supercomputer.

But the issue of an outdated OS once again reared its ugly head. Watching Apple's highly sophisticated architecture getting burned by CISC-based Pentiums in 3D rendering seemed shameful. The G4, hardware-wise, is more than capable of besting an Intel PC for tasks like these. Once again, the hindrance is the current "classic" operating system.

With a FreeBSD architecture native to Mac OS X, it's likely that the tables will turn in these 3D computation benchmarks, as well as with other tasks. Just imagine the new hot software you can write for OS X. But what's even more mind-boggling is that an operating system developed back in 1976 (Unix) is probably what's going to restore the reign of Apple Computer. Had Apple started with Unix when it shipped the original Macs, it's possible the software icon, Microsoft, may have never become a household name.

Michael J. Norton is a software engineer at Cisco Systems.

Related Articles

Writing a Speech Recognition App. in Carbon

Connecting PCs to Apple's Wireless Airport

Mac OS X Terms and Definitions

Discuss this article in the O'Reilly Network Forum.

Return to the O'Reilly Network Hub.