Skip to main content

Module events

Module events 

Source
Expand description

Host-side event system for Oxide guest modules.

Guests register listeners with api_on_event(type, callback_id) and receive events through the optional exported on_event(callback_id: u32) function. While the callback runs, the guest may read the current event type and payload via api_event_type_* and api_event_data_*.

Two sources push events onto the per-guest queue:

  1. Custom events emitted by the guest itself via api_emit_event(type, data).
  2. Built-in events produced by the host each tick:
    • resize — payload is (width: u32, height: u32) little-endian.
    • focus / blur — empty payload; fires when the canvas gains/loses focus.
    • visibility_change — payload is "visible" or "hidden" UTF-8.
    • online / offline — empty payload; backed by a 30 s reachability check.
    • touch_start / touch_move / touch_end — payload is (x: f32, y: f32) little-endian. Synthesised from primary mouse button + mouse position; touch_cancel is reserved for real touch input on platforms that surface it.
    • gamepad_connected — payload is the device name (UTF-8).
    • gamepad_button — payload is (gamepad_id: u32, button_code: u32, pressed: u32) little-endian (pressed = 1 for press, 0 for release).
    • gamepad_axis — payload is (gamepad_id: u32, axis_code: u32, value: f32) little-endian (value in [-1.0, 1.0]).
    • drop_files — payload is a JSON array of dropped file paths (UTF-8).

Built-in producers run inside drain_pending_events, which the runtime calls once per frame before invoking the guest on_event exports.

Structs§

EventState
Per-guest event system state: listener table, pending queue, current event, and built-in event detector state (last canvas size, focus, mouse, online).
PendingEvent
One queued event: type name and arbitrary opaque payload bytes.

Functions§

drain_pending_events
Drain built-in event sources + the pending custom-event queue and return (callback_id, type, data) tuples to dispatch via on_event.
enqueue_drop_files
Push a freshly-dropped file batch onto the queue as a drop_files event.
register_event_functions
Register api_on_event, api_off_event, api_emit_event, and the in-callback type/data read functions on the linker.
set_current_event
Populate current so the guest can read the event type/data inside its on_event callback. Called by the runtime immediately before each on_event(callback_id) invocation.