Smithay/udev-rs

Monitor kernel events

Closed this issue · 2 comments

Hi,

Would you be open to changing the builder for supporting kernel event monitoring?
Though not sure what the best api changes would be as the current new() simply defaults to udev.

Example:

+pub(crate) enum EventSource {
+    Udev,
+    Kernel,
+}
+
 impl Builder {
-    /// Creates a new `Monitor`.
+    /// Creates a new udev event `Monitor`.
     pub fn new() -> Result<Self> {
         // Create a new Udev context for this monitor
         // It would be more efficient to allow callers to create just one context and use multiple
@@ -59,9 +69,27 @@ impl Builder {
         Self::with_udev(Udev::new()?)
     }
 
+    /// Creates a new kernel event `Monitor`.
+    pub fn new_kernel() -> Result<Self> {
+        Self::with_kernel(Udev::new()?)
+    }
+
     /// Creates a new `Monitor` using an existing `Udev` instance
     pub(crate) fn with_udev(udev: Udev) -> Result<Self> {
-        let name = b"udev\0".as_ptr() as *const libc::c_char;
+        Self::with_source(udev, EventSource::Udev)
+    }
+
+    /// Creates a new kernel event `Monitor` using an existing `Udev` instance.
+    pub(crate) fn with_kernel(udev: Udev) -> Result<Self> {
+        Self::with_source(udev, EventSource::Kernel)
+    }
+
+    /// Creates a `Monitor` for the given source, using an existing `Udev` instance.
+    pub(crate) fn with_source(udev: Udev, source: EventSource) -> Result<Self> {
+        let name = match source {
+            EventSource::Udev => b"udev\0".as_ptr() as *const libc::c_char,
+            EventSource::Kernel => b"kernel\0".as_ptr() as *const libc::c_char,
+        };

Your proposed patch and naming seems good to me. I am certainly open to add support to this, if you would open a PR.

Great, I shall open a PR, thank you