xlib_display_server/xwrap/setters.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
//! `XWrap` setters.
use super::WindowHandle;
use crate::{XWrap, XlibWindowHandle};
use leftwm_core::models::TagId;
use std::ffi::CString;
use std::os::raw::{c_long, c_ulong};
use x11_dl::xlib;
impl XWrap {
// Public functions.
/// Appends a window property.
// `XChangeProperty`: https://tronche.com/gui/x/xlib/window-information/XChangeProperty.html
pub fn append_property_long(
&self,
window: xlib::Window,
property: xlib::Atom,
r#type: xlib::Atom,
data: &[c_long],
) {
unsafe {
(self.xlib.XChangeProperty)(
self.display,
window,
property,
r#type,
32,
xlib::PropModeAppend,
data.as_ptr().cast::<u8>(),
data.len() as i32,
);
}
}
/// Replaces a window property.
// `XChangeProperty`: https://tronche.com/gui/x/xlib/window-information/XChangeProperty.html
pub fn replace_property_long(
&self,
window: xlib::Window,
property: xlib::Atom,
r#type: xlib::Atom,
data: &[c_long],
) {
unsafe {
(self.xlib.XChangeProperty)(
self.display,
window,
property,
r#type,
32,
xlib::PropModeReplace,
data.as_ptr().cast::<u8>(),
data.len() as i32,
);
}
}
/// Sets the client list to the currently managed windows.
// `XDeleteProperty`: https://tronche.com/gui/x/xlib/window-information/XDeleteProperty.html
pub fn set_client_list(&self) {
unsafe {
(self.xlib.XDeleteProperty)(self.display, self.root, self.atoms.NetClientList);
}
for w in &self.managed_windows {
let list = vec![*w as c_long];
self.append_property_long(self.root, self.atoms.NetClientList, xlib::XA_WINDOW, &list);
}
}
/// Sets the current desktop.
pub fn set_current_desktop(&self, current_tag: Option<TagId>) {
let indexes: Vec<u32> = match current_tag {
Some(tag) => vec![tag as u32 - 1],
None => vec![0],
};
self.set_desktop_prop(&indexes, self.atoms.NetCurrentDesktop);
}
// /// Sets the current viewport.
// fn set_current_viewport(&self, tags: Vec<&String>) {
// let mut indexes: Vec<u32> = vec![];
// for tag in tags {
// for (i, mytag) in self.tags.iter().enumerate() {
// if tag.contains(mytag) {
// indexes.push(i as u32);
// }
// }
// }
// if indexes.is_empty() {
// indexes.push(0);
// }
// self.set_desktop_prop(&indexes, self.atoms.NetDesktopViewport);
// }
/// Sets a desktop property.
// We allow the lossless cast here so that 32 bit systems may work with
// leftwm. See https://github.com/leftwm/leftwm/discussions/1201 for
// more details.
#[allow(clippy::cast_lossless)]
pub fn set_desktop_prop(&self, data: &[u32], atom: c_ulong) {
let x_data: Vec<c_long> = data.iter().map(|x| *x as c_long).collect();
self.replace_property_long(self.root, atom, xlib::XA_CARDINAL, &x_data);
}
/// Sets a desktop property with type `c_ulong`.
pub fn set_desktop_prop_c_ulong(&self, value: c_ulong, atom: c_ulong, r#type: c_ulong) {
let data = vec![value as c_long];
self.replace_property_long(self.root, atom, r#type, &data);
}
/// Sets a desktop property with type string.
// `XChangeProperty`: https://tronche.com/gui/x/xlib/window-information/XChangeProperty.html
pub fn set_desktop_prop_string(&self, value: &str, atom: c_ulong, encoding: xlib::Atom) {
if let Ok(cstring) = CString::new(value) {
unsafe {
(self.xlib.XChangeProperty)(
self.display,
self.root,
atom,
encoding,
8,
xlib::PropModeReplace,
cstring.as_ptr().cast::<u8>(),
value.len() as i32,
);
std::mem::forget(cstring);
}
}
}
/// Sets a windows state.
pub fn set_state(
&self,
handle: WindowHandle<XlibWindowHandle>,
toggle_to: bool,
atom: xlib::Atom,
) {
let WindowHandle(XlibWindowHandle(h)) = handle;
let mut states = self.get_window_states_atoms(h);
if toggle_to {
if states.contains(&atom) {
return;
}
states.push(atom);
} else {
let Some(index) = states.iter().position(|s| s == &atom) else {
return;
};
states.remove(index);
}
self.set_window_states_atoms(h, &states);
}
/// Sets a windows border color.
// `XSetWindowBorder`: https://tronche.com/gui/x/xlib/window/XSetWindowBorder.html
pub fn set_window_border_color(&self, window: xlib::Window, mut color: c_ulong) {
unsafe {
// Force border opacity to 0xff. (color is <aarrggbb> in hex format)
color |= 0xff00_0000;
(self.xlib.XSetWindowBorder)(self.display, window, color);
}
}
pub fn set_background_color(&self, mut color: c_ulong) {
unsafe {
// Force border opacity to 0xff. (color is <aarrggbb> in hex format)
color |= 0xff00_0000;
(self.xlib.XSetWindowBackground)(self.display, self.root, color);
(self.xlib.XClearWindow)(self.display, self.root);
(self.xlib.XFlush)(self.display);
(self.xlib.XSync)(self.display, 0);
}
}
/// Sets a windows configuration.
pub fn set_window_config(
&self,
window: xlib::Window,
mut window_changes: xlib::XWindowChanges,
unlock: u32,
) {
unsafe { (self.xlib.XConfigureWindow)(self.display, window, unlock, &mut window_changes) };
self.sync();
}
/// Sets what desktop a window is on.
pub fn set_window_desktop(&self, window: xlib::Window, current_tag: &TagId) {
let mut indexes: Vec<c_long> = vec![*current_tag as c_long - 1];
if indexes.is_empty() {
indexes.push(0);
}
self.replace_property_long(window, self.atoms.NetWMDesktop, xlib::XA_CARDINAL, &indexes);
}
/// Sets the atom states of a window.
pub fn set_window_states_atoms(&self, window: xlib::Window, states: &[xlib::Atom]) {
let data: Vec<c_long> = states.iter().map(|x| *x as c_long).collect();
self.replace_property_long(window, self.atoms.NetWMState, xlib::XA_ATOM, &data);
}
pub fn set_window_urgency(&self, window: xlib::Window, is_urgent: bool) {
if let Some(mut wmh) = self.get_wmhints(window) {
if ((wmh.flags & xlib::XUrgencyHint) != 0) == is_urgent {
return;
}
wmh.flags = if is_urgent {
wmh.flags | xlib::XUrgencyHint
} else {
wmh.flags & !xlib::XUrgencyHint
};
self.set_wmhints(window, &mut wmh);
}
}
/// Sets the `XWMHints` of a window.
pub fn set_wmhints(&self, window: xlib::Window, wmh: &mut xlib::XWMHints) {
unsafe { (self.xlib.XSetWMHints)(self.display, window, wmh) };
}
/// Sets the `WM_STATE` of a window.
pub fn set_wm_states(&self, window: xlib::Window, states: &[c_long]) {
self.replace_property_long(window, self.atoms.WMState, self.atoms.WMState, states);
}
}