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
+38 -23
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,11 +70,16 @@ 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() {
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 { let mut dev = Backlight {
device_path: first_device.path(), device_path: first_device.path(),
cur_brightness: 0, cur_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,
@@ -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();
}, }
_ => {} _ => {}
} }
}, }
} }
} }
} }
+18 -14
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>,
@@ -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(
@@ -221,13 +225,13 @@ 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;
+8 -6
View File
@@ -1,7 +1,7 @@
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 {
@@ -17,7 +17,8 @@ impl Clock {
} }
impl ModuleImpl for Clock { impl ModuleImpl for Clock {
fn draw(&self, fn draw(
&self,
buf: &mut Buffer, buf: &mut Buffer,
bg: &Color, bg: &Color,
time: &DateTime<Local>, time: &DateTime<Local>,
@@ -53,10 +54,11 @@ impl ModuleImpl for Clock {
} }
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 time.date() != self.cur_time.date() || if time.date() != self.cur_time.date()
time.hour() != self.cur_time.hour() || || time.hour() != self.cur_time.hour()
time.minute() != self.cur_time.minute() || || time.minute() != self.cur_time.minute()
force { || force
{
self.cur_time = time.clone(); self.cur_time = time.clone();
Ok(true) Ok(true)
} else { } else {
-1
View File
@@ -101,4 +101,3 @@ pub fn draw_text_fixed_width(
Ok(()) Ok(())
} }
+71 -30
View File
@@ -15,26 +15,26 @@ 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,
@@ -72,12 +72,14 @@ 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());
let mut d = module.draw(&mut b, &bg, &time)?;
damage.append(&mut d); damage.append(&mut d);
} }
} }
@@ -136,7 +138,7 @@ 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
@@ -204,40 +206,74 @@ impl App {
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 {
serial: _,
surface: _,
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{ serial: _, surface: _ } => { wl_pointer::Event::Leave {
serial: _,
surface: _,
} => {
pos = (0, 0); pos = (0, 0);
}, }
wl_pointer::Event::Motion{ time: _, surface_x, 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::Axis{ time: _, axis, value } => { wl_pointer::Event::Axis {
time: _,
axis,
value,
} => {
if axis == wl_pointer::Axis::VerticalScroll { if axis == wl_pointer::Axis::VerticalScroll {
vert_scroll += value; vert_scroll += value;
} }
}, }
wl_pointer::Event::Button{ serial: _, time: _, button, state } => match state { wl_pointer::Event::Button {
serial: _,
time: _,
button,
state,
} => match state {
wl_pointer::ButtonState::Released => { wl_pointer::ButtonState::Released => {
btn = button; btn = button;
btn_clicked = true; btn_clicked = true;
}, }
_ => {} _ => {}
}, },
wl_pointer::Event::Frame => { wl_pointer::Event::Frame => {
if pos.0 < 20 || pos.1 < 20 { if pos.0 < 20 || pos.1 < 20 {
// Ignore stuff outside our margins // Ignore stuff outside our margins
return return;
} }
let pos = (pos.0 - 20, pos.1 - 20); let pos = (pos.0 - 20, pos.1 - 20);
if vert_scroll != 0.0 || horiz_scroll != 0.0 { 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}}); 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; vert_scroll = 0.0;
horiz_scroll = 0.0; horiz_scroll = 0.0;
} }
if btn_clicked { if btn_clicked {
pointer_clone.lock().unwrap().push_back(Cmd::Input{pos: pos, input: Input::Click{pos: pos, button: btn}}); pointer_clone.lock().unwrap().push_back(Cmd::Input {
pos: pos,
input: Input::Click {
pos: pos,
button: btn,
},
});
btn_clicked = false; btn_clicked = false;
} }
} }
@@ -251,10 +287,14 @@ impl App {
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,9 +349,10 @@ 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 {
// We need to deal with our margin.
if let Some(m) = app.get_module(pos) { if let Some(m) = app.get_module(pos) {
m.input(input); m.input(input);
q.lock().unwrap().push_back(Cmd::Draw); q.lock().unwrap().push_back(Cmd::Draw);
+21 -7
View File
@@ -11,7 +11,12 @@ pub enum Input {
} }
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(
&self,
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 update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error>;
fn input(&mut self, input: Input); fn input(&mut self, input: Input);
} }
@@ -29,19 +34,28 @@ impl Module {
} }
} }
pub fn get_bounds(&self) -> (u32, u32, u32, u32) {
self.pos
}
pub fn update(&self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> { pub fn update(&self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
self.m.lock().unwrap().update(time, force) self.m.lock().unwrap().update(time, force)
} }
pub fn draw(&self, buf: &mut Buffer, bg: &Color, time: &DateTime<Local>) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> { pub fn draw(
self.m.lock().unwrap().draw(&mut buf.subdimensions(self.pos), bg, time) &self,
buf: &mut Buffer,
bg: &Color,
time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
self.m.lock().unwrap().draw(buf, bg, time)
} }
pub fn intersect(&self, pos: (u32, u32)) -> bool { pub fn intersect(&self, pos: (u32, u32)) -> bool {
pos.0 >= self.pos.0 && pos.0 >= self.pos.0
pos.0 < (self.pos.0 + self.pos.2) && && pos.0 < (self.pos.0 + self.pos.2)
pos.1 >= self.pos.1 && && pos.1 >= self.pos.1
pos.1 < (self.pos.1 + self.pos.3) && pos.1 < (self.pos.1 + self.pos.3)
} }
pub fn input(&self, input: Input) { pub fn input(&self, input: Input) {