2020-06-15 17:15:27 +10:00
|
|
|
#![cfg(any(
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "netbsd",
|
|
|
|
target_os = "openbsd"
|
|
|
|
))]
|
|
|
|
|
|
|
|
#[cfg(all(not(feature = "x11"), not(feature = "wayland")))]
|
|
|
|
compile_error!("Please select a feature to build for unix: `x11`, `wayland`");
|
|
|
|
|
|
|
|
use std::{collections::VecDeque, env, fmt};
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Arc};
|
|
|
|
|
|
|
|
#[cfg(feature = "x11")]
|
2018-06-18 10:44:38 +10:00
|
|
|
use parking_lot::Mutex;
|
2019-08-14 21:57:16 +10:00
|
|
|
use raw_window_handle::RawWindowHandle;
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-06-18 04:27:00 +10:00
|
|
|
use smithay_client_toolkit::reexports::client::ConnectError;
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
pub use self::x11::XNotSupported;
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2020-01-04 17:31:23 +11:00
|
|
|
use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError};
|
2019-06-22 01:33:15 +10:00
|
|
|
use crate::{
|
2020-01-04 17:31:23 +11:00
|
|
|
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
|
2019-06-22 01:33:15 +10:00
|
|
|
error::{ExternalError, NotSupportedError, OsError as RootOsError},
|
|
|
|
event::Event,
|
|
|
|
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
|
|
|
|
icon::Icon,
|
2019-07-30 04:16:14 +10:00
|
|
|
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
|
|
|
|
window::{CursorIcon, Fullscreen, WindowAttributes},
|
2019-06-22 01:33:15 +10:00
|
|
|
};
|
2017-09-01 19:04:57 +10:00
|
|
|
|
2020-03-08 06:42:21 +11:00
|
|
|
pub(crate) use crate::icon::RgbaIcon as PlatformIcon;
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2017-03-04 06:56:27 +11:00
|
|
|
pub mod wayland;
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
pub mod x11;
|
2017-03-04 06:56:27 +11:00
|
|
|
|
2017-06-22 05:10:23 +10:00
|
|
|
/// Environment variable specifying which backend should be used on unix platform.
|
|
|
|
///
|
|
|
|
/// Legal values are x11 and wayland. If this variable is set only the named backend
|
|
|
|
/// will be tried by winit. If it is not set, winit will try to connect to a wayland connection,
|
|
|
|
/// and if it fails will fallback on x11.
|
|
|
|
///
|
|
|
|
/// If this variable is set with any other value, winit will panic.
|
2017-06-22 04:54:21 +10:00
|
|
|
const BACKEND_PREFERENCE_ENV_VAR: &str = "WINIT_UNIX_BACKEND";
|
2017-06-22 03:34:16 +10:00
|
|
|
|
2019-09-24 00:10:33 +10:00
|
|
|
#[derive(Clone)]
|
2017-01-28 23:03:17 +11:00
|
|
|
pub struct PlatformSpecificWindowBuilderAttributes {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
pub visual_infos: Option<XVisualInfo>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2017-01-28 23:03:17 +11:00
|
|
|
pub screen_id: Option<i32>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2020-01-04 17:14:11 +11:00
|
|
|
pub resize_increments: Option<Size>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2020-01-04 17:14:11 +11:00
|
|
|
pub base_size: Option<Size>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2018-05-21 00:47:22 +10:00
|
|
|
pub class: Option<(String, String)>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2018-05-21 00:47:22 +10:00
|
|
|
pub override_redirect: bool,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
pub x11_window_types: Vec<XWindowType>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2018-09-21 07:00:04 +10:00
|
|
|
pub gtk_theme_variant: Option<String>,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-06-22 01:33:15 +10:00
|
|
|
pub app_id: Option<String>,
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2019-09-24 00:10:33 +10:00
|
|
|
impl Default for PlatformSpecificWindowBuilderAttributes {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
visual_infos: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
screen_id: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
resize_increments: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
base_size: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
class: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
override_redirect: false,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
x11_window_types: vec![XWindowType::Normal],
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-09-24 00:10:33 +10:00
|
|
|
gtk_theme_variant: None,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-09-24 00:10:33 +10:00
|
|
|
app_id: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-06-22 01:33:15 +10:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref X11_BACKEND: Mutex<Result<Arc<XConnection>, XNotSupported>> =
|
2020-04-11 04:29:33 +10:00
|
|
|
Mutex::new(XConnection::new(Some(x_error_callback)).map(Arc::new));
|
2019-06-22 01:33:15 +10:00
|
|
|
}
|
2017-01-28 23:03:17 +11:00
|
|
|
|
2019-05-30 11:29:54 +10:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum OsError {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-05-30 11:29:54 +10:00
|
|
|
XError(XError),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-05-30 11:29:54 +10:00
|
|
|
XMisc(&'static str),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for OsError {
|
2020-06-15 17:15:27 +10:00
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
|
|
|
match *self {
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
OsError::XError(ref e) => _f.pad(&e.description),
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
OsError::XMisc(ref e) => _f.pad(e),
|
2019-05-30 11:29:54 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
pub enum Window {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
X(x11::Window),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2018-06-15 09:42:18 +10:00
|
|
|
Wayland(wayland::Window),
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2017-03-04 07:41:51 +11:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum WindowId {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
X(x11::WindowId),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2018-06-15 09:42:18 +10:00
|
|
|
Wayland(wayland::WindowId),
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2018-12-22 03:51:48 +11:00
|
|
|
impl WindowId {
|
|
|
|
pub unsafe fn dummy() -> Self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
return WindowId::Wayland(wayland::WindowId::dummy());
|
|
|
|
#[cfg(all(not(feature = "wayland"), feature = "x11"))]
|
|
|
|
return WindowId::X(x11::WindowId::dummy());
|
2018-12-22 03:51:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 06:52:35 +10:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub enum DeviceId {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
X(x11::DeviceId),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2018-06-15 09:42:18 +10:00
|
|
|
Wayland(wayland::DeviceId),
|
2017-04-23 06:52:35 +10:00
|
|
|
}
|
|
|
|
|
2018-12-22 03:51:48 +11:00
|
|
|
impl DeviceId {
|
|
|
|
pub unsafe fn dummy() -> Self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
return DeviceId::Wayland(wayland::DeviceId::dummy());
|
|
|
|
#[cfg(all(not(feature = "wayland"), feature = "x11"))]
|
|
|
|
return DeviceId::X(x11::DeviceId::dummy());
|
2018-12-22 03:51:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 04:16:14 +10:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
2019-02-06 02:30:33 +11:00
|
|
|
pub enum MonitorHandle {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
X(x11::MonitorHandle),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-06 02:30:33 +11:00
|
|
|
Wayland(wayland::MonitorHandle),
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
/// `x11_or_wayland!(match expr; Enum(foo) => foo.something())`
|
|
|
|
/// expands to the equivalent of
|
|
|
|
/// ```ignore
|
|
|
|
/// match self {
|
|
|
|
/// Enum::X(foo) => foo.something(),
|
|
|
|
/// Enum::Wayland(foo) => foo.something(),
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// The result can be converted to another enum by adding `; as AnotherEnum`
|
|
|
|
macro_rules! x11_or_wayland {
|
|
|
|
(match $what:expr; $enum:ident ( $($c1:tt)* ) => $x:expr; as $enum2:ident ) => {
|
|
|
|
match $what {
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
$enum::X($($c1)*) => $enum2::X($x),
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
$enum::Wayland($($c1)*) => $enum2::Wayland($x),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(match $what:expr; $enum:ident ( $($c1:tt)* ) => $x:expr) => {
|
|
|
|
match $what {
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
$enum::X($($c1)*) => $x,
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
$enum::Wayland($($c1)*) => $x,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
impl MonitorHandle {
|
2017-01-28 23:03:17 +11:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn name(&self) -> Option<String> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => m.name())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn native_identifier(&self) -> u32 {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => m.native_identifier())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => m.size())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
2017-09-07 18:33:46 +10:00
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn position(&self) -> PhysicalPosition<i32> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => m.position())
|
2017-09-07 18:33:46 +10:00
|
|
|
}
|
2017-10-17 22:56:38 +11:00
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 06:52:27 +11:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => m.scale_factor() as f64)
|
2017-10-17 22:56:38 +11:00
|
|
|
}
|
2019-06-13 04:07:25 +10:00
|
|
|
|
|
|
|
#[inline]
|
2019-07-30 04:16:14 +10:00
|
|
|
pub fn video_modes(&self) -> Box<dyn Iterator<Item = RootVideoMode>> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; MonitorHandle(m) => Box::new(m.video_modes()))
|
2019-06-13 04:07:25 +10:00
|
|
|
}
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2019-07-30 04:16:14 +10:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum VideoMode {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-07-30 04:16:14 +10:00
|
|
|
X(x11::VideoMode),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-07-30 04:16:14 +10:00
|
|
|
Wayland(wayland::VideoMode),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VideoMode {
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; VideoMode(m) => m.size())
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn bit_depth(&self) -> u16 {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; VideoMode(m) => m.bit_depth())
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn refresh_rate(&self) -> u16 {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; VideoMode(m) => m.refresh_rate())
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn monitor(&self) -> RootMonitorHandle {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; VideoMode(m) => m.monitor())
|
2019-07-30 04:16:14 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:24 +10:00
|
|
|
impl Window {
|
2017-01-28 23:03:17 +11:00
|
|
|
#[inline]
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn new<T>(
|
|
|
|
window_target: &EventLoopWindowTarget<T>,
|
2018-05-08 07:36:21 +10:00
|
|
|
attribs: WindowAttributes,
|
|
|
|
pl_attribs: PlatformSpecificWindowBuilderAttributes,
|
2019-05-30 11:29:54 +10:00
|
|
|
) -> Result<Self, RootOsError> {
|
2019-02-21 20:51:43 +11:00
|
|
|
match *window_target {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-21 20:51:43 +11:00
|
|
|
EventLoopWindowTarget::Wayland(ref window_target) => {
|
|
|
|
wayland::Window::new(window_target, attribs, pl_attribs).map(Window::Wayland)
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
EventLoopWindowTarget::X(ref window_target) => {
|
|
|
|
x11::Window::new(window_target, attribs, pl_attribs).map(Window::X)
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 07:41:51 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn id(&self) -> WindowId {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.id(); as WindowId)
|
2017-03-04 07:41:51 +11:00
|
|
|
}
|
|
|
|
|
2017-01-28 23:03:17 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn set_title(&self, title: &str) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_title(title));
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn set_visible(&self, visible: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_visible(visible))
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.outer_position())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.inner_position())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2018-04-17 11:40:30 +10:00
|
|
|
#[inline]
|
2020-01-04 17:31:23 +11:00
|
|
|
pub fn set_outer_position(&self, position: Position) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_outer_position(position))
|
2018-04-17 11:40:30 +10:00
|
|
|
}
|
|
|
|
|
2017-01-28 23:03:17 +11:00
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn inner_size(&self) -> PhysicalSize<u32> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.inner_size())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:33:07 +11:00
|
|
|
pub fn outer_size(&self) -> PhysicalSize<u32> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.outer_size())
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:31:23 +11:00
|
|
|
pub fn set_inner_size(&self, size: Size) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_inner_size(size))
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:35:35 +11:00
|
|
|
#[inline]
|
2020-01-04 17:31:23 +11:00
|
|
|
pub fn set_min_inner_size(&self, dimensions: Option<Size>) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_min_inner_size(dimensions))
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:31:23 +11:00
|
|
|
pub fn set_max_inner_size(&self, dimensions: Option<Size>) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_max_inner_size(dimensions))
|
2018-03-23 20:35:35 +11:00
|
|
|
}
|
2018-06-15 09:42:18 +10:00
|
|
|
|
2018-06-12 08:47:50 +10:00
|
|
|
#[inline]
|
|
|
|
pub fn set_resizable(&self, resizable: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_resizable(resizable))
|
2018-06-12 08:47:50 +10:00
|
|
|
}
|
2018-03-23 20:35:35 +11:00
|
|
|
|
2017-01-28 23:03:17 +11:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn set_cursor_icon(&self, cursor: CursorIcon) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_cursor_icon(cursor))
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn set_cursor_grab(&self, grab: bool) -> Result<(), ExternalError> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(window) => window.set_cursor_grab(grab))
|
2018-06-19 02:32:18 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn set_cursor_visible(&self, visible: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(window) => window.set_cursor_visible(visible))
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 06:52:27 +11:00
|
|
|
pub fn scale_factor(&self) -> f64 {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.scale_factor() as f64)
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 17:31:23 +11:00
|
|
|
pub fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_cursor_position(position))
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
2017-08-28 09:19:26 +10:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_maximized(&self, maximized: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_maximized(maximized))
|
2017-08-28 09:19:26 +10:00
|
|
|
}
|
2017-08-28 09:22:26 +10:00
|
|
|
|
2019-12-22 17:04:11 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn set_minimized(&self, minimized: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_minimized(minimized))
|
2019-12-22 17:04:11 +11:00
|
|
|
}
|
|
|
|
|
2019-04-26 03:09:32 +10:00
|
|
|
#[inline]
|
2019-07-30 04:16:14 +10:00
|
|
|
pub fn fullscreen(&self) -> Option<Fullscreen> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.fullscreen())
|
2019-04-26 03:09:32 +10:00
|
|
|
}
|
|
|
|
|
2017-08-28 09:22:26 +10:00
|
|
|
#[inline]
|
2019-07-30 04:16:14 +10:00
|
|
|
pub fn set_fullscreen(&self, monitor: Option<Fullscreen>) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_fullscreen(monitor))
|
2017-12-22 23:50:46 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_decorations(&self, decorations: bool) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.set_decorations(decorations))
|
2017-08-28 09:22:26 +10:00
|
|
|
}
|
2017-09-07 18:33:46 +10:00
|
|
|
|
2018-05-21 00:24:05 +10:00
|
|
|
#[inline]
|
2020-06-15 17:15:27 +10:00
|
|
|
pub fn set_always_on_top(&self, _always_on_top: bool) {
|
2018-05-21 00:24:05 +10:00
|
|
|
match self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
&Window::X(ref w) => w.set_always_on_top(_always_on_top),
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
_ => (),
|
2018-05-21 00:24:05 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 07:36:21 +10:00
|
|
|
#[inline]
|
2020-06-15 17:15:27 +10:00
|
|
|
pub fn set_window_icon(&self, _window_icon: Option<Icon>) {
|
2018-05-08 07:36:21 +10:00
|
|
|
match self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
&Window::X(ref w) => w.set_window_icon(_window_icon),
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
_ => (),
|
2018-05-08 07:36:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 11:28:30 +10:00
|
|
|
#[inline]
|
2020-06-15 17:15:27 +10:00
|
|
|
pub fn set_ime_position(&self, _position: Position) {
|
2018-05-18 11:28:30 +10:00
|
|
|
match self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
&Window::X(ref w) => w.set_ime_position(_position),
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
_ => (),
|
2018-05-18 11:28:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
#[inline]
|
|
|
|
pub fn request_redraw(&self) {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(w) => w.request_redraw())
|
2019-02-21 20:51:43 +11:00
|
|
|
}
|
|
|
|
|
2017-09-07 18:33:46 +10:00
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn current_monitor(&self) -> RootMonitorHandle {
|
2020-06-15 17:15:27 +10:00
|
|
|
RootMonitorHandle {
|
|
|
|
inner: x11_or_wayland!(match self; Window(window) => window.current_monitor(); as MonitorHandle),
|
2018-06-17 00:14:12 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
|
2018-06-17 00:14:12 +10:00
|
|
|
match self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-06-25 02:14:55 +10:00
|
|
|
&Window::X(ref window) => window
|
|
|
|
.available_monitors()
|
|
|
|
.into_iter()
|
|
|
|
.map(MonitorHandle::X)
|
|
|
|
.collect(),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-06-25 02:14:55 +10:00
|
|
|
&Window::Wayland(ref window) => window
|
|
|
|
.available_monitors()
|
|
|
|
.into_iter()
|
|
|
|
.map(MonitorHandle::Wayland)
|
|
|
|
.collect(),
|
2018-06-17 00:14:12 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-30 11:29:54 +10:00
|
|
|
pub fn primary_monitor(&self) -> MonitorHandle {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; Window(window) => window.primary_monitor(); as MonitorHandle)
|
2017-09-07 18:33:46 +10:00
|
|
|
}
|
2019-08-14 21:57:16 +10:00
|
|
|
|
|
|
|
pub fn raw_window_handle(&self) -> RawWindowHandle {
|
|
|
|
match self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-10-01 01:17:01 +10:00
|
|
|
&Window::X(ref window) => RawWindowHandle::Xlib(window.raw_window_handle()),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-08-14 21:57:16 +10:00
|
|
|
&Window::Wayland(ref window) => RawWindowHandle::Wayland(window.raw_window_handle()),
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 23:03:17 +11:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
unsafe extern "C" fn x_error_callback(
|
|
|
|
display: *mut x11::ffi::Display,
|
|
|
|
event: *mut x11::ffi::XErrorEvent,
|
|
|
|
) -> c_int {
|
2018-06-18 10:44:38 +10:00
|
|
|
let xconn_lock = X11_BACKEND.lock();
|
|
|
|
if let Ok(ref xconn) = *xconn_lock {
|
2019-07-12 02:34:32 +10:00
|
|
|
// `assume_init` is safe here because the array consists of `MaybeUninit` values,
|
|
|
|
// which do not require initialization.
|
|
|
|
let mut buf: [MaybeUninit<c_char>; 1024] = MaybeUninit::uninit().assume_init();
|
2018-06-18 10:44:38 +10:00
|
|
|
(xconn.xlib.XGetErrorText)(
|
|
|
|
display,
|
|
|
|
(*event).error_code as c_int,
|
2019-07-12 02:34:32 +10:00
|
|
|
buf.as_mut_ptr() as *mut c_char,
|
2018-06-18 10:44:38 +10:00
|
|
|
buf.len() as c_int,
|
|
|
|
);
|
2019-07-12 02:34:32 +10:00
|
|
|
let description = CStr::from_ptr(buf.as_ptr() as *const c_char).to_string_lossy();
|
2018-06-18 10:44:38 +10:00
|
|
|
|
|
|
|
let error = XError {
|
|
|
|
description: description.into_owned(),
|
|
|
|
error_code: (*event).error_code,
|
|
|
|
request_code: (*event).request_code,
|
|
|
|
minor_code: (*event).minor_code,
|
|
|
|
};
|
|
|
|
|
2018-11-18 07:51:39 +11:00
|
|
|
error!("X11 error: {:#?}", error);
|
2018-06-18 10:44:38 +10:00
|
|
|
|
|
|
|
*xconn.latest_error.lock() = Some(error);
|
|
|
|
}
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
// Fun fact: this return value is completely ignored.
|
2017-01-28 23:03:17 +11:00
|
|
|
0
|
|
|
|
}
|
2019-04-28 02:06:51 +10:00
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
pub enum EventLoop<T: 'static> {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-21 20:51:43 +11:00
|
|
|
Wayland(wayland::EventLoop<T>),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-06-22 01:33:15 +10:00
|
|
|
X(x11::EventLoop<T>),
|
2017-03-04 07:41:51 +11:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
pub enum EventLoopProxy<T: 'static> {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
X(x11::EventLoopProxy<T>),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-21 20:51:43 +11:00
|
|
|
Wayland(wayland::EventLoopProxy<T>),
|
2017-05-25 23:19:13 +10:00
|
|
|
}
|
|
|
|
|
2019-08-06 06:51:42 +10:00
|
|
|
impl<T: 'static> Clone for EventLoopProxy<T> {
|
|
|
|
fn clone(&self) -> Self {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoopProxy(proxy) => proxy.clone(); as EventLoopProxy)
|
2019-08-06 06:51:42 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:33:15 +10:00
|
|
|
impl<T: 'static> EventLoop<T> {
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn new() -> EventLoop<T> {
|
2019-10-19 02:51:06 +11:00
|
|
|
assert_is_main_thread("new_any_thread");
|
|
|
|
|
|
|
|
EventLoop::new_any_thread()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_any_thread() -> EventLoop<T> {
|
2017-09-01 19:04:57 +10:00
|
|
|
if let Ok(env_var) = env::var(BACKEND_PREFERENCE_ENV_VAR) {
|
|
|
|
match env_var.as_str() {
|
|
|
|
"x11" => {
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
// TODO: propagate
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-10-19 02:51:06 +11:00
|
|
|
return EventLoop::new_x11_any_thread()
|
|
|
|
.expect("Failed to initialize X11 backend");
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(not(feature = "x11"))]
|
|
|
|
panic!("x11 feature is not enabled")
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2017-09-01 19:04:57 +10:00
|
|
|
"wayland" => {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-10-19 02:51:06 +11:00
|
|
|
return EventLoop::new_wayland_any_thread()
|
|
|
|
.expect("Failed to initialize Wayland backend");
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(not(feature = "wayland"))]
|
|
|
|
panic!("wayland feature is not enabled");
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
|
|
|
_ => panic!(
|
|
|
|
"Unknown environment variable value for {}, try one of `x11`,`wayland`",
|
|
|
|
BACKEND_PREFERENCE_ENV_VAR,
|
|
|
|
),
|
2017-09-01 19:04:57 +10:00
|
|
|
}
|
|
|
|
}
|
2017-03-04 07:41:51 +11:00
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-10-19 02:51:06 +11:00
|
|
|
let wayland_err = match EventLoop::new_wayland_any_thread() {
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
Ok(event_loop) => return event_loop,
|
|
|
|
Err(err) => err,
|
|
|
|
};
|
2017-03-04 07:41:51 +11:00
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-10-19 02:51:06 +11:00
|
|
|
let x11_err = match EventLoop::new_x11_any_thread() {
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
Ok(event_loop) => return event_loop,
|
|
|
|
Err(err) => err,
|
|
|
|
};
|
2017-09-01 19:04:57 +10:00
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(not(feature = "wayland"))]
|
|
|
|
let wayland_err = "backend disabled";
|
|
|
|
#[cfg(not(feature = "x11"))]
|
|
|
|
let x11_err = "backend disabled";
|
|
|
|
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
let err_string = format!(
|
2018-10-22 09:12:51 +11:00
|
|
|
"Failed to initialize any backend! Wayland status: {:?} X11 status: {:?}",
|
2019-06-22 01:33:15 +10:00
|
|
|
wayland_err, x11_err,
|
X11: General cleanup (#491)
* 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
2018-05-03 23:15:49 +10:00
|
|
|
);
|
|
|
|
panic!(err_string);
|
2017-09-01 19:04:57 +10:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn new_wayland() -> Result<EventLoop<T>, ConnectError> {
|
2019-10-19 02:51:06 +11:00
|
|
|
assert_is_main_thread("new_wayland_any_thread");
|
|
|
|
|
|
|
|
EventLoop::new_wayland_any_thread()
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-10-19 02:51:06 +11:00
|
|
|
pub fn new_wayland_any_thread() -> Result<EventLoop<T>, ConnectError> {
|
2019-06-22 01:33:15 +10:00
|
|
|
wayland::EventLoop::new().map(EventLoop::Wayland)
|
2017-09-01 19:04:57 +10:00
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-04-28 02:06:51 +10:00
|
|
|
pub fn new_x11() -> Result<EventLoop<T>, XNotSupported> {
|
2019-10-19 02:51:06 +11:00
|
|
|
assert_is_main_thread("new_x11_any_thread");
|
|
|
|
|
|
|
|
EventLoop::new_x11_any_thread()
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-10-19 02:51:06 +11:00
|
|
|
pub fn new_x11_any_thread() -> Result<EventLoop<T>, XNotSupported> {
|
2020-02-20 04:39:00 +11:00
|
|
|
let xconn = match X11_BACKEND.lock().as_ref() {
|
|
|
|
Ok(xconn) => xconn.clone(),
|
|
|
|
Err(err) => return Err(err.clone()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(EventLoop::X(x11::EventLoop::new(xconn)))
|
2017-09-01 19:04:57 +10:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn create_proxy(&self) -> EventLoopProxy<T> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy)
|
2017-05-25 23:19:13 +10:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn run_return<F>(&mut self, callback: F)
|
2019-06-22 01:33:15 +10:00
|
|
|
where
|
2020-01-04 17:31:23 +11:00
|
|
|
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
2017-03-04 07:41:51 +11:00
|
|
|
{
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_return(callback))
|
2017-03-04 07:41:51 +11:00
|
|
|
}
|
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
pub fn run<F>(self, callback: F) -> !
|
2019-06-22 01:33:15 +10:00
|
|
|
where
|
2020-01-04 17:31:23 +11:00
|
|
|
F: 'static + FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
2017-03-04 07:41:51 +11:00
|
|
|
{
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoop(evlp) => evlp.run(callback))
|
2017-03-04 07:41:51 +11:00
|
|
|
}
|
2017-09-23 17:36:30 +10:00
|
|
|
|
2019-06-18 04:27:00 +10:00
|
|
|
pub fn window_target(&self) -> &crate::event_loop::EventLoopWindowTarget<T> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoop(evl) => evl.window_target())
|
2017-09-23 17:36:30 +10:00
|
|
|
}
|
2017-03-04 07:41:51 +11:00
|
|
|
}
|
2017-05-25 23:19:13 +10:00
|
|
|
|
2019-02-21 20:51:43 +11:00
|
|
|
impl<T: 'static> EventLoopProxy<T> {
|
2019-12-08 04:22:03 +11:00
|
|
|
pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed<T>> {
|
2020-06-15 17:15:27 +10:00
|
|
|
x11_or_wayland!(match self; EventLoopProxy(proxy) => proxy.send_event(event))
|
2017-05-25 23:19:13 +10:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 20:51:43 +11:00
|
|
|
|
|
|
|
pub enum EventLoopWindowTarget<T> {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-02-21 20:51:43 +11:00
|
|
|
Wayland(wayland::EventLoopWindowTarget<T>),
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
2019-06-22 01:33:15 +10:00
|
|
|
X(x11::EventLoopWindowTarget<T>),
|
2019-02-21 20:51:43 +11:00
|
|
|
}
|
2019-04-28 02:06:51 +10:00
|
|
|
|
2019-08-09 07:50:22 +10:00
|
|
|
impl<T> EventLoopWindowTarget<T> {
|
|
|
|
#[inline]
|
|
|
|
pub fn is_wayland(&self) -> bool {
|
|
|
|
match *self {
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "wayland")]
|
2019-08-09 07:50:22 +10:00
|
|
|
EventLoopWindowTarget::Wayland(_) => true,
|
2020-06-15 17:15:27 +10:00
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
_ => false,
|
2019-08-09 07:50:22 +10:00
|
|
|
}
|
|
|
|
}
|
2020-07-05 05:46:41 +10:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
|
|
|
|
match *self {
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
EventLoopWindowTarget::Wayland(ref evlp) => evlp
|
|
|
|
.available_monitors()
|
|
|
|
.into_iter()
|
|
|
|
.map(MonitorHandle::Wayland)
|
|
|
|
.collect(),
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
EventLoopWindowTarget::X(ref evlp) => evlp
|
|
|
|
.x_connection()
|
|
|
|
.available_monitors()
|
|
|
|
.into_iter()
|
|
|
|
.map(MonitorHandle::X)
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn primary_monitor(&self) -> MonitorHandle {
|
|
|
|
match *self {
|
|
|
|
#[cfg(feature = "wayland")]
|
|
|
|
EventLoopWindowTarget::Wayland(ref evlp) => {
|
|
|
|
MonitorHandle::Wayland(evlp.primary_monitor())
|
|
|
|
}
|
|
|
|
#[cfg(feature = "x11")]
|
|
|
|
EventLoopWindowTarget::X(ref evlp) => {
|
|
|
|
MonitorHandle::X(evlp.x_connection().primary_monitor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 07:50:22 +10:00
|
|
|
}
|
|
|
|
|
2019-04-28 02:06:51 +10:00
|
|
|
fn sticky_exit_callback<T, F>(
|
2020-01-04 17:31:23 +11:00
|
|
|
evt: Event<'_, T>,
|
2019-06-22 01:33:15 +10:00
|
|
|
target: &RootELW<T>,
|
|
|
|
control_flow: &mut ControlFlow,
|
|
|
|
callback: &mut F,
|
|
|
|
) where
|
2020-01-04 17:31:23 +11:00
|
|
|
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
2019-04-28 02:06:51 +10:00
|
|
|
{
|
|
|
|
// make ControlFlow::Exit sticky by providing a dummy
|
|
|
|
// control flow reference if it is already Exit.
|
|
|
|
let mut dummy = ControlFlow::Exit;
|
|
|
|
let cf = if *control_flow == ControlFlow::Exit {
|
|
|
|
&mut dummy
|
|
|
|
} else {
|
|
|
|
control_flow
|
|
|
|
};
|
|
|
|
// user callback
|
|
|
|
callback(evt, target, cf)
|
2019-05-30 11:29:54 +10:00
|
|
|
}
|
2019-10-19 02:51:06 +11:00
|
|
|
|
|
|
|
fn assert_is_main_thread(suggested_method: &str) {
|
|
|
|
if !is_main_thread() {
|
|
|
|
panic!(
|
|
|
|
"Initializing the event loop outside of the main thread is a significant \
|
|
|
|
cross-platform compatibility hazard. If you really, absolutely need to create an \
|
|
|
|
EventLoop on a different thread, please use the `EventLoopExtUnix::{}` function.",
|
|
|
|
suggested_method
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn is_main_thread() -> bool {
|
|
|
|
use libc::{c_long, getpid, syscall, SYS_gettid};
|
|
|
|
|
|
|
|
unsafe { syscall(SYS_gettid) == getpid() as c_long }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
|
|
|
|
fn is_main_thread() -> bool {
|
|
|
|
use libc::pthread_main_np;
|
|
|
|
|
|
|
|
unsafe { pthread_main_np() == 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "netbsd")]
|
|
|
|
fn is_main_thread() -> bool {
|
2020-08-21 04:12:01 +10:00
|
|
|
std::thread::current().name() == Some("main")
|
2019-10-19 02:51:06 +11:00
|
|
|
}
|