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/output.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/output.rs (limited to 'src/output.rs') 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; + } + } + _ => (), + } +} -- cgit v1.3.1