| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import vx11 |
| 5 | import vx11.randr |
| 6 | |
| 7 | #include "@VMODROOT/randr/tests/trap.h" |
| 8 | |
| 9 | fn C.vx11_rr_trap_begin(&vx11.Display) |
| 10 | fn C.vx11_rr_trap_end(&vx11.Display) int |
| 11 | |
| 12 | fn fails(f fn () !) bool { |
| 13 | f() or { return true } |
| 14 | return false |
| 15 | } |
| 16 | |
| 17 | fn drain(c &vx11.Connection) ![]vx11.Event { |
| 18 | c.sync()! |
| 19 | return c.poll_events()! |
| 20 | } |
| 21 | |
| 22 | fn main() { |
| 23 | mut c := vx11.connect('')! |
| 24 | defer { c.close() } |
| 25 | if '--unavailable' in os.args { |
| 26 | assert !randr.available(c)! |
| 27 | assert fails(fn [c] () ! { |
| 28 | randr.bind(c)! |
| 29 | }) |
| 30 | println('RandR unavailable: OK') |
| 31 | return |
| 32 | } |
| 33 | assert randr.available(c)! |
| 34 | rr := randr.bind(c)! |
| 35 | assert rr.supports(1, 5) && !rr.supports(rr.major + 1, 0) |
| 36 | root := c.root()! |
| 37 | d := c.raw()! |
| 38 | |
| 39 | // Screen resources (1.2) and the RandR 1.0 view of the same screen. |
| 40 | limits := rr.size_range(root)! |
| 41 | assert limits.min_width >= 1 && limits.max_width == 1024 && limits.max_height == 768 |
| 42 | res := rr.resources(root)! |
| 43 | assert res.crtcs.len >= 1 && res.outputs.len >= 1 |
| 44 | out := res.output_named('screen') or { panic('Xvfb output is missing') } |
| 45 | assert out.connected() && out.crtc != 0 && out.modes.len >= 1 |
| 46 | crtc := res.crtc(out.crtc) or { panic('active CRTC is missing') } |
| 47 | assert crtc.enabled() && crtc.rotation == randr.rotate_0 |
| 48 | assert crtc.width == 1024 && crtc.height == 768 && crtc.x == 0 && crtc.y == 0 |
| 49 | assert out.id in crtc.outputs && out.id in crtc.possible |
| 50 | original := res.mode(crtc.mode) or { panic('active mode is missing') } |
| 51 | assert original.width == 1024 && original.height == 768 && original.id in out.modes |
| 52 | polled := rr.poll_resources(root)! |
| 53 | assert polled.output_named('screen')?.id == out.id |
| 54 | info := rr.screen_info(root)! |
| 55 | assert info.sizes[info.size_index].width == 1024 && info.rotation == randr.rotate_0 |
| 56 | assert info.rotations & randr.rotate_0 != 0 && info.rates.len == info.sizes.len |
| 57 | rr.set_screen_config(root, info.size_index, randr.rotate_0, 0)! |
| 58 | |
| 59 | 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 |
| 60 | rr.watch(root, all_events)! |
| 61 | assert fails(fn [rr, root] () ! { |
| 62 | rr.watch(root, 0x100)! |
| 63 | }) |
| 64 | drain(c)! |
| 65 | |
| 66 | // Output properties: create, constrain, write, read, notify, delete. |
| 67 | prop := c.atom('VX11_RANDR_TEST')! |
| 68 | assert prop !in rr.output_properties(out.id)! |
| 69 | assert fails(fn [rr, out, prop] () ! { |
| 70 | rr.output_property_info(out.id, prop)! |
| 71 | }) |
| 72 | rr.configure_output_property(out.id, prop, false, true, [i32(-5), 100])! |
| 73 | limits_info := rr.output_property_info(out.id, prop)! |
| 74 | assert limits_info.range && !limits_info.pending && !limits_info.immutable |
| 75 | assert limits_info.values == [i32(-5), 100] |
| 76 | assert fails(fn [rr, out, prop] () ! { |
| 77 | rr.configure_output_property(out.id, prop, false, true, [i32(3)])! |
| 78 | }) |
| 79 | rr.set_output_property_words(out.id, prop, vx11.Atom(vx11.xa_integer), [u32(i32(-5)), 42])! |
| 80 | value := rr.output_property(out.id, prop, false)! |
| 81 | assert value.typ == vx11.Atom(vx11.xa_integer) && value.format == 32 |
| 82 | assert value.words == [u32(0xfffffffb), 42] |
| 83 | assert prop in rr.output_properties(out.id)! |
| 84 | text := c.atom('VX11_RANDR_TEXT')! |
| 85 | rr.set_output_property_bytes(out.id, text, vx11.Atom(vx11.xa_string), 'hello'.bytes())! |
| 86 | assert rr.output_property(out.id, text, false)!.text()! == 'hello' |
| 87 | rr.set_output_property_shorts(out.id, text, vx11.Atom(vx11.xa_cardinal), [u16(1), 65535])! |
| 88 | assert rr.output_property(out.id, text, false)!.shorts == [u16(1), 65535] |
| 89 | mut property_events := 0 |
| 90 | for event in drain(c)! { |
| 91 | if n := rr.output_property_change(event) { |
| 92 | assert n.output == out.id && n.window == root |
| 93 | if n.property == prop { property_events++ } |
| 94 | } |
| 95 | } |
| 96 | assert property_events >= 1 |
| 97 | rr.delete_output_property(out.id, prop)! |
| 98 | rr.delete_output_property(out.id, text)! |
| 99 | rr.delete_output_property(out.id, prop)! |
| 100 | assert prop !in rr.output_properties(out.id)! |
| 101 | assert fails(fn [rr, out, prop] () ! { |
| 102 | rr.output_property(out.id, prop, false)! |
| 103 | }) |
| 104 | non_desktop := c.atom(randr.property_non_desktop)! |
| 105 | if non_desktop in rr.output_properties(out.id)! { |
| 106 | assert rr.output_property(out.id, non_desktop, false)!.words == [u32(0)] |
| 107 | } |
| 108 | |
| 109 | // Primary output (1.3). |
| 110 | rr.set_primary_output(root, out.id)! |
| 111 | assert rr.primary_output(root)! == out.id |
| 112 | rr.set_primary_output(root, 0)! |
| 113 | assert rr.primary_output(root)! == 0 |
| 114 | assert fails(fn [rr, root] () ! { |
| 115 | rr.set_primary_output(root, 0x7fff_fff0)! |
| 116 | }) |
| 117 | |
| 118 | // Monitors (1.5). |
| 119 | automatic := rr.monitors(root, true)! |
| 120 | assert automatic.len == 1 && automatic[0].automatic && automatic[0].width == 1024 |
| 121 | assert c.atom_name(automatic[0].name)! == 'screen' |
| 122 | left := c.atom('VX11-LEFT')! |
| 123 | rr.set_monitor(root, randr.Monitor{ name: left, primary: true, width: 512, height: 768, mm_width: 135, mm_height: 203, outputs: [out.id] })! |
| 124 | mut found_left := false |
| 125 | for m in rr.monitors(root, false)! { |
| 126 | if m.name == left { |
| 127 | assert !m.automatic && m.primary && m.width == 512 && m.height == 768 && m.outputs == [out.id] |
| 128 | found_left = true |
| 129 | } |
| 130 | } |
| 131 | assert found_left |
| 132 | rr.delete_monitor(root, left)! |
| 133 | assert fails(fn [rr, root, left] () ! { |
| 134 | rr.delete_monitor(root, left)! |
| 135 | }) |
| 136 | output_name := c.atom('screen')! |
| 137 | assert fails(fn [rr, root, output_name] () ! { |
| 138 | rr.set_monitor(root, randr.Monitor{ name: output_name, width: 10, height: 10 })! |
| 139 | }) |
| 140 | |
| 141 | // Gamma (1.2). |
| 142 | gamma_size := rr.gamma_size(crtc.id)! |
| 143 | if gamma_size > 0 { |
| 144 | saved := rr.gamma(crtc.id)! |
| 145 | assert saved.red.len == gamma_size && saved.blue.len == gamma_size |
| 146 | ramp := []u16{len: gamma_size, init: u16(index * 65535 / (gamma_size - 1) / 2)} |
| 147 | rr.set_gamma(crtc.id, randr.Gamma{ramp, ramp, ramp})! |
| 148 | assert rr.gamma(crtc.id)!.green == ramp |
| 149 | rr.set_gamma(crtc.id, saved)! |
| 150 | assert rr.gamma(crtc.id)!.red == saved.red |
| 151 | assert fails(fn [rr, crtc] () ! { |
| 152 | rr.set_gamma(crtc.id, randr.Gamma{[u16(1)], [u16(1)], [u16(1)]})! |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | // Transforms and panning (1.3). Xvfb reports identity and rejects writes; |
| 157 | // the trap proves the requests are well formed and reach the server. |
| 158 | transform := rr.crtc_transform(crtc.id)! |
| 159 | assert transform.current_transform.matrix == randr.identity_transform().matrix |
| 160 | assert transform.current_params.len == 0 |
| 161 | C.vx11_rr_trap_begin(d) |
| 162 | rr.set_crtc_transform(crtc.id, randr.identity_transform(), 'bilinear', [])! |
| 163 | assert C.vx11_rr_trap_end(d) == vx11.bad_value |
| 164 | assert fails(fn [rr, crtc] () ! { |
| 165 | rr.set_crtc_transform(crtc.id, randr.identity_transform(), '', [i32(1)])! |
| 166 | }) |
| 167 | panning := rr.panning(root, crtc.id)! |
| 168 | assert panning.width == 0 && panning.height == 0 |
| 169 | C.vx11_rr_trap_begin(d) |
| 170 | panning_rejected := fails(fn [rr, root, crtc] () ! { |
| 171 | rr.set_panning(root, crtc.id, randr.Panning{ width: 1024, height: 768 })! |
| 172 | }) |
| 173 | assert C.vx11_rr_trap_end(d) != 0 && panning_rejected |
| 174 | |
| 175 | // Providers (1.4): Xvfb has none, so changes are rejected locally. |
| 176 | assert rr.providers(root)!.len == 0 |
| 177 | assert fails(fn [rr, root] () ! { |
| 178 | rr.set_provider_output_source(root, 1, 0)! |
| 179 | }) |
| 180 | |
| 181 | // Modes and CRTC configuration: switch to a new mode, shrink the screen, |
| 182 | // check events and Xlib's cached size, then restore everything. |
| 183 | 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 } |
| 184 | mode := rr.create_mode(root, vga)! |
| 185 | assert fails(fn [rr, root, vga] () ! { |
| 186 | rr.create_mode(root, vga)! |
| 187 | }) |
| 188 | created := rr.resources(root)!.mode(mode) or { panic('created mode is missing') } |
| 189 | assert created.name == vga.name && created.h_total == 1056 && created.flags == vga.flags |
| 190 | assert created.refresh_rate() > 60.3 && created.refresh_rate() < 60.4 |
| 191 | assert fails(fn [rr, root, crtc, mode, out] () ! { |
| 192 | rr.set_crtc(root, crtc.id, mode: mode, outputs: [out.id])! |
| 193 | }) |
| 194 | rr.add_output_mode(root, out.id, mode)! |
| 195 | assert mode in rr.resources(root)!.output(out.id)?.modes |
| 196 | for bad in [ |
| 197 | randr.CrtcConfig{ mode: mode, outputs: [out.id], rotation: randr.rotate_90 }, |
| 198 | randr.CrtcConfig{ mode: mode, outputs: [out.id], rotation: randr.rotate_0 | randr.rotate_180 }, |
| 199 | randr.CrtcConfig{ mode: mode, outputs: [out.id, out.id] }, |
| 200 | randr.CrtcConfig{ mode: mode }, |
| 201 | randr.CrtcConfig{ x: 40000, mode: mode, outputs: [out.id] }, |
| 202 | ] { |
| 203 | assert fails(fn [rr, root, crtc, bad] () ! { |
| 204 | rr.set_crtc(root, crtc.id, bad)! |
| 205 | }) |
| 206 | } |
| 207 | drain(c)! |
| 208 | mm_width := vx11.display_width_mm(d, 0) |
| 209 | mm_height := vx11.display_height_mm(d, 0) |
| 210 | rr.set_crtc(root, crtc.id, mode: mode, outputs: [out.id])! |
| 211 | assert fails(fn [rr, root, mode] () ! { |
| 212 | rr.destroy_mode(root, mode)! |
| 213 | }) |
| 214 | assert fails(fn [rr, root, out, mode] () ! { |
| 215 | rr.delete_output_mode(root, out.id, mode)! |
| 216 | }) |
| 217 | assert fails(fn [rr, root] () ! { |
| 218 | rr.set_screen_size(root, 1025, 768, 0, 0)! |
| 219 | }) |
| 220 | rr.set_screen_size(root, 800, 600, 0, 0)! |
| 221 | mut crtc_seen := false |
| 222 | mut screen_seen := false |
| 223 | for event in drain(c)! { |
| 224 | if n := rr.crtc_change(event) { |
| 225 | if n.crtc == crtc.id && n.mode == mode { |
| 226 | assert n.width == 800 && n.height == 600 |
| 227 | crtc_seen = true |
| 228 | } |
| 229 | } |
| 230 | if n := rr.screen_change(event) { |
| 231 | assert n.root == root |
| 232 | if n.width == 800 { screen_seen = true } |
| 233 | } |
| 234 | rr.update_configuration(event)! |
| 235 | } |
| 236 | assert crtc_seen && screen_seen |
| 237 | w, h := c.dimensions()! |
| 238 | assert w == 800 && h == 600 |
| 239 | current := rr.resources(root)!.crtc(crtc.id) or { panic('CRTC is missing') } |
| 240 | assert current.mode == mode && current.width == 800 |
| 241 | |
| 242 | // Disabling sends an empty output list; then grow the screen and re-enable. |
| 243 | rr.disable_crtc(root, crtc.id)! |
| 244 | disabled := rr.resources(root)! |
| 245 | assert !disabled.crtc(crtc.id)?.enabled() && disabled.output(out.id)?.crtc == 0 |
| 246 | rr.set_screen_size(root, 1024, 768, 0, 0)! |
| 247 | rr.set_crtc(root, crtc.id, mode: original.id, outputs: [out.id])! |
| 248 | rr.delete_output_mode(root, out.id, mode)! |
| 249 | rr.destroy_mode(root, mode)! |
| 250 | for event in drain(c)! { rr.update_configuration(event)! } |
| 251 | w2, h2 := c.dimensions()! |
| 252 | assert w2 == 1024 && h2 == 768 |
| 253 | assert vx11.display_width_mm(d, 0) == mm_width && vx11.display_height_mm(d, 0) == mm_height |
| 254 | restored := rr.resources(root)! |
| 255 | assert restored.mode(mode) == none |
| 256 | assert restored.crtc(crtc.id)?.mode == original.id |
| 257 | rr.watch(root, 0)! |
| 258 | |
| 259 | // The extension borrows its connection; snapshots outlive it. |
| 260 | c.close() |
| 261 | assert fails(fn [rr, root] () ! { |
| 262 | rr.resources(root)! |
| 263 | }) |
| 264 | assert fails(fn [c] () ! { |
| 265 | _ = randr.available(c)! |
| 266 | }) |
| 267 | assert restored.output_named('screen')?.name == 'screen' |
| 268 | println('RandR OK: resources, properties, primary, monitors, gamma, transforms, panning, modes, CRTCs, screen size, events, lifetimes') |
| 269 | } |
| 270 | |