# GXWI Applications

---

# The GXWI Application Model

_Peios / Developing for Peios / GXWI Applications_

> Separate process state, browser presentation, and desktop authority.

A GXWI application runs on the Peios machine and presents surfaces through
the desktop. It connects to the display named by `GXWI_DISPLAY`; it does
not expose an independent public listening port.

## Three layers

The SDK has distinct responsibilities:

| Layer | Responsibility |
|---|---|
| `libgxwi.so` | C interface for desktop connections and surface requests |
| Rust `gxwi` crate | Application HTTP service, rendering, and settings helpers |
| Compositor | Desktop layout, browser presentation, and shell authorization |

Use the C interface when integrating another language or a custom runtime.
Use the Rust rendering layer for process-owned HTML surfaces. The shared
library does not implement the Rust rendering helpers.

## State belongs to the application

A live surface owns its application state and named input fields in the
process. Browsers receive renderings of that state. Multiple browsers can
interact with the same surface, and reconnecting browsers receive its
latest state.

This is not automatic persistence across application failure or session
shutdown. Save durable data explicitly. Keep focus, viewport geometry, and
other device-specific presentation out of shared application state.

## Surfaces and requests

An application can present windows and dialogs. Shell programs can also use
panels, backgrounds, and overlay surfaces where the compositor permits
those roles. Merely requesting a role or obtaining an SDK shell handle does
not grant shell authority.

Closing a live surface is a request to the application. The application can
ask about unsaved work before allowing the close. Closing the last surface
ends the application's desktop loop.

## Trust the operation, not the markup

Treat events and field values as untrusted input. Validate actions in the
process and rely on the backend's authorization checks. Escape untrusted
text before placing it in HTML. The Rust `escape` helper handles text
escaping; it does not validate URLs or authorize operations.

## Launching files

`Application::command` returns a result when adding a file argument. The
desktop startup protocol carries UTF-8 text, so a native filename that is
not valid UTF-8 cannot be represented in that request. The SDK returns an
error instead of replacing bytes and opening a different path. Show the
error and keep the original native path intact.

## Application declarations

The application catalog accepts regular declaration files up to 64 KiB.
It checks the opened descriptor and bounds the actual read, so a file that
grows beyond the limit is rejected. A declaration at the exact limit is
accepted if its content is valid. Special files are not declarations.

Executable icon probes also require an opened regular file. These checks
prevent catalog discovery from treating a pipe or device as a normal file.
They do not replace validation of the declaration's fields.

---

# Live Surfaces and Input

_Peios / Developing for Peios / GXWI Applications_

> Use process-owned rendering, named fields, and explicit event handlers.

Implement the Rust `Live` trait to draw a surface from application state.
`render` returns HTML. `event` handles named actions, `input` responds to
field changes, and `closing` decides whether a close request can proceed.

## Rendering and actions

`App::live` creates a window and returns a `Surface` handle. Use
`Surface::update` to change state from a worker and request another render.
Use `Surface::look` to inspect the application, fields, and surface facts
under their shared state lock.

An element's `fx-click` attribute names an action. Attributes beginning
with `fx-value-` provide action data. A form with `fx-submit` names its
submission action. The browser protocol is implemented by the compositor;
adding arbitrary HTML event attributes does not create a Rust callback.

Keep rendering free of irreversible effects. A browser reconnect or field
change can cause another render. Put effects in explicit action handlers
and background operations.

## Named fields

`Fields` stores values for named inputs. `Fields::get` returns an empty
string for an absent field. `Fields::set` changes a field from the process
and updates connected views, including a view editing that field.

The browser input path accepts at most 256 fields. Each value is limited
to 1 MiB, their total to 4 MiB, and each name to 256 bytes. These are
transport limits, not application-specific validation rules. Validate
lengths and formats required by the operation itself.

Do not use the field transport for files larger than its limits. A custom
bounded HTTP route can serve a different data-transfer requirement.

## Shared edits

Multiple views share field values. They do not form an independent draft
per browser. Design conflict-sensitive operations around explicit state,
revisions, or confirmation rather than assuming only one viewer edits a
surface.

