1
0
Fork 0

adapt x11 backend to new api

This commit is contained in:
micah 2020-09-07 14:16:48 -04:00
parent bb6a5eeeac
commit 057324e688

View file

@ -4,20 +4,19 @@ use std::sync::mpsc;
use super::XcbConnection; use super::XcbConnection;
use crate::{ use crate::{
AppWindow, Event, MouseButtonID, MouseScroll, Parent, RawWindow, WindowInfo, WindowOpenOptions, AppWindow, Event, MouseButtonID, MouseScroll, Parent, WindowOpenOptions,
}; };
use raw_window_handle::RawWindowHandle; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
pub struct Window<A: AppWindow> { pub struct Window {
scaling: f64,
xcb_connection: XcbConnection, xcb_connection: XcbConnection,
app_window: A, window_id: u32,
app_message_rx: mpsc::Receiver<A::AppMessage>, scaling: f64,
} }
impl<A: AppWindow> Window<A> { impl Window {
pub fn open(options: WindowOpenOptions, app_message_rx: mpsc::Receiver<A::AppMessage>) -> Self { pub fn open<A: AppWindow>(options: WindowOpenOptions, app_message_rx: mpsc::Receiver<A::AppMessage>) {
// 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
let parent = match options.parent { let parent = match options.parent {
Parent::None => None, Parent::None => None,
@ -93,46 +92,38 @@ impl<A: AppWindow> Window<A> {
xcb_connection.conn.flush(); xcb_connection.conn.flush();
let raw_handle = RawWindowHandle::Xlib(raw_window_handle::unix::XlibHandle {
window: window_id as c_ulong,
display: xcb_connection.conn.get_raw_dpy() as *mut c_void,
..raw_window_handle::unix::XlibHandle::empty()
});
let raw_window = RawWindow {
raw_window_handle: raw_handle,
};
let scaling = get_scaling_xft(&xcb_connection) let scaling = get_scaling_xft(&xcb_connection)
.or(get_scaling_screen_dimensions(&xcb_connection)) .or(get_scaling_screen_dimensions(&xcb_connection))
.unwrap_or(1.0); .unwrap_or(1.0);
let window_info = WindowInfo { let mut window = Self {
width: options.width as u32,
height: options.height as u32,
scale: scaling,
};
let app_window = A::build(raw_window, &window_info);
let mut x11_window = Self {
scaling,
xcb_connection, xcb_connection,
app_window, window_id,
app_message_rx, scaling,
}; };
x11_window.run_event_loop(); let mut app_window = A::build(&mut window);
x11_window run_event_loop(&mut window, &mut app_window);
} }
}
// Event loop unsafe impl HasRawWindowHandle for Window {
fn run_event_loop(&mut self) { 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()
})
}
}
// Event loop
fn run_event_loop<A: AppWindow>(window: &mut Window, app_window: &mut A) {
loop { loop {
// somehow poll self.app_message_rx for messages at the same time // somehow poll app_message_rx for messages at the same time
let ev = self.xcb_connection.conn.wait_for_event(); let ev = window.xcb_connection.conn.wait_for_event();
if let Some(event) = ev { if let Some(event) = ev {
let event_type = event.response_type() & !0x80; let event_type = event.response_type() & !0x80;
@ -158,14 +149,14 @@ impl<A: AppWindow> Window<A> {
match event_type { match event_type {
xcb::EXPOSE => { xcb::EXPOSE => {
self.app_window.draw(); app_window.draw(window);
} }
xcb::MOTION_NOTIFY => { xcb::MOTION_NOTIFY => {
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
let detail = event.detail(); let detail = event.detail();
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
self.app_window.on_event(Event::CursorMotion( app_window.on_event(window, Event::CursorMotion(
event.event_x() as i32, event.event_x() as i32,
event.event_y() as i32, event.event_y() as i32,
)); ));
@ -177,20 +168,20 @@ impl<A: AppWindow> Window<A> {
match detail { match detail {
4 => { 4 => {
self.app_window.on_event(Event::MouseScroll(MouseScroll { app_window.on_event(window, Event::MouseScroll(MouseScroll {
x_delta: 0.0, x_delta: 0.0,
y_delta: 1.0, y_delta: 1.0,
})); }));
} }
5 => { 5 => {
self.app_window.on_event(Event::MouseScroll(MouseScroll { app_window.on_event(window, Event::MouseScroll(MouseScroll {
x_delta: 0.0, x_delta: 0.0,
y_delta: -1.0, y_delta: -1.0,
})); }));
} }
detail => { detail => {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
self.app_window.on_event(Event::MouseDown(button_id)); app_window.on_event(window, Event::MouseDown(button_id));
} }
} }
} }
@ -200,20 +191,20 @@ impl<A: AppWindow> Window<A> {
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
self.app_window.on_event(Event::MouseUp(button_id)); app_window.on_event(window, 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();
self.app_window.on_event(Event::KeyDown(detail)); app_window.on_event(window, 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();
self.app_window.on_event(Event::KeyUp(detail)); app_window.on_event(window, Event::KeyUp(detail));
} }
_ => { _ => {
println!("Unhandled event type: {:?}", event_type); println!("Unhandled event type: {:?}", event_type);
@ -221,7 +212,6 @@ impl<A: AppWindow> Window<A> {
} }
} }
} }
}
} }
// Try to get the scaling with this function first. // Try to get the scaling with this function first.