Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for X11 and Windows #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ edition = "2021"

[dependencies]
image = "0.24.9"
libwayshot = { git = "https://github.com/waycrate/wayshot", branch = "freeze-feat-andreas" }

[target.'cfg(target_os = "windows")'.dependencies]
win-screenshot = "4.0.10"
raylib = { version = "5.0.1", features = ["opengl_33"] }

[target.'cfg(target_os = "linux")'.dependencies]
raylib = { version = "5.0.1", features = ["wayland", "opengl_33"] }
libwayshot = { git = "https://github.com/waycrate/wayshot", branch = "freeze-feat-andreas" }
rxscreen = "0.1.7"

[features]
dev = []
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
checks = self.checks.${system};
packages = with pkgs; [
rust-analyzer
cargo-cross
];

};
Expand Down
43 changes: 33 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
use libwayshot::WayshotConnection;
#[cfg(all(not(target_os = "linux"), not(target_os = "windows")))]
compile_error!("Only linux and windows are supported");
use image::DynamicImage;
use image::ImageBuffer;
use raylib::{ffi::Image as FfiImage, prelude::*};
const SPOTLIGHT_TINT: Color = Color::new(0x00, 0x00, 0x00, 190);

#[cfg(target_os = "linux")]
fn screenshot() -> DynamicImage {
use libwayshot::WayshotConnection;
fn wayland_screnshot() -> Option<DynamicImage> {
let wayshot_connection =
WayshotConnection::new().ok()?;
Some(wayshot_connection
.screenshot_all(false)
.expect("failed to take a screenshot on wayland"))
}
fn x11_screenshot() -> Option<DynamicImage> {
rxscreen::Display::new(std::env::var("DISPLAY").ok()?).ok()?.capture().ok().and_then(|i| {
let rgb_image_pixels = unsafe {i.as_bytes()}.windows(3).flat_map(|pixels| {
let mut pixel = pixels.to_vec();
pixel.reverse();
pixel
}).collect::<Vec<u8>>();
Some(DynamicImage::ImageRgb8(ImageBuffer::from_vec(i.width() as u32, i.height() as u32, rgb_image_pixels)?))
})
}
wayland_screnshot().or_else(x11_screenshot).expect("Failed to take screnshot on both wayland and x11")
}
#[cfg(target_os = "windows")]
fn screenshot() -> DynamicImage {
let ss = win_screenshot::capture::capture_display().expect("Failed to take screenshot on windows");
DynamicImage::ImageRgba8(ImageBuffer::from_vec(ss.width, ss.height, ss.pixels).expect("invalid rgb8 format image"))
}
fn main() {
let wayshot_connection =
WayshotConnection::new().expect("failed to connect to the wayland display server");
let screenshot_image = wayshot_connection
.screenshot_all(false)
.expect("failed to take a screenshot")
.to_rgba8();
let screenshot_image = screenshot().to_rgba8();
let (width, height) = screenshot_image.dimensions();
let (mut rl, thread) = raylib::init()
.title(env!("CARGO_BIN_NAME"))
Expand Down Expand Up @@ -136,8 +160,8 @@ fn main() {

let mut d = rl.begin_drawing(&thread);
let mut mode2d = d.begin_mode2D(rl_camera);
mode2d.clear_background(SPOTLIGHT_TINT);
if enable_spotlight {
mode2d.clear_background(SPOTLIGHT_TINT);
let mouse_position = mode2d.get_mouse_position();
spotlight_shader.set_shader_value(
spotlight_tint_uniform_location,
Expand All @@ -156,7 +180,6 @@ fn main() {
let mut shader_mode = mode2d.begin_shader_mode(&spotlight_shader);
shader_mode.draw_texture(&screenshot_texture, 0, 0, Color::WHITE);
} else {
mode2d.clear_background(Color::get_color(0));
mode2d.draw_texture(&screenshot_texture, 0, 0, Color::WHITE);
}
}
Expand Down