Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when not able to load cursor #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Renderer {
})
}

fn setup_cursor(&mut self, wl: &WaylandClient) {
fn setup_cursor(&mut self, wl: &WaylandClient) -> Result<(), String> {
let mut scale = 1;

for output in wl.output.outputs() {
Expand All @@ -177,9 +177,7 @@ impl Renderer {
)
.unwrap();
let cursor = cursor_theme
.get_cursor("default")
.expect("Could not load cursor, check XCURSOR_THEME")
.clone();
.get_cursor("default").ok_or(String::from("Could not load cursor, check XCURSOR_THEME"))?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just return if get_cursor is None, not of create a String that you then ignore. If you want to have an error message, use a log macro like warn!.


let cursor_surf = wl.compositor.create_surface(&wl.queue);
let cursor_img = &cursor[0];
Expand All @@ -191,16 +189,20 @@ impl Renderer {
cursor_surf.damage_buffer(0, 0, w as _, h as _);
cursor_surf.commit();
self.cursor_surf = Some(cursor_surf);

return Ok(());
}

pub fn set_cursor(&mut self, wl: &WaylandClient, mouse: &WlPointer, serial: u32) {
if self.cursor_surf.is_none() {
self.setup_cursor(wl);
}
if self.cursor_surf.is_some() {
let (x, y) = self.cursor_spot;
mouse.set_cursor(serial, self.cursor_surf.as_ref(), x, y);
}
match self.cursor_surf {
Some(_) => {
let (x, y) = self.cursor_spot;
mouse.set_cursor(serial, self.cursor_surf.as_ref(), x, y);
},
None => {
_ = self.setup_cursor(wl);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first time this function is run, it will fail to set the cursor even if the cursor is found. In fact, this function didn't need to change to address the error.

}
}
}
}

Expand Down