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.
Three Integration Paths
Section titled “Three Integration Paths”| 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) |
Zero-Link Registration Handshake
Section titled “Zero-Link Registration Handshake”- Your Emacs dynamic module exports
plugin_is_GPL_compatibleandemacs_module_init, registering a Lisp function(my-provider--register abi-handle). - In Elisp, load your module via
module-loadand pass your registration callback to(egfx-register-native-module #'my-provider--register). egfxpasses its opaque(egfx-abi-handle)user-ptr. Your C/Rust code checksapi->abi_version == EGFX_ABI_VERSION(3) and callsapi->register_provider(&g_vtable).- 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)
Critical Engineering Contracts
Section titled “Critical Engineering Contracts”1. Pointer Policy & Raw Keyboard Focus
Section titled “1. Pointer Policy & Raw Keyboard Focus”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.
2. Multi-Threading Boundaries
Section titled “2. Multi-Threading Boundaries”| 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 |
3. Scancodes & Bare ESC Focus Return
Section titled “3. Scancodes & Bare ESC Focus Return”egfx_input_event_t.codedelivers OS-native physical key codes (macOSkeyCode, Win32VK_*, X11keycode). Normalize them into logical game buttons per OS inside your provider.- Unmodified
ESCautomatically returns OS keyboard focus to Emacs across all three hosts so users are never trapped inside a raw surface. Treat bareESCorvisible == falseas a clean pause trigger and open an Emacstransientmenu over the canvas.
4. Rust cdylib Integration
Section titled “4. Rust cdylib Integration”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.