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

Move to clap_derive so CLI arguments are typed #75

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 42 additions & 6 deletions Cargo.lock

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

98 changes: 68 additions & 30 deletions libwayshot/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
use std::{
collections::HashSet,
process::exit,
sync::atomic::{AtomicBool, Ordering},
};
use wayland_client::{
delegate_noop,
globals::GlobalListContents,
protocol::{
wl_buffer::WlBuffer, wl_output, wl_output::WlOutput, wl_registry, wl_registry::WlRegistry,
wl_shm::WlShm, wl_shm_pool::WlShmPool,
wl_buffer::WlBuffer, wl_compositor::WlCompositor, wl_output, wl_output::WlOutput,
wl_registry, wl_registry::WlRegistry, wl_shm::WlShm, wl_shm_pool::WlShmPool,
wl_surface::WlSurface,
},
Connection, Dispatch, QueueHandle, WEnum,
WEnum::Value,
};
use wayland_protocols::xdg::xdg_output::zv1::client::{
zxdg_output_manager_v1::ZxdgOutputManagerV1, zxdg_output_v1, zxdg_output_v1::ZxdgOutputV1,
};
use wayland_protocols_wlr::layer_shell::v1::client::{
zwlr_layer_shell_v1::ZwlrLayerShellV1,
zwlr_layer_surface_v1::{self, ZwlrLayerSurfaceV1},
};
use wayland_protocols_wlr::screencopy::v1::client::{
zwlr_screencopy_frame_v1, zwlr_screencopy_frame_v1::ZwlrScreencopyFrameV1,
zwlr_screencopy_manager_v1::ZwlrScreencopyManagerV1,
Expand All @@ -25,11 +31,13 @@ use crate::{
screencopy::FrameFormat,
};

#[derive(Debug)]
pub struct OutputCaptureState {
pub outputs: Vec<OutputInfo>,
}

impl Dispatch<WlRegistry, ()> for OutputCaptureState {
#[tracing::instrument(skip(wl_registry, qh), ret, level = "trace")]
fn event(
state: &mut Self,
wl_registry: &WlRegistry,
Expand All @@ -56,16 +64,9 @@ impl Dispatch<WlRegistry, ()> for OutputCaptureState {
name: "".to_string(),
description: String::new(),
transform: wl_output::Transform::Normal,
dimensions: OutputPositioning {
x: 0,
y: 0,
width: 0,
height: 0,
},
mode: WlOutputMode {
width: 0,
height: 0,
},
scale: 1,
dimensions: OutputPositioning::default(),
mode: WlOutputMode::default(),
});
} else {
tracing::error!("Ignoring a wl_output with version < 4.");
Expand All @@ -76,6 +77,7 @@ impl Dispatch<WlRegistry, ()> for OutputCaptureState {
}

impl Dispatch<WlOutput, ()> for OutputCaptureState {
#[tracing::instrument(skip(wl_output), ret, level = "trace")]
fn event(
state: &mut Self,
wl_output: &WlOutput,
Expand Down Expand Up @@ -106,14 +108,19 @@ impl Dispatch<WlOutput, ()> for OutputCaptureState {
} => {
output.transform = transform;
}
_ => (),
wl_output::Event::Scale { factor } => {
output.scale = factor;
}
wl_output::Event::Done => {}
_ => {}
}
}
}

delegate_noop!(OutputCaptureState: ignore ZxdgOutputManagerV1);

impl Dispatch<ZxdgOutputV1, usize> for OutputCaptureState {
#[tracing::instrument(ret, level = "trace")]
fn event(
state: &mut Self,
_: &ZxdgOutputV1,
Expand All @@ -128,19 +135,20 @@ impl Dispatch<ZxdgOutputV1, usize> for OutputCaptureState {
zxdg_output_v1::Event::LogicalPosition { x, y } => {
output_info.dimensions.x = x;
output_info.dimensions.y = y;
tracing::debug!("Logical position event fired!");
}
zxdg_output_v1::Event::LogicalSize { width, height } => {
output_info.dimensions.width = width;
output_info.dimensions.height = height;
tracing::debug!("Logical size event fired!");
}
zxdg_output_v1::Event::Done => {}
zxdg_output_v1::Event::Name { .. } => {}
zxdg_output_v1::Event::Description { .. } => {}
_ => {}
};
}
}

/// State of the frame after attemting to copy it's data to a wl_buffer.
/// State of the frame after attempting to copy it's data to a wl_buffer.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum FrameState {
/// Compositor returned a failed event on calling `frame.copy`.
Expand All @@ -156,6 +164,7 @@ pub struct CaptureFrameState {
}

impl Dispatch<ZwlrScreencopyFrameV1, ()> for CaptureFrameState {
#[tracing::instrument(skip(frame), ret, level = "trace")]
fn event(
frame: &mut Self,
_: &ZwlrScreencopyFrameV1,
Expand All @@ -171,7 +180,6 @@ impl Dispatch<ZwlrScreencopyFrameV1, ()> for CaptureFrameState {
height,
stride,
} => {
tracing::debug!("Received Buffer event");
if let Value(f) = format {
frame.formats.push(FrameFormat {
format: f,
Expand All @@ -184,30 +192,20 @@ impl Dispatch<ZwlrScreencopyFrameV1, ()> for CaptureFrameState {
exit(1);
}
}
zwlr_screencopy_frame_v1::Event::Flags { .. } => {
tracing::debug!("Received Flags event");
}
zwlr_screencopy_frame_v1::Event::Ready { .. } => {
// If the frame is successfully copied, a “flags” and a “ready” events are sent. Otherwise, a “failed” event is sent.
// This is useful when we call .copy on the frame object.
tracing::debug!("Received Ready event");
frame.state.replace(FrameState::Finished);
}
zwlr_screencopy_frame_v1::Event::Failed => {
tracing::debug!("Received Failed event");
frame.state.replace(FrameState::Failed);
}
zwlr_screencopy_frame_v1::Event::Damage { .. } => {
tracing::debug!("Received Damage event");
}
zwlr_screencopy_frame_v1::Event::LinuxDmabuf { .. } => {
tracing::debug!("Received LinuxDmaBuf event");
}
zwlr_screencopy_frame_v1::Event::Damage { .. } => {}
zwlr_screencopy_frame_v1::Event::LinuxDmabuf { .. } => {}
zwlr_screencopy_frame_v1::Event::BufferDone => {
tracing::debug!("Received bufferdone event");
frame.buffer_done.store(true, Ordering::SeqCst);
}
_ => unreachable!(),
_ => {}
};
}
}
Expand All @@ -232,3 +230,43 @@ impl wayland_client::Dispatch<wl_registry::WlRegistry, GlobalListContents> for W
) {
}
}

pub struct LayerShellState {
pub configured_outputs: HashSet<WlOutput>,
}

delegate_noop!(LayerShellState: ignore WlCompositor);
delegate_noop!(LayerShellState: ignore WlShm);
delegate_noop!(LayerShellState: ignore WlShmPool);
delegate_noop!(LayerShellState: ignore WlBuffer);
delegate_noop!(LayerShellState: ignore ZwlrLayerShellV1);
delegate_noop!(LayerShellState: ignore WlSurface);

impl wayland_client::Dispatch<ZwlrLayerSurfaceV1, WlOutput> for LayerShellState {
// No need to instrument here, span from lib.rs is automatically used.
fn event(
state: &mut Self,
proxy: &ZwlrLayerSurfaceV1,
event: <ZwlrLayerSurfaceV1 as wayland_client::Proxy>::Event,
data: &WlOutput,
_conn: &Connection,
_qhandle: &QueueHandle<Self>,
) {
match event {
zwlr_layer_surface_v1::Event::Configure {
serial,
width: _,
height: _,
} => {
tracing::debug!("Acking configure");
state.configured_outputs.insert(data.clone());
proxy.ack_configure(serial);
tracing::trace!("Acked configure");
}
zwlr_layer_surface_v1::Event::Closed => {
tracing::debug!("Closed")
}
_ => {}
}
}
}
2 changes: 2 additions & 0 deletions libwayshot/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ pub enum Error {
NoSupportedBufferFormat,
#[error("Cannot find required wayland protocol")]
ProtocolNotFound(String),
#[error("error occurred in freeze callback")]
FreezeCallbackError,
}
8 changes: 8 additions & 0 deletions libwayshot/src/image_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ pub(crate) fn rotate_image_buffer(
width: u32,
height: u32,
) -> DynamicImage {
// TODO Better document whether width and height are before or after the transform.
// Perhaps this should be part of a cleanup of the FrameCopy struct.
let (width, height) = match transform {
Transform::_90 | Transform::_270 | Transform::Flipped90 | Transform::Flipped270 => {
(height, width)
}
_ => (width, height),
};
let final_image = match transform {
Transform::_90 => image::imageops::rotate90(&image).into(),
Transform::_180 => image::imageops::rotate180(&image).into(),
Expand Down
Loading
Loading