From 52b97bc36d99061baac68433ea1258140807e058 Mon Sep 17 00:00:00 2001 From: Wynd Date: Mon, 18 Nov 2024 11:26:06 +0200 Subject: [PATCH] Attempt at making this work on windows systems too --- Cargo.lock | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------ Cargo.toml | 7 ++++- src/app.rs | 48 ++++++++++++++++++++++++++------- 3 files changed, 115 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a4dc1a..9d007ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -985,7 +985,7 @@ dependencies = [ "log", "presser", "thiserror", - "windows", + "windows 0.58.0", ] [[package]] @@ -2985,8 +2985,8 @@ dependencies = [ "wasm-bindgen", "web-sys", "wgpu-types", - "windows", - "windows-core", + "windows 0.58.0", + "windows-core 0.58.0", ] [[package]] @@ -3000,6 +3000,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "win-screenshot" +version = "4.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1724fbfec1c2344f8da8d2da6b0f8371f009df3291399df8c7b575a8abd121b" +dependencies = [ + "windows 0.57.0", +] + [[package]] name = "winapi" version = "0.3.9" @@ -3031,13 +3040,35 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core 0.57.0", + "windows-targets 0.52.6", +] + [[package]] name = "windows" version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" dependencies = [ - "windows-core", + "windows-core 0.58.0", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement 0.57.0", + "windows-interface 0.57.0", + "windows-result 0.1.2", "windows-targets 0.52.6", ] @@ -3047,13 +3078,24 @@ version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" dependencies = [ - "windows-implement", - "windows-interface", - "windows-result", + "windows-implement 0.58.0", + "windows-interface 0.58.0", + "windows-result 0.2.0", "windows-strings", "windows-targets 0.52.6", ] +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-implement" version = "0.58.0" @@ -3065,6 +3107,17 @@ dependencies = [ "syn", ] +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-interface" version = "0.58.0" @@ -3076,6 +3129,15 @@ dependencies = [ "syn", ] +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-result" version = "0.2.0" @@ -3091,7 +3153,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" dependencies = [ - "windows-result", + "windows-result 0.2.0", "windows-targets 0.52.6", ] @@ -3608,6 +3670,7 @@ dependencies = [ "image", "pollster", "wgpu", + "win-screenshot", "winit", ] diff --git a/Cargo.toml b/Cargo.toml index eabcf57..78b270f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ path = "src/lib.rs" unsafe_code = { level = "forbid" } [dependencies] -ashpd = { version = "0.10", default-features = false, features = ["async-std"] } winit = "0.30" image = { version = "0.25", default-features = false, features = [ "png", @@ -25,6 +24,12 @@ anyhow = "1.0" pollster = "0.4" cgmath = "0.18" +[target.'cfg(target_os = "windows")'.dependencies] +win-screenshot = { version = "4.0" } + +[target.'cfg(target_os = "linux")'.dependencies] +ashpd = { version = "0.10", default-features = false, features = ["async-std"] } + [profile.dev] codegen-backend = "cranelift" diff --git a/src/app.rs b/src/app.rs index 1b959d8..c12fd2c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,15 @@ -use std::{fs::File, io::BufReader, sync::Arc}; +use std::{ + fs::File, + io::BufReader, + path::{Path, PathBuf}, + sync::Arc, +}; +#[cfg(target_os = "linux")] use ashpd::desktop::screenshot::Screenshot; use pollster::FutureExt; +#[cfg(target_os = "windows")] +use win_screenshot::prelude::capture_display; use winit::{ application::ApplicationHandler, event::WindowEvent, @@ -18,16 +26,36 @@ pub struct App<'a> { impl<'a> ApplicationHandler for App<'a> { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { - let response = Screenshot::request() - .interactive(false) - .modal(false) - .send() - .block_on() - .expect("failed to send screenshot request") - .response() - .expect("failed to receive screenshot response"); + let mut path: Option = None; - let path = response.uri().to_file_path().unwrap(); + #[cfg(target_os = "linux")] + { + let response = Screenshot::request() + .interactive(false) + .modal(false) + .send() + .block_on() + .expect("failed to send screenshot request") + .response() + .expect("failed to receive screenshot response"); + path = Some(response.uri().to_file_path().unwrap()); + } + #[cfg(target_os = "windows")] + { + let buf = capture_display().expect("failed to capture display"); + let img = image::DynamicImage::ImageRgba8( + image::RgbaImage::from_raw(buf.width, buf.height, buf.pixels).unwrap(), + ); + let img_path = [".", "screen.png"].iter().collect(); + img.to_rgb8().save(&img_path).unwrap(); + path = Some(img_path); + } + + if path.is_none() { + panic!("Unable to capture screen"); + } + + let path = path.unwrap(); let file = File::open(&path).unwrap(); let buffer = BufReader::new(file); let mut image = image::load(buffer, image::ImageFormat::Png).unwrap();