Skip to content

Commit

Permalink
daemon: Avoid panicking when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
danyspin97 committed Apr 16, 2024
1 parent 4d07bc0 commit cfab904
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 27 deletions.
2 changes: 1 addition & 1 deletion daemon/src/display_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl DisplayInfo {
// let width = info.logical_size.unwrap().0;
// let height = info.logical_size.unwrap().1;
Self {
name: info.name.unwrap(),
name: info.name.unwrap_or_default(),
width: 0,
height: 0,
scale: info.scale_factor,
Expand Down
12 changes: 8 additions & 4 deletions daemon/src/render/egl_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ impl EglContext {

/// Resize the surface
/// Resizing the surface means to destroy the previous one and then recreate it
pub fn resize(&mut self, wl_surface: &WlSurface, width: i32, height: i32) {
egl.destroy_surface(self.display, self.surface).unwrap();
let wl_egl_surface = WlEglSurface::new(wl_surface.id(), width, height).unwrap();
pub fn resize(&mut self, wl_surface: &WlSurface, width: i32, height: i32) -> Result<()> {
egl.destroy_surface(self.display, self.surface)
.context("unable to destroy EGL surface")?;
let wl_egl_surface = WlEglSurface::new(wl_surface.id(), width, height)
.context("unable to create a new WlEglSurface")?;

let surface = unsafe {
egl.create_window_surface(
Expand All @@ -97,10 +99,12 @@ impl EglContext {
wl_egl_surface.ptr() as egl::NativeWindowType,
None,
)
.expect("unable to create an EGL surface")
.context("unable to create an EGL surface")?
};

self.surface = surface;
self.wl_egl_surface = wl_egl_surface;

Ok(())
}
}
3 changes: 2 additions & 1 deletion daemon/src/render/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl Renderer {
transition_time: u32,
) -> Result<Self> {
let gl = gl::Gl::load_with(|name| {
egl.get_proc_address(name).unwrap() as *const std::ffi::c_void
egl.get_proc_address(name)
.expect("egl.get_proc_address to work") as *const std::ffi::c_void
});
let vertex_shader = create_shader(&gl, gl::VERTEX_SHADER, VERTEX_SHADER_SOURCE)
.expect("vertex shader creation succeed");
Expand Down
7 changes: 5 additions & 2 deletions daemon/src/render/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ pub unsafe fn create_shader(
log.as_mut_ptr() as _,
);
gl_check!(gl, "calling GetShaderInfoLog");
let log = String::from_utf8(log).unwrap();
Err(color_eyre::eyre::anyhow!(log))
let res = String::from_utf8(log);
match res {
Ok(log) => Err(color_eyre::eyre::anyhow!(log)),
Err(err) => Err(color_eyre::eyre::anyhow!(err)),
}
} else {
Ok(shader)
}
Expand Down
43 changes: 36 additions & 7 deletions daemon/src/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ impl Surface {
let surface = layer.wl_surface().clone();
let egl_context = EglContext::new(egl_display, &surface);
// Make the egl context as current to make the renderer creation work
egl_context.make_current().unwrap();
egl_context
.make_current()
.expect("EGL context switching to work");

// Commit the surface
surface.commit();
Expand Down Expand Up @@ -150,7 +152,11 @@ impl Surface {
break true;
}
}
let (image_path, index) = self.loading_image.as_ref().unwrap().clone();
let (image_path, index) = self
.loading_image
.as_ref()
.expect("loading image to be set")
.clone();
let res = self
.image_loader
.borrow_mut()
Expand Down Expand Up @@ -226,10 +232,27 @@ impl Surface {
let width = info.adjusted_width();
let height = info.adjusted_height();
// self.layer.set_size(width as u32, height as u32);
self.egl_context.resize(&self.surface, width, height);
let display_name = self.name();
let res = self
.egl_context
.resize(&self.surface, width, height)
.with_context(|| {
format!("unable to switch resize EGL context for display {display_name}",)
})
.and_then(|_| {
self.egl_context.make_current().with_context(|| {
format!("unable to switch the openGL context for display {display_name}")
})
})
.and_then(|_| {
self.renderer.resize().with_context(|| {
format!("unable to resize the GL window for display {display_name}")
})
});
// Resize the gl viewport
self.egl_context.make_current().unwrap();
self.renderer.resize().unwrap();
if let Err(err) = res {
error!("{err:?}");
}
self.surface.frame(qh, self.surface.clone());
}

Expand Down Expand Up @@ -374,10 +397,16 @@ impl Surface {
.insert_source(
timer,
move |_deadline, _: &mut (), wpaperd: &mut Wpaperd| {
let surface = wpaperd
let surface = match wpaperd
.surface_from_name(&name)
.with_context(|| format!("expecting surface {name} to be available"))
.unwrap();
{
Ok(surface) => surface,
Err(err) => {
error!("{err:?}");
return TimeoutAction::Drop;
}
};
if let Some(duration) = surface.wallpaper_info.duration {
// Check that the timer has expired
// if the daemon received a next or previous image command
Expand Down
30 changes: 18 additions & 12 deletions daemon/src/wpaperd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,18 @@ impl OutputHandler for Wpaperd {
output: wl_output::WlOutput,
) {
// Find the destroyed output and remove it
self.surfaces.swap_remove(
self.surfaces
.iter()
.enumerate()
.find(|(_, surface)| surface.output == output)
.unwrap()
.0,
);

match self
.surfaces
.iter()
.enumerate()
.find(|(_, surface)| surface.output == output)
{
Some((index, _)) => {
self.surfaces.swap_remove(index);
}
None => error!("could not find display while handling output_destroyed"),
}
}
}

Expand All @@ -241,12 +245,14 @@ impl LayerShellHandler for Wpaperd {
configure: LayerSurfaceConfigure,
_serial: u32,
) {
self.surfaces
match self
.surfaces
.iter_mut()
.find(|surface| &surface.layer == layer)
// We always know the surface that it is being configured
.unwrap()
.change_size(configure, qh);
{
Some(surface) => surface.change_size(configure, qh),
None => error!("could not find display while handling configure in wayland"),
}
}
}

Expand Down

0 comments on commit cfab904

Please sign in to comment.