From 8a11565cc1da2868a0628b557f7b8edec8e478e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guido=20G=C3=BCnther?= Date: Wed, 11 Dec 2024 12:46:45 +0100 Subject: [PATCH] dev-vibra: Allow to check if the device is busy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We accept `NULL` to make the case of no haptic easy to handle. Signed-off-by: Guido Günther --- src/fbd-dev-vibra.c | 28 ++++++++++++++++++++++++++++ src/fbd-dev-vibra.h | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/fbd-dev-vibra.c b/src/fbd-dev-vibra.c index fc72f27..2e7584e 100644 --- a/src/fbd-dev-vibra.c +++ b/src/fbd-dev-vibra.c @@ -219,6 +219,7 @@ fbd_dev_vibra_class_init (FbdDevVibraClass *klass) static void fbd_dev_vibra_init (FbdDevVibra *self) { + self->id = -1; } FbdDevVibra * @@ -330,11 +331,16 @@ fbd_dev_vibra_remove_effect (FbdDevVibra *self) { g_return_val_if_fail (FBD_IS_DEV_VIBRA (self), FALSE); + if (self->id == -1) + return TRUE; + g_debug("Erasing vibra effect (%d)", self->fd); if (ioctl(self->fd, EVIOCRMFF, self->id) == -1) { g_warning ("Failed to erase vibra effect with id %d: %s", self->id, strerror(errno)); + self->id = -1; return FALSE; } + self->id = -1; return TRUE; } @@ -346,6 +352,9 @@ fbd_dev_vibra_stop (FbdDevVibra *self) g_return_val_if_fail (FBD_IS_DEV_VIBRA (self), FALSE); + if (self->id == -1) + return TRUE; + stop.type = EV_FF; stop.code = self->id; stop.value = 0; @@ -365,3 +374,22 @@ fbd_dev_vibra_get_device (FbdDevVibra *self) return self->device; } + +/** + * fbd_dev_vibra_is_busy: + * @self: The vibra device + * + * Check whether the device is currently in used + * + * Returns: `TRUE` when the device is in use, otherwise `FALSE` + */ +gboolean +fbd_dev_vibra_is_busy (FbdDevVibra *self) +{ + if (self == NULL) + return FALSE; + + g_return_val_if_fail (FBD_IS_DEV_VIBRA (self), TRUE); + + return self->id != -1; +} diff --git a/src/fbd-dev-vibra.h b/src/fbd-dev-vibra.h index 55c03c6..a653d63 100644 --- a/src/fbd-dev-vibra.h +++ b/src/fbd-dev-vibra.h @@ -21,6 +21,6 @@ gboolean fbd_dev_vibra_periodic (FbdDevVibra *self, guint duration, guint ma gboolean fbd_dev_vibra_stop (FbdDevVibra *self); gboolean fbd_dev_vibra_remove_effect (FbdDevVibra *self); GUdevDevice *fbd_dev_vibra_get_device(FbdDevVibra *self); - +gboolean fbd_dev_vibra_is_busy (FbdDevVibra *self); G_END_DECLS