Skip to content
简体中文

Native C & Rust Provider Guide

External packages can register custom C, C++, or Rust renderers with egfx using a single 260-line C99 header (packages/egfx/native/include/egfx_abi.h) — with zero OS GUI header requirements and zero cross-library linking against egfx-core.

Path vtable->render Best For Platform GPU Code Needed
A. Pixel Upload (Recommended Start) Returns false; worker calls host->upload_pixels Libretro cores (NES / GBA / PS1 / PICO-8), DOOM / Quake, PDFium, software renderers Zero — reuses egfx’s built-in Metal / D3D11 / Vulkan blitter and egfx-snapshot
B. Direct Native GPU Returns true; draws via egfx_gpu_desc_t Hardware video decoders (libmpv IOSurface / D3D11VA), custom 3D shaders, native engines Per-OS (EGFX_GPU_METAL / EGFX_GPU_D3D11 / EGFX_GPU_VULKAN)
C. GPU-Less Native Container NULL (:backend 'native-container) OS WebViews (egfx-browser), Rust wgpu / Bevy engines owning their own swapchain Obtains egfx_raw_window_handle_t (compatible with Rust raw-window-handle)
  1. Your Emacs dynamic module exports plugin_is_GPL_compatible and emacs_module_init, registering a Lisp function (my-provider--register abi-handle).
  2. In Elisp, load your module via module-load and pass your registration callback to (egfx-register-native-module #'my-provider--register).
  3. egfx passes its opaque (egfx-abi-handle) user-ptr. Your C/Rust code checks api->abi_version == EGFX_ABI_VERSION (3) and calls api->register_provider(&g_vtable).
  4. Mount the surface with:
    (egfx-canvas :in (selected-window)
    :native-renderer 'my-provider
    :native-args init-string
    :key-mode 'raw
    :on-click #'ignore
    :on-native-event #'my-handle-event)

egfx-canvas auto-selects pointer policy 'pass-through when no mouse callbacks are passed. On macOS and Windows, a 'pass-through view does not accept mouse clicks to take keyboard focus. Always pass at least one pointer callback (even :on-click #'ignore) alongside :key-mode 'raw.

Callback Invoking Thread Rule
create / resize / render / grab_rgba / destroy GPU presentation thread Never call back into Emacs Lisp; join worker threads in destroy before freeing memory
call (egfx-native-call) Emacs main thread Never touch GPU contexts directly — marshal GPU work through host->run_on_gpu(surf, fn, ctx)
on_input OS window event thread Keep lock-free or atomic-only; never run simulation ticks here
  • egfx_input_event_t.code delivers OS-native physical key codes (macOS keyCode, Win32 VK_*, X11 keycode). Normalize them into logical game buttons per OS inside your provider.
  • Unmodified ESC automatically returns OS keyboard focus to Emacs across all three hosts so users are never trapped inside a raw surface. Treat bare ESC or visible == false as a clean pause trigger and open an Emacs transient menu over the canvas.

Build a Rust crate with crate-type = ["cdylib"], generate bindings for egfx_abi.h via bindgen, and wrap every extern "C" callback in std::panic::catch_unwind so panics never unwind across C stack frames into Emacs. For audio and gamepads, run cpal / kira and gilrs (or single-header miniaudio.h in C) directly on native threads outside Emacs.