Files
wldash/src/main.rs
T

101 lines
3.2 KiB
Rust
Raw Normal View History

2019-07-30 16:31:42 +02:00
use std::io::{Read, Write};
2019-07-30 00:42:05 +02:00
use std::os::unix::io::AsRawFd;
2019-08-12 01:47:50 +02:00
use std::sync::mpsc::channel;
2019-07-30 00:42:05 +02:00
use nix::poll::{poll, PollFd, PollFlags};
2019-07-30 16:31:42 +02:00
use os_pipe::pipe;
2019-07-30 00:42:05 +02:00
2019-07-31 10:26:12 +02:00
mod buffer;
mod color;
mod draw;
2019-08-11 00:52:47 +02:00
mod modules;
2019-08-12 01:47:50 +02:00
mod app;
2019-07-30 00:42:05 +02:00
2019-08-12 01:47:50 +02:00
use app::{App, Cmd};
2019-07-30 00:42:05 +02:00
fn main() {
2019-07-30 16:31:42 +02:00
let (mut rx_pipe, mut tx_pipe) = pipe().unwrap();
2019-08-01 19:51:06 +02:00
let (tx_draw, rx_draw) = channel();
2019-07-30 16:31:42 +02:00
2019-08-04 14:23:16 +02:00
let mut app = App::new(tx_draw);
2019-08-05 23:45:59 +02:00
app.wipe();
2019-07-30 00:42:05 +02:00
2019-07-30 16:31:42 +02:00
let worker_queue = app.cmd_queue();
std::thread::spawn(move || loop {
2019-08-10 00:01:23 +02:00
if rx_draw.recv().unwrap() {
worker_queue.lock().unwrap().push_back(Cmd::Draw);
} else {
worker_queue.lock().unwrap().push_back(Cmd::ForceDraw);
}
2019-07-30 16:31:42 +02:00
tx_pipe.write_all(&[0x1]).unwrap();
});
let mut fds = [
PollFd::new(app.event_queue().get_connection_fd(), PollFlags::POLLIN),
PollFd::new(rx_pipe.as_raw_fd(), PollFlags::POLLIN),
];
2019-07-30 00:42:05 +02:00
2019-08-05 23:45:59 +02:00
app.cmd_queue().lock().unwrap().push_back(Cmd::Draw);
2019-07-30 00:42:05 +02:00
let q = app.cmd_queue();
loop {
let cmd = q.lock().unwrap().pop_front();
match cmd {
Some(cmd) => match cmd {
Cmd::Draw => {
2019-07-31 00:42:03 +02:00
app.redraw(false).expect("Failed to draw");
2019-07-30 00:42:05 +02:00
app.flush_display();
2019-07-31 10:26:12 +02:00
}
2019-08-10 00:01:23 +02:00
Cmd::ForceDraw => {
app.redraw(true).expect("Failed to draw");
app.flush_display();
}
2019-08-02 15:59:10 +02:00
Cmd::MouseInput { pos, input } => {
2019-07-31 17:37:15 +02:00
if let Some(m) = app.get_module(pos) {
let bounds = m.get_bounds();
let input = input.offset((bounds.0, bounds.1));
m.input(input);
q.lock().unwrap().push_back(Cmd::Draw);
2019-07-31 10:26:12 +02:00
}
2019-07-30 00:42:05 +02:00
}
2019-08-02 15:59:10 +02:00
Cmd::KeyboardInput { input } => {
2019-08-12 01:47:50 +02:00
app.with_modules(|m| {
2019-08-02 15:59:10 +02:00
m.input(input.clone());
2019-08-12 01:47:50 +02:00
});
2019-08-02 15:59:10 +02:00
q.lock().unwrap().push_back(Cmd::Draw);
}
2019-07-30 00:42:05 +02:00
Cmd::Exit => {
std::process::exit(0);
}
},
None => {
app.flush_display();
poll(&mut fds, -1).unwrap();
if fds[0].revents().unwrap().contains(PollFlags::POLLIN) {
if let Some(guard) = app.event_queue().prepare_read() {
if let Err(e) = guard.read_events() {
if e.kind() != ::std::io::ErrorKind::WouldBlock {
eprintln!(
"Error while trying to read from the wayland socket: {:?}",
e
);
}
}
}
app.event_queue()
.dispatch_pending()
.expect("Failed to dispatch all messages.");
}
if fds[1].revents().unwrap().contains(PollFlags::POLLIN) {
2019-07-30 16:31:42 +02:00
let mut v: Vec<u8> = vec![0x00];
rx_pipe.read_exact(&mut v).unwrap();
2019-07-30 00:42:05 +02:00
}
}
}
}
}