diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-03-23 20:47:31 +0200 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-03-23 20:47:31 +0200 |
| commit | 1e73ed219792dcb59ec51f744e6dead5fcc06928 (patch) | |
| tree | c512df5bd82b0a3b80d0c7ef8c18b292c994d420 | |
| parent | 3f160dfcc16cc742916d2266820133af40714dfe (diff) | |
| download | i3bar-mango-1e73ed219792dcb59ec51f744e6dead5fcc06928.tar.gz | |
use wayrs_utils::outptus
| -rw-r--r-- | Cargo.lock | 4 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/bar.rs | 1 | ||||
| -rw-r--r-- | src/state.rs | 167 |
4 files changed, 77 insertions, 97 deletions
@@ -966,9 +966,9 @@ dependencies = [ [[package]] name = "wayrs-utils" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdfe083c28bb9ae239fb1fcbfa73b9386b29a26bb621f3df1ebd82ea44c04eb3" +checksum = "fe669d651d2409b50ac3accad4a12ba0fe419c122cb91c0efe90954d8c489002" dependencies = [ "memmap2", "shmemfdrs", @@ -16,7 +16,7 @@ memchr = "2" thiserror = "1.0.38" wayrs-client = { version = "0.4", features = ["tokio"] } -wayrs-utils = { version = "0.1", features = ["cursor", "shm_alloc", "seats"] } +wayrs-utils = { version = "0.2", features = ["cursor", "shm_alloc", "seats", "outputs"] } wayrs-protocols = { version = "0.4", features = ["wlr-layer-shell-unstable-v1", "viewporter", "fractional-scale-v1"] } [dependencies.tokio] @@ -19,7 +19,6 @@ use crate::wm_info_provider::WmInfo; pub struct Bar { pub output: WlOutput, - pub output_reg_name: u32, pub configured: bool, pub frame_cb: Option<WlCallback>, pub width: u32, diff --git a/src/state.rs b/src/state.rs index 02a9e80..6a6db41 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,9 +5,10 @@ use crate::wm_info_provider::*; use std::future::pending; use wayrs_client::connection::Connection; -use wayrs_client::global::{Global, GlobalExt, Globals, GlobalsExt}; +use wayrs_client::global::{Globals, GlobalsExt}; use wayrs_client::proxy::Proxy; use wayrs_utils::cursor::CursorTheme; +use wayrs_utils::outputs::{OutputHandler, Outputs}; use wayrs_utils::seats::{SeatHandler, Seats}; use wayrs_utils::shm_alloc::ShmAlloc; @@ -25,6 +26,7 @@ pub struct State { seats: Seats, pointers: Vec<Pointer>, + outptus: Outputs, pub bars: Vec<Bar>, pub shared_state: SharedState, @@ -57,8 +59,6 @@ impl State { .as_ref() .and_then(|cmd| StatusCmd::new(cmd).map_err(|e| error = Err(e)).ok()); - conn.add_registry_cb(wl_registry_cb); - let wl_shm = globals.bind(conn, 1..=1).expect("could not bind wl_shm"); let mut cursor_theme = CursorTheme::new(None, None); @@ -76,6 +76,7 @@ impl State { seats: Seats::bind(conn, globals), pointers: Vec::new(), + outptus: Outputs::bind(conn, globals), bars: Vec::new(), shared_state: SharedState { @@ -90,11 +91,6 @@ impl State { cursor_theme: cursor_theme_ok.then_some(cursor_theme), }; - globals - .iter() - .filter(|g| g.is::<WlOutput>()) - .for_each(|g| this.bind_output(conn, g)); - if let Err(e) = error { this.set_error(conn, e.to_string()); } @@ -145,10 +141,56 @@ impl State { Ok(()) } - fn bind_output(&mut self, conn: &mut Connection<Self>, global: &Global) { - let output = global - .bind_with_cb(conn, 2..=4, wl_output_cb) - .expect("could not bind wl_output"); + fn drop_bar(&mut self, conn: &mut Connection<Self>, bar_index: usize) { + let bar = self.bars.swap_remove(bar_index); + bar.layer_surface.destroy(conn); + bar.surface.destroy(conn); + if let Some(wm_info_provider) = &mut self.shared_state.wm_info_provider { + wm_info_provider.output_removed(conn, bar.output); + } + } +} + +impl SeatHandler for State { + fn get_seats(&mut self) -> &mut Seats { + &mut self.seats + } + + fn pointer_added(&mut self, conn: &mut Connection<Self>, seat: WlSeat) { + assert!(seat.version() >= 5); + self.pointers.push(Pointer { + seat, + pointer: seat.get_pointer_with_cb(conn, wl_pointer_cb), + cursor_surface: self.wl_compositor.create_surface(conn), + current_surface: None, + x: 0.0, + y: 0.0, + pending_button: None, + pending_scroll: 0.0, + scroll_frame: ScrollFrame::default(), + }); + } + + fn pointer_removed(&mut self, conn: &mut Connection<Self>, seat: WlSeat) { + let pointer_i = self.pointers.iter().position(|p| p.seat == seat).unwrap(); + let pointer = self.pointers.swap_remove(pointer_i); + pointer.pointer.release(conn); + pointer.cursor_surface.destroy(conn); + } +} + +impl OutputHandler for State { + fn get_outputs(&mut self) -> &mut Outputs { + &mut self.outptus + } + + fn output_added(&mut self, conn: &mut Connection<Self>, output: WlOutput) { + let scale = self + .outptus + .iter() + .find(|o| o.wl_output == output) + .unwrap() + .scale; if let Some(wm_info_provider) = &mut self.shared_state.wm_info_provider { wm_info_provider.new_outut(conn, output); @@ -190,16 +232,15 @@ impl State { }, ); - // Note: layer_surface is commited when we receive the scale factor of this output + surface.commit(conn); self.bars.push(Bar { output, - output_reg_name: global.name, configured: false, frame_cb: None, width: 0, height: self.shared_state.config.height, - scale: 1, + scale, scale120: None, surface, viewport: self.viewporter.get_viewport(conn, surface), @@ -213,83 +254,22 @@ impl State { }); } - fn drop_bar(&mut self, conn: &mut Connection<Self>, bar_index: usize) { - let bar = self.bars.swap_remove(bar_index); - bar.surface.destroy(conn); - bar.layer_surface.destroy(conn); - if let Some(wm_info_provider) = &mut self.shared_state.wm_info_provider { - wm_info_provider.output_removed(conn, bar.output); - } - if bar.output.version() >= 3 { - bar.output.release(conn); - } - } -} - -impl SeatHandler for State { - fn get_seats(&mut self) -> &mut Seats { - &mut self.seats - } - - fn pointer_added(&mut self, conn: &mut Connection<Self>, seat: WlSeat) { - assert!(seat.version() >= 5); - self.pointers.push(Pointer { - seat, - pointer: seat.get_pointer_with_cb(conn, wl_pointer_cb), - cursor_surface: self.wl_compositor.create_surface(conn), - current_surface: None, - x: 0.0, - y: 0.0, - pending_button: None, - pending_scroll: 0.0, - scroll_frame: ScrollFrame::default(), - }); - } - - fn pointer_removed(&mut self, conn: &mut Connection<Self>, seat: WlSeat) { - let pointer_i = self.pointers.iter().position(|p| p.seat == seat).unwrap(); - let pointer = self.pointers.swap_remove(pointer_i); - pointer.pointer.release(conn); - pointer.cursor_surface.destroy(conn); - } -} - -fn wl_registry_cb(conn: &mut Connection<State>, state: &mut State, event: &wl_registry::Event) { - match event { - wl_registry::Event::Global(global) if global.is::<WlOutput>() => { - state.bind_output(conn, global); - } - wl_registry::Event::GlobalRemove(name) => { - if let Some(bar_index) = state - .bars - .iter() - .position(|bar| bar.output_reg_name == *name) - { - state.drop_bar(conn, bar_index); - } + fn output_removed(&mut self, conn: &mut Connection<Self>, output: WlOutput) { + if let Some(bar_i) = self.bars.iter().position(|o| o.output == output) { + self.drop_bar(conn, bar_i); } - _ => (), } -} -fn wl_output_cb( - conn: &mut Connection<State>, - state: &mut State, - output: WlOutput, - event: wl_output::Event, -) { - if let wl_output::Event::Scale(scale) = event { - let bar = state - .bars - .iter_mut() - .find(|bar| bar.output == output) - .unwrap(); - bar.scale = scale as u32; - // If bar is not configured yet, it is because we were waiting for the "scale" event - // before commiting the surface. Otherwise, there is no need to do any redrawing (we'll - // do that after "configure" event). - if !bar.configured { - bar.surface.commit(conn); + fn info_updated( + &mut self, + _: &mut Connection<Self>, + output: WlOutput, + mask: wayrs_utils::outputs::UpdatesMask, + ) { + if mask.scale { + let info = self.outptus.iter().find(|o| o.wl_output == output).unwrap(); + let bar = self.bars.iter_mut().find(|b| b.output == output).unwrap(); + bar.scale = info.scale; } } } @@ -318,12 +298,13 @@ fn layer_surface_cb( } } zwlr_layer_surface_v1::Event::Closed => { - let bar_index = state + if let Some(bar_i) = state .bars .iter() - .position(|bar| bar.layer_surface == layer_surface) - .unwrap(); - state.drop_bar(conn, bar_index); + .position(|o| o.layer_surface == layer_surface) + { + state.drop_bar(conn, bar_i); + } } } } |
