summaryrefslogtreecommitdiff
path: root/src/event.rs
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2015-11-22 20:02:10 -0300
committerMatias Linares <matiaslina@openmailbox.org>2015-11-22 20:02:10 -0300
commit83b7d0cb4f30c95ce6f27d2c0944727d75eb6e5e (patch)
tree255c9e3f5e0703676382fda1157bdb5daf14bc62 /src/event.rs
parent47fc031feeddc955e6c7c43410613c75e3370e96 (diff)
downloaddotwm-83b7d0cb4f30c95ce6f27d2c0944727d75eb6e5e.tar.gz
Death to the threads.
All the socket stuff is done syncing it with select calls so we can get either a X11 event or a Socket event. Also cleanup the C mess, it's done all in rust now :).
Diffstat (limited to 'src/event.rs')
-rw-r--r--src/event.rs55
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),