Initial import. Most things seem working.
This includes an abortive attempt to do a gtk4 dialog (which I don't think is possible, as gtk4 doesn't allow embedding toplevels anymore), and an iced dialog, which I just never started writing.
This commit is contained in:
50
util/src/lib.rs
Normal file
50
util/src/lib.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use std::{ffi::CStr, io};
|
||||
use xcb::x;
|
||||
|
||||
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 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_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())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user