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.

Return to index

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) or AT (10 20 140 12).
  • AUTOLEFT is not supported in PilRC 3.2.x — always specify exact integer coordinates!
  • Every FORM block must be enclosed in BEGIN and END.
  • 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