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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/

//! This module contains code responsible for gluing all other parts of crate together.

// -------------------------------------------------------------------------------------------------

use std;
use std::collections::HashMap;
use std::os::unix::io::RawFd;

use dharma;
use skylane::server as wl;

use qualia::{Axis, Button, DrmBundle, Milliseconds, OutputInfo, Position, Size};
use qualia::{Key, KeyboardConfig, Perceptron, Settings};
use qualia::{surface_state, SurfaceId};
use qualia::FrontendsCoordinationTrait;
use inputs::{KeyboardState, KeyMods};

use protocol;
use gateway::Gateway;
use proxy::{Proxy, ProxyRef};
use mediator::{Mediator, MediatorRef};
use event_handlers::{ClientEventHandler, DisplayEventHandler};
use std::path::PathBuf;

// -------------------------------------------------------------------------------------------------

/// Helper structure for aggregating `Connection` with its `Proxy`.
struct Client {
    connection: wl::Connection,
    proxy: ProxyRef,
}

// -------------------------------------------------------------------------------------------------

/// This is main structure of `wayland_frontend` crate.
///
/// For information about its role and place among other structures see crate-level documentation.
pub struct Engine {
    display: wl::DisplaySocket,
    mediator: MediatorRef,
    clients: HashMap<dharma::EventHandlerId, Client>,
    output_infos: Vec<OutputInfo>,
    coordinator: Box<FrontendsCoordinationTrait>,
    settings: Settings,
    dispatcher: dharma::LocalDispatcher,
    keyboard_state: KeyboardState,
}

// -------------------------------------------------------------------------------------------------

impl Engine {
    /// Constructs new `Engine`.
    ///
    /// Sets display socket up. First tries default `skylane` socket, if that fails tries
    /// `$XDG_RUNTIME_DIR/wayland-X` where `X` is number from 0 to 9.
    ///
    /// Panics if failed to set display socket up or to initialize keyboard state.
    pub fn new(coordinator: Box<FrontendsCoordinationTrait>,
               settings: Settings,
               keyboard_config: KeyboardConfig)
               -> Self {
        Engine {
            display: Self::create_display_socket().expect("creating display socket"),
            mediator: MediatorRef::new(Mediator::new()),
            clients: HashMap::new(),
            output_infos: Vec::new(),
            coordinator: coordinator,
            settings: settings,
            dispatcher: dharma::LocalDispatcher::new(),
            keyboard_state: KeyboardState::new(&keyboard_config).expect("creating keyboard state"),
        }
    }

    /// Starts `Engine`: adds display socket to `LocalDispatcher`.
    pub fn start(&mut self, sender: dharma::DirectSender<Perceptron>) {
        let handler = Box::new(DisplayEventHandler::new(self.display.clone(), sender));
        self.dispatcher.add_source(handler, dharma::event_kind::READ);
    }

    /// Reads client requests without blocking.
    pub fn receive(&mut self) {
        self.dispatcher.wait_and_process(Some(0));
    }
}

// -------------------------------------------------------------------------------------------------

/// Public handlers for client related events.
impl Engine {
    /// Handles new client:
    /// - accepts socket and adds it to `Dispatcher`
    /// - creates proxy for new client and registers global Wayland objects.
    /// - creates global display Wayland objects and bind it to client
    pub fn handle_new_client(&mut self,
                             sender: dharma::DirectSender<Perceptron>,
                             coordinator: Box<FrontendsCoordinationTrait>) {
        // Accept the client.
        let mut client_socket = self.display.accept().expect("Accepting client");
        client_socket.set_logger(Some(Self::logger));

        // Prepare event handler.
        let id = self.dispatcher
            .add_source(Box::new(ClientEventHandler::new(client_socket.clone(), sender)),
                        dharma::event_kind::READ);

        // Prepare proxy.
        let mut proxy = Proxy::new(id,
                                   coordinator,
                                   self.settings.clone(),
                                   self.mediator.clone(),
                                   client_socket.clone());
        proxy.register_global(protocol::shm::get_global());
        proxy.register_global(protocol::compositor::get_global());
        proxy.register_global(protocol::shell::get_global());
        proxy.register_global(protocol::xdg_shell_v6::get_global());
        proxy.register_global(protocol::data_device_manager::get_global());
        proxy.register_global(protocol::seat::get_global());
        proxy.register_global(protocol::subcompositor::get_global());
        proxy.register_global(protocol::weston_screenshooter::get_global());
        proxy.register_global(protocol::linux_dmabuf_v1::get_global());
        proxy.register_global(protocol::mesa_drm::get_global());
        for info in self.output_infos.iter() {
            proxy.register_global(protocol::output::get_global(info.clone()));
        }
        let proxy_ref = ProxyRef::new(proxy);

        // Prepare client.
        let display = protocol::display::Display::new_object(proxy_ref.clone());
        let mut connection = wl::Connection::new(client_socket);
        connection.add_object(wl::DISPLAY_ID, display);
        let client = Client {
            connection: connection,
            proxy: proxy_ref,
        };
        self.clients.insert(id, client);
    }

