1
0
Fork 0

x11: frame/draw callbacks

currently fixed at 15ms (just above 60fps), but easily configurable and
something we can query the display server for in the future.
This commit is contained in:
William Light 2020-09-11 16:32:21 +02:00
parent dcb99e5c43
commit b650bf772f
2 changed files with 25 additions and 4 deletions

View file

@ -20,7 +20,7 @@ impl WindowHandler for MyProgram {
Self {} Self {}
} }
fn draw(&mut self, window: &mut Window) {} fn draw(&mut self, _window: &mut Window) {}
fn on_event(&mut self, window: &mut Window, event: Event) { fn on_event(&mut self, window: &mut Window, event: Event) {
match event { match event {

View file

@ -1,4 +1,5 @@
use std::os::raw::{c_ulong, c_void}; use std::os::raw::{c_ulong, c_void};
use std::time::*;
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
@ -10,6 +11,9 @@ pub struct Window {
xcb_connection: XcbConnection, xcb_connection: XcbConnection,
window_id: u32, window_id: u32,
scaling: f64, scaling: f64,
frame_interval: Duration,
event_loop_running: bool
} }
// FIXME: move to outer crate context // FIXME: move to outer crate context
@ -93,6 +97,9 @@ impl Window {
xcb_connection, xcb_connection,
window_id, window_id,
scaling, scaling,
frame_interval: Duration::from_millis(15),
event_loop_running: false
}; };
let mut handler = H::build(&mut window); let mut handler = H::build(&mut window);
@ -121,13 +128,28 @@ impl Window {
xcb::ffi::xcb_get_file_descriptor(raw_conn) xcb::ffi::xcb_get_file_descriptor(raw_conn)
}; };
loop { let mut next_frame = Instant::now() + self.frame_interval;
self.event_loop_running = true;
while self.event_loop_running {
let now = Instant::now();
let until_next_frame =
if now > next_frame {
handler.draw(self);
next_frame = now + self.frame_interval;
self.frame_interval
} else {
next_frame - now
};
let mut fds = [ let mut fds = [
PollFd::new(xcb_fd, PollFlags::POLLIN) PollFd::new(xcb_fd, PollFlags::POLLIN)
]; ];
// FIXME: handle errors // FIXME: handle errors
poll(&mut fds, -1).unwrap(); poll(&mut fds, until_next_frame.subsec_millis() as i32)
.unwrap();
if let Some(revents) = fds[0].revents() { if let Some(revents) = fds[0].revents() {
if revents.contains(PollFlags::POLLERR) { if revents.contains(PollFlags::POLLERR) {
@ -234,7 +256,6 @@ impl Window {
} }
} }
} }
} }
unsafe impl HasRawWindowHandle for Window { unsafe impl HasRawWindowHandle for Window {