Commit ba1b279a authored by Camil Staps's avatar Camil Staps 🐧

Expose Inotify type itself in callbacks to allow adding/removing watches

parent 940a3f26
......@@ -8,11 +8,14 @@ from Data.Either import ::Either
from Data.Maybe import ::Maybe
/**
* An inotify file descriptor
*
* @var A state that is used for callbacks
* An inotify file descriptor.
* @var A state that can be used for application-specific information.
*/
:: *Inotify st
:: *Inotify st =
{ fd :: !Int
, watches :: ![(INWatch, INCallback st)]
, state :: !st
}
//* An inotify watch descriptor
:: INWatch
......@@ -28,7 +31,7 @@ from Data.Maybe import ::Maybe
*
* @var A state that is passed on.
*/
:: INCallback st :== INEvent (Maybe String) st *World -> *(st, *World)
:: INCallback st :== INEvent (Maybe String) *(Inotify st) -> *(*World -> *(*Inotify st, *World))
//* Bitwise OR for event masks
(|-) infixl 6 :: (INMask INMask -> INMask)
......
......@@ -17,12 +17,6 @@ import System._Posix
import code from "inotify_c.o"
:: *Inotify st =
{ fd :: !Int
, watches :: ![(INWatch, INCallback st)]
, state :: !st
}
:: INWatch :== Int
(|-) infixl 6 :: (INMask INMask -> INMask)
......@@ -87,21 +81,20 @@ where
inotify_check :: !*(Inotify st) !*World -> *(!*Inotify st, !*World)
inotify_check inot=:{fd,watches,state} w
# (ok, wds, masks, fnames, fd) = c_check fd
inot = { inot & fd=fd }
inot & fd = fd
| not ok = (inot, w)
| (size wds) rem 4 <> 0 || (size masks) rem 4 <> 0 = (inot,w)
# (wds,masks,fnames) = (split 4 wds, split 4 masks, splitOn '\0' fnames)
| length wds <> length masks = (inot, w)
# infos = zip3 (map bytesToInt wds) (map bytesToInt masks) fnames
# (fd,st,w`) = seq (map (check infos) watches) (inot.fd, state, w)
= ({ inot & fd=fd, state=st }, w`)
= seq (map (check infos) watches) (inot, w)
where
check :: [(Int,Int,String)] (INWatch, INCallback st) *(Int, st, *World)
-> *(Int, st, *World)
check infos (watch,f) (fd,st,w)
check :: [(Int,Int,String)] (INWatch, INCallback st) *(*Inotify st, *World)
-> *(*Inotify st, *World)
check infos (watch,f) (st,w)
# (st,w) = seq [\(st,w) -> f mask (toMaybe name) st w
\\ (wd,mask,name) <- infos | wd == watch] (st,w)
= (fd,st,w)
= (st,w)
where
toMaybe :: String -> Maybe String
toMaybe s = if (s=="") Nothing (Just s)
......
......@@ -26,12 +26,12 @@ Start w
# (inot, w) = inotify_loop_with_timeout (Just 10000) inot w
= inotify_close inot
where
echo :: String INEvent (Maybe String) () *World -> *((), *World)
echo fname ev f _ w
echo :: String INEvent (Maybe String) *(Inotify ()) *World -> *(*Inotify (), *World)
echo fname ev f st w
# (io,w) = stdio w
# io = io <<< "EVENT: ["<<< fname <<<"; "<<< ev <<<"; "<<< f <<<"]\n"
# (ok,w) = fclose io w
= ((), w)
= (st, w)
instance <<< (Maybe a) | <<< a
where
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment