From 5ce50b0987812a1ade2d1262e8d7df6916cfc39a Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Mon, 10 Oct 2022 21:59:44 +0100 Subject: [PATCH] refactor: tidy and format --- src/main.rs | 9 ++++-- src/modules/focused.rs | 9 ++---- src/modules/launcher/item.rs | 2 +- src/modules/launcher/mod.rs | 12 ++++---- src/{sway/mod.rs => sway.rs} | 28 +++--------------- src/sway/node.rs | 50 --------------------------------- src/wayland/client.rs | 36 +++++++++++++++--------- src/wayland/mod.rs | 2 +- src/wayland/toplevel.rs | 19 ++++++++----- src/wayland/toplevel_manager.rs | 32 ++++++++++----------- 10 files changed, 73 insertions(+), 126 deletions(-) rename src/{sway/mod.rs => sway.rs} (67%) delete mode 100644 src/sway/node.rs diff --git a/src/main.rs b/src/main.rs index 113f1e3..108013d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ async fn main() -> Result<()> { }; debug!("Loaded config file"); - if let Err(err) = await_sync(create_bars(app, &display, wayland_client, &config)) { + if let Err(err) = create_bars(app, &display, wayland_client, &config) { error!("{:?}", err); exit(2); } @@ -102,7 +102,12 @@ async fn main() -> Result<()> { } /// Creates each of the bars across each of the (configured) outputs. -async fn create_bars(app: &Application, display: &Display, wl: &WaylandClient, config: &Config) -> Result<()> { +fn create_bars( + app: &Application, + display: &Display, + wl: &WaylandClient, + config: &Config, +) -> Result<()> { let outputs = wl.outputs.as_slice(); debug!("Received {} outputs from Wayland", outputs.len()); diff --git a/src/modules/focused.rs b/src/modules/focused.rs index 2f4a6bc..39e577d 100644 --- a/src/modules/focused.rs +++ b/src/modules/focused.rs @@ -1,5 +1,5 @@ use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; -use crate::wayland::{ToplevelChange}; +use crate::wayland::ToplevelChange; use crate::{await_sync, icon, wayland}; use color_eyre::Result; use glib::Continue; @@ -51,10 +51,7 @@ impl Module for FocusedModule { }); if let Some((top, _)) = focused { - tx.try_send(ModuleUpdateEvent::Update(( - top.title.clone(), - top.app_id - )))?; + tx.try_send(ModuleUpdateEvent::Update((top.title.clone(), top.app_id)))?; } spawn(async move { @@ -67,7 +64,7 @@ impl Module for FocusedModule { let update = match event.change { ToplevelChange::Focus(focus) => focus, ToplevelChange::Title(_) => event.toplevel.active, - _ => false + _ => false, }; if update { diff --git a/src/modules/launcher/item.rs b/src/modules/launcher/item.rs index 7986982..0e4fdd9 100644 --- a/src/modules/launcher/item.rs +++ b/src/modules/launcher/item.rs @@ -4,12 +4,12 @@ use crate::icon::get_icon; use crate::modules::launcher::{ItemEvent, LauncherUpdate}; use crate::modules::ModuleUpdateEvent; use crate::popup::Popup; +use crate::wayland::ToplevelInfo; use gtk::prelude::*; use gtk::{Button, IconTheme, Image}; use std::rc::Rc; use std::sync::RwLock; use tokio::sync::mpsc::Sender; -use crate::wayland::ToplevelInfo; #[derive(Debug, Clone)] pub struct Item { diff --git a/src/modules/launcher/mod.rs b/src/modules/launcher/mod.rs index 61d8486..5070bb8 100644 --- a/src/modules/launcher/mod.rs +++ b/src/modules/launcher/mod.rs @@ -108,7 +108,7 @@ impl Module for LauncherModule { let mut items = items.lock().expect("Failed to get lock on items"); - for (window, _) in open_windows.clone().into_iter() { + for (window, _) in open_windows.clone() { let item = items.get_mut(&window.app_id); match item { Some(item) => { @@ -154,7 +154,8 @@ impl Module for LauncherModule { ToplevelChange::New => { let new_item = { let mut items = items(); - match items.get_mut(&app_id) { + let item = items.get_mut(&app_id); + match item { None => { let item: Item = window.into(); items.insert(app_id.clone(), item.clone()); @@ -180,7 +181,8 @@ impl Module for LauncherModule { ToplevelChange::Close => { let remove_item = { let mut items = items(); - match items.get_mut(&app_id) { + let item = items.get_mut(&app_id); + match item { Some(item) => { item.unmerge_toplevel(&window); @@ -239,7 +241,7 @@ impl Module for LauncherModule { send_update(LauncherUpdate::Title(app_id, window.id, title)).await?; } - _ => {} + ToplevelChange::Fullscreen(_) => {} } } @@ -290,7 +292,7 @@ impl Module for LauncherModule { .toplevels .read() .expect("Failed to get read lock on toplevels"); - let seat = wl.seats.first().unwrap(); + let seat = wl.seats.first().expect("Failed to get Wayland seat"); if let Some((_top, handle)) = toplevels.get(&id) { handle.activate(seat); }; diff --git a/src/sway/mod.rs b/src/sway.rs similarity index 67% rename from src/sway/mod.rs rename to src/sway.rs index e1c4f74..073ba47 100644 --- a/src/sway/mod.rs +++ b/src/sway.rs @@ -3,50 +3,37 @@ use color_eyre::Report; use futures_util::StreamExt; use lazy_static::lazy_static; use std::sync::Arc; -use swayipc_async::{Connection, Event, EventType, WindowEvent, WorkspaceEvent}; +use swayipc_async::{Connection, Event, EventType, WorkspaceEvent}; use tokio::spawn; use tokio::sync::broadcast::{channel, Receiver, Sender}; use tokio::sync::Mutex; use tracing::{info, trace}; -pub mod node; - pub struct SwayEventClient { workspace_tx: Sender>, _workspace_rx: Receiver>, - window_tx: Sender>, - _window_rx: Receiver>, } impl SwayEventClient { fn new() -> Self { let (workspace_tx, workspace_rx) = channel(16); - let (window_tx, window_rx) = channel(16); let workspace_tx2 = workspace_tx.clone(); - let window_tx2 = window_tx.clone(); spawn(async move { let workspace_tx = workspace_tx2; - let window_tx = window_tx2; let client = Connection::new().await?; info!("Sway IPC subscription client connected"); - let event_types = [EventType::Window, EventType::Workspace]; + let event_types = [EventType::Workspace]; let mut events = client.subscribe(event_types).await?; while let Some(event) = events.next().await { trace!("event: {:?}", event); - match event? { - Event::Workspace(ev) => { - workspace_tx.send(ev)?; - } - Event::Window(ev) => { - window_tx.send(ev)?; - } - _ => {} + if let Event::Workspace(ev) = event? { + workspace_tx.send(ev)?; }; } @@ -56,8 +43,6 @@ impl SwayEventClient { Self { workspace_tx, _workspace_rx: workspace_rx, - window_tx, - _window_rx: window_rx, } } @@ -65,11 +50,6 @@ impl SwayEventClient { pub fn subscribe_workspace(&self) -> Receiver> { self.workspace_tx.subscribe() } - - /// Gets an event receiver for window events - pub fn subscribe_window(&self) -> Receiver> { - self.window_tx.subscribe() - } } lazy_static! { diff --git a/src/sway/node.rs b/src/sway/node.rs deleted file mode 100644 index 6796170..0000000 --- a/src/sway/node.rs +++ /dev/null @@ -1,50 +0,0 @@ -use color_eyre::Result; -use swayipc_async::{Connection, Node, NodeType, ShellType}; - -pub fn get_node_id(node: &Node) -> &str { - node.app_id.as_ref().map_or_else( - || { - node.window_properties - .as_ref() - .expect("Cannot find node window properties") - .class - .as_ref() - .expect("Cannot find node name") - }, - |app_id| app_id, - ) -} - -/// Checks whether this application -/// is running under xwayland. -pub fn is_node_xwayland(node: &Node) -> bool { - node.shell == Some(ShellType::Xwayland) -} - -/// Recursively checks the provided node for any child application nodes. -/// Returns a list of any found application nodes. -fn check_node(node: Node, window_nodes: &mut Vec) { - if node.name.is_some() - && (node.node_type == NodeType::Con || node.node_type == NodeType::FloatingCon) - { - window_nodes.push(node); - } else { - node.nodes.into_iter().for_each(|node| { - check_node(node, window_nodes); - }); - - node.floating_nodes.into_iter().for_each(|node| { - check_node(node, window_nodes); - }); - } -} - -/// Gets a flat vector of all currently open windows. -pub async fn get_open_windows(client: &mut Connection) -> Result> { - let root_node = client.get_tree().await?; - - let mut window_nodes = vec![]; - check_node(root_node, &mut window_nodes); - - Ok(window_nodes) -} diff --git a/src/wayland/client.rs b/src/wayland/client.rs index 36d05f4..9763d9e 100644 --- a/src/wayland/client.rs +++ b/src/wayland/client.rs @@ -12,11 +12,11 @@ use std::time::Duration; use tokio::sync::{broadcast, oneshot}; use tokio::task::spawn_blocking; use tracing::trace; +use wayland_client::protocol::wl_seat::WlSeat; use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{ zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1, zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1, }; -use wayland_client::protocol::wl_seat::WlSeat; pub struct WaylandClient { pub outputs: Vec, @@ -30,6 +30,7 @@ impl WaylandClient { pub(super) async fn new() -> Self { let (output_tx, output_rx) = oneshot::channel(); let (seat_tx, seat_rx) = oneshot::channel(); + let (toplevel_tx, toplevel_rx) = broadcast::channel(32); let toplevel_tx2 = toplevel_tx.clone(); @@ -49,23 +50,30 @@ impl WaylandClient { .expect("Failed to send outputs out of task"); let seats = env.get_all_seats(); - seat_tx.send(seats.into_iter().map(|seat| seat.detach()).collect::>()).expect("Failed to send seats out of task"); + seat_tx + .send( + seats + .into_iter() + .map(|seat| seat.detach()) + .collect::>(), + ) + .expect("Failed to send seats out of task"); let _toplevel_manager = env.require_global::(); let _listener = listen_for_toplevels(env, move |handle, event, _ddata| { trace!("Received toplevel event: {:?}", event); - if event.change != ToplevelChange::Close { - toplevels2 - .write() - .expect("Failed to get write lock on toplevels") - .insert(event.toplevel.id, (event.toplevel.clone(), handle)); - } else { + if event.change == ToplevelChange::Close { toplevels2 .write() .expect("Failed to get write lock on toplevels") .remove(&event.toplevel.id); + } else { + toplevels2 + .write() + .expect("Failed to get write lock on toplevels") + .insert(event.toplevel.id, (event.toplevel.clone(), handle)); } toplevel_tx2 @@ -73,15 +81,17 @@ impl WaylandClient { .expect("Failed to send toplevel event"); }); - let mut event_loop = calloop::EventLoop::<()>::try_new().unwrap(); + let mut event_loop = + calloop::EventLoop::<()>::try_new().expect("Failed to create new event loop"); WaylandSource::new(queue) .quick_insert(event_loop.handle()) - .unwrap(); + .expect("Failed to insert event loop into wayland event queue"); loop { // TODO: Avoid need for duration here - can we force some event when sending requests? - event_loop.dispatch(Duration::from_millis(50), &mut ()).unwrap(); - event_loop. + event_loop + .dispatch(Duration::from_millis(50), &mut ()) + .expect("Failed to dispatch pending wayland events"); } }); @@ -109,7 +119,7 @@ impl WaylandClient { outputs .iter() - .filter_map(|output| with_output_info(output, |info| info.clone())) + .filter_map(|output| with_output_info(output, Clone::clone)) .collect() } } diff --git a/src/wayland/mod.rs b/src/wayland/mod.rs index c89ea67..fdd0357 100644 --- a/src/wayland/mod.rs +++ b/src/wayland/mod.rs @@ -18,7 +18,7 @@ use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{ pub use client::WaylandClient; /// A utility for lazy-loading globals. -/// Taken from smithay_client_toolkit where it's not exposed +/// Taken from `smithay_client_toolkit` where it's not exposed #[derive(Debug)] enum LazyGlobal { Unknown, diff --git a/src/wayland/toplevel.rs b/src/wayland/toplevel.rs index 0117ebb..d02d534 100644 --- a/src/wayland/toplevel.rs +++ b/src/wayland/toplevel.rs @@ -8,7 +8,9 @@ const STATE_ACTIVE: u32 = 2; const STATE_FULLSCREEN: u32 = 3; static COUNTER: AtomicUsize = AtomicUsize::new(1); -fn get_id() -> usize { COUNTER.fetch_add(1, Ordering::Relaxed) } +fn get_id() -> usize { + COUNTER.fetch_add(1, Ordering::Relaxed) +} #[derive(Debug, Clone, Default)] pub struct ToplevelInfo { @@ -24,7 +26,10 @@ pub struct ToplevelInfo { impl ToplevelInfo { fn new() -> Self { let id = get_id(); - Self { id, ..Default::default() } + Self { + id, + ..Default::default() + } } } @@ -99,11 +104,11 @@ where Event::Parent { parent: _ } => None, Event::Done => { assert_ne!(info.app_id, ""); - if !info.ready { + if info.ready { + None + } else { info.ready = true; Some(ToplevelChange::New) - } else { - None } } _ => unreachable!(), @@ -120,7 +125,7 @@ where } impl Toplevel { - pub fn init(handle: Main, mut callback: F) -> Self + pub fn init(handle: &Main, mut callback: F) -> Self where F: FnMut(ToplevelEvent, DispatchData) + 'static, { @@ -130,7 +135,7 @@ impl Toplevel { let mut inner = inner .write() .expect("Failed to get write lock on toplevel inner state"); - toplevel_implem(event, &mut *inner, &mut callback, ddata); + toplevel_implem(event, &mut inner, &mut callback, ddata); }); Self diff --git a/src/wayland/toplevel_manager.rs b/src/wayland/toplevel_manager.rs index ce8e9c2..97d3a39 100644 --- a/src/wayland/toplevel_manager.rs +++ b/src/wayland/toplevel_manager.rs @@ -12,7 +12,6 @@ use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{ zwlr_foreign_toplevel_manager_v1::{self, ZwlrForeignToplevelManagerV1}, }; - struct ToplevelHandlerInner { manager: LazyGlobal, registry: Option>, @@ -20,7 +19,7 @@ struct ToplevelHandlerInner { } impl ToplevelHandlerInner { - fn new() -> Self { + const fn new() -> Self { let toplevels = vec![]; Self { @@ -64,7 +63,7 @@ impl GlobalHandler for ToplevelHandler { } else { warn!( "Compositor advertised zwlr_foreign_toplevel_manager_v1 multiple times, ignoring." - ) + ); } } @@ -92,14 +91,15 @@ impl GlobalHandler for ToplevelHandler { zwlr_foreign_toplevel_manager_v1::Event::Toplevel { toplevel: handle, } => { - let toplevel = Toplevel::init(handle.clone(), move |event, ddata| { - notify_status_listeners( - &handle, - event, - ddata, - &status_listeners, - ); - }); + let toplevel = + Toplevel::init(&handle.clone(), move |event, ddata| { + notify_status_listeners( + &handle, + &event, + ddata, + &status_listeners, + ); + }); inner.toplevels.push(toplevel); } @@ -122,18 +122,16 @@ type ToplevelStatusCallback = /// Notifies the callbacks of an event on the toplevel fn notify_status_listeners( toplevel: &ZwlrForeignToplevelHandleV1, - event: ToplevelEvent, + event: &ToplevelEvent, mut ddata: DispatchData, listeners: &RefCell>>>, ) { listeners.borrow_mut().retain(|lst| { - if let Some(cb) = rc::Weak::upgrade(lst) { + rc::Weak::upgrade(lst).map_or(false, |cb| { (cb.borrow_mut())(toplevel.clone(), event.clone(), ddata.reborrow()); true - } else { - false - } - }) + }) + }); } pub struct ToplevelStatusListener {