| 1 | // Optional XComposite window previews. Requires XComposite 0.2 and XRender. |
| 2 | module composite |
| 3 | |
| 4 | import vx11 |
| 5 | import vx11.render |
| 6 | |
| 7 | #include "@VMODROOT/composite/bridge.h" |
| 8 | |
| 9 | @[typedef] |
| 10 | struct C.Vx11CompositePreview {} |
| 11 | |
| 12 | fn C.vx11_comp_extensions(&vx11.Display) int |
| 13 | fn C.vx11_comp_preview_new(&vx11.Display, vx11.Window) &C.Vx11CompositePreview |
| 14 | fn C.vx11_comp_preview_free(&vx11.Display, &C.Vx11CompositePreview) |
| 15 | fn C.vx11_comp_preview_update(&vx11.Display, &C.Vx11CompositePreview, int, int) int |
| 16 | fn C.vx11_comp_preview_draw(&vx11.Display, &C.Vx11CompositePreview, vx11.Drawable, int, int, int, int) int |
| 17 | |
| 18 | // Preview owns its redirect request and cached server-side thumbnail, borrowing |
| 19 | // connection. Close before the connection. All methods run on one Xlib thread; |
| 20 | // capture uses a scoped synchronous X error trap for disappearing windows. |
| 21 | @[heap; noinit] |
| 22 | pub struct Preview { |
| 23 | connection &vx11.Connection |
| 24 | mut: |
| 25 | handle &C.Vx11CompositePreview = unsafe { nil } |
| 26 | } |
| 27 | |
| 28 | pub fn available(c &vx11.Connection) !bool { |
| 29 | // Keep XRender linkage confined to the optional extension modules. |
| 30 | if !render.available(c)! { return false } |
| 31 | return C.vx11_comp_extensions(c.raw()!) != 0 |
| 32 | } |
| 33 | |
| 34 | pub fn new_preview(c &vx11.Connection, window vx11.Window) !&Preview { |
| 35 | if !available(c)! { return error('window previews require XComposite 0.2 and XRender') } |
| 36 | if window == 0 { return error('invalid preview window') } |
| 37 | handle := C.vx11_comp_preview_new(c.raw()!, window) |
| 38 | if handle == unsafe { nil } { return error('cannot allocate preview') } |
| 39 | return &Preview{ connection: c, handle: handle } |
| 40 | } |
| 41 | |
| 42 | // update retains the last good image when a window is unmapped or gone. |
| 43 | // False means no image has ever been captured, so draw a placeholder. |
| 44 | pub fn (p &Preview) update(width int, height int) !bool { |
| 45 | if p.handle == unsafe { nil } { return error('preview is closed') } |
| 46 | if width < 1 || height < 1 || width > 65535 || height > 65535 { |
| 47 | return error('invalid preview dimensions') |
| 48 | } |
| 49 | return C.vx11_comp_preview_update(p.connection.raw()!, p.handle, width, height) != 0 |
| 50 | } |
| 51 | |
| 52 | // draw scales the cached image into a drawable of the default screen visual/depth. |
| 53 | // This does not refresh the source. Returns false when no cached image exists. |
| 54 | pub fn (p &Preview) draw(target vx11.Drawable, x int, y int, width int, height int) !bool { |
| 55 | if p.handle == unsafe { nil } { return error('preview is closed') } |
| 56 | if width < 1 || height < 1 || width > 65535 || height > 65535 { |
| 57 | return error('invalid preview dimensions') |
| 58 | } |
| 59 | return C.vx11_comp_preview_draw(p.connection.raw()!, p.handle, target, x, y, width, height) != 0 |
| 60 | } |
| 61 | |
| 62 | pub fn (mut p Preview) close() { |
| 63 | if p.handle != unsafe { nil } { |
| 64 | if d := p.connection.raw() { C.vx11_comp_preview_free(d, p.handle) } |
| 65 | p.handle = unsafe { nil } |
| 66 | } |
| 67 | } |
| 68 | |