From 87673d7ecce661f3b8c5db949e70640934a55de9 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Thu, 7 Sep 2023 16:31:06 +0300 Subject: some refactoring --- src/event_loop.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/event_loop.rs') diff --git a/src/event_loop.rs b/src/event_loop.rs index d869d80..3b02624 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -3,12 +3,20 @@ use std::io; use std::os::fd::RawFd; use anyhow::Result; +use wayrs_client::Connection; -type Callback = Box Result>; +use crate::state::State; -pub struct EventLoop { +type Callback = Box Result>; + +pub struct EventLoopCtx<'a> { + pub conn: &'a mut Connection, + pub state: &'a mut State, +} + +pub struct EventLoop { pollfds: Vec, - cbs: HashMap>, + cbs: HashMap, } pub enum Action { @@ -16,7 +24,7 @@ pub enum Action { Unregister, } -impl EventLoop { +impl EventLoop { pub fn new() -> Self { Self { pollfds: Vec::new(), @@ -26,12 +34,12 @@ impl EventLoop { pub fn register_with_fd(&mut self, fd: RawFd, cb: F) where - F: FnMut(&mut D) -> Result + 'static, + F: FnMut(EventLoopCtx) -> Result + 'static, { self.cbs.insert(fd, Box::new(cb)); } - pub fn run(&mut self, state: &mut D) -> Result<()> { + pub fn run(&mut self, conn: &mut Connection, state: &mut State) -> Result<()> { if self.cbs.is_empty() { return Ok(()); } @@ -61,7 +69,7 @@ impl EventLoop { for fd in &self.pollfds { if fd.revents != 0 { let mut cb = self.cbs.remove(&fd.fd).unwrap(); - match cb(state)? { + match cb(EventLoopCtx { conn, state })? { Action::Keep => { self.cbs.insert(fd.fd, cb); } -- cgit v1.3.1