Chapter 5: UI, Resources, and Event-Driven Programming
This is an LLM ass-isted document, as are all in the ass/ subdirectory.
Of these, here in the dump/ subdirectory I keep the sloppy & doubtful ones.
I keep these as learning materials for myself, yet to be went thru.
1. The Palm OS Event-Driven Architecture
A Palm OS application is fundamentally an event consumer. Unlike modern multithreaded GUI frameworks where background threads trigger reactive state updates, Palm OS operates on a strict, single-threaded cooperative event loop:
[ Hardware Events ]
(Pen down, Key press, Timer, Alarm)
│
▼
[ System Event Queue ]
│
▼
┌─────────────────────┐
│ EvtGetEvent │ ◄─── Blocks if queue is empty
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ SysHandleEvent │ ◄─── Battery auto-off, contrast, power key
└──────────┬──────────┘
│ (if not handled)
▼
┌─────────────────────┐
│ MenuHandleEvent │ ◄─── Silk-screen menu key, title bar taps
└──────────┬──────────┘
│ (if not handled)
▼
┌─────────────────────┐
│ AppHandleEvent │ ◄─── Form loading (frmLoadEvent)
└──────────┬──────────┘
│ (if not handled)
▼
┌─────────────────────┐
│ FrmDispatchEvent │ ◄─── Routes event to active Form's Handler
└─────────────────────┘
2. The Canonical Application Event Loop in C++
Here is how a clean, robust C++ application loop is structured:
struct EventType { UInt16 eType; Boolean penDown; UInt8 tapCount; Int16 screenX; Int16 screenY; // Union of event-specific data (data.ctlSelect, data.keyDown, etc.) UInt8 data[32]; }; void EventLoop() { EventType event; UInt16 error; do { // Wait indefinitely (timeout = -1) until an event arrives EvtGetEvent(&event, evtWaitForever); // Allow system shortcuts and power management to handle the event if (!SysHandleEvent(&event)) { // Allow active pull-down menus to intercept the event if (!MenuHandleEvent(nullptr, &event, &error)) { // Application-level routing (form switches) if (!AppHandleEvent(&event)) { // Send event to the active Form event handler callback FrmDispatchEvent(&event); } } } } while (event.eType != appStopEvent); }
3. Form Event Handlers and C++ Member Callbacks
Form event handlers in Palm OS are C function pointers with the signature:
Boolean FormEventHandler(EventType* eventP);
Because C++ non-static member functions have a hidden this parameter passed in registers, they cannot be registered directly as Palm OS event handlers!
To bridge modern C++ objects with Palm OS forms, we use a static dispatcher pattern:
class MainForm { UInt16 formId_; int counter_ = 0; public: explicit MainForm(UInt16 id) : formId_(id) {} // Static C-linkage trampoline static Boolean EventHandler(EventType* event) { // Retrieve current active form instance MainForm* self = GetActiveInstance(); if (self) { return self->HandleEvent(event); } return false; } Boolean HandleEvent(EventType* event) { switch (event->eType) { case frmOpenEvent: FrmDrawForm(FrmGetActiveForm()); return true; case ctlSelectEvent: // Handle button tap! counter_++; UpdateDisplay(); return true; default: break; } return false; } void UpdateDisplay() { // Draw directly to the Palm OS window buffer WinDrawChars("Hello Palm!", 11, 20, 40); } private: static MainForm* s_activeInstance; static MainForm* GetActiveInstance() { return s_activeInstance; } };
4. UI Resources and PilRC
Palm OS UI interfaces (forms, buttons, text fields, checkboxes, bitmaps, menus, and alert dialogs) are declared in text resource definition files (.rcp) and compiled by PilRC into binary resource chunks.
4.1. PilRC Syntax Rules
- Coordinates:
(x y width height)orAT (10 20 140 12). AUTOLEFTis not supported in PilRC 3.2.x — always specify exact integer coordinates!- Every
FORMblock must be enclosed inBEGINandEND. - Double-slash
//comments often fail in older PilRC versions — use#or omit comments.
4.2. Example Resource File (app.rcp)
FORM ID 1000 AT (0 0 160 160)
FRAME
MENUID 1000
BEGIN
TITLE "GCC 14 Modern C++"
LABEL "Counter App" AUTOID AT (10 25)
FIELD ID 1001 AT (10 45 140 20) USABLE EDITABLE MULTIPLELINES
BUTTON "Increment" ID 1002 AT (10 135 60 18)
BUTTON "About" ID 1003 AT (90 135 60 18)
END
ALERT ID 2000
INFORMATION
BEGIN
TITLE "About This App"
MESSAGE "Built with GCC 14.4.0 targeting Motorola 68000 DragonBall."
BUTTONS "OK"
END
4.3. Compiling Resources with PilRC
pilrc -q app.rcp # Generates binary resource files: tFRM03e8.bin, Talt07d0.bin, etc.
When packaging with build-prc, simply include the .bin or .grc files:
build-prc MyApp.prc "MyAppName" CRID *.bin code0001.grc data0000.grc
5. Further Reading & Reference Resources
- Palm OS User Interface Guidelines:
- PalmSource, Inc. Palm OS User Interface Guidelines (v4.0): https://palm.wiki/development/docs/601/UI_Guidelines/
- Designing Zen-style Instant Interfaces for Handhelds: https://www.palminfocenter.com
- Resource Definition & PilRC Documentation:
- Aaron Ardiri & PilRC Community: PilRC Manual and Syntax Reference: https://pilrc.sourceforge.net/
- PilRC UI Control Types (Buttons, Graffiti State Indicators, Tables, Sliders, Popups): https://github.com/jichu4n/pilrc/blob/master/doc/manual.html
- Event Loop Design & Architecture:
- Steve Mann. Palm OS Programming for Dummies. IDG Books.
- Event Handling Deep Dive: https://palm.wiki/development/docs/601/PalmOSCompanion/Events_Chapter.html