The surface state lock serializes its callbacks. Avoid blocking operations
inside rendering or a callback that must keep the interface responsive.
Move long work to a worker and publish progress through the surface handle.

## Closing

Return `false` from `closing` to keep a surface while asking about unsaved
work. Once the user confirms, use the surface or closer handle to close it.
A process failure or ended desktop session is not an interactive close
request, so save durable data independently of this callback.

---

# Building a Settings Application

_Peios / Developing for Peios / GXWI Applications_

> Compose settings pages and apply validated changes with the Rust settings kit.

The Rust `gxwi::settings` module builds settings interfaces from HTML
helpers. It supplies layout and controls; your application owns state,
validation, backend calls, and authorization.

## Before you start

Create an application using `App` and implement `Live` for its page state.
Choose the backend operation and permissions each control needs. Use the
SDK's checked example and Rust API documentation for complete signatures.

## Build the page

1. Register `settings::stylesheet` with the application before adding your
   own stylesheet.
2. Choose `settings::window` for a page with navigation or `settings::single`
   for one section without navigation.
3. Build navigation with stable section identifiers. Handle the `section`
   event by validating the requested identifier and selecting its page.
4. Assemble controls with settings helpers and named fields. Escape text
   that comes from users, files, or services before embedding it in HTML.
5. Show the effective backend state and an actionable error when an
   operation fails.

The kit adapts navigation to narrow views. It does not require the process
to track a browser's viewport.

## Apply changes deliberately

Use immediate application for controls such as a switch when the operation
is reversible and failure can be shown clearly. Restore the displayed field
if the backend refuses the change.

Use an explicit **Apply** action for a group of typed values that must be
validated together. Validate the complete proposed state before submitting
it. Rendering a success message before the backend accepts the operation
misrepresents the effective configuration.

For a destructive operation, ask for confirmation and focus the safe
choice. A confirmation is not authorization: the backend still checks the
caller's rights.

## Verify the result

Exercise successful changes, refused access, invalid values, and backend
failure. Open the same surface in two browser views and check that both
reflect accepted state. Verify keyboard operation and a narrow viewport.

---

# Custom HTTP Pages

_Peios / Developing for Peios / GXWI Applications_

> Understand custom application routes and browser-owned state.

Use a custom HTTP page when a live surface cannot represent the interface,
such as a terminal renderer. The application still serves through the
desktop's connection; it does not need its own public network listener.

## Routes and resources

`App::routes` adds application HTTP routes. `App::file` serves a resource,
and `App::stylesheet` registers a stylesheet for live surfaces. Paths are
relative to the application's routing space. Do not assume that the
application owns the browser's root path.

`App::raw_page` presents a scripted page. The compositor keeps that page
apart from other applications. Its browser-owned state does not
automatically reach another device or survive a tab being closed.

## Isolation and keyboard access

A raw page has no ordinary access to the desktop's cookies or browser
storage. Worker code uses a blob where required by the isolated page's
origin. Do not use the absence of storage as a substitute for validating
requests in the application.

Include the compositor's key-forwarding module from `.fx/keys.js` within
the application's path space. Without it, desktop keyboard shortcuts can
be unavailable while focus is inside the page. The SDK's hello raw-page
example demonstrates the expected resource layout.

## Lifecycle

Closing a raw window allows it to close without the live surface's
`closing` decision. Keep important state in the process and implement an
explicit save or stop flow where the interface requires it.

Limit request bodies, pending work, and output queues according to the
application's needs. The shared transport cannot choose an appropriate
memory or cancellation policy for every application protocol.

---

# The C Client Lifecycle

_Peios / Developing for Peios / GXWI Applications_

> Manage desktop handles, service descriptors, and owned events through libgxwi.

`libgxwi.so.1` exposes the desktop control interface to C and other
languages with a C foreign-function interface. Include `peios/gxwi.h`
and link with `libgxwi`. The header and `libgxwi(3)` define exact types,
function signatures, errors, and concurrency requirements.

## Connect and serve

`peios_gxwi_connect` uses `GXWI_DISPLAY` and returns an owned opaque display
handle. The library sets Identification before connecting. It does not
adopt arbitrary caller-supplied display descriptors.

