diff options
| author | MaxVerevkin <maxxverrr@gmail.com> | 2023-01-24 00:56:39 +0200 |
|---|---|---|
| committer | MaxVerevkin <maxxverrr@gmail.com> | 2023-01-24 00:56:39 +0200 |
| commit | b04f6e8190c691261c8eb94466d95c34ec6164a7 (patch) | |
| tree | 3c0988f45a65e74d368a0751030003449554124b /src | |
| parent | 0467b9f964f7ee91c0d679feedaf62626f6fe065 (diff) | |
| download | i3bar-mango-b04f6e8190c691261c8eb94466d95c34ec6164a7.tar.gz | |
update dependencies
Diffstat (limited to 'src')
| -rw-r--r-- | src/bar.rs | 22 | ||||
| -rw-r--r-- | src/cursor.rs | 2 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/protocol.rs | 8 | ||||
| -rw-r--r-- | src/state.rs | 456 |
5 files changed, 248 insertions, 242 deletions
@@ -65,7 +65,18 @@ impl Bar { }; river_control.add_argument(conn, cmd.into()); river_control.add_argument(conn, CString::new((1u32 << tag).to_string()).unwrap()); - river_control.run_command(conn, seat); + river_control.run_command_with_cb( + conn, + seat, + |conn, state, _, event| match event { + zriver_command_callback_v1::Event::Success(msg) => { + info!("river_control: {msg:?}") + } + zriver_command_callback_v1::Event::Failure(msg) => { + state.set_error(conn, msg.to_string_lossy()) + } + }, + ); } } else if let Some((name, instance)) = self.blocks_btns.click(x) { if let Some(cmd) = &mut ss.status_cmd { @@ -214,7 +225,7 @@ impl Bar { height_f, ); - self.surface.attach(conn, buffer, 0, 0); + self.surface.attach(conn, buffer.wl, 0, 0); self.surface .damage_buffer(conn, 0, 0, width * self.scale, height * self.scale); self.surface.commit(conn); @@ -222,7 +233,12 @@ impl Bar { pub fn request_frame(&mut self, conn: &mut Connection<State>) { if self.configured && self.frame_cb.is_none() { - self.frame_cb = Some(self.surface.frame(conn)); + self.frame_cb = Some(self.surface.frame_with_cb(conn, |conn, state, cb, _| { + if let Some(bar) = state.bars.iter_mut().find(|bar| bar.frame_cb == Some(cb)) { + bar.frame_cb = None; + bar.frame(conn, &mut state.shared_state); + } + })); self.surface.commit(conn); } } diff --git a/src/cursor.rs b/src/cursor.rs index 57db8bb..3a827d0 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -92,7 +92,7 @@ impl Cursor { assert_eq!(image.pixels_argb.len(), canvas.len(),); canvas.copy_from_slice(&image.pixels_rgba); - self.surface.attach(conn, buffer, 0, 0); + self.surface.attach(conn, buffer.wl, 0, 0); self.surface .damage_buffer(conn, 0, 0, image.width as i32, image.height as i32); self.surface.set_buffer_scale(conn, scale as i32); diff --git a/src/main.rs b/src/main.rs index 73c2d6c..7ef0acb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ async fn main() -> anyhow::Result<()> { tokio::select! { recv_events = conn.async_recv_events() => { recv_events?; - conn.dispatch_events(&mut state)?; + conn.dispatch_events(&mut state); conn.async_flush().await?; } reat_res = state.status_cmd_read() => { diff --git a/src/protocol.rs b/src/protocol.rs index ac8b2c1..a53a22a 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -2,7 +2,7 @@ use wayrs_client; pub use wayrs_client::protocol::*; -wayrs_scanner::generate!("protocols/xdg-shell.xml"); -wayrs_scanner::generate!("protocols/wlr-layer-shell-unstable-v1.xml"); -wayrs_scanner::generate!("protocols/river-status-unstable-v1.xml"); -wayrs_scanner::generate!("protocols/river-control-unstable-v1.xml"); +wayrs_client::scanner::generate!("protocols/xdg-shell.xml"); +wayrs_client::scanner::generate!("protocols/wlr-layer-shell-unstable-v1.xml"); +wayrs_client::scanner::generate!("protocols/river-status-unstable-v1.xml"); +wayrs_client::scanner::generate!("protocols/river-control-unstable-v1.xml"); diff --git a/src/state.rs b/src/state.rs index dbe73fd..dfc2a2a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,13 +1,12 @@ use crate::cursor::Cursor; use crate::protocol::*; -use std::convert::Infallible; use std::future::pending; use wayrs_client::connection::Connection; use wayrs_client::global::{Global, GlobalExt, Globals, GlobalsExt}; -use wayrs_client::proxy::{Dispatch, Dispatcher, Proxy}; -use wayrs_shm_alloc::ShmAlloc; +use wayrs_client::proxy::Proxy; +use wayrs_shm_alloc::{ShmAlloc, ShmAllocState}; use crate::{ bar::Bar, config::Config, i3bar_protocol::Block, pointer_btn::PointerBtn, @@ -21,13 +20,13 @@ pub struct State { river_status_manager: Option<ZriverStatusManagerV1>, river_control: Option<ZriverControlV1>, - seats: Vec<Seat>, - bars: Vec<Bar>, + pub seats: Vec<Seat>, + pub bars: Vec<Bar>, pub shared_state: SharedState, } -struct Seat { +pub struct Seat { seat: WlSeat, reg_name: u32, pointer: Option<WlPointer>, @@ -53,6 +52,7 @@ impl State { .as_ref() .and_then(|cmd| StatusCmd::new(cmd).map_err(|e| error = Err(e)).ok()); + conn.set_callback_for(conn.registry(), wl_registry_cb); let shm = globals.bind(conn, 1..=1).expect("could not bind wl_shm"); let mut this = Self { @@ -138,17 +138,21 @@ impl State { } fn bind_output(&mut self, conn: &mut Connection<Self>, global: &Global) { - let output = global.bind(conn, 2..=4).expect("could not bind wl_output"); + let output = global + .bind_with_cb(conn, 2..=4, wl_output_cb) + .expect("could not bind wl_output"); + let surface = self.wl_compositor.create_surface(conn); use zwlr_layer_shell_v1::Layer; use zwlr_layer_surface_v1::Anchor; - let layer_surface = self.layer_shell.get_layer_surface( + let layer_surface = self.layer_shell.get_layer_surface_with_cb( conn, surface, output, Layer::Top, wayrs_client::cstr!("i3bar-river").into(), + layer_surface_cb, ); layer_surface.set_size(conn, 0, self.shared_state.config.height); layer_surface.set_anchor(conn, Anchor::Top | Anchor::Left | Anchor::Right); // Top + Left + Right @@ -159,7 +163,7 @@ impl State { let river_output_status = self .river_status_manager .as_ref() - .map(|s| s.get_river_output_status(conn, output)); + .map(|s| s.get_river_output_status_with_cb(conn, output, river_output_status_cb)); self.bars.push(Bar { output, @@ -183,7 +187,7 @@ impl State { } fn bind_seat(&mut self, conn: &mut Connection<Self>, global: &Global) { - let seat: WlSeat = global.bind(conn, 5..=8).unwrap(); + let seat: WlSeat = global.bind_with_cb(conn, 5..=8, wl_seat_cb).unwrap(); let cursor = Cursor::new(conn, self.wl_compositor).unwrap(); self.seats.push(Seat { seat, @@ -224,277 +228,263 @@ impl State { } } -impl Dispatcher for State { - type Error = Infallible; +impl ShmAllocState for State { + fn shm_alloc(&mut self) -> &mut ShmAlloc { + &mut self.shared_state.shm + } } -impl Dispatch<WlRegistry> for State { - fn event(&mut self, conn: &mut Connection<Self>, _: WlRegistry, event: wl_registry::Event) { - match event { - wl_registry::Event::Global(global) if global.is::<WlOutput>() => { - self.bind_output(conn, &global); - } - wl_registry::Event::Global(global) if global.is::<WlSeat>() => { - self.bind_seat(conn, &global); - } - wl_registry::Event::GlobalRemove(name) => { - if let Some(bar_index) = - self.bars.iter().position(|bar| bar.output_reg_name == name) - { - self.drop_bar(conn, bar_index); - } else if let Some(seat_index) = - self.seats.iter().position(|seat| seat.reg_name == name) - { - self.drop_seat(conn, seat_index); - } +fn wl_registry_cb( + conn: &mut Connection<State>, + state: &mut State, + _: WlRegistry, + event: wl_registry::Event, +) { + match event { + wl_registry::Event::Global(global) if global.is::<WlOutput>() => { + state.bind_output(conn, &global); + } + wl_registry::Event::Global(global) if global.is::<WlSeat>() => { + state.bind_seat(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); + } else if let Some(seat_index) = + state.seats.iter().position(|seat| seat.reg_name == name) + { + state.drop_seat(conn, seat_index); } - _ => (), } + _ => (), } } -impl Dispatch<WlCallback> for State { - fn event(&mut self, conn: &mut Connection<Self>, cb: WlCallback, event: wl_callback::Event) { - let wl_callback::Event::Done(_) = event; - if let Some(bar) = self.bars.iter_mut().find(|bar| bar.frame_cb == Some(cb)) { - bar.frame_cb = None; - bar.frame(conn, &mut self.shared_state); +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; + // 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); } } } -impl Dispatch<WlOutput> for State { - fn event(&mut self, conn: &mut Connection<Self>, output: WlOutput, event: wl_output::Event) { - if let wl_output::Event::Scale(scale) = event { - let bar = self +fn layer_surface_cb( + conn: &mut Connection<State>, + state: &mut State, + layer_surface: zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, + event: zwlr_layer_surface_v1::Event, +) { + match event { + zwlr_layer_surface_v1::Event::Configure(args) => { + let bar = state .bars .iter_mut() - .find(|bar| bar.output == output) + .find(|bar| bar.layer_surface == layer_surface) .unwrap(); - bar.scale = scale; - // 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); - } - } - } -} - -impl Dispatch<zwlr_layer_surface_v1::ZwlrLayerSurfaceV1> for State { - fn event( - &mut self, - conn: &mut Connection<Self>, - layer_surface: zwlr_layer_surface_v1::ZwlrLayerSurfaceV1, - event: zwlr_layer_surface_v1::Event, - ) { - match event { - zwlr_layer_surface_v1::Event::Configure(args) => { - let bar = self - .bars - .iter_mut() - .find(|bar| bar.layer_surface == layer_surface) - .unwrap(); - assert_ne!(args.width, 0); - bar.width = args.width; + assert_ne!(args.width, 0); + bar.width = args.width; + bar.layer_surface.ack_configure(conn, args.serial); + if bar.configured { + bar.request_frame(conn); + } else { bar.configured = true; - bar.layer_surface.ack_configure(conn, args.serial); - bar.frame(conn, &mut self.shared_state); - } - zwlr_layer_surface_v1::Event::Closed => { - let bar_index = self - .bars - .iter() - .position(|bar| bar.layer_surface == layer_surface) - .unwrap(); - self.drop_bar(conn, bar_index); + bar.frame(conn, &mut state.shared_state); } } + zwlr_layer_surface_v1::Event::Closed => { + let bar_index = state + .bars + .iter() + .position(|bar| bar.layer_surface == layer_surface) + .unwrap(); + state.drop_bar(conn, bar_index); + } } } -impl Dispatch<WlBuffer> for State { - fn event(&mut self, _: &mut Connection<Self>, buffer: WlBuffer, event: wl_buffer::Event) { - let wl_buffer::Event::Release = event; - self.shared_state.shm.free_buffer(buffer); - } -} - -impl Dispatch<WlSeat> for State { - fn event(&mut self, conn: &mut Connection<Self>, seat: WlSeat, event: wl_seat::Event) { - if let wl_seat::Event::Capabilities(capabilities) = event { - let seat = self.seats.iter_mut().find(|s| s.seat == seat).unwrap(); - match &seat.pointer { - Some(pointer) if !capabilities.contains(wl_seat::Capability::Pointer) => { - if pointer.version() >= 3 { - pointer.release(conn); - } - seat.pointer = None; - } - None if capabilities.contains(wl_seat::Capability::Pointer) => { - let pointer = seat.seat.get_pointer(conn); - seat.pointer = Some(pointer); +fn wl_seat_cb( + conn: &mut Connection<State>, + state: &mut State, + seat: WlSeat, + event: wl_seat::Event, +) { + if let wl_seat::Event::Capabilities(capabilities) = event { + let seat = state.seats.iter_mut().find(|s| s.seat == seat).unwrap(); + match &seat.pointer { + Some(pointer) if !capabilities.contains(wl_seat::Capability::Pointer) => { + if pointer.version() >= 3 { + pointer.release(conn); } - _ => (), + seat.pointer = None; } + None if capabilities.contains(wl_seat::Capability::Pointer) => { + seat.pointer = Some(seat.seat.get_pointer_with_cb(conn, wl_pointer_cb)); + } + _ => (), } } } -impl Dispatch<WlPointer> for State { - fn event(&mut self, conn: &mut Connection<Self>, pointer: WlPointer, event: wl_pointer::Event) { - let seat = self - .seats - .iter_mut() - .find(|s| s.pointer == Some(pointer)) - .unwrap(); +fn wl_pointer_cb( + conn: &mut Connection<State>, + state: &mut State, + pointer: WlPointer, + event: wl_pointer::Event, +) { + let seat = state + .seats + .iter_mut() + .find(|s| s.pointer == Some(pointer)) + .unwrap(); - use wl_pointer::Event; - match event { - Event::Frame => { - let btn = seat.pending_button.take(); - let scroll = seat.scroll_frame.finalize(); - if let Some(surface) = seat.cur_surface { - let bar = self - .bars - .iter_mut() - .find(|bar| bar.surface == surface) - .unwrap(); + use wl_pointer::Event; + match event { + Event::Frame => { + let btn = seat.pending_button.take(); + let scroll = seat.scroll_frame.finalize(); + if let Some(surface) = seat.cur_surface { + let bar = state + .bars + .iter_mut() + .find(|bar| bar.surface == surface) + .unwrap(); - if let Some(btn) = btn { - bar.click(conn, &mut self.shared_state, btn, seat.seat, seat.x, seat.y) - .unwrap(); - } + if let Some(btn) = btn { + bar.click( + conn, + &mut state.shared_state, + btn, + seat.seat, + seat.x, + seat.y, + ) + .unwrap(); + } - if scroll.is_finder && self.shared_state.config.invert_touchpad_scrolling { - seat.pending_scroll -= scroll.absolute; - } else { - seat.pending_scroll += scroll.absolute; - } + if scroll.is_finder && state.shared_state.config.invert_touchpad_scrolling { + seat.pending_scroll -= scroll.absolute; + } else { + seat.pending_scroll += scroll.absolute; + } - if scroll.stop { - seat.pending_scroll = 0.0; - } + if scroll.stop { + seat.pending_scroll = 0.0; + } - let btn = if seat.pending_scroll >= 15.0 { - seat.pending_scroll = 0.0; - Some(PointerBtn::WheelDown) - } else if seat.pending_scroll <= -15.0 { - seat.pending_scroll = 0.0; - Some(PointerBtn::WheelUp) - } else { - None - }; + let btn = if seat.pending_scroll >= 15.0 { + seat.pending_scroll = 0.0; + Some(PointerBtn::WheelDown) + } else if seat.pending_scroll <= -15.0 { + seat.pending_scroll = 0.0; + Some(PointerBtn::WheelUp) + } else { + None + }; - if let Some(btn) = btn { - bar.click(conn, &mut self.shared_state, btn, seat.seat, seat.x, seat.y) - .unwrap(); - } - } - } - Event::Enter(args) => { - let bar = self - .bars - .iter() - .find(|bar| bar.surface.id() == args.surface) + if let Some(btn) = btn { + bar.click( + conn, + &mut state.shared_state, + btn, + seat.seat, + seat.x, + seat.y, + ) .unwrap(); - seat.cur_surface = Some(bar.surface); - seat.x = args.surface_x.as_f64(); - seat.y = args.surface_y.as_f64(); - seat.cursor.set( - conn, - args.serial, - pointer, - bar.scale as u32, - &mut self.shared_state.shm, - ); - } - Event::Leave(_) => seat.cur_surface = None, - Event::Motion(args) => { - seat.x = args.surface_x.as_f64(); - seat.y = args.surface_y.as_f64(); - } - Event::Button(args) => { - if args.state == wl_pointer::ButtonState::Pressed { - seat.pending_button = Some(args.button.into()); } } - Event::Axis(args) => { - if args.axis == wl_pointer::Axis::VerticalScroll { - seat.scroll_frame.absolute += args.value.as_f64(); - } + } + Event::Enter(args) => { + let bar = state + .bars + .iter() + .find(|bar| bar.surface.id() == args.surface) + .unwrap(); + seat.cur_surface = Some(bar.surface); + seat.x = args.surface_x.as_f64(); + seat.y = args.surface_y.as_f64(); + seat.cursor.set( + conn, + args.serial, + pointer, + bar.scale as u32, + &mut state.shared_state.shm, + ); + } + Event::Leave(_) => seat.cur_surface = None, + Event::Motion(args) => { + seat.x = args.surface_x.as_f64(); + seat.y = args.surface_y.as_f64(); + } + Event::Button(args) => { + if args.state == wl_pointer::ButtonState::Pressed { + seat.pending_button = Some(args.button.into()); } - Event::AxisSource(source) => { - seat.scroll_frame.is_finder = source == wl_pointer::AxisSource::Finger; + } + Event::Axis(args) => { + if args.axis == wl_pointer::Axis::VerticalScroll { + seat.scroll_frame.absolute += args.value.as_f64(); } - Event::AxisStop(args) => { - if args.axis == wl_pointer::Axis::VerticalScroll { - seat.scroll_frame.stop = true; - } + } + Event::AxisSource(source) => { + seat.scroll_frame.is_finder = source == wl_pointer::AxisSource::Finger; + } + Event::AxisStop(args) => { + if args.axis == wl_pointer::Axis::VerticalScroll { + seat.scroll_frame.stop = true; } - Event::AxisDiscrete(_) | Event::AxisValue120(_) => (), } + Event::AxisDiscrete(_) | Event::AxisValue120(_) => (), } } -impl Dispatch<ZriverOutputStatusV1> for State { - fn event( - &mut self, - conn: &mut Connection<Self>, - status: ZriverOutputStatusV1, - event: zriver_output_status_v1::Event, - ) { - let bar = self - .bars - .iter_mut() - .find(|b| b.river_output_status == Some(status)) - .unwrap(); +fn river_output_status_cb( + conn: &mut Connection<State>, + state: &mut State, + status: ZriverOutputStatusV1, + event: zriver_output_status_v1::Event, +) { + let bar = state + .bars + .iter_mut() + .find(|b| b.river_output_status == Some(status)) + .unwrap(); - use zriver_output_status_v1::Event; - match event { - Event::FocusedTags(tags) => bar.tags_info.focused = tags, - Event::UrgentTags(tags) => bar.tags_info.urgent = tags, - Event::LayoutName(name) => bar.set_layout_name(Some(name.into_string().unwrap())), - Event::LayoutNameClear => bar.set_layout_name(None), - Event::ViewTags(vt) => { - bar.tags_info.active = vt - .chunks_exact(4) - .map(|bytes| u32::from_ne_bytes(bytes.try_into().unwrap())) - .fold(0, |a, b| a | b); - } + use zriver_output_status_v1::Event; + match event { + Event::FocusedTags(tags) => bar.tags_info.focused = tags, + Event::UrgentTags(tags) => bar.tags_info.urgent = tags, + Event::LayoutName(name) => bar.set_layout_name(Some(name.into_string().unwrap())), + Event::LayoutNameClear => bar.set_layout_name(None), + Event::ViewTags(vt) => { + bar.tags_info.active = vt + .chunks_exact(4) + .map(|bytes| u32::from_ne_bytes(bytes.try_into().unwrap())) + .fold(0, |a, b| a | b); } - - bar.request_frame(conn); } -} -impl Dispatch<ZriverCommandCallbackV1> for State { - fn event( - &mut self, - conn: &mut Connection<Self>, - _: ZriverCommandCallbackV1, - event: zriver_command_callback_v1::Event, - ) { - use zriver_command_callback_v1::Event; - match event { - Event::Success(msg) => info!("river_control: {msg:?}"), - Event::Failure(msg) => self.set_error(conn, msg.into_string().unwrap()), - } - } + bar.request_frame(conn); } -// Interfaces with no events -impl Dispatch<WlCompositor> for State {} -impl Dispatch<ZwlrLayerShellV1> for State {} -impl Dispatch<ZriverStatusManagerV1> for State {} -impl Dispatch<ZriverControlV1> for State {} -impl Dispatch<WlShmPool> for State {} - -// Dont care -impl Dispatch<WlShm> for State {} -impl Dispatch<WlSurface> for State {} - #[derive(Debug)] pub struct ComputedBlock { pub block: Block, |
