-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start working on a safe X11 module (#55)
I had this idea when trying `slice::from_raw_parts` for debugging this part of the code the other day. If I could encapsulate all of the unsafe code in this `x` module, then rwm proper could use only safe code. One day this could even be a separate crate for other window managers to use.
- Loading branch information
Showing
3 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! Safe bindings to X11 functions | ||
use std::os::raw::c_int; | ||
|
||
use x11::xlib::{self, Atom, Display, XFree}; | ||
|
||
use crate::Window; | ||
|
||
pub struct WmProtocols<'a> { | ||
atoms: *mut Atom, | ||
slice: &'a [Atom], | ||
} | ||
|
||
impl WmProtocols<'_> { | ||
pub fn iter(&self) -> impl DoubleEndedIterator<Item = &Atom> { | ||
self.slice.iter() | ||
} | ||
} | ||
|
||
impl Drop for WmProtocols<'_> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
XFree(self.atoms.cast()); | ||
} | ||
} | ||
} | ||
|
||
/// Return the list of atoms stored in the `WM_PROTOCOLS` property on `w`. | ||
/// | ||
/// See `XGetWMProtocols(3)` for more details. | ||
pub fn get_wm_protocols<'a>( | ||
display: *mut Display, | ||
w: Window, | ||
) -> Result<WmProtocols<'a>, c_int> { | ||
let mut protocols = std::ptr::null_mut(); | ||
let mut n = 0; | ||
unsafe { | ||
let status = xlib::XGetWMProtocols(display, w, &mut protocols, &mut n); | ||
|
||
if status == 0 { | ||
return Err(status); | ||
} | ||
|
||
Ok(WmProtocols { | ||
atoms: protocols, | ||
slice: std::slice::from_raw_parts( | ||
protocols, | ||
usize::try_from(n).unwrap(), | ||
), | ||
}) | ||
} | ||
} |