From fb1a8329164f8e914b923b3b55f8b2e86a1997dd Mon Sep 17 00:00:00 2001 From: Lucy Date: Fri, 24 Jun 2022 12:56:38 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Get=20`WAYLAND=5FSOCKET`=20from=20c?= =?UTF-8?q?omp;=20run=20cosmic-panel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/comp.rs | 23 +++++++++++++++++------ src/main.rs | 13 +++++++++++-- src/panel.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/panel.rs diff --git a/src/comp.rs b/src/comp.rs index e5c3fbd..81416d1 100644 --- a/src/comp.rs +++ b/src/comp.rs @@ -1,8 +1,10 @@ +// SPDX-License-Identifier: GPL-3.0-only use crate::process::{ProcessEvent, ProcessHandler}; -use tokio::sync::mpsc::unbounded_channel; +use tokio::sync::{mpsc::unbounded_channel, oneshot}; use tokio_util::sync::CancellationToken; -pub async fn run_compositor(token: CancellationToken) { +pub async fn run_compositor(token: CancellationToken, wayland_socket_tx: oneshot::Sender) { + let mut wayland_socket_tx = Some(wayland_socket_tx); let (tx, mut rx) = unbounded_channel::(); ProcessHandler::new(tx, &token).run("cosmic-comp", vec![]); let span = info_span!("cosmic-comp"); @@ -12,12 +14,21 @@ pub async fn run_compositor(token: CancellationToken) { ProcessEvent::Started => { info!("started"); } - ProcessEvent::Stdout(line) => { + // cosmic-comp outputs everything to stderr because slog + ProcessEvent::Stdout(line) | ProcessEvent::Stderr(line) => { + if line.contains("Listening on \"") { + // Message format: Listening on "wayland-0" + if let Some(tx) = wayland_socket_tx.take() { + let socket_name = line + .split('"') + .nth(1) + .expect("failed to get WAYLAND_SOCKET"); + tx.send(socket_name.to_string()) + .expect("failed to send WAYLAND_SOCKET back to main app"); + } + } info!("{}", line); } - ProcessEvent::Stderr(line) => { - error!("{}", line); - } ProcessEvent::Ended(Some(status)) => { error!("exited with status {}", status); return; diff --git a/src/main.rs b/src/main.rs index 3399583..21dc1dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,13 @@ extern crate tracing; mod comp; +mod panel; mod process; use async_signals::Signals; use color_eyre::{eyre::WrapErr, Result}; use futures_util::StreamExt; +use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; use tracing::metadata::LevelFilter; use tracing_subscriber::{fmt, prelude::*, EnvFilter}; @@ -29,8 +31,15 @@ async fn main() -> Result<()> { info!("Starting cosmic-session"); let token = CancellationToken::new(); + let (wayland_socket_tx, wayland_socket_rx) = oneshot::channel(); + tokio::spawn(comp::run_compositor(token.child_token(), wayland_socket_tx)); + let wayland_socket = wayland_socket_rx + .await + .expect("failed to get WAYLAND_SOCKET"); + info!("got WAYLAND_SOCKET: {}", wayland_socket); + std::env::set_var("WAYLAND_SOCKET", wayland_socket); - tokio::spawn(comp::run_compositor(token.child_token())); + tokio::spawn(panel::run_panel(token.child_token())); let mut signals = Signals::new(vec![libc::SIGTERM, libc::SIGINT]).unwrap(); while let Some(signal) = signals.next().await { @@ -38,7 +47,7 @@ async fn main() -> Result<()> { libc::SIGTERM | libc::SIGINT => { info!("received request to terminate"); token.cancel(); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; + tokio::time::sleep(std::time::Duration::from_secs(2)).await; break; } _ => unreachable!("received unhandled signal {}", signal), diff --git a/src/panel.rs b/src/panel.rs new file mode 100644 index 0000000..52ae3bd --- /dev/null +++ b/src/panel.rs @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-3.0-only +use crate::process::{ProcessEvent, ProcessHandler}; +use tokio::sync::mpsc::unbounded_channel; +use tokio_util::sync::CancellationToken; + +pub async fn run_panel(token: CancellationToken) { + let (tx, mut rx) = unbounded_channel::(); + ProcessHandler::new(tx, &token).run("cosmic-panel", vec![]); + let span = info_span!("cosmic-panel"); + let _enter = span.enter(); + while let Some(event) = rx.recv().await { + match event { + ProcessEvent::Started => { + info!("started"); + } + ProcessEvent::Stdout(line) => { + info!("{}", line); + } + ProcessEvent::Stderr(line) => { + error!("{}", line); + } + ProcessEvent::Ended(Some(status)) => { + error!("exited with status {}", status); + return; + } + ProcessEvent::Ended(None) => { + error!("exited"); + return; + } + } + } +}