Skip to content

Commit

Permalink
automatically detect outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Givralix committed Jan 1, 2024
1 parent d836796 commit 85bcbde
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 51 deletions.
52 changes: 18 additions & 34 deletions src/dbus_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,24 @@ use crate::color::Color;
use crate::wayland::Request;
use anyhow::Result;
use tokio::sync::mpsc;
use zbus::dbus_interface;
use zbus::{dbus_interface, Connection};

#[derive(Debug)]
struct Server {
pub struct Server {
tx: mpsc::Sender<Request>,
color: Color,
output_name: Option<String>,
}

pub async fn run(tx: mpsc::Sender<Request>, output_names: Vec<String>) -> Result<bool> {
let mut connection_builder = zbus::ConnectionBuilder::session()?;

let no_output_names = output_names.is_empty();
for output_name in output_names {
connection_builder = connection_builder.serve_at(
format!("/outputs/{}", output_name.replace('-', "_")),
Server {
tx: tx.clone(),
color: Default::default(),
output_name: Some(output_name),
},
)?;
}

if no_output_names {
connection_builder = connection_builder.serve_at(
"/",
Server {
tx,
color: Default::default(),
output_name: None,
},
)?;
}

match connection_builder.name("rs.wl-gammarelay")?.build().await {
Err(zbus::Error::NameTaken) => Ok(false),
pub async fn run(tx: mpsc::Sender<Request>) -> Result<Option<Connection>> {
let mut builder = zbus::ConnectionBuilder::session()?;
builder = builder.serve_at("/", new_server(tx, None))?;
builder = builder.name("rs.wl-gammarelay")?;
let session = builder.build().await;
match session {
Err(zbus::Error::NameTaken) => Ok(None),
Err(e) => Err(e.into()),
Ok(server) => {
std::mem::forget(server);
Ok(true)
}
Ok(server) => Ok(Some(server)),
}
}

Expand All @@ -59,6 +35,14 @@ impl Server {
}
}

pub fn new_server(tx: mpsc::Sender<Request>, output_name: Option<String>) -> Server {
Server {
tx,
output_name,
color: Default::default(),
}
}

#[dbus_interface(name = "rs.wl.gammarelay")]
impl Server {
#[dbus_interface(property)]
Expand Down
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use tokio::sync::mpsc;
struct Cli {
#[clap(subcommand)]
command: Option<Command>,

#[arg(long = "output-name", value_name = "OUTPUT_NAME")]
output_names: Vec<String>,
}

#[derive(Debug, Subcommand)]
Expand All @@ -30,17 +27,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let command = cli.command.unwrap_or(Command::Run);

let (tx, rx) = mpsc::channel(16);
let new_instance = dbus_server::run(tx, cli.output_names).await?;
let new_instance = dbus_server::run(tx.clone()).await?;

match command {
Command::Run => {
if new_instance {
wayland::run(rx).await?;
if let Some(instance) = new_instance {
wayland::run(rx, tx.clone(), instance).await?;
}
}
Command::Watch { format } => {
if new_instance {
tokio::try_join!(wayland::run(rx), dbus_client::watch_dbus(&format))?;
if let Some(instance) = new_instance {
tokio::try_join!(
wayland::run(rx, tx.clone(), instance),
dbus_client::watch_dbus(&format)
)?;
} else {
dbus_client::watch_dbus(&format).await?;
}
Expand Down
31 changes: 22 additions & 9 deletions src/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::os::unix::io::FromRawFd;
use tokio::sync::mpsc;

use crate::color::{colorramp_fill, Color};
use crate::dbus_server::new_server;

#[derive(Debug)]
pub enum Request {
Expand All @@ -24,7 +25,11 @@ pub enum Request {
},
}

pub async fn run(mut rx: mpsc::Receiver<Request>) -> Result<()> {
pub async fn run(
mut rx: mpsc::Receiver<Request>,
tx: mpsc::Sender<Request>,
instance: zbus::Connection,
) -> Result<()> {
let (mut conn, globals) = Connection::async_connect_and_collect_globals().await?;
conn.add_registry_cb(wl_registry_cb);

Expand All @@ -36,10 +41,13 @@ pub async fn run(mut rx: mpsc::Receiver<Request>) -> Result<()> {
.map(|output| Output::bind(&mut conn, output, gamma_manager))
.collect();

let (tx_output_names, mut rx_output_names) = mpsc::unbounded_channel();

let mut state = State {
color: Default::default(),
outputs,
gamma_manager,
tx_output_names,
};

loop {
Expand All @@ -51,20 +59,23 @@ pub async fn run(mut rx: mpsc::Receiver<Request>) -> Result<()> {
conn.dispatch_events(&mut state);
}
Some(request) = rx.recv() => {
let Request::SetColor {color, output_name} = request;
let Request::SetColor { color, output_name } = request;
state.color = color;
state
.outputs
.iter_mut()
.filter(|o|
if let Some(output_name) = &output_name {
o.name.as_ref() == Some(output_name)
} else {
true
}
)
.filter(|o| output_name.is_none() || o.name.as_ref() == output_name.as_ref())
.try_for_each(|o| o.set_color(&mut conn, color))?;
}
Some(output_name) = rx_output_names.recv() => {
instance
.object_server()
.at(
format!("/outputs/{}", output_name.replace('-', "_")),
new_server(tx.clone(), Some(output_name)),
)
.await?;
}
}
}
}
Expand All @@ -74,6 +85,7 @@ struct State {
color: Color,
outputs: Vec<Output>,
gamma_manager: ZwlrGammaControlManagerV1,
tx_output_names: mpsc::UnboundedSender<String>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -180,6 +192,7 @@ fn wl_output_cb(ctx: EventCtx<State, WlOutput>) {
.unwrap();
let name = String::from_utf8(name.into_bytes()).expect("invalid output name");
eprintln!("Output {}: name = {name:?}", output.reg_name);
ctx.state.tx_output_names.send(name.clone()).unwrap();
output.name = Some(name);
}
}

0 comments on commit 85bcbde

Please sign in to comment.