2020-05-04 13:04:30 +00:00
|
|
|
use std::{cell::RefCell, rc::Rc, sync::atomic::Ordering, time::Duration};
|
2017-06-11 21:03:25 +00:00
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
//#[cfg(feature = "egl")]
|
|
|
|
//use smithay::backend::egl::EGLGraphicsBackend;
|
2018-10-02 21:37:24 +00:00
|
|
|
use smithay::{
|
2021-04-25 21:38:48 +00:00
|
|
|
backend::{renderer::Renderer, input::InputBackend, 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, Output, PhysicalProperties},
|
2020-05-04 13:04:30 +00:00
|
|
|
seat::CursorImageStatus,
|
2018-10-02 21:37:24 +00:00
|
|
|
},
|
|
|
|
};
|
2018-05-07 17:56:38 +00:00
|
|
|
|
|
|
|
use slog::Logger;
|
|
|
|
|
2020-04-21 17:42:03 +00:00
|
|
|
use crate::state::AnvilState;
|
2021-04-06 23:15:03 +00:00
|
|
|
use crate::buffer_utils::BufferUtils;
|
|
|
|
use crate::drawing::*;
|
2017-09-03 17:53:29 +00:00
|
|
|
|
2020-04-10 15:01:49 +00:00
|
|
|
pub fn run_winit(
|
2020-04-17 16:27:26 +00:00
|
|
|
display: Rc<RefCell<Display>>,
|
2020-04-21 16:56:59 +00:00
|
|
|
event_loop: &mut EventLoop<AnvilState>,
|
2020-04-10 15:01:49 +00:00
|
|
|
log: Logger,
|
|
|
|
) -> Result<(), ()> {
|
2021-04-06 23:15:03 +00:00
|
|
|
let (mut renderer, mut input) = winit::init(log.clone()).map_err(|err| {
|
2020-05-04 11:03:36 +00:00
|
|
|
slog::crit!(log, "Failed to initialize Winit backend: {}", err);
|
|
|
|
})?;
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2018-12-15 19:18:38 +00:00
|
|
|
#[cfg(feature = "egl")]
|
2020-04-15 20:19:20 +00:00
|
|
|
let egl_buffer_reader = Rc::new(RefCell::new(
|
2020-04-17 16:27:26 +00:00
|
|
|
if let Ok(egl_buffer_reader) = renderer.bind_wl_display(&display.borrow()) {
|
2018-01-05 19:04:46 +00:00
|
|
|
info!(log, "EGL hardware-acceleration enabled");
|
2020-04-15 20:19:20 +00:00
|
|
|
Some(egl_buffer_reader)
|
2018-01-05 19:04:46 +00:00
|
|
|
} else {
|
|
|
|
None
|
2018-01-07 21:30:38 +00:00
|
|
|
},
|
2018-01-05 19:04:46 +00:00
|
|
|
));
|
2017-12-21 15:01:16 +00:00
|
|
|
|
2020-02-03 05:42:12 +00:00
|
|
|
#[cfg(feature = "egl")]
|
2020-04-15 20:19:20 +00:00
|
|
|
let buffer_utils = BufferUtils::new(egl_buffer_reader, log.clone());
|
2020-02-03 05:42:12 +00:00
|
|
|
#[cfg(not(feature = "egl"))]
|
|
|
|
let buffer_utils = BufferUtils::new(log.clone());
|
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
let (w, h): (u32, u32) = renderer.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
|
|
|
|
2020-05-04 13:04:30 +00:00
|
|
|
let mut state = AnvilState::init(
|
|
|
|
display.clone(),
|
|
|
|
event_loop.handle(),
|
2021-04-26 19:44:40 +00:00
|
|
|
buffer_utils.clone(),
|
2020-05-04 13:04:30 +00:00
|
|
|
None,
|
2020-06-11 17:36:57 +00:00
|
|
|
None,
|
2020-04-17 16:27:26 +00:00
|
|
|
log.clone(),
|
|
|
|
);
|
2017-09-22 12:57:11 +00:00
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
let (output, _) = Output::new(
|
2020-04-17 16:27:26 +00:00
|
|
|
&mut display.borrow_mut(),
|
2017-10-01 17:14:25 +00:00
|
|
|
"Winit".into(),
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
log.clone(),
|
|
|
|
);
|
|
|
|
|
2018-04-22 09:58:39 +00:00
|
|
|
output.change_current_state(
|
|
|
|
Some(Mode {
|
2017-10-01 17:14:25 +00:00
|
|
|
width: w as i32,
|
|
|
|
height: h as i32,
|
|
|
|
refresh: 60_000,
|
2018-04-22 09:58:39 +00:00
|
|
|
}),
|
|
|
|
None,
|
|
|
|
None,
|
2018-01-12 14:49:55 +00:00
|
|
|
);
|
2018-04-22 09:58:39 +00:00
|
|
|
output.set_preferred(Mode {
|
|
|
|
width: w as i32,
|
|
|
|
height: h as i32,
|
|
|
|
refresh: 60_000,
|
|
|
|
});
|
|
|
|
|
2020-10-10 18:01:18 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
|
|
|
|
2018-05-07 17:56:38 +00:00
|
|
|
info!(log, "Initialization completed, starting the main loop.");
|
2017-03-07 10:53:57 +00:00
|
|
|
|
2020-04-17 16:27:26 +00:00
|
|
|
while state.running.load(Ordering::SeqCst) {
|
2021-04-06 23:15:03 +00:00
|
|
|
if input
|
2020-05-04 13:04:30 +00:00
|
|
|
.dispatch_new_events(|event, _| state.process_input_event(event))
|
2021-04-06 23:15:03 +00:00
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
state.running.store(false, Ordering::SeqCst);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-02 13:26:50 +00:00
|
|
|
|
2020-10-10 18:31:44 +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);
|
|
|
|
|
2018-12-10 08:18:00 +00:00
|
|
|
// drawing logic
|
|
|
|
{
|
2021-04-25 21:38:48 +00:00
|
|
|
renderer.begin().expect("Failed to render frame");
|
|
|
|
renderer.clear([0.8, 0.8, 0.9, 1.0]).expect("Failed to clear frame");
|
2018-12-10 08:18:00 +00:00
|
|
|
|
|
|
|
// draw the windows
|
2021-04-26 19:44:40 +00:00
|
|
|
draw_windows(&mut renderer, 0, &buffer_utils, &*state.window_map.borrow(), None, state.ctoken, &log);
|
2018-12-10 08:18:00 +00:00
|
|
|
|
2020-05-04 13:04:30 +00:00
|
|
|
let (x, y) = *state.pointer_location.borrow();
|
2018-12-10 08:18:00 +00:00
|
|
|
// draw the dnd icon if any
|
|
|
|
{
|
2020-04-21 16:56:59 +00:00
|
|
|
let guard = state.dnd_icon.lock().unwrap();
|
2018-12-10 08:18:00 +00:00
|
|
|
if let Some(ref surface) = *guard {
|
2019-02-22 21:50:46 +00:00
|
|
|
if surface.as_ref().is_alive() {
|
2021-04-26 19:44:40 +00:00
|
|
|
draw_dnd_icon(&mut renderer, 0, &buffer_utils, surface, (x as i32, y as i32), state.ctoken, &log);
|
2018-12-10 08:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// draw the cursor as relevant
|
|
|
|
{
|
2020-05-04 13:04:30 +00:00
|
|
|
let mut guard = state.cursor_status.lock().unwrap();
|
2018-12-10 08:18:00 +00:00
|
|
|
// reset the cursor if the surface is no longer alive
|
|
|
|
let mut reset = false;
|
|
|
|
if let CursorImageStatus::Image(ref surface) = *guard {
|
2019-02-22 21:50:46 +00:00
|
|
|
reset = !surface.as_ref().is_alive();
|
2018-12-10 08:18:00 +00:00
|
|
|
}
|
|
|
|
if reset {
|
|
|
|
*guard = CursorImageStatus::Default;
|
|
|
|
}
|
2021-04-06 23:15:03 +00:00
|
|
|
|
2018-12-10 08:18:00 +00:00
|
|
|
// draw as relevant
|
|
|
|
if let CursorImageStatus::Image(ref surface) = *guard {
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer.window().set_cursor_visible(false);
|
2021-04-26 19:44:40 +00:00
|
|
|
draw_cursor(&mut renderer, 0, &buffer_utils, surface, (x as i32, y as i32), state.ctoken, &log);
|
2020-06-27 21:59:10 +00:00
|
|
|
} else {
|
2021-04-06 23:15:03 +00:00
|
|
|
renderer.window().set_cursor_visible(true);
|
2018-12-10 08:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 21:38:48 +00:00
|
|
|
if let Err(SwapBuffersError::ContextLost(err)) = renderer.finish() {
|
2021-04-06 23:15:03 +00:00
|
|
|
error!(log, "Critical Rendering Error: {}", err);
|
|
|
|
state.running.store(false, Ordering::SeqCst);
|
2018-12-10 08:18:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-31 17:33:18 +00:00
|
|
|
|
2021-04-06 23:15:03 +00:00
|
|
|
|
2019-06-17 01:10:50 +00:00
|
|
|
if event_loop
|
2020-04-21 17:42:03 +00:00
|
|
|
.dispatch(Some(Duration::from_millis(16)), &mut state)
|
2019-06-17 01:10:50 +00:00
|
|
|
.is_err()
|
|
|
|
{
|
2020-04-17 16:27:26 +00:00
|
|
|
state.running.store(false, Ordering::SeqCst);
|
2019-06-17 01:10:50 +00:00
|
|
|
} else {
|
2020-04-17 16:27:26 +00:00
|
|
|
display.borrow_mut().flush_clients(&mut state);
|
2020-04-21 16:56:59 +00:00
|
|
|
state.window_map.borrow_mut().refresh();
|
2019-06-17 01:10:50 +00:00
|
|
|
}
|
2017-06-02 13:26:50 +00:00
|
|
|
}
|
2019-06-17 01:10:50 +00:00
|
|
|
|
|
|
|
// Cleanup stuff
|
2020-04-21 16:56:59 +00:00
|
|
|
state.window_map.borrow_mut().clear();
|
2019-06-17 01:10:50 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-05-08 10:47:09 +00:00
|
|
|
}
|