From b188bc714614406935d8bb88a719adab2dfce32f Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Tue, 27 Sep 2022 20:24:16 +0100 Subject: [PATCH] feat: initial support for running outside sway Progress is being tracked in #18. Currently the workspaces, focused and launcher modules are not supported. --- Cargo.lock | 87 +++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 ++- src/main.rs | 18 +++-------- src/wayland.rs | 49 ++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 15 deletions(-) create mode 100644 src/wayland.rs diff --git a/Cargo.lock b/Cargo.lock index d31563b..33ca2ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -594,6 +594,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + [[package]] name = "either" version = "1.7.0" @@ -1133,6 +1139,8 @@ dependencies = [ "tracing-error", "tracing-subscriber", "walkdir", + "wayland-client", + "wayland-protocols", ] [[package]] @@ -1281,6 +1289,18 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "195cdbc1741b8134346d515b3a56a1c94b0912758009cfd53f99ea0f57b065fc" +dependencies = [ + "bitflags", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "nom" version = "7.1.1" @@ -2280,6 +2300,65 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wayland-client" +version = "0.29.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f3b068c05a039c9f755f881dc50f01732214f5685e379829759088967c46715" +dependencies = [ + "bitflags", + "downcast-rs", + "libc", + "nix 0.24.2", + "wayland-commons", + "wayland-scanner", + "wayland-sys", +] + +[[package]] +name = "wayland-commons" +version = "0.29.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" +dependencies = [ + "nix 0.24.2", + "once_cell", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-protocols" +version = "0.29.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b950621f9354b322ee817a23474e479b34be96c2e909c14f7bc0100e9a970bc6" +dependencies = [ + "bitflags", + "wayland-client", + "wayland-commons", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.29.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f4303d8fa22ab852f789e75a967f0a2cdc430a607751c0499bada3e451cbd53" +dependencies = [ + "proc-macro2", + "quote", + "xml-rs", +] + +[[package]] +name = "wayland-sys" +version = "0.29.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be12ce1a3c39ec7dba25594b97b42cb3195d54953ddb9d3d95a7c3902bc6e9d4" +dependencies = [ + "pkg-config", +] + [[package]] name = "wepoll-ffi" version = "0.1.2" @@ -2363,6 +2442,12 @@ version = "0.36.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +[[package]] +name = "xml-rs" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3" + [[package]] name = "zbus" version = "2.3.2" @@ -2386,7 +2471,7 @@ dependencies = [ "futures-util", "hex", "lazy_static", - "nix", + "nix 0.23.1", "once_cell", "ordered-stream", "rand", diff --git a/Cargo.toml b/Cargo.toml index b4863ad..9d7d189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,4 +35,6 @@ walkdir = "2.3.2" notify = "5.0.0" mpd_client = "1.0.0" swayipc-async = { git = "https://github.com/JakeStanger/swayipc-rs.git", branch = "feat/derive-clone" } -sysinfo = "0.26.2" \ No newline at end of file +sysinfo = "0.26.2" +wayland-client = "0.29.5" +wayland-protocols = { version = "0.29.5", features=["unstable_protocols", "client"] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 419ea78..da391cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ mod modules; mod popup; mod style; mod sway; +mod wayland; use crate::bar::create_bar; use crate::config::{Config, MonitorConfig}; @@ -100,25 +101,16 @@ async fn main() -> Result<()> { /// Creates each of the bars across each of the (configured) outputs. async fn create_bars(app: &Application, display: &Display, config: &Config) -> Result<()> { - let outputs = { - let sway = get_client().await; - let mut sway = sway.lock().await; + let outputs = wayland::get_output_names(); - let outputs = sway.get_outputs().await; - - match outputs { - Ok(outputs) => Ok(outputs), - Err(err) => Err(err), - } - }?; - - debug!("Received {} outputs from Sway IPC", outputs.len()); + debug!("Received {} outputs from Wayland", outputs.len()); + debug!("Output names: {:?}", outputs); let num_monitors = display.n_monitors(); for i in 0..num_monitors { let monitor = display.monitor(i).ok_or_else(|| Report::msg("GTK and Sway are reporting a different number of outputs - this is a severe bug and should never happen"))?; - let monitor_name = &outputs.get(i as usize).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?.name; + let monitor_name = outputs.get(i as usize).ok_or_else(|| Report::msg("GTK and Sway are reporting a different set of outputs - this is a severe bug and should never happen"))?; info!("Creating bar on '{}'", monitor_name); diff --git a/src/wayland.rs b/src/wayland.rs new file mode 100644 index 0000000..6159038 --- /dev/null +++ b/src/wayland.rs @@ -0,0 +1,49 @@ +use std::cell::RefCell; +use std::rc::Rc; +use wayland_client::protocol::wl_output::{self, Event}; +use wayland_client::{global_filter, Display as WlDisplay, GlobalManager, Main}; + +pub fn get_output_names() -> Vec { + // Connect to the server + let display = WlDisplay::connect_to_env().unwrap(); + + let mut event_queue = display.create_event_queue(); + + let attached_display = (*display).clone().attach(event_queue.token()); + + let outputs = Rc::new(RefCell::new(Vec::::new())); + + let _globals = { + let outputs = outputs.clone(); + GlobalManager::new_with_cb(&attached_display, { + global_filter!([ + wl_output::WlOutput, + 4, + move |output: Main, _: DispatchData| { + let outputs = outputs.clone(); + + output.quick_assign(move |_, event, _| match event { + Event::Name { name: title } => { + let outputs = outputs.clone(); + outputs.as_ref().borrow_mut().push(title); + } + _ => {} + }) + } + ]) + }) + }; + + // A roundtrip synchronization to make sure the server received our registry + // creation and sent us the global list + event_queue + .sync_roundtrip(&mut (), |_, _, _| unreachable!()) + .unwrap(); + + // for some reason we need to call this twice? + event_queue + .sync_roundtrip(&mut (), |_, _, _| unreachable!()) + .unwrap(); + + outputs.take() +}