fn wait_for_event_with_deadline(
    conn: &XCBConnection,
    deadline: Instant,
) -> Result<Event, ConnectionError> {
    use nix::poll::{poll, PollFd, PollFlags};
    use std::os::raw::c_int;
    use std::os::unix::io::AsRawFd;

    loop {
        // Is there already an event?
        if let Some(event) = conn.poll_for_event()? {
            return Ok(event);
        }

        // Are we past the deadline?
        let now = Instant::now();
        if deadline <= now {
            return Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Timeout while waiting for selection owner to reply",
            )
            .into());
        }

        // Use poll() to wait for the socket to become readable.
        let mut poll_fds = [PollFd::new(conn.as_raw_fd(), PollFlags::POLLIN)];
        let poll_timeout = c_int::try_from(deadline.duration_since(now).as_millis())
            .unwrap_or(c_int::max_value() - 1)
            // The above rounds down, but we don't want to wake up to early, so add one
            .saturating_add(1);

        // Wait for the socket to be readable via poll() and try again
        match poll(&mut poll_fds, poll_timeout) {
            Ok(_) => {}
            Err(nix::Error::Sys(nix::errno::Errno::EINTR)) => {}
            Err(e) => return Err(nix_error_to_io(e).into()),
        }
    }
}
/// Wait for an X11 event or return a timeout error if the given deadline is in the past.
Wait for an X11 event or return a timeout error if the given deadline is in the past.
Wait for an X11 event or return a timeout error if the given deadline is in the past .
fn wait_for_event_with_deadline(
    conn: &XCBConnection,
    deadline: Instant,
) -> Result<Event, ConnectionError> {
    use nix::poll::{poll, PollFd, PollFlags};
    use std::os::raw::c_int;
    use std::os::unix::io::AsRawFd;
    loop {
        // Is there already an event?
        if let Some(event) = conn.poll_for_event()? {
            return Ok(event);
        }
        // Are we past the deadline?
        let now = Instant::now();
        if deadline <= now {
            return Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Timeout while waiting for selection owner to reply",
            )
            .into());
        }
        // Use poll() to wait for the socket to become readable.
        let mut poll_fds = [PollFd::new(conn.as_raw_fd(), PollFlags::POLLIN)];
        let poll_timeout = c_int::try_from(deadline.duration_since(now).as_millis())
            .unwrap_or(c_int::max_value() - 1)
            // The above rounds down, but we don't want to wake up to early, so add one
            .saturating_add(1);
        // Wait for the socket to be readable via poll() and try again
        match poll(&mut poll_fds, poll_timeout) {
            Ok(_) => {}
            Err(nix::Error::Sys(nix::errno::Errno::EINTR)) => {}
            Err(e) => return Err(nix_error_to_io(e).into()),
        }
    }
}
fn wait_for_event_with_deadline ( conn : & XCBConnection , deadline : Instant , ) -> Result < Event , ConnectionError > { use nix :: poll :: { poll , PollFd , PollFlags } ; use std :: os :: raw :: c_int ; use std :: os :: unix :: io :: AsRawFd ; loop { // Is there already an event? if let Some ( event ) = conn . poll_for_event ( ) ? { return Ok ( event ) ; } // Are we past the deadline? let now = Instant :: now ( ) ; if deadline <= now { return Err ( std :: io :: Error :: new ( std :: io :: ErrorKind :: TimedOut , " " , ) . into ( ) ) ; } // Use poll() to wait for the socket to become readable. let mut poll_fds = [ PollFd :: new ( conn . as_raw_fd ( ) , PollFlags :: POLLIN ) ] ; let poll_timeout = c_int :: try_from ( deadline . duration_since ( now ) . as_millis ( ) ) . unwrap_or ( c_int :: max_value ( ) - 1 ) // The above rounds down, but we don't want to wake up to early, so add one . saturating_add ( 1 ) ; // Wait for the socket to be readable via poll() and try again match poll ( & mut poll_fds , poll_timeout ) { Ok ( _ ) => { } Err ( nix :: Error :: Sys ( nix :: errno :: Errno :: EINTR ) ) => { } Err ( e ) => return Err ( nix_error_to_io ( e ) . into ( ) ) , } } }
Wait for an X11 event or return a timeout error if the given deadline is in the past.
Wait for an X11 event or return a timeout error if the given deadline is in the past .
conn
&XCBConnection
deadline
Instant
conn
&XCBConnection
deadline
Instant