diff --git a/src/backlight.rs b/src/backlight.rs index 055bca2..388bacb 100644 --- a/src/backlight.rs +++ b/src/backlight.rs @@ -1,13 +1,13 @@ -use crate::module::{ModuleImpl, Input}; use crate::buffer::Buffer; use crate::color::Color; use crate::draw::{draw_text, ROBOTO_REGULAR}; +use crate::module::{Input, ModuleImpl}; -use std::fs::OpenOptions; -use std::path::{Path, PathBuf}; -use std::io::{Read, Write}; -use std::io::{Error, ErrorKind}; use chrono::{DateTime, Local}; +use std::fs::OpenOptions; +use std::io::{Error, ErrorKind}; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; pub struct Backlight { device_path: PathBuf, @@ -17,19 +17,16 @@ pub struct Backlight { } fn read_file_as_u64(path: &Path) -> Result { - let mut file = OpenOptions::new() - .read(true) - .open(path)?; + let mut file = OpenOptions::new().read(true).open(path)?; let mut str = String::new(); file.read_to_string(&mut str)?; str.pop(); - str.parse::().map_err(|_e| {Error::new(ErrorKind::Other, "unable to parse value")}) + str.parse::() + .map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value")) } fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> { - let mut file = OpenOptions::new() - .write(true) - .open(path)?; + let mut file = OpenOptions::new().write(true).open(path)?; file.write_fmt(format_args!("{}", value)) } @@ -42,7 +39,10 @@ impl Backlight { } pub fn sync(&mut self) -> Result<(), Error> { - write_file_as_u64(self.device_path.join("brightness").as_path(), self.cur_brightness)?; + write_file_as_u64( + self.device_path.join("brightness").as_path(), + self.cur_brightness, + )?; self.update()?; Ok(()) } @@ -70,12 +70,17 @@ impl Backlight { Ok(()) } - pub fn default() -> Result { - let devices = Path::new("/sys/class/backlight") - .read_dir()?; + pub fn new() -> Result { + let devices = Path::new("/sys/class/backlight").read_dir()?; - let first_device = devices.take(1).next().unwrap().unwrap(); - let mut dev = Backlight{ + let first_device = match devices.take(1).next() { + Some(v) => match v { + Ok(v) => v, + Err(_) => return Err(Error::new(ErrorKind::Other, "no backlight device")), + }, + None => return Err(Error::new(ErrorKind::Other, "no backlight device")), + }; + let mut dev = Backlight { device_path: first_device.path(), cur_brightness: 0, max_brightness: 0, @@ -89,7 +94,12 @@ impl Backlight { } impl ModuleImpl for Backlight { - fn draw(&self, buf: &mut Buffer, bg: &Color, _time: &DateTime) -> Result, Error> { + fn draw( + &self, + buf: &mut Buffer, + bg: &Color, + _time: &DateTime, + ) -> Result, Error> { buf.memset(bg); draw_text( ROBOTO_REGULAR, @@ -102,7 +112,7 @@ impl ModuleImpl for Backlight { Ok(vec![buf.get_signed_bounds()]) } - fn update(&mut self, _time: &DateTime, force: bool) -> Result{ + fn update(&mut self, _time: &DateTime, force: bool) -> Result { if self.dirty || force { self.dirty = false; Ok(true) @@ -113,23 +123,28 @@ impl ModuleImpl for Backlight { fn input(&mut self, input: Input) { match input { - Input::Scroll{pos: _pos, x: _x, y} => { + Input::Scroll { + pos: _pos, + x: _x, + y, + } => { self.add((y * -1.0) as f32 / 8.0).unwrap(); self.sync().unwrap(); - }, - Input::Click{pos: _pos, button} => { + } + Input::Click { pos: _pos, button } => { match button { - 273 => { // Right click + 273 => { + // Right click self.cur_brightness = if self.cur_brightness == 1 { self.max_brightness } else { 1 }; self.sync().unwrap(); - }, + } _ => {} } - }, + } } } -} \ No newline at end of file +} diff --git a/src/calendar.rs b/src/calendar.rs index 751eb9c..3ee2eba 100644 --- a/src/calendar.rs +++ b/src/calendar.rs @@ -1,8 +1,8 @@ use crate::buffer::Buffer; use crate::color::Color; -use crate::module::{ModuleImpl, Input}; -use crate::draw::{draw_text, ROBOTO_REGULAR, DEJAVUSANS_MONO}; -use chrono::{DateTime, Date, Datelike, Local}; +use crate::draw::{draw_text, DEJAVUSANS_MONO, ROBOTO_REGULAR}; +use crate::module::{Input, ModuleImpl}; +use chrono::{Date, DateTime, Datelike, Local}; pub struct Calendar { cur_date: Date, @@ -149,7 +149,7 @@ fn draw_month( impl Calendar { pub fn new() -> Calendar { - Calendar{ + Calendar { cur_date: Local::now().date(), first_draw: true, offset: 0, @@ -167,17 +167,21 @@ impl ModuleImpl for Calendar { let time = time.date(); let mut t = time.with_day(1).unwrap(); if self.offset != 0 { - let mut new_month = t.month() as i32 + self.offset; - let mut new_year = t.year(); - while new_month > 12 { - new_year += 1; - new_month -= 12; + let mut month = (t.month() - 1) as i32 + self.offset; + let mut year = t.year(); + while month > 11 { + year += 1; + month -= 12; } - while new_month < 0 { - new_year -= 1; - new_month += 12; + while month < 0 { + year -= 1; + month += 12; } - t = t.with_year(new_year).unwrap().with_month(new_month as u32).unwrap(); + t = t + .with_year(year) + .unwrap() + .with_month((month + 1) as u32) + .unwrap(); } let mut damage: Vec<(i32, i32, i32, i32)> = Vec::new(); damage.push(draw_month( @@ -206,11 +210,11 @@ impl ModuleImpl for Calendar { Ok(damage) } - fn update(&mut self, time: &DateTime, force: bool) -> Result{ + fn update(&mut self, time: &DateTime, force: bool) -> Result { if self.first_draw { self.first_draw = false; Ok(true) - } else if time.date() != self.cur_date || force{ + } else if time.date() != self.cur_date || force { self.cur_date = time.date(); Ok(true) } else { @@ -220,15 +224,15 @@ impl ModuleImpl for Calendar { fn input(&mut self, input: Input) { match input { - Input::Scroll{pos: _, x: _, y} => { - if y < 0.0 { + Input::Scroll { pos: _, x: _, y } => { + if y > 0.0 { self.offset += 1; } else { self.offset -= 1; } self.first_draw = true; - }, - Input::Click{pos: _, button: _} => { + } + Input::Click { pos: _, button: _ } => { self.offset = 0; self.first_draw = true; } diff --git a/src/clock.rs b/src/clock.rs index 1ac05bf..91597b0 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -1,68 +1,70 @@ use crate::buffer::Buffer; use crate::color::Color; -use crate::module::{ModuleImpl, Input}; use crate::draw::{draw_text, draw_text_fixed_width, ROBOTO_REGULAR}; +use crate::module::{Input, ModuleImpl}; use chrono::{DateTime, Datelike, Local, Timelike}; pub struct Clock { - cur_time: DateTime, + cur_time: DateTime, } impl Clock { - pub fn new() -> Clock { - Clock{ - cur_time: Local::now(), - } - } + pub fn new() -> Clock { + Clock { + cur_time: Local::now(), + } + } } impl ModuleImpl for Clock { - fn draw(&self, - buf: &mut Buffer, - bg: &Color, - time: &DateTime, - ) -> Result, ::std::io::Error> { - buf.memset(bg); + fn draw( + &self, + buf: &mut Buffer, + bg: &Color, + time: &DateTime, + ) -> Result, ::std::io::Error> { + buf.memset(bg); - draw_text( - ROBOTO_REGULAR, - &mut buf.subdimensions((0, 0, 448, 64)), - bg, - &Color::new(1.0, 1.0, 1.0, 1.0), - 64.0, - &format!( - "{:?}, {:02}/{:02}/{:4}", - time.weekday(), - time.day(), - time.month(), - time.year() - ), - )?; + draw_text( + ROBOTO_REGULAR, + &mut buf.subdimensions((0, 0, 448, 64)), + bg, + &Color::new(1.0, 1.0, 1.0, 1.0), + 64.0, + &format!( + "{:?}, {:02}/{:02}/{:4}", + time.weekday(), + time.day(), + time.month(), + time.year() + ), + )?; - draw_text_fixed_width( - ROBOTO_REGULAR, - &mut buf.subdimensions((0, 64, 288 * 2 + 64, 256)), - bg, - &Color::new(1.0, 1.0, 1.0, 1.0), - 256.0, - vec![120, 120, 64, 120, 120], - &format!("{:02}:{:02}", time.hour(), time.minute()), - )?; + draw_text_fixed_width( + ROBOTO_REGULAR, + &mut buf.subdimensions((0, 64, 288 * 2 + 64, 256)), + bg, + &Color::new(1.0, 1.0, 1.0, 1.0), + 256.0, + vec![120, 120, 64, 120, 120], + &format!("{:02}:{:02}", time.hour(), time.minute()), + )?; - Ok(vec![buf.get_signed_bounds()]) - } - - fn update(&mut self, time: &DateTime, force: bool) -> Result { - if time.date() != self.cur_time.date() || - time.hour() != self.cur_time.hour() || - time.minute() != self.cur_time.minute() || - force { - self.cur_time = time.clone(); - Ok(true) - } else { - Ok(false) - } + Ok(vec![buf.get_signed_bounds()]) } - fn input(&mut self, _input: Input) {} -} \ No newline at end of file + fn update(&mut self, time: &DateTime, force: bool) -> Result { + if time.date() != self.cur_time.date() + || time.hour() != self.cur_time.hour() + || time.minute() != self.cur_time.minute() + || force + { + self.cur_time = time.clone(); + Ok(true) + } else { + Ok(false) + } + } + + fn input(&mut self, _input: Input) {} +} diff --git a/src/draw.rs b/src/draw.rs index 0ed4225..8c52741 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -101,4 +101,3 @@ pub fn draw_text_fixed_width( Ok(()) } - diff --git a/src/main.rs b/src/main.rs index 4106705..06e1e18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,32 +15,32 @@ use os_pipe::pipe; use chrono::{Duration as ChronoDuration, Local, Timelike}; use sctk::keyboard::{map_keyboard_auto_with_repeat, Event as KbEvent, KeyRepeatKind}; -use sctk::reexports::client::protocol::{wl_shm, wl_pointer}; +use sctk::reexports::client::protocol::{wl_pointer, wl_shm}; use sctk::reexports::client::{Display, EventQueue, NewProxy}; use sctk::utils::DoubleMemPool; use sctk::window::{ConceptFrame, Event as WEvent, Window}; use sctk::Environment; -mod buffer; -mod color; -mod module; -mod draw; -mod clock; -mod calendar; mod backlight; +mod buffer; +mod calendar; +mod clock; +mod color; +mod draw; +mod module; -use crate::buffer::Buffer; -use crate::color::Color; -use crate::module::{Module, Input}; -use crate::clock::Clock; use crate::backlight::Backlight; +use crate::buffer::Buffer; use crate::calendar::Calendar; +use crate::clock::Clock; +use crate::color::Color; +use crate::module::{Input, Module}; enum Cmd { Exit, Configure, Draw, - Input{pos: (u32, u32), input: Input}, + Input { pos: (u32, u32), input: Input }, } struct App { @@ -72,14 +72,16 @@ impl App { let mmap = pool.mmap(); let mut buf = Buffer::new(mmap, self.dimensions); let mut margin_buf = buf.subdimensions((20, 20, buf_x - 40, buf_y - 40)); + let bg = Color::new(0.0, 0.0, 0.0, 0.9); let mut damage = vec![]; for module in self.modules.iter() { - if module.update(&time, force)? { - let mut d = module.draw(&mut margin_buf, &bg, &time)?; - damage.append(&mut d); - } + if module.update(&time, force)? { + let mut b = &mut margin_buf.subdimensions(module.get_bounds()); + let mut d = module.draw(&mut b, &bg, &time)?; + damage.append(&mut d); + } } mmap.flush().unwrap(); @@ -134,12 +136,12 @@ impl App { } fn get_module(&self, pos: (u32, u32)) -> Option<&Module> { - for m in self.modules.iter() { - if m.intersect(pos) { - return Some(&m) - } - } - None + for m in self.modules.iter() { + if m.intersect(pos) { + return Some(&m); + } + } + None } fn new(dimensions: (u32, u32)) -> App { @@ -197,64 +199,102 @@ impl App { let pointer_clone = cmd_queue.clone(); seat.get_pointer(move |ptr| { - let mut pos: (u32, u32) = (0, 0); - let mut vert_scroll: f64 = 0.0; - let mut horiz_scroll: f64 = 0.0; - let mut btn: u32 = 0; - let mut btn_clicked = false; - ptr.implement_closure( - move |evt, _| match evt { - wl_pointer::Event::Enter{ serial: _, surface: _, surface_x, surface_y } => { - pos = (surface_x as u32, surface_y as u32); - }, - wl_pointer::Event::Leave{ serial: _, surface: _ } => { - pos = (0, 0); - }, - wl_pointer::Event::Motion{ time: _, surface_x, surface_y } => { - pos = (surface_x as u32, surface_y as u32); - }, - wl_pointer::Event::Axis{ time: _, axis, value } => { - if axis == wl_pointer::Axis::VerticalScroll { - vert_scroll += value; - } - }, - wl_pointer::Event::Button{ serial: _, time: _, button, state } => match state { - wl_pointer::ButtonState::Released => { - btn = button; - btn_clicked = true; - }, - _ => {} - }, - wl_pointer::Event::Frame => { - if pos.0 < 20 || pos.1 < 20 { - // Ignore stuff outside our margins - return - } - let pos = (pos.0-20, pos.1-20); - if vert_scroll != 0.0 || horiz_scroll != 0.0 { - pointer_clone.lock().unwrap().push_back(Cmd::Input{pos: pos, input: Input::Scroll{pos: pos, x: horiz_scroll, y: vert_scroll}}); - vert_scroll = 0.0; - horiz_scroll = 0.0; - } - if btn_clicked { - pointer_clone.lock().unwrap().push_back(Cmd::Input{pos: pos, input: Input::Click{pos: pos, button: btn}}); - btn_clicked = false; - } - } - _ => {} - }, - (), - ) - }) - .unwrap(); + let mut pos: (u32, u32) = (0, 0); + let mut vert_scroll: f64 = 0.0; + let mut horiz_scroll: f64 = 0.0; + let mut btn: u32 = 0; + let mut btn_clicked = false; + ptr.implement_closure( + move |evt, _| match evt { + wl_pointer::Event::Enter { + serial: _, + surface: _, + surface_x, + surface_y, + } => { + pos = (surface_x as u32, surface_y as u32); + } + wl_pointer::Event::Leave { + serial: _, + surface: _, + } => { + pos = (0, 0); + } + wl_pointer::Event::Motion { + time: _, + surface_x, + surface_y, + } => { + pos = (surface_x as u32, surface_y as u32); + } + wl_pointer::Event::Axis { + time: _, + axis, + value, + } => { + if axis == wl_pointer::Axis::VerticalScroll { + vert_scroll += value; + } + } + wl_pointer::Event::Button { + serial: _, + time: _, + button, + state, + } => match state { + wl_pointer::ButtonState::Released => { + btn = button; + btn_clicked = true; + } + _ => {} + }, + wl_pointer::Event::Frame => { + if pos.0 < 20 || pos.1 < 20 { + // Ignore stuff outside our margins + return; + } + let pos = (pos.0 - 20, pos.1 - 20); + if vert_scroll != 0.0 || horiz_scroll != 0.0 { + pointer_clone.lock().unwrap().push_back(Cmd::Input { + pos: pos, + input: Input::Scroll { + pos: pos, + x: horiz_scroll, + y: vert_scroll, + }, + }); + vert_scroll = 0.0; + horiz_scroll = 0.0; + } + if btn_clicked { + pointer_clone.lock().unwrap().push_back(Cmd::Input { + pos: pos, + input: Input::Click { + pos: pos, + button: btn, + }, + }); + btn_clicked = false; + } + } + _ => {} + }, + (), + ) + }) + .unwrap(); window.set_title("dashboard".to_string()); window.set_app_id("dashboard".to_string()); - let modules = vec![ - Module::new(Box::new(Clock::new()), (0, 0, 720, 320)), - Module::new(Box::new(Calendar::new()), (0, 384, 1280, 344)), - Module::new(Box::new(Backlight::default().unwrap()), (720, 0, 256, 24))]; + let mut modules = vec![ + Module::new(Box::new(Clock::new()), (0, 0, 720, 320)), + Module::new(Box::new(Calendar::new()), (0, 384, 1280, 344)), + ]; + + if let Ok(m) = Backlight::new() { + modules.push(Module::new(Box::new(m), (720, 0, 256, 24))); + } App { window: window, @@ -309,14 +349,15 @@ fn main() { Cmd::Draw => { app.redraw(false).expect("Failed to draw"); app.flush_display(); - }, - Cmd::Input{pos, input} => { - if pos.0 >= 20 || pos.1 >= 20 { // We need to deal with our margin. - if let Some(m) = app.get_module(pos) { - m.input(input); - q.lock().unwrap().push_back(Cmd::Draw); - } - } + } + Cmd::Input { pos, input } => { + if pos.0 >= 20 || pos.1 >= 20 { + // We need to deal with our margin. + if let Some(m) = app.get_module(pos) { + m.input(input); + q.lock().unwrap().push_back(Cmd::Draw); + } + } } Cmd::Exit => { std::process::exit(0); diff --git a/src/module.rs b/src/module.rs index 08c8c0b..5963f41 100644 --- a/src/module.rs +++ b/src/module.rs @@ -6,45 +6,59 @@ use std::sync::{Arc, Mutex}; use chrono::{DateTime, Local}; pub enum Input { - Click{pos: (u32, u32), button: u32}, - Scroll{pos: (u32, u32), x: f64, y: f64}, + Click { pos: (u32, u32), button: u32 }, + Scroll { pos: (u32, u32), x: f64, y: f64 }, } pub trait ModuleImpl { - fn draw(&self, buf: &mut Buffer, bg: &Color, time: &DateTime) -> Result, ::std::io::Error>; - fn update(&mut self, time: &DateTime, force: bool) -> Result; - fn input(&mut self, input: Input); + fn draw( + &self, + buf: &mut Buffer, + bg: &Color, + time: &DateTime, + ) -> Result, ::std::io::Error>; + fn update(&mut self, time: &DateTime, force: bool) -> Result; + fn input(&mut self, input: Input); } pub struct Module { - m: Arc>>, - pos: (u32, u32, u32, u32), + m: Arc>>, + pos: (u32, u32, u32, u32), } impl Module { - pub fn new(m: Box, pos: (u32, u32, u32, u32)) -> Module { - Module{ - m: Arc::new(Mutex::new(m)), - pos: pos, - } - } + pub fn new(m: Box, pos: (u32, u32, u32, u32)) -> Module { + Module { + m: Arc::new(Mutex::new(m)), + pos: pos, + } + } - pub fn update(&self, time: &DateTime, force: bool) -> Result { - self.m.lock().unwrap().update(time, force) - } + pub fn get_bounds(&self) -> (u32, u32, u32, u32) { + self.pos + } - pub fn draw(&self, buf: &mut Buffer, bg: &Color, time: &DateTime) -> Result, ::std::io::Error> { - self.m.lock().unwrap().draw(&mut buf.subdimensions(self.pos), bg, time) - } + pub fn update(&self, time: &DateTime, force: bool) -> Result { + self.m.lock().unwrap().update(time, force) + } - pub fn intersect(&self, pos: (u32, u32)) -> bool { - pos.0 >= self.pos.0 && - pos.0 < (self.pos.0 + self.pos.2) && - pos.1 >= self.pos.1 && - pos.1 < (self.pos.1 + self.pos.3) - } + pub fn draw( + &self, + buf: &mut Buffer, + bg: &Color, + time: &DateTime, + ) -> Result, ::std::io::Error> { + self.m.lock().unwrap().draw(buf, bg, time) + } - pub fn input(&self, input: Input) { - self.m.lock().unwrap().input(input) - } -} \ No newline at end of file + pub fn intersect(&self, pos: (u32, u32)) -> bool { + pos.0 >= self.pos.0 + && pos.0 < (self.pos.0 + self.pos.2) + && pos.1 >= self.pos.1 + && pos.1 < (self.pos.1 + self.pos.3) + } + + pub fn input(&self, input: Input) { + self.m.lock().unwrap().input(input) + } +}