1// Welcome to the util module, where we try to keep you from shooting yourself in the foot.
2// *results may vary
34use std::mem::{self, MaybeUninit};
5use std::ops::BitAnd;
6use std::os::raw::*;
78mod client_msg;
9pub mod cookie;
10mod cursor;
11mod geometry;
12mod hint;
13mod icon;
14mod input;
15pub mod keys;
16pub(crate) mod memory;
17mod mouse;
18mod randr;
19mod window_property;
20mod wm;
21mod xmodmap;
2223pub use self::cursor::*;
24pub use self::geometry::*;
25pub use self::hint::*;
26pub use self::input::*;
27pub use self::mouse::*;
28pub use self::window_property::*;
29pub use self::wm::*;
30pub use self::xmodmap::ModifierKeymap;
3132use super::atoms::*;
33use super::{ffi, VoidCookie, X11Error, XConnection, XError};
34use x11rb::protocol::xproto::{self, ConnectionExt as _};
3536pub fn maybe_change<T: PartialEq>(field: &mut Option<T>, value: T) -> bool {
37let wrapped = Some(value);
38if *field != wrapped {
39*field = wrapped;
40true
41} else {
42false
43}
44}
4546pub fn has_flag<T>(bitset: T, flag: T) -> bool
47where
48T: Copy + PartialEq + BitAnd<T, Output = T>,
49{
50 bitset & flag == flag
51}
5253impl XConnection {
54// This is impoartant, so pay attention!
55 // Xlib has an output buffer, and tries to hide the async nature of X from you.
56 // This buffer contains the requests you make, and is flushed under various circumstances:
57 // 1. `XPending`, `XNextEvent`, and `XWindowEvent` flush "as needed"
58 // 2. `XFlush` explicitly flushes
59 // 3. `XSync` flushes and blocks until all requests are responded to
60 // 4. Calls that have a return dependent on a response (i.e. `XGetWindowProperty`) sync
61 // internally. When in doubt, check the X11 source; if a function calls `_XReply`, it flushes
62 // and waits.
63 // All util functions that abstract an async function will return a `Flusher`.
64pub fn flush_requests(&self) -> Result<(), XError> {
65unsafe { (self.xlib.XFlush)(self.display) };
66// println!("XFlush");
67 // This isn't necessarily a useful time to check for errors (since our request hasn't
68 // necessarily been processed yet)
69self.check_errors()
70 }
7172pub fn sync_with_server(&self) -> Result<(), XError> {
73unsafe { (self.xlib.XSync)(self.display, ffi::False) };
74// println!("XSync");
75self.check_errors()
76 }
77}