mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-08 04:31:29 +11:00
c4b92ebd45
* X11: General cleanup This is almost entirely internal changes, and as usual, doesn't actually fix any problems people have complained about. - `XSetInputFocus` can't be called before the window is visible. This was previously handled by looping (with a sleep) and querying for the window's state until it was visible. Now we use `XIfEvent`, which blocks until we receive `VisibilityNotify`. Note that this can't be replaced with an `XSync` (I tried). - We now call `XSync` at the end of window creation and check for errors, assuring that broken windows are never returned. When creating invisible windows, this is the only time the output buffer is flushed during the entire window creation process (AFAIK). For visible windows, `XIfEvent` will generally flush, but window creation has overall been reduced to the minimum number of flushes. - `check_errors().expect()` has been a common pattern throughout the backend, but it seems that people (myself included) didn't make a distinction between using it after synchronous requests and asynchronous requests. Now we only use it after async requests if we flush first, though this still isn't correct (since the request likely hasn't been processed yet). The only real solution (besides forcing a sync *every time*) is to handle asynchronous errors *asynchronously*. For future work, I plan on adding logging, though I don't plan on actually *handling* those errors; that's more of something to hope for in the hypothetical async/await XCB paradise. - We now flush whenever it makes sense to. `util::Flusher` was added to force contributors to be aware of the output buffer. - `Window::get_position`, `Window::get_inner_position`, `Window::get_inner_size`, and `Window::get_outer_size` previously all required *several* round-trips. On my machine, it took an average of around 80µs. They've now been reduced to one round-trip each, which reduces my measurement to 16µs. This was accomplished simply by caching the frame extents, which are expensive to calculate (due to various queries and heuristics), but change infrequently and predictably. I still recommend that application developers use these methods sparingly and generally prefer storing the values from `Resized`/`Moved`, as that's zero overhead. - The above change enabled me to change the `Moved` event to supply window positions, rather than client area positions. Additionally, we no longer generate `Moved` for real (as in, not synthetic) `ConfigureNotify` events. Real `ConfigureNotify` events contain positions relative to the parent window, which are typically constant and useless. Since that position would be completely different from the root-relative positions supplied by synthetic `ConfigureNotify` events (which are the vast majority of them), that meant real `ConfigureNotify` events would *always* be detected as the position having changed, so the resultant `Moved` was multiple levels of misleading. In practice, this meant a garbage `Moved` would be sent every time the window was resized; now a resize has to actually change the window's position to be accompanied by `Moved`. - Every time we processed an `XI_Enter` event, we would leak 4 bytes via `util::query_pointer` (`XIQueryPointer`). `XIButtonState` contains a dynamically-allocated mask field which we weren't freeing. As this event occurs with fairly high frequency, long-running applications could easily accumulate substantial leaks. `util::PointerState::drop` now takes care of this. - The `util` module has been split up into several sub-modules, as it was getting rather lengthy. This accounts for a significant part of this diff, unfortunately. - Atoms are now cached. Xlib caches them too, so `XInternAtom` wouldn't typically be a round-trip anyway, but the added complexity is negligible. - Switched from `std::sync::Mutex` to `parking_lot::Mutex` (within this backend). There appears to be no downside to this, but if anyone finds one, this would be easy to revert. - The WM name and supported hints are now global to the application, and are updated upon `ReparentNotify`, which should detect when the WM was replaced (assuming a reparenting WM was involved, that is). Previously, these values were per-window and would never update, meaning replacing the WM could potentially lead to (admittedly very minor) problems. - The result of `Window2::create_empty_cursor` will now only be used if it actually succeeds. - `Window2::load_cursor` no longer re-allocates the cursor name. - `util::lookup_utf8` previously allocated a 16-byte buffer on the heap. Now it allocates a 1024-byte buffer on the stack, and falls back to dynamic allocation if the buffer is too small. This base buffer size is admittedly gratuitous, but less so if you're using IME. - `with_c_str` was finally removed. - Added `util::Format` enum to help prevent goofs when dealing with format arguments. - `util::get_property`, something I added way back in my first winit PR, only calculated offsets correctly for `util::Format::Char`. This was concealed by the accomodating buffer size, as it would be very rare for the offset to be needed; however, testing with a buffer size of 1, `util::Format::Long` would read from the same offset multiple times, and `util::Format::Short` would miss data. This function now works correctly for all formats, relying on the simple fact that the offset increases by the buffer size on each iteration. We also account for the extra byte that `XGetWindowProperty` allocates at the end of the buffer, and copy data from the buffer instead of moving it and taking ownership of the pointer. - Drag and drop now reliably works in release mode. This is presumably related to the `util::get_property` changes. - `util::change_property` now exists, which should make it easier to add features in the future. - The `EventsLoop` device map is no longer in a mutex. - `XConnection` now implements `Debug`. - Valgrind no longer complains about anything related to winit (with either the system allocator or jemalloc, though "not having valgrind complain about jemalloc" isn't something to strive for). * X11: Add better diagnostics when initialization fails * X11: Handle XIQueryDevice failure * X11: Use correct types in error handler
159 lines
5 KiB
Rust
159 lines
5 KiB
Rust
use parking_lot::Mutex;
|
|
|
|
use super::*;
|
|
|
|
// This info is global to the window manager.
|
|
lazy_static! {
|
|
static ref SUPPORTED_HINTS: Mutex<Vec<ffi::Atom>> = Mutex::new(Vec::with_capacity(0));
|
|
static ref WM_NAME: Mutex<Option<String>> = Mutex::new(None);
|
|
}
|
|
|
|
pub fn hint_is_supported(hint: ffi::Atom) -> bool {
|
|
(*SUPPORTED_HINTS.lock()).contains(&hint)
|
|
}
|
|
|
|
pub fn wm_name_is_one_of(names: &[&str]) -> bool {
|
|
if let Some(ref name) = *WM_NAME.lock() {
|
|
names.contains(&name.as_str())
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn update_cached_wm_info(xconn: &Arc<XConnection>, root: ffi::Window) {
|
|
*SUPPORTED_HINTS.lock() = self::get_supported_hints(xconn, root);
|
|
*WM_NAME.lock() = self::get_wm_name(xconn, root);
|
|
}
|
|
|
|
fn get_supported_hints(xconn: &Arc<XConnection>, root: ffi::Window) -> Vec<ffi::Atom> {
|
|
let supported_atom = unsafe { self::get_atom(xconn, b"_NET_SUPPORTED\0") }
|
|
.expect("Failed to call XInternAtom (_NET_SUPPORTED)");
|
|
unsafe {
|
|
self::get_property(
|
|
xconn,
|
|
root,
|
|
supported_atom,
|
|
ffi::XA_ATOM,
|
|
)
|
|
}.unwrap_or_else(|_| Vec::with_capacity(0))
|
|
}
|
|
|
|
fn get_wm_name(xconn: &Arc<XConnection>, root: ffi::Window) -> Option<String> {
|
|
let check_atom = unsafe { self::get_atom(xconn, b"_NET_SUPPORTING_WM_CHECK\0") }
|
|
.expect("Failed to call XInternAtom (_NET_SUPPORTING_WM_CHECK)");
|
|
let wm_name_atom = unsafe { self::get_atom(xconn, b"_NET_WM_NAME\0") }
|
|
.expect("Failed to call XInternAtom (_NET_WM_NAME)");
|
|
|
|
// Mutter/Muffin/Budgie doesn't have _NET_SUPPORTING_WM_CHECK in its _NET_SUPPORTED, despite
|
|
// it working and being supported. This has been reported upstream, but due to the
|
|
// inavailability of time machines, we'll just try to get _NET_SUPPORTING_WM_CHECK
|
|
// regardless of whether or not the WM claims to support it.
|
|
//
|
|
// Blackbox 0.70 also incorrectly reports not supporting this, though that appears to be fixed
|
|
// in 0.72.
|
|
/*if !supported_hints.contains(&check_atom) {
|
|
return None;
|
|
}*/
|
|
|
|
// IceWM (1.3.x and earlier) doesn't report supporting _NET_WM_NAME, but will nonetheless
|
|
// provide us with a value for it. Note that the unofficial 1.4 fork of IceWM works fine.
|
|
/*if !supported_hints.contains(&wm_name_atom) {
|
|
return None;
|
|
}*/
|
|
|
|
// Of the WMs tested, only xmonad and dwm fail to provide a WM name.
|
|
|
|
// Querying this property on the root window will give us the ID of a child window created by
|
|
// the WM.
|
|
let root_window_wm_check = {
|
|
let result = unsafe {
|
|
self::get_property(
|
|
xconn,
|
|
root,
|
|
check_atom,
|
|
ffi::XA_WINDOW,
|
|
)
|
|
};
|
|
|
|
let wm_check = result
|
|
.ok()
|
|
.and_then(|wm_check| wm_check.get(0).cloned());
|
|
|
|
if let Some(wm_check) = wm_check {
|
|
wm_check
|
|
} else {
|
|
return None;
|
|
}
|
|
};
|
|
|
|
// Querying the same property on the child window we were given, we should get this child
|
|
// window's ID again.
|
|
let child_window_wm_check = {
|
|
let result = unsafe {
|
|
self::get_property(
|
|
xconn,
|
|
root_window_wm_check,
|
|
check_atom,
|
|
ffi::XA_WINDOW,
|
|
)
|
|
};
|
|
|
|
let wm_check = result
|
|
.ok()
|
|
.and_then(|wm_check| wm_check.get(0).cloned());
|
|
|
|
if let Some(wm_check) = wm_check {
|
|
wm_check
|
|
} else {
|
|
return None;
|
|
}
|
|
};
|
|
|
|
// These values should be the same.
|
|
if root_window_wm_check != child_window_wm_check {
|
|
return None;
|
|
}
|
|
|
|
// All of that work gives us a window ID that we can get the WM name from.
|
|
let wm_name = {
|
|
let utf8_string_atom = unsafe { self::get_atom(xconn, b"UTF8_STRING\0") }
|
|
.expect("Failed to call XInternAtom (UTF8_STRING)");
|
|
|
|
let result = unsafe {
|
|
self::get_property(
|
|
xconn,
|
|
root_window_wm_check,
|
|
wm_name_atom,
|
|
utf8_string_atom,
|
|
)
|
|
};
|
|
|
|
// IceWM requires this. IceWM was also the only WM tested that returns a null-terminated
|
|
// string. For more fun trivia, IceWM is also unique in including version and uname
|
|
// information in this string (this means you'll have to be careful if you want to match
|
|
// against it, though).
|
|
// The unofficial 1.4 fork of IceWM still includes the extra details, but properly
|
|
// returns a UTF8 string that isn't null-terminated.
|
|
let no_utf8 = if let Err(ref err) = result {
|
|
err.is_actual_property_type(ffi::XA_STRING)
|
|
} else {
|
|
false
|
|
};
|
|
|
|
if no_utf8 {
|
|
unsafe {
|
|
self::get_property(
|
|
xconn,
|
|
root_window_wm_check,
|
|
wm_name_atom,
|
|
ffi::XA_STRING,
|
|
)
|
|
}
|
|
} else {
|
|
result
|
|
}
|
|
}.ok();
|
|
|
|
wm_name.and_then(|wm_name| String::from_utf8(wm_name).ok())
|
|
}
|