diff options
Diffstat (limited to 'src/event.rs')
-rw-r--r-- | src/event.rs | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/src/event.rs b/src/event.rs index 5a5b1b9..f683c63 100644 --- a/src/event.rs +++ b/src/event.rs @@ -11,13 +11,29 @@ use x11::xlib::{ XUnmapEvent, XMapEvent, XConfigureEvent, + + XPending, }; use x11::xlib; use x11::xlib::Display; + +use unix_socket::UnixStream; +use unix_socket::UnixListener; + use libc::c_int; +use libc::{ + FD_SET, + FD_ZERO, + FD_ISSET, + select, + fd_set, +}; use std::fmt; +use std::ptr; +use std::mem::uninitialized; +use std::cmp::max; -use safe_x11::event::next_xevent; +use safe_x11::next_xevent; pub enum Event { Key(XKeyEvent, bool), @@ -31,6 +47,7 @@ pub enum Event { Unmap(XUnmapEvent), Map(XMapEvent), Configure(XConfigureEvent), + Socket(UnixStream), NoEvent } @@ -48,6 +65,7 @@ impl fmt::Debug for Event { &Event::Unmap(ev) => format!("Unmap({})", XEvent::from(ev).get_type()), &Event::Map(ev) => format!("Map({})", XEvent::from(ev).get_type()), &Event::Configure(ev) => format!("Configure({})", XEvent::from(ev).get_type()), + &Event::Socket(_) => format!("Socket()"), &Event::NoEvent => format!("NoEvent"), }; @@ -55,8 +73,39 @@ impl fmt::Debug for Event { } } -pub fn next_event(display: *mut Display, fd: c_int) -> Event { - let ev = next_xevent(display, fd); +pub unsafe fn select_event(display: *mut Display, x11fd: c_int, socket_fd: c_int, + listener: &UnixListener) -> Event { + // Return right now if there're some XEvent on the queue + if XPending(display) > 0 { + return next_event(display); + } + + let mut read_fdset: fd_set = uninitialized(); + + FD_ZERO(&mut read_fdset); + FD_SET(x11fd, &mut read_fdset); + FD_SET(socket_fd, &mut read_fdset); + + select(max(x11fd, socket_fd) + 1, + &mut read_fdset, ptr::null_mut(), ptr::null_mut(), + ptr::null_mut()); + + if FD_ISSET(x11fd, &mut read_fdset) { + next_event(display) + } else if FD_ISSET(socket_fd, &mut read_fdset) { + let mut listener_iter = listener.incoming(); + if let Some(Ok(stream)) = listener_iter.nth(0) { + return Event::Socket(stream) + } else { + Event::NoEvent + } + } else { + Event::NoEvent + } +} + +pub fn next_event(display: *mut Display) -> Event { + let ev = next_xevent(display); match ev.get_type() { 2 => Event::Key(xlib::XKeyEvent::from(ev), true), |