Skip to content

Commit

Permalink
fix: scale
Browse files Browse the repository at this point in the history
and wrong use of slurp, slurp only accept args like `-d`, it do not
accept region, just run -d is enough
  • Loading branch information
Decodetalkers committed Apr 2, 2024
1 parent 2cb242c commit 9ed1487
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libwayshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ thiserror = "1"
wayland-client = "0.31.1"
wayland-protocols = { version = "0.31.0", features = ["client", "unstable"] }
wayland-protocols-wlr = { version = "0.2.0", features = ["client"] }
tempfile = "3.10.1"
54 changes: 52 additions & 2 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod screencopy;
use std::{
collections::HashSet,
fs::File,
io::Write,
os::fd::AsFd,
sync::atomic::{AtomicBool, Ordering},
thread,
Expand Down Expand Up @@ -446,13 +447,53 @@ impl WayshotConnection {
}
};

let shm = self.globals.bind::<WlShm, _, _>(&qh, 1..=1, ())?;
for (frame_copy, frame_guard, output_info) in frames {
tracing::span!(
tracing::Level::DEBUG,
"overlay_frames::surface",
output = format!("{output_info}")
)
.in_scope(|| -> Result<()> {
let (scale, buffer) = {
let output_size = output_info.logical_region.inner.size;
if output_info.scale() % 1.0 >= 0.1 {
let file = tempfile::tempfile()?;
let image: DynamicImage = frame_copy.try_into()?;
let image = image::imageops::resize(
&image,
output_size.width,
output_size.height,
image::imageops::FilterType::Triangle,
);
init_overlay(&image, &file);
let pool = shm.create_pool(
file.as_fd(),
frame_copy
.frame_format
.byte_size()
.try_into()
.map_err(|_| Error::BufferTooSmall)?,
&qh,
(),
);

(
1,
pool.create_buffer(
0,
output_size.width as i32,
output_size.height as i32,
(output_size.width * 4) as i32,
frame_copy.frame_format.format,
&qh,
(),
),
)
} else {
(output_info.scale() as i32, frame_guard.buffer.clone())
}
};
let surface = compositor.create_surface(&qh, ());

let layer_surface = layer_shell.get_layer_surface(
Expand Down Expand Up @@ -480,8 +521,8 @@ impl WayshotConnection {
}

surface.set_buffer_transform(output_info.transform);
// surface.set_buffer_scale(output_info.scale());
surface.attach(Some(&frame_guard.buffer), 0, 0);
surface.set_buffer_scale(scale);
surface.attach(Some(&buffer), 0, 0);

debug!("Committing surface with attached buffer.");
surface.commit();
Expand Down Expand Up @@ -675,3 +716,12 @@ impl WayshotConnection {
self.screenshot_outputs(self.get_all_outputs(), cursor_overlay)
}
}

fn init_overlay(origin_image: &image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, tmp: &File) {
let mut buf = std::io::BufWriter::new(tmp);

for index in origin_image.pixels() {
buf.write_all(&index.0).unwrap();
}
buf.flush().unwrap();
}
4 changes: 2 additions & 2 deletions wayshot/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pub struct Cli {
pub log_level: tracing::Level,

/// Arguments to call slurp with for selecting a region
#[arg(short, long, value_name = "SLURP_ARGS")]
pub slurp: Option<String>,
#[arg(short, long)]
pub slurp: bool,

/// Enable cursor in screenshots
#[arg(short, long)]
Expand Down
8 changes: 2 additions & 6 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ fn main() -> Result<()> {
return Ok(());
}

let image_buffer = if let Some(slurp_region) = cli.slurp {
let slurp_region = slurp_region.clone();
let image_buffer = if cli.slurp {
wayshot_conn.screenshot_freeze(
Box::new(move || {
|| -> Result<LogicalRegion> {
let slurp_output = Command::new("slurp")
.args(slurp_region.split(' '))
.output()?
.stdout;
let slurp_output = Command::new("slurp").arg("-d").output()?.stdout;

utils::parse_geometry(&String::from_utf8(slurp_output)?)
}()
Expand Down

0 comments on commit 9ed1487

Please sign in to comment.