smithay/anvil/src/winit.rs

188 lines
5.6 KiB
Rust
Raw Normal View History

2018-10-02 21:37:24 +00:00
use std::{
cell::RefCell,
rc::Rc,
sync::{atomic::Ordering, Arc, Mutex},
time::Duration,
2018-10-02 21:37:24 +00:00
};
2017-06-11 21:03:25 +00:00
2018-10-02 21:37:24 +00:00
use smithay::{
2018-11-22 09:10:30 +00:00
backend::{egl::EGLGraphicsBackend, graphics::gl::GLGraphicsBackend, input::InputBackend, winit},
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::{
data_device::set_data_device_focus,
2018-10-02 21:37:24 +00:00
output::{Mode, Output, PhysicalProperties},
seat::{CursorImageStatus, Seat, XkbConfig},
SERIAL_COUNTER as SCOUNTER,
2018-10-02 21:37:24 +00:00
},
};
use slog::Logger;
use crate::buffer_utils::BufferUtils;
2018-12-15 20:32:28 +00:00
use crate::glium_drawer::GliumDrawer;
use crate::input_handler::AnvilInputHandler;
use crate::state::AnvilState;
2017-09-03 17:53:29 +00:00
2020-04-10 15:01:49 +00:00
pub fn run_winit(
display: Rc<RefCell<Display>>,
event_loop: &mut EventLoop<AnvilState>,
2020-04-10 15:01:49 +00:00
log: Logger,
) -> Result<(), ()> {
let (renderer, mut input) = winit::init(log.clone()).map_err(|_| ())?;
2017-03-07 10:53:57 +00:00
#[cfg(feature = "egl")]
let egl_buffer_reader = Rc::new(RefCell::new(
if let Ok(egl_buffer_reader) = renderer.bind_wl_display(&display.borrow()) {
info!(log, "EGL hardware-acceleration enabled");
Some(egl_buffer_reader)
} else {
None
2018-01-07 21:30:38 +00:00
},
));
2017-12-21 15:01:16 +00:00
2017-12-28 14:28:15 +00:00
let (w, h) = renderer.get_framebuffer_dimensions();
#[cfg(feature = "egl")]
let drawer = GliumDrawer::init(renderer, egl_buffer_reader.clone(), log.clone());
#[cfg(not(feature = "egl"))]
let drawer = GliumDrawer::init(renderer, log.clone());
2017-12-21 15:01:16 +00:00
#[cfg(feature = "egl")]
let buffer_utils = BufferUtils::new(egl_buffer_reader, log.clone());
#[cfg(not(feature = "egl"))]
let buffer_utils = BufferUtils::new(log.clone());
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(), buffer_utils, log.clone());
2018-11-18 18:39:33 +00:00
let (mut seat, _) = Seat::new(
&mut display.borrow_mut(),
"winit".into(),
state.ctoken,
log.clone(),
);
2017-09-22 12:57:11 +00:00
let cursor_status = Arc::new(Mutex::new(CursorImageStatus::Default));
let cursor_status2 = cursor_status.clone();
let pointer = seat.add_pointer(state.ctoken.clone(), move |new_status| {
// TODO: hide winit system cursor when relevant
*cursor_status2.lock().unwrap() = new_status
});
2018-11-18 18:39:33 +00:00
2018-09-24 22:32:09 +00:00
let keyboard = seat
.add_keyboard(XkbConfig::default(), 200, 25, |seat, focus| {
2019-02-22 21:50:46 +00:00
set_data_device_focus(seat, focus.and_then(|s| s.as_ref().client()))
2018-12-08 17:31:08 +00:00
})
.expect("Failed to initialize the keyboard");
2017-06-13 14:52:43 +00:00
2018-04-22 09:58:39 +00:00
let (output, _) = Output::new(
&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-04-22 09:58:39 +00:00
output.set_preferred(Mode {
width: w as i32,
height: h as i32,
refresh: 60_000,
});
let pointer_location = Rc::new(RefCell::new((0.0, 0.0)));
2018-05-08 10:47:09 +00:00
input.set_handler(AnvilInputHandler::new(
log.clone(),
2018-04-22 09:58:39 +00:00
pointer,
keyboard,
state.window_map.clone(),
2018-05-08 10:47:09 +00:00
(0, 0),
state.running.clone(),
pointer_location.clone(),
2018-05-08 10:47:09 +00:00
));
2017-09-22 12:57:11 +00:00
info!(log, "Initialization completed, starting the main loop.");
2017-03-07 10:53:57 +00:00
while state.running.load(Ordering::SeqCst) {
2018-04-22 09:58:39 +00:00
input.dispatch_new_events().unwrap();
// drawing logic
{
use glium::Surface;
let mut frame = drawer.draw();
frame.clear(None, Some((0.8, 0.8, 0.9, 1.0)), false, Some(1.0), None);
// draw the windows
drawer.draw_windows(&mut frame, &*state.window_map.borrow(), state.ctoken);
let (x, y) = *pointer_location.borrow();
// draw the dnd icon if any
{
let guard = state.dnd_icon.lock().unwrap();
if let Some(ref surface) = *guard {
2019-02-22 21:50:46 +00:00
if surface.as_ref().is_alive() {
drawer.draw_dnd_icon(&mut frame, surface, (x as i32, y as i32), state.ctoken);
}
}
}
// draw the cursor as relevant
{
let mut guard = 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 {
2019-02-22 21:50:46 +00:00
reset = !surface.as_ref().is_alive();
}
if reset {
*guard = CursorImageStatus::Default;
}
// draw as relevant
if let CursorImageStatus::Image(ref surface) = *guard {
drawer.draw_cursor(&mut frame, surface, (x as i32, y as i32), state.ctoken);
}
}
if let Err(err) = frame.finish() {
error!(log, "Error during rendering: {:?}", err);
}
}
// Send frame events so that client start drawing their next frame
state.window_map.borrow().send_frames(SCOUNTER.next_serial());
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();
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
}