2016-09-01 17:24:20 +00:00
|
|
|
//! Process window events
|
|
|
|
use std::sync::{Arc, mpsc};
|
|
|
|
|
|
|
|
use glutin;
|
|
|
|
|
|
|
|
use input;
|
2016-09-23 17:12:11 +00:00
|
|
|
use sync::FairMutex;
|
2016-09-01 17:24:20 +00:00
|
|
|
use term::Term;
|
2016-10-14 23:38:15 +00:00
|
|
|
use util::encode_char;
|
2016-09-01 17:24:20 +00:00
|
|
|
|
|
|
|
/// The event processor
|
2016-09-24 23:11:50 +00:00
|
|
|
pub struct Processor<N> {
|
|
|
|
notifier: N,
|
2016-09-01 17:24:20 +00:00
|
|
|
input_processor: input::Processor,
|
2016-09-23 17:12:11 +00:00
|
|
|
terminal: Arc<FairMutex<Term>>,
|
2016-09-01 17:24:20 +00:00
|
|
|
resize_tx: mpsc::Sender<(u32, u32)>,
|
|
|
|
}
|
|
|
|
|
2016-09-24 23:11:50 +00:00
|
|
|
impl<N: input::Notify> Processor<N> {
|
2016-09-01 17:24:20 +00:00
|
|
|
/// Create a new event processor
|
|
|
|
///
|
|
|
|
/// Takes a writer which is expected to be hooked up to the write end of a
|
|
|
|
/// pty.
|
2016-09-24 23:11:50 +00:00
|
|
|
pub fn new(
|
|
|
|
notifier: N,
|
|
|
|
terminal: Arc<FairMutex<Term>>,
|
|
|
|
resize_tx: mpsc::Sender<(u32, u32)>
|
|
|
|
) -> Processor<N> {
|
2016-09-01 17:24:20 +00:00
|
|
|
Processor {
|
2016-09-24 23:11:50 +00:00
|
|
|
notifier: notifier,
|
2016-09-01 17:24:20 +00:00
|
|
|
terminal: terminal,
|
|
|
|
input_processor: input::Processor::new(),
|
|
|
|
resize_tx: resize_tx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_event(&mut self, event: glutin::Event) {
|
|
|
|
match event {
|
|
|
|
glutin::Event::Closed => panic!("window closed"), // TODO ...
|
2016-11-12 03:59:18 +00:00
|
|
|
// glutin::Event::ReceivedCharacter(c) => {
|
|
|
|
// match c {
|
|
|
|
// // Ignore BACKSPACE and DEL. These are handled specially.
|
|
|
|
// '\u{8}' | '\u{7f}' => (),
|
|
|
|
// // Extra thing on macOS delete?
|
|
|
|
// '\u{f728}' => (),
|
|
|
|
// // OSX arrow keys send invalid characters; ignore.
|
|
|
|
// '\u{f700}' | '\u{f701}' | '\u{f702}' | '\u{f703}' => (),
|
|
|
|
// // Same with home/end. Am I missing something? Would be
|
|
|
|
// // nice if glutin provided the received char in
|
|
|
|
// // KeyboardInput event so a choice could be made there
|
|
|
|
// // instead of having to special case everything.
|
|
|
|
// '\u{f72b}' | '\u{f729}' | '\u{f72c}' | '\u{f72d}' => (),
|
|
|
|
// // These letters are handled in the bindings system
|
|
|
|
// 'v' => (),
|
|
|
|
// _ => {
|
|
|
|
// println!("printing char {:?}", c);
|
|
|
|
// let buf = encode_char(c);
|
|
|
|
// self.notifier.notify(buf);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// },
|
2016-09-01 17:24:20 +00:00
|
|
|
glutin::Event::Resized(w, h) => {
|
|
|
|
self.resize_tx.send((w, h)).expect("send new size");
|
2016-09-06 14:43:03 +00:00
|
|
|
// Acquire term lock
|
2016-09-23 17:12:11 +00:00
|
|
|
let mut terminal = self.terminal.lock();
|
2016-09-06 14:43:03 +00:00
|
|
|
terminal.dirty = true;
|
2016-09-01 17:24:20 +00:00
|
|
|
},
|
2016-11-12 03:59:18 +00:00
|
|
|
glutin::Event::KeyboardInput(state, _code, key, mods, string) => {
|
2016-09-01 17:24:20 +00:00
|
|
|
// Acquire term lock
|
2016-09-23 17:12:11 +00:00
|
|
|
let terminal = self.terminal.lock();
|
2016-09-24 23:11:50 +00:00
|
|
|
let processor = &mut self.input_processor;
|
|
|
|
let notifier = &mut self.notifier;
|
2016-09-01 17:24:20 +00:00
|
|
|
|
2016-11-12 03:59:18 +00:00
|
|
|
processor.process_key(state, key, mods, notifier, *terminal.mode(), string);
|
2016-09-27 05:22:28 +00:00
|
|
|
},
|
|
|
|
glutin::Event::MouseInput(state, button) => {
|
|
|
|
let terminal = self.terminal.lock();
|
|
|
|
let processor = &mut self.input_processor;
|
|
|
|
let notifier = &mut self.notifier;
|
|
|
|
|
|
|
|
processor.mouse_input(state, button, notifier, *terminal.mode());
|
2016-09-01 17:24:20 +00:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process at least one event and handle any additional queued events.
|
|
|
|
pub fn process_events(&mut self, window: &glutin::Window) {
|
|
|
|
for event in window.wait_events() {
|
|
|
|
self.handle_event(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for event in window.poll_events() {
|
|
|
|
self.handle_event(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|