From 3b4c9267bb2168fb88c650c123a16623cdb268f4 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 3 Sep 2019 13:22:58 +0200 Subject: [PATCH] Simplify buffer positioning, introduce WLDASH_SCALE Replace most uses of Buffer::subdimensions with Buffer::offset. Introduce WLDASH_SCALE. Set to 2 to make wldash render at half size. --- README.md | 2 +- src/app.rs | 21 +++++++++++++-------- src/buffer.rs | 29 +++++++++++++++++++++++++++++ src/draw.rs | 4 ---- src/main.rs | 6 +++++- src/modules/backlight.rs | 6 +++--- src/modules/battery.rs | 6 +++--- src/modules/calendar.rs | 16 ++++++++-------- src/modules/clock.rs | 4 ++-- src/modules/launcher.rs | 14 +++++++------- src/modules/sound.rs | 8 ++++---- 11 files changed, 75 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 531ad9d..faa1d05 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Consider this alpha-quality: While it works, *everything* is subject to change w 3. Put somewhere: `cp target/release/wldash /usr/local/bin/wldash` 4. Run: `wldash` -To display wldash on all outputs, set `WLDASH_ALL_OUTPUTS=1`. +To display wldash on all outputs, set `WLDASH_ALL_OUTPUTS=1`. To cut size in half, set `WLDASH_SCALE=2`. ## How to use launcher diff --git a/src/app.rs b/src/app.rs index cf634c6..f86eeb9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,10 +43,11 @@ struct AppInner { draw_tx: Sender, output_mode: OutputMode, visible: bool, + scale: u32, } impl AppInner { - fn new(tx: Sender, output_mode: OutputMode) -> AppInner { + fn new(tx: Sender, output_mode: OutputMode, scale: u32) -> AppInner { AppInner { compositor: None, surfaces: Vec::new(), @@ -58,6 +59,7 @@ impl AppInner { draw_tx: tx, output_mode: output_mode, visible: true, + scale: scale, } } @@ -65,6 +67,7 @@ impl AppInner { compositor: &wl_compositor::WlCompositor, shell: &zwlr_layer_shell_v1::ZwlrLayerShellV1, dimensions: (u32, u32), + scale: u32, configured_surfaces: Arc>, tx: Sender, output: Option<&wl_output::WlOutput>, @@ -99,8 +102,8 @@ impl AppInner { .unwrap(); shell_surface.set_keyboard_interactivity(1); - shell_surface.set_size(dimensions.0, dimensions.1); - + shell_surface.set_size(dimensions.0/scale, dimensions.1/scale); + surface.set_buffer_scale(scale as i32); surface.commit(); (surface, shell_surface) } @@ -134,6 +137,7 @@ impl AppInner { &compositor, &shell, self.dimensions, + self.scale, self.configured_surfaces.clone(), self.draw_tx.clone(), None, @@ -149,6 +153,7 @@ impl AppInner { &compositor, &shell, self.dimensions, + self.scale, self.configured_surfaces.clone(), self.draw_tx.clone(), Some(&output.1), @@ -271,10 +276,10 @@ impl App { for surface in inner.surfaces.iter() { surface.attach(Some(&new_buffer), 0, 0); if force { - surface.damage(0, 0, buf_x as i32, buf_y as i32); + surface.damage_buffer(0, 0, buf_x as i32, buf_y as i32); } else { for d in damage.iter() { - surface.damage(d.0, d.1, d.2, d.3); + surface.damage_buffer(d.0, d.1, d.2, d.3); } } surface.commit(); @@ -332,8 +337,8 @@ impl App { None } - pub fn new(tx: Sender, output_mode: OutputMode) -> App { - let inner = Arc::new(Mutex::new(AppInner::new(tx.clone(), output_mode))); + pub fn new(tx: Sender, output_mode: OutputMode, scale: u32) -> App { + let inner = Arc::new(Mutex::new(AppInner::new(tx.clone(), output_mode, scale))); // // Set up modules @@ -527,7 +532,7 @@ impl App { surface_y, .. } => { - pos = (surface_x as u32, surface_y as u32); + pos = (surface_x as u32 * scale, surface_y as u32 * scale); } wl_pointer::Event::Axis { axis, value, .. } => { if axis == wl_pointer::Axis::VerticalScroll { diff --git a/src/buffer.rs b/src/buffer.rs index c191998..e784ed4 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -67,6 +67,35 @@ impl<'a> Buffer<'a> { }) } + pub fn offset( + &mut self, + offset: (u32, u32), + ) -> Result { + let bounds = self.get_bounds(); + if offset.0 > bounds.2 + || offset.1 > bounds.3 + { + return Err(::std::io::Error::new( + ::std::io::ErrorKind::Other, + format!( + "cannot create offset outside buffer: {:?} > {:?}", + offset, bounds + ), + )); + } + + Ok(Buffer { + buf: self.buf, + dimensions: self.dimensions, + subdimensions: Some(( + offset.0 + bounds.0, + offset.1 + bounds.1, + bounds.2 - offset.0, + bounds.3 - offset.1, + )), + }) + } + pub fn memset(&mut self, c: &Color) { if let Some(subdim) = self.subdimensions { unsafe { diff --git a/src/draw.rs b/src/draw.rs index bc2e0cc..75e7571 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -230,8 +230,6 @@ impl Font { } pub fn draw_box(buf: &mut Buffer, c: &Color, dim: (u32, u32)) -> Result<(), ::std::io::Error> { - let mut buf = buf.subdimensions((0, 0, dim.0, dim.1))?; - for x in 0..dim.0 { let _ = buf.put((x, 0), c); let _ = buf.put((x, dim.1 - 1), c); @@ -251,8 +249,6 @@ pub fn draw_bar( height: u32, fill: f32, ) -> Result<(), ::std::io::Error> { - let mut buf = buf.subdimensions((0, 0, length, height))?; - let mut fill_pos = ((length as f32) * fill) as u32; if fill_pos > length { fill_pos = length; diff --git a/src/main.rs b/src/main.rs index d881104..005cbb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,6 +29,10 @@ fn main() { Ok(dir) => dir + "/wldash", Err(_) => "/tmp/wldash".to_string(), }; + let scale = match env::var("WLDASH_SCALE") { + Ok(s) => s.parse().unwrap(), + Err(_) => 1, + }; let args: Vec = env::args().collect(); let mode = match args.len() { @@ -84,7 +88,7 @@ fn main() { Err(_) => OutputMode::Active, }; - let mut app = App::new(tx_draw, output_mode); + let mut app = App::new(tx_draw, output_mode, scale); app.wipe(); let worker_queue = app.cmd_queue(); diff --git a/src/modules/backlight.rs b/src/modules/backlight.rs index 4f2e2f8..966b386 100644 --- a/src/modules/backlight.rs +++ b/src/modules/backlight.rs @@ -108,19 +108,19 @@ impl ModuleImpl for Backlight { buf.memset(bg); let c = Color::new(1.0, 1.0, 1.0, 1.0); self.font.draw_text( - &mut buf.subdimensions((0, 0, 128, 24))?, + buf, bg, &c, "backlight", )?; draw_bar( - &mut buf.subdimensions((128, 0, 464, 24))?, + &mut buf.offset((128, 0))?, &c, 464, 24, self.brightness(), )?; - draw_box(&mut buf.subdimensions((128, 0, 464, 24))?, &c, (464, 24))?; + draw_box(&mut buf.offset((128, 0))?, &c, (464, 24))?; Ok(vec![buf.get_signed_bounds()]) } diff --git a/src/modules/battery.rs b/src/modules/battery.rs index 141269b..6916ccd 100644 --- a/src/modules/battery.rs +++ b/src/modules/battery.rs @@ -214,13 +214,13 @@ impl ModuleImpl for UpowerBattery { }; self.font.draw_text( - &mut buf.subdimensions((0, 0, 128, 24))?, + &mut buf.offset((0, 0))?, bg, &text_color, "battery", )?; draw_bar( - &mut buf.subdimensions((128, 0, 464, 24))?, + &mut buf.offset((128, 0))?, &bar_color, 464, 24, @@ -228,7 +228,7 @@ impl ModuleImpl for UpowerBattery { )?; draw_box( - &mut buf.subdimensions((128, 0, 464, 24))?, + &mut buf.offset((128, 0))?, &bar_color, (464, 24), )?; diff --git a/src/modules/calendar.rs b/src/modules/calendar.rs index 81f08e6..1f96b1d 100644 --- a/src/modules/calendar.rs +++ b/src/modules/calendar.rs @@ -48,14 +48,14 @@ impl Calendar { // self.month_cache.draw_text( - &mut buf.subdimensions((0, 0, 304, 72))?, + buf, background_color, &Color::new(1.0, 1.0, 1.0, 1.0), month_str, )?; if time.year() != orig.year() { self.year_cache.draw_text( - &mut buf.subdimensions((320, 0, 48, 32))?, + &mut buf.offset((320, 0))?, background_color, &Color::new(0.8, 0.8, 0.8, 1.0), &format!("{:}", time.year()), @@ -79,7 +79,7 @@ impl Calendar { }; self.day_cache.draw_text( - &mut buf.subdimensions((idx * 48 + 4, (y_off * 32) + 64, 24, 16))?, + &mut buf.offset((idx * 48 + 4, (y_off * 32) + 64))?, background_color, &Color::new(1.0, 1.0, 1.0, 1.0), &wk_chr, @@ -105,7 +105,7 @@ impl Calendar { // let wk = time.iso_week(); self.calendar_cache.draw_text( - &mut buf.subdimensions((0 * 48, (y_off * 32) + 64, 32, 32))?, + &mut buf.offset((0 * 48, (y_off * 32) + 64))?, background_color, &Color::new(0.75, 0.75, 0.75, 1.0), &format!("{:02}", wk.week()), @@ -123,7 +123,7 @@ impl Calendar { }; self.calendar_cache.draw_text( - &mut buf.subdimensions((x_pos * 48, (y_off * 32) + 64, 32, 32))?, + &mut buf.offset((x_pos * 48, (y_off * 32) + 64))?, background_color, &c, &format!("{:02}", time.day()), @@ -195,13 +195,13 @@ impl ModuleImpl for Calendar { .unwrap(); } self.draw_month( - &mut buf.subdimensions((0, 0, 368, 320))?, + &mut buf.offset((0, 0))?, background_color, &time, &t.pred().with_day(1).unwrap(), )?; self.draw_month( - &mut buf.subdimensions((432, 0, 368, 320))?, + &mut buf.offset((432, 0))?, background_color, &time, &t, @@ -212,7 +212,7 @@ impl ModuleImpl for Calendar { t.with_month(t.month() + 1).unwrap() }; self.draw_month( - &mut buf.subdimensions((864, 0, 368, 320))?, + &mut buf.offset((864, 0))?, background_color, &time, &n, diff --git a/src/modules/clock.rs b/src/modules/clock.rs index 24c7bd0..d895542 100644 --- a/src/modules/clock.rs +++ b/src/modules/clock.rs @@ -58,7 +58,7 @@ impl ModuleImpl for Clock { buf.memset(bg); self.date_cache.draw_text( - &mut buf.subdimensions((0, 0, 240 * 2 + 56, 64))?, + buf, bg, &Color::new(1.0, 1.0, 1.0, 1.0), &format!( @@ -71,7 +71,7 @@ impl ModuleImpl for Clock { )?; self.clock_cache.draw_text_fixed_width( - &mut buf.subdimensions((0, 64, 240 * 2 + 56, 256))?, + &mut buf.offset((0, 64))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0), &[120, 120, 56, 120, 120], diff --git a/src/modules/launcher.rs b/src/modules/launcher.rs index 872b07d..e4fd8e0 100644 --- a/src/modules/launcher.rs +++ b/src/modules/launcher.rs @@ -73,7 +73,7 @@ impl Launcher { Color::new(1.0, 1.0, 1.0, 1.0) }; let dim = self.font.borrow_mut().auto_draw_text( - &mut buf.subdimensions((0, 0, 1232, 32))?, + buf, bg, &c, &self.input, @@ -86,7 +86,7 @@ impl Launcher { let mut width_remaining: i32 = 1232 - x_off as i32; for (idx, m) in self.matches.iter().enumerate() { - let mut b = match buf.subdimensions((x_off, 0, width_remaining as u32, 32)) { + let mut b = match buf.offset((x_off, 0)) { Ok(b) => b, Err(_) => break, }; @@ -135,7 +135,7 @@ impl Launcher { .font .borrow_mut() .auto_draw_text( - &mut buf.subdimensions((0, 0, 1232, 32))?, + buf, bg, &Color::new(1.0, 1.0, 0.0, 1.0), "=", @@ -145,7 +145,7 @@ impl Launcher { let x_off = if self.input.len() > 0 { let dim = self.font.borrow_mut().auto_draw_text( - &mut buf.subdimensions((x_off, 0, 1232 - x_off, 32))?, + &mut buf.offset((x_off, 0))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0), &self.input[1..], @@ -158,7 +158,7 @@ impl Launcher { if let Some(result) = &self.result { self.font.borrow_mut().auto_draw_text( - &mut buf.subdimensions((x_off, 0, 1232 - x_off, 32))?, + &mut buf.offset((x_off, 0))?, bg, &Color::new(0.75, 0.75, 0.75, 1.0), &format!(" = {:}", result), @@ -179,7 +179,7 @@ impl Launcher { .font .borrow_mut() .auto_draw_text( - &mut buf.subdimensions((0, 0, 1232, 32))?, + &mut buf.offset((0, 0))?, bg, &Color::new(1.0, 1.0, 0.0, 1.0), "!", @@ -189,7 +189,7 @@ impl Launcher { if self.input.len() > 0 { self.font.borrow_mut().auto_draw_text( - &mut buf.subdimensions((x_off, 0, 1232 - x_off, 32))?, + &mut buf.offset((x_off, 0))?, bg, &Color::new(1.0, 1.0, 1.0, 1.0), &self.input[1..], diff --git a/src/modules/sound.rs b/src/modules/sound.rs index 16d1351..9ddc1e5 100644 --- a/src/modules/sound.rs +++ b/src/modules/sound.rs @@ -487,20 +487,20 @@ impl ModuleImpl for PulseAudio { Color::new(1.0, 1.0, 1.0, 1.0) }; self.font.draw_text( - &mut buf.subdimensions((0, 0, 128, 24))?, + buf, bg, &Color::new(1.0, 1.0, 1.0, 1.0), "volume", )?; - draw_bar(&mut buf.subdimensions((128, 0, 464, 24))?, &c, 464, 24, vol)?; + draw_bar(&mut buf.offset((128, 0))?, &c, 464, 24, vol)?; let mut iter = 1.0; while vol > 1.0 { let c = &Color::new(0.75 / iter, 0.25 / iter, 0.25 / iter, 1.0); vol -= 1.0; iter += 1.0; - draw_bar(&mut buf.subdimensions((128, 0, 464, 24))?, &c, 464, 24, vol)?; + draw_bar(&mut buf.offset((128, 0))?, &c, 464, 24, vol)?; } - draw_box(&mut buf.subdimensions((128, 0, 464, 24))?, &c, (464, 24))?; + draw_box(&mut buf.offset((128, 0))?, &c, (464, 24))?; Ok(vec![buf.get_signed_bounds()]) }