mirror of
https://github.com/storytold/winit.git
synced 2026-11-30 02:08:57 +01:00
This helps with portability and defines some top-level structure around the event loop, so in the future, backends can get an idea of what API to use. This also changes the API to be object safe by using `dyn` throughout.
194 lines
7.2 KiB
Rust
194 lines
7.2 KiB
Rust
use std::marker::PhantomData;
|
|
#[cfg(any(x11_platform, wayland_platform))]
|
|
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use rwh_06::{DisplayHandle, HandleError, HasDisplayHandle};
|
|
|
|
use crate::application::ApplicationHandler;
|
|
use crate::cursor::{CustomCursor, CustomCursorSource};
|
|
use crate::error::{EventLoopError, RequestError};
|
|
use crate::event_loop::{
|
|
ControlFlow, DeviceEvents, EventLoopProvider, EventLoopProxy, OwnedDisplayHandle,
|
|
};
|
|
use crate::platform_impl;
|
|
|
|
/// Provides a way to retrieve events from the system and from the windows that were registered to
|
|
/// the events loop.
|
|
///
|
|
/// An `EventLoop` can be seen more or less as a "context". Calling [`EventLoop::new`]
|
|
/// initializes everything that will be required to create windows. For example on Linux creating
|
|
/// an event loop opens a connection to the X or Wayland server.
|
|
///
|
|
/// To wake up an `EventLoop` from a another thread, see the [`EventLoopProxy`] docs.
|
|
///
|
|
/// Note that this cannot be shared across threads (due to platform-dependant logic
|
|
/// forbidding it), as such it is neither [`Send`] nor [`Sync`]. If you need cross-thread access,
|
|
/// the [`Window`] created from this _can_ be sent to an other thread, and the
|
|
/// [`EventLoopProxy`] allows you to wake up an `EventLoop` from another thread.
|
|
///
|
|
/// [`Window`]: crate::window::Window
|
|
#[derive(Debug)]
|
|
pub struct EventLoop {
|
|
pub(crate) event_loop: platform_impl::EventLoop,
|
|
pub(crate) _marker: PhantomData<*mut ()>, // Not Send nor Sync
|
|
}
|
|
|
|
impl EventLoop {
|
|
/// Create the event loop.
|
|
///
|
|
/// This is an alias of `EventLoop::builder().build()`.
|
|
#[inline]
|
|
pub fn new() -> Result<EventLoop, EventLoopError> {
|
|
Self::builder().build()
|
|
}
|
|
|
|
/// Start building a new event loop.
|
|
///
|
|
/// This returns an [`EventLoopBuilder`], to allow configuring the event loop before creation.
|
|
///
|
|
/// To get the actual event loop, call [`build`][EventLoopBuilder::build] on that.
|
|
#[inline]
|
|
pub fn builder() -> EventLoopBuilder {
|
|
EventLoopBuilder { platform_specific: Default::default() }
|
|
}
|
|
}
|
|
|
|
impl EventLoopProvider for EventLoop {
|
|
#[cfg(not(all(web_platform, target_feature = "exception-handling")))]
|
|
fn run_app(self, app: impl ApplicationHandler) -> Result<(), EventLoopError> {
|
|
self.event_loop.run_app(app)
|
|
}
|
|
|
|
fn create_proxy(&self) -> EventLoopProxy {
|
|
self.event_loop.window_target().create_proxy()
|
|
}
|
|
|
|
fn owned_display_handle(&self) -> OwnedDisplayHandle {
|
|
self.event_loop.window_target().owned_display_handle()
|
|
}
|
|
|
|
fn listen_device_events(&self, allowed: DeviceEvents) {
|
|
let _span = tracing::debug_span!(
|
|
"winit::EventLoop::listen_device_events",
|
|
allowed = ?allowed
|
|
)
|
|
.entered();
|
|
self.event_loop.window_target().listen_device_events(allowed)
|
|
}
|
|
|
|
fn set_control_flow(&self, control_flow: ControlFlow) {
|
|
self.event_loop.window_target().set_control_flow(control_flow);
|
|
}
|
|
|
|
fn create_custom_cursor(
|
|
&self,
|
|
custom_cursor: CustomCursorSource,
|
|
) -> Result<CustomCursor, RequestError> {
|
|
self.event_loop.window_target().create_custom_cursor(custom_cursor)
|
|
}
|
|
}
|
|
|
|
impl HasDisplayHandle for EventLoop {
|
|
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
|
|
HasDisplayHandle::display_handle(self.event_loop.window_target().rwh_06_handle())
|
|
}
|
|
}
|
|
|
|
#[cfg(any(x11_platform, wayland_platform))]
|
|
impl AsFd for EventLoop {
|
|
/// Get the underlying [EventLoop]'s `fd` which you can register
|
|
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
|
|
/// loop must be polled with the [`pump_app_events`] API.
|
|
///
|
|
/// [`calloop`]: https://crates.io/crates/calloop
|
|
/// [`mio`]: https://crates.io/crates/mio
|
|
/// [`pump_app_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_app_events
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
self.event_loop.as_fd()
|
|
}
|
|
}
|
|
|
|
#[cfg(any(x11_platform, wayland_platform))]
|
|
impl AsRawFd for EventLoop {
|
|
/// Get the underlying [EventLoop]'s raw `fd` which you can register
|
|
/// into other event loop, like [`calloop`] or [`mio`]. When doing so, the
|
|
/// loop must be polled with the [`pump_app_events`] API.
|
|
///
|
|
/// [`calloop`]: https://crates.io/crates/calloop
|
|
/// [`mio`]: https://crates.io/crates/mio
|
|
/// [`pump_app_events`]: crate::platform::pump_events::EventLoopExtPumpEvents::pump_app_events
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
self.event_loop.as_raw_fd()
|
|
}
|
|
}
|
|
|
|
/// Object that allows building the event loop.
|
|
///
|
|
/// This is used to make specifying options that affect the whole application
|
|
/// easier. But note that constructing multiple event loops is not supported.
|
|
///
|
|
/// This can be created using [`EventLoop::builder`].
|
|
#[derive(Default, Debug, PartialEq, Eq, Hash)]
|
|
pub struct EventLoopBuilder {
|
|
pub(crate) platform_specific: platform_impl::PlatformSpecificEventLoopAttributes,
|
|
}
|
|
|
|
static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false);
|
|
|
|
impl EventLoopBuilder {
|
|
/// Builds a new event loop.
|
|
///
|
|
/// ***For cross-platform compatibility, the [`EventLoop`] must be created on the main thread,
|
|
/// and only once per application.***
|
|
///
|
|
/// Calling this function will result in display backend initialisation.
|
|
///
|
|
/// ## Panics
|
|
///
|
|
/// Attempting to create the event loop off the main thread will panic. This
|
|
/// restriction isn't strictly necessary on all platforms, but is imposed to
|
|
/// eliminate any nasty surprises when porting to platforms that require it.
|
|
/// `EventLoopBuilderExt::with_any_thread` functions are exposed in the relevant
|
|
/// [`platform`] module if the target platform supports creating an event
|
|
/// loop on any thread.
|
|
///
|
|
/// ## Platform-specific
|
|
///
|
|
/// - **Wayland/X11:** to prevent running under `Wayland` or `X11` unset `WAYLAND_DISPLAY` or
|
|
/// `DISPLAY` respectively when building the event loop.
|
|
/// - **Android:** must be configured with an `AndroidApp` from `android_main()` by calling
|
|
/// [`.with_android_app(app)`] before calling `.build()`, otherwise it'll panic.
|
|
///
|
|
/// [`platform`]: crate::platform
|
|
#[cfg_attr(
|
|
android_platform,
|
|
doc = "[`.with_android_app(app)`]: \
|
|
crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
|
|
)]
|
|
#[cfg_attr(
|
|
not(android_platform),
|
|
doc = "[`.with_android_app(app)`]: #only-available-on-android"
|
|
)]
|
|
#[inline]
|
|
pub fn build(&mut self) -> Result<EventLoop, EventLoopError> {
|
|
let _span = tracing::debug_span!("winit::EventLoopBuilder::build").entered();
|
|
|
|
if EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) {
|
|
return Err(EventLoopError::RecreationAttempt);
|
|
}
|
|
|
|
// Certain platforms accept a mutable reference in their API.
|
|
#[allow(clippy::unnecessary_mut_passed)]
|
|
Ok(EventLoop {
|
|
event_loop: platform_impl::EventLoop::new(&mut self.platform_specific)?,
|
|
_marker: PhantomData,
|
|
})
|
|
}
|
|
|
|
#[cfg(web_platform)]
|
|
pub(crate) fn allow_event_loop_recreation() {
|
|
EVENT_LOOP_CREATED.store(false, Ordering::Relaxed);
|
|
}
|
|
}
|