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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]

use std::collections::VecDeque;
use std::{env, mem};
use std::ffi::CStr;
use std::os::raw::*;
use std::sync::Arc;

use parking_lot::Mutex;
use sctk::reexports::client::ConnectError;

use {
    CreationError,
    EventsLoopClosed,
    Icon,
    MouseCursor,
    ControlFlow,
    WindowAttributes,
};
use dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize};
use window::MonitorId as RootMonitorId;
use self::x11::{XConnection, XError};
use self::x11::ffi::XVisualInfo;
pub use self::x11::XNotSupported;

mod dlopen;
pub mod wayland;
pub mod x11;

/// Environment variable specifying which backend should be used on unix platform.
///
/// Legal values are x11 and wayland. If this variable is set only the named backend
/// will be tried by winit. If it is not set, winit will try to connect to a wayland connection,
/// and if it fails will fallback on x11.
///
/// If this variable is set with any other value, winit will panic.
const BACKEND_PREFERENCE_ENV_VAR: &str = "WINIT_UNIX_BACKEND";

#[derive(Clone, Default)]
pub struct PlatformSpecificWindowBuilderAttributes {
    pub visual_infos: Option<XVisualInfo>,
    pub screen_id: Option<i32>,
    pub resize_increments: Option<(u32, u32)>,
    pub base_size: Option<(u32, u32)>,
    pub class: Option<(String, String)>,
    pub override_redirect: bool,
    pub x11_window_type: x11::util::WindowType,
    pub gtk_theme_variant: Option<String>,
    pub app_id: Option<String>
}

lazy_static!(
    pub static ref X11_BACKEND: Mutex<Result<Arc<XConnection>, XNotSupported>> = {
        Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new))
    };
);

