x11: split event loop and XCB event handling into separate fns
This commit is contained in:
parent
23af18020e
commit
54f8bc7345
|
@ -12,6 +12,10 @@ pub struct Window {
|
||||||
scaling: f64,
|
scaling: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: move to outer crate context
|
||||||
|
pub struct WindowHandle;
|
||||||
|
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
|
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
|
||||||
// Convert the parent to a X11 window ID if we're given one
|
// Convert the parent to a X11 window ID if we're given one
|
||||||
|
@ -101,29 +105,22 @@ impl Window {
|
||||||
|
|
||||||
let mut handler = H::build(&mut window);
|
let mut handler = H::build(&mut window);
|
||||||
|
|
||||||
run_event_loop(&mut window, &mut handler);
|
window.run_event_loop(&mut handler);
|
||||||
|
|
||||||
WindowHandle
|
WindowHandle
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
unsafe impl HasRawWindowHandle for Window {
|
// Event loop
|
||||||
fn raw_window_handle(&self) -> RawWindowHandle {
|
fn run_event_loop<H: WindowHandler>(&mut self, handler: &mut H) {
|
||||||
RawWindowHandle::Xlib(XlibHandle {
|
|
||||||
window: self.window_id as c_ulong,
|
|
||||||
display: self.xcb_connection.conn.get_raw_dpy() as *mut c_void,
|
|
||||||
..raw_window_handle::unix::XlibHandle::empty()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WindowHandle;
|
|
||||||
|
|
||||||
// Event loop
|
|
||||||
fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
|
||||||
loop {
|
loop {
|
||||||
let ev = window.xcb_connection.conn.wait_for_event();
|
let ev = self.xcb_connection.conn.wait_for_event();
|
||||||
if let Some(event) = ev {
|
if let Some(event) = ev {
|
||||||
|
self.handle_xcb_event(handler, event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_xcb_event<H: WindowHandler>(&mut self, handler: &mut H, event: xcb::GenericEvent) {
|
||||||
let event_type = event.response_type() & !0x80;
|
let event_type = event.response_type() & !0x80;
|
||||||
|
|
||||||
// For all of the keyboard and mouse events, you can fetch
|
// For all of the keyboard and mouse events, you can fetch
|
||||||
|
@ -148,7 +145,7 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
|
|
||||||
match event_type {
|
match event_type {
|
||||||
xcb::EXPOSE => {
|
xcb::EXPOSE => {
|
||||||
handler.draw(window);
|
handler.draw(self);
|
||||||
}
|
}
|
||||||
xcb::MOTION_NOTIFY => {
|
xcb::MOTION_NOTIFY => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
|
||||||
|
@ -156,7 +153,7 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
|
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
self,
|
||||||
Event::CursorMotion(event.event_x() as i32, event.event_y() as i32),
|
Event::CursorMotion(event.event_x() as i32, event.event_y() as i32),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -168,7 +165,7 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
match detail {
|
match detail {
|
||||||
4 => {
|
4 => {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
self,
|
||||||
Event::MouseScroll(MouseScroll {
|
Event::MouseScroll(MouseScroll {
|
||||||
x_delta: 0.0,
|
x_delta: 0.0,
|
||||||
y_delta: 1.0,
|
y_delta: 1.0,
|
||||||
|
@ -177,7 +174,7 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
handler.on_event(
|
handler.on_event(
|
||||||
window,
|
self,
|
||||||
Event::MouseScroll(MouseScroll {
|
Event::MouseScroll(MouseScroll {
|
||||||
x_delta: 0.0,
|
x_delta: 0.0,
|
||||||
y_delta: -1.0,
|
y_delta: -1.0,
|
||||||
|
@ -186,7 +183,7 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
}
|
}
|
||||||
detail => {
|
detail => {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
handler.on_event(window, Event::MouseDown(button_id));
|
handler.on_event(self, Event::MouseDown(button_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,26 +193,36 @@ fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
|
||||||
|
|
||||||
if detail != 4 && detail != 5 {
|
if detail != 4 && detail != 5 {
|
||||||
let button_id = mouse_id(detail);
|
let button_id = mouse_id(detail);
|
||||||
handler.on_event(window, Event::MouseUp(button_id));
|
handler.on_event(self, Event::MouseUp(button_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xcb::KEY_PRESS => {
|
xcb::KEY_PRESS => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
handler.on_event(window, Event::KeyDown(detail));
|
handler.on_event(self, Event::KeyDown(detail));
|
||||||
}
|
}
|
||||||
xcb::KEY_RELEASE => {
|
xcb::KEY_RELEASE => {
|
||||||
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
|
||||||
let detail = event.detail();
|
let detail = event.detail();
|
||||||
|
|
||||||
handler.on_event(window, Event::KeyUp(detail));
|
handler.on_event(self, Event::KeyUp(detail));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Unhandled event type: {:?}", event_type);
|
println!("Unhandled event type: {:?}", event_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl HasRawWindowHandle for Window {
|
||||||
|
fn raw_window_handle(&self) -> RawWindowHandle {
|
||||||
|
RawWindowHandle::Xlib(XlibHandle {
|
||||||
|
window: self.window_id as c_ulong,
|
||||||
|
display: self.xcb_connection.conn.get_raw_dpy() as *mut c_void,
|
||||||
|
..raw_window_handle::unix::XlibHandle::empty()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue