smithay/anvil/src/winit.rs

203 lines
6.2 KiB
Rust
Raw Normal View History

use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration};
2017-06-11 21:03:25 +00:00
2021-06-09 20:12:35 +00:00
#[cfg(feature = "egl")]
2021-06-27 20:18:45 +00:00
use smithay::{
backend::renderer::{ImportDma, ImportEgl},
wayland::dmabuf::init_dmabuf_global,
};
2018-10-02 21:37:24 +00:00
use smithay::{
2021-05-23 21:03:43 +00:00
backend::{input::InputBackend, renderer::Frame, winit, SwapBuffersError},
2019-02-05 16:26:09 +00:00
reexports::{
calloop::EventLoop,
wayland_server::{protocol::wl_output, Display},
},
2018-10-02 21:37:24 +00:00
wayland::{
output::{Mode, PhysicalProperties},
seat::CursorImageStatus,
2018-10-02 21:37:24 +00:00
},
};
use slog::Logger;
use crate::drawing::*;
use crate::state::{AnvilState, Backend};
2017-09-03 17:53:29 +00:00
pub const OUTPUT_NAME: &str = "winit";
pub struct WinitData;
impl Backend for WinitData {
fn seat_name(&self) -> String {
String::from("winit")
}
}
2020-04-10 15:01:49 +00:00
pub fn run_winit(
display: Rc<RefCell<Display>>,
event_loop: &mut EventLoop<'static, AnvilState<WinitData>>,
2020-04-10 15:01:49 +00:00
log: Logger,
) -> Result<(), ()> {
2021-05-13 17:59:47 +00:00
let (renderer, mut input) = winit::init(log.clone()).map_err(|err| {
slog::crit!(log, "Failed to initialize Winit backend: {}", err);
})?;
2021-05-13 17:59:47 +00:00
let renderer = Rc::new(RefCell::new(renderer));
2017-03-07 10:53:57 +00:00
#[cfg(feature = "egl")]
2021-06-27 20:18:45 +00:00
if renderer
.borrow_mut()
.renderer()
.bind_wl_display(&display.borrow())
.is_ok()
{
2021-05-13 17:59:47 +00:00
info!(log, "EGL hardware-acceleration enabled");
2021-06-09 20:12:35 +00:00
let dmabuf_formats = renderer
.borrow_mut()
.renderer()
.dmabuf_formats()
.cloned()
.collect::<Vec<_>>();
2021-05-13 21:33:22 +00:00
let renderer = renderer.clone();
2021-06-09 20:12:35 +00:00
init_dmabuf_global(
&mut *display.borrow_mut(),
dmabuf_formats,
move |buffer, _| renderer.borrow_mut().renderer().import_dmabuf(buffer).is_ok(),
log.clone(),
);
2021-05-13 17:59:47 +00:00
};
2021-05-13 17:59:47 +00:00
let (w, h): (u32, u32) = renderer.borrow().window_size().physical_size.into();
2020-06-27 16:27:47 +00:00
2017-06-11 21:03:25 +00:00
/*
2017-09-20 13:03:58 +00:00
* Initialize the globals
2017-06-11 21:03:25 +00:00
*/
2017-06-13 14:52:43 +00:00
let mut state = AnvilState::init(display.clone(), event_loop.handle(), WinitData, log.clone());
2017-09-22 12:57:11 +00:00
let mode = Mode {
width: w as i32,
height: h as i32,
refresh: 60_000,
};
state.output_map.borrow_mut().add(
OUTPUT_NAME,
2017-10-01 17:14:25 +00:00
PhysicalProperties {
width: 0,
height: 0,
subpixel: wl_output::Subpixel::Unknown,
2018-09-24 22:30:39 +00:00
make: "Smithay".into(),
2017-10-01 17:14:25 +00:00
model: "Winit".into(),
},
mode,
2017-10-01 17:14:25 +00:00
);
let start_time = std::time::Instant::now();
2021-05-23 21:03:43 +00:00
let mut cursor_visible = true;
#[cfg(feature = "xwayland")]
state.start_xwayland();
info!(log, "Initialization completed, starting the main loop.");
2017-03-07 10:53:57 +00:00
while state.running.load(Ordering::SeqCst) {
if input
.dispatch_new_events(|event| state.process_input_event(event))
.is_err()
{
state.running.store(false, Ordering::SeqCst);
break;
}
// drawing logic
{
2021-05-13 17:59:47 +00:00
let mut renderer = renderer.borrow_mut();
// This is safe to do as with winit we are guaranteed to have exactly one output
let output_geometry = state
.output_map
.borrow()
.find_by_name(OUTPUT_NAME, |_, geometry| geometry)
.ok()
.unwrap();
2021-06-29 15:30:48 +00:00
2021-05-26 17:12:45 +00:00
let result = renderer
.render(|renderer, frame| {
frame.clear([0.8, 0.8, 0.9, 1.0])?;
// draw the windows
2021-06-29 15:30:48 +00:00
draw_windows(
renderer,
frame,
&*state.window_map.borrow(),
output_geometry,
2021-06-29 15:30:48 +00:00
&log,
)?;
2021-05-26 17:12:45 +00:00
2021-05-31 19:06:58 +00:00
let (x, y) = state.pointer_location;
2021-05-26 17:12:45 +00:00
// draw the dnd icon if any
{
let guard = state.dnd_icon.lock().unwrap();
if let Some(ref surface) = *guard {
if surface.as_ref().is_alive() {
draw_dnd_icon(renderer, frame, surface, (x as i32, y as i32), &log)?;
2021-05-26 17:12:45 +00:00
}
}
}
// draw the cursor as relevant
{
let mut guard = state.cursor_status.lock().unwrap();
// reset the cursor if the surface is no longer alive
let mut reset = false;
if let CursorImageStatus::Image(ref surface) = *guard {
reset = !surface.as_ref().is_alive();
}
if reset {
*guard = CursorImageStatus::Default;
}
2021-05-23 21:03:43 +00:00
2021-05-26 17:12:45 +00:00
// draw as relevant
if let CursorImageStatus::Image(ref surface) = *guard {
cursor_visible = false;
draw_cursor(renderer, frame, surface, (x as i32, y as i32), &log)?;
2021-05-26 17:12:45 +00:00
} else {
cursor_visible = true;
2021-05-23 21:03:43 +00:00
}
}
2021-05-26 17:12:45 +00:00
Ok(())
})
.map_err(Into::<SwapBuffersError>::into)
2021-06-09 20:46:09 +00:00
.and_then(|x| x);
2021-04-28 22:32:47 +00:00
2021-05-23 21:03:43 +00:00
renderer.window().set_cursor_visible(cursor_visible);
2021-05-23 21:03:43 +00:00
if let Err(SwapBuffersError::ContextLost(err)) = result {
error!(log, "Critical Rendering Error: {}", err);
state.running.store(false, Ordering::SeqCst);
}
}
2020-10-31 17:33:18 +00:00
// Send frame events so that client start drawing their next frame
state
.window_map
.borrow()
.send_frames(start_time.elapsed().as_millis() as u32);
display.borrow_mut().flush_clients(&mut state);
2019-06-17 01:10:50 +00:00
if event_loop
.dispatch(Some(Duration::from_millis(16)), &mut state)
2019-06-17 01:10:50 +00:00
.is_err()
{
state.running.store(false, Ordering::SeqCst);
2019-06-17 01:10:50 +00:00
} else {
display.borrow_mut().flush_clients(&mut state);
state.window_map.borrow_mut().refresh();
state.output_map.borrow_mut().refresh();
2019-06-17 01:10:50 +00:00
}
}
2019-06-17 01:10:50 +00:00
// Cleanup stuff
state.window_map.borrow_mut().clear();
2019-06-17 01:10:50 +00:00
Ok(())
2018-05-08 10:47:09 +00:00
}