From b3aae074e42fc3acaf190aa444f2e9f1eb5620e3 Mon Sep 17 00:00:00 2001 From: Victor Berger Date: Sat, 25 Apr 2020 19:10:02 +0200 Subject: [PATCH] Update calloop to 0.6 --- Cargo.toml | 2 +- anvil/src/state.rs | 18 +++----- anvil/src/udev.rs | 73 ++++++++++++++---------------- src/backend/drm/mod.rs | 15 +++--- src/backend/libinput/mod.rs | 20 ++++---- src/backend/session/auto.rs | 2 +- src/backend/session/dbus/logind.rs | 36 ++++++++------- src/backend/session/direct.rs | 20 ++++++-- src/backend/udev.rs | 16 +++---- src/xwayland/xserver.rs | 11 +++-- 10 files changed, 106 insertions(+), 107 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ca8f1af..6f6f4ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ members = [ "anvil" ] [dependencies] bitflags = "1" -calloop = "0.5.1" +calloop = "0.6.2" dbus = { version = "0.8", optional = true } drm = { version = "^0.4.0", git = "https://github.com/drakulix/drm-rs", branch = "develop", optional = true } gbm = { version = "^0.6.0", git = "https://github.com/drakulix/gbm.rs", branch = "develop", optional = true, default-features = false, features = ["drm-support"] } diff --git a/anvil/src/state.rs b/anvil/src/state.rs index 9af1ad5..7b0927e 100644 --- a/anvil/src/state.rs +++ b/anvil/src/state.rs @@ -10,9 +10,8 @@ use std::{ use smithay::{ reexports::{ calloop::{ - generic::{Generic, SourceRawFd}, - mio::Interest, - LoopHandle, Source, + generic::{Fd, Generic}, + Interest, LoopHandle, Mode, Source, }, wayland_server::{protocol::wl_surface::WlSurface, Display}, }, @@ -35,7 +34,7 @@ pub struct AnvilState { pub dnd_icon: Arc>>, pub log: slog::Logger, // things we must keep alive - _wayland_event_source: Source>, + _wayland_event_source: Source>, } impl AnvilState { @@ -48,21 +47,18 @@ impl AnvilState { // init the wayland connection let _wayland_event_source = handle .insert_source( - { - let mut source = Generic::from_raw_fd(display.borrow().get_poll_fd()); - source.set_interest(Interest::READABLE); - source - }, + Generic::from_fd(display.borrow().get_poll_fd(), Interest::Readable, Mode::Level), { let display = display.clone(); let log = log.clone(); - move |_, state: &mut AnvilState| { + move |_, _, state: &mut AnvilState| { let mut display = display.borrow_mut(); match display.dispatch(std::time::Duration::from_millis(0), state) { - Ok(_) => {} + Ok(_) => Ok(()), Err(e) => { error!(log, "I/O error on the Wayland display: {}", e); state.running.store(false, Ordering::SeqCst); + Err(e) } } } diff --git a/anvil/src/udev.rs b/anvil/src/udev.rs index 5120dee..393cae2 100644 --- a/anvil/src/udev.rs +++ b/anvil/src/udev.rs @@ -35,10 +35,7 @@ use smithay::{ udev::{primary_gpu, udev_backend_bind, UdevBackend, UdevHandler}, }, reexports::{ - calloop::{ - generic::{Generic, SourceFd}, - EventLoop, LoopHandle, Source, - }, + calloop::{generic::Generic, EventLoop, LoopHandle, Source}, drm::control::{ connector::{Info as ConnectorInfo, State as ConnectorState}, crtc, @@ -226,7 +223,7 @@ pub fn run_udev( let libinput_event_source = libinput_bind(libinput_backend, event_loop.handle()) .map_err(|e| -> IoError { e.into() }) .unwrap(); - let session_event_source = auto_session_bind(notifier, &event_loop.handle()) + let session_event_source = auto_session_bind(notifier, event_loop.handle()) .map_err(|(e, _)| e) .unwrap(); let udev_event_source = udev_backend_bind(udev_backend, &event_loop.handle()) @@ -256,15 +253,15 @@ pub fn run_udev( notifier.unregister(libinput_session_id); notifier.unregister(udev_session_id); - libinput_event_source.remove(); - udev_event_source.remove(); + event_loop.handle().remove(libinput_event_source); + event_loop.handle().remove(udev_event_source); Ok(()) } struct BackendData { id: S::Id, - event_source: Source>>, + event_source: Source>, surfaces: Rc>>>, } @@ -482,33 +479,37 @@ impl UdevHandler for UdevHandlerImpl fn device_changed(&mut self, device: dev_t) { //quick and dirty, just re-init all backends if let Some(ref mut backend_data) = self.backends.get_mut(&device) { - let source = backend_data.event_source.clone_inner(); - let mut evented = source.borrow_mut(); - let mut backends = backend_data.surfaces.borrow_mut(); - #[cfg(feature = "egl")] - let new_backends = UdevHandlerImpl::::scan_connectors( - &mut (*evented).0, - self.egl_buffer_reader.clone(), - &self.logger, - ); - #[cfg(not(feature = "egl"))] - let new_backends = UdevHandlerImpl::::scan_connectors(&mut (*evented).0, &self.logger); - *backends = new_backends; + let logger = &self.logger; + let pointer_image = &self.pointer_image; + let egl_buffer_reader = self.egl_buffer_reader.clone(); + self.loop_handle + .with_source(&backend_data.event_source, |source| { + let mut backends = backend_data.surfaces.borrow_mut(); + #[cfg(feature = "egl")] + let new_backends = UdevHandlerImpl::::scan_connectors( + &mut source.file, + egl_buffer_reader, + logger, + ); + #[cfg(not(feature = "egl"))] + let new_backends = UdevHandlerImpl::::scan_connectors(&mut source.file, logger); + *backends = new_backends; - for renderer in backends.values() { - // create cursor - renderer - .borrow() - .set_cursor_representation(&self.pointer_image, (2, 2)) - .unwrap(); + for renderer in backends.values() { + // create cursor + renderer + .borrow() + .set_cursor_representation(pointer_image, (2, 2)) + .unwrap(); - // render first frame - { - let mut frame = renderer.draw(); - frame.clear_color(0.8, 0.8, 0.9, 1.0); - frame.finish().unwrap(); - } - } + // render first frame + { + let mut frame = renderer.draw(); + frame.clear_color(0.8, 0.8, 0.9, 1.0); + frame.finish().unwrap(); + } + } + }); } } @@ -519,11 +520,7 @@ impl UdevHandler for UdevHandlerImpl backend_data.surfaces.borrow_mut().clear(); debug!(self.logger, "Surfaces dropped"); - let device = Rc::try_unwrap(backend_data.event_source.remove().unwrap()) - .map_err(|_| "This should not happend") - .unwrap() - .into_inner() - .0; + let device = self.loop_handle.remove(backend_data.event_source).unwrap(); // don't use hardware acceleration anymore, if this was the primary gpu #[cfg(feature = "egl")] diff --git a/src/backend/drm/mod.rs b/src/backend/drm/mod.rs index 1e05df0..e7eb844 100644 --- a/src/backend/drm/mod.rs +++ b/src/backend/drm/mod.rs @@ -46,10 +46,7 @@ use std::iter::IntoIterator; use std::os::unix::io::AsRawFd; use std::path::PathBuf; -use calloop::generic::{Generic, SourceFd}; -use calloop::mio::Interest; -use calloop::InsertError; -use calloop::{LoopHandle, Source}; +use calloop::{generic::Generic, InsertError, LoopHandle, Source}; use super::graphics::SwapBuffersError; @@ -245,7 +242,7 @@ impl DevPath for A { } /// calloop source associated with a Device -pub type DrmSource = Generic>; +pub type DrmSource = Generic; /// Bind a `Device` to an [`EventLoop`](calloop::EventLoop), /// @@ -259,10 +256,10 @@ where D: Device, Data: 'static, { - let mut source = Generic::from_fd_source(device); - source.set_interest(Interest::READABLE); + let source = Generic::new(device, calloop::Interest::Readable, calloop::Mode::Level); - handle.insert_source(source, |evt, _| { - evt.source.borrow_mut().0.process_events(); + handle.insert_source(source, |_, source, _| { + source.process_events(); + Ok(()) }) } diff --git a/src/backend/libinput/mod.rs b/src/backend/libinput/mod.rs index 954d660..eb0b13b 100644 --- a/src/backend/libinput/mod.rs +++ b/src/backend/libinput/mod.rs @@ -17,11 +17,7 @@ use std::{ os::unix::io::{AsRawFd, RawFd}, }; -use calloop::{ - generic::{Generic, SourceFd}, - mio::Interest, - InsertError, LoopHandle, Source, -}; +use calloop::{generic::Generic, InsertError, LoopHandle, Source}; // No idea if this is the same across unix platforms // Lets make this linux exclusive for now, once someone tries to build it for @@ -426,7 +422,7 @@ impl AsRawFd for LibinputInputBackend { } /// calloop source associated with the libinput backend -pub type LibinputSource = Generic>; +pub type LibinputSource = Generic; /// Binds a [`LibinputInputBackend`] to a given [`LoopHandle`]. /// @@ -436,13 +432,13 @@ pub fn libinput_bind( backend: LibinputInputBackend, handle: LoopHandle, ) -> Result, InsertError> { - let mut source = Generic::from_fd_source(backend); - source.set_interest(Interest::READABLE); + let source = Generic::new(backend, calloop::Interest::Readable, calloop::Mode::Level); - handle.insert_source(source, move |evt, _| { - let mut backend = evt.source.borrow_mut(); - if let Err(error) = backend.0.dispatch_new_events() { - warn!(backend.0.logger, "Libinput errored: {}", error); + handle.insert_source(source, move |_, backend, _| { + if let Err(error) = backend.dispatch_new_events() { + warn!(backend.logger, "Libinput errored: {}", error); + return Err(std::io::Error::new(std::io::ErrorKind::Other, Box::new(error))); } + Ok(()) }) } diff --git a/src/backend/session/auto.rs b/src/backend/session/auto.rs index 9514b3c..4f541df 100644 --- a/src/backend/session/auto.rs +++ b/src/backend/session/auto.rs @@ -146,7 +146,7 @@ impl AutoSession { /// session state and call its [`SessionObserver`]s. pub fn auto_session_bind( notifier: AutoSessionNotifier, - handle: &LoopHandle, + handle: LoopHandle, ) -> ::std::result::Result { Ok(match notifier { #[cfg(feature = "backend_session_logind")] diff --git a/src/backend/session/dbus/logind.rs b/src/backend/session/dbus/logind.rs index 8319c74..b049e2a 100644 --- a/src/backend/session/dbus/logind.rs +++ b/src/backend/session/dbus/logind.rs @@ -53,9 +53,8 @@ use std::{ use systemd::login; use calloop::{ - generic::{Event, Generic, SourceRawFd}, - mio::Interest, - InsertError, LoopHandle, Source, + generic::{Fd, Generic}, + InsertError, Interest, LoopHandle, Readiness, Source, }; struct LogindSessionImpl { @@ -433,7 +432,8 @@ impl SessionNotifier for LogindSessionNotifier { pub struct BoundLogindSession { notifier: LogindSessionNotifier, _watches: Vec, - sources: Vec>>, + sources: Vec>>, + kill_source: Box>)>, } /// Bind a [`LogindSessionNotifier`] to an [`EventLoop`](calloop::EventLoop). @@ -443,7 +443,7 @@ pub struct BoundLogindSession { /// session state and call it's [`SessionObserver`]s. pub fn logind_session_bind( notifier: LogindSessionNotifier, - handle: &LoopHandle, + handle: LoopHandle, ) -> ::std::result::Result { let watches = notifier.internal.conn.borrow().watch_fds(); @@ -453,19 +453,22 @@ pub fn logind_session_bind( .into_iter() .filter_map(|watch| { let interest = match (watch.writable(), watch.readable()) { - (true, true) => Interest::WRITABLE | Interest::READABLE, - (true, false) => Interest::WRITABLE, - (false, true) => Interest::READABLE, - (false, false) => return None + (true, true) => Interest::Both, + (true, false) => Interest::Writable, + (false, true) => Interest::Readable, + (false, false) => return None, }; - let mut source = Generic::from_raw_fd(watch.fd()); - source.set_interest(interest); + let source = Generic::from_fd(watch.fd(), interest, calloop::Mode::Level); let source = handle.insert_source(source, { let mut notifier = notifier.clone(); - move |evt, _| notifier.event(evt) + move |readiness, fd, _| { + notifier.event(readiness, fd.0); + Ok(()) + } }); Some(source) - }).collect::<::std::result::Result>>, InsertError>>>() + }) + .collect::<::std::result::Result>>, InsertError>>>() .map_err(|err| { ( err.into(), @@ -479,6 +482,7 @@ pub fn logind_session_bind( notifier, _watches: watches, sources, + kill_source: Box::new(move |source| handle.kill(source)), }) } @@ -486,7 +490,7 @@ impl BoundLogindSession { /// Unbind the logind session from the [`EventLoop`](calloop::EventLoop) pub fn unbind(self) -> LogindSessionNotifier { for source in self.sources { - source.remove(); + (self.kill_source)(source); } self.notifier } @@ -508,9 +512,7 @@ impl Drop for LogindSessionNotifier { } impl LogindSessionNotifier { - fn event(&mut self, event: Event) { - let fd = event.source.borrow().0; - let readiness = event.readiness; + fn event(&mut self, readiness: Readiness, fd: RawFd) { let conn = self.internal.conn.borrow(); let items = conn.watch_handle( fd, diff --git a/src/backend/session/direct.rs b/src/backend/session/direct.rs index ffa7920..2bc94c4 100644 --- a/src/backend/session/direct.rs +++ b/src/backend/session/direct.rs @@ -396,13 +396,18 @@ impl DirectSessionNotifier { pub struct BoundDirectSession { source: Source, notifier: Rc>, + kill_source: Box)>, } impl BoundDirectSession { /// Unbind the direct session from the [`EventLoop`](calloop::EventLoop) pub fn unbind(self) -> DirectSessionNotifier { - let BoundDirectSession { source, notifier } = self; - source.remove(); + let BoundDirectSession { + source, + notifier, + kill_source, + } = self; + kill_source(source); Rc::try_unwrap(notifier) .map(RefCell::into_inner) .unwrap_or_else(|_| panic!("Notifier should have been freed from the event loop!")) @@ -416,7 +421,7 @@ impl BoundDirectSession { /// session state and call it's [`SessionObserver`]s. pub fn direct_session_bind( notifier: DirectSessionNotifier, - handle: &LoopHandle, + handle: LoopHandle, ) -> ::std::result::Result { let signal = notifier.signal; let source = match Signals::new(&[signal]) { @@ -428,7 +433,7 @@ pub fn direct_session_bind( let source = handle .insert_source(source, { let notifier = notifier.clone(); - move |_, _| notifier.borrow_mut().signal_received() + move |_, _, _| notifier.borrow_mut().signal_received() }) .map_err(move |e| { // the backend in the closure should already have been dropped @@ -437,7 +442,12 @@ pub fn direct_session_bind( .into_inner(); (e.into(), notifier) })?; - Ok(BoundDirectSession { source, notifier }) + let kill_source = Box::new(move |source| handle.kill(source)); + Ok(BoundDirectSession { + source, + notifier, + kill_source, + }) } /// Errors related to direct/tty sessions diff --git a/src/backend/udev.rs b/src/backend/udev.rs index cc41ca4..d0baeaa 100644 --- a/src/backend/udev.rs +++ b/src/backend/udev.rs @@ -19,11 +19,7 @@ use std::{ }; use udev::{Enumerator, EventType, MonitorBuilder, MonitorSocket}; -use calloop::{ - generic::{Generic, SourceFd}, - mio::Interest, - InsertError, LoopHandle, Source, -}; +use calloop::{generic::Generic, InsertError, LoopHandle, Source}; /// Backend to monitor available drm devices. /// @@ -91,7 +87,7 @@ impl Drop for UdevBackend { } /// calloop event source associated with the Udev backend -pub type UdevSource = Generic>>; +pub type UdevSource = Generic>; /// Binds a [`UdevBackend`] to a given [`EventLoop`](calloop::EventLoop). /// @@ -101,11 +97,11 @@ pub fn udev_backend_bind( udev: UdevBackend, handle: &LoopHandle, ) -> Result>, InsertError>> { - let mut source = Generic::from_fd_source(udev); - source.set_interest(Interest::READABLE); + let source = Generic::new(udev, calloop::Interest::Readable, calloop::Mode::Level); - handle.insert_source(source, |evt, _| { - evt.source.borrow_mut().0.process_events(); + handle.insert_source(source, |_, backend, _| { + backend.process_events(); + Ok(()) }) } diff --git a/src/xwayland/xserver.rs b/src/xwayland/xserver.rs index a2ce2d8..575203c 100644 --- a/src/xwayland/xserver.rs +++ b/src/xwayland/xserver.rs @@ -91,11 +91,15 @@ impl XWayland { let log = crate::slog_or_stdlog(logger); let inner = Rc::new(RefCell::new(Inner { wm, + kill_source: { + let handle = handle.clone(); + Box::new(move |source| handle.kill(source)) + }, source_maker: Box::new(move |inner| { handle .insert_source( Signals::new(&[Signal::SIGUSR1]).map_err(|_| ())?, - move |evt, _| { + move |evt, _, _| { debug_assert!(evt.signal() == Signal::SIGUSR1); xwayland_ready(&inner); }, @@ -134,6 +138,7 @@ struct Inner { source_maker: Box>, wayland_display: Rc>, instance: Option, + kill_source: Box)>, log: ::slog::Logger, } @@ -252,7 +257,7 @@ impl Inner { instance.wayland_client.kill(); // remove the event source if let Some(s) = instance.sigusr1_handler.take() { - s.remove(); + (self.kill_source)(s); } // All connections and lockfiles are cleaned by their destructors @@ -336,7 +341,7 @@ fn xwayland_ready(inner: &Rc>>) { // in all cases, cleanup if let Some(s) = instance.sigusr1_handler.take() { - s.remove(); + (inner.kill_source)(s); } }