From e36c1aaff019b2d4633763ddf191aa2192cba4c4 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Thu, 3 Aug 2023 13:56:14 +0300 Subject: refactor wl_output stuff into its own mod, require wl_output v4 (for name) --- src/bar.rs | 6 ++--- src/main.rs | 1 + src/output.rs | 54 ++++++++++++++++++++++++++++++++++++++++ src/state.rs | 80 ++++++++++++++--------------------------------------------- 4 files changed, 77 insertions(+), 64 deletions(-) create mode 100644 src/output.rs diff --git a/src/bar.rs b/src/bar.rs index 3a81bf2..e6ae583 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -8,6 +8,7 @@ use crate::button_manager::ButtonManager; use crate::color::Color; use crate::config::{Config, Position}; use crate::i3bar_protocol; +use crate::output::Output; use crate::pointer_btn::PointerBtn; use crate::protocol::*; use crate::shared_state::SharedState; @@ -16,8 +17,7 @@ use crate::text::{self, ComputedText, RenderOptions}; use crate::wm_info_provider::WmInfo; pub struct Bar { - pub output: WlOutput, - pub output_reg_name: u32, + pub output: Output, pub hidden: bool, pub mapped: bool, pub frame_cb: Option, @@ -61,7 +61,7 @@ impl Bar { ) -> anyhow::Result<()> { if let Some(tag) = self.tags_btns.click(x) { ss.wm_info_provider - .click_on_tag(conn, self.output, seat, tag, button); + .click_on_tag(conn, self.output.wl, seat, tag, button); } else if let Some((name, instance)) = self.blocks_btns.click(x) { if let Some(cmd) = &mut ss.status_cmd { cmd.send_click_event(&i3bar_protocol::Event { diff --git a/src/main.rs b/src/main.rs index e06b1f5..a4af365 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod button_manager; mod color; mod config; mod i3bar_protocol; +mod output; mod pointer_btn; mod protocol; mod shared_state; diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..9e1b78c --- /dev/null +++ b/src/output.rs @@ -0,0 +1,54 @@ +use wayrs_client::global::{Global, GlobalExt}; +use wayrs_client::protocol::*; +use wayrs_client::Connection; + +use crate::state::State; + +pub struct Output { + pub wl: WlOutput, + pub reg_name: u32, + pub scale: u32, +} + +impl Output { + pub fn bind(conn: &mut Connection, global: &Global) -> Self { + Self { + wl: global + .bind_with_cb(conn, 4, wl_output_cb) + .expect("could not bind wl_output"), + reg_name: global.name, + scale: 1, + } + } + + pub fn destroy(self, conn: &mut Connection) { + self.wl.release(conn); + } +} + +fn wl_output_cb( + conn: &mut Connection, + state: &mut State, + output: WlOutput, + event: wl_output::Event, +) { + match event { + wl_output::Event::Name(name) => { + let i = state + .pending_outputs + .iter() + .position(|o| o.wl == output) + .unwrap(); + let output = state.pending_outputs.swap_remove(i); + state.register_output(conn, output, name.to_str().expect("invalid output name")); + } + wl_output::Event::Scale(scale) => { + if let Some(bar) = state.bars.iter_mut().find(|bar| bar.output.wl == output) { + bar.scale = scale as u32; + } else if let Some(output) = state.pending_outputs.iter_mut().find(|o| o.wl == output) { + output.scale = scale as u32; + } + } + _ => (), + } +} diff --git a/src/state.rs b/src/state.rs index fe363d4..5ad4511 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,11 +1,12 @@ use crate::blocks_cache::BlocksCache; +use crate::output::Output; use crate::protocol::*; use crate::wm_info_provider::*; use std::fmt::Display; use std::os::unix::io::{AsRawFd, RawFd}; -use wayrs_client::global::{Global, GlobalExt, Globals, GlobalsExt}; +use wayrs_client::global::{GlobalExt, Globals, GlobalsExt}; use wayrs_client::proxy::Proxy; use wayrs_client::Connection; use wayrs_utils::cursor::{CursorImage, CursorShape, CursorTheme, ThemedPointer}; @@ -27,7 +28,7 @@ pub struct State { pointers: Vec, // Outputs that haven't yet advertised their names - pending_outputs: Vec, + pub pending_outputs: Vec, pub hidden: bool, pub bars: Vec, @@ -50,12 +51,6 @@ struct Pointer { scroll_frame: ScrollFrame, } -struct PendingOutput { - wl: WlOutput, - reg_name: u32, - scale: u32, -} - impl State { pub fn new(conn: &mut Connection, globals: &Globals) -> Self { let mut error = Ok(()); @@ -86,7 +81,11 @@ impl State { seats: Seats::bind(conn, globals), pointers: Vec::new(), - pending_outputs: Vec::new(), + pending_outputs: globals + .iter() + .filter(|g| g.is::()) + .map(|g| Output::bind(conn, g)) + .collect(), hidden: false, bars: Vec::new(), @@ -103,11 +102,6 @@ impl State { default_cursor, }; - globals - .iter() - .filter(|g| g.is::()) - .for_each(|g| this.bind_output(conn, g)); - if let Err(e) = error { this.set_error(conn, e.to_string()); } @@ -145,17 +139,7 @@ impl State { .map(|cmd| cmd.output.as_raw_fd()) } - fn bind_output(&mut self, conn: &mut Connection, global: &Global) { - self.pending_outputs.push(PendingOutput { - wl: global - .bind_with_cb(conn, 2..=4, wl_output_cb) - .expect("could not bind wl_output"), - reg_name: global.name, - scale: 1, - }); - } - - fn register_output(&mut self, conn: &mut Connection, output: PendingOutput, name: &str) { + pub fn register_output(&mut self, conn: &mut Connection, output: Output, name: &str) { if !self.shared_state.config.output_enabled(name) { return; } @@ -180,8 +164,7 @@ impl State { ); let mut bar = Bar { - output: output.wl, - output_reg_name: output.reg_name, + output, hidden: self.hidden, mapped: false, frame_cb: None, @@ -213,10 +196,8 @@ impl State { bar.layer_surface.destroy(conn); self.shared_state .wm_info_provider - .output_removed(conn, bar.output); - if bar.output.version() >= 3 { - bar.output.release(conn); - } + .output_removed(conn, bar.output.wl); + bar.output.destroy(conn); } pub fn toggle_visibility(&mut self, conn: &mut Connection) { @@ -263,13 +244,13 @@ impl SeatHandler for State { fn wl_registry_cb(conn: &mut Connection, state: &mut State, event: &wl_registry::Event) { match event { wl_registry::Event::Global(global) if global.is::() => { - state.bind_output(conn, global); + state.pending_outputs.push(Output::bind(conn, global)); } wl_registry::Event::GlobalRemove(name) => { if let Some(bar_index) = state .bars .iter() - .position(|bar| bar.output_reg_name == *name) + .position(|bar| bar.output.reg_name == *name) { state.drop_bar(conn, bar_index); } @@ -278,33 +259,6 @@ fn wl_registry_cb(conn: &mut Connection, state: &mut State, event: &wl_re } } -fn wl_output_cb( - conn: &mut Connection, - state: &mut State, - output: WlOutput, - event: wl_output::Event, -) { - match event { - wl_output::Event::Name(name) => { - let i = state - .pending_outputs - .iter() - .position(|o| o.wl == output) - .unwrap(); - let output = state.pending_outputs.swap_remove(i); - state.register_output(conn, output, name.to_str().expect("invalid output name")); - } - wl_output::Event::Scale(scale) => { - if let Some(bar) = state.bars.iter_mut().find(|bar| bar.output == output) { - bar.scale = scale as u32; - } else if let Some(output) = state.pending_outputs.iter_mut().find(|o| o.wl == output) { - output.scale = scale as u32; - } - } - _ => (), - } -} - fn layer_surface_cb( conn: &mut Connection, state: &mut State, @@ -479,7 +433,11 @@ fn fractional_scale_cb( } fn wm_info_cb(conn: &mut Connection, state: &mut State, output: WlOutput, info: WmInfo) { - let bar = state.bars.iter_mut().find(|b| b.output == output).unwrap(); + let bar = state + .bars + .iter_mut() + .find(|b| b.output.wl == output) + .unwrap(); bar.set_wm_info(info); bar.request_frame(conn); } -- cgit v1.3.1