Skip to content

Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.

4.0 KB
Raw Blame
//! What RustCord needs on Linux that Windows does by itself.
//!
//! The embedded web pages (Discord's login page, captchas, media players) are
//! WebKitGTK views. wry places them inside RustCord's window through X11, so
//! RustCord runs as an X11 window (XWayland on a Wayland desktop), and GTK has
//! to be started on the UI thread and given its events every frame.

use std::sync::atomic::{AtomicBool, Ordering};

static GTK_READY: AtomicBool = AtomicBool::new(false);

static FORCED_X11: AtomicBool = AtomicBool::new(false);

/// First thing in `main`, before any other thread exists (changing the
/// environment isn't safe once one does): winit and GTK pick Wayland when they
/// can, and the child web views need X11.
pub fn prefer_x11() {
    // WebKitGTK's DMA-BUF renderer fails inside an X11 window on some setups
    // (NVIDIA under XWayland: GLXBadWindow as the login page opens, issue
    // #15); its older renderer draws the pages fine. A value the person set
    // themselves is kept.
    if std::env::var_os("WEBKIT_DISABLE_DMABUF_RENDERER").is_none() {
        std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
    }
    if std::env::var_os("WAYLAND_DISPLAY").is_some() && std::env::var_os("DISPLAY").is_some() {
        std::env::remove_var("WAYLAND_DISPLAY");
        std::env::set_var("GDK_BACKEND", "x11");
        FORCED_X11.store(true, Ordering::Relaxed);
    }
}

/// Before the window exists, on the UI thread: start GTK. Without it (no
/// display, libraries missing) everything but the embedded pages still works.
pub fn init() {
    if FORCED_X11.load(Ordering::Relaxed) {
        log::info!("linux: running as an X11 window (XWayland), for the embedded web pages");
    }
    match gtk::init() {
        Ok(()) => GTK_READY.store(true, Ordering::Relaxed),
        Err(e) => log::warn!("linux: GTK didn't start ({e}); Discord's login page and web players are unavailable"),
    }
}

/// As the window opens: X errors on any connection but the window's own are
/// left to GTK (the embedded web pages talk to X through a connection of
/// their own). Xlib has a single error handler for the whole process, winit's,
/// and winit keeps the last error it sees for its own next check; a stray
/// error from WebKitGTK as the login page opened (GLXBadWindow under
/// XWayland) made winit panic with "Failed to focus input context".
pub fn keep_x_errors_apart(cc: &eframe::CreationContext<'_>) {
    use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
    let Ok(handle) = cc.display_handle() else { return };
    let RawDisplayHandle::Xlib(xlib) = handle.as_raw() else { return };
    let Some(display) = xlib.display else { return };
    // (As a number: the hook must be Send, and only compares it.)
    let window_display = display.as_ptr() as usize;
    static LOGGED: AtomicBool = AtomicBool::new(false);
    winit::platform::x11::register_xlib_error_hook(Box::new(move |display, _event| {
        let others = display as usize != window_display;
        if others && !LOGGED.swap(true, Ordering::Relaxed) {
            log::info!("linux: an X error on the web pages' connection was left to GTK");
        }
        others
    }));
}

/// `Err` with a message for the person when web pages can't be shown.
pub fn web_pages_available() -> Result<(), String> {
    if GTK_READY.load(Ordering::Relaxed) {
        Ok(())
    } else {
        Err("Web pages need GTK and WebKitGTK (webkit2gtk 4.1), which couldn't be started.".into())
    }
}

/// Each frame: hand GTK its pending events. While a web page is open the
/// window keeps drawing (about 60 times a second) so the page stays live.
pub fn pump(ctx: &eframe::egui::Context, web_page_open: bool) {
    if !GTK_READY.load(Ordering::Relaxed) {
        return;
    }
    while gtk::events_pending() {
        gtk::main_iteration_do(false);
    }
    if web_page_open {
        ctx.request_repaint_after(std::time::Duration::from_millis(16));
    }
}

/// Open a folder in the file manager.
pub fn open_folder(dir: &std::path::Path) {
    let _ = std::process::Command::new("xdg-open").arg(dir).spawn();
}