 
 : wd>monitor ( wd -- monitor ) watches at ;
 
-: <inotify> ( -- port )
+: <inotify> ( -- port/f )
     H{ } clone
-    inotify_init dup io-error inotify <buffered-port>
-    { set-inotify-watches set-delegate } inotify construct ;
+    inotify_init dup 0 < [ 2drop f ] [
+        inotify <buffered-port>
+        { set-inotify-watches set-delegate } inotify construct
+    ] if ;
 
 : inotify-fd inotify get-global port-handle ;
 

    f inotify-task <input-task> ;

: init-inotify ( mx -- )
    <inotify>
    dup inotify set-global
    <inotify-task> swap register-io-task ;

M: inotify-task do-io-task ( task -- )

    dup simple-monitor-handle watches delete-at
    simple-monitor-handle inotify-fd swap inotify_rm_watch io-error ;

: check-inotify
    inotify get [
        "inotify is not supported by this Linux release" throw
    ] unless ;

M: linux-io <monitor> ( path recursive? -- monitor )
    check-inotify
    drop IN_CHANGE_EVENTS add-watch ;

M: linux-monitor dispose ( monitor -- )
