@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
rs 373 lines 12.5 KB
Raw
1 use crate::{
2 Result,
3 protocols::{
4 export::{
5 hyprland_toplevel_export_frame_v1::{
6 self as frame, HyprlandToplevelExportFrameV1 as Frame,
7 },
8 hyprland_toplevel_export_manager_v1::HyprlandToplevelExportManagerV1 as Export,
9 },
10 mapping::{
11 hyprland_toplevel_mapping_manager_v1::HyprlandToplevelMappingManagerV1 as Mapping,
12 hyprland_toplevel_window_mapping_handle_v1::{
13 self as mapping, HyprlandToplevelWindowMappingHandleV1 as Handle,
14 },
15 },
16 },
17 };
18 use smithay_client_toolkit::{
19 delegate_registry, delegate_shm,
20 registry::{ProvidesRegistryState, RegistryState},
21 shm::{
22 Shm, ShmHandler,
23 slot::{Buffer, SlotPool},
24 },
25 };
26 use std::{
27 fs::OpenOptions,
28 io::Write,
29 os::unix::fs::OpenOptionsExt,
30 path::Path,
31 time::{Duration, Instant},
32 };
33 use wayland_client::{
34 Connection, Dispatch, EventQueue, QueueHandle, WEnum, delegate_noop,
35 globals::{GlobalList, registry_queue_init},
36 protocol::wl_shm,
37 };
38 use wayland_protocols_wlr::foreign_toplevel::v1::client::{
39 zwlr_foreign_toplevel_handle_v1::{self as toplevel, ZwlrForeignToplevelHandleV1 as Toplevel},
40 zwlr_foreign_toplevel_manager_v1::{self as manager, ZwlrForeignToplevelManagerV1 as Manager},
41 };
42
43 struct Capture {
44 registry: RegistryState,
45 shm: Shm,
46 mapping: Mapping,
47 export: Export,
48 target: u64,
49 started: bool,
50 done: bool,
51 error: Option<String>,
52 pixels: Option<Pixels>,
53 inverted: bool,
54 }
55 struct Pixels {
56 pool: SlotPool,
57 buffer: Buffer,
58 width: u32,
59 height: u32,
60 stride: u32,
61 format: wl_shm::Format,
62 }
63
64 fn connect(
65 connection: Connection,
66 ) -> Result<(Connection, GlobalList, EventQueue<Capture>, Capture)> {
67 let (globals, queue) = registry_queue_init::<Capture>(&connection)?;
68 let qh = queue.handle();
69 let mapping = globals.bind::<Mapping,_,_>(&qh, 1..=1, ())
70 .map_err(|_| "Update Hyprland: its window-mapping protocol is required for exact active-window capture.")?;
71 let export = globals
72 .bind::<Export, _, _>(&qh, 2..=2, ())
73 .map_err(|_| "This Hyprland version does not support window export v2.")?;
74 let state = Capture {
75 registry: RegistryState::new(&globals),
76 shm: Shm::bind(&globals, &qh)?,
77 mapping,
78 export,
79 target: 0,
80 started: false,
81 done: false,
82 error: None,
83 pixels: None,
84 inverted: false,
85 };
86 Ok((connection, globals, queue, state))
87 }
88 pub fn check() -> Result<bool> {
89 let (_, globals, queue, _) = connect(Connection::connect_to_env()?)?;
90 let _manager: Manager = globals.bind(&queue.handle(), 1..=3, ())?;
91 Ok(globals.contents().with_list(|list| {
92 [
93 "zwlr_layer_shell_v1",
94 "wp_viewporter",
95 "wl_subcompositor",
96 "wp_presentation",
97 ]
98 .iter()
99 .all(|name| list.iter().any(|g| &g.interface == name))
100 }))
101 }
102
103 pub fn capture(address: u64, directory: &Path) -> Result<()> {
104 capture_on(Connection::connect_to_env()?, address, directory)
105 }
106
107 pub(super) fn capture_on(connection: Connection, address: u64, directory: &Path) -> Result<()> {
108 let (connection, globals, mut queue, mut state) = connect(connection)?;
109 state.target = address;
110 let _manager: Manager = globals.bind(&queue.handle(), 1..=3, ())?;
111 let deadline = Instant::now() + Duration::from_secs(15);
112 while !state.done && state.error.is_none() {
113 queue.dispatch_pending(&mut state)?;
114 if state.done || state.error.is_some() {
115 break;
116 }
117 connection.flush()?;
118 if let Some(read) = queue.prepare_read() {
119 let timeout = rustix::event::Timespec::try_from(deadline.checked_duration_since(Instant::now()).ok_or("Hyprland capture timed out. Approve its screen-sharing prompt, then try again.")?)?;
120 let mut fds = [rustix::event::PollFd::new(
121 &connection,
122 rustix::event::PollFlags::IN,
123 )];
124 if rustix::event::poll(&mut fds, Some(&timeout))? == 0 {
125 return Err("Hyprland capture timed out. Check screen-sharing permission.".into());
126 }
127 read.read()?;
128 }
129 }
130 if let Some(error) = state.error {
131 return Err(error.into());
132 }
133 let pixels = state.pixels.as_mut().ok_or("Hyprland returned no image.")?;
134 let bytes = pixels.pool.raw_data_mut(&pixels.buffer.slot());
135 let rgba = to_rgba(
136 bytes,
137 pixels.width,
138 pixels.height,
139 pixels.stride,
140 pixels.format,
141 state.inverted,
142 )?;
143 let png = encode_png(&rgba, pixels.width, pixels.height, MAX_PNG_BYTES)?;
144 OpenOptions::new()
145 .write(true)
146 .create_new(true)
147 .mode(0o600)
148 .open(directory.join("capture.png"))?
149 .write_all(&png)?;
150 Ok(())
151 }
152
153 /// The desktop app rejects larger capture PNGs (`linuxCaptureSession.ts`).
154 const MAX_PNG_BYTES: usize = 32 * 1024 * 1024;
155
156 /// Level 6 dominated capture time, so encode at the much faster level 2 and fall back
157 /// to level 6 only when level 2's slightly larger output would be rejected.
158 fn encode_png(rgba: &[u8], width: u32, height: u32, limit: usize) -> Result<Vec<u8>> {
159 for level in [2, 6] {
160 let mut png = Vec::new();
161 let mut encoder = png::Encoder::new(&mut png, width, height);
162 encoder.set_color(png::ColorType::Rgba);
163 encoder.set_depth(png::BitDepth::Eight);
164 encoder.set_deflate_compression(png::DeflateCompression::Level(level));
165 encoder.write_header()?.write_image_data(rgba)?;
166 if png.len() <= limit {
167 return Ok(png);
168 }
169 }
170 Err("Capture is too large.".into())
171 }
172
173 fn image_size(width: u32, height: u32, stride: u32) -> Result<usize> {
174 if width == 0 || height == 0 || width > 16384 || height > 16384 || stride < width * 4 {
175 return Err("Invalid capture dimensions.".into());
176 }
177 let size = u64::from(height) * u64::from(stride);
178 if size > 128 * 1024 * 1024 {
179 return Err("Capture is too large.".into());
180 }
181 Ok(size as usize)
182 }
183
184 fn to_rgba(
185 bytes: &[u8],
186 width: u32,
187 height: u32,
188 stride: u32,
189 format: wl_shm::Format,
190 inverted: bool,
191 ) -> Result<Vec<u8>> {
192 let size = image_size(width, height, stride)?;
193 if bytes.len() < size {
194 return Err("Truncated capture buffer.".into());
195 }
196 let (red, blue, alpha) = match format {
197 wl_shm::Format::Argb8888 => (2, 0, true),
198 wl_shm::Format::Xrgb8888 => (2, 0, false),
199 wl_shm::Format::Abgr8888 => (0, 2, true),
200 wl_shm::Format::Xbgr8888 => (0, 2, false),
201 _ => return Err("Hyprland returned an unsupported capture pixel format.".into()),
202 };
203 let mut rgba = vec![0; (width * height * 4) as usize];
204 for y in 0..height {
205 let row = if inverted { height - 1 - y } else { y };
206 for x in 0..width {
207 let source = (row * stride + x * 4) as usize;
208 let dest = ((y * width + x) * 4) as usize;
209 let a = if alpha { bytes[source + 3] } else { 255 };
210 for (i, channel) in [red, 1, blue].into_iter().enumerate() {
211 rgba[dest + i] = if a == 0 {
212 0
213 } else {
214 (u32::from(bytes[source + channel]) * 255 / u32::from(a)).min(255) as u8
215 };
216 }
217 rgba[dest + 3] = a;
218 }
219 }
220 Ok(rgba)
221 }
222
223 impl Dispatch<Manager, ()> for Capture {
224 fn event(
225 state: &mut Self,
226 _: &Manager,
227 event: manager::Event,
228 _: &(),
229 _: &Connection,
230 qh: &QueueHandle<Self>,
231 ) {
232 if let manager::Event::Toplevel { toplevel } = event {
233 state
234 .mapping
235 .get_window_for_toplevel_wlr(&toplevel, qh, toplevel.clone());
236 }
237 }
238 wayland_client::event_created_child!(Capture, Manager, [0 => (Toplevel, ())]);
239 }
240 impl Dispatch<Toplevel, ()> for Capture {
241 fn event(
242 _: &mut Self,
243 handle: &Toplevel,
244 event: toplevel::Event,
245 _: &(),
246 _: &Connection,
247 _: &QueueHandle<Self>,
248 ) {
249 if let toplevel::Event::Closed = event {
250 handle.destroy();
251 }
252 }
253 }
254 impl Dispatch<Handle, Toplevel> for Capture {
255 fn event(
256 state: &mut Self,
257 handle: &Handle,
258 event: mapping::Event,
259 toplevel: &Toplevel,
260 _: &Connection,
261 qh: &QueueHandle<Self>,
262 ) {
263 if let mapping::Event::WindowAddress {
264 address_hi,
265 address,
266 } = event
267 {
268 if (u64::from(address_hi) << 32 | u64::from(address)) == state.target && !state.started
269 {
270 state.started = true;
271 state
272 .export
273 .capture_toplevel_with_wlr_toplevel_handle(0, toplevel, qh, ());
274 }
275 }
276 handle.destroy();
277 }
278 }
279 impl Dispatch<Frame, ()> for Capture {
280 fn event(
281 state: &mut Self,
282 frame: &Frame,
283 event: frame::Event,
284 _: &(),
285 _: &Connection,
286 _: &QueueHandle<Self>,
287 ) {
288 if state.error.is_some() || state.done {
289 return;
290 }
291 let result = (|| -> Result<()> {
292 match event {
293 frame::Event::Buffer { format: WEnum::Value(format), width, height, stride } => {
294 let mut pool = SlotPool::new(image_size(width, height, stride)?, &state.shm)?;
295 let (buffer, _) = pool.create_buffer(width as i32, height as i32, stride as i32, format)?;
296 state.pixels = Some(Pixels { pool, buffer, width, height, stride, format });
297 }
298 frame::Event::BufferDone => {
299 let pixels = state.pixels.as_ref().ok_or("Hyprland did not offer a shared-memory image.")?;
300 frame.copy(pixels.buffer.wl_buffer(), 1);
301 }
302 frame::Event::Flags { flags: WEnum::Value(flags) } => state.inverted = flags.contains(frame::Flags::YInvert),
303 frame::Event::Ready { .. } => { state.done = true; frame.destroy(); }
304 frame::Event::Failed => return Err("Hyprland could not capture this window. Check screen-sharing permission and try again.".into()),
305 _ => {}
306 }
307 Ok(())
308 })();
309 if let Err(error) = result {
310 state.error = Some(error.to_string());
311 }
312 }
313 }
314 delegate_noop!(Capture: ignore Export);
315 delegate_noop!(Capture: ignore Mapping);
316 delegate_registry!(Capture);
317 delegate_shm!(Capture);
318 impl ProvidesRegistryState for Capture {
319 fn registry(&mut self) -> &mut RegistryState {
320 &mut self.registry
321 }
322 smithay_client_toolkit::registry_handlers!();
323 }
324 impl ShmHandler for Capture {
325 fn shm_state(&mut self) -> &mut Shm {
326 &mut self.shm
327 }
328 }
329
330 #[cfg(test)]
331 mod tests {
332 use super::*;
333 #[test]
334 fn bounds_buffer_allocations() {
335 assert!(image_size(100, 100, 399).is_err());
336 assert!(image_size(16384, 16384, 65536).is_err());
337 assert!(image_size(0, 10, 4).is_err());
338 }
339 #[test]
340 fn converts_channels_padding_flip_and_alpha() {
341 let bytes = [10, 20, 30, 255, 0, 0, 0, 0, 25, 50, 100, 128, 0, 0, 0, 0];
342 assert_eq!(
343 to_rgba(&bytes, 1, 2, 8, wl_shm::Format::Argb8888, true).unwrap(),
344 [199, 99, 49, 128, 30, 20, 10, 255]
345 );
346 assert_eq!(
347 to_rgba(&bytes, 1, 1, 8, wl_shm::Format::Xbgr8888, false).unwrap(),
348 [10, 20, 30, 255]
349 );
350 assert!(to_rgba(&bytes[..2], 1, 1, 4, wl_shm::Format::Argb8888, false).is_err());
351 }
352 #[test]
353 fn falls_back_to_smaller_png_when_fast_encode_exceeds_limit() {
354 let (width, height) = (256, 256);
355 let rgba: Vec<u8> = (0..width * height * 4)
356 .map(|i| (((i / 4) % 251) ^ (i % 7)) as u8)
357 .collect();
358 let decode = |png: &[u8]| {
359 let mut reader = png::Decoder::new(std::io::Cursor::new(png))
360 .read_info()
361 .unwrap();
362 let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
363 reader.next_frame(&mut pixels).unwrap();
364 pixels
365 };
366 let fast = encode_png(&rgba, width, height, usize::MAX).unwrap();
367 let fallback = encode_png(&rgba, width, height, fast.len() - 1).unwrap();
368 assert!(fallback.len() < fast.len());
369 assert_eq!(decode(&fast), rgba);
370 assert_eq!(decode(&fallback), rgba);
371 assert!(encode_png(&rgba, width, height, fallback.len() - 1).is_err());
372 }
373 }