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 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> {
scaling: f64,
pub struct Window {
xcb_connection: XcbConnection,
app_window: A,
app_message_rx: mpsc::Receiver<A::AppMessage>,
window_id: u32,
scaling: f64,
}
impl<A: AppWindow> Window<A> {
pub fn open(options: WindowOpenOptions, app_message_rx: mpsc::Receiver<A::AppMessage>) -> Self {
impl Window {
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
let parent = match options.parent {
Parent::None => None,
@ -93,46 +92,38 @@ impl<A: AppWindow> Window<A> {
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)
.or(get_scaling_screen_dimensions(&xcb_connection))
.unwrap_or(1.0);
let window_info = WindowInfo {
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,
let mut window = Self {
xcb_connection,
app_window,
app_message_rx,
window_id,
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
fn run_event_loop(&mut self) {
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()
})
}
}
// Event loop
fn run_event_loop<A: AppWindow>(window: &mut Window, app_window: &mut A) {
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 {
let event_type = event.response_type() & !0x80;
@ -158,14 +149,14 @@ impl<A: AppWindow> Window<A> {
match event_type {
xcb::EXPOSE => {
self.app_window.draw();
app_window.draw(window);
}
xcb::MOTION_NOTIFY => {
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
let detail = event.detail();
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_y() as i32,
));
@ -177,20 +168,20 @@ impl<A: AppWindow> Window<A> {
match detail {
4 => {
self.app_window.on_event(Event::MouseScroll(MouseScroll {
app_window.on_event(window, Event::MouseScroll(MouseScroll {
x_delta: 0.0,
y_delta: 1.0,
}));
}
5 => {
self.app_window.on_event(Event::MouseScroll(MouseScroll {
app_window.on_event(window, Event::MouseScroll(MouseScroll {
x_delta: 0.0,
y_delta: -1.0,
}));
}
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 {
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 => {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
let detail = event.detail();
self.app_window.on_event(Event::KeyDown(detail));
app_window.on_event(window, Event::KeyDown(detail));
}
xcb::KEY_RELEASE => {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
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);
@ -221,7 +212,6 @@ impl<A: AppWindow> Window<A> {
}
}
}
}
}
// Try to get the scaling with this function first.