Add module system, backlight control

This commit is contained in:
Kenny Levinsen
2019-07-31 00:42:03 +02:00
parent 9d2dd4ec37
commit 0a4e0829b9
8 changed files with 611 additions and 234 deletions
+124 -31
View File
@@ -15,26 +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;
use sctk::reexports::client::protocol::{wl_shm, wl_pointer};
use sctk::reexports::client::{Display, EventQueue, NewProxy};
use sctk::utils::DoubleMemPool;
use sctk::window::{ConceptFrame, Event as WEvent, Window};
use sctk::Environment;
mod color;
mod buffer;
mod color;
mod module;
mod draw;
mod clock;
mod calendar;
mod backlight;
use crate::color::Color;
use crate::buffer::Buffer;
use crate::draw::{draw_clock, draw_calendar};
use crate::color::Color;
use crate::module::{Module, Input};
use crate::clock::Clock;
use crate::backlight::Backlight;
use crate::calendar::Calendar;
#[derive(Debug)]
enum Cmd {
Exit,
Configure,
MaybeDraw,
Draw,
Input{pos: (u32, u32), input: Input},
}
struct App {
@@ -44,10 +50,13 @@ struct App {
window: Window<ConceptFrame>,
cmd_queue: Arc<Mutex<VecDeque<Cmd>>>,
dimensions: (u32, u32),
modules: Vec<Module>,
}
impl App {
fn redraw(&mut self) -> Result<(), ::std::io::Error> {
fn redraw(&mut self, force: bool) -> Result<(), ::std::io::Error> {
let time = Local::now();
let pool = match self.pools.pool() {
Some(pool) => pool,
None => return Ok(()),
@@ -62,17 +71,16 @@ impl App {
let mmap = pool.mmap();
let mut buf = Buffer::new(mmap, self.dimensions);
let bg = Color::new(0.0, 0.0, 0.0, 0.8);
buf.memset(&bg);
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 time = Local::now();
draw_clock(&mut buf.subdimensions((0, 0, 720, 320)), &bg, &time)?;
draw_calendar(
&mut buf.subdimensions((0, 384, 1472, 384)),
&bg,
&time.date(),
)?;
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);
}
}
mmap.flush().unwrap();
@@ -85,6 +93,9 @@ impl App {
wl_shm::Format::Argb8888,
);
self.window.surface().attach(Some(&new_buffer), 0, 0);
for d in damage {
self.window.surface().damage(d.0, d.1, d.2, d.3);
}
self.window.surface().commit();
Ok(())
}
@@ -109,6 +120,28 @@ impl App {
&mut self.event_queue
}
fn wipe(&mut self) {
let pool = match self.pools.pool() {
Some(pool) => pool,
None => return,
};
pool.resize((4 * self.dimensions.0 * self.dimensions.1) as usize)
.expect("Failed to resize the memory pool.");
let mmap = pool.mmap();
let mut buf = Buffer::new(mmap, self.dimensions);
let bg = Color::new(0.0, 0.0, 0.0, 0.9);
buf.memset(&bg);
}
fn get_module(&self, pos: (u32, u32)) -> Option<&Module> {
for m in self.modules.iter() {
if m.intersect(pos) {
return Some(&m)
}
}
None
}
fn new(dimensions: (u32, u32)) -> App {
let cmd_queue = Arc::new(Mutex::new(VecDeque::new()));
@@ -128,7 +161,7 @@ impl App {
match evt {
WEvent::Close => return,
WEvent::Refresh => {
event_clone.lock().unwrap().push_back(Cmd::MaybeDraw);
event_clone.lock().unwrap().push_back(Cmd::Draw);
}
WEvent::Configure {
new_size: _,
@@ -162,9 +195,67 @@ impl App {
)
.expect("Failed to map keyboard");
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();
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))];
App {
window: window,
display: display,
@@ -172,6 +263,7 @@ impl App {
cmd_queue: cmd_queue,
pools: pools,
dimensions: dimensions,
modules: modules,
}
}
}
@@ -179,9 +271,7 @@ impl App {
fn main() {
let (mut rx_pipe, mut tx_pipe) = pipe().unwrap();
let mut app = App::new((1600u32, 784u32));
let mut last_time = Local::now();
let mut app = App::new((1320u32, 784u32));
let worker_queue = app.cmd_queue();
std::thread::spawn(move || loop {
@@ -195,7 +285,7 @@ fn main() {
let d = target - n;
std::thread::sleep(d.to_std().unwrap());
worker_queue.lock().unwrap().push_back(Cmd::MaybeDraw);
worker_queue.lock().unwrap().push_back(Cmd::Draw);
tx_pipe.write_all(&[0x1]).unwrap();
});
@@ -212,18 +302,21 @@ fn main() {
Cmd::Configure => {
let d = app.dimensions();
app.window().resize(d.0, d.1);
q.lock().unwrap().push_back(Cmd::Draw);
app.wipe();
app.redraw(true).expect("Failed to draw");
app.flush_display();
}
Cmd::Draw => {
app.redraw().expect("Failed to draw");
app.redraw(false).expect("Failed to draw");
app.flush_display();
last_time = Local::now();
}
Cmd::MaybeDraw => {
let t = Local::now();
if t.hour() != last_time.hour() || t.minute() != last_time.minute() {
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);