Skip to content

Commit

Permalink
Allow the filter device type to be omitted, to match any device type.
Browse files Browse the repository at this point in the history
* 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) <udev-monitor-add-filter!>: Document it.
<make-udev-monitor>: Adjust documentation.
* modules/udev/monitor.scm (make-udev-monitor): Add doc string.
  • Loading branch information
Apteryks committed Dec 20, 2023
1 parent dce6c60 commit d9676bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
12 changes: 8 additions & 4 deletions doc/guile-udev.texi
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions libguile-udev/udev-monitor-func.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion modules/udev/monitor.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit d9676bf

Please sign in to comment.