`peios_gxwi_serve` transfers one end of a socket to the desktop and returns
the other as a close-on-exec descriptor. Your application owns that
descriptor and serves HTTP/2 on it. Start serving before asking the desktop
to present routes from that connection.

Create live or raw surfaces with application-chosen identifiers. The C
library sends the surface description; it does not implement rendering,
HTTP handling, or the live-surface WebSocket protocol for you. The Rust
`gxwi` framework supplies those higher layers.

## Receive and release events

`peios_gxwi_receive` blocks and returns one of three results:

| Result | Meaning |
|---|---|
| `1` | An independently owned event is available |
| `0` | End of file, with a null event output |
| `-1` | Failure reported through `errno` |

Inspect an event's kind before using its typed getters. A close event
identifies an application surface. A surface-list event describes the
desktop surfaces visible to an authorized shell connection. Unknown or
malformed messages become `PEIOS_GXWI_EVENT_OTHER`. Truncated packet data
or descriptor control data also produces that event rather than a parsed
partial message.

Destroy each event after use. Text returned by an event getter borrows
storage from that event and becomes invalid when the event is destroyed.
An event remains valid after its display handle is destroyed.

## Text and output ownership

Text is UTF-8 with explicit byte lengths. No NUL terminator is required,
and embedded NUL bytes are supported. A null pointer is valid for an empty
span or array, not a nonempty one.

Input storage is borrowed only for the duration of the call. Output
parameters remain unchanged on failure. Supply writable outputs that do not
alias inputs. The library cannot make an invalid non-null pointer safe.

## Shutdown and concurrent calls

Display operations can run concurrently. Concurrent receivers consume
different packets without a promised completion order. Use one receiver
when your application requires a single ordered event-processing loop.

`peios_gxwi_stop_listening` wakes receivers with end of file and leaves
sending available. Wait until all calls using a display finish before
destroying it. Events can be read concurrently, but destruction cannot
race a reader.

## Shell authority

Program startup, desktop-surface close, and sign-out calls send requests.
Successful transmission does not acknowledge that the desktop accepted or
completed the action. The compositor decides whether the connection has
shell authority. Observe subsequent desktop state rather than inventing a
completion guarantee.

---

# Interaction Markup

_Peios / Developing for Peios / GXWI Applications_

> Choose declarative controls, keyboard actions, menus, and shell presentation.

Live-surface HTML describes actions for the compositor to dispatch.
Attributes do not replace validation in the application. The Rust `Live`
API documentation gives the complete markup contract.

## Actions and forms

The common action attributes serve these purposes:

| Attribute | Purpose |
|---|---|
| `fx-click` | Named activation action |
| `fx-dblclick` | Named repeated activation action |
| `fx-value-*` | Values associated with an action |
| `fx-submit` | Named form submission action |
| `fx-autofocus` | Focus target when a control appears |
| `fx-key` | Surface-local keyboard activation |

Named input fields belong to the surface. Validate form and action values
in the process before calling a backend. Escape text when generating HTML
attributes as well as element content.

## Keyboard and menus

A keyboard chord uses modifiers such as `Ctrl`, `Alt`, `Shift`, and `Meta`
with a key name. Field-editing shortcuts remain field operations where
appropriate. The desktop reserves its own chords, so an application cannot
assume every chord reaches its handler.

`fx-menu` identifies a hidden `menu` element. Its action items provide
labels and values; the compositor presents the menu. A disabled item
cannot be selected through the normal interface, but its handler must
still enforce operation validity and access.

## Device-local presentation

`fx-copy` copies the named value to the activating device's clipboard.
This is distinct from an application-owned clipboard shared through a
session file or service.

`fx-columns` lets a view resize columns. `fx-reorder` reports a requested
reordering with a destination position. The next process rendering remains
the authority for the accepted application order.

## Shell presentation

Authorized shell surfaces can describe desktop focus, workspace, and
overlay actions. `fx-focus`, `fx-workspace`, and `fx-overlay` affect the
presenting device where the compositor implements them. They do not make
focus into shared process state.

`fx-global-key` names a desktop-wide chord for a shell control. Requesting
that attribute does not make an ordinary application a shell. The
compositor determines which program connections have that authority.