pub enum Window {
    X(x11::Window),
    Wayland(wayland::Window),
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WindowId {
    X(x11::WindowId),
    Wayland(wayland::WindowId),
}

impl WindowId {
    pub unsafe fn dummy() -> Self {
        WindowId::X(x11::WindowId::dummy())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DeviceId {
    X(x11::DeviceId),
    Wayland(wayland::DeviceId),
}

impl DeviceId {
    pub unsafe fn dummy() -> Self {
        DeviceId::X(x11::DeviceId::dummy())
    }
}

#[derive(Debug, Clone)]
pub enum MonitorId {
    X(x11::MonitorId),
    Wayland(wayland::MonitorId),
}

impl MonitorId {
    #[inline]
    pub fn get_name(&self) -> Option<String> {
        match self {
            &MonitorId::X(ref m) => m.get_name(),
            &MonitorId::Wayland(ref m) => m.get_name(),
        }
    }

    #[inline]
    pub fn get_native_identifier(&self) -> u32 {
        match self {
            &MonitorId::X(ref m) => m.get_native_identifier(),
            &MonitorId::Wayland(ref m) => m.get_native_identifier(),
        }
    }

    #[inline]
    pub fn get_dimensions(&self) -> PhysicalSize {
        match self {
            &MonitorId::X(ref m) => m.get_dimensions(),
            &MonitorId::Wayland(ref m) => m.get_dimensions(),
        }
    }

    #[inline]
    pub fn get_position(&self) -> PhysicalPosition {
        match self {
            &MonitorId::X(ref m) => m.get_position(),
            &MonitorId::Wayland(ref m) => m.get_position(),
        }
    }

    #[inline]
    pub fn get_hidpi_factor(&self) -> f64 {
        match self {
            &MonitorId::X(ref m) => m.get_hidpi_factor(),
            &MonitorId::Wayland(ref m) => m.get_hidpi_factor() as f64,
        }
    }
}

impl Window {
    #[inline]
    pub fn new(
        events_loop: &EventsLoop,
        attribs: WindowAttributes,
        pl_attribs: PlatformSpecificWindowBuilderAttributes,
    ) -> Result<Self, CreationError> {
        match *events_loop {
            EventsLoop::Wayland(ref events_loop) => {
                wayland::Window::new(events_loop, attribs, pl_attribs).map(Window::Wayland)
            },
            EventsLoop::X(ref events_loop) => {
                x11::Window::new(events_loop, attribs, pl_attribs).map(Window::X)
            },
        }
    }

    #[inline]
    pub fn id(&self) -> WindowId {
        match self {
            &Window::X(ref w) => WindowId::X(w.id()),
            &Window::Wayland(ref w) => WindowId::Wayland(w.id()),
        }
    }

    #[inline]
    pub fn set_title(&self, title: &str) {
        match self {
            &Window::X(ref w) => w.set_title(title),
            &Window::Wayland(ref w) => w.set_title(title),
        }
    }

    #[inline]
    pub fn show(&self) {
        match self {
            &Window::X(ref w) => w.show(),
            &Window::Wayland(ref w) => w.show(),
        }
    }

    #[inline]
    pub fn hide(&self) {
        match self {
            &Window::X(ref w) => w.hide(),
            &Window::Wayland(ref w) => w.hide(),
        }
    }

    #[inline]
    pub fn get_position(&self) -> Option<LogicalPosition> {
        match self {
            &Window::X(ref w) => w.get_position(),
            &Window::Wayland(ref w) => w.get_position(),
        }
    }

    #[inline]
    pub fn get_inner_position(&self) -> Option<LogicalPosition> {
        match self {
            &Window::X(ref m) => m.get_inner_position(),
            &Window::Wayland(ref m) => m.get_inner_position(),
        }
    }

    #[inline]
    pub fn set_position(&self, position: LogicalPosition) {
        match self {
            &Window::X(ref w) => w.set_position(position),
            &Window::Wayland(ref w) => w.set_position(position),
        }
    }

    #[inline]
    pub fn get_inner_size(&self) -> Option<LogicalSize> {
        match self {
            &Window::X(ref w) => w.get_inner_size(),
            &Window::Wayland(ref w) => w.get_inner_size(),
        }
    }

    #[inline]
    pub fn get_outer_size(&self) -> Option<LogicalSize> {
        match self {
            &Window::X(ref w) => w.get_outer_size(),
            &Window::Wayland(ref w) => w.get_outer_size(),
        }
    }

    #[inline]
    pub fn set_inner_size(&self, size: LogicalSize) {
        match self {
            &Window::X(ref w) => w.set_inner_size(size),
            &Window::Wayland(ref w) => w.set_inner_size(size),
        }
    }

    #[inline]
    pub fn set_min_dimensions(&self, dimensions: Option<LogicalSize>) {
        match self {
            &Window::X(ref w) => w.set_min_dimensions(dimensions),
            &Window::Wayland(ref w) => w.set_min_dimensions(dimensions),
        }
    }

    #[inline]
    pub fn set_max_dimensions(&self, dimensions: Option<LogicalSize>) {
        match self {
            &Window::X(ref w) => w.set_max_dimensions(dimensions),
            &Window::Wayland(ref w) => w.set_max_dimensions(dimensions),
        }
    }

    #[inline]
    pub fn set_resizable(&self, resizable: bool) {
        match self {
            &Window::X(ref w) => w.set_resizable(resizable),
            &Window::Wayland(ref w) => w.set_resizable(resizable),
        }
    }

    #[inline]
    pub fn set_cursor(&self, cursor: MouseCursor) {
        match self {
            &Window::X(ref w) => w.set_cursor(cursor),
            &Window::Wayland(ref w) => w.set_cursor(cursor)
        }
    }

    #[inline]
    pub fn grab_cursor(&self, grab: bool) -> Result<(), String> {
        match self {
            &Window::X(ref window) => window.grab_cursor(grab),
            &Window::Wayland(ref window) => window.grab_cursor(grab),
        }
    }

    #[inline]
    pub fn hide_cursor(&self, hide: bool) {
        match self {
            &Window::X(ref window) => window.hide_cursor(hide),
            &Window::Wayland(ref window) => window.hide_cursor(hide),
        }
    }

    #[inline]
    pub fn get_hidpi_factor(&self) -> f64 {
       match self {
            &Window::X(ref w) => w.get_hidpi_factor(),
            &Window::Wayland(ref w) => w.hidpi_factor() as f64,
        }
    }

    #[inline]
    pub fn set_cursor_position(&self, position: LogicalPosition) -> Result<(), String> {
        match self {
            &Window::X(ref w) => w.set_cursor_position(position),
            &Window::Wayland(ref w) => w.set_cursor_position(position),
        }
    }

    #[inline]
    pub fn set_maximized(&self, maximized: bool) {
        match self {
            &Window::X(ref w) => w.set_maximized(maximized),
            &Window::Wayland(ref w) => w.set_maximized(maximized),
        }
    }

    #[inline]
    pub fn get_fullscreen(&self) -> Option<RootMonitorId> {
        match self {
            &Window::X(ref w) => w.get_fullscreen(),
            &Window::Wayland(ref w) => w.get_fullscreen()
                .map(|monitor_id| RootMonitorId { inner: MonitorId::Wayland(monitor_id) })
        }
    }

    #[inline]
    pub fn set_fullscreen(&self, monitor: Option<RootMonitorId>) {
        match self {
            &Window::X(ref w) => w.set_fullscreen(monitor),
            &Window::Wayland(ref w) => w.set_fullscreen(monitor)
        }
    }

    #[inline]
    pub fn set_decorations(&self, decorations: bool) {
        match self {
            &Window::X(ref w) => w.set_decorations(decorations),
            &Window::Wayland(ref w) => w.set_decorations(decorations)
        }
    }

    #[inline]
    pub fn set_always_on_top(&self, always_on_top: bool) {
        match self {
            &Window::X(ref w) => w.set_always_on_top(always_on_top),
            &Window::Wayland(_) => (),
        }
    }

    #[inline]
    pub fn set_window_icon(&self, window_icon: Option<Icon>) {
        match self {
            &Window::X(ref w) => w.set_window_icon(window_icon),
            &Window::Wayland(_) => (),
        }
    }

    #[inline]
    pub fn set_ime_spot(&self, position: LogicalPosition) {
        match self {
            &Window::X(ref w) => w.set_ime_spot(position),
            &Window::Wayland(_) => (),
        }
    }

    #[inline]
    pub fn get_current_monitor(&self) -> RootMonitorId {
        match self {
            &Window::X(ref window) => RootMonitorId { inner: MonitorId::X(window.get_current_monitor()) },
            &Window::Wayland(ref window) => RootMonitorId { inner: MonitorId::Wayland(window.get_current_monitor()) },
        }
    }

    #[inline]
    pub fn get_available_monitors(&self) -> VecDeque<MonitorId> {
        match self {
            &Window::X(ref window) => window.get_available_monitors()
                .into_iter()
                .map(MonitorId::X)
                .collect(),
            &Window::Wayland(ref window) => window.get_available_monitors()
                .into_iter()
                .map(MonitorId::Wayland)
                .collect(),
        }
    }

    #[inline]
    pub fn get_primary_monitor(&self) -> MonitorId {
        match self {
            &Window::X(ref window) => MonitorId::X(window.get_primary_monitor()),
            &Window::Wayland(ref window) => MonitorId::Wayland(window.get_primary_monitor()),
        }
    }
}

unsafe extern "C" fn x_error_callback(
    display: *mut x11::ffi::Display,
    event: *mut x11::ffi::XErrorEvent,
) -> c_int {
    let xconn_lock = X11_BACKEND.lock();
    if let Ok(ref xconn) = *xconn_lock {
        let mut buf: [c_char; 1024] = mem::uninitialized();
        (xconn.xlib.XGetErrorText)(
            display,
            (*event).error_code as c_int,
            buf.as_mut_ptr(),
            buf.len() as c_int,
        );
        let description = CStr::from_ptr(buf.as_ptr()).to_string_lossy();

        let error = XError {
            description: description.into_owned(),
            error_code: (*event).error_code,
            request_code: (*event).request_code,
            minor_code: (*event).minor_code,
        };

        error!("X11 error: {:#?}", error);

        *xconn.latest_error.lock() = Some(error);
    }
    // Fun fact: this return value is completely ignored.
    0
}

pub enum EventsLoop {
    Wayland(wayland::EventsLoop),
    X(x11::EventsLoop)
}

#[derive(Clone)]
pub enum EventsLoopProxy {
    X(x11::EventsLoopProxy),
    Wayland(wayland::EventsLoopProxy),
}

impl EventsLoop {
    pub fn new() -> EventsLoop {
        if let Ok(env_var) = env::var(BACKEND_PREFERENCE_ENV_VAR) {
            match env_var.as_str() {
                "x11" => {
                    // TODO: propagate
                    return EventsLoop::new_x11().expect("Failed to initialize X11 backend");
                },
                "wayland" => {
                    return EventsLoop::new_wayland()
                        .expect("Failed to initialize Wayland backend");
                },
                _ => panic!(
                    "Unknown environment variable value for {}, try one of `x11`,`wayland`",
                    BACKEND_PREFERENCE_ENV_VAR,
                ),
            }
        }

        let wayland_err = match EventsLoop::new_wayland() {
            Ok(event_loop) => return event_loop,
            Err(err) => err,
        };

        let x11_err = match EventsLoop::new_x11() {
            Ok(event_loop) => return event_loop,
            Err(err) => err,
        };

        let err_string = format!(
            "Failed to initialize any backend! Wayland status: {:?} X11 status: {:?}",
            wayland_err,
            x11_err,
        );
        panic!(err_string);
    }

    pub fn new_wayland() -> Result<EventsLoop, ConnectError> {
        wayland::EventsLoop::new()
            .map(EventsLoop::Wayland)
    }

    pub fn new_x11() -> Result<EventsLoop, XNotSupported> {
        X11_BACKEND
            .lock()
            .as_ref()
            .map(Arc::clone)
            .map(x11::EventsLoop::new)
            .map(EventsLoop::X)
            .map_err(|err| err.clone())
    }

    #[inline]
    pub fn get_available_monitors(&self) -> VecDeque<MonitorId> {
        match *self {
            EventsLoop::Wayland(ref evlp) => evlp
                .get_available_monitors()
                .into_iter()
                .map(MonitorId::Wayland)
                .collect(),
            EventsLoop::X(ref evlp) => evlp
                .x_connection()
                .get_available_monitors()
                .into_iter()
                .map(MonitorId::X)
                .collect(),
        }
    }

    #[inline]
    pub fn get_primary_monitor(&self) -> MonitorId {
        match *self {
            EventsLoop::Wayland(ref evlp) => MonitorId::Wayland(evlp.get_primary_monitor()),
            EventsLoop::X(ref evlp) => MonitorId::X(evlp.x_connection().get_primary_monitor()),
        }
    }

    pub fn create_proxy(&self) -> EventsLoopProxy {
        match *self {
            EventsLoop::Wayland(ref evlp) => EventsLoopProxy::Wayland(evlp.create_proxy()),
            EventsLoop::X(ref evlp) => EventsLoopProxy::X(evlp.create_proxy()),
        }
    }

    pub fn poll_events<F>(&mut self, callback: F)
        where F: FnMut(::Event)
    {
        match *self {
            EventsLoop::Wayland(ref mut evlp) => evlp.poll_events(callback),
            EventsLoop::X(ref mut evlp) => evlp.poll_events(callback)
        }
    }

    pub fn run_forever<F>(&mut self, callback: F)
        where F: FnMut(::Event) -> ControlFlow
    {
        match *self {
            EventsLoop::Wayland(ref mut evlp) => evlp.run_forever(callback),
            EventsLoop::X(ref mut evlp) => evlp.run_forever(callback)
        }
    }

    #[inline]
    pub fn is_wayland(&self) -> bool {
        match *self {
            EventsLoop::Wayland(_) => true,
            EventsLoop::X(_) => false,
        }
    }

    #[inline]
    pub fn x_connection(&self) -> Option<&Arc<XConnection>> {
        match *self {
            EventsLoop::Wayland(_) => None,
            EventsLoop::X(ref ev) => Some(ev.x_connection()),
        }
    }
}

impl EventsLoopProxy {
    pub fn wakeup(&self) -> Result<(), EventsLoopClosed> {
        match *self {
            EventsLoopProxy::Wayland(ref proxy) => proxy.wakeup(),
            EventsLoopProxy::X(ref proxy) => proxy.wakeup(),
        }
    }
}