Use cfg aliases throught the code base

Co-authored-by: Mads Marquart <mads@marquart.dk>
This commit is contained in:
Amr Bashir 2022-12-25 09:57:27 +02:00 committed by GitHub
parent 58ec458877
commit 5e77d70245
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 185 additions and 227 deletions

View file

@ -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<ApplicationName>,
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
pub visual_infos: Option<XVisualInfo>,
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
pub screen_id: Option<i32>,
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
pub base_size: Option<Size>,
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
pub override_redirect: bool,
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
pub x11_window_types: Vec<XWindowType>,
}
@ -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<Mutex<Result<Arc<XConnection>, 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<Self, RootOsError> {
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<Icon>) {
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<UserAttentionType>) {
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<MonitorHandle> {
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<MonitorHandle> {
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<MonitorHandle> {
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<Mutex<Vec<XlibErrorHook>>> =
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<T: 'static> {
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
Wayland(Box<wayland::EventLoop<T>>),
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
X(x11::EventLoop<T>),
}
pub enum EventLoopProxy<T: 'static> {
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
X(x11::EventLoopProxy<T>),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
Wayland(wayland::EventLoopProxy<T>),
}
@ -684,13 +678,13 @@ impl<T: 'static> EventLoop<T> {
);
}
#[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<T: 'static> EventLoop<T> {
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<T: 'static> EventLoop<T> {
}
}
#[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<T: 'static> EventLoop<T> {
);
}
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
fn new_wayland_any_thread() -> Result<EventLoop<T>, Box<dyn Error>> {
wayland::EventLoop::new().map(|evlp| EventLoop::Wayland(Box::new(evlp)))
}
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
fn new_x11_any_thread() -> Result<EventLoop<T>, XNotSupported> {
let xconn = match X11_BACKEND.lock().unwrap().as_ref() {
Ok(xconn) => xconn.clone(),
@ -788,9 +782,9 @@ impl<T: 'static> EventLoopProxy<T> {
}
pub enum EventLoopWindowTarget<T> {
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
Wayland(wayland::EventLoopWindowTarget<T>),
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
X(x11::EventLoopWindowTarget<T>),
}
@ -798,9 +792,9 @@ impl<T> EventLoopWindowTarget<T> {
#[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<T> EventLoopWindowTarget<T> {
#[inline]
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
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<T> EventLoopWindowTarget<T> {
#[inline]
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
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<T> EventLoopWindowTarget<T> {
#[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),
}
}

View file

@ -268,7 +268,7 @@ impl<T: 'static> EventLoop<T> {
PlatformEventLoopWindowTarget::Wayland(window_target) => {
window_target.state.get_mut()
}
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
_ => unreachable!(),
};
@ -561,7 +561,7 @@ impl<T: 'static> EventLoop<T> {
fn with_state<U, F: FnOnce(&mut WinitState) -> 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<T: 'static> EventLoop<T> {
fn loop_dispatch<D: Into<Option<std::time::Duration>>>(&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!(),
};

View file

@ -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;

View file

@ -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,
});

View file

@ -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<T: 'static> EventLoop<T> {
pub(crate) fn get_xtarget<T>(target: &RootELW<T>) -> &EventLoopWindowTarget<T> {
match target.p {
super::EventLoopWindowTarget::X(ref target) => target,
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => unreachable!(),
}
}

View file

@ -697,7 +697,7 @@ impl UnownedWindow {
(None, monitor)
}
Fullscreen::Borderless(None) => (None, self.current_monitor()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => unreachable!(),
};