From c3b27011f1cd015e747aefcda842bee9c87dec47 Mon Sep 17 00:00:00 2001 From: PapyElGringo Date: Sun, 9 Mar 2025 11:22:40 +0100 Subject: [PATCH] add get client id to x11 surface --- Cargo.toml | 2 +- src/xwayland/xwm/surface.rs | 43 ++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6909acb26f91..afc700bc8f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ wayland-server = { version = "0.31.7", optional = true } wayland-sys = { version = "0.31.6", optional = true } wayland-backend = { version = "0.3.8", optional = true } winit = { version = "0.30.0", default-features = false, features = ["wayland", "wayland-dlopen", "x11", "rwh_06"], optional = true } -x11rb = { version = "0.13.0", optional = true } +x11rb = { version = "0.13.0", optional = true, features = ["res"]} xkbcommon = { version = "0.8.0", features = ["wayland"]} encoding_rs = { version = "0.8.33", optional = true } profiling = "1.0.13" diff --git a/src/xwayland/xwm/surface.rs b/src/xwayland/xwm/surface.rs index 09748b99fba0..1707f1aa083d 100644 --- a/src/xwayland/xwm/surface.rs +++ b/src/xwayland/xwm/surface.rs @@ -27,9 +27,12 @@ use wayland_server::protocol::wl_surface::WlSurface; use x11rb::{ connection::Connection as _, properties::{WmClass, WmHints, WmSizeHints}, - protocol::xproto::{ - Atom, AtomEnum, ClientMessageEvent, ConfigureWindowAux, ConnectionExt as _, EventMask, InputFocus, - PropMode, Window as X11Window, + protocol::{ + res::{query_client_ids, ClientIdSpec}, + xproto::{ + Atom, AtomEnum, ClientMessageEvent, ConfigureWindowAux, ConnectionExt as _, EventMask, + InputFocus, PropMode, Window as X11Window, + }, }, rust_connection::{ConnectionError, RustConnection}, wrapper::ConnectionExt, @@ -924,6 +927,40 @@ impl X11Surface { } conn.flush() } + + /// Get the client PID associated with the X11 window. + pub fn get_client_pid(&self) -> Result> { + if let Some(connection) = self.conn.upgrade() { + let window = self.window; + + match query_client_ids( + &connection, + &[ClientIdSpec { + client: window, + mask: x11rb::protocol::res::ClientIdMask::LOCAL_CLIENT_PID, + }], + ) { + Ok(cookie) => { + let reply = cookie.reply()?; + + if let Some(id_value) = reply.ids.first() { + if let Some(pid) = id_value.value.first().copied() { + return Ok(pid); + } else { + return Err(Box::new(std::io::Error::new( + std::io::ErrorKind::NotFound, + "No matching client ID found", + ))); + } + } + } + Err(_) => { + return Ok(0); + } + } + } + Ok(0) + } } /// Trait for objects, that represent an x11 window in some shape or form