Skip to content

Allow linux users manipulate wm_class and wm_instance of windows #4609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ filesystem_watcher = ["bevy_asset/filesystem_watcher"]
serialize = ["bevy_input/serialize"]

# Display server protocol support (X11 is enabled by default)
wayland = ["bevy_winit/wayland"]
x11 = ["bevy_winit/x11"]
wayland = ["bevy_winit/wayland", "bevy_window/wayland"]
x11 = ["bevy_winit/x11", "bevy_window/x11"]

# enable rendering of font glyphs using subpixel accuracy
subpixel_glyph_atlas = ["bevy_text/subpixel_glyph_atlas"]
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_window/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[features]
wayland = []
x11 = []

[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.8.0-dev" }
Expand All @@ -20,3 +24,6 @@ raw-window-handle = "0.4.2"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = "0.3"

[package.metadata.docs.rs]
features = ["x11"]
8 changes: 8 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ pub struct WindowDescriptor {
pub resize_constraints: WindowResizeConstraints,
pub scale_factor_override: Option<f64>,
pub title: String,
#[cfg(any(feature = "x11", feature = "wayland"))]
pub desktop_id: String,
#[cfg(feature = "x11")]
pub desktop_instance: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(any(feature = "x11", feature = "wayland"))]
pub desktop_id: String,
#[cfg(feature = "x11")]
pub desktop_instance: String,
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
any(feature = "x11", feature = "wayland")
))]
pub desktop_id: String,
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "x11"
))]
pub desktop_instance: String,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I changed platform configuration, now committing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it restricted to linux and {free,net,open}bsd? Is it not supported on macOS with X for example? Or on other systems using X natively?

#[doc(alias = "vsync")]
pub present_mode: PresentMode,
pub resizable: bool,
Expand All @@ -617,6 +621,10 @@ impl Default for WindowDescriptor {
fn default() -> Self {
WindowDescriptor {
title: "app".to_string(),
#[cfg(any(feature = "x11", feature = "wayland"))]
desktop_id: "app".to_string(),
#[cfg(feature = "x11")]
desktop_instance: "Main".to_string(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(any(feature = "x11", feature = "wayland"))]
desktop_id: "app".to_string(),
#[cfg(feature = "x11")]
desktop_instance: "Main".to_string(),
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
any(feature = "x11", feature = "wayland")
))]
desktop_id: "app".to_string(),
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "x11"
))]
desktop_instance: "Main".to_string(),

width: 1280.,
height: 720.,
position: None,
Expand Down
51 changes: 51 additions & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode};
use raw_window_handle::HasRawWindowHandle;
use winit::dpi::LogicalSize;

#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
any(feature = "x11", feature = "wayland")
))]
use winit::platform::unix::WindowBuilderExtUnix;

#[derive(Debug, Default)]
pub struct WinitWindows {
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
Expand Down Expand Up @@ -102,6 +113,46 @@ impl WinitWindows {
#[allow(unused_mut)]
let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title);

#[allow(unused_mut)]
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "x11"
))]
let mut winit_window_builder = winit_window_builder.with_class(
window_descriptor.desktop_instance.clone(), // that's actually wm_instance
window_descriptor.desktop_id.clone(), // and that's wm_class
);
/*
https://github.com/rust-windowing/winit/blob/ea1c031b54438e64576353b288848c07d2068214/src/platform/unix.rs#L337
337| fn with_class(self, class: String, instance: String) -> Self;
https://github.com/rust-windowing/winit/blob/ea1c031b54438e64576353b288848c07d2068214/src/platform/unix.rs#L383
383| fn with_class(mut self, instance: String, class: String) -> Self {

Because of type equality someone wrote those wrong and Rust didn't catch it!
rust-analyser uses only trait declaration, therefore it shows wrong parameter names

This bug persists not only in 0.26.0, but also in 0.26.1
The next update will be breaking, no more differentiation between x11 class and wayland appid
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to repeatedly read over this comment to understand why it was added.

Either this comment or another comment inside the with_class call should have an actionable Instrucction.

// TODO: Switch these two calls once winit releases 0.27 or later.

With such a comment, the rest should be much clearer.
Perhaps also prepend the URLs with Trait and Trait Impl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It unifies these two methods and in wayland case, the instance field will be noop.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment explains the issue with misleading argument names, which led to me searching the internet for correct variant.


#[allow(unused_mut)]
#[cfg(all(
any(
target_os = "linux",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "wayland"
))]
let mut winit_window_builder =
winit_window_builder.with_app_id(window_descriptor.desktop_id.clone());

#[cfg(target_arch = "wasm32")]
{
use wasm_bindgen::JsCast;
Expand Down