Visual Studio Code, rebuilt in Rust
Rust
//! The X server calls the Linux build makes beside GPUI's own connection, as Chromium's X11 code does them: window
//! manager states, key grabs and the keyboard layout. On Wayland they reach XWayland, or nothing without `DISPLAY`.
use anyhow::Context as _;
use gpui::Window;
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use x11rb::connection::Connection;
use x11rb::protocol::xproto::{ClientMessageEvent, ConnectionExt, EventMask};
/// The window's X11 id; none for a Wayland window.
pub fn window_xid(window: &Window) -> Option<u32> {
match HasWindowHandle::window_handle(window).ok()?.as_raw() {
RawWindowHandle::Xcb(handle) => Some(handle.window.get()),
RawWindowHandle::Xlib(handle) => u32::try_from(handle.window).ok(),
_ => None,
}
}
/// ui/base/x/x11_util.cc `SetWMSpecState`: a `_NET_WM_STATE` add or remove message to the root window.
pub fn set_wm_state(xid: u32, state: &str, enabled: bool) -> anyhow::Result<()> {
let (connection, screen) = x11rb::connect(None).context("connecting to the X server")?;
let root = connection.setup().roots[screen].root;
let wm_state = connection.intern_atom(false, b"_NET_WM_STATE")?.reply()?.atom;
let state = connection.intern_atom(false, state.as_bytes())?.reply()?.atom;
// _NET_WM_STATE_ADD and _NET_WM_STATE_REMOVE; source 1 is a normal application.
let event = ClientMessageEvent::new(32, xid, wm_state, [u32::from(enabled), state, 0, 1, 0]);
connection.send_event(false, root, EventMask::SUBSTRUCTURE_NOTIFY | EventMask::SUBSTRUCTURE_REDIRECT, event)?;
connection.flush()?;
Ok(())
}