module Graphics.V4L2.Device
( Device()
, openDevice
, closeDevice
, withDevice
) where
import Control.Exception (bracket)
import Data.Bits (Bits)
import Data.Typeable (Typeable)
import Foreign.C.Error (throwErrnoIfMinus1, throwErrnoIfMinus1_)
import Foreign.C.String (withCString)
import Foreign.Storable (Storable)
import System.Posix.Types (Fd)
import Bindings.LibV4L2 (c'v4l2_open, c'v4l2_close)
import Bindings.Posix.Fcntl (c'O_RDWR)
newtype Device = Device Fd
deriving (Bits, Bounded, Enum, Eq, Integral, Num, Ord, Read, Real, Show, Storable, Typeable)
openDevice :: FilePath -> IO Device
openDevice f = withCString f $ \s -> do
h <- throwErrnoIfMinus1 "Graphics.V4L2.Device.openDevice" (c'v4l2_open s c'O_RDWR 0)
return (fromIntegral h)
closeDevice :: Device -> IO ()
closeDevice d = throwErrnoIfMinus1_ "Graphics.V4L2.Device.closeDevice" (c'v4l2_close (fromIntegral d))
withDevice :: FilePath -> (Device -> IO a) -> IO a
withDevice f = bracket (openDevice f) closeDevice