diff options
author | Camil Staps | 2020-01-06 16:27:27 +0100 |
---|---|---|
committer | Camil Staps | 2020-01-06 16:27:27 +0100 |
commit | ba1b279a40c6b491ccd352bf22b888ae7dfbad97 (patch) | |
tree | af08dec7afefa240e94a3c0f7bd01d3aaa24acec | |
parent | Cleanup example (diff) |
Expose Inotify type itself in callbacks to allow adding/removing watches
-rw-r--r-- | Inotify.dcl | 13 | ||||
-rw-r--r-- | Inotify.icl | 19 | ||||
-rw-r--r-- | test.icl | 6 |
3 files changed, 17 insertions, 21 deletions
diff --git a/Inotify.dcl b/Inotify.dcl index e6893e2..611e616 100644 --- a/Inotify.dcl +++ b/Inotify.dcl @@ -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) diff --git a/Inotify.icl b/Inotify.icl index acf56b9..fcd1d60 100644 --- a/Inotify.icl +++ b/Inotify.icl @@ -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 |