2016-09-01 13:24:20 -04:00
|
|
|
//! Process window events
|
2016-11-19 19:16:20 -05:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
2016-09-01 13:24:20 -04:00
|
|
|
use std::sync::{Arc, mpsc};
|
2016-11-19 19:16:20 -05:00
|
|
|
use serde_json as json;
|
2016-09-01 13:24:20 -04:00
|
|
|
|
|
|
|
use glutin;
|
2016-12-11 01:44:13 -05:00
|
|
|
|
2016-12-12 12:31:48 -05:00
|
|
|
use config::Config;
|
|
|
|
use display::OnResize;
|
2016-12-25 18:54:01 -05:00
|
|
|
use input::{self, ActionContext};
|
2016-12-22 13:43:06 -05:00
|
|
|
use selection::Selection;
|
2016-09-23 13:12:11 -04:00
|
|
|
use sync::FairMutex;
|
2016-12-12 12:31:48 -05:00
|
|
|
use term::{Term, SizeInfo};
|
|
|
|
use window::Window;
|
2016-09-01 13:24:20 -04:00
|
|
|
|
|
|
|
/// The event processor
|
2016-09-24 19:11:50 -04:00
|
|
|
pub struct Processor<N> {
|
|
|
|
notifier: N,
|
2016-09-01 13:24:20 -04:00
|
|
|
input_processor: input::Processor,
|
2016-09-23 13:12:11 -04:00
|
|
|
terminal: Arc<FairMutex<Term>>,
|
2016-09-01 13:24:20 -04:00
|
|
|
resize_tx: mpsc::Sender<(u32, u32)>,
|
2016-11-19 19:16:20 -05:00
|
|
|
ref_test: bool,
|
2016-12-22 13:43:06 -05:00
|
|
|
pub selection: Selection,
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|
|
|
|
|
2016-12-12 12:31:48 -05:00
|
|
|
/// Notify that the terminal was resized
|
|
|
|
///
|
|
|
|
/// Currently this just forwards the notice to the input processor.
|
|
|
|
impl<N> OnResize for Processor<N> {
|
|
|
|
fn on_resize(&mut self, size: &SizeInfo) {
|
|
|
|
self.input_processor.resize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 19:11:50 -04:00
|
|
|
impl<N: input::Notify> Processor<N> {
|
2016-09-01 13:24:20 -04: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 19:11:50 -04:00
|
|
|
pub fn new(
|
|
|
|
notifier: N,
|
|
|
|
terminal: Arc<FairMutex<Term>>,
|
2016-11-15 12:38:50 -05:00
|
|
|
resize_tx: mpsc::Sender<(u32, u32)>,
|
|
|
|
config: &Config,
|
2016-11-19 19:16:20 -05:00
|
|
|
ref_test: bool,
|
2016-09-24 19:11:50 -04:00
|
|
|
) -> Processor<N> {
|
2016-12-12 01:02:03 -05:00
|
|
|
let input_processor = {
|
|
|
|
let terminal = terminal.lock();
|
|
|
|
input::Processor::new(config, terminal.size_info())
|
|
|
|
};
|
|
|
|
|
2016-09-01 13:24:20 -04:00
|
|
|
Processor {
|
2016-09-24 19:11:50 -04:00
|
|
|
notifier: notifier,
|
2016-09-01 13:24:20 -04:00
|
|
|
terminal: terminal,
|
2016-12-12 01:02:03 -05:00
|
|
|
input_processor: input_processor,
|
2016-09-01 13:24:20 -04:00
|
|
|
resize_tx: resize_tx,
|
2016-11-19 19:16:20 -05:00
|
|
|
ref_test: ref_test,
|
2016-12-22 13:43:06 -05:00
|
|
|
selection: Default::default(),
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 02:32:12 -05:00
|
|
|
fn handle_event(&mut self, event: glutin::Event, wakeup_request: &mut bool) {
|
2016-09-01 13:24:20 -04:00
|
|
|
match event {
|
2016-11-19 19:16:20 -05:00
|
|
|
glutin::Event::Closed => {
|
|
|
|
if self.ref_test {
|
|
|
|
// dump grid state
|
|
|
|
let terminal = self.terminal.lock();
|
|
|
|
let grid = terminal.grid();
|
|
|
|
|
|
|
|
let serialized_grid = json::to_string(&grid)
|
|
|
|
.expect("serialize grid");
|
|
|
|
|
|
|
|
let serialized_size = json::to_string(terminal.size_info())
|
|
|
|
.expect("serialize size");
|
|
|
|
|
|
|
|
File::create("./grid.json")
|
|
|
|
.and_then(|mut f| f.write_all(serialized_grid.as_bytes()))
|
|
|
|
.expect("write grid.json");
|
|
|
|
|
|
|
|
File::create("./size.json")
|
|
|
|
.and_then(|mut f| f.write_all(serialized_size.as_bytes()))
|
|
|
|
.expect("write size.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME
|
|
|
|
panic!("window closed");
|
|
|
|
},
|
2016-09-01 13:24:20 -04:00
|
|
|
glutin::Event::Resized(w, h) => {
|
|
|
|
self.resize_tx.send((w, h)).expect("send new size");
|
2016-12-12 01:02:03 -05:00
|
|
|
|
|
|
|
// Previously, this marked the terminal state as "dirty", but
|
|
|
|
// now the wakeup_request controls whether a display update is
|
|
|
|
// triggered.
|
|
|
|
*wakeup_request = true;
|
2016-09-01 13:24:20 -04:00
|
|
|
},
|
2016-11-11 22:59:18 -05:00
|
|
|
glutin::Event::KeyboardInput(state, _code, key, mods, string) => {
|
2016-09-01 13:24:20 -04:00
|
|
|
// Acquire term lock
|
2016-09-23 13:12:11 -04:00
|
|
|
let terminal = self.terminal.lock();
|
2016-09-24 19:11:50 -04:00
|
|
|
let processor = &mut self.input_processor;
|
2016-09-01 13:24:20 -04:00
|
|
|
|
2016-12-25 18:54:01 -05:00
|
|
|
{
|
|
|
|
let mut context = ActionContext {
|
|
|
|
terminal: &terminal,
|
|
|
|
notifier: &mut self.notifier,
|
|
|
|
selection: &mut self.selection,
|
|
|
|
};
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2016-12-25 18:54:01 -05:00
|
|
|
processor.process_key(&mut context, state, key, mods, string);
|
|
|
|
}
|
|
|
|
self.selection.clear();
|
2016-09-27 01:22:28 -04:00
|
|
|
},
|
|
|
|
glutin::Event::MouseInput(state, button) => {
|
|
|
|
let terminal = self.terminal.lock();
|
|
|
|
let processor = &mut self.input_processor;
|
|
|
|
|
2016-12-25 18:54:01 -05:00
|
|
|
let mut context = ActionContext {
|
|
|
|
terminal: &terminal,
|
|
|
|
notifier: &mut self.notifier,
|
|
|
|
selection: &mut self.selection,
|
|
|
|
};
|
|
|
|
|
|
|
|
processor.mouse_input(&mut context, state, button);
|
2016-12-22 13:43:06 -05:00
|
|
|
*wakeup_request = true;
|
2016-11-23 23:25:37 -05:00
|
|
|
},
|
|
|
|
glutin::Event::MouseMoved(x, y) => {
|
|
|
|
if x > 0 && y > 0 {
|
2016-12-22 13:43:06 -05:00
|
|
|
let terminal = self.terminal.lock();
|
|
|
|
self.input_processor.mouse_moved(
|
|
|
|
&mut self.selection,
|
|
|
|
*terminal.mode(),
|
|
|
|
x as u32,
|
|
|
|
y as u32
|
|
|
|
);
|
|
|
|
if !self.selection.is_empty() {
|
|
|
|
*wakeup_request = true;
|
|
|
|
}
|
2016-11-23 23:25:37 -05:00
|
|
|
}
|
2016-09-01 13:24:20 -04:00
|
|
|
},
|
2016-11-14 16:34:30 -05:00
|
|
|
glutin::Event::Focused(true) => {
|
|
|
|
let mut terminal = self.terminal.lock();
|
|
|
|
terminal.dirty = true;
|
2016-11-24 22:50:34 -05:00
|
|
|
},
|
|
|
|
glutin::Event::MouseWheel(scroll_delta, touch_phase) => {
|
2016-12-25 18:54:01 -05:00
|
|
|
let mut terminal = self.terminal.lock();
|
2016-11-24 22:50:34 -05:00
|
|
|
let processor = &mut self.input_processor;
|
2016-12-25 18:54:01 -05:00
|
|
|
|
|
|
|
let mut context = ActionContext {
|
|
|
|
terminal: &terminal,
|
|
|
|
notifier: &mut self.notifier,
|
|
|
|
selection: &mut self.selection,
|
|
|
|
};
|
2016-11-24 22:50:34 -05:00
|
|
|
|
|
|
|
processor.on_mouse_wheel(
|
2016-12-25 18:54:01 -05:00
|
|
|
&mut context,
|
2016-11-24 22:50:34 -05:00
|
|
|
scroll_delta,
|
|
|
|
touch_phase,
|
|
|
|
);
|
|
|
|
},
|
2016-12-11 02:32:12 -05:00
|
|
|
glutin::Event::Awakened => {
|
|
|
|
*wakeup_request = true;
|
|
|
|
},
|
2016-09-01 13:24:20 -04:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process at least one event and handle any additional queued events.
|
2016-12-11 02:32:12 -05:00
|
|
|
pub fn process_events(&mut self, window: &Window) -> bool {
|
|
|
|
let mut wakeup_request = false;
|
2016-09-01 13:24:20 -04:00
|
|
|
for event in window.wait_events() {
|
2016-12-11 02:32:12 -05:00
|
|
|
self.handle_event(event, &mut wakeup_request);
|
2016-09-01 13:24:20 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for event in window.poll_events() {
|
2016-12-11 02:32:12 -05:00
|
|
|
self.handle_event(event, &mut wakeup_request);
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|
2016-12-11 02:32:12 -05:00
|
|
|
|
|
|
|
wakeup_request
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|
2016-11-15 12:38:50 -05:00
|
|
|
|
|
|
|
pub fn update_config(&mut self, config: &Config) {
|
|
|
|
self.input_processor.update_config(config);
|
|
|
|
}
|
2016-09-01 13:24:20 -04:00
|
|
|
}
|