From f1c7be48a971e54050544c3fbb537ec3ef1d8d69 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 02:22:03 +0300 Subject: [PATCH 1/8] Return an error when Windows event loop is busy This was a regression introduced in #3418 This new error could be used to retry events at later time This also removes `EventLoopClosed` struct and replaces it with `EventLoopProxyError` enum --- src/changelog/unreleased.md | 2 ++ src/event_loop.rs | 29 +++++++++++++------ src/platform_impl/android/mod.rs | 4 +-- src/platform_impl/ios/event_loop.rs | 6 ++-- src/platform_impl/linux/mod.rs | 5 ++-- .../linux/wayland/event_loop/proxy.rs | 6 ++-- src/platform_impl/linux/x11/mod.rs | 8 +++-- src/platform_impl/macos/event_loop.rs | 8 +++-- src/platform_impl/orbital/event_loop.rs | 4 +-- src/platform_impl/web/event_loop/proxy.rs | 6 ++-- src/platform_impl/windows/event_loop.rs | 17 ++++++----- 11 files changed, 58 insertions(+), 37 deletions(-) diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 9ad14dffad..afadbbb303 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -41,3 +41,5 @@ - Add `Window::default_attributes` to get default `WindowAttributes`. - `log` has been replaced with `tracing`. The old behavior can be emulated by setting the `log` feature on the `tracing` crate. - On Windows, confine cursor to center of window when grabbed and hidden. +- Removed `event_loop::EventLoopClosed`, and replaced with `event_lopp::EventLoopProxyError` +- Return an error in `EventLoopProxy::send_event` when the event loop is busy on Windows and can't accept new events. diff --git a/src/event_loop.rs b/src/event_loop.rs index 48455e3062..5eb3880bbe 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -576,7 +576,7 @@ impl EventLoopProxy { /// Returns an `Err` if the associated [`EventLoop`] no longer exists. /// /// [`UserEvent(event)`]: Event::UserEvent - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { let _span = tracing::debug_span!("winit::EventLoopProxy::send_event",).entered(); self.event_loop_proxy.send_event(event) @@ -589,20 +589,31 @@ impl fmt::Debug for EventLoopProxy { } } -/// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that -/// no longer exists. -/// -/// Contains the original event given to [`EventLoopProxy::send_event`]. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] -pub struct EventLoopClosed(pub T); +pub enum EventLoopProxyError { + /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that + /// no longer exists. + /// + /// Contains the original event given to [`EventLoopProxy::send_event`]. + Closed(T), + /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that + /// is busy handling events and can't accept new events because it reached its limit. This will happen on Windows for example, + /// if more than 10,000 events are sent in a short time. + Busy, +} -impl fmt::Display for EventLoopClosed { +impl fmt::Display for EventLoopProxyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("Tried to wake up a closed `EventLoop`") + match self { + EventLoopProxyError::Closed(_) => f.write_str("Tried to wake up a closed `EventLoop`"), + EventLoopProxyError::Busy => { + f.write_str("Tried to wake up `EventLoop` while it is busy") + } + } } } -impl error::Error for EventLoopClosed {} +impl error::Error for EventLoopProxyError {} /// Control when device events are captured. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 08dbb6752d..8ff4a8dbbc 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -656,10 +656,10 @@ impl Clone for EventLoopProxy { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopProxyError> { self.user_events_sender .send(event) - .map_err(|err| event_loop::EventLoopClosed(err.0))?; + .map_err(|err| event_loop::EventLoopProxyError::Closed(err.0))?; self.waker.wake(); Ok(()) } diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 27921c1bed..50e658dc7e 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -20,7 +20,7 @@ use crate::{ error::EventLoopError, event::Event, event_loop::{ - ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopClosed, + ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProxyError, }, platform::ios::Idiom, platform_impl::platform::app_state::{EventLoopHandler, HandlePendingUserEvents}, @@ -291,10 +291,10 @@ impl EventLoopProxy { } } - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.sender .send(event) - .map_err(|::std::sync::mpsc::SendError(x)| EventLoopClosed(x))?; + .map_err(|::std::sync::mpsc::SendError(x)| EventLoopProxyError::Closed(x))?; unsafe { // let the main thread know there's a new event CFRunLoopSourceSignal(self.source); diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index bd5e606687..4d4511e457 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -23,7 +23,8 @@ use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}, event_loop::{ - ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed, + ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, + EventLoopProxyError, }, icon::Icon, keyboard::Key, @@ -831,7 +832,7 @@ impl AsRawFd for EventLoop { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { x11_or_wayland!(match self; EventLoopProxy(proxy) => proxy.send_event(event)) } } diff --git a/src/platform_impl/linux/wayland/event_loop/proxy.rs b/src/platform_impl/linux/wayland/event_loop/proxy.rs index dad64ef2c9..e8b1bf2478 100644 --- a/src/platform_impl/linux/wayland/event_loop/proxy.rs +++ b/src/platform_impl/linux/wayland/event_loop/proxy.rs @@ -4,7 +4,7 @@ use std::sync::mpsc::SendError; use sctk::reexports::calloop::channel::Sender; -use crate::event_loop::EventLoopClosed; +use crate::event_loop::EventLoopProxyError; /// A handle that can be sent across the threads and used to wake up the `EventLoop`. pub struct EventLoopProxy { @@ -24,9 +24,9 @@ impl EventLoopProxy { Self { user_events_sender } } - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.user_events_sender .send(event) - .map_err(|SendError(error)| EventLoopClosed(error)) + .map_err(|SendError(error)| EventLoopProxyError::Closed(error)) } } diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 72b9a5c9e6..d3e7fd825f 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -30,7 +30,9 @@ use x11rb::xcb_ffi::ReplyOrIdError; use crate::error::{EventLoopError, OsError as RootOsError}; use crate::event::{Event, StartCause, WindowEvent}; -use crate::event_loop::{ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents, EventLoopClosed}; +use crate::event_loop::{ + ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents, EventLoopProxyError, +}; use crate::platform::pump_events::PumpStatus; use crate::platform_impl::common::xkb::Context; use crate::platform_impl::platform::{min_timeout, WindowId}; @@ -760,10 +762,10 @@ impl ActiveEventLoop { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.user_sender .send(event) - .map_err(|e| EventLoopClosed(e.0)) + .map_err(|e| EventLoopProxyError::Closed(e.0)) } } diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 1e0e34ee37..a6c0bbdedc 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -39,7 +39,9 @@ use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource}; use crate::{ error::EventLoopError, event::Event, - event_loop::{ActiveEventLoop as RootWindowTarget, ControlFlow, DeviceEvents, EventLoopClosed}, + event_loop::{ + ActiveEventLoop as RootWindowTarget, ControlFlow, DeviceEvents, EventLoopProxyError, + }, platform::{macos::ActivationPolicy, pump_events::PumpStatus}, }; @@ -496,10 +498,10 @@ impl EventLoopProxy { } } - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.sender .send(event) - .map_err(|mpsc::SendError(x)| EventLoopClosed(x))?; + .map_err(|mpsc::SendError(x)| EventLoopProxyError::Closed(x))?; unsafe { // let the main thread know there's a new event CFRunLoopSourceSignal(self.source); diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index 11216e1176..26b8c2bbdd 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -772,10 +772,10 @@ pub struct EventLoopProxy { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopProxyError> { self.user_events_sender .send(event) - .map_err(|mpsc::SendError(x)| event_loop::EventLoopClosed(x))?; + .map_err(|mpsc::SendError(x)| event_loop::EventLoopProxyError::Closed(x))?; self.wake_socket.wake().unwrap(); diff --git a/src/platform_impl/web/event_loop/proxy.rs b/src/platform_impl/web/event_loop/proxy.rs index 691efa32b8..ee0a8fdca8 100644 --- a/src/platform_impl/web/event_loop/proxy.rs +++ b/src/platform_impl/web/event_loop/proxy.rs @@ -2,7 +2,7 @@ use std::rc::Weak; use std::sync::mpsc::{SendError, Sender}; use super::runner::Execution; -use crate::event_loop::EventLoopClosed; +use crate::event_loop::EventLoopProxyError; use crate::platform_impl::platform::r#async::Waker; pub struct EventLoopProxy { @@ -15,10 +15,10 @@ impl EventLoopProxy { Self { runner, sender } } - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.sender .send(event) - .map_err(|SendError(event)| EventLoopClosed(event))?; + .map_err(|SendError(event)| EventLoopProxyError::Closed(event))?; self.runner.wake(); Ok(()) } diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index f7d91caa1b..c5cfd1736b 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -17,7 +17,7 @@ use std::{ time::{Duration, Instant}, }; -use crate::utils::Lazy; +use crate::{event_loop::EventLoopProxyError, utils::Lazy}; use windows_sys::Win32::{ Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE, @@ -74,7 +74,7 @@ use crate::{ DeviceEvent, Event, Force, Ime, InnerSizeWriter, RawKeyEvent, Touch, TouchPhase, WindowEvent, }, - event_loop::{ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents, EventLoopClosed}, + event_loop::{ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents}, keyboard::ModifiersState, platform::pump_events::PumpStatus, platform_impl::platform::{ @@ -776,14 +776,17 @@ impl Clone for EventLoopProxy { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { self.event_send .send(event) - .map(|result| { - unsafe { PostMessageW(self.target_window, USER_EVENT_MSG_ID.get(), 0, 0) }; - result + .map_err(|e| EventLoopProxyError::Closed(e.0)) + .and_then(|result| { + if unsafe { PostMessageW(self.target_window, USER_EVENT_MSG_ID.get(), 0, 0) } == 0 { + Err(EventLoopProxyError::Busy) + } else { + Ok(result) + } }) - .map_err(|e| EventLoopClosed(e.0)) } } From 623afa448cb5f20494b004a8a87b2f5dd6254131 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 02:29:52 +0300 Subject: [PATCH 2/8] fmt --- src/event_loop.rs | 13 +++++++------ src/platform_impl/linux/mod.rs | 2 +- src/platform_impl/linux/x11/mod.rs | 19 ++++++++----------- src/platform_impl/windows/event_loop.rs | 3 ++- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index 0ae77b5ee3..31b88e4163 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -568,14 +568,15 @@ impl fmt::Debug for EventLoopProxy { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum EventLoopProxyError { - /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that - /// no longer exists. + /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] + /// that no longer exists. /// /// Contains the original event given to [`EventLoopProxy::send_event`]. Closed(T), - /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] that - /// is busy handling events and can't accept new events because it reached its limit. This will happen on Windows for example, - /// if more than 10,000 events are sent in a short time. + /// The error that is returned when an [`EventLoopProxy`] attempts to wake up an [`EventLoop`] + /// that is busy handling events and can't accept new events because it reached its limit. + /// This will happen on Windows for example, if more than 10,000 events are sent in a short + /// time. Busy, } @@ -585,7 +586,7 @@ impl fmt::Display for EventLoopProxyError { EventLoopProxyError::Closed(_) => f.write_str("Tried to wake up a closed `EventLoop`"), EventLoopProxyError::Busy => { f.write_str("Tried to wake up `EventLoop` while it is busy") - } + }, } } } diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 121ab9c64f..128f03eae5 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -19,9 +19,9 @@ use smol_str::SmolStr; use self::x11::{X11Error, XConnection, XError, XNotSupported}; use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}; +use crate::event_loop::EventLoopProxyError::Closed; use crate::event_loop::{ ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, - EventLoopProxyError::Closed, }; use crate::icon::Icon; use crate::keyboard::Key; diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 468b0b7a83..9a1e293684 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -1007,18 +1007,15 @@ impl Device { let ty = unsafe { (*class_ptr)._type }; if ty == ffi::XIScrollClass { let info = unsafe { &*(class_ptr as *const ffi::XIScrollClassInfo) }; - scroll_axes.push(( - info.number, - ScrollAxis { - increment: info.increment, - orientation: match info.scroll_type { - ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, - ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, - _ => unreachable!(), - }, - position: 0.0, + scroll_axes.push((info.number, ScrollAxis { + increment: info.increment, + orientation: match info.scroll_type { + ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, + ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, + _ => unreachable!(), }, - )); + position: 0.0, + })); } } } diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 66cfc1445b..ca2ae26fd7 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -13,7 +13,8 @@ use std::sync::{Arc, Mutex, MutexGuard}; use std::time::{Duration, Instant}; use std::{mem, panic, ptr}; -use crate::{event_loop::EventLoopProxyError, utils::Lazy}; +use crate::event_loop::EventLoopProxyError; +use crate::utils::Lazy; use windows_sys::Win32::Devices::HumanInterfaceDevice::MOUSE_MOVE_RELATIVE; use windows_sys::Win32::Foundation::{HWND, LPARAM, LRESULT, POINT, RECT, WPARAM}; From 39bd6af055a6681679bced1a0817268f3e0c2275 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 02:31:01 +0300 Subject: [PATCH 3/8] fix x11 --- src/platform_impl/linux/x11/mod.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 9a1e293684..f088e565ab 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -84,8 +84,8 @@ impl Clone for WakeSender { } impl WakeSender { - pub fn send(&self, t: T) -> Result<(), EventLoopClosed> { - let res = self.sender.send(t).map_err(|e| EventLoopClosed(e.0)); + pub fn send(&self, t: T) -> Result<(), EventLoopProxyError> { + let res = self.sender.send(t).map_err(|e| EventLoopProxyError::Closed(e.0)); if res.is_ok() { self.waker.ping(); } @@ -1007,15 +1007,18 @@ impl Device { let ty = unsafe { (*class_ptr)._type }; if ty == ffi::XIScrollClass { let info = unsafe { &*(class_ptr as *const ffi::XIScrollClassInfo) }; - scroll_axes.push((info.number, ScrollAxis { - increment: info.increment, - orientation: match info.scroll_type { - ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, - ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, - _ => unreachable!(), + scroll_axes.push(( + info.number, + ScrollAxis { + increment: info.increment, + orientation: match info.scroll_type { + ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, + ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, + _ => unreachable!(), + }, + position: 0.0, }, - position: 0.0, - })); + )); } } } From 1a1288c67e3cf9fe8f344804f4c87b4c0c6ea22f Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 02:54:29 +0300 Subject: [PATCH 4/8] fix linux again && fmt --- src/platform_impl/linux/mod.rs | 3 +-- src/platform_impl/linux/x11/mod.rs | 19 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 128f03eae5..b0f234d5c3 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -19,9 +19,8 @@ use smol_str::SmolStr; use self::x11::{X11Error, XConnection, XError, XNotSupported}; use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size}; use crate::error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}; -use crate::event_loop::EventLoopProxyError::Closed; use crate::event_loop::{ - ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, + ActiveEventLoop as RootELW, AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopProxyError, }; use crate::icon::Icon; use crate::keyboard::Key; diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index f088e565ab..c95d4291a7 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -1007,18 +1007,15 @@ impl Device { let ty = unsafe { (*class_ptr)._type }; if ty == ffi::XIScrollClass { let info = unsafe { &*(class_ptr as *const ffi::XIScrollClassInfo) }; - scroll_axes.push(( - info.number, - ScrollAxis { - increment: info.increment, - orientation: match info.scroll_type { - ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, - ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, - _ => unreachable!(), - }, - position: 0.0, + scroll_axes.push((info.number, ScrollAxis { + increment: info.increment, + orientation: match info.scroll_type { + ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, + ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, + _ => unreachable!(), }, - )); + position: 0.0, + })); } } } From 045608648c40877dc3e66503ce62e8d8ad0120f8 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 03:31:38 +0300 Subject: [PATCH 5/8] fix 11 one more time --- src/platform_impl/linux/x11/mod.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index c95d4291a7..86ed0b89a0 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -729,7 +729,7 @@ impl ActiveEventLoop { impl EventLoopProxy { pub fn send_event(&self, event: T) -> Result<(), EventLoopProxyError> { - self.user_sender.send(event).map_err(|e| EventLoopProxyError::Closed(e.0)) + self.user_sender.send(event) } } @@ -1007,15 +1007,18 @@ impl Device { let ty = unsafe { (*class_ptr)._type }; if ty == ffi::XIScrollClass { let info = unsafe { &*(class_ptr as *const ffi::XIScrollClassInfo) }; - scroll_axes.push((info.number, ScrollAxis { - increment: info.increment, - orientation: match info.scroll_type { - ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, - ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, - _ => unreachable!(), + scroll_axes.push(( + info.number, + ScrollAxis { + increment: info.increment, + orientation: match info.scroll_type { + ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, + ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, + _ => unreachable!(), + }, + position: 0.0, }, - position: 0.0, - })); + )); } } } From fce3c0c9936688cedaed5d9fa1e5c2cc33e5f791 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 03:36:37 +0300 Subject: [PATCH 6/8] fmt --- src/platform_impl/linux/x11/mod.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 86ed0b89a0..278f54e97d 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -1007,18 +1007,15 @@ impl Device { let ty = unsafe { (*class_ptr)._type }; if ty == ffi::XIScrollClass { let info = unsafe { &*(class_ptr as *const ffi::XIScrollClassInfo) }; - scroll_axes.push(( - info.number, - ScrollAxis { - increment: info.increment, - orientation: match info.scroll_type { - ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, - ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, - _ => unreachable!(), - }, - position: 0.0, + scroll_axes.push((info.number, ScrollAxis { + increment: info.increment, + orientation: match info.scroll_type { + ffi::XIScrollTypeHorizontal => ScrollOrientation::Horizontal, + ffi::XIScrollTypeVertical => ScrollOrientation::Vertical, + _ => unreachable!(), }, - )); + position: 0.0, + })); } } } From 437ede948e02eee99f4ca5a3a57d8a1ab54b3abe Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 04:15:35 +0300 Subject: [PATCH 7/8] spam-events example --- examples/spam-events.rs | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/spam-events.rs diff --git a/examples/spam-events.rs b/examples/spam-events.rs new file mode 100644 index 0000000000..e733de778a --- /dev/null +++ b/examples/spam-events.rs @@ -0,0 +1,86 @@ +use std::error::Error; +use std::fs::File; + +use winit::application::ApplicationHandler; +use winit::event_loop::{ActiveEventLoop, EventLoop}; + +#[allow(dead_code)] +#[derive(Debug, Clone, Copy)] +enum UserEvent { + WakeUp, + Counter(u64), +} + +struct Application { + file: std::fs::File, +} + +impl Application { + fn new(_event_loop: &EventLoop) -> Self { + Self { + file: File::options() + .write(true) + .truncate(true) + .append(false) + .create(true) + .open("log.txt") + .unwrap(), + } + } +} + +impl ApplicationHandler for Application { + fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) { + // write events to file, leave stdout for other info + use std::io::Write; + write!(&mut self.file, "User event: {event:?}\n").unwrap(); + + match event { + UserEvent::Counter(c) => { + if c == 15000 { + std::process::exit(0); + } + }, + _ => {}, + } + } + + fn resumed(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {} + + fn window_event( + &mut self, + _event_loop: &winit::event_loop::ActiveEventLoop, + _window_id: winit::window::WindowId, + _event: winit::event::WindowEvent, + ) { + } +} + +fn main() -> Result<(), Box> { + let event_loop = EventLoop::::with_user_event().build()?; + let proxy = event_loop.create_proxy(); + + std::thread::spawn(move || { + let mut counter = 0; + loop { + if proxy.send_event(UserEvent::Counter(counter)).is_err() { + println!("Failed: {}", counter); + } + + counter += 1; + + if counter > 15000 { + let mut wakeup_counter = 1; + loop { + let _ = proxy.send_event(UserEvent::WakeUp); + println!("Sent {wakeup_counter} WakeUp events"); + wakeup_counter += 1; + } + } + } + }); + + let mut state = Application::new(&event_loop); + + event_loop.run_app(&mut state).map_err(Into::into) +} From 6c03e368f788158444da9add2621c098cf102a61 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 04:21:52 +0300 Subject: [PATCH 8/8] clippy --- examples/spam-events.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/spam-events.rs b/examples/spam-events.rs index e733de778a..ae903a61ec 100644 --- a/examples/spam-events.rs +++ b/examples/spam-events.rs @@ -33,15 +33,12 @@ impl ApplicationHandler for Application { fn user_event(&mut self, _event_loop: &ActiveEventLoop, event: UserEvent) { // write events to file, leave stdout for other info use std::io::Write; - write!(&mut self.file, "User event: {event:?}\n").unwrap(); + writeln!(&mut self.file, "User event: {event:?}").unwrap(); - match event { - UserEvent::Counter(c) => { - if c == 15000 { - std::process::exit(0); - } - }, - _ => {}, + if let UserEvent::Counter(c) = event { + if c == 15000 { + std::process::exit(0); + } } }