module main import os import vx11 import vx11.randr #include "@VMODROOT/randr/tests/trap.h" fn C.vx11_rr_trap_begin(&vx11.Display) fn C.vx11_rr_trap_end(&vx11.Display) int fn fails(f fn () !) bool { f() or { return true } return false } fn drain(c &vx11.Connection) ![]vx11.Event { c.sync()! return c.poll_events()! } fn main() { mut c := vx11.connect('')! defer { c.close() } if '--unavailable' in os.args { assert !randr.available(c)! assert fails(fn [c] () ! { randr.bind(c)! }) println('RandR unavailable: OK') return } assert randr.available(c)! rr := randr.bind(c)! assert rr.supports(1, 5) && !rr.supports(rr.major + 1, 0) root := c.root()! d := c.raw()! // Screen resources (1.2) and the RandR 1.0 view of the same screen. limits := rr.size_range(root)! assert limits.min_width >= 1 && limits.max_width == 1024 && limits.max_height == 768 res := rr.resources(root)! assert res.crtcs.len >= 1 && res.outputs.len >= 1 out := res.output_named('screen') or { panic('Xvfb output is missing') } assert out.connected() && out.crtc != 0 && out.modes.len >= 1 crtc := res.crtc(out.crtc) or { panic('active CRTC is missing') } assert crtc.enabled() && crtc.rotation == randr.rotate_0 assert crtc.width == 1024 && crtc.height == 768 && crtc.x == 0 && crtc.y == 0 assert out.id in crtc.outputs && out.id in crtc.possible original := res.mode(crtc.mode) or { panic('active mode is missing') } assert original.width == 1024 && original.height == 768 && original.id in out.modes polled := rr.poll_resources(root)! assert polled.output_named('screen')?.id == out.id info := rr.screen_info(root)! assert info.sizes[info.size_index].width == 1024 && info.rotation == randr.rotate_0 assert info.rotations & randr.rotate_0 != 0 && info.rates.len == info.sizes.len rr.set_screen_config(root, info.size_index, randr.rotate_0, 0)! all_events := randr.screen_change_notify_mask | randr.crtc_change_notify_mask | randr.output_change_notify_mask | randr.output_property_notify_mask | randr.resource_change_notify_mask rr.watch(root, all_events)! assert fails(fn [rr, root] () ! { rr.watch(root, 0x100)! }) drain(c)! // Output properties: create, constrain, write, read, notify, delete. prop := c.atom('VX11_RANDR_TEST')! assert prop !in rr.output_properties(out.id)! assert fails(fn [rr, out, prop] () ! { rr.output_property_info(out.id, prop)! }) rr.configure_output_property(out.id, prop, false, true, [i32(-5), 100])! limits_info := rr.output_property_info(out.id, prop)! assert limits_info.range && !limits_info.pending && !limits_info.immutable assert limits_info.values == [i32(-5), 100] assert fails(fn [rr, out, prop] () ! { rr.configure_output_property(out.id, prop, false, true, [i32(3)])! }) rr.set_output_property_words(out.id, prop, vx11.Atom(vx11.xa_integer), [u32(i32(-5)), 42])! value := rr.output_property(out.id, prop, false)! assert value.typ == vx11.Atom(vx11.xa_integer) && value.format == 32 assert value.words == [u32(0xfffffffb), 42] assert prop in rr.output_properties(out.id)! text := c.atom('VX11_RANDR_TEXT')! rr.set_output_property_bytes(out.id, text, vx11.Atom(vx11.xa_string), 'hello'.bytes())! assert rr.output_property(out.id, text, false)!.text()! == 'hello' rr.set_output_property_shorts(out.id, text, vx11.Atom(vx11.xa_cardinal), [u16(1), 65535])! assert rr.output_property(out.id, text, false)!.shorts == [u16(1), 65535] mut property_events := 0 for event in drain(c)! { if n := rr.output_property_change(event) { assert n.output == out.id && n.window == root if n.property == prop { property_events++ } } } assert property_events >= 1 rr.delete_output_property(out.id, prop)! rr.delete_output_property(out.id, text)! rr.delete_output_property(out.id, prop)! assert prop !in rr.output_properties(out.id)! assert fails(fn [rr, out, prop] () ! { rr.output_property(out.id, prop, false)! }) non_desktop := c.atom(randr.property_non_desktop)! if non_desktop in rr.output_properties(out.id)! { assert rr.output_property(out.id, non_desktop, false)!.words == [u32(0)] } // Primary output (1.3). rr.set_primary_output(root, out.id)! assert rr.primary_output(root)! == out.id rr.set_primary_output(root, 0)! assert rr.primary_output(root)! == 0 assert fails(fn [rr, root] () ! { rr.set_primary_output(root, 0x7fff_fff0)! }) // Monitors (1.5). automatic := rr.monitors(root, true)! assert automatic.len == 1 && automatic[0].automatic && automatic[0].width == 1024 assert c.atom_name(automatic[0].name)! == 'screen' left := c.atom('VX11-LEFT')! rr.set_monitor(root, randr.Monitor{ name: left, primary: true, width: 512, height: 768, mm_width: 135, mm_height: 203, outputs: [out.id] })! mut found_left := false for m in rr.monitors(root, false)! { if m.name == left { assert !m.automatic && m.primary && m.width == 512 && m.height == 768 && m.outputs == [out.id] found_left = true } } assert found_left rr.delete_monitor(root, left)! assert fails(fn [rr, root, left] () ! { rr.delete_monitor(root, left)! }) output_name := c.atom('screen')! assert fails(fn [rr, root, output_name] () ! { rr.set_monitor(root, randr.Monitor{ name: output_name, width: 10, height: 10 })! }) // Gamma (1.2). gamma_size := rr.gamma_size(crtc.id)! if gamma_size > 0 { saved := rr.gamma(crtc.id)! assert saved.red.len == gamma_size && saved.blue.len == gamma_size ramp := []u16{len: gamma_size, init: u16(index * 65535 / (gamma_size - 1) / 2)} rr.set_gamma(crtc.id, randr.Gamma{ramp, ramp, ramp})! assert rr.gamma(crtc.id)!.green == ramp rr.set_gamma(crtc.id, saved)! assert rr.gamma(crtc.id)!.red == saved.red assert fails(fn [rr, crtc] () ! { rr.set_gamma(crtc.id, randr.Gamma{[u16(1)], [u16(1)], [u16(1)]})! }) } // Transforms and panning (1.3). Xvfb reports identity and rejects writes; // the trap proves the requests are well formed and reach the server. transform := rr.crtc_transform(crtc.id)! assert transform.current_transform.matrix == randr.identity_transform().matrix assert transform.current_params.len == 0 C.vx11_rr_trap_begin(d) rr.set_crtc_transform(crtc.id, randr.identity_transform(), 'bilinear', [])! assert C.vx11_rr_trap_end(d) == vx11.bad_value assert fails(fn [rr, crtc] () ! { rr.set_crtc_transform(crtc.id, randr.identity_transform(), '', [i32(1)])! }) panning := rr.panning(root, crtc.id)! assert panning.width == 0 && panning.height == 0 C.vx11_rr_trap_begin(d) panning_rejected := fails(fn [rr, root, crtc] () ! { rr.set_panning(root, crtc.id, randr.Panning{ width: 1024, height: 768 })! }) assert C.vx11_rr_trap_end(d) != 0 && panning_rejected // Providers (1.4): Xvfb has none, so changes are rejected locally. assert rr.providers(root)!.len == 0 assert fails(fn [rr, root] () ! { rr.set_provider_output_source(root, 1, 0)! }) // Modes and CRTC configuration: switch to a new mode, shrink the screen, // check events and Xlib's cached size, then restore everything. vga := randr.ModeInfo{ name: 'vx11-800x600', width: 800, height: 600, dot_clock: 40_000_000, h_sync_start: 840, h_sync_end: 968, h_total: 1056, v_sync_start: 601, v_sync_end: 605, v_total: 628, flags: randr.hsync_positive | randr.vsync_positive } mode := rr.create_mode(root, vga)! assert fails(fn [rr, root, vga] () ! { rr.create_mode(root, vga)! }) created := rr.resources(root)!.mode(mode) or { panic('created mode is missing') } assert created.name == vga.name && created.h_total == 1056 && created.flags == vga.flags assert created.refresh_rate() > 60.3 && created.refresh_rate() < 60.4 assert fails(fn [rr, root, crtc, mode, out] () ! { rr.set_crtc(root, crtc.id, mode: mode, outputs: [out.id])! }) rr.add_output_mode(root, out.id, mode)! assert mode in rr.resources(root)!.output(out.id)?.modes for bad in [ randr.CrtcConfig{ mode: mode, outputs: [out.id], rotation: randr.rotate_90 }, randr.CrtcConfig{ mode: mode, outputs: [out.id], rotation: randr.rotate_0 | randr.rotate_180 }, randr.CrtcConfig{ mode: mode, outputs: [out.id, out.id] }, randr.CrtcConfig{ mode: mode }, randr.CrtcConfig{ x: 40000, mode: mode, outputs: [out.id] }, ] { assert fails(fn [rr, root, crtc, bad] () ! { rr.set_crtc(root, crtc.id, bad)! }) } drain(c)! mm_width := vx11.display_width_mm(d, 0) mm_height := vx11.display_height_mm(d, 0) rr.set_crtc(root, crtc.id, mode: mode, outputs: [out.id])! assert fails(fn [rr, root, mode] () ! { rr.destroy_mode(root, mode)! }) assert fails(fn [rr, root, out, mode] () ! { rr.delete_output_mode(root, out.id, mode)! }) assert fails(fn [rr, root] () ! { rr.set_screen_size(root, 1025, 768, 0, 0)! }) rr.set_screen_size(root, 800, 600, 0, 0)! mut crtc_seen := false mut screen_seen := false for event in drain(c)! { if n := rr.crtc_change(event) { if n.crtc == crtc.id && n.mode == mode { assert n.width == 800 && n.height == 600 crtc_seen = true } } if n := rr.screen_change(event) { assert n.root == root if n.width == 800 { screen_seen = true } } rr.update_configuration(event)! } assert crtc_seen && screen_seen w, h := c.dimensions()! assert w == 800 && h == 600 current := rr.resources(root)!.crtc(crtc.id) or { panic('CRTC is missing') } assert current.mode == mode && current.width == 800 // Disabling sends an empty output list; then grow the screen and re-enable. rr.disable_crtc(root, crtc.id)! disabled := rr.resources(root)! assert !disabled.crtc(crtc.id)?.enabled() && disabled.output(out.id)?.crtc == 0 rr.set_screen_size(root, 1024, 768, 0, 0)! rr.set_crtc(root, crtc.id, mode: original.id, outputs: [out.id])! rr.delete_output_mode(root, out.id, mode)! rr.destroy_mode(root, mode)! for event in drain(c)! { rr.update_configuration(event)! } w2, h2 := c.dimensions()! assert w2 == 1024 && h2 == 768 assert vx11.display_width_mm(d, 0) == mm_width && vx11.display_height_mm(d, 0) == mm_height restored := rr.resources(root)! assert restored.mode(mode) == none assert restored.crtc(crtc.id)?.mode == original.id rr.watch(root, 0)! // The extension borrows its connection; snapshots outlive it. c.close() assert fails(fn [rr, root] () ! { rr.resources(root)! }) assert fails(fn [c] () ! { _ = randr.available(c)! }) assert restored.output_named('screen')?.name == 'screen' println('RandR OK: resources, properties, primary, monitors, gamma, transforms, panning, modes, CRTCs, screen size, events, lifetimes') }