diff --git a/API.md b/API.md index 815493b..8a246c3 100644 --- a/API.md +++ b/API.md @@ -238,8 +238,20 @@ struct js_event { uint8_t number; /* axis/button number */ } e; +/* + * Called on any joystick event + * + * Note: We handle errors here by exiting, for a background daemon with + * multiple watchers you may want another approach. + */ static void joystick_cb(uev_t *w, void *arg, int events) { + if (UEV_ERROR == events) { + /* Possibly joystick was unplugged */ + warn("Unrecoverable error, exiting"); + uev_exit(w->ctx); + } + read(w->fd, &e, sizeof(e)); switch (e.type) { diff --git a/examples/joystick.c b/examples/joystick.c index 8cf1d7f..9513b7e 100644 --- a/examples/joystick.c +++ b/examples/joystick.c @@ -37,8 +37,15 @@ struct js_event { uint8_t number; /* axis/button number */ } e; +/* + * Always check for UEV_ERROR in watcher callback! + * Possibly joystick was unplugged + */ static void joystick_cb(uev_t *w, void *arg, int events) { + if (UEV_ERROR == events) + errx(EBADF, "Unrecoverable error, exiting"); + if (read(w->fd, &e, sizeof(e)) < 0) errx(errno, "Failed reading joystick event");