cabytcini/src/xsetroot.rs

57 lines
1.2 KiB
Rust
Raw Normal View History

2020-04-07 01:09:09 +00:00
#![allow(unsafe_code)]
2020-04-10 11:18:51 +00:00
use anyhow::{anyhow, Result};
2020-04-07 01:09:09 +00:00
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;
use x11::xlib;
pub(crate) struct XSetRoot {
display: *mut xlib::Display,
root_window: xlib::Window,
}
impl XSetRoot {
2020-04-10 11:18:51 +00:00
pub(crate) fn init() -> Result<Self> {
2020-04-07 01:09:09 +00:00
unsafe {
let display = xlib::XOpenDisplay(ptr::null());
if display.is_null() {
2020-04-10 11:18:51 +00:00
return Err(anyhow!("cannot open display"));
2020-04-07 01:09:09 +00:00
}
let screen = xlib::XDefaultScreen(display);
let root_window = xlib::XRootWindow(display, screen);
2020-04-10 11:18:51 +00:00
Ok(Self {
2020-04-07 01:09:09 +00:00
display,
root_window,
2020-04-10 11:18:51 +00:00
})
2020-04-07 01:09:09 +00:00
}
}
2020-04-10 11:18:51 +00:00
pub(crate) fn render(&self, text: String) -> Result<()> {
let status_c = CString::new(text)?;
2020-04-07 01:09:09 +00:00
unsafe {
xlib::XStoreName(
self.display,
self.root_window,
status_c.as_ptr() as *mut c_char,
);
xlib::XFlush(self.display);
}
2020-04-10 11:18:51 +00:00
Ok(())
2020-04-07 01:09:09 +00:00
}
}
impl Drop for XSetRoot {
fn drop(&mut self) {
unsafe {
xlib::XCloseDisplay(self.display);
}
}
}