Address code review feedback

* Fix an issue where PollEventsIterator::next() would fail to return
   keyboard input and mouse events immediately but instead only
   return them on the next call to next()

 * Inline process_generic_event() and queue_event()
This commit is contained in:
Robert Knight 2015-07-13 07:26:07 +01:00
parent b1223bc041
commit d960753360

View file

@ -143,42 +143,17 @@ pub struct PollEventsIterator<'a> {
window: &'a Window window: &'a Window
} }
impl<'a> PollEventsIterator<'a> {
fn queue_event(&mut self, event: Event) {
self.window.pending_events.lock().unwrap().push_back(event);
}
fn process_generic_event(&mut self, event: &ffi::XEvent) {
if let Some(cookie) = GenericEventCookie::from_event(self.window.x.display.borrow(), *event) {
match cookie.cookie.evtype {
ffi::XI_DeviceChanged...ffi::XI_LASTEVENT => {
match self.window.input_handler.lock() {
Ok(mut handler) => {
match handler.translate_event(&cookie.cookie) {
Some(event) => self.queue_event(event),
None => {}
}
},
Err(_) => {}
}
},
_ => {}
}
}
}
}
impl<'a> Iterator for PollEventsIterator<'a> { impl<'a> Iterator for PollEventsIterator<'a> {
type Item = Event; type Item = Event;
fn next(&mut self) -> Option<Event> { fn next(&mut self) -> Option<Event> {
if let Some(ev) = self.window.pending_events.lock().unwrap().pop_front() {
return Some(ev);
}
let xlib = &self.window.x.display.xlib; let xlib = &self.window.x.display.xlib;
loop { loop {
if let Some(ev) = self.window.pending_events.lock().unwrap().pop_front() {
return Some(ev);
}
let mut xev = unsafe { mem::uninitialized() }; let mut xev = unsafe { mem::uninitialized() };
let res = unsafe { (xlib.XCheckMaskEvent)(self.window.x.display.display, -1, &mut xev) }; let res = unsafe { (xlib.XCheckMaskEvent)(self.window.x.display.display, -1, &mut xev) };
@ -231,10 +206,28 @@ impl<'a> Iterator for PollEventsIterator<'a> {
let mut event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&mut xev) }; let mut event: &mut ffi::XKeyEvent = unsafe { mem::transmute(&mut xev) };
let events = self.window.input_handler.lock().unwrap().translate_key_event(&mut event); let events = self.window.input_handler.lock().unwrap().translate_key_event(&mut event);
for event in events { for event in events {
self.queue_event(event); self.window.pending_events.lock().unwrap().push_back(event);
} }
}, },
ffi::GenericEvent => { self.process_generic_event(&mut xev); }
ffi::GenericEvent => {
if let Some(cookie) = GenericEventCookie::from_event(self.window.x.display.borrow(), xev) {
match cookie.cookie.evtype {
ffi::XI_DeviceChanged...ffi::XI_LASTEVENT => {
match self.window.input_handler.lock() {
Ok(mut handler) => {
match handler.translate_event(&cookie.cookie) {
Some(event) => self.window.pending_events.lock().unwrap().push_back(event),
None => {}
}
},
Err(_) => {}
}
},
_ => {}
}
}
}
_ => {} _ => {}
}; };