aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/wayland/context.rs
blob: f303b54a192546514256bda75147b8c05326caf0 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use Event as GlutinEvent;

use std::collections::{HashMap, VecDeque, HashSet};
use std::sync::{Arc, Mutex};

use libc::c_void;

use wayland_client::{EventIterator, Proxy, ProxyId};
use wayland_client::wayland::get_display;
use wayland_client::wayland::compositor::{WlCompositor, WlSurface};
use wayland_client::wayland::output::WlOutput;
use wayland_client::wayland::seat::{WlSeat, WlPointer};
use wayland_client::wayland::shell::{WlShell, WlShellSurface};
use wayland_client::wayland::shm::WlShm;
use wayland_client::wayland::subcompositor::WlSubcompositor;

use super::wayland_kbd::MappedKeyboard;
use super::wayland_window::DecoratedSurface;

lazy_static! {
    pub static ref WAYLAND_CONTEXT: Option<WaylandContext> = {
        WaylandContext::init()
    };
}

wayland_env!(InnerEnv,
    compositor: WlCompositor,
    seat: WlSeat,
    shell: WlShell,
    shm: WlShm,
    subcompositor: WlSubcompositor
);

pub struct WaylandFocuses {
    pub pointer: Option<WlPointer>,
    pub pointer_on: Option<ProxyId>,
    pub pointer_at: Option<(f64, f64)>,
    pub keyboard: Option<MappedKeyboard>,
    pub keyboard_on: Option<ProxyId>
}

pub struct WaylandContext {
    inner: InnerEnv,
    iterator: Mutex<EventIterator>,
    monitors: Vec<(WlOutput, u32, u32, String)>,
    queues: Mutex<HashMap<ProxyId, Arc<Mutex<VecDeque<GlutinEvent>>>>>,
    known_surfaces: Mutex<HashSet<ProxyId>>,
    focuses: Mutex<WaylandFocuses>
}

impl WaylandContext {
    fn init() -> Option<WaylandContext> {
        let display = match get_display() {
            Some(display) => display,
            None => return None
        };

        let (mut inner_env, iterator) = InnerEnv::init(display);

        let mut outputs_events = EventIterator::new();

        let mut monitors = inner_env.globals.iter()
            .flat_map(|&(id, _, _)| inner_env.rebind_id::<WlOutput>(id))
            .map(|(mut monitor, _)| {
                monitor.set_evt_iterator(&outputs_events);
                (monitor, 0, 0, String::new())
            }).collect();

        inner_env.display.sync_roundtrip().unwrap();

        super::monitor::init_monitors(&mut monitors, outputs_events);

        Some(WaylandContext {
            inner: inner_env,
            iterator: Mutex::new(iterator),
            monitors: monitors,
            queues: Mutex::new(HashMap::new()),
            known_surfaces: Mutex::new(HashSet::new()),
            focuses: Mutex::new(WaylandFocuses {
                pointer: None,
                pointer_on: None,
                pointer_at: None,
                keyboard: None,
                keyboard_on: None
            })
        })
    }

    pub fn new_surface(&self) -> Option<(WlSurface, Arc<Mutex<VecDeque<GlutinEvent>>>)> {
        self.inner.compositor.as_ref().map(|c| {
            let s = c.0.create_surface();
            let id = s.id();
            let queue = {
                let mut q = VecDeque::new();
                q.push_back(GlutinEvent::Refresh);
                Arc::new(Mutex::new(q))
            };
            self.queues.lock().unwrap().insert(id, queue.clone());
            self.known_surfaces.lock().unwrap().insert(id);
            (s, queue)
        })
    }

    pub fn dropped_surface(&self, id: ProxyId) {
        self.queues.lock().unwrap().remove(&id);
        self.known_surfaces.lock().unwrap().remove(&id);
    }

    pub fn decorated_from(&self, surface: &WlSurface, width: i32, height: i32) -> Option<DecoratedSurface> {
        let inner = &self.inner;
        match (&inner.compositor, &inner.subcompositor, &inner.shm, &inner.shell) {
            (&Some(ref compositor), &Some(ref subcompositor), &Some(ref shm), &Some(ref shell)) => {
                DecoratedSurface::new(
                    surface, width, height,
                    &compositor.0, &subcompositor.0, &shm.0, &shell.0,
                    self.inner.rebind::<WlSeat>().map(|(seat, _)| seat)
                ).ok()
            }
            _ => None
        }
    }

    pub fn plain_from(&self, surface: &WlSurface, fullscreen: Option<ProxyId>) -> Option<WlShellSurface> {
        use wayland_client::wayland::shell::WlShellSurfaceFullscreenMethod;

        let inner = &self.inner;
        if let Some((ref shell, _)) = inner.shell {
            let shell_surface = shell.get_shell_surface(surface);
            if let Some(monitor_id) = fullscreen {
                for m in &self.monitors {
                    if m.0.id() == monitor_id {
                        shell_surface.set_fullscreen(
                            WlShellSurfaceFullscreenMethod::Default,
                            0,
                            Some(&m.0)
                        );
                        return Some(shell_surface)
                    }
                }
            }
            shell_surface.set_toplevel();
            Some(shell_surface)
        } else {
            None
        }
    }

    pub fn display_ptr(&self) -> *const c_void {
        self.inner.display.ptr() as *const _
    }

    pub fn dispatch_events(&self) {
        self.inner.display.dispatch_pending().unwrap();
        let mut iterator = self.iterator.lock().unwrap();
        let mut focuses = self.focuses.lock().unwrap();
        let known_surfaces = self.known_surfaces.lock().unwrap();
        let queues = self.queues.lock().unwrap();
        // first, keyboard events
        let kdb_evts = super::keyboard::translate_kbd_events(&mut *focuses, &known_surfaces);
        for (evt, id) in kdb_evts {
            if let Some(q) = queues.get(&id) {
                q.lock().unwrap().push_back(evt);
            }
        }
        // then, the rest
        for evt in &mut *iterator {
            if let Some((evt, id)) = super::events::translate_event(
                evt, &mut *focuses, &known_surfaces,
                self.inner.seat.as_ref().map(|s| &s.0))
            {
                if let Some(q) = queues.get(&id) {
                    q.lock().unwrap().push_back(evt);
                }
            }
        }
    }

    pub fn flush_events(&self) -> ::std::io::Result<i32> {
        self.inner.display.flush()
    }

    pub fn read_events(&self) -> ::std::io::Result<Option<i32>> {
        let guard = match self.inner.display.prepare_read() {
            Some(g) => g,
            None => return Ok(None)
        };
        return guard.read_events().map(|i| Some(i));
    }

    pub fn monitor_ids(&self) -> Vec<ProxyId> {
        self.monitors.iter().map(|o| o.0.id()).collect()
    }

    pub fn monitor_name(&self, pid: ProxyId) -> Option<String> {
        for o in &self.monitors {
            if o.0.id() == pid {
                return Some(o.3.clone())
            }
        }
        None
    }

    pub fn monitor_dimensions(&self, pid: ProxyId) -> Option<(u32, u32)> {
        for o in &self.monitors {
            if o.0.id() == pid {
                return Some((o.1, o.2))
            }
        }
        None
    }
}