vx11 / randr / README.md
84 lines · 69 sloc · 3.86 KB · 6de7c1113616d30335010eea79fd03d19722b474
Raw

XRandR

Optional vx11.randr import, linked with libXrandr (and libXrender, through vx11.render, for the shared Transform type). It binds every public function, type and constant in Xrandr.h and randr.h (protocol 1.0 to 1.6), and adds native helpers that return V-owned snapshots.

import vx11
import vx11.randr

mut c := vx11.connect('')!
defer { c.close() }
rr := randr.bind(c)!
root := c.root()!
resources := rr.resources(root)!
for output in resources.outputs {
    crtc := resources.crtc(output.crtc) or { continue }
    mode := resources.mode(crtc.mode) or { continue }
    println('${output.name}: ${mode.width}x${mode.height} @ ${mode.refresh_rate():.2f} Hz')
}
Version Native helpers
1.0 screen_info, set_screen_config, watch, event decoding, update_configuration
1.2 size_range, set_screen_size, resources, poll_resources, set_crtc, disable_crtc, modes, gamma, output properties
1.3 primary_output, set_primary_output, crtc_transform, set_crtc_transform, panning, set_panning
1.4 providers, PRIME source and offload sinks, provider properties
1.5 monitors, set_monitor, delete_monitor

bind negotiates the version; helpers check supports(major, minor) and return an error on older servers. Call bind before selecting events, because libXrandr converts RandR events only after the extension is queried.

Ownership and lifetimes

Extension borrows its connection and owns nothing on the server; it fails after the connection closes. Snapshots (Resources, OutputInfo, CrtcInfo, Monitor, Gamma and so on) are V copies, and their C allocations are freed before the helper returns. Output, CRTC and mode IDs persist, but snapshots go stale: query again after change events. resources does not probe hardware on 1.3+ servers; poll_resources does and can block.

Created modes, monitors and properties live on the server. Destroy a mode with destroy_mode after removing it from outputs. Monitor.name is an atom; read it with Connection.atom_name.

Raw functions keep Xlib's rules. Free XRRScreenResources, output, CRTC, provider, panning, gamma, mode and monitor results with their matching free_* function. Free property lists, property info, property data and transform attributes with vx11.free. sizes and rates return libXrandr's cache, so never free those arrays.

Errors

Configuration changes report RandR status codes as V errors. Before a request, the helpers check what the current server state can tell them: IDs exist in the screen's resources, modes belong to outputs, rotations are supported, sizes fit X11 limits and the screen's size range, properties exist before being queried, monitors exist before being deleted. This stops most fatal X errors from the default handler. Some server errors cannot be caught in advance, such as a CRTC that would not fit inside the screen, or a driver without transform or panning support. Those arrive asynchronously, like other Xlib errors. Methods that take only a CRTC, output or provider ID expect an ID from a current snapshot.

To move outputs, grow the screen with set_screen_size before enlarging CRTCs, and shrink it after reducing them. Pass every screen change event and root ConfigureNotify to update_configuration so that Connection.dimensions stays current.

Tests

v -path '..|@vlib|@vmodules' test randr
v -path '..|@vlib|@vmodules' -o /tmp/vx11-randr randr/tests/integration.v
xvfb-run -a -s '-screen 0 1024x768x24' /tmp/vx11-randr
xvfb-run -a -s '-screen 0 1024x768x24 -extension RANDR' /tmp/vx11-randr --unavailable

The integration test changes the Xvfb screen and restores it: it switches to a created mode, shrinks the screen, and checks properties, monitors, gamma, events and Xlib's cached size. Xvfb lacks transform and panning support, so those requests are checked under a temporary error trap.