anvil: explain the implementation a bit more

This commit is contained in:
Victor Brekenfeld 2018-12-05 16:00:17 +01:00
parent bbe767002d
commit 14d2e6e373
1 changed files with 26 additions and 10 deletions

View File

@ -132,6 +132,9 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
log.clone(), log.clone(),
).map_err(|_| ())?; ).map_err(|_| ())?;
/*
* Initialize wayland clipboard
*/
init_data_device( init_data_device(
&mut display.borrow_mut(), &mut display.borrow_mut(),
|_| {}, |_| {},
@ -139,6 +142,9 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
log.clone(), log.clone(),
); );
/*
* Initialize wayland input object
*/
let (mut w_seat, _) = Seat::new(&mut display.borrow_mut(), session.seat(), log.clone()); let (mut w_seat, _) = Seat::new(&mut display.borrow_mut(), session.seat(), log.clone());
let pointer = w_seat.add_pointer(); let pointer = w_seat.add_pointer();
@ -147,6 +153,9 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
set_data_device_focus(seat, focus.and_then(|s| s.client())) set_data_device_focus(seat, focus.and_then(|s| s.client()))
}).expect("Failed to initialize the keyboard"); }).expect("Failed to initialize the keyboard");
/*
* Initialize a fake output (we render one screen to every device in this example)
*/
let (output, _output_global) = Output::new( let (output, _output_global) = Output::new(
&mut display.borrow_mut(), &mut display.borrow_mut(),
"Drm".into(), "Drm".into(),
@ -194,6 +203,10 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
pointer_location, pointer_location,
session, session,
)); ));
/*
* Bind all our objects that get driven by the event loop
*/
let libinput_event_source = libinput_bind(libinput_backend, event_loop.handle()) let libinput_event_source = libinput_bind(libinput_backend, event_loop.handle())
.map_err(|e| -> IoError { e.into() }) .map_err(|e| -> IoError { e.into() })
.unwrap(); .unwrap();
@ -204,6 +217,9 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
.map_err(|e| -> IoError { e.into() }) .map_err(|e| -> IoError { e.into() })
.unwrap(); .unwrap();
/*
* And run our loop
*/
while running.load(Ordering::SeqCst) { while running.load(Ordering::SeqCst) {
if event_loop if event_loop
.dispatch(Some(::std::time::Duration::from_millis(16)), &mut ()) .dispatch(Some(::std::time::Duration::from_millis(16)), &mut ())
@ -216,6 +232,7 @@ pub fn run_udev(mut display: Display, mut event_loop: EventLoop<()>, log: Logger
} }
} }
// Cleanup stuff
window_map.borrow_mut().clear(); window_map.borrow_mut().clear();
let mut notifier = session_event_source.unbind(); let mut notifier = session_event_source.unbind();
@ -283,9 +300,7 @@ impl<S: SessionNotifier, Data: 'static> UdevHandlerImpl<S, Data> {
for crtc in res_handles.filter_crtcs(encoder_info.possible_crtcs()) { for crtc in res_handles.filter_crtcs(encoder_info.possible_crtcs()) {
if !backends.contains_key(&crtc) { if !backends.contains_key(&crtc) {
let renderer = GliumDrawer::init( let renderer = GliumDrawer::init(
device device.create_surface(crtc).unwrap(),
.create_surface(crtc)
.unwrap(),
egl_display.clone(), egl_display.clone(),
logger.clone(), logger.clone(),
); );
@ -329,12 +344,8 @@ impl<S: SessionNotifier, Data: 'static> UdevHandlerImpl<S, Data> {
for encoder_info in encoder_infos { for encoder_info in encoder_infos {
for crtc in res_handles.filter_crtcs(encoder_info.possible_crtcs()) { for crtc in res_handles.filter_crtcs(encoder_info.possible_crtcs()) {
if !backends.contains_key(&crtc) { if !backends.contains_key(&crtc) {
let renderer = GliumDrawer::init( let renderer =
device GliumDrawer::init(device.create_surface(crtc).unwrap(), logger.clone());
.create_surface(crtc)
.unwrap(),
logger.clone(),
);
backends.insert(crtc, renderer); backends.insert(crtc, renderer);
break; break;
@ -349,6 +360,7 @@ impl<S: SessionNotifier, Data: 'static> UdevHandlerImpl<S, Data> {
impl<S: SessionNotifier, Data: 'static> UdevHandler for UdevHandlerImpl<S, Data> { impl<S: SessionNotifier, Data: 'static> UdevHandler for UdevHandlerImpl<S, Data> {
fn device_added(&mut self, _device: dev_t, path: PathBuf) { fn device_added(&mut self, _device: dev_t, path: PathBuf) {
// Try to open the device
if let Some(mut device) = self if let Some(mut device) = self
.session .session
.open( .open(
@ -381,6 +393,9 @@ impl<S: SessionNotifier, Data: 'static> UdevHandler for UdevHandlerImpl<S, Data>
&self.logger, &self.logger,
))); )));
// Set the handler.
// Note: if you replicate this (very simple) structure, it is rather easy
// to introduce reference cycles with Rc. Be sure about your drop order
device.set_handler(DrmHandlerImpl { device.set_handler(DrmHandlerImpl {
compositor_token: self.compositor_token, compositor_token: self.compositor_token,
backends: backends.clone(), backends: backends.clone(),
@ -455,13 +470,13 @@ impl<S: SessionNotifier, Data: 'static> UdevHandler for UdevHandlerImpl<S, Data>
renderers.borrow_mut().clear(); renderers.borrow_mut().clear();
debug!(self.logger, "Surfaces dropped"); debug!(self.logger, "Surfaces dropped");
// don't use hardware acceleration anymore, if this was the primary gpu
let device = Rc::try_unwrap(evt_source.remove().unwrap()) let device = Rc::try_unwrap(evt_source.remove().unwrap())
.map_err(|_| "This should not happend") .map_err(|_| "This should not happend")
.unwrap() .unwrap()
.into_inner() .into_inner()
.0; .0;
// don't use hardware acceleration anymore, if this was the primary gpu
#[cfg(feature = "egl")] #[cfg(feature = "egl")]
{ {
if device.dev_path().and_then(|path| path.canonicalize().ok()) == self.primary_gpu { if device.dev_path().and_then(|path| path.canonicalize().ok()) == self.primary_gpu {
@ -495,6 +510,7 @@ impl DeviceHandler for DrmHandlerImpl {
.set_cursor_position(x.trunc().abs() as u32, y.trunc().abs() as u32); .set_cursor_position(x.trunc().abs() as u32, y.trunc().abs() as u32);
} }
// and draw in sync with our monitor
drawer.draw_windows(&*self.window_map.borrow(), self.compositor_token, &self.logger); drawer.draw_windows(&*self.window_map.borrow(), self.compositor_token, &self.logger);
} }
} }