    /// Handles termination (socket hung up) of client.
    pub fn terminate_client(&mut self, id: dharma::EventHandlerId) {
        let result1 = if let Some(_handler) = self.dispatcher.delete_source(id) {
            true
        } else {
            log_warn2!("Dispatching handler not found for client {} on termination", id);
            false
        };

        let result2 = if let Some(_client) = self.clients.remove(&id) {
            true
        } else {
            log_warn2!("Proxy not found for client {} on termination", id);
            false
        };

        if result1 && result2 {
            log_wayl3!("Client {} terminated successfully", id);
        }
    }

    /// Handles request from client associated with given `id`.
    pub fn process_events(&mut self, id: dharma::EventHandlerId) {
        if let Some(ref mut client) = self.clients.get_mut(&id) {
            if let Err(err) = client.connection.process_events() {
                log_warn3!("Wayland Engine: ERROR: {:?}", err);
            }
        } else {
            log_warn1!("Wayland Engine: No client: {}", id);
        }
    }
}

// -------------------------------------------------------------------------------------------------

/// Private helper methods.
impl Engine {
    /// Logs `skylane` debugs.
    fn logger(s: String) {
        log_wayl4!("Skylane: {}", s);
    }

    /// Creates new display socket.
    fn create_display_socket() -> Option<wl::DisplaySocket> {
        match wl::DisplaySocket::new_default() {
            Ok(socket) => Some(socket),
            Err(err) => {
                log_warn1!("Failed to create default display socket: {:?}", err);
                let runtime_dir = std::env::var("XDG_RUNTIME_DIR").expect("reading runtime dir");
                let mut socket_path = PathBuf::from(runtime_dir);
                let mut socket = None;
                for i in 0..10 {
                    socket_path.push(format!("wayland-{}", i));
                    socket = wl::DisplaySocket::new(&socket_path).ok();
                    if socket.is_some() {
                        log_info1!("Using {:?} as display socket", socket_path);
                        break;
                    }
                    socket_path.pop();
                }
                socket
            }
        }
    }
}

// -------------------------------------------------------------------------------------------------

impl Gateway for Engine {
    fn on_output_found(&mut self, bundle: DrmBundle) {
        self.mediator.borrow_mut().set_drm_device(bundle.fd, bundle.path);
    }

    fn on_display_created(&mut self, output_info: OutputInfo) {
        self.output_infos.push(output_info.clone());
        for (_, client) in self.clients.iter() {
            client.proxy.borrow_mut().on_display_created(output_info.clone());
        }
    }

    fn on_keyboard_input(&mut self, key: Key, _mods: Option<KeyMods>) {
        let mods = if self.keyboard_state.update(key.code, key.value) {
            Some(self.keyboard_state.get_mods())
        } else {
            None
        };

        let sid = self.coordinator.get_keyboard_focused_sid();
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow_mut().on_keyboard_input(key, mods);
            }
        }
    }

    fn on_surface_frame(&mut self, sid: SurfaceId, milliseconds: Milliseconds) {
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow_mut().on_surface_frame(sid, milliseconds);
            }
        }
    }

    fn on_pointer_focus_changed(&self,
                                old_sid: SurfaceId,
                                new_sid: SurfaceId,
                                position: Position) {
        let mediator = self.mediator.borrow();
        let old_client_id = mediator.get_client_for_sid(old_sid);
        let new_client_id = mediator.get_client_for_sid(new_sid);

        if new_client_id != old_client_id {
            if let Some(client_id) = old_client_id {
                if let Some(client) = self.clients.get(&client_id) {
                    client.proxy.borrow_mut().on_pointer_focus_changed(old_sid,
                                                                       SurfaceId::invalid(),
                                                                       Position::default());
                }
            }
            if let Some(client_id) = new_client_id {
                if let Some(client) = self.clients.get(&client_id) {
                    client.proxy.borrow_mut().on_pointer_focus_changed(SurfaceId::invalid(),
                                                                       new_sid,
                                                                       position);
                }
            }
        } else {
            if let Some(client_id) = old_client_id {
                if let Some(client) = self.clients.get(&client_id) {
                    client.proxy.borrow_mut().on_pointer_focus_changed(old_sid, new_sid, position);
                }
            }
        }
    }

    fn on_pointer_relative_motion(&self,
                                  sid: SurfaceId,
                                  position: Position,
                                  milliseconds: Milliseconds) {
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow_mut().on_pointer_relative_motion(sid, position, milliseconds);
            }
        }
    }

    fn on_pointer_button(&self, btn: Button) {
        let sid = self.coordinator.get_pointer_focused_sid();
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow_mut().on_pointer_button(btn);
            }
        }
    }

    fn on_pointer_axis(&self, axis: Axis) {
        let sid = self.coordinator.get_pointer_focused_sid();
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow_mut().on_pointer_axis(axis);
            }
        }
    }

    fn on_keyboard_focus_changed(&mut self, old_sid: SurfaceId, new_sid: SurfaceId) {
        let mediator = self.mediator.borrow();
        let old_client_id = mediator.get_client_for_sid(old_sid);
        let new_client_id = mediator.get_client_for_sid(new_sid);


        if new_client_id != old_client_id {
            if let Some(client_id) = old_client_id {
                if let Some(client) = self.clients.get(&client_id) {
                    let mut proxy = client.proxy.borrow_mut();
                    proxy.on_keyboard_focus_changed(old_sid, SurfaceId::invalid());
                }
            }
            if let Some(client_id) = new_client_id {
                if let Some(client) = self.clients.get_mut(&client_id) {
                    let mut proxy = client.proxy.borrow_mut();
                    proxy.make_data_offer(&mut client.connection, client.proxy.clone());
                    proxy.on_keyboard_focus_changed(SurfaceId::invalid(), new_sid);
                }
            }
        } else {
            if let Some(client_id) = old_client_id {
                if let Some(client) = self.clients.get(&client_id) {
                    let mut proxy = client.proxy.borrow_mut();
                    proxy.on_keyboard_focus_changed(old_sid, new_sid);
                }
            }
        }
    }

    fn on_transfer_offered(&mut self) {
        let sid = self.coordinator.get_keyboard_focused_sid();
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get_mut(&id) {
                client.proxy.borrow_mut().make_data_offer(&mut client.connection,
                                                          client.proxy.clone());
            }
        }
    }

    fn on_transfer_requested(&mut self, mime_type: String, fd: RawFd) {
        if let Some(id) = self.mediator.borrow().get_transfer_offerer() {
            if let Some(client) = self.clients.get_mut(&id) {
                client.proxy.borrow_mut().on_transfer_requested(mime_type, fd);
            }
        }
    }

    fn on_surface_reconfigured(&self,
                               sid: SurfaceId,
                               size: Size,
                               state_flags: surface_state::SurfaceState) {
        if let Some(id) = self.mediator.borrow().get_client_for_sid(sid) {
            if let Some(client) = self.clients.get(&id) {
                client.proxy.borrow().on_surface_reconfigured(sid, size, state_flags);
            }
        }
    }

    fn on_screenshot_done(&mut self) {
        let id = {
            let mediator = self.mediator.borrow();
            mediator.get_screenshooter()
        };

        if let Some(id) = id {
            if let Some(client) = self.clients.get_mut(&id) {
                client.proxy.borrow_mut().on_screenshot_done();
            }
        }
    }
}

// -------------------------------------------------------------------------------------------------