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 wayland implementation and basic dbus method to take screenshot #13

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
35 changes: 33 additions & 2 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ inotify = "0.11.0"
gamescope-wayland-client = { git = "https://github.com/ShadowBlip/gamescope-wayland-client.git", version = "0.1.0" }
gamescope-x11-client = { git = "https://github.com/ShadowBlip/gamescope-x11-client.git", rev = "9526a6e0d684d9530e8705f7f1a53efa401350f3" }
serde = "1.0.214"
nix = { version = "0.29.0", features = ["user"] }
tokio-stream = "0.1.17"
15 changes: 14 additions & 1 deletion src/gamescope/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use zbus_macros::dbus_interface;

use crate::watcher::{self, WatchEvent};

use super::xwayland;
use super::{wayland, xwayland};

/// Manager commands define all the different ways to interact with [Manager]
/// over a channel. These commands are processed in an asyncronous thread and
/// dispatched as they come in.
#[derive(Debug)]
#[allow(dead_code)]
pub enum Command {
FilesystemEvent { event: WatchEvent },
XWaylandAdded { name: String },
Expand Down Expand Up @@ -45,6 +46,18 @@ impl Manager {
}
}

/// Starts the wayland manager and adds its dbus interface
pub async fn start_wayland_manager(&self) -> Result<(), Box<dyn Error>> {
Ericky14 marked this conversation as resolved.
Show resolved Hide resolved
let path = "/org/shadowblip/Gamescope/Wayland".to_owned();
let interface = wayland::dbus::DBusInterface::new(path.clone(), self.dbus.clone()).await?;
self.dbus
.object_server()
.at(path.clone(), interface)
.await?;
log::info!("Initialized wayland manager at path:{path}");
Ok(())
}

/// Starts listening for [Command] messages to be sent from clients and
/// dispatch those events.
pub async fn run(&mut self) -> Result<(), Box<dyn Error>> {
Expand Down
181 changes: 0 additions & 181 deletions src/gamescope/wayland.rs

This file was deleted.

85 changes: 85 additions & 0 deletions src/gamescope/wayland/dbus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::error::Error;

use gamescope_wayland_client::control::gamescope_control::ScreenshotType;
use zbus::{dbus_interface, fdo, Connection};

use super::manager::{screenshot_type_from_u8, WaylandManager, WaylandMessage};

/// DBus interface implementation for Gamescope Wayland instance.
#[allow(dead_code)]
pub struct DBusInterface {
path: String,
wayland: WaylandManager,
dbus: Connection,
}

#[allow(dead_code)]
impl DBusInterface {
/// Returns a new instance of the Wayland DBus interface. Will error if
/// it cannot establish a connection.
pub async fn new(path: String, dbus: Connection) -> Result<DBusInterface, Box<dyn Error>> {
let wayland = WaylandManager::new().await?;

Ok(DBusInterface {
path,
wayland,
dbus,
})
}

/// Returns a reference to the dbus interface
async fn get_interface(&self) -> Result<zbus::InterfaceRef<DBusInterface>, zbus::Error> {
self.dbus
.clone()
.object_server()
.interface::<_, DBusInterface>(self.path.clone())
.await
}
}

#[dbus_interface(name = "org.shadowblip.Gamescope.Wayland")]
impl DBusInterface {
/// Takes a screenshot using Wayland
/// the screenshot_type u8 converts to [ScreenshotType]
/// 0 => [ScreenshotType::AllRealLayers]
/// 1 => [ScreenshotType::BasePlaneOnly]
/// 2 => [ScreenshotType::FullComposition]
/// 3 => [ScreenshotType::ScreenBuffer]
pub async fn take_screenshot(&self, file_path: String, screenshot_type: u8) -> fdo::Result<()> {
let (tx, mut rx) = tokio::sync::mpsc::channel::<Result<(), String>>(16);
let Some(screenshot_type): Option<ScreenshotType> =
screenshot_type_from_u8(screenshot_type)
else {
return Err(fdo_error("Invalid screenshot type"));
};

self.wayland
.send(WaylandMessage::CommandTakeScreenshot(
tx,
file_path,
screenshot_type,
))
.await
.map_err(|err| to_fdo_error("Error when sending screenshot command", err))?;

match rx.recv().await {
Some(Ok(_)) => {
log::info!("Screenshot taken");
Ok(())
}
Some(Err(err)) => Err(to_fdo_error("Error from screenshot command", err.into())),
None => Err(fdo_error("No response received for screenshot command")),
}
}
}

fn to_fdo_error(description: &str, err: Box<dyn Error>) -> fdo::Error {
let err = format!("{description}, err:{err:?}");
log::error!("{err}");
fdo::Error::Failed(err)
}

fn fdo_error(description: &str) -> fdo::Error {
log::error!("{description}");
fdo::Error::Failed(description.to_owned())
}
Loading
Loading