RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Notes for agents working on RediWM
Details that are easy to get wrong; read the section for what you touch.
Tests: TESTING.md. Where code lives: source map.
Area notes are in an AGENTS.md beside the code; read
the one for the directory you work in: src/files, src/editor,
src/desktop, src/input, src/control_center, src/ui, src/session,
src/dm, src/accounts_helper, src/firewall, src/printing, src/player,
scripts, src/wlr, src/platform.
Working rules
- Keep this file to cross-cutting rules and pointers; put detailed subsystem
notes in the linked documents or the area’s
AGENTS.md. - Tool use is costly: no tight polling (wait 15–30 s between checks of long operations), batch checks, don't re-read unchanged files.
- Add tests only when really needed or asked; prefer extending existing ones.
- Keep files small enough to read whole. When a file you touch grows past
~800 lines or a function past ~150, split it by concern (below), moving
code verbatim (C and C++ too). Tests go in a
*_tests.zigbeside the code once they outgrow a screen. Exempt: generated or table files (ui/fold_data.zig,ipc/commands.zig).tools/split/moves Zig declarations from a spec and checks C splits; read its README first. - Before finishing: build Debug and
-Doptimize=ReleaseSafe, runzig build testand the integration tests for what you touched. Some suites fail on HEAD: compare with agit worktreeof HEAD before calling a failure a regression.
Split files
Large types are spread over sibling files by concern. A TitleCase host keeps
its methods in a lowercase directory: Toplevel.zig (toplevel/),
Input.zig (input/), Taskbar.zig (taskbar/), Server.zig (server/),
Output.zig (output/), World.zig (world/), Dock.zig (dock/),
Keyboard.zig (keyboard/). Other hosts keep them beside them under a
prefix: files/app.zig, pdf/app.zig, desktop/app.zig, images/app.zig,
editor/app.zig (app_*.zig); files/Client.zig and the Client in
editor/main.zig and pdf/main.zig (client_*.zig);
control_center/ControlCenter.zig (tree, keys, relayout, paint.zig);
start_menu/StartMenu.zig (menu_*.zig); session/lock.zig, power.zig
(lock_*, power_*.zig); systemd.zig (systemd/); network/manager.zig,
dbus/connection.zig, ipc/wait.zig, ui/input.zig (<name>_*.zig).
Hosts keep the fields, init/deinit and one
pub const name = group.name; alias per method, so self.name() is unchanged;
the bodies are free functions taking the struct pointer. A new method goes in
its group file plus an alias line. ipc/protocol_*.zig, ipc/handlers_*.zig,
config/loader_*.zig, control_center/sections/appearance_*.zig,
printers_*.zig and msg/msg_*.zig, ui/paint_*.zig, ui/text_*.zig,
ui/theme_*.zig, ui/anim_*.zig, chrome_paint.zig re-export their public
declarations from the original file. handleRequest/handleAction dispatch
groups of tags to handlers_*.zig, each a switch with else => unreachable.
build.zig keeps the options and shared modules; programs, data, session
binaries and test steps are in build/ and run in the order build() calls
them.
Tests that used to sit in the big file are in a *tests.zig file reached by a
test { _ = @import(...); } block: a file nothing imports contributes no tests.
State (var) stays in the file that uses it; an alias would copy it.
C splits keep the public header and the file's lifecycle in the original
(projection.c, scene_output.c, scene_pass.c), with the parts
(projection_*.c, …) sharing a private *_internal.h: the structs, and small
per-item helpers as static inline (no LTO, so a file boundary must not cost
them inlining); other shared functions lose static and take the file's
prefix. build/support.zig lists the parts, and fixtures that #include a .c
include every part (projection_test.c); their cases are *_test_*.c files
included into the same unit. The transfers helper is files/torrent/helper*.cpp
around helper.hpp, all in namespace helper.
Rendering and damage
Read rendering details before changing projection, the output pass, glass, window shapes, chrome, capture or retained panel painting. The key rules:
- Colours are premultiplied in wlroots/Cairo buffers; theme colours are
col.Straight. Usecol.createRect/setRectandStraight.argb()/Premul.argb(). White border edges suggest a mix-up. - Resolve geometry, then present: derive geometry only in
resolve;presentsnaps edges once (rediwm_edges_snap), never origin plus size. New clip/opacity/placement rules belong on resolved items. Detach paint-list entries before freeing their heads. - Scene nodes are ours (
node.zig/node.c, wlr_scene's method names). A buffer or rect node keeps its pixels in a hidden wlr_scene carrier that the output pass reads; every setter keeps the two in step, and every change asks for a frame through the scene's hook. Client surfaces are own nodes too (node_surface.c,node_subsurface_tree.c,node_xdg_shell.c, ported from wlr_scene_surface/subsurface_tree/xdg_shell/drag_icon): a surface is a buffer node whosesurfacebinding follows its wl_surface, an xdg surface a tree. The carrier's signals (outputs_update,output_sample,frame_done) are where the binding listens, so the projection raises them on the carrier. Hits come fromProjection.nodeAt(a leaf node; a client surface hasbuffer.surface). A sync resolves only the top-level subtrees (groups) whose nodes changed, and nothing when no group and not the camera did: a change the node setters cannot see must callNode.touch, and a node whose geometry depends on another group's (a glass effect) is walked every sync. The-Ddamage-auditbuild logs any item the gate left stale. - The world has no second tree: its source tree stays disabled. Pick with
rediwm_projection_node_at, send callbacks throughServer.sendFrameDone, and emit feedback/output_sampleon source buffers. GLES may releasewlr_scene_buffer.bufferafter upload: persist raster sizes and treat a buffer, texture or single-pixel colour as content. - Commit through
Output.commitFrameStateand build output state throughServer.buildOutputState, including capture. Register new commit reasons incommitIsRedundant; skipped callbacks wait forvblank_timer. - Late rendering is opt-in (
[compositor] late_render; glue inoutput/late.zig, model inlate_render.zig). Off, nothing in the frame path changes. On, a frame event that finds work and a trusted presentation timeline (vsync + hardware clock, a stream of frames, no VRR/tearing) is deferred: frame callbacks go out then so clients answer inside the wait,late_timerrunsrunFrame, which takes the plan and skips its own post-commit callbacks, and frame events meanwhile are ignored. Frames after idle always render at once. The deadline isvblank - worst recent cost - margin, cost = max(CPU to commit, build + GPU elapsed fromserver.gpu_recent); a missed vblank grows the margin, three in two seconds pause it with backoff. Headless presents synchronously and nested pipelines are deeper, so onlylate_render_tests.zigand a DRM run reach the engaged path (tests/frame_latency.py). - Window frames are shapes: every ring pixel needs body content beneath
it. Shadows, resize grabs, popups and other nodes outside the window need
setTreeUnclipped. Capture the client alone with its shape supplied explicitly. Shaped programs are opt-in; without them windows are square. - Restore owned GL state, especially
GL_ARRAY_BUFFER, before wlroots draws. Draw borders with their bodies; blend NONE only in the opaque, ring-free core. - Glass belongs to the output pass. Attribute changes through projection damage events; only painted leaves emit them. Input-only nodes must not invalidate glass. Retained blurs and holds are per effect and output.
- Damage follows each buffer's history: never write a busy shm buffer.
Use
markDirty/markPaintDirty, damage old and new move bounds, publish at the real raster scale, and report owned-buffer damage viaProjection.damageBufferorpresentOwned. Cache keys include visual state, text, geometry and Cairo transforms. Round globally before group offsets. - Fractional buffer size differs from snapped scene size; bilinear chrome and panel buffers must not be rerasterized for device-grid phase changes.
- Idle makes no wakeups: deadline-only timers, eventfd worker wakes,
text.applySize, and title-span/chip repainting. Measure per-thread context switches. Nested sessions never set buffer scale: judge atREDIWM_SCALE=1using IPC screenshots/buffer dumps, not hostgrim. - Client-buffer damage needs ignorable long-held locks for in-place updates. Only nonzero v4 attach offsets remap; don't resend unchanged buffer scales; prefer compositor v5.
Windows and focus
- Unmapped ≠ destroyed.
frame_treestays disabled untilhandleMapped;focusSurface/minimize/restoreneedin_world;destroyNowassertsbackend_gone(or a compositor-owned backend). - Dock geometry: reserve space only without auto-hide; use
dockAreaBoxfor the dock and taskbar popups,usableBoxfor windows. Read dock details before changing the dock, its painting, layout/reservation, OSD clearance, hover or auto-hide. - Tiles are compositor-owned geometry like maximize (
snap.zig,forcedGeometry()).Toplevel.layout()reads themaximizedfield, notisMaximized()(xdg acks later). Leaving a layout without moving goes throughleaveLayout(). - Tiles glide and workspaces slide.
setAutoTile(rect, animate)eases like snapping (auto_tiling.Manager.quietfor non-user reflows). A switch keeps the leaving workspace's frames shown but inert (rediwm_projection_set_tree_inert) untiltickSwitchsettles; anything that stops showing a window mid-slide callsfinishSwitch. Input follows what is drawn andchrome_boxsizes are animated: testswait_for("window_geometry_settled")before pointer input or geometry snapshots. Backend.shellwindows (Settings) paint intoscene_treeand present withpresentOwned. No wl_surface: focused = first inworld.toplevels, activated, no seat focus (hasKeyboardFocus). Never close one from a widget callback; details insrc/control_center/AGENTS.md.- Desktop keyboard focus is shell focus: taking it clears seat focus; a
surface gaining seat focus, or a
.shellwindow, takes it back. - Input details are in
src/input/AGENTS.md. Cross-cutting: every client motion path callsclientMotionthensync; Escape cancels drags throughInput.cancelDrag(wlroots doesn't); compositor drop targets (desktop_drop.Target) end the grab before negotiating;wlr_data_source_sendcloses its fd. - Internal dock drags use
dock_drag.zig, not wl_data_device. Pins act on release; Escape cancels throughInput.cancelDragbefore shell panels handle it. Read dock details for start-menu pinning, reordering and pin storage.
Xwayland
- X11 renders at N =
ceil(max output scale).Xwayland.createruns before outputs exist;refreshScalerecomputes N afterOutput.create. Reproduce ordering bugs with per-output config scale, notREDIWM_SCALE. Scale changes wait for a restart once X11 surfaces exist. - Units:
clientSurfaceGeometry()X units;clientGeometry(),requestSize(),commitPosition()world units (ceil(surface/N));applySizeClip()surface units;surfaceScale()= N. Content trees userediwm_projection_set_tree_scale(1/N); X11 chrome density is 0.75. - Clamp stale off-screen geometry of new X11 windows in
handleRequestConfigure(exceptopen_rules/forcedGeometry()). - Keep
scene.restack_xwayland_surfaces = false;Toplevel.raisesyncs scene and X11 stacking and never raises an owner above its transients. Compositor hit tests can't prove X11 routing.
Security, lock and login
- Greeter (
rediwm --greeter) never unlocks (onlyrediwm-dmdecides), writes nothing under$HOME(it disables the GLES2 shader cache), and runs no Xwayland, autostart, IPC, notifications, tray, portal, polkit, services, audio, desktop or external-tool globals: skip anything new that owns a bus name, spawns or writes (tests/greeter.py). Secrets stay in mlocked, wiped buffers. Boot waits for it: every program it builds is an uncached compile, so it builds only those it draws with (readiness). - A crash never unlocks.
Lock.announcereports state onREDIWM_SESSION_FDfirst thing inengage;rediwm-sessionqueueslockfor a compositor that died locked, andmain.ziglocks before the backend starts. Lock-before-sleep holds a logind delay inhibitor untilLock.covered(). OnlyREDIWM_LOGIN_SESSION's logindLockis followed;Unlockis ignored on purpose. The lock never polls. - Only rediwm-dm, the accounts helper and the firewall helper run as
root (see their
AGENTS.md); never the compositor. - No test hooks in production:
REDIWM_*_PREVIEWonly intestblocks,REDIWM_TEST_*only in noninstalled fixtures; onlypolkit_test_servicereadsREDIWM_POLKIT_HELPER_SOCKET(it decides where passwords go).stringsa ReleaseSaferediwmafter touching env handling. - App scopes: every user-app launch calls
app_scope.place(own scope in app.slice, since systemd-oomd kills whole cgroups); workers hand pids to the main thread first. Off nested/headless unlessREDIWM_APP_SCOPES=1; never in the greeter. - IPC fails with
SessionLockedwhilelocker != null(subscriptions re-check). Synthetic input and pixel reads need[ipc] automation. Socket only in a private directory, umask 0177. Sandboxed callers need an explicit[[sandbox_allow]]ipcallowance, fail closed and are re-checked on reload; a connector that has exited or whose pid is not visible is refused (a child may hold its socket).scrollis natural by default (negative dy pages down). - Security context (
global_filter.zig) hides privileged globals fromwp_security_context_v1clients (guess-binds are protocol errors) unless[[sandbox_allow]]grants the group; nested contexts are fatal. - Protocols: a client ext-session-lock is a
Lockwithorigin = .client, solockerguards apply and its death keeps the session locked. Screencopy policy isguardScreencopy; foreign-toplevel sync sits beside everyonWindowChanged/onWindowMoved; output management goes throughOutput.applyAndPersist; only a clientonclearsOutput.power_off. - Explicit sync: the pass emits
output_sampleon the source buffer, which carries the wait points; other GPU reads useReadFence, CPU reads checkAcquireWait.ready.
Shared UI
- Widget callbacks carry
owner+id: a stable host pointer that outlives the tree, never a global. Finish state updates before calling back, reset the dispatcher on rebuild, defer host destruction until dispatch returns. ui/widgets/owns every control's look (fields, buttons, dialogs, checkboxes, toggles, scrollbars, sliders, battery badge): never hand-draw or hand-hit-test one (src/ui/AGENTS.md).- App clients (Files, Images, PDF) draw through
ui/cairo.zigwith theme tokens (app_*,shellPalette().accent) and shell icons. Text is the effectivewindow_fg(placeholder at half alpha) attext_size,heading_sizeorstatus_size, never hardcoded sizes. Load the theme withfiles/settings.zigbefore the first frame (PDF: before its sandbox). That loader resolvesTheme.forApp()soapp_fg, menu and field overrides can differ from compositor chrome; sidebar text usessidebarForeground(). - Secrets live in
secret_input.Inputover caller-owned mlocked storage; hand-laid secret fields draw the caret withCaretOverlay. - Dead keys:
xkb_state_key_get_utf8returns nothing for them, so any code turning xkb presses into text feedsui.compose.Composefirst (shared lazy table from the user's language: ´+c is ç inpt_BR, ć elsewhere):Keyboard.composeShellfor compositor fields,deliverKeyin app clients. Reset on focus loss or Ctrl/Alt, no repeat for a consumed key (willConsume/consumed),compose.preloadbefore a sandbox closes the filesystem (PDF).tests/dead_keys.py. - text-input-v3 (IMEs): an app-side text-input client must send
disable+commitin itsleavehandler. Theenablebelongs to the surface fromenter; left enabled, the compositor sees no change when the surface is entered again, no enable event reachesinput/text_input.zig, and the input method is never activated again — composition silently stops after a window switch.tests/editor.py.
Build and platform
ui,dbusandconfigare named modules with one-way dependencies (no compositor imports). Parsing/persistence isconfig/, applying and reloadsconfig_runtime/, shared raster code (text,anim,sdf, icons)ui/.memoryowns the process allocator; executables own its shutdown.- System paths come from
install_paths:sysconfdir(/etc;/usr/local/etctargeting FreeBSD) for our own configuration andrundirfor run state, never a literal/etc/rediwmor/run/...(C gets-DREDIWM_SYSCONFDIR). Third-party locations (/etc/xdg,/etc/login.defs) stay literal. - Other processes (list, stat, parent, uids, exe) are read through
system_monitor/proc.zig; only its Linux backend touches/proc. - OS calls go through
src/platform/: use it, notpidfd_*,inotify_*,SO_PEERCRED,prctl,eventfdflags,renameat2,statxor/proc/self/exe(C and C++: itschild.h,self_exe.h,peer.h,process.h). Elsewhere call libc orstd.posix, notstd.os.linux(itsAGENTS.mdlists the exceptions). - Zig 0.17 has no
@cImport: the build translates header roots insrc/c/into modules (support.cHeaders):c(libc),c_gfx(libc, cairo, freetype, harfbuzz, fontconfig, xkbcommon),c_pulse,c_libinput,c_png,c_archive. Add a header to its root; a module links nothing, so artifacts link their libraries. A header that exists on one OS only goes under#ifdef __linux__(or__FreeBSD__) at the end oflibc.h. The roots undefine_FORTIFY_SOURCE(fortified wrappers break translate-c in ReleaseSafe) and define_GNU_SOURCE. glibc's socket calls take a transparent union, FreeBSD's a plain pointer: callstd.c.connect(fd, @ptrCast(&addr), len)(alsobind,getsockname,accept,recvfrom), neverc.connectwith.__sockaddr__. Other differences that break FreeBSD's translation:c.SIG_IGN/SIG_DFL(useplatform.process.ignoreBrokenPipesanddefaultSignal),mode_tis 16 bits (@intCastintochmod,mkdir,fchmod; shift withstd.math.Log2Int(c.mode_t)), and glibc-only names (_NL_*,statx,renameat2,CLOCK.BOOTTIME) need@hasDeclor aplatformfunction. Never translate glib/gobject/gio/librsvg; hand-declare the ABI. No runtimeparseColor()in comptime defaults. std.os.linux.*return-errnoinusize(std.os.linux.errno);std.posix.system.*are libc (std.posix.errno).- The compositor never calls GdkPixbuf. Current gdk-pixbuf starts a
sandboxed glycin loader process per decode. Wallpapers (the loader, the
Appearance strip, the desktop's own) decode in-process through
wallpaper_codec.c(libjpeg, libpng, libwebp;png.zigstays the fast path for plain PNGs): PNG, JPEG and still WebP only, chosen by content, EXIF orientation ignored, at most 16384 px per edge and 100 Mpx.nm -D --undefined-onlyonrediwmmust show nogdk_pixbuf_*;python3 tests/wallpaper_codec.pychecks that and every variant against Pillow (--sanitizeadds ASan/UBSan). The Files and Images apps still fall back to GdkPixbuf for formats their codecs lack (files/AGENTS.md). - wlroots bindings live in
src/wlr/, backed by one translation of the installed headers (src/wlr/c.h, modulewlr_c); readsrc/wlr/AGENTS.mdbefore extending them.ext.*/wp.*Wayland types need their protocol inbuild/protocols.zig. Nullable outputs/buffers are represented explicitly. Full layout checks protect embedded input devices, so no extra Pointer padding is needed.zig build test-wlrootsvalidates every exported binding and C input-device initialization; it also runs as part ofzig build test. - Debug
main.gpaisDebugAllocator: freed memory reads0xaa…(glibcMALLOC_PERTURB_:0xa5…), leaks print at exit in the compositor log. Free process-lifetime caches inmain.start's defers; state workers read during shutdown comes fromstd.heap.c_allocator. Never mix allocators. - Cairo
ARGB32is premultiplied0xAARRGGBB, like our buffers.png.zigreads only 8-bpc non-interlaced truecolour(+alpha). Read/sys/class/power_supply/*/ueventonly on uevents and the minute tick.