Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,3 @@ jobs:
- name: Install Rust
run: rustup update stable
- run: cargo fmt --all --check

security_audit:
permissions:
checks: write
contents: read
issues: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# https://github.com/rustsec/audit-check/issues/2
- uses: rustsec/audit-check@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
14 changes: 14 additions & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) struct XErrorEvent {
type XOpenDisplay = unsafe extern "C" fn(display_name: *const c_char) -> *mut Display;
type XCloseDisplay = unsafe extern "C" fn(display: *mut Display) -> c_int;
type XGetXCBConnection = unsafe extern "C" fn(display: *mut Display) -> *mut xcb_connection_t;
type XDefaultScreen = unsafe extern "C" fn(display: *mut Display) -> c_int;
pub(crate) type XErrorHook =
Option<unsafe extern "C" fn(display: *mut Display, error_event: *mut XErrorEvent) -> c_int>;
type XSetErrorHandler = unsafe extern "C" fn(handler: XErrorHook) -> XErrorHook;
Expand All @@ -72,6 +73,9 @@ pub(crate) struct Xlib {
/// The XGetXCBConnection function.
x_get_xcb_connection: XGetXCBConnection,

/// The XDefaultScreen function.
x_default_screen: XDefaultScreen,

/// The XSetErrorHandler function.
x_set_error_handler: XSetErrorHandler,

Expand All @@ -95,6 +99,11 @@ impl Xlib {
(self.x_get_xcb_connection)(display)
}

/// Get the default screen index.
pub(crate) unsafe fn default_screen(&self, display: *mut Display) -> c_int {
(self.x_default_screen)(display)
}

/// Set the error handler.
pub(crate) unsafe fn set_error_handler(&self, handler: XErrorHook) -> XErrorHook {
(self.x_set_error_handler)(handler)
Expand All @@ -113,6 +122,7 @@ impl Xlib {
extern "C" {
fn XOpenDisplay(display_name: *const c_char) -> *mut Display;
fn XCloseDisplay(display: *mut Display) -> c_int;
fn XDefaultScreen(display: *mut Display) -> c_int;
fn XSetErrorHandler(handler: XErrorHook) -> XErrorHook;
fn XInitThreads() -> c_int;
}
Expand All @@ -126,6 +136,7 @@ impl Xlib {
x_open_display: XOpenDisplay,
x_close_display: XCloseDisplay,
x_get_xcb_connection: XGetXCBConnection,
x_default_screen: XDefaultScreen,
x_set_error_handler: XSetErrorHandler,
x_init_threads: XInitThreads,
})
Expand All @@ -146,6 +157,8 @@ impl Xlib {
let x_set_error_handler =
unsafe { xlib_library.get::<XSetErrorHandler>(b"XSetErrorHandler\0")? };

let x_default_screen = unsafe { xlib_library.get::<XDefaultScreen>(b"XDefaultScreen\0")? };

let x_get_xcb_connection =
unsafe { xlib_xcb_library.get::<XGetXCBConnection>(b"XGetXCBConnection\0")? };

Expand All @@ -155,6 +168,7 @@ impl Xlib {
x_open_display: *x_open_display,
x_close_display: *x_close_display,
x_get_xcb_connection: *x_get_xcb_connection,
x_default_screen: *x_default_screen,
x_set_error_handler: *x_set_error_handler,
x_init_threads: *x_init_threads,
_xlib_library: xlib_library,
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,23 @@ impl Display {
pub fn as_ptr(&self) -> *mut c_void {
self.ptr.as_ptr().cast()
}

/// Get the default screen index for this display.
pub fn screen_index(&self) -> usize {
let xlib = get_xlib(&XLIB).expect("failed to load Xlib");

// SAFETY: Valid display pointer.
let index = unsafe { xlib.default_screen(self.ptr.as_ptr()) };

// Cast down to usize.
index.try_into().unwrap_or_else(|_| {
#[cfg(feature = "tracing")]
tracing::error!(
"XDefaultScreen returned a value out of usize range (how?!), returning zero"
);
0
})
}
}

unsafe impl as_raw_xcb_connection::AsRawXcbConnection for Display {
Expand Down