#+TITLE: Chapter 5: UI, Resources, and Event-Driven Programming #+AUTHOR: Hermes & Claude #+DATE: 2026-10-07 #+PARENT: [[file:index.org][Index]] #+INCLUDE: goback.org * 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: #+BEGIN_SRC text [ 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 └─────────────────────┘ #+END_SRC * The Canonical Application Event Loop in C++ Here is how a clean, robust C++ application loop is structured: #+BEGIN_SRC cpp 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); } #+END_SRC * Form Event Handlers and C++ Member Callbacks Form event handlers in Palm OS are C function pointers with the signature: #+BEGIN_SRC c Boolean FormEventHandler(EventType* eventP); #+END_SRC 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: #+BEGIN_SRC cpp 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; } }; #+END_SRC * 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. ** 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. ** Example Resource File (~app.rcp~) #+BEGIN_SRC text 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 #+END_SRC ** Compiling Resources with PilRC #+BEGIN_SRC bash pilrc -q app.rcp # Generates binary resource files: tFRM03e8.bin, Talt07d0.bin, etc. #+END_SRC When packaging with ~build-prc~, simply include the ~.bin~ or ~.grc~ files: #+BEGIN_SRC bash build-prc MyApp.prc "MyAppName" CRID *.bin code0001.grc data0000.grc #+END_SRC #+LF * 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