Skip to content

Commit

Permalink
Replace libc with rustix in some modules
Browse files Browse the repository at this point in the history
Unfortunately this isn't a total removal, for two reasons:

- We still need "libc" for the Xlib XIM implementation, for locales.
- BSD requires libc to check for main-threadedness.

First one we can likely resolve in the near future, not so sure about
the second one without using some weird pthreads trick.
  • Loading branch information
notgull authored Jul 22, 2023
1 parent c62e640 commit 43acf7f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,7 @@ fn sticky_exit_callback<T, F>(

#[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"))]
Expand Down
11 changes: 8 additions & 3 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<T: 'static> EventLoop<T> {
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",
Expand Down
53 changes: 19 additions & 34 deletions src/platform_impl/linux/x11/window.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<c_char>; 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(
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/linux/x11/xdisplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ unsafe impl Send for XConnection {}
unsafe impl Sync for XConnection {}

pub type XErrorHandler =
Option<unsafe extern "C" fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> libc::c_int>;
Option<unsafe extern "C" fn(*mut ffi::Display, *mut ffi::XErrorEvent) -> std::os::raw::c_int>;

impl XConnection {
pub fn new(error_handler: XErrorHandler) -> Result<XConnection, XNotSupported> {
Expand Down

0 comments on commit 43acf7f

Please sign in to comment.