azul_winit/platform_impl/linux/x11/util/
mod.rs

1// Welcome to the util module, where we try to keep you from shooting yourself in the foot.
2// *results may vary
3
4mod atom;
5mod client_msg;
6mod cursor;
7mod format;
8mod geometry;
9mod hint;
10mod icon;
11mod input;
12pub mod keys;
13mod memory;
14pub mod modifiers;
15mod randr;
16mod window_property;
17mod wm;
18
19pub use self::{
20    atom::*, client_msg::*, format::*, geometry::*, hint::*, icon::*, input::*, memory::*,
21    randr::*, window_property::*, wm::*,
22};
23
24use std::{
25    mem::{self, MaybeUninit},
26    ops::BitAnd,
27    os::raw::*,
28    ptr,
29};
30
31use super::{ffi, XConnection, XError};
32
33pub fn maybe_change<T: PartialEq>(field: &mut Option<T>, value: T) -> bool {
34    let wrapped = Some(value);
35    if *field != wrapped {
36        *field = wrapped;
37        true
38    } else {
39        false
40    }
41}
42
43pub fn has_flag<T>(bitset: T, flag: T) -> bool
44where
45    T: Copy + PartialEq + BitAnd<T, Output = T>,
46{
47    bitset & flag == flag
48}
49
50#[must_use = "This request was made asynchronously, and is still in the output buffer. You must explicitly choose to either `.flush()` (empty the output buffer, sending the request now) or `.queue()` (wait to send the request, allowing you to continue to add more requests without additional round-trips). For more information, see the documentation for `util::flush_requests`."]
51pub struct Flusher<'a> {
52    xconn: &'a XConnection,
53}
54
55impl<'a> Flusher<'a> {
56    pub fn new(xconn: &'a XConnection) -> Self {
57        Flusher { xconn }
58    }
59
60    // "I want this request sent now!"
61    pub fn flush(self) -> Result<(), XError> {
62        self.xconn.flush_requests()
63    }
64
65    // "I want the response now too!"
66    pub fn sync(self) -> Result<(), XError> {
67        self.xconn.sync_with_server()
68    }
69
70    // "I'm aware that this request hasn't been sent, and I'm okay with waiting."
71    pub fn queue(self) {}
72}
73
74impl XConnection {
75    // This is impoartant, so pay attention!
76    // Xlib has an output buffer, and tries to hide the async nature of X from you.
77    // This buffer contains the requests you make, and is flushed under various circumstances:
78    // 1. `XPending`, `XNextEvent`, and `XWindowEvent` flush "as needed"
79    // 2. `XFlush` explicitly flushes
80    // 3. `XSync` flushes and blocks until all requests are responded to
81    // 4. Calls that have a return dependent on a response (i.e. `XGetWindowProperty`) sync internally.
82    //    When in doubt, check the X11 source; if a function calls `_XReply`, it flushes and waits.
83    // All util functions that abstract an async function will return a `Flusher`.
84    pub fn flush_requests(&self) -> Result<(), XError> {
85        unsafe { (self.xlib.XFlush)(self.display) };
86        //println!("XFlush");
87        // This isn't necessarily a useful time to check for errors (since our request hasn't
88        // necessarily been processed yet)
89        self.check_errors()
90    }
91
92    pub fn sync_with_server(&self) -> Result<(), XError> {
93        unsafe { (self.xlib.XSync)(self.display, ffi::False) };
94        //println!("XSync");
95        self.check_errors()
96    }
97}