Fix dummy & blt backends

This commit is contained in:
Alexandre Bury 2018-07-19 19:45:26 -07:00
parent 2610b697a8
commit af4e55ed18
4 changed files with 58 additions and 23 deletions

View File

@ -28,6 +28,9 @@ enum ColorRole {
pub struct Backend { pub struct Backend {
buttons_pressed: HashSet<MouseButton>, buttons_pressed: HashSet<MouseButton>,
mouse_position: Vec2, mouse_position: Vec2,
inner_sender: Sender<Option<Event>>,
inner_receiver: Receiver<Option<Event>>,
} }
impl Backend { impl Backend {
@ -45,9 +48,13 @@ impl Backend {
}, },
]); ]);
let (inner_sender, inner_receiver) = crossbeam_channel::bounded(1);
let c = Backend { let c = Backend {
buttons_pressed: HashSet::new(), buttons_pressed: HashSet::new(),
mouse_position: Vec2::zero(), mouse_position: Vec2::zero(),
inner_sender,
inner_receiver,
}; };
Box::new(c) Box::new(c)
@ -307,10 +314,23 @@ impl backend::Backend for Backend {
terminal::print_xy(pos.x as i32, pos.y as i32, text); terminal::print_xy(pos.x as i32, pos.y as i32, text);
} }
fn prepare_input( fn start_input_thread(
&mut self, event_sink: &Sender<Option<Event>>, &mut self, event_sink: Sender<Option<Event>>,
input_request: backend::InputRequest, input_request: Receiver<backend::InputRequest>,
) { ) {
let receiver = self.inner_receiver.clone();
thread::spawn(move || {
for _ in input_requests {
match receiver.recv() {
None => return,
Some(event) => event_sink.send(event),
}
}
});
}
fn prepare_input(&mut self, input_request: backend::InputRequest) {
match input_request { match input_request {
backend::InputRequest::Peek => event_sink.send(self.parse_next()), backend::InputRequest::Peek => event_sink.send(self.parse_next()),
backend::InputRequest::Block => { backend::InputRequest::Block => {
@ -323,7 +343,7 @@ impl backend::Backend for Backend {
return; return;
} }
} }
event_sink.send(Some(Event::Refresh)); self.inner_sender.send(Some(Event::Refresh));
} }
} }
} }

View File

@ -1,19 +1,28 @@
//! Dummy backend //! Dummy backend
use std::thread;
use crossbeam_channel::{self, Receiver, Sender};
use backend; use backend;
use event; use event::Event;
use theme; use theme;
use vec::Vec2; use vec::Vec2;
use crossbeam_channel::Sender; pub struct Backend {
inner_sender: Sender<Option<Event>>,
pub struct Backend; inner_receiver: Receiver<Option<Event>>,
}
impl Backend { impl Backend {
pub fn init() -> Box<backend::Backend> pub fn init() -> Box<backend::Backend>
where where
Self: Sized, Self: Sized,
{ {
Box::new(Backend) let (inner_sender, inner_receiver) = crossbeam_channel::bounded(1);
Box::new(Backend {
inner_sender,
inner_receiver,
})
} }
} }
@ -30,11 +39,24 @@ impl backend::Backend for Backend {
(1, 1).into() (1, 1).into()
} }
fn prepare_input( fn prepare_input(&mut self, _input_request: backend::InputRequest) {
&mut self, event_sink: &Sender<Option<event::Event>>, self.inner_sender.send(Some(Event::Exit));
_input_request: backend::InputRequest, }
fn start_input_thread(
&mut self, event_sink: Sender<Option<Event>>,
input_requests: Receiver<backend::InputRequest>,
) { ) {
event_sink.send(Some(event::Event::Exit)) let receiver = self.inner_receiver.clone();
thread::spawn(move || {
for _ in input_requests {
match receiver.recv() {
None => return,
Some(event) => event_sink.send(event),
}
}
});
} }
fn print_at(&self, _: Vec2, _: &str) {} fn print_at(&self, _: Vec2, _: &str) {}

View File

@ -57,13 +57,9 @@ pub trait Backend {
/// ///
/// This is only required for non-thread-safe backends like BearLibTerminal /// This is only required for non-thread-safe backends like BearLibTerminal
/// where we cannot collect input in a separate thread. /// where we cannot collect input in a separate thread.
fn prepare_input( fn prepare_input(&mut self, input_request: InputRequest) {
&mut self, event_sink: &Sender<Option<Event>>,
input_request: InputRequest,
) {
// Dummy implementation for most backends. // Dummy implementation for most backends.
// Little trick to avoid unused variables. // Little trick to avoid unused variables.
let _ = event_sink;
let _ = input_request; let _ = input_request;
} }

View File

@ -605,8 +605,7 @@ impl Cursive {
} }
self.input_trigger.send(backend::InputRequest::Peek); self.input_trigger.send(backend::InputRequest::Peek);
self.backend self.backend.prepare_input(backend::InputRequest::Peek);
.prepare_input(&self.event_sink, backend::InputRequest::Peek);
self.event_source.recv().unwrap().map(Interruption::Event) self.event_source.recv().unwrap().map(Interruption::Event)
} }
@ -616,8 +615,7 @@ impl Cursive {
fn poll(&mut self) -> Option<Interruption> { fn poll(&mut self) -> Option<Interruption> {
if !self.expecting_event { if !self.expecting_event {
self.input_trigger.send(backend::InputRequest::Block); self.input_trigger.send(backend::InputRequest::Block);
self.backend self.backend.prepare_input(backend::InputRequest::Block);
.prepare_input(&self.event_sink, backend::InputRequest::Block);
self.expecting_event = true; self.expecting_event = true;
} }
@ -765,7 +763,6 @@ impl Cursive {
fn handle_interruption(&mut self, interruption: Interruption) { fn handle_interruption(&mut self, interruption: Interruption) {
match interruption { match interruption {
Interruption::Event(event) => { Interruption::Event(event) => {
// eprintln!("{:?}, {:?}", event, self.screen_size());
if event == Event::Exit { if event == Event::Exit {
self.quit(); self.quit();
} }