Add command to start/stop xwayland-satellite

This commit is contained in:
2024-11-17 15:42:56 +01:00
parent c214573f59
commit a3c9a5536a
2 changed files with 90 additions and 8 deletions

View File

@ -10,6 +10,7 @@ mod niri;
mod output; mod output;
mod pulse; mod pulse;
mod util; mod util;
mod xwayland;
#[derive(Parser)] #[derive(Parser)]
struct Opt { struct Opt {
@ -17,6 +18,22 @@ struct Opt {
command: Command, command: Command,
} }
#[derive(Clone, Subcommand)]
enum Command {
/// Commands that need to be handled differently on a per-wm basis
#[clap(flatten)]
Wm(WmCommand),
/// Get the list of audio sinks
Sinks,
/// Print battery information
Battery,
#[clap(subcommand)]
Xwayland(XwaylandCommand),
}
#[derive(Clone, Subcommand)] #[derive(Clone, Subcommand)]
enum WmCommand { enum WmCommand {
/// Get the list of workspaces /// Get the list of workspaces
@ -34,16 +51,18 @@ enum WmCommand {
} }
#[derive(Clone, Subcommand)] #[derive(Clone, Subcommand)]
enum Command { enum XwaylandCommand {
/// Commands that need to be handled differently on a per-wm basis /// Print whether xwayland is enabled.
#[clap(flatten)] IsRunning,
Wm(WmCommand),
/// Get the list of audio sinks /// Enable xwayland
Sinks, Start,
/// Print battery information /// Disable xwayland
Battery, Stop,
/// Toggle xwayland
Toggle,
} }
enum WindowManager { enum WindowManager {
@ -85,6 +104,7 @@ fn main() -> eyre::Result<()> {
println!("{}", serde_json::to_string(&pulse::get_sinks()?)?); println!("{}", serde_json::to_string(&pulse::get_sinks()?)?);
} }
Command::Battery => battery::print_info(), Command::Battery => battery::print_info(),
Command::Xwayland(cmd) => xwayland::handle(cmd)?,
} }
Ok(()) Ok(())

62
src/xwayland.rs Normal file
View File

@ -0,0 +1,62 @@
use std::process::{Command, Stdio};
use eyre::Context;
use crate::{util::CommandExt, XwaylandCommand};
pub fn handle(cmd: XwaylandCommand) -> eyre::Result<()> {
match cmd {
XwaylandCommand::IsRunning => {
println!("{}", xwayland_is_running()?);
}
XwaylandCommand::Start => {
if !xwayland_is_running()? {
start_xwayland()?;
} else {
eprintln!("xwayland is already running");
}
}
XwaylandCommand::Stop => {
if xwayland_is_running()? {
stop_xwayland()?;
} else {
eprintln!("xwayland is not running");
}
}
XwaylandCommand::Toggle => {
if !xwayland_is_running()? {
start_xwayland()?;
} else {
stop_xwayland()?;
}
}
}
Ok(())
}
/// Is xwayland running?
pub fn xwayland_is_running() -> eyre::Result<bool> {
let processes = Command::new("ps").arg("-A").just_exec()?;
Ok(processes.contains("xwayland") || processes.contains("Xwayland"))
}
pub fn start_xwayland() -> eyre::Result<()> {
let child = Command::new("xwayland-satellite")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.wrap_err("Failed to spawn xwayland-satellite")?;
drop(child);
Ok(())
}
pub fn stop_xwayland() -> eyre::Result<()> {
Command::new("killall")
.arg("xwayland-satellite")
.just_exec()?;
Ok(())
}