Brian J. Tarricone
a516a25a9f
The semantics of the XFIXES cursor hide deal are annoying and make things harder.
79 lines
2.2 KiB
Rust
79 lines
2.2 KiB
Rust
use std::{ffi::CStr, io};
|
|
use xcb::x;
|
|
|
|
pub mod desktop;
|
|
pub mod settings;
|
|
|
|
pub const BSCREENSAVER_WM_CLASS: &[u8] = b"bscreensaver\0Bscreensaver\0";
|
|
|
|
pub fn init_logging(env_name: &str) {
|
|
env_logger::builder()
|
|
.format_timestamp_millis()
|
|
.parse_env(env_name)
|
|
.init();
|
|
}
|
|
|
|
pub fn opt_contains<T: PartialEq>(o: &Option<T>, v: &T) -> bool {
|
|
o.as_ref().filter(|ov| ov == &v).is_some()
|
|
}
|
|
|
|
pub fn result_contains<T: PartialEq, E>(res: &Result<T, E>, v: &T) -> bool {
|
|
match res {
|
|
Ok(ok) if ok == v => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn create_atom(conn: &xcb::Connection, name: &[u8]) -> xcb::Result<x::Atom> {
|
|
let cookie = conn.send_request(&x::InternAtom {
|
|
only_if_exists: false,
|
|
name,
|
|
});
|
|
Ok(conn.wait_for_reply(cookie)?.atom())
|
|
}
|
|
|
|
pub fn destroy_pixmap(conn: &xcb::Connection, pixmap: x::Pixmap) -> xcb::Result<()> {
|
|
conn.send_and_check_request(&x::FreePixmap {
|
|
pixmap,
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn destroy_cursor(conn: &xcb::Connection, cursor: x::Cursor) -> xcb::Result<()> {
|
|
conn.send_and_check_request(&x::FreeCursor {
|
|
cursor,
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn destroy_gc(conn: &xcb::Connection, gc: x::Gcontext) -> xcb::Result<()> {
|
|
conn.send_and_check_request(&x::FreeGc {
|
|
gc,
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn destroy_window(conn: &xcb::Connection, window: x::Window) -> xcb::Result<()> {
|
|
conn.send_and_check_request(&x::DestroyWindow {
|
|
window,
|
|
})?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn get_username() -> io::Result<String> {
|
|
// SAFETY: libc must be sane
|
|
let uid = unsafe { libc::getuid() };
|
|
// SAFETY: libc must be sane
|
|
let pwd = unsafe { libc::getpwuid(uid) };
|
|
// SAFETY: null-check occurs on same line
|
|
if pwd.is_null() || unsafe { *pwd }.pw_name.is_null() {
|
|
Err(io::Error::new(io::ErrorKind::NotFound, "Username not found".to_string()))
|
|
} else {
|
|
// SAFETY: libc must be sane; null checks performed above
|
|
let cstr = unsafe { CStr::from_ptr((*pwd).pw_name) };
|
|
cstr.to_str()
|
|
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "Invalid UTF-8 data in username".to_string()))
|
|
.map(|s| s.to_string())
|
|
}
|
|
}
|