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:
2022-05-03 17:05:06 -07:00
commit 2e86445c3d
29 changed files with 4597 additions and 0 deletions

13
util/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "bscreensaver-util"
version = "0.1.0"
edition = "2021"
[lib]
[dependencies]
clap = "3"
env_logger = "0.9"
lazy_static = "1"
libc = "0.2"
xcb = { git = "https://github.com/rust-x-bindings/rust-xcb", rev = "d09b5f91bc07d56673f1bc0d6c7ecd72b5ff7b3e", features = ["randr", "screensaver", "xfixes"] }

50
util/src/lib.rs Normal file
View 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())
}
}