diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 45dd2625..e67e728f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -20,7 +20,7 @@ jobs: components: clippy rustfmt - name: Install wayland dependencies run: | - pacman -Syu --noconfirm egl-wayland egl-gbm wayland base-devel mesa + pacman -Syu --noconfirm egl-wayland egl-gbm wayland base-devel mesa pango cairo - name: Build run: | diff --git a/Cargo.lock b/Cargo.lock index cec7a85a..aebcd800 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -193,6 +193,29 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" +[[package]] +name = "cairo-rs" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae50b5510d86cf96ac2370e66d8dc960882f3df179d6a5a1e52bd94a1416c0f7" +dependencies = [ + "bitflags 2.9.0", + "cairo-sys-rs", + "glib", + "libc", +] + +[[package]] +name = "cairo-sys-rs" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f18b6bb8e43c7eb0f2aac7976afe0c61b6f5fc2ab7bc4c139537ea56c92290df" +dependencies = [ + "glib-sys", + "libc", + "system-deps 7.0.3", +] + [[package]] name = "cc" version = "1.2.16" @@ -214,6 +237,16 @@ dependencies = [ "target-lexicon", ] +[[package]] +name = "cfg-expr" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d4ba6e40bd1184518716a6e1a781bf9160e286d219ccdb8ab2612e74cfe4789" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -494,6 +527,69 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "futures-channel" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-executor" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + +[[package]] +name = "futures-macro" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-macro", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "fuzzy-matcher" version = "0.3.7" @@ -550,6 +646,36 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "gio" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4f00c70f8029d84ea7572dd0e1aaa79e5329667b4c17f329d79ffb1e6277487" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-util", + "gio-sys", + "glib", + "libc", + "pin-project-lite", + "smallvec", +] + +[[package]] +name = "gio-sys" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "160eb5250a26998c3e1b54e6a3d4ea15c6c7762a6062a19a7b63eff6e2b33f9e" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps 7.0.3", + "windows-sys 0.59.0", +] + [[package]] name = "gl" version = "0.14.0" @@ -580,6 +706,61 @@ dependencies = [ "libc", ] +[[package]] +name = "glib" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "707b819af8059ee5395a2de9f2317d87a53dbad8846a2f089f0bb44703f37686" +dependencies = [ + "bitflags 2.9.0", + "futures-channel", + "futures-core", + "futures-executor", + "futures-task", + "futures-util", + "gio-sys", + "glib-macros", + "glib-sys", + "gobject-sys", + "libc", + "memchr", + "smallvec", +] + +[[package]] +name = "glib-macros" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "715601f8f02e71baef9c1f94a657a9a77c192aea6097cf9ae7e5e177cd8cde68" +dependencies = [ + "heck", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "glib-sys" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8928869a44cfdd1fccb17d6746e4ff82c8f82e41ce705aa026a52ca8dc3aefb" +dependencies = [ + "libc", + "system-deps 7.0.3", +] + +[[package]] +name = "gobject-sys" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c773a3cb38a419ad9c26c81d177d96b4b08980e8bdbbf32dace883e96e96e7e3" +dependencies = [ + "glib-sys", + "libc", + "system-deps 7.0.3", +] + [[package]] name = "hashbrown" version = "0.15.2" @@ -787,6 +968,24 @@ dependencies = [ "wayland-protocols-wlr", ] +[[package]] +name = "libwaysip" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04c500526d9e9bf4ed4cfedc5bcc73e48bf4bbe4b4a832275eb71a67fd4027d6" +dependencies = [ + "cairo-rs", + "memmap2", + "pango", + "pangocairo", + "tempfile", + "thiserror 2.0.12", + "wayland-client", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-wlr", +] + [[package]] name = "linux-raw-sys" version = "0.4.15" @@ -979,6 +1178,56 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" +[[package]] +name = "pango" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1f5dc1b8cf9bc08bfc0843a04ee0fa2e78f1e1fa4b126844a383af4f25f0ec" +dependencies = [ + "gio", + "glib", + "libc", + "pango-sys", +] + +[[package]] +name = "pango-sys" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dbb9b751673bd8fe49eb78620547973a1e719ed431372122b20abd12445bab5" +dependencies = [ + "glib-sys", + "gobject-sys", + "libc", + "system-deps 7.0.3", +] + +[[package]] +name = "pangocairo" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4690509a2fea2a6552a0ef8aa3e5f790c1365365ee0712afa1aedb39af3997b6" +dependencies = [ + "cairo-rs", + "glib", + "libc", + "pango", + "pangocairo-sys", +] + +[[package]] +name = "pangocairo-sys" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be6ac24147911a6a46783922fc288cf02f67570bc0d360e563b5b26aead6767" +dependencies = [ + "cairo-sys-rs", + "glib-sys", + "libc", + "pango-sys", + "system-deps 7.0.3", +] + [[package]] name = "paste" version = "1.0.15" @@ -1001,6 +1250,12 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkg-config" version = "0.3.32" @@ -1029,6 +1284,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "proc-macro-crate" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edce586971a4dfaa28950c6f18ed55e0406c1ab88bbce2c6f6293a7aaba73d35" +dependencies = [ + "toml_edit", +] + [[package]] name = "proc-macro2" version = "1.0.94" @@ -1149,7 +1413,7 @@ dependencies = [ "rand", "rand_chacha", "simd_helpers", - "system-deps", + "system-deps 6.2.2", "thiserror 1.0.69", "v_frame", "wasm-bindgen", @@ -1309,6 +1573,15 @@ dependencies = [ "quote", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.14.0" @@ -1338,7 +1611,20 @@ version = "6.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e535eb8dded36d55ec13eddacd30dec501792ff23a0b1682c38601b8cf2349" dependencies = [ - "cfg-expr", + "cfg-expr 0.15.8", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "system-deps" +version = "7.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d23aaf9f331227789a99e8de4c91bf46703add012bdfd45fdecdfb2975a005" +dependencies = [ + "cfg-expr 0.17.2", "heck", "pkg-config", "toml", @@ -1353,11 +1639,10 @@ checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" [[package]] name = "tempfile" -version = "3.18.0" +version = "3.19.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c317e0a526ee6120d8dabad239c8dadca62b24b6f168914bbbc8e2fb1f0e567" +checksum = "7437ac7763b9b123ccf33c338a5cc1bac6f69b45a136c19bdd8a65e3916435bf" dependencies = [ - "cfg-if", "fastrand", "getrandom 0.3.1", "once_cell", @@ -1659,6 +1944,17 @@ dependencies = [ "wayland-scanner", ] +[[package]] +name = "wayland-cursor" +version = "0.31.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a93029cbb6650748881a00e4922b076092a6a08c11e7fbdb923f064b23968c5d" +dependencies = [ + "rustix 0.38.44", + "wayland-client", + "xcursor", +] + [[package]] name = "wayland-egl" version = "0.32.5" @@ -1761,6 +2057,7 @@ dependencies = [ "flate2", "image", "libwayshot", + "libwaysip", "rustix 1.0.3", "serde", "shellexpand", @@ -1983,6 +2280,12 @@ dependencies = [ "wayland-protocols-wlr", ] +[[package]] +name = "xcursor" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef33da6b1660b4ddbfb3aef0ade110c8b8a781a3b6382fa5f2b5b040fd55f61" + [[package]] name = "xml-rs" version = "0.8.25" diff --git a/README.md b/README.md index a59bb7e6..a3d96335 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ NOTE: Read `man wayshot` for flag information. Screenshot and Crop Region: ```bash -wayshot -s +wayshot -g ``` Fullscreen: @@ -47,7 +47,7 @@ wayshot --clipboard Pick a hex color code, using ImageMagick: ```bash -wayshot -s - | convert - -format '%[pixel:p{0,0}]' txt:-|grep -E "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" -o +wayshot -g - | convert - -format '%[pixel:p{0,0}]' txt:-|grep -E "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" -o ``` # Installation diff --git a/docs/wayshot.1.scd b/docs/wayshot.1.scd index ae8c902b..a8f36646 100644 --- a/docs/wayshot.1.scd +++ b/docs/wayshot.1.scd @@ -18,7 +18,7 @@ Wayshot - Screenshot tool for compositors implementing zwlr_screencopy_v1 such a 3. '-' which sends the screenshot to stdout. - The *--clipboard* option can also be used simultaneously with any of the above to copy the image to the clipboard too. + The *--clipboard* option can also be used simultaneously with any of the above to copy the image to the clipboard too. # OPTIONS @@ -32,7 +32,7 @@ Wayshot - Screenshot tool for compositors implementing zwlr_screencopy_v1 such a Print version information. *--log-level * - Log level to be used for printing to stderr + Log level to be used for printing to stderr Possible values: trace, debug, info, warn, error Default value: info @@ -41,12 +41,12 @@ Wayshot - Screenshot tool for compositors implementing zwlr_screencopy_v1 such a Enable cursor visibility in screenshots. *--clipboard* - Copy image contents to clipboard also. + Copy image contents to clipboard also. Using this flag will cause the wayshot process to fork and persist in the background offering the image on the wayland clipboard until some other program overwrites the clipboard. *-e*, *--extension* - Set the image encoder. Without this option, encoding is either inferred from the *output* filename or defaults to png. + Set the image encoder. Without this option, encoding is either inferred from the *output* filename or defaults to png. Valid arguments: - jpg - png (Default encoder) @@ -64,13 +64,9 @@ Wayshot - Screenshot tool for compositors implementing zwlr_screencopy_v1 such a *-o*, *--output* Choose a particular display (wl_output) to screenshot. -*-s*, *--slurp*= - If this option is passed, wayshot takes a screenshot first and then uses the *slurp* program to select a portion of that screenshot - https://github.com/emersion/slurp . SLURP_ARGS are any arguments that need to be passed to *slurp*, see *slurp(1)* for more information. - - Example: - *wayshot -s="-b 11223377"* - - This takes a screenshot then calls slurp with "-b 11223377" as an argument for cropping the screenshot. +*-g*, *--geometry* + If this option is passed, wayshot takes a screenshot first and then uses + libwaysip (https://github.com/waycrate/waysip) to select a portion of that screenshot. *--file-name-format* Output file name's formatting. Refer to chrono formatting rules: https://docs.rs/chrono/latest/chrono/format/strftime/index.html. diff --git a/docs/wayshot.7.scd b/docs/wayshot.7.scd index 459edcc0..672e0c36 100644 --- a/docs/wayshot.7.scd +++ b/docs/wayshot.7.scd @@ -9,7 +9,7 @@ Wayshot - Screenshot tool for compositors implementing zwlr_screencopy_v1 such a *wayshot* [_options_] [_output_] # REGION SELECTION -wayshot -s +wayshot -g # CAPTURE FULLSCREEN @@ -30,15 +30,15 @@ wayshot -o eDP-1 # PICK A HEX COLOR CODE, USING IMAGEMAGICk -wayshot -s - | convert - -format '%[pixel:p{0,0}]' txt:-|grep -E "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" -o +wayshot -g - | convert - -format '%[pixel:p{0,0}]' txt:-|grep -E "#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" -o # PICK A HEX COLOR CODE WITHOUT USING IMAGEMAGICK -wayshot -s - --encoding ppm | tail -c 3 | od -An -tuC | xargs printf '#%02X%02X%02X\n' +wayshot -g - --encoding ppm | tail -c 3 | od -An -tuC | xargs printf '#%02X%02X%02X\n' # PICK A COLOR, USING IMAGEMAGICK -wayshot -s - | convert - -format '%[pixel:p{0,0}]' txt:- +wayshot -g - | convert - -format '%[pixel:p{0,0}]' txt:- # AUTHORS diff --git a/libwayshot/src/error.rs b/libwayshot/src/error.rs index a74dc694..c0c37be9 100644 --- a/libwayshot/src/error.rs +++ b/libwayshot/src/error.rs @@ -34,7 +34,7 @@ pub enum Error { #[error("Cannot find required wayland protocol")] ProtocolNotFound(String), #[error("error occurred in freeze callback")] - FreezeCallbackError, + FreezeCallbackError(String), #[error( "dmabuf configuration not initialized. Did you not use Wayshot::from_connection_with_dmabuf()?" )] diff --git a/libwayshot/src/lib.rs b/libwayshot/src/lib.rs index ca573823..3b358b30 100644 --- a/libwayshot/src/lib.rs +++ b/libwayshot/src/lib.rs @@ -815,11 +815,14 @@ impl WayshotConnection { /// Create a layer shell surface for each output, /// render the screen captures on them and use the callback to select a region from them - fn overlay_frames_and_select_region( + fn overlay_frames_and_select_region( &self, frames: &[(FrameCopy, FrameGuard, OutputInfo)], - callback: Box Result>, - ) -> Result { + callback: F, + ) -> Result + where + F: Fn(&WayshotConnection) -> Result, + { let mut state = LayerShellState { configured_outputs: HashSet::new(), }; @@ -914,7 +917,7 @@ impl WayshotConnection { })?; } - let callback_result = callback(); + let callback_result = callback(self); debug!("Unmapping and destroying layer shell surfaces."); for (surface, layer_shell_surface) in layer_shell_surfaces.iter() { @@ -1075,7 +1078,7 @@ impl WayshotConnection { /// unfreeze the screenshot and return the selected region. pub fn screenshot_freeze(&self, callback: F, cursor_overlay: bool) -> Result where - F: Fn() -> Result + 'static, + F: Fn(&WayshotConnection) -> Result + 'static, { self.screenshot_region_capturer(RegionCapturer::Freeze(Box::new(callback)), cursor_overlay) } diff --git a/libwayshot/src/region.rs b/libwayshot/src/region.rs index 5dcf2faf..e2f37b66 100644 --- a/libwayshot/src/region.rs +++ b/libwayshot/src/region.rs @@ -1,10 +1,13 @@ use std::cmp; use crate::{ + WayshotConnection, error::{Error, Result}, output::OutputInfo, }; +pub type FreezeCallback = Box Result>; + /// Ways to say how a region for a screenshot should be captured. pub enum RegionCapturer { /// Capture all of the given outputs. @@ -14,7 +17,7 @@ pub enum RegionCapturer { /// The outputs will be "frozen" to the user at which point the given /// callback is called to get the region to capture. This callback is often /// a user interaction to let the user select a region. - Freeze(Box Result>), + Freeze(FreezeCallback), } /// `Region` where the coordinate system is the logical coordinate system used diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 6fd49180..89c8dbbb 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -43,6 +43,7 @@ shellexpand = "3.1.0" toml = { version = "0.8.20", default-features = false, features = ["parse"] } serde = { version = "1.0.219", features = ["derive"] } dirs = "6.0.0" +libwaysip = "0.3.0" [[bin]] name = "wayshot" diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 36dd6174..d0caac67 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -38,9 +38,9 @@ pub struct Cli { #[arg(long, verbatim_doc_comment)] pub log_level: Option, - /// Arguments to call slurp with for selecting a region - #[arg(short, long, value_name = "SLURP_ARGS")] - pub slurp: Option>, + /// Region aware screenshotting + #[arg(short, long)] + pub geometry: bool, /// Enable cursor in screenshots #[arg(short, long)] @@ -56,11 +56,11 @@ pub struct Cli { pub list_outputs: bool, /// Choose a particular output/display to screenshot - #[arg(short, long, conflicts_with = "slurp")] + #[arg(short, long, conflicts_with = "geometry")] pub output: Option, /// Present a fuzzy selector for output/display selection - #[arg(long, alias = "choose-output", conflicts_with_all = ["slurp", "output"])] + #[arg(long, alias = "choose-output", conflicts_with_all = ["geometry", "output"])] pub choose_output: bool, /// Output file name's formatting. diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index 0695f45d..47a4bc97 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -1,5 +1,5 @@ use clap::ValueEnum; -use eyre::{ContextCompat, Error, Result, bail}; +use eyre::{ContextCompat, Error, bail}; use serde::{Deserialize, Serialize}; use std::{ @@ -10,46 +10,25 @@ use std::{ }; use chrono::Local; +use libwayshot::Result; use libwayshot::region::{LogicalRegion, Position, Region, Size}; -pub fn parse_geometry(g: &str) -> Result { - let tail = g.trim(); - let x_coordinate: i32; - let y_coordinate: i32; - let width: u32; - let height: u32; - - let validation_error = - "Invalid geometry provided.\nValid geometries:\n1) %d,%d %dx%d\n2) %d %d %d %d"; - - if tail.contains(',') { - // this accepts: "%d,%d %dx%d" - let (head, tail) = tail.split_once(',').wrap_err(validation_error)?; - x_coordinate = head.parse::()?; - let (head, tail) = tail.split_once(' ').wrap_err(validation_error)?; - y_coordinate = head.parse::()?; - let (head, tail) = tail.split_once('x').wrap_err(validation_error)?; - width = head.parse::()?; - height = tail.parse::()?; - } else { - // this accepts: "%d %d %d %d" - let (head, tail) = tail.split_once(' ').wrap_err(validation_error)?; - x_coordinate = head.parse::()?; - let (head, tail) = tail.split_once(' ').wrap_err(validation_error)?; - y_coordinate = head.parse::()?; - let (head, tail) = tail.split_once(' ').wrap_err(validation_error)?; - width = head.parse::()?; - height = tail.parse::()?; - } +pub fn waysip_to_region(size: libwaysip::Size, point: libwaysip::Point) -> Result { + let size: Size = Size { + width: size.width.try_into().map_err(|_| { + libwayshot::Error::FreezeCallbackError("width cannot be negative".to_string()) + })?, + height: size.height.try_into().map_err(|_| { + libwayshot::Error::FreezeCallbackError("height cannot be negative".to_string()) + })?, + }; + let position: Position = Position { + x: point.x, + y: point.y, + }; Ok(LogicalRegion { - inner: Region { - position: Position { - x: x_coordinate, - y: y_coordinate, - }, - size: Size { width, height }, - }, + inner: Region { position, size }, }) } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index bd73eea8..2112436e 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -2,18 +2,18 @@ use config::Config; use std::{ env, io::{self, BufWriter, Cursor, Write}, - process::Command, }; use clap::Parser; use eyre::{Result, bail}; -use libwayshot::{WayshotConnection, region::LogicalRegion}; +use libwayshot::WayshotConnection; mod cli; mod config; mod utils; use dialoguer::{FuzzySelect, theme::ColorfulTheme}; +use utils::waysip_to_region; use wl_clipboard_rs::copy::{MimeType, Options, Source}; @@ -121,19 +121,21 @@ fn main() -> Result<()> { return Ok(()); } - let image_buffer = if let Some(slurp_args) = cli.slurp { - let slurp_region = slurp_args.unwrap_or("".to_string()); + let image_buffer = if cli.geometry { wayshot_conn.screenshot_freeze( - move || { - || -> Result { - let slurp_output = Command::new("slurp") - .args(slurp_region.split(' ')) - .output()? - .stdout; - - utils::parse_geometry(&String::from_utf8(slurp_output)?) - }() - .map_err(|_| libwayshot::Error::FreezeCallbackError) + |w_conn| { + let info = libwaysip::get_area( + Some(libwaysip::WaysipConnection { + connection: &w_conn.conn, + globals: &w_conn.globals, + }), + libwaysip::SelectionType::Area, + ) + .map_err(|e| libwayshot::Error::FreezeCallbackError(e.to_string()))? + .ok_or(libwayshot::Error::FreezeCallbackError( + "Failed to capture the area".to_string(), + ))?; + waysip_to_region(info.size(), info.left_top_point()) }, cursor, )?