From d9676bf1cfedac8930f781353c042a3434563850 Mon Sep 17 00:00:00 2001 From: Maxim Cournoyer Date: Tue, 19 Dec 2023 21:05:42 -0500 Subject: [PATCH] Allow the filter device type to be omitted, to match any device type. * libguile-udev/udev-monitor-func.c (udev-monitor-add-filter!): Adjust definition and doc to make the last argument, DEVTYPE, optional. * doc/guile-udev.texi (API Reference) : Document it. : Adjust documentation. * modules/udev/monitor.scm (make-udev-monitor): Add doc string. --- doc/guile-udev.texi | 12 ++++++++---- libguile-udev/udev-monitor-func.c | 15 ++++++++++----- modules/udev/monitor.scm | 11 ++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/doc/guile-udev.texi b/doc/guile-udev.texi index e435dbf..c6b3d01 100644 --- a/doc/guile-udev.texi +++ b/doc/guile-udev.texi @@ -198,8 +198,9 @@ Default value: @code{(const #f)} Udev event filter. Expected type: a list with two elements: the first element is the subsystem -name, while the second element is the device type. If needed, more filters -can be installed via the @code{udev-monitor-add-filter!} procedure. +name, while the second element is the device type. To match @emph{any} device +type, you may use @code{#f} instead of an actual device type. If needed, more +filters can be installed via the @code{udev-monitor-add-filter!} procedure. Default value: @code{#f} @@ -240,9 +241,12 @@ Check if @var{x} is an udev-monitor object. Return @code{#t} if it is, or @code{#f} otherwise. @end deffn -@deffn {Scheme Procedure} udev-monitor-add-filter! udev-monitor sybsystem devtype +@deffn {Scheme Procedure} udev-monitor-add-filter! udev-monitor subsystem [devtype] Add a new filter for the events on @var{udev-monitor} that will trigger the -provided callback. This procedure must be called before starting the scanning. +provided callback, for a device of type @var{devtype} belonging to the +@var{subsystem} subsystem. @var{devtype} is optional; when omitted, devices +of @emph{any} type are matched. This procedure must be called before starting +the scanning. @end deffn @deffn {Scheme Procedure} udev-monitor-remove-filters! udev-monitor diff --git a/libguile-udev/udev-monitor-func.c b/libguile-udev/udev-monitor-func.c index 4586653..94b3b4c 100644 --- a/libguile-udev/udev-monitor-func.c +++ b/libguile-udev/udev-monitor-func.c @@ -30,9 +30,11 @@ #include "udev-device-type.h" #include "error.h" -SCM_DEFINE_N(gudev_add_filter_x, "udev-monitor-add-filter!", 3, - (SCM udev_monitor, SCM subsystem, SCM devtype), - "Add a filter to the monitor.") +SCM_DEFINE(gudev_add_filter_x, "udev-monitor-add-filter!", 2, 1, 0, + (SCM udev_monitor, SCM subsystem, SCM devtype), + "Add a filter to the monitor. @var{subsystem} is the subsystem\n" + "associated with a device while @var{devtype} is its device type.\n" + "The @var{devtype} argument is optional.") #define FUNC_NAME s_gudev_add_filter_x { char* c_subsystem = NULL; @@ -41,14 +43,17 @@ SCM_DEFINE_N(gudev_add_filter_x, "udev-monitor-add-filter!", 3, int result; SCM_ASSERT(scm_is_string(subsystem), subsystem, SCM_ARG1, FUNC_NAME); - SCM_ASSERT(scm_is_string(devtype), devtype, SCM_ARG2, FUNC_NAME); + SCM_ASSERT(scm_is_string(devtype) || SCM_UNBNDP(devtype), devtype, + SCM_ARG2, FUNC_NAME); scm_dynwind_begin(0); c_subsystem = scm_to_locale_string(subsystem); scm_dynwind_free(c_subsystem); - c_devtype = scm_to_locale_string(devtype); + if (!SCM_UNBNDP(devtype)) + c_devtype = scm_to_locale_string(devtype); + scm_dynwind_free(c_devtype); result = udev_monitor_filter_add_match_subsystem_devtype(umd->udev_monitor, diff --git a/modules/udev/monitor.scm b/modules/udev/monitor.scm index 025cb5e..0fd640d 100644 --- a/modules/udev/monitor.scm +++ b/modules/udev/monitor.scm @@ -24,12 +24,21 @@ (filter #f) (timeout-usec 0) (timeout-sec 0)) + "Create a new 'udev-monitor' object configured with the specified parameters. +CALLBACK is a one argument procedure (receiving a 'udev-device' object) called +when an event matching the specified FILTER. FILTER is a list whose first +element is the device subsystem, and whose second argument is the device type, +or #f to match any type." (let ((monitor (%make-udev-monitor udev))) (udev-monitor-set-timeout! monitor timeout-sec timeout-usec) (udev-monitor-set-callback! monitor callback) (udev-monitor-set-error-callback! monitor callback) (when filter - (udev-monitor-add-filter! monitor (car filter) (cadr filter))) + (let ((subsystem (car filter)) + (devtype (cadr filter))) + (if devtype + (udev-monitor-add-filter! monitor subsystem devtype) + (udev-monitor-add-filter! monitor subsystem)))) monitor)) (load-extension "libguile-udev" "init_udev_monitor")