From 63c0e84b11c603dd87e352c08807ba1505eea379 Mon Sep 17 00:00:00 2001 From: Zitro Date: Wed, 27 Apr 2022 04:13:51 +0400 Subject: [PATCH 1/6] Allow linux users manipulate wm_class and wm_instance of windows --- crates/bevy_internal/Cargo.toml | 4 ++-- crates/bevy_window/Cargo.toml | 7 +++++++ crates/bevy_window/src/window.rs | 8 ++++++++ crates/bevy_winit/src/winit_windows.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 360c957e984bc..fd72b1dfe2880 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -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"] diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index d8bdd76e5bffb..475cd58eea8b4 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -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" } @@ -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"] \ No newline at end of file diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 47c03d085ce7e..40e0bd9999144 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -594,6 +594,10 @@ pub struct WindowDescriptor { pub resize_constraints: WindowResizeConstraints, pub scale_factor_override: Option, pub title: String, + #[cfg(any(feature = "x11", feature = "wayland"))] + pub desktop_id: String, + #[cfg(feature = "x11")] + pub desktop_instance: String, #[doc(alias = "vsync")] pub present_mode: PresentMode, pub resizable: bool, @@ -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(), width: 1280., height: 720., position: None, diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 65354e80882f8..f07388931a147 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -4,6 +4,9 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode}; use raw_window_handle::HasRawWindowHandle; use winit::dpi::LogicalSize; +#[cfg(any(feature = "x11", feature = "wayland"))] +use winit::platform::unix::WindowBuilderExtUnix; + #[derive(Debug, Default)] pub struct WinitWindows { pub windows: HashMap, @@ -102,6 +105,29 @@ impl WinitWindows { #[allow(unused_mut)] let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title); + #[allow(unused_mut)] + #[cfg(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 + */ + + #[allow(unused_mut)] + #[cfg(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; From 9840deb96662fe0f0893134e9a7b4d104cb01ee8 Mon Sep 17 00:00:00 2001 From: Zitro Date: Wed, 27 Apr 2022 04:36:29 +0400 Subject: [PATCH 2/6] polishing code --- crates/bevy_winit/src/winit_windows.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index f07388931a147..01f4e9fe9839f 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -109,24 +109,25 @@ impl WinitWindows { #[cfg(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 + 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 */ - + #[allow(unused_mut)] #[cfg(feature = "wayland")] - let mut winit_window_builder = winit_window_builder.with_app_id(window_descriptor.desktop_id.clone()); + let mut winit_window_builder = + winit_window_builder.with_app_id(window_descriptor.desktop_id.clone()); #[cfg(target_arch = "wasm32")] { From 4b640be7335c5239af3d440078782b62ac6a463f Mon Sep 17 00:00:00 2001 From: Zitro Date: Wed, 27 Apr 2022 04:49:13 +0400 Subject: [PATCH 3/6] Fixing up OS conflicts --- crates/bevy_winit/src/winit_windows.rs | 30 +++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 01f4e9fe9839f..fd39d4e8dc040 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -4,7 +4,15 @@ use bevy_window::{Window, WindowDescriptor, WindowId, WindowMode}; use raw_window_handle::HasRawWindowHandle; use winit::dpi::LogicalSize; -#[cfg(any(feature = "x11", feature = "wayland"))] +#[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)] @@ -106,7 +114,15 @@ impl WinitWindows { let mut winit_window_builder = winit_window_builder.with_title(&window_descriptor.title); #[allow(unused_mut)] - #[cfg(feature = "x11")] + #[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 @@ -125,7 +141,15 @@ impl WinitWindows { */ #[allow(unused_mut)] - #[cfg(feature = "wayland")] + #[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()); From a9ab5ccbbcef2221759b82581ec81871b29c3613 Mon Sep 17 00:00:00 2001 From: Zitro Date: Wed, 27 Apr 2022 17:25:09 +0400 Subject: [PATCH 4/6] Fix OS conflicts once more --- crates/bevy_window/src/window.rs | 40 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 40e0bd9999144..6fa350e931335 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -594,9 +594,25 @@ pub struct WindowDescriptor { pub resize_constraints: WindowResizeConstraints, pub scale_factor_override: Option, pub title: String, - #[cfg(any(feature = "x11", feature = "wayland"))] + #[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(feature = "x11")] + #[cfg(all( + any( + target_os = "linux", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ), + feature = "x11" + ))] pub desktop_instance: String, #[doc(alias = "vsync")] pub present_mode: PresentMode, @@ -621,9 +637,25 @@ impl Default for WindowDescriptor { fn default() -> Self { WindowDescriptor { title: "app".to_string(), - #[cfg(any(feature = "x11", feature = "wayland"))] + #[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(feature = "x11")] + #[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., From 3b81996444362094ce06c982dc916486c27dbf98 Mon Sep 17 00:00:00 2001 From: Zitro Date: Thu, 28 Apr 2022 22:23:09 +0400 Subject: [PATCH 5/6] Clarify WM_CLASS initialisation comment --- crates/bevy_winit/src/winit_windows.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index fd39d4e8dc040..638779e07f5c5 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -124,21 +124,19 @@ impl WinitWindows { 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 + 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 - */ + + /// The right `with_class` function signature is: + /// fn with_class(mut self, instance: String, class: String) -> Self; + + /// When winit is upgraded to v0.27.0, change this to + /// let mut winit_window_builder = winit_window_builder.with_name( + /// window_descriptor.desktop_id.clone(), + /// window_descriptor.desktop_instance.clone() + /// ) + /// (Source) https://github.com/rust-windowing/winit/blob/ce890c34551d9fb542f10dcb644d22d382e0c921/src/platform/unix.rs#L288 #[allow(unused_mut)] #[cfg(all( From 3246b79456f029a9d44c075215d6e175d2746af9 Mon Sep 17 00:00:00 2001 From: Zitro Date: Thu, 28 Apr 2022 22:42:09 +0400 Subject: [PATCH 6/6] Fix unintentional doc comment --- crates/bevy_winit/src/winit_windows.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/bevy_winit/src/winit_windows.rs b/crates/bevy_winit/src/winit_windows.rs index 638779e07f5c5..f77566737da1c 100644 --- a/crates/bevy_winit/src/winit_windows.rs +++ b/crates/bevy_winit/src/winit_windows.rs @@ -128,15 +128,15 @@ impl WinitWindows { window_descriptor.desktop_id.clone(), // And that's WM_CLASS ); - /// The right `with_class` function signature is: - /// fn with_class(mut self, instance: String, class: String) -> Self; - - /// When winit is upgraded to v0.27.0, change this to - /// let mut winit_window_builder = winit_window_builder.with_name( - /// window_descriptor.desktop_id.clone(), - /// window_descriptor.desktop_instance.clone() - /// ) - /// (Source) https://github.com/rust-windowing/winit/blob/ce890c34551d9fb542f10dcb644d22d382e0c921/src/platform/unix.rs#L288 + // The right `with_class` function signature is: + // fn with_class(mut self, instance: String, class: String) -> Self; + + // When winit is upgraded to v0.27.0, change this to + // let mut winit_window_builder = winit_window_builder.with_name( + // window_descriptor.desktop_id.clone(), + // window_descriptor.desktop_instance.clone() + // ) + // (Source) https://github.com/rust-windowing/winit/blob/ce890c34551d9fb542f10dcb644d22d382e0c921/src/platform/unix.rs#L288 #[allow(unused_mut)] #[cfg(all(