diff --git a/Cargo.toml b/Cargo.toml index cfaf593b..264eec8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] -x11 = ["x11-dl", "bytemuck", "rustix", "percent-encoding", "xkbcommon-dl/x11", "x11rb"] +x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"] wayland = ["wayland-client", "wayland-backend", "wayland-protocols", "sctk", "fnv", "memmap2"] wayland-dlopen = ["wayland-backend/dlopen"] wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"] @@ -123,7 +123,7 @@ wayland-client = { version = "0.30.0", optional = true } wayland-backend = { version = "0.1.0", default_features = false, features = ["client_system"], optional = true } wayland-protocols = { version = "0.30.0", features = [ "staging"], optional = true } calloop = "0.10.5" -rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "process"], optional = true } +rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "thread", "process"] } x11-dl = { version = "2.18.5", optional = true } x11rb = { version = "0.12.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "xinput", "xkb"], optional = true } xkbcommon-dl = "0.4.0" diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index fa3ef928..47c57700 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -935,9 +935,7 @@ fn sticky_exit_callback( #[cfg(target_os = "linux")] fn is_main_thread() -> bool { - use libc::{c_long, getpid, syscall, SYS_gettid}; - - unsafe { syscall(SYS_gettid) == getpid() as c_long } + rustix::thread::gettid() == rustix::process::getpid() } #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index b049dd37..e2cee68f 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -1,6 +1,11 @@ -use std::{cell::RefCell, collections::HashMap, rc::Rc, slice, sync::Arc}; - -use libc::{c_char, c_int, c_long, c_ulong}; +use std::{ + cell::RefCell, + collections::HashMap, + os::raw::{c_char, c_int, c_long, c_ulong}, + rc::Rc, + slice, + sync::Arc, +}; use x11rb::protocol::xproto::{self, ConnectionExt as _}; use x11rb::x11_utils::Serialize; diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index fd7df53a..14e79056 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -230,7 +230,7 @@ impl EventLoop { xconn.display, &mut xinput_major_ver, &mut xinput_minor_ver, - ) != ffi::Success as libc::c_int + ) != ffi::Success as std::os::raw::c_int { panic!( "X server has XInput extension {xinput_major_ver}.{xinput_minor_ver} but does not support XInput2", diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 75774f8d..7d58fc75 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1,14 +1,12 @@ use std::{ cmp, env, ffi::CString, - mem::{replace, MaybeUninit}, + mem::replace, os::raw::*, path::Path, - ptr, slice, sync::{Arc, Mutex, MutexGuard}, }; -use libc; use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XlibDisplayHandle, XlibWindowHandle}; use x11rb::{ connection::Connection, @@ -542,41 +540,28 @@ impl UnownedWindow { let atoms = self.xconn.atoms(); let pid_atom = atoms[_NET_WM_PID]; let client_machine_atom = atoms[WM_CLIENT_MACHINE]; - unsafe { - // 64 would suffice for Linux, but 256 will be enough everywhere (as per SUSv2). For instance, this is - // the limit defined by OpenBSD. - const MAXHOSTNAMELEN: usize = 256; - // `assume_init` is safe here because the array consists of `MaybeUninit` values, - // which do not require initialization. - let mut buffer: [MaybeUninit; MAXHOSTNAMELEN] = - MaybeUninit::uninit().assume_init(); - let status = libc::gethostname(buffer.as_mut_ptr() as *mut c_char, buffer.len()); - if status != 0 { - return Ok(None); - } - ptr::write(buffer[MAXHOSTNAMELEN - 1].as_mut_ptr() as *mut u8, b'\0'); // a little extra safety - let hostname_length = libc::strlen(buffer.as_ptr() as *const c_char); - let hostname = slice::from_raw_parts(buffer.as_ptr() as *const c_char, hostname_length); + // Get the hostname and the PID. + let uname = rustix::system::uname(); + let pid = rustix::process::getpid(); - self.xconn - .change_property( - self.xwindow, - pid_atom, - xproto::Atom::from(xproto::AtomEnum::CARDINAL), - xproto::PropMode::REPLACE, - &[libc::getpid() as util::Cardinal], - )? - .ignore_error(); - let flusher = self.xconn.change_property( + self.xconn + .change_property( self.xwindow, - client_machine_atom, - xproto::Atom::from(xproto::AtomEnum::STRING), + pid_atom, + xproto::Atom::from(xproto::AtomEnum::CARDINAL), xproto::PropMode::REPLACE, - &hostname[0..hostname_length], - ); - flusher.map(Some) - } + &[pid.as_raw_nonzero().get() as util::Cardinal], + )? + .ignore_error(); + let flusher = self.xconn.change_property( + self.xwindow, + client_machine_atom, + xproto::Atom::from(xproto::AtomEnum::STRING), + xproto::PropMode::REPLACE, + uname.nodename().to_bytes(), + ); + flusher.map(Some) } fn set_window_types( diff --git a/src/platform_impl/linux/x11/xdisplay.rs b/src/platform_impl/linux/x11/xdisplay.rs index 016f0586..7535daba 100644 --- a/src/platform_impl/linux/x11/xdisplay.rs +++ b/src/platform_impl/linux/x11/xdisplay.rs @@ -46,7 +46,7 @@ unsafe impl Send for XConnection {} unsafe impl Sync for XConnection {} pub type XErrorHandler = - Option libc::c_int>; + Option std::os::raw::c_int>; impl XConnection { pub fn new(error_handler: XErrorHandler) -> Result {