Fix all the things

This commit is contained in:
Kenny Levinsen
2019-07-31 10:26:12 +02:00
parent dea9378d8d
commit bf0a3617a6
6 changed files with 286 additions and 211 deletions
+42 -27
View File
@@ -1,13 +1,13 @@
use crate::module::{ModuleImpl, Input};
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::color::Color; use crate::color::Color;
use crate::draw::{draw_text, ROBOTO_REGULAR}; 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 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 { pub struct Backlight {
device_path: PathBuf, device_path: PathBuf,
@@ -17,19 +17,16 @@ pub struct Backlight {
} }
fn read_file_as_u64(path: &Path) -> Result<u64, Error> { fn read_file_as_u64(path: &Path) -> Result<u64, Error> {
let mut file = OpenOptions::new() let mut file = OpenOptions::new().read(true).open(path)?;
.read(true)
.open(path)?;
let mut str = String::new(); let mut str = String::new();
file.read_to_string(&mut str)?; file.read_to_string(&mut str)?;
str.pop(); str.pop();
str.parse::<u64>().map_err(|_e| {Error::new(ErrorKind::Other, "unable to parse value")}) str.parse::<u64>()
.map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value"))
} }
fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> { fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> {
let mut file = OpenOptions::new() let mut file = OpenOptions::new().write(true).open(path)?;
.write(true)
.open(path)?;
file.write_fmt(format_args!("{}", value)) file.write_fmt(format_args!("{}", value))
} }
@@ -42,7 +39,10 @@ impl Backlight {
} }
pub fn sync(&mut self) -> Result<(), Error> { 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()?; self.update()?;
Ok(()) Ok(())
} }
@@ -70,12 +70,17 @@ impl Backlight {
Ok(()) Ok(())
} }
pub fn default() -> Result<Self, Error> { pub fn new() -> Result<Self, Error> {
let devices = Path::new("/sys/class/backlight") let devices = Path::new("/sys/class/backlight").read_dir()?;
.read_dir()?;
let first_device = devices.take(1).next().unwrap().unwrap(); let first_device = match devices.take(1).next() {
let mut dev = Backlight{ 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(), device_path: first_device.path(),
cur_brightness: 0, cur_brightness: 0,
max_brightness: 0, max_brightness: 0,
@@ -89,7 +94,12 @@ impl Backlight {
} }
impl ModuleImpl for Backlight { impl ModuleImpl for Backlight {
fn draw(&self, buf: &mut Buffer, bg: &Color, _time: &DateTime<Local>) -> Result<Vec<(i32, i32, i32, i32)>, Error> { fn draw(
&self,
buf: &mut Buffer,
bg: &Color,
_time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, Error> {
buf.memset(bg); buf.memset(bg);
draw_text( draw_text(
ROBOTO_REGULAR, ROBOTO_REGULAR,
@@ -102,7 +112,7 @@ impl ModuleImpl for Backlight {
Ok(vec![buf.get_signed_bounds()]) Ok(vec![buf.get_signed_bounds()])
} }
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error>{ fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
if self.dirty || force { if self.dirty || force {
self.dirty = false; self.dirty = false;
Ok(true) Ok(true)
@@ -113,23 +123,28 @@ impl ModuleImpl for Backlight {
fn input(&mut self, input: Input) { fn input(&mut self, input: Input) {
match 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.add((y * -1.0) as f32 / 8.0).unwrap();
self.sync().unwrap(); self.sync().unwrap();
}, }
Input::Click{pos: _pos, button} => { Input::Click { pos: _pos, button } => {
match button { match button {
273 => { // Right click 273 => {
// Right click
self.cur_brightness = if self.cur_brightness == 1 { self.cur_brightness = if self.cur_brightness == 1 {
self.max_brightness self.max_brightness
} else { } else {
1 1
}; };
self.sync().unwrap(); self.sync().unwrap();
}, }
_ => {} _ => {}
} }
}, }
} }
} }
} }
+23 -19
View File
@@ -1,8 +1,8 @@
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::color::Color; use crate::color::Color;
use crate::module::{ModuleImpl, Input}; use crate::draw::{draw_text, DEJAVUSANS_MONO, ROBOTO_REGULAR};
use crate::draw::{draw_text, ROBOTO_REGULAR, DEJAVUSANS_MONO}; use crate::module::{Input, ModuleImpl};
use chrono::{DateTime, Date, Datelike, Local}; use chrono::{Date, DateTime, Datelike, Local};
pub struct Calendar { pub struct Calendar {
cur_date: Date<Local>, cur_date: Date<Local>,
@@ -149,7 +149,7 @@ fn draw_month(
impl Calendar { impl Calendar {
pub fn new() -> Calendar { pub fn new() -> Calendar {
Calendar{ Calendar {
cur_date: Local::now().date(), cur_date: Local::now().date(),
first_draw: true, first_draw: true,
offset: 0, offset: 0,
@@ -167,17 +167,21 @@ impl ModuleImpl for Calendar {
let time = time.date(); let time = time.date();
let mut t = time.with_day(1).unwrap(); let mut t = time.with_day(1).unwrap();
if self.offset != 0 { if self.offset != 0 {
let mut new_month = t.month() as i32 + self.offset; let mut month = (t.month() - 1) as i32 + self.offset;
let mut new_year = t.year(); let mut year = t.year();
while new_month > 12 { while month > 11 {
new_year += 1; year += 1;
new_month -= 12; month -= 12;
} }
while new_month < 0 { while month < 0 {
new_year -= 1; year -= 1;
new_month += 12; 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(); let mut damage: Vec<(i32, i32, i32, i32)> = Vec::new();
damage.push(draw_month( damage.push(draw_month(
@@ -206,11 +210,11 @@ impl ModuleImpl for Calendar {
Ok(damage) Ok(damage)
} }
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error>{ fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
if self.first_draw { if self.first_draw {
self.first_draw = false; self.first_draw = false;
Ok(true) Ok(true)
} else if time.date() != self.cur_date || force{ } else if time.date() != self.cur_date || force {
self.cur_date = time.date(); self.cur_date = time.date();
Ok(true) Ok(true)
} else { } else {
@@ -220,15 +224,15 @@ impl ModuleImpl for Calendar {
fn input(&mut self, input: Input) { fn input(&mut self, input: Input) {
match input { match input {
Input::Scroll{pos: _, x: _, y} => { Input::Scroll { pos: _, x: _, y } => {
if y < 0.0 { if y > 0.0 {
self.offset += 1; self.offset += 1;
} else { } else {
self.offset -= 1; self.offset -= 1;
} }
self.first_draw = true; self.first_draw = true;
}, }
Input::Click{pos: _, button: _} => { Input::Click { pos: _, button: _ } => {
self.offset = 0; self.offset = 0;
self.first_draw = true; self.first_draw = true;
} }
+53 -51
View File
@@ -1,68 +1,70 @@
use crate::buffer::Buffer; use crate::buffer::Buffer;
use crate::color::Color; use crate::color::Color;
use crate::module::{ModuleImpl, Input};
use crate::draw::{draw_text, draw_text_fixed_width, ROBOTO_REGULAR}; use crate::draw::{draw_text, draw_text_fixed_width, ROBOTO_REGULAR};
use crate::module::{Input, ModuleImpl};
use chrono::{DateTime, Datelike, Local, Timelike}; use chrono::{DateTime, Datelike, Local, Timelike};
pub struct Clock { pub struct Clock {
cur_time: DateTime<Local>, cur_time: DateTime<Local>,
} }
impl Clock { impl Clock {
pub fn new() -> Clock { pub fn new() -> Clock {
Clock{ Clock {
cur_time: Local::now(), cur_time: Local::now(),
} }
} }
} }
impl ModuleImpl for Clock { impl ModuleImpl for Clock {
fn draw(&self, fn draw(
buf: &mut Buffer, &self,
bg: &Color, buf: &mut Buffer,
time: &DateTime<Local>, bg: &Color,
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { time: &DateTime<Local>,
buf.memset(bg); ) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
buf.memset(bg);
draw_text( draw_text(
ROBOTO_REGULAR, ROBOTO_REGULAR,
&mut buf.subdimensions((0, 0, 448, 64)), &mut buf.subdimensions((0, 0, 448, 64)),
bg, bg,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
64.0, 64.0,
&format!( &format!(
"{:?}, {:02}/{:02}/{:4}", "{:?}, {:02}/{:02}/{:4}",
time.weekday(), time.weekday(),
time.day(), time.day(),
time.month(), time.month(),
time.year() time.year()
), ),
)?; )?;
draw_text_fixed_width( draw_text_fixed_width(
ROBOTO_REGULAR, ROBOTO_REGULAR,
&mut buf.subdimensions((0, 64, 288 * 2 + 64, 256)), &mut buf.subdimensions((0, 64, 288 * 2 + 64, 256)),
bg, bg,
&Color::new(1.0, 1.0, 1.0, 1.0), &Color::new(1.0, 1.0, 1.0, 1.0),
256.0, 256.0,
vec![120, 120, 64, 120, 120], vec![120, 120, 64, 120, 120],
&format!("{:02}:{:02}", time.hour(), time.minute()), &format!("{:02}:{:02}", time.hour(), time.minute()),
)?; )?;
Ok(vec![buf.get_signed_bounds()]) Ok(vec![buf.get_signed_bounds()])
}
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
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) {} fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
} 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) {}
}
-1
View File
@@ -101,4 +101,3 @@ pub fn draw_text_fixed_width(
Ok(()) Ok(())
} }
+125 -84
View File
@@ -15,32 +15,32 @@ use os_pipe::pipe;
use chrono::{Duration as ChronoDuration, Local, Timelike}; use chrono::{Duration as ChronoDuration, Local, Timelike};
use sctk::keyboard::{map_keyboard_auto_with_repeat, Event as KbEvent, KeyRepeatKind}; 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::reexports::client::{Display, EventQueue, NewProxy};
use sctk::utils::DoubleMemPool; use sctk::utils::DoubleMemPool;
use sctk::window::{ConceptFrame, Event as WEvent, Window}; use sctk::window::{ConceptFrame, Event as WEvent, Window};
use sctk::Environment; use sctk::Environment;
mod buffer;
mod color;
mod module;
mod draw;
mod clock;
mod calendar;
mod backlight; 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::backlight::Backlight;
use crate::buffer::Buffer;
use crate::calendar::Calendar; use crate::calendar::Calendar;
use crate::clock::Clock;
use crate::color::Color;
use crate::module::{Input, Module};
enum Cmd { enum Cmd {
Exit, Exit,
Configure, Configure,
Draw, Draw,
Input{pos: (u32, u32), input: Input}, Input { pos: (u32, u32), input: Input },
} }
struct App { struct App {
@@ -72,14 +72,16 @@ impl App {
let mmap = pool.mmap(); let mmap = pool.mmap();
let mut buf = Buffer::new(mmap, self.dimensions); let mut buf = Buffer::new(mmap, self.dimensions);
let mut margin_buf = buf.subdimensions((20, 20, buf_x - 40, buf_y - 40)); 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 bg = Color::new(0.0, 0.0, 0.0, 0.9);
let mut damage = vec![]; let mut damage = vec![];
for module in self.modules.iter() { for module in self.modules.iter() {
if module.update(&time, force)? { if module.update(&time, force)? {
let mut d = module.draw(&mut margin_buf, &bg, &time)?; let mut b = &mut margin_buf.subdimensions(module.get_bounds());
damage.append(&mut d); let mut d = module.draw(&mut b, &bg, &time)?;
} damage.append(&mut d);
}
} }
mmap.flush().unwrap(); mmap.flush().unwrap();
@@ -134,12 +136,12 @@ impl App {
} }
fn get_module(&self, pos: (u32, u32)) -> Option<&Module> { fn get_module(&self, pos: (u32, u32)) -> Option<&Module> {
for m in self.modules.iter() { for m in self.modules.iter() {
if m.intersect(pos) { if m.intersect(pos) {
return Some(&m) return Some(&m);
} }
} }
None None
} }
fn new(dimensions: (u32, u32)) -> App { fn new(dimensions: (u32, u32)) -> App {
@@ -197,64 +199,102 @@ impl App {
let pointer_clone = cmd_queue.clone(); let pointer_clone = cmd_queue.clone();
seat.get_pointer(move |ptr| { seat.get_pointer(move |ptr| {
let mut pos: (u32, u32) = (0, 0); let mut pos: (u32, u32) = (0, 0);
let mut vert_scroll: f64 = 0.0; let mut vert_scroll: f64 = 0.0;
let mut horiz_scroll: f64 = 0.0; let mut horiz_scroll: f64 = 0.0;
let mut btn: u32 = 0; let mut btn: u32 = 0;
let mut btn_clicked = false; let mut btn_clicked = false;
ptr.implement_closure( ptr.implement_closure(
move |evt, _| match evt { move |evt, _| match evt {
wl_pointer::Event::Enter{ serial: _, surface: _, surface_x, surface_y } => { wl_pointer::Event::Enter {
pos = (surface_x as u32, surface_y as u32); serial: _,
}, surface: _,
wl_pointer::Event::Leave{ serial: _, surface: _ } => { surface_x,
pos = (0, 0); surface_y,
}, } => {
wl_pointer::Event::Motion{ time: _, surface_x, surface_y } => { pos = (surface_x as u32, surface_y as u32);
pos = (surface_x as u32, surface_y as u32); }
}, wl_pointer::Event::Leave {
wl_pointer::Event::Axis{ time: _, axis, value } => { serial: _,
if axis == wl_pointer::Axis::VerticalScroll { surface: _,
vert_scroll += value; } => {
} pos = (0, 0);
}, }
wl_pointer::Event::Button{ serial: _, time: _, button, state } => match state { wl_pointer::Event::Motion {
wl_pointer::ButtonState::Released => { time: _,
btn = button; surface_x,
btn_clicked = true; surface_y,
}, } => {
_ => {} pos = (surface_x as u32, surface_y as u32);
}, }
wl_pointer::Event::Frame => { wl_pointer::Event::Axis {
if pos.0 < 20 || pos.1 < 20 { time: _,
// Ignore stuff outside our margins axis,
return value,
} } => {
let pos = (pos.0-20, pos.1-20); if axis == wl_pointer::Axis::VerticalScroll {
if vert_scroll != 0.0 || horiz_scroll != 0.0 { vert_scroll += value;
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; wl_pointer::Event::Button {
} serial: _,
if btn_clicked { time: _,
pointer_clone.lock().unwrap().push_back(Cmd::Input{pos: pos, input: Input::Click{pos: pos, button: btn}}); button,
btn_clicked = false; state,
} } => match state {
} wl_pointer::ButtonState::Released => {
_ => {} btn = button;
}, btn_clicked = true;
(), }
) _ => {}
}) },
.unwrap(); 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_title("dashboard".to_string());
window.set_app_id("dashboard".to_string()); window.set_app_id("dashboard".to_string());
let modules = vec![ let mut modules = vec![
Module::new(Box::new(Clock::new()), (0, 0, 720, 320)), Module::new(Box::new(Clock::new()), (0, 0, 720, 320)),
Module::new(Box::new(Calendar::new()), (0, 384, 1280, 344)), Module::new(Box::new(Calendar::new()), (0, 384, 1280, 344)),
Module::new(Box::new(Backlight::default().unwrap()), (720, 0, 256, 24))]; ];
if let Ok(m) = Backlight::new() {
modules.push(Module::new(Box::new(m), (720, 0, 256, 24)));
}
App { App {
window: window, window: window,
@@ -309,14 +349,15 @@ fn main() {
Cmd::Draw => { Cmd::Draw => {
app.redraw(false).expect("Failed to draw"); app.redraw(false).expect("Failed to draw");
app.flush_display(); app.flush_display();
}, }
Cmd::Input{pos, input} => { Cmd::Input { pos, input } => {
if pos.0 >= 20 || pos.1 >= 20 { // We need to deal with our margin. if pos.0 >= 20 || pos.1 >= 20 {
if let Some(m) = app.get_module(pos) { // We need to deal with our margin.
m.input(input); if let Some(m) = app.get_module(pos) {
q.lock().unwrap().push_back(Cmd::Draw); m.input(input);
} q.lock().unwrap().push_back(Cmd::Draw);
} }
}
} }
Cmd::Exit => { Cmd::Exit => {
std::process::exit(0); std::process::exit(0);
+43 -29
View File
@@ -6,45 +6,59 @@ use std::sync::{Arc, Mutex};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
pub enum Input { pub enum Input {
Click{pos: (u32, u32), button: u32}, Click { pos: (u32, u32), button: u32 },
Scroll{pos: (u32, u32), x: f64, y: f64}, Scroll { pos: (u32, u32), x: f64, y: f64 },
} }
pub trait ModuleImpl { pub trait ModuleImpl {
fn draw(&self, buf: &mut Buffer, bg: &Color, time: &DateTime<Local>) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error>; fn draw(
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error>; &self,
fn input(&mut self, input: Input); buf: &mut Buffer,
bg: &Color,
time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error>;
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error>;
fn input(&mut self, input: Input);
} }
pub struct Module { pub struct Module {
m: Arc<Mutex<Box<dyn ModuleImpl>>>, m: Arc<Mutex<Box<dyn ModuleImpl>>>,
pos: (u32, u32, u32, u32), pos: (u32, u32, u32, u32),
} }
impl Module { impl Module {
pub fn new(m: Box<dyn ModuleImpl>, pos: (u32, u32, u32, u32)) -> Module { pub fn new(m: Box<dyn ModuleImpl>, pos: (u32, u32, u32, u32)) -> Module {
Module{ Module {
m: Arc::new(Mutex::new(m)), m: Arc::new(Mutex::new(m)),
pos: pos, pos: pos,
} }
} }
pub fn update(&self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> { pub fn get_bounds(&self) -> (u32, u32, u32, u32) {
self.m.lock().unwrap().update(time, force) self.pos
} }
pub fn draw(&self, buf: &mut Buffer, bg: &Color, time: &DateTime<Local>) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { pub fn update(&self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
self.m.lock().unwrap().draw(&mut buf.subdimensions(self.pos), bg, time) self.m.lock().unwrap().update(time, force)
} }
pub fn intersect(&self, pos: (u32, u32)) -> bool { pub fn draw(
pos.0 >= self.pos.0 && &self,
pos.0 < (self.pos.0 + self.pos.2) && buf: &mut Buffer,
pos.1 >= self.pos.1 && bg: &Color,
pos.1 < (self.pos.1 + self.pos.3) time: &DateTime<Local>,
} ) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
self.m.lock().unwrap().draw(buf, bg, time)
}
pub fn input(&self, input: Input) { pub fn intersect(&self, pos: (u32, u32)) -> bool {
self.m.lock().unwrap().input(input) 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)
}
}