An Introduction To Palm Programming
Developing for the Palm OS is very similar to developing for other platforms (Windows and Motif, for example). At the same
time, however, it is very, very different due to the drastically different requirements placed upon the developer by limited
processing power and screen space. In this tutorial, we'll focus on equipping the software developer with the knowledge
required to begin a Palm application development project. We will discuss popular development tools, information resources,
and testing options and will construct the inevitable "Hello World!" application.
Getting Started
Become A Registered Palm Developer
It would seem that the obvious first step in beginning Palm programming would be to purchase a Palm device. However, we will
see that this is not actually a requirement (at least not for beginning Palm programmers). Instead, the actual recommended
first step is to register yourself with Palm Computing as a professional developer. This registration process makes you
a member of the Palm Solution Provider Program, which opens a new world of technical and marketing support from Palm Computing.
To register, point your browser to http://www.palm.com/devzone/program.html,
fill out the form, and submit it. A username and password will be emailed to you and, from that point on, you will have access
to protected areas of the Palm Development Zone. Inside this area are information tools, tutorials, marketing programs, and
development support.
Obtain the Palm OS SDK
Following the registration process, we would advise that your next step be to stay at the Developer Zone and download the latest
copy of the Palm SDK (currently the 3.0 release at the time of this writing). The Palm OS SDK (located at http://www.palm.com/devzone/tools/sdks.html)
contains:
- Headers and libraries for Palm OS development
- API Documentation
- Source code for the built-in applications
- Samples
- A 21-step tutorial with source code
- Additional tools such as a debugger, MPW tools, and the Palm OS Emulator (known as POSE)
Note that the Palm OS SDK does not come with a compiler. Read on for information on tools that can be used to construct
applications.
Choose A Development Tool
At this point, it is time to choose the actual programming language and tool for your project. The "official" development
tool for the Palm OS platform is the Metrowerks CodeWarrior For Palm OS IDE.
This is a Palm OS-platform specific version of the venerable Metrowerks CodeWarrior C/C++/Java integrated development environment (IDE).
No official market-share numbers are available, but it is safe to say that the majority of application development for the Palm
OS are developed using CodeWarrior for Windows or the Macintosh. Will this always be the case? Possibly not. If you compare
the Palm to other popular computing platforms such as Windows, early Windows commercial developers chose C/C++ due to
synergy with the platform API and also because of performance issues. As the platform progressed, though, and corporations
began to demand quicker turnaround times for projects, Rapid Application Development (RAD) tools such as Visual Basic,
PowerBuilder, and Delphi began to take over the market. The Palm platform is unique, however, in terms of power, processing,
and memory constraints so it is likely that for the foreseeable future, C/C++ will be the development language of choice. A trial copy of CodeWarrior Lite is available for download from the Metrowerks site as well
as the Palm Developer Zone.
An open source suite of Palm development tools (including the GCC C/C++ compiler) is available under the GNU license.
GCC has two primary advantages over CodeWarrior: it is free (always a plus!) and it is available on Unix platforms. To most developers, its primary
drawback is its lack of a graphical development environment and project manager. For instance, while CodeWarrior allows you to
build graphical user interfaces visually (using a tool named Constructor), GCC requires resource definitions be built by hand
and compiled using a resource compiler. In addition, CodeWarrior includes example applications, documentation, and developer support, all of which
combine to make CodeWarrior worth the asking price. For information on the GCC toolkit, visit the Palm Development How-To
Page.
A number of other non-C/C++ tools are available all of which will be addressed on this site from time to time. The most popular
of these includes:
- CASL, from Feras Information Technologies. CASL includes a graphical IDE that
supports the construction of Palm applications with database access and synchronization capabilities through the CASL conduit.
Advantages include ease-of-use and low cost. Disadvantages are that code is interpreted on the handheld device and that CASL
uses its own proprietary language.
- Pendragon Forms, from Pendragon Software. Another graphical IDE that
supports the creation of forms-based data collection applications for the Palm. Pendragon Forms differs a bit from CASL in
that most of the development is done inside a forms generation tool. Scripting code is added to "tie" events together to create
the application. Advantages include synchronization capabilities to ODBC data sources and the advanced GUI development environment.
Disadvantages include the runtime module resulting in interpreted code and per-client licensing charges.
- Puma Satellite Forms, from Puma Technology. In our opinion, Satellite Forms offers the
best feature set of the Palm RAD tools. Included with the graphical IDE are the capability to extend Satellite Forms applications
with API calls and Satellite Forms controls (known as SFX controls). Advantages include data synchronization capabilities with
enterprise datastores such as Oracle and Lotus Notes as well as free runtime distribution for the standard version. Disadvantages
include the runtime performance hit due to interpreted code and the more expensive cost of the IDE.
- Waba, from WabaSoft. To quote WabaSoft, "Waba is a programming platform that lets developers write one program that can run on PalmPilot and Windows CE devices and on any machine that supports Java."
As of February 15, 2000, Waba will be an open source project as well. It has the advantages of being free and based on Java, which
means that a host of developers will already be familiar with the programming language. Disadvantages include its poor
performance (relative to C) and the Sun K Virtual Machine. As the KVM increases in functionality and popularity, Waba
may be deemed a duplication of effort.
Building Your First Application
In this tutorial, you will step through the process of creating a simple Palm application.
This includes the process of building the user interface, handling events, and popping
up a simple message box. After this, we'll discuss testing using the Palm emulator (known as POSE).
For this example, we'll make use of the Metrowerks CodeWarrior product. CodeWarrior gives the user two options when creating a new
project. New projects can be created using "Stationery" or they can be created as an empty project. Project stationery
is a template project that includes commonly used libraries, source code placeholders, and resource file placeholders.
By default, CodeWarrior presents the user with two Stationery options: Palm OS C App and Palm OS C++ App. For this example,
we'll choose the C App. Doing this creates a project with a readme file, starter.c (C source code), starter.rsrc (Resource file),
and StartupCode.lib (a commonly used library). Figure 1 shows this project loaded in CodeWarrior.

Figure 1 - The CodeWarrior Project
Let's take a look at the resource file first. If you are familiar with Macintosh or Windows C/C++ programming, then the concept
of resources is nothing new to you. A resource file is simply a file that defines that resources used by an application.
Resources typically include descriptions of items such as icons, menus, dialogs, strings, and bitmaps. A resource can be
modified globally throughout an application by modifying the resource file. It should be noted that nearly all modern development tools
include a resource editor and CodeWarrior is no exception. Figure 2 shows the CodeWarrior resource editor, known as Constructor.

Figure 2 - The Constructor Resource Editor
An examination of Figure 2 will reveal several resources were already created for us as part of the Project Stationery including
a form (named Main), menu bars, a menu, and an alert. For our purposes, we'll simply modify the text of a few of these
items and lay some new widgets out on the form. Double-click on any of the resources to edit them. We modified the title
of the application to "DevNetDemo" for our purposes and added two labels, two text entry areas, and a button. Note that this
example won't make use of these additions (we'll save that for our Palm database tutorial). To add the objects to the form,
select Catalog from the Window menu. This will produce the Catalog box shown in Figure 3. Items from the catalog
can be dragged and dropped onto the form.

Figure 3 - Our Form At Design Time
Examining the code produced as part of the Project Stationery serves to provide a good introduction to Palm programming
fundamentals. To start with, every Palm program must include one entry function named PilotMain(). The function definition
for PilotMain looks like:
DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
The cmd variable is a launch code used to give the application information on how the application was launched. Possible
values include sysAppLaunchCmdNormalLaunch, sysAppLaunchCmdFind, and sysAppLaunchCmdGoTo. The application can check
this value within the PilotMain function and determine how to proceed. The other variables passed in (cmdPBP and launchFlags)
provide additional information on the application's launch.
If you have ever done any Windows, Motif, or Macintosh programming, you are familiar with the concept of an event loop. An event
loop is a code construct that handles messages delivered to the application by the operating system. This handling is done
continuously in a loop until an application exit message or unhandled error is sent. The code generated with the Project
Stationery includes a function named AppEventLoop() that calls system (SysHandleEvent()), menu (MenuHandleEvent()), and a simple
application event handler (AppHandleEvent()). The AppHandleEvent() function is as follows:
static Boolean AppHandleEvent( EventPtr eventP)
{
Word formId;
FormPtr frmP;
if (eventP->eType == frmLoadEvent)
{
// Load the form resource.
formId = eventP->data.frmLoad.formID;
frmP = FrmInitForm(formId);
FrmSetActiveForm(frmP);
// Set the event handler for the form. The handler of the currently
// active form is called by FrmHandleEvent each time is receives an
// event.
switch (formId)
{
case MainForm:
FrmSetEventHandler(frmP, MainFormHandleEvent);
break;
default:
// ErrFatalDisplay("Invalid Form Load Event");
break;
}
return true;
}
return false;
}
As you can see, this code checks for the frmLoadEvent (sent when the application's main form is initially loaded) and
then sets the event handler for the MainForm to the MainFormHandleEvent() function. The code for MainFormHandleEvent()
is as follows:
static Boolean MainFormHandleEvent(EventPtr eventP)
{
Boolean handled = false;
FormPtr frmP;
switch (eventP->eType)
{
case menuEvent:
return MainFormDoCommand(eventP->data.menu.itemID);
case frmOpenEvent:
frmP = FrmGetActiveForm();
MainFormInit( frmP);
FrmDrawForm ( frmP);
handled = true;
break;
default:
break;
}
return handled;
}
As you can see, MainFormHandleEvent() handles two event types: menuEvents and frmOpenEvents. When
the form is opened, it is drawn using the FrmDrawForm() function. Pretty simple. If a menuEvent is received
(as happens when the user selected the "About DevNetDemo" menu option), the MainFormDoCommand() function is called. MainFormDoCommand()
looks like this:
static Boolean MainFormDoCommand(Word command)
{
Boolean handled = false;
switch (command)
{
case MainOptionsAboutDevNetDemo:
MenuEraseStatus (0);
AbtShowAbout (appFileCreator);
handled = true;
break;
}
return handled;
}
This function checks the eventP->data.menu.itemID value passed in from MainFormHandleEvent(). If it is equal to the
value MainOptionsAboutDevNetDemo (defined in the StarterRsc.h header file), an About Box is shown. Every Palm
application follows this same basic methodology (PilotMain() -> Application Event Handler -> Form Event Handler()).
Memory Management
For developers coming from the desktop world (where 200+ Mb of virtual memory is common), memory on Palm devices can seem
very constraining. This memory is broken up into two areas: the storage area (managed by the Palm Database Manager) and the
dynamic area (managed by the Palm Memory Manager). When your application stores or accesses data, it uses the storage area.
As variables are declared within your application, they are maintainined in the dynamic area along with system global variables,
system and application dynamic allocation, and the application stack. On the Palm OS, version 3.0 and greater, the size of
the dynamic heap (as the dynamic area is called) is 96 Kb. Because of this limited amount of memory, it is very important
that you remember to free up memory once it is no longer being used. The Palm Memory API provides two ways to allocated/deallocate
memory. The first method, using the MemPtrNew() function, allocates a pointer to memory. The second method, using the MemHandleNew()
allocates a handle. The handle approach is preferable because it allows the memory manager to move data in order to keep
free space contiguous. Before reading or writing data represented by a handle, you are required to lock it (using the MemHandleLock() function).
This prevents the memory chunk from being moved while in use.
Testing Our Application
At this point, we've covered the basics of Palm development. You should be ready to begin your first Palm project with your
knowledge of development tools, GUI programming, and memory management. One more item that you may find useful is the Palm
OS Emulator, or POSE as it is commonly known. POSE emulates a Palm organizer in software so you can quickly compile and run
your application on your local machine without having to sync up with a hardware device each time code is modified. Naturally,
before actually deploying an application, it should be installed and tested on the hardware just to be sure, however POSE makes
the job much easier during the development process. To download POSE, go to the Palm Developer Zone.
Once you have POSE downloaded and installed, run it and load our DevNetDemo application by right-mouse clicking (under Windows)
on the POSE screen and selecting "Install Application/Database->Other...". From there, select the .PRC file you've built to
add the Application to the virtual device. If it doesn't show up in the Applications view, click to another application then
back to the Applications view to refresh the list. From there, run the DevNetDemo application (see Figure 4).

Figure 4 - Our App In POSE
Other Resources
Books
See our Books section for recommended Palm books that can be ordered directly from WirelessDevNet.com.
|