From 5e77d70245995417aa412c5e350c4163f8d34755 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Sun, 25 Dec 2022 09:57:27 +0200 Subject: [PATCH] Use cfg aliases throught the code base Co-authored-by: Mads Marquart --- Cargo.toml | 3 + build.rs | 21 ++ examples/child_window.rs | 12 +- examples/custom_events.rs | 4 +- examples/multithreaded.rs | 4 +- examples/request_redraw_threaded.rs | 4 +- examples/web.rs | 6 +- examples/web_aspect_ratio.rs | 2 +- examples/window_run_return.rs | 15 +- src/event_loop.rs | 13 +- src/platform/mod.rs | 45 ++--- src/platform/wayland.rs | 6 +- src/platform/x11.rs | 12 +- src/platform_impl/android/mod.rs | 2 +- src/platform_impl/ios/mod.rs | 2 +- src/platform_impl/linux/mod.rs | 184 +++++++++--------- .../linux/wayland/event_loop/mod.rs | 6 +- src/platform_impl/linux/wayland/mod.rs | 8 +- src/platform_impl/linux/wayland/window/mod.rs | 4 +- src/platform_impl/linux/x11/mod.rs | 10 +- src/platform_impl/linux/x11/window.rs | 2 +- src/platform_impl/mod.rs | 35 ++-- src/platform_impl/windows/mod.rs | 2 +- src/platform_impl/windows/window.rs | 2 +- src/window.rs | 2 +- tests/send_objects.rs | 4 +- tests/sync_object.rs | 2 +- 27 files changed, 185 insertions(+), 227 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index f6b8918d..c86d1dee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,9 @@ wayland-csd-adwaita-notitle = ["sctk-adwaita"] android-native-activity = [ "android-activity/native-activity" ] android-game-activity = [ "android-activity/game-activity" ] +[build-dependencies] +cfg_aliases = "0.1.1" + [dependencies] instant = { version = "0.1", features = ["wasm-bindgen"] } once_cell = "1.12" diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..3f802290 --- /dev/null +++ b/build.rs @@ -0,0 +1,21 @@ +use cfg_aliases::cfg_aliases; + +fn main() { + // The script doesn't depend on our code + println!("cargo:rerun-if-changed=build.rs"); + // Setup cfg aliases + cfg_aliases! { + // Systems. + android_platform: { target_os = "android" }, + wasm_platform: { target_arch = "wasm32" }, + macos_platform: { target_os = "macos" }, + ios_platform: { target_os = "ios" }, + windows_platform: { target_os = "windows" }, + apple: { any(target_os = "ios", target_os = "macos") }, + free_unix: { all(unix, not(apple), not(android_platform)) }, + + // Native displays. + x11_platform: { all(feature = "x11", free_unix, not(wasm)) }, + wayland_platform: { all(feature = "wayland", free_unix, not(wasm)) }, + } +} diff --git a/examples/child_window.rs b/examples/child_window.rs index 865e72f5..8cb1a883 100644 --- a/examples/child_window.rs +++ b/examples/child_window.rs @@ -1,8 +1,4 @@ -#[cfg(any( - all(target_os = "linux", feature = "x11"), - target_os = "macos", - target_os = "windows" -))] +#[cfg(any(x11_platform, macos_platform, windows_platform))] fn main() { use std::collections::HashMap; @@ -78,11 +74,7 @@ fn main() { }) } -#[cfg(not(any( - all(target_os = "linux", feature = "x11"), - target_os = "macos", - target_os = "windows" -)))] +#[cfg(not(any(x11_platform, macos_platform, windows_platform)))] fn main() { panic!("This example is supported only on x11, macOS, and Windows."); } diff --git a/examples/custom_events.rs b/examples/custom_events.rs index c1b7133d..61077c8b 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -1,6 +1,6 @@ #![allow(clippy::single_match)] -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] fn main() { use simple_logger::SimpleLogger; use winit::{ @@ -49,7 +49,7 @@ fn main() { }); } -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] fn main() { panic!("This example is not supported on web."); } diff --git a/examples/multithreaded.rs b/examples/multithreaded.rs index b999f6c9..a5bdcd3d 100644 --- a/examples/multithreaded.rs +++ b/examples/multithreaded.rs @@ -1,6 +1,6 @@ #![allow(clippy::single_match)] -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] fn main() { use std::{collections::HashMap, sync::mpsc, thread, time::Duration}; @@ -193,7 +193,7 @@ fn main() { }) } -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] fn main() { panic!("Example not supported on Wasm"); } diff --git a/examples/request_redraw_threaded.rs b/examples/request_redraw_threaded.rs index 2500a8c0..759de6f6 100644 --- a/examples/request_redraw_threaded.rs +++ b/examples/request_redraw_threaded.rs @@ -1,6 +1,6 @@ #![allow(clippy::single_match)] -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] fn main() { use std::{thread, time}; @@ -42,7 +42,7 @@ fn main() { }); } -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] fn main() { unimplemented!() // `Window` can't be sent between threads } diff --git a/examples/web.rs b/examples/web.rs index 82a3971a..fe758064 100644 --- a/examples/web.rs +++ b/examples/web.rs @@ -14,13 +14,13 @@ pub fn main() { .build(&event_loop) .unwrap(); - #[cfg(target_arch = "wasm32")] + #[cfg(wasm_platform)] let log_list = wasm::insert_canvas_and_create_log_list(&window); event_loop.run(move |event, _, control_flow| { control_flow.set_wait(); - #[cfg(target_arch = "wasm32")] + #[cfg(wasm_platform)] wasm::log_event(&log_list, &event); match event { @@ -36,7 +36,7 @@ pub fn main() { }); } -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] mod wasm { use wasm_bindgen::prelude::*; use winit::{event::Event, window::Window}; diff --git a/examples/web_aspect_ratio.rs b/examples/web_aspect_ratio.rs index 0bbba211..a1983dfc 100644 --- a/examples/web_aspect_ratio.rs +++ b/examples/web_aspect_ratio.rs @@ -2,7 +2,7 @@ pub fn main() { println!("This example must be run with cargo run-wasm --example web_aspect_ratio") } -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] mod wasm { use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; diff --git a/examples/window_run_return.rs b/examples/window_run_return.rs index e6b3c662..f9ef8dbf 100644 --- a/examples/window_run_return.rs +++ b/examples/window_run_return.rs @@ -2,14 +2,11 @@ // Limit this example to only compatible platforms. #[cfg(any( - target_os = "windows", - target_os = "macos", - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - target_os = "android", + windows_platform, + macos_platform, + x11_platform, + wayland_platform, + android_platform ))] fn main() { use std::{thread::sleep, time::Duration}; @@ -60,7 +57,7 @@ fn main() { } } -#[cfg(any(target_os = "ios", target_arch = "wasm32"))] +#[cfg(any(ios_platform, wasm_platform))] fn main() { println!("This platform doesn't support run_return."); } diff --git a/src/event_loop.rs b/src/event_loop.rs index 92861cad..6b0df65b 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -102,11 +102,11 @@ impl EventLoopBuilder { /// /// [`platform`]: crate::platform #[cfg_attr( - target_os = "android", + android, doc = "[`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app" )] #[cfg_attr( - not(target_os = "android"), + not(android), doc = "[`.with_android_app(app)`]: #only-available-on-android" )] #[inline] @@ -339,14 +339,7 @@ impl EventLoopWindowTarget { /// /// [`DeviceEvent`]: crate::event::DeviceEvent pub fn set_device_event_filter(&self, _filter: DeviceEventFilter) { - #[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - target_os = "windows" - ))] + #[cfg(any(x11_platform, wayland_platform, windows))] self.p.set_device_event_filter(_filter); } } diff --git a/src/platform/mod.rs b/src/platform/mod.rs index f819414e..8ca16032 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -15,47 +15,26 @@ //! //! However only the module corresponding to the platform you're compiling to will be available. -#[cfg(target_os = "android")] +#[cfg(android_platform)] pub mod android; -#[cfg(target_os = "ios")] +#[cfg(ios_platform)] pub mod ios; -#[cfg(target_os = "macos")] +#[cfg(macos_platform)] pub mod macos; -#[cfg(all( - feature = "wayland", - any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ) -))] +#[cfg(wayland_platform)] pub mod wayland; -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] pub mod web; -#[cfg(target_os = "windows")] +#[cfg(windows_platform)] pub mod windows; -#[cfg(all( - feature = "x11", - any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd", - ) -))] +#[cfg(x11_platform)] pub mod x11; #[cfg(any( - target_os = "windows", - target_os = "macos", - target_os = "android", - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" + windows_platform, + macos_platform, + android_platform, + x11_platform, + wayland_platform ))] pub mod run_return; diff --git a/src/platform/wayland.rs b/src/platform/wayland.rs index 313b4d00..7bbbd991 100644 --- a/src/platform/wayland.rs +++ b/src/platform/wayland.rs @@ -41,7 +41,7 @@ impl EventLoopWindowTargetExtWayland for EventLoopWindowTarget { LinuxEventLoopWindowTarget::Wayland(ref p) => { Some(p.display().get_display_ptr() as *mut _) } - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => None, } } @@ -95,7 +95,7 @@ impl WindowExtWayland for Window { fn wayland_surface(&self) -> Option<*mut raw::c_void> { match self.window { LinuxWindow::Wayland(ref w) => Some(w.surface().as_ref().c_ptr() as *mut _), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => None, } } @@ -104,7 +104,7 @@ impl WindowExtWayland for Window { fn wayland_display(&self) -> Option<*mut raw::c_void> { match self.window { LinuxWindow::Wayland(ref w) => Some(w.display().get_display_ptr() as *mut _), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => None, } } diff --git a/src/platform/x11.rs b/src/platform/x11.rs index 7fabe656..245902f7 100644 --- a/src/platform/x11.rs +++ b/src/platform/x11.rs @@ -60,7 +60,7 @@ impl EventLoopWindowTargetExtX11 for EventLoopWindowTarget { fn xlib_xconnection(&self) -> Option> { match self.p { LinuxEventLoopWindowTarget::X(ref e) => Some(e.x_connection().clone()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } @@ -124,7 +124,7 @@ impl WindowExtX11 for Window { fn xlib_window(&self) -> Option { match self.window { LinuxWindow::X(ref w) => Some(w.xlib_window()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } @@ -133,7 +133,7 @@ impl WindowExtX11 for Window { fn xlib_display(&self) -> Option<*mut raw::c_void> { match self.window { LinuxWindow::X(ref w) => Some(w.xlib_display()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } @@ -142,7 +142,7 @@ impl WindowExtX11 for Window { fn xlib_screen_id(&self) -> Option { match self.window { LinuxWindow::X(ref w) => Some(w.xlib_screen_id()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } @@ -151,7 +151,7 @@ impl WindowExtX11 for Window { fn xlib_xconnection(&self) -> Option> { match self.window { LinuxWindow::X(ref w) => Some(w.xlib_xconnection()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } @@ -160,7 +160,7 @@ impl WindowExtX11 for Window { fn xcb_connection(&self) -> Option<*mut raw::c_void> { match self.window { LinuxWindow::X(ref w) => Some(w.xcb_connection()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => None, } } diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 68e4cf66..4e903a96 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -1,4 +1,4 @@ -#![cfg(target_os = "android")] +#![cfg(android_platform)] use std::{ collections::VecDeque, diff --git a/src/platform_impl/ios/mod.rs b/src/platform_impl/ios/mod.rs index 18362b69..be5e6fad 100644 --- a/src/platform_impl/ios/mod.rs +++ b/src/platform_impl/ios/mod.rs @@ -55,7 +55,7 @@ //! //! Also note that app may not receive the LoopDestroyed event if suspended; it might be SIGKILL'ed. -#![cfg(target_os = "ios")] +#![cfg(ios_platform)] #![allow(clippy::let_unit_value)] // TODO: (mtak-) UIKit requires main thread for virtually all function/method calls. This could be diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 4677b8cd..a1a0da16 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -1,19 +1,13 @@ -#![cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#![cfg(free_unix)] -#[cfg(all(not(feature = "x11"), not(feature = "wayland")))] +#[cfg(all(not(x11_platform), not(wayland_platform)))] compile_error!("Please select a feature to build for unix: `x11`, `wayland`"); -#[cfg(feature = "wayland")] +#[cfg(wayland_platform)] use std::error::Error; use std::{collections::VecDeque, env, fmt}; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] use std::{ ffi::CStr, mem::MaybeUninit, @@ -21,15 +15,15 @@ use std::{ sync::{Arc, Mutex}, }; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] use once_cell::sync::Lazy; use raw_window_handle::{RawDisplayHandle, RawWindowHandle}; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] pub use self::x11::XNotSupported; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError}; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] use crate::platform::x11::XlibErrorHook; use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, @@ -48,9 +42,9 @@ use crate::{ pub(crate) use crate::icon::RgbaIcon as PlatformIcon; pub(self) use crate::platform_impl::Fullscreen; -#[cfg(feature = "wayland")] +#[cfg(wayland_platform)] pub mod wayland; -#[cfg(feature = "x11")] +#[cfg(x11_platform)] pub mod x11; /// Environment variable specifying which backend should be used on unix platform. @@ -64,9 +58,9 @@ const BACKEND_PREFERENCE_ENV_VAR: &str = "WINIT_UNIX_BACKEND"; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub(crate) enum Backend { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X, - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland, } @@ -91,15 +85,15 @@ impl ApplicationName { #[derive(Clone)] pub struct PlatformSpecificWindowBuilderAttributes { pub name: Option, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] pub visual_infos: Option, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] pub screen_id: Option, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] pub base_size: Option, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] pub override_redirect: bool, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] pub x11_window_types: Vec, } @@ -107,51 +101,51 @@ impl Default for PlatformSpecificWindowBuilderAttributes { fn default() -> Self { Self { name: None, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] visual_infos: None, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] screen_id: None, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] base_size: None, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] override_redirect: false, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] x11_window_types: vec![XWindowType::Normal], } } } -#[cfg(feature = "x11")] +#[cfg(x11_platform)] pub static X11_BACKEND: Lazy, XNotSupported>>> = Lazy::new(|| Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new))); #[derive(Debug, Clone)] pub enum OsError { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] XError(XError), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] XMisc(&'static str), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] WaylandMisc(&'static str), } impl fmt::Display for OsError { fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match *self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] OsError::XError(ref e) => _f.pad(&e.description), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] OsError::XMisc(e) => _f.pad(e), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] OsError::WaylandMisc(e) => _f.pad(e), } } } pub enum Window { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::Window), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::Window), } @@ -178,26 +172,26 @@ impl WindowId { #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum DeviceId { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::DeviceId), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::DeviceId), } impl DeviceId { pub const unsafe fn dummy() -> Self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] return DeviceId::Wayland(wayland::DeviceId::dummy()); - #[cfg(all(not(feature = "wayland"), feature = "x11"))] + #[cfg(all(not(wayland_platform), x11_platform))] return DeviceId::X(x11::DeviceId::dummy()); } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] pub enum MonitorHandle { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::MonitorHandle), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::MonitorHandle), } @@ -213,17 +207,17 @@ pub enum MonitorHandle { macro_rules! x11_or_wayland { (match $what:expr; $enum:ident ( $($c1:tt)* ) => $x:expr; as $enum2:ident ) => { match $what { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] $enum::X($($c1)*) => $enum2::X($x), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] $enum::Wayland($($c1)*) => $enum2::Wayland($x), } }; (match $what:expr; $enum:ident ( $($c1:tt)* ) => $x:expr) => { match $what { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] $enum::X($($c1)*) => $x, - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] $enum::Wayland($($c1)*) => $x, } }; @@ -268,9 +262,9 @@ impl MonitorHandle { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum VideoMode { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::VideoMode), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::VideoMode), } @@ -304,11 +298,11 @@ impl Window { pl_attribs: PlatformSpecificWindowBuilderAttributes, ) -> Result { match *window_target { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] EventLoopWindowTarget::Wayland(ref window_target) => { wayland::Window::new(window_target, attribs, pl_attribs).map(Window::Wayland) } - #[cfg(feature = "x11")] + #[cfg(x11_platform)] EventLoopWindowTarget::X(ref window_target) => { x11::Window::new(window_target, attribs, pl_attribs).map(Window::X) } @@ -318,9 +312,9 @@ impl Window { #[inline] pub fn id(&self) -> WindowId { match self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Self::Wayland(window) => window.id(), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Self::X(window) => window.id(), } } @@ -483,9 +477,9 @@ impl Window { #[inline] pub fn set_window_level(&self, _level: WindowLevel) { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref w) => w.set_window_level(_level), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(_) => (), } } @@ -493,9 +487,9 @@ impl Window { #[inline] pub fn set_window_icon(&self, _window_icon: Option) { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref w) => w.set_window_icon(_window_icon), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(_) => (), } } @@ -513,17 +507,17 @@ impl Window { #[inline] pub fn focus_window(&self) { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref w) => w.focus_window(), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(_) => (), } } pub fn request_user_attention(&self, request_type: Option) { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref w) => w.request_user_attention(request_type), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(ref w) => w.request_user_attention(request_type), } } @@ -536,12 +530,12 @@ impl Window { #[inline] pub fn current_monitor(&self) -> Option { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref window) => { let current_monitor = MonitorHandle::X(window.current_monitor()); Some(current_monitor) } - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(ref window) => { let current_monitor = MonitorHandle::Wayland(window.current_monitor()?); Some(current_monitor) @@ -552,13 +546,13 @@ impl Window { #[inline] pub fn available_monitors(&self) -> VecDeque { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref window) => window .available_monitors() .into_iter() .map(MonitorHandle::X) .collect(), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(ref window) => window .available_monitors() .into_iter() @@ -570,12 +564,12 @@ impl Window { #[inline] pub fn primary_monitor(&self) -> Option { match self { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] Window::X(ref window) => { let primary_monitor = MonitorHandle::X(window.primary_monitor()); Some(primary_monitor) } - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Window::Wayland(ref window) => window.primary_monitor(), } } @@ -607,11 +601,11 @@ impl Window { } /// Hooks for X11 errors. -#[cfg(feature = "x11")] +#[cfg(x11_platform)] pub(crate) static mut XLIB_ERROR_HOOKS: Lazy>> = Lazy::new(|| Mutex::new(Vec::new())); -#[cfg(feature = "x11")] +#[cfg(x11_platform)] unsafe extern "C" fn x_error_callback( display: *mut x11::ffi::Display, event: *mut x11::ffi::XErrorEvent, @@ -654,16 +648,16 @@ unsafe extern "C" fn x_error_callback( } pub enum EventLoop { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(Box>), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::EventLoop), } pub enum EventLoopProxy { - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::EventLoopProxy), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::EventLoopProxy), } @@ -684,13 +678,13 @@ impl EventLoop { ); } - #[cfg(feature = "x11")] + #[cfg(x11_platform)] if attributes.forced_backend == Some(Backend::X) { // TODO: Propagate return EventLoop::new_x11_any_thread().unwrap(); } - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] if attributes.forced_backend == Some(Backend::Wayland) { // TODO: Propagate return EventLoop::new_wayland_any_thread().expect("failed to open Wayland connection"); @@ -700,17 +694,17 @@ impl EventLoop { match env_var.as_str() { "x11" => { // TODO: propagate - #[cfg(feature = "x11")] + #[cfg(x11_platform)] return EventLoop::new_x11_any_thread() .expect("Failed to initialize X11 backend"); - #[cfg(not(feature = "x11"))] + #[cfg(not(x11_platform))] panic!("x11 feature is not enabled") } "wayland" => { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] return EventLoop::new_wayland_any_thread() .expect("Failed to initialize Wayland backend"); - #[cfg(not(feature = "wayland"))] + #[cfg(not(wayland_platform))] panic!("wayland feature is not enabled"); } _ => panic!( @@ -720,21 +714,21 @@ impl EventLoop { } } - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] let wayland_err = match EventLoop::new_wayland_any_thread() { Ok(event_loop) => return event_loop, Err(err) => err, }; - #[cfg(feature = "x11")] + #[cfg(x11_platform)] let x11_err = match EventLoop::new_x11_any_thread() { Ok(event_loop) => return event_loop, Err(err) => err, }; - #[cfg(not(feature = "wayland"))] + #[cfg(not(wayland_platform))] let wayland_err = "backend disabled"; - #[cfg(not(feature = "x11"))] + #[cfg(not(x11_platform))] let x11_err = "backend disabled"; panic!( @@ -743,12 +737,12 @@ impl EventLoop { ); } - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] fn new_wayland_any_thread() -> Result, Box> { wayland::EventLoop::new().map(|evlp| EventLoop::Wayland(Box::new(evlp))) } - #[cfg(feature = "x11")] + #[cfg(x11_platform)] fn new_x11_any_thread() -> Result, XNotSupported> { let xconn = match X11_BACKEND.lock().unwrap().as_ref() { Ok(xconn) => xconn.clone(), @@ -788,9 +782,9 @@ impl EventLoopProxy { } pub enum EventLoopWindowTarget { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] Wayland(wayland::EventLoopWindowTarget), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] X(x11::EventLoopWindowTarget), } @@ -798,9 +792,9 @@ impl EventLoopWindowTarget { #[inline] pub fn is_wayland(&self) -> bool { match *self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] EventLoopWindowTarget::Wayland(_) => true, - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => false, } } @@ -808,13 +802,13 @@ impl EventLoopWindowTarget { #[inline] pub fn available_monitors(&self) -> VecDeque { match *self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] EventLoopWindowTarget::Wayland(ref evlp) => evlp .available_monitors() .into_iter() .map(MonitorHandle::Wayland) .collect(), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] EventLoopWindowTarget::X(ref evlp) => evlp .x_connection() .available_monitors() @@ -827,9 +821,9 @@ impl EventLoopWindowTarget { #[inline] pub fn primary_monitor(&self) -> Option { match *self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] EventLoopWindowTarget::Wayland(ref evlp) => evlp.primary_monitor(), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] EventLoopWindowTarget::X(ref evlp) => { let primary_monitor = MonitorHandle::X(evlp.x_connection().primary_monitor()); Some(primary_monitor) @@ -840,9 +834,9 @@ impl EventLoopWindowTarget { #[inline] pub fn set_device_event_filter(&self, _filter: DeviceEventFilter) { match *self { - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] EventLoopWindowTarget::Wayland(_) => (), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] EventLoopWindowTarget::X(ref evlp) => evlp.set_device_event_filter(_filter), } } diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 60f951fc..e63848b0 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -268,7 +268,7 @@ impl EventLoop { PlatformEventLoopWindowTarget::Wayland(window_target) => { window_target.state.get_mut() } - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => unreachable!(), }; @@ -561,7 +561,7 @@ impl EventLoop { fn with_state U>(&mut self, f: F) -> U { let state = match &mut self.window_target.p { PlatformEventLoopWindowTarget::Wayland(window_target) => window_target.state.get_mut(), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => unreachable!(), }; @@ -571,7 +571,7 @@ impl EventLoop { fn loop_dispatch>>(&mut self, timeout: D) -> IOResult<()> { let state = match &mut self.window_target.p { PlatformEventLoopWindowTarget::Wayland(window_target) => window_target.state.get_mut(), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] _ => unreachable!(), }; diff --git a/src/platform_impl/linux/wayland/mod.rs b/src/platform_impl/linux/wayland/mod.rs index 9871b2f2..19bf6c92 100644 --- a/src/platform_impl/linux/wayland/mod.rs +++ b/src/platform_impl/linux/wayland/mod.rs @@ -1,10 +1,4 @@ -#![cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#![cfg(wayland_platform)] use sctk::reexports::client::protocol::wl_surface::WlSurface; diff --git a/src/platform_impl/linux/wayland/window/mod.rs b/src/platform_impl/linux/wayland/window/mod.rs index 7b64948b..9ddfd9e9 100644 --- a/src/platform_impl/linux/wayland/window/mod.rs +++ b/src/platform_impl/linux/wayland/window/mod.rs @@ -219,7 +219,7 @@ impl Window { Some(Fullscreen::Borderless(monitor)) => { let monitor = monitor.and_then(|monitor| match monitor { PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] PlatformMonitorHandle::X(_) => None, }); @@ -490,7 +490,7 @@ impl Window { Some(Fullscreen::Borderless(monitor)) => { let monitor = monitor.and_then(|monitor| match monitor { PlatformMonitorHandle::Wayland(monitor) => Some(monitor.proxy), - #[cfg(feature = "x11")] + #[cfg(x11_platform)] PlatformMonitorHandle::X(_) => None, }); diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 92384b6f..b52890a3 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -1,10 +1,4 @@ -#![cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#![cfg(x11_platform)] mod dnd; mod event_processor; @@ -536,7 +530,7 @@ impl EventLoop { pub(crate) fn get_xtarget(target: &RootELW) -> &EventLoopWindowTarget { match target.p { super::EventLoopWindowTarget::X(ref target) => target, - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => unreachable!(), } } diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 981d56cf..d21dacf7 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -697,7 +697,7 @@ impl UnownedWindow { (None, monitor) } Fullscreen::Borderless(None) => (None, self.current_monitor()), - #[cfg(feature = "wayland")] + #[cfg(wayland_platform)] _ => unreachable!(), }; diff --git a/src/platform_impl/mod.rs b/src/platform_impl/mod.rs index 4ae9a243..a81b4403 100644 --- a/src/platform_impl/mod.rs +++ b/src/platform_impl/mod.rs @@ -1,28 +1,22 @@ use crate::monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode}; use crate::window::Fullscreen as RootFullscreen; -#[cfg(target_os = "windows")] +#[cfg(windows_platform)] #[path = "windows/mod.rs"] mod platform; -#[cfg(any( - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] +#[cfg(any(x11_platform, wayland_platform))] #[path = "linux/mod.rs"] mod platform; -#[cfg(target_os = "macos")] +#[cfg(macos_platform)] #[path = "macos/mod.rs"] mod platform; -#[cfg(target_os = "android")] +#[cfg(android_platform)] #[path = "android/mod.rs"] mod platform; -#[cfg(target_os = "ios")] +#[cfg(ios_platform)] #[path = "ios/mod.rs"] mod platform; -#[cfg(target_arch = "wasm32")] +#[cfg(wasm_platform)] #[path = "web/mod.rs"] mod platform; @@ -58,15 +52,12 @@ impl From for RootFullscreen { } #[cfg(all( - not(target_os = "ios"), - not(target_os = "windows"), - not(target_os = "linux"), - not(target_os = "macos"), - not(target_os = "android"), - not(target_os = "dragonfly"), - not(target_os = "freebsd"), - not(target_os = "netbsd"), - not(target_os = "openbsd"), - not(target_arch = "wasm32"), + not(ios_platform), + not(windows_platform), + not(macos_platform), + not(android_platform), + not(x11_platform), + not(wayland_platform), + not(wasm_platform), ))] compile_error!("The platform you're compiling for is not supported by winit"); diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 5ae072a5..9c58d1c4 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -1,4 +1,4 @@ -#![cfg(target_os = "windows")] +#![cfg(windows_platform)] use windows_sys::Win32::{ Foundation::{HANDLE, HWND}, diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 4d6eaacc..a93d97be 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -1,4 +1,4 @@ -#![cfg(target_os = "windows")] +#![cfg(windows_platform)] use raw_window_handle::{ RawDisplayHandle, RawWindowHandle, Win32WindowHandle, WindowsDisplayHandle, diff --git a/src/window.rs b/src/window.rs index 4405df50..b646ddd0 100644 --- a/src/window.rs +++ b/src/window.rs @@ -1056,7 +1056,7 @@ impl Window { /// /// [`NSWindowSharingNone`]: https://developer.apple.com/documentation/appkit/nswindowsharingtype/nswindowsharingnone pub fn set_content_protected(&self, _protected: bool) { - #[cfg(any(target_os = "macos", target_os = "windows"))] + #[cfg(any(macos_platform, windows_platform))] self.window.set_content_protected(_protected); } diff --git a/tests/send_objects.rs b/tests/send_objects.rs index 252b271a..15fd8793 100644 --- a/tests/send_objects.rs +++ b/tests/send_objects.rs @@ -1,7 +1,7 @@ #[allow(dead_code)] fn needs_send() {} -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] #[test] fn event_loop_proxy_send() { #[allow(dead_code)] @@ -11,7 +11,7 @@ fn event_loop_proxy_send() { } } -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] #[test] fn window_send() { // ensures that `winit::Window` implements `Send` diff --git a/tests/sync_object.rs b/tests/sync_object.rs index 56524cb2..80be1773 100644 --- a/tests/sync_object.rs +++ b/tests/sync_object.rs @@ -1,7 +1,7 @@ #[allow(dead_code)] fn needs_sync() {} -#[cfg(not(target_arch = "wasm32"))] +#[cfg(not(wasm_platform))] #[test] fn window_sync() { // ensures that `winit::Window` implements `Sync`