0 | module System.Linux.Inotify.Inotify
  1 |
  2 | import Data.C.Ptr
  3 | import Derive.Prelude
  4 | import System.Posix.File.FileDesc
  5 | import System.Posix.File.ReadRes
  6 | import System.Linux.Inotify.Flags
  7 |
  8 | %default total
  9 | %language ElabReflection
 10 |
 11 | --------------------------------------------------------------------------------
 12 | -- FFI
 13 | --------------------------------------------------------------------------------
 14 |
 15 | %foreign "C:li_inotify_more, linux-idris"
 16 | prim__inotify_more : AnyPtr -> AnyPtr -> Bits32 -> Bits32
 17 |
 18 | %foreign "C:li_inotify_next, linux-idris"
 19 | prim__inotify_next : AnyPtr -> AnyPtr
 20 |
 21 | %foreign "C:li_inotify_wd, linux-idris"
 22 | prim__inotify_wd : AnyPtr -> Bits32
 23 |
 24 | %foreign "C:li_inotify_mask, linux-idris"
 25 | prim__inotify_mask : AnyPtr -> Bits32
 26 |
 27 | %foreign "C:li_inotify_cookie, linux-idris"
 28 | prim__inotify_cookie : AnyPtr -> Bits32
 29 |
 30 | --------------------------------------------------------------------------------
 31 | -- Types
 32 | --------------------------------------------------------------------------------
 33 |
 34 | ||| An `inotify` file descriptor.
 35 | export
 36 | record Inotify where
 37 |   constructor I
 38 |   fd : Bits32
 39 |
 40 | export %inline
 41 | Cast Inotify Fd where cast = cast . fd
 42 |
 43 | export %inline
 44 | Cast CInt Inotify where cast = I . cast
 45 |
 46 | %runElab derive "Inotify" [Show,Eq,Ord]
 47 |
 48 | ||| An `inotify` file descriptor.
 49 | export
 50 | record Watch where
 51 |   constructor W
 52 |   wd : Bits32
 53 |
 54 | %runElab derive "Watch" [Show,Eq,Ord]
 55 |
 56 | export %inline
 57 | Interpolation Watch where interpolate = show . wd
 58 |
 59 | export %inline
 60 | Cast CInt Watch where cast = W . cast
 61 |
 62 | export %inline
 63 | Cast Watch Bits32 where cast = wd
 64 |
 65 | public export
 66 | record InotifyRes where
 67 |   constructor IR
 68 |   watch  : Watch
 69 |   mask   : InotifyMask
 70 |   cookie : Bits32
 71 |   name   : String
 72 |
 73 | %runElab derive "InotifyRes" [Show,Eq]
 74 |
 75 |
 76 | reslt : AnyPtr -> InotifyRes
 77 | reslt p =
 78 |   IR
 79 |     { watch  = W $ prim__inotify_wd p
 80 |     , mask   = IM $ prim__inotify_mask p
 81 |     , cookie = prim__inotify_cookie p
 82 |     , name   = ""
 83 |     }
 84 |
 85 | export
 86 | results : SnocList InotifyRes -> AnyPtr -> AnyPtr -> Bits32 -> List InotifyRes
 87 | results sx orig cur sz =
 88 |   case prim__inotify_more orig cur sz of
 89 |     0 => sx <>> []
 90 |     _ =>
 91 |       results
 92 |         (sx :< reslt cur)
 93 |         orig
 94 |         (assert_smaller cur $ prim__inotify_next cur)
 95 |         sz
 96 |
 97 | export
 98 | FromPtr (List InotifyRes) where
 99 |   fromPtr (CP sz p) t = results [<] p p sz # t
100 |
101 | export %inline
102 | FromBuf (List InotifyRes) where
103 |   fromBuf = viaPtrFromBuf
104 |