aboutsummaryrefslogtreecommitdiff
path: root/src/event.rs
blob: d8fec37eb48d21206cf03ae860e2b96b7a8e9c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Process window events
use std::fs::File;
use std::io::Write;
use std::sync::{Arc, mpsc};
use serde_json as json;

use glutin;

use window::Window;

use input;
use sync::FairMutex;
use term::Term;
use config::Config;

/// The event processor
pub struct Processor<N> {
    notifier: N,
    input_processor: input::Processor,
    terminal: Arc<FairMutex<Term>>,
    resize_tx: mpsc::Sender<(u32, u32)>,
    ref_test: bool,
}

impl<N: input::Notify> Processor<N> {
    /// Create a new event processor
    ///
    /// Takes a writer which is expected to be hooked up to the write end of a
    /// pty.
    pub fn new(
        notifier: N,
        terminal: Arc<FairMutex<Term>>,
        resize_tx: mpsc::Sender<(u32, u32)>,
        config: &Config,
        ref_test: bool,
    ) -> Processor<N> {
        Processor {
            notifier: notifier,
            terminal: terminal,
            input_processor: input::Processor::new(config),
            resize_tx: resize_tx,
            ref_test: ref_test,
        }
    }

    fn handle_event(&mut self, event: glutin::Event, wakeup_request: &mut bool) {
        match event {
            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");
            },
            glutin::Event::Resized(w, h) => {
                self.resize_tx.send((w, h)).expect("send new size");
                // Acquire term lock
                let mut terminal = self.terminal.lock();
                terminal.dirty = true;
            },
            glutin::Event::KeyboardInput(state, _code, key, mods, string) => {
                // Acquire term lock
                let terminal = self.terminal.lock();
                let processor = &mut self.input_processor;
                let notifier = &mut self.notifier;

                processor.process_key(state, key, mods, notifier, *terminal.mode(), string);
            },
            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);
            },
            glutin::Event::MouseMoved(x, y) => {
                if x > 0 && y > 0 {
                    self.input_processor.mouse_moved(x as u32, y as u32);
                }
            },
            glutin::Event::Focused(true) => {
                let mut terminal = self.terminal.lock();
                terminal.dirty = true;
            },
            glutin::Event::MouseWheel(scroll_delta, touch_phase) => {
                let terminal = self.terminal.lock();
                let processor = &mut self.input_processor;
                let notifier = &mut self.notifier;

                processor.on_mouse_wheel(
                    notifier,
                    scroll_delta,
                    touch_phase,
                    &terminal
                );
            },
            glutin::Event::Awakened => {
                *wakeup_request = true;
            },
            _ => (),
        }
    }

    /// Process at least one event and handle any additional queued events.
    pub fn process_events(&mut self, window: &Window) -> bool {
        let mut wakeup_request = false;
        for event in window.wait_events() {
            self.handle_event(event, &mut wakeup_request);
            break;
        }

        for event in window.poll_events() {
            self.handle_event(event, &mut wakeup_request);
        }

        wakeup_request
    }

    pub fn update_config(&mut self, config: &Config) {
        self.input_processor.update_config(config);
    }
}