{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module System.UDev.Device
( Device
, Devnum
, Action (..)
, newFromSysPath
, newFromDevnum
, newFromSubsystemSysname
, newFromDeviceId
, newFromEnvironment
, getParent
, getParentWithSubsystemDevtype
, getDevpath
, getSubsystem
, getDevtype
, getSyspath
, getSysname
, getSysnum
, getDevnode
, isInitialized
, getDevlinksListEntry
, getPropertiesListEntry
, getTagsListEntry
, getPropertyValue
, getDriver
, getDevnum
, getAction
, getSysattrValue
, setSysattrValue
, getSysattrListEntry
, getSeqnum
, getUsecSinceInitialized
, hasTag
) where
import Control.Applicative
import Data.Bits
import Data.ByteString as BS
import Foreign hiding (unsafePerformIO)
import Foreign.C
import System.IO.Unsafe
import System.Posix.FilePath
import System.UDev.Context
import System.UDev.List
import System.UDev.Types
foreign import ccall unsafe "udev_device_new_from_syspath"
c_newFromSysPath :: UDev -> CString -> IO Device
newFromSysPath :: UDev -> RawFilePath -> IO Device
newFromSysPath :: UDev -> RawFilePath -> IO Device
newFromSysPath UDev
udev RawFilePath
sysPath =
Ptr Device -> Device
Device (Ptr Device -> Device) -> IO (Ptr Device) -> IO Device
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO (Ptr Device) -> IO (Ptr Device)
forall a. String -> IO (Ptr a) -> IO (Ptr a)
throwErrnoIfNull String
"newFromSysPath"
(RawFilePath -> (CString -> IO (Ptr Device)) -> IO (Ptr Device)
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
sysPath ((CString -> IO (Ptr Device)) -> IO (Ptr Device))
-> (CString -> IO (Ptr Device)) -> IO (Ptr Device)
forall a b. (a -> b) -> a -> b
$ \ CString
c_sysPath ->
Device -> Ptr Device
getDevice (Device -> Ptr Device) -> IO Device -> IO (Ptr Device)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UDev -> CString -> IO Device
c_newFromSysPath UDev
udev CString
c_sysPath)
type Dev_t = CULong
foreign import ccall unsafe "udev_device_new_from_devnum"
c_newFromDevnum :: UDev -> CChar -> Dev_t -> IO Device
data Devnum = Devnum
{ Devnum -> Int
major :: {-# UNPACK #-} !Int
, Devnum -> Int
minor :: {-# UNPACK #-} !Int
} deriving (Int -> Devnum -> ShowS
[Devnum] -> ShowS
Devnum -> String
(Int -> Devnum -> ShowS)
-> (Devnum -> String) -> ([Devnum] -> ShowS) -> Show Devnum
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Devnum] -> ShowS
$cshowList :: [Devnum] -> ShowS
show :: Devnum -> String
$cshow :: Devnum -> String
showsPrec :: Int -> Devnum -> ShowS
$cshowsPrec :: Int -> Devnum -> ShowS
Show, ReadPrec [Devnum]
ReadPrec Devnum
Int -> ReadS Devnum
ReadS [Devnum]
(Int -> ReadS Devnum)
-> ReadS [Devnum]
-> ReadPrec Devnum
-> ReadPrec [Devnum]
-> Read Devnum
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Devnum]
$creadListPrec :: ReadPrec [Devnum]
readPrec :: ReadPrec Devnum
$creadPrec :: ReadPrec Devnum
readList :: ReadS [Devnum]
$creadList :: ReadS [Devnum]
readsPrec :: Int -> ReadS Devnum
$creadsPrec :: Int -> ReadS Devnum
Read, Devnum -> Devnum -> Bool
(Devnum -> Devnum -> Bool)
-> (Devnum -> Devnum -> Bool) -> Eq Devnum
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Devnum -> Devnum -> Bool
$c/= :: Devnum -> Devnum -> Bool
== :: Devnum -> Devnum -> Bool
$c== :: Devnum -> Devnum -> Bool
Eq, Eq Devnum
Eq Devnum
-> (Devnum -> Devnum -> Ordering)
-> (Devnum -> Devnum -> Bool)
-> (Devnum -> Devnum -> Bool)
-> (Devnum -> Devnum -> Bool)
-> (Devnum -> Devnum -> Bool)
-> (Devnum -> Devnum -> Devnum)
-> (Devnum -> Devnum -> Devnum)
-> Ord Devnum
Devnum -> Devnum -> Bool
Devnum -> Devnum -> Ordering
Devnum -> Devnum -> Devnum
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Devnum -> Devnum -> Devnum
$cmin :: Devnum -> Devnum -> Devnum
max :: Devnum -> Devnum -> Devnum
$cmax :: Devnum -> Devnum -> Devnum
>= :: Devnum -> Devnum -> Bool
$c>= :: Devnum -> Devnum -> Bool
> :: Devnum -> Devnum -> Bool
$c> :: Devnum -> Devnum -> Bool
<= :: Devnum -> Devnum -> Bool
$c<= :: Devnum -> Devnum -> Bool
< :: Devnum -> Devnum -> Bool
$c< :: Devnum -> Devnum -> Bool
compare :: Devnum -> Devnum -> Ordering
$ccompare :: Devnum -> Devnum -> Ordering
$cp1Ord :: Eq Devnum
Ord)
nrToDevnum :: Dev_t -> Devnum
nrToDevnum :: Dev_t -> Devnum
nrToDevnum Dev_t
x = Devnum :: Int -> Int -> Devnum
Devnum
{ major :: Int
major = Dev_t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Dev_t
x Dev_t -> Int -> Dev_t
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
8) Dev_t -> Dev_t -> Dev_t
forall a. Bits a => a -> a -> a
.&. Dev_t
0xff)
, minor :: Int
minor = Dev_t -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Dev_t
x Dev_t -> Dev_t -> Dev_t
forall a. Bits a => a -> a -> a
.&. Dev_t
0xff)
}
{-# INLINE nrToDevnum #-}
devnumToNr :: Devnum -> Dev_t
devnumToNr :: Devnum -> Dev_t
devnumToNr Devnum {Int
minor :: Int
major :: Int
minor :: Devnum -> Int
major :: Devnum -> Int
..}
= Int -> Dev_t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (((Int
major Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xff) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8)
Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. (Int
minor Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xff))
{-# INLINE devnumToNr #-}
newFromDevnum :: UDev -> Char -> Devnum -> IO Device
newFromDevnum :: UDev -> Char -> Devnum -> IO Device
newFromDevnum UDev
udev Char
char Devnum
devnum
= UDev -> CChar -> Dev_t -> IO Device
c_newFromDevnum UDev
udev (Int -> CChar
forall a. Enum a => Int -> a
toEnum (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
char)) (Devnum -> Dev_t
devnumToNr Devnum
devnum)
{-# INLINE newFromDevnum #-}
foreign import ccall unsafe "udev_device_new_from_subsystem_sysname"
c_newFromSubsystemSysname :: UDev -> CString -> CString -> IO Device
newFromSubsystemSysname :: UDev -> ByteString -> ByteString -> IO Device
newFromSubsystemSysname :: UDev -> RawFilePath -> RawFilePath -> IO Device
newFromSubsystemSysname UDev
udev RawFilePath
subsystem RawFilePath
sysname =
RawFilePath -> (CString -> IO Device) -> IO Device
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
subsystem ((CString -> IO Device) -> IO Device)
-> (CString -> IO Device) -> IO Device
forall a b. (a -> b) -> a -> b
$ \ CString
c_subsystem ->
RawFilePath -> (CString -> IO Device) -> IO Device
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
sysname ((CString -> IO Device) -> IO Device)
-> (CString -> IO Device) -> IO Device
forall a b. (a -> b) -> a -> b
$ \ CString
c_sysname ->
UDev -> CString -> CString -> IO Device
c_newFromSubsystemSysname UDev
udev CString
c_subsystem CString
c_sysname
foreign import ccall unsafe "udev_device_new_from_device_id"
c_newFromDeviceId :: UDev -> CString -> IO Device
newFromDeviceId :: UDev -> ByteString -> IO Device
newFromDeviceId :: UDev -> RawFilePath -> IO Device
newFromDeviceId UDev
udev RawFilePath
devId =
RawFilePath -> (CString -> IO Device) -> IO Device
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
devId ((CString -> IO Device) -> IO Device)
-> (CString -> IO Device) -> IO Device
forall a b. (a -> b) -> a -> b
$ \ CString
c_devId ->
UDev -> CString -> IO Device
c_newFromDeviceId UDev
udev CString
c_devId
foreign import ccall unsafe "udev_device_new_from_environment"
c_newFromEnvironment :: UDev -> IO Device
newFromEnvironment :: UDev -> IO Device
newFromEnvironment :: UDev -> IO Device
newFromEnvironment = UDev -> IO Device
c_newFromEnvironment
foreign import ccall unsafe "udev_device_get_parent"
c_getParent :: Device -> IO Device
getParent :: Device -> IO Device
getParent :: Device -> IO Device
getParent = Device -> IO Device
c_getParent
foreign import ccall unsafe "udev_device_get_parent_with_subsystem_devtype"
c_getParentWithSubsystemDevtype :: Device -> CString -> CString
-> IO Device
getParentWithSubsystemDevtype :: Device -> ByteString -> ByteString
-> IO (Maybe Device)
getParentWithSubsystemDevtype :: Device -> RawFilePath -> RawFilePath -> IO (Maybe Device)
getParentWithSubsystemDevtype Device
udev RawFilePath
subsystem RawFilePath
devtype = do
Device
mdev <- RawFilePath -> (CString -> IO Device) -> IO Device
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
subsystem ((CString -> IO Device) -> IO Device)
-> (CString -> IO Device) -> IO Device
forall a b. (a -> b) -> a -> b
$ \ CString
c_subsystem ->
RawFilePath -> (CString -> IO Device) -> IO Device
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
devtype ((CString -> IO Device) -> IO Device)
-> (CString -> IO Device) -> IO Device
forall a b. (a -> b) -> a -> b
$ \ CString
c_devtype ->
Device -> CString -> CString -> IO Device
c_getParentWithSubsystemDevtype Device
udev CString
c_subsystem CString
c_devtype
Maybe Device -> IO (Maybe Device)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Device -> IO (Maybe Device))
-> Maybe Device -> IO (Maybe Device)
forall a b. (a -> b) -> a -> b
$ if Device -> Ptr Device
getDevice Device
mdev Ptr Device -> Ptr Device -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Device
forall a. Ptr a
nullPtr then Maybe Device
forall a. Maybe a
Nothing else Device -> Maybe Device
forall a. a -> Maybe a
Just Device
mdev
foreign import ccall unsafe "udev_device_get_devpath"
c_getDevpath :: Device -> IO CString
getDevpath :: Device -> RawFilePath
getDevpath :: Device -> RawFilePath
getDevpath Device
dev = IO RawFilePath -> RawFilePath
forall a. IO a -> a
unsafePerformIO (IO RawFilePath -> RawFilePath) -> IO RawFilePath -> RawFilePath
forall a b. (a -> b) -> a -> b
$
CString -> IO RawFilePath
packCString (CString -> IO RawFilePath) -> IO CString -> IO RawFilePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getDevpath Device
dev
foreign import ccall unsafe "udev_device_get_subsystem"
c_getSubsystem :: Device -> IO CString
packCStringMaybe :: CString -> IO (Maybe ByteString)
packCStringMaybe :: CString -> IO (Maybe RawFilePath)
packCStringMaybe CString
cstring =
if CString
cstring CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
== CString
forall a. Ptr a
nullPtr
then Maybe RawFilePath -> IO (Maybe RawFilePath)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RawFilePath
forall a. Maybe a
Nothing
else RawFilePath -> Maybe RawFilePath
forall a. a -> Maybe a
Just (RawFilePath -> Maybe RawFilePath)
-> IO RawFilePath -> IO (Maybe RawFilePath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CString -> IO RawFilePath
packCString CString
cstring
getSubsystem :: Device -> Maybe ByteString
getSubsystem :: Device -> Maybe RawFilePath
getSubsystem Device
dev = IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a. IO a -> a
unsafePerformIO (IO (Maybe RawFilePath) -> Maybe RawFilePath)
-> IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO (Maybe RawFilePath)
packCStringMaybe (CString -> IO (Maybe RawFilePath))
-> IO CString -> IO (Maybe RawFilePath)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getSubsystem Device
dev
foreign import ccall unsafe "udev_device_get_devtype"
c_getDevtype :: Device -> IO CString
getDevtype :: Device -> Maybe ByteString
getDevtype :: Device -> Maybe RawFilePath
getDevtype Device
dev = IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a. IO a -> a
unsafePerformIO (IO (Maybe RawFilePath) -> Maybe RawFilePath)
-> IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO (Maybe RawFilePath)
packCStringMaybe (CString -> IO (Maybe RawFilePath))
-> IO CString -> IO (Maybe RawFilePath)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getDevtype Device
dev
foreign import ccall unsafe "udev_device_get_syspath"
c_getSyspath :: Device -> IO CString
getSyspath :: Device -> RawFilePath
getSyspath :: Device -> RawFilePath
getSyspath Device
dev = IO RawFilePath -> RawFilePath
forall a. IO a -> a
unsafePerformIO (IO RawFilePath -> RawFilePath) -> IO RawFilePath -> RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO RawFilePath
packCString (CString -> IO RawFilePath) -> IO CString -> IO RawFilePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getSyspath Device
dev
foreign import ccall unsafe "udev_device_get_sysname"
c_getSysname :: Device -> IO CString
getSysname :: Device -> ByteString
getSysname :: Device -> RawFilePath
getSysname Device
dev = IO RawFilePath -> RawFilePath
forall a. IO a -> a
unsafePerformIO (IO RawFilePath -> RawFilePath) -> IO RawFilePath -> RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO RawFilePath
packCString (CString -> IO RawFilePath) -> IO CString -> IO RawFilePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getSysname Device
dev
foreign import ccall unsafe "udev_device_get_sysnum"
c_getSysnum :: Device -> IO CString
getSysnum :: Device -> Maybe ByteString
getSysnum :: Device -> Maybe RawFilePath
getSysnum Device
dev = IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a. IO a -> a
unsafePerformIO (IO (Maybe RawFilePath) -> Maybe RawFilePath)
-> IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO (Maybe RawFilePath)
packCStringMaybe (CString -> IO (Maybe RawFilePath))
-> IO CString -> IO (Maybe RawFilePath)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getSysnum Device
dev
foreign import ccall unsafe "udev_device_get_devnode"
c_getDevnode :: Device -> IO CString
getDevnode :: Device -> Maybe ByteString
getDevnode :: Device -> Maybe RawFilePath
getDevnode Device
udev = IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a. IO a -> a
unsafePerformIO (IO (Maybe RawFilePath) -> Maybe RawFilePath)
-> IO (Maybe RawFilePath) -> Maybe RawFilePath
forall a b. (a -> b) -> a -> b
$ CString -> IO (Maybe RawFilePath)
packCStringMaybe (CString -> IO (Maybe RawFilePath))
-> IO CString -> IO (Maybe RawFilePath)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getDevnode Device
udev
foreign import ccall unsafe "udev_device_get_is_initialized"
c_isInitialized :: Device -> IO CInt
isInitialized :: Device -> IO Bool
isInitialized :: Device -> IO Bool
isInitialized Device
dev = (CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
< CInt
0) (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Device -> IO CInt
c_isInitialized Device
dev
foreign import ccall unsafe "udev_device_get_devlinks_list_entry"
c_getDevlinksListEntry :: Device -> IO List
getDevlinksListEntry :: Device -> IO List
getDevlinksListEntry :: Device -> IO List
getDevlinksListEntry = Device -> IO List
c_getDevlinksListEntry
{-# INLINE getDevlinksListEntry #-}
foreign import ccall unsafe "udev_device_get_properties_list_entry"
c_getPropertiesListEntry :: Device -> IO List
getPropertiesListEntry :: Device -> IO List
getPropertiesListEntry :: Device -> IO List
getPropertiesListEntry = Device -> IO List
c_getPropertiesListEntry
{-# INLINE getPropertiesListEntry #-}
foreign import ccall unsafe "udev_device_get_tags_list_entry"
c_getTagsListEntry :: Device -> IO List
getTagsListEntry :: Device -> IO List
getTagsListEntry :: Device -> IO List
getTagsListEntry = Device -> IO List
c_getTagsListEntry
{-# INLINE getTagsListEntry #-}
foreign import ccall unsafe "udev_device_get_property_value"
c_getPropertyValue :: Device -> CString -> IO CString
getPropertyValue :: Device -> ByteString -> IO (Maybe ByteString)
getPropertyValue :: Device -> RawFilePath -> IO (Maybe RawFilePath)
getPropertyValue Device
dev RawFilePath
prop = do
CString
res <- RawFilePath -> (CString -> IO CString) -> IO CString
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
prop ((CString -> IO CString) -> IO CString)
-> (CString -> IO CString) -> IO CString
forall a b. (a -> b) -> a -> b
$ \ CString
c_prop ->
Device -> CString -> IO CString
c_getPropertyValue Device
dev CString
c_prop
if CString
res CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
== CString
forall a. Ptr a
nullPtr then Maybe RawFilePath -> IO (Maybe RawFilePath)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RawFilePath
forall a. Maybe a
Nothing else RawFilePath -> Maybe RawFilePath
forall a. a -> Maybe a
Just (RawFilePath -> Maybe RawFilePath)
-> IO RawFilePath -> IO (Maybe RawFilePath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CString -> IO RawFilePath
packCString CString
res
foreign import ccall unsafe "udev_device_get_driver"
c_getDriver :: Device -> IO CString
getDriver :: Device -> ByteString
getDriver :: Device -> RawFilePath
getDriver Device
dev = IO RawFilePath -> RawFilePath
forall a. IO a -> a
unsafePerformIO (IO RawFilePath -> RawFilePath) -> IO RawFilePath -> RawFilePath
forall a b. (a -> b) -> a -> b
$
CString -> IO RawFilePath
packCString (CString -> IO RawFilePath) -> IO CString -> IO RawFilePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Device -> IO CString
c_getDriver Device
dev
foreign import ccall unsafe "udev_device_get_devnum"
c_getDevnum :: Device -> Dev_t
getDevnum :: Device -> Devnum
getDevnum :: Device -> Devnum
getDevnum = Dev_t -> Devnum
nrToDevnum (Dev_t -> Devnum) -> (Device -> Dev_t) -> Device -> Devnum
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Device -> Dev_t
c_getDevnum
{-# INLINE getDevnum #-}
foreign import ccall unsafe "udev_device_get_action"
c_getAction :: Device -> CString
data Action = Add
| Remove
| Change
| Online
| Offline
| Other ByteString
deriving (Int -> Action -> ShowS
[Action] -> ShowS
Action -> String
(Int -> Action -> ShowS)
-> (Action -> String) -> ([Action] -> ShowS) -> Show Action
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Action] -> ShowS
$cshowList :: [Action] -> ShowS
show :: Action -> String
$cshow :: Action -> String
showsPrec :: Int -> Action -> ShowS
$cshowsPrec :: Int -> Action -> ShowS
Show, ReadPrec [Action]
ReadPrec Action
Int -> ReadS Action
ReadS [Action]
(Int -> ReadS Action)
-> ReadS [Action]
-> ReadPrec Action
-> ReadPrec [Action]
-> Read Action
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Action]
$creadListPrec :: ReadPrec [Action]
readPrec :: ReadPrec Action
$creadPrec :: ReadPrec Action
readList :: ReadS [Action]
$creadList :: ReadS [Action]
readsPrec :: Int -> ReadS Action
$creadsPrec :: Int -> ReadS Action
Read, Action -> Action -> Bool
(Action -> Action -> Bool)
-> (Action -> Action -> Bool) -> Eq Action
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Action -> Action -> Bool
$c/= :: Action -> Action -> Bool
== :: Action -> Action -> Bool
$c== :: Action -> Action -> Bool
Eq, Eq Action
Eq Action
-> (Action -> Action -> Ordering)
-> (Action -> Action -> Bool)
-> (Action -> Action -> Bool)
-> (Action -> Action -> Bool)
-> (Action -> Action -> Bool)
-> (Action -> Action -> Action)
-> (Action -> Action -> Action)
-> Ord Action
Action -> Action -> Bool
Action -> Action -> Ordering
Action -> Action -> Action
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Action -> Action -> Action
$cmin :: Action -> Action -> Action
max :: Action -> Action -> Action
$cmax :: Action -> Action -> Action
>= :: Action -> Action -> Bool
$c>= :: Action -> Action -> Bool
> :: Action -> Action -> Bool
$c> :: Action -> Action -> Bool
<= :: Action -> Action -> Bool
$c<= :: Action -> Action -> Bool
< :: Action -> Action -> Bool
$c< :: Action -> Action -> Bool
compare :: Action -> Action -> Ordering
$ccompare :: Action -> Action -> Ordering
$cp1Ord :: Eq Action
Ord)
marshalAction :: ByteString -> Action
marshalAction :: RawFilePath -> Action
marshalAction RawFilePath
"add" = Action
Add
marshalAction RawFilePath
"remove" = Action
Remove
marshalAction RawFilePath
"change" = Action
Remove
marshalAction RawFilePath
"online" = Action
Online
marshalAction RawFilePath
"offline" = Action
Offline
marshalAction RawFilePath
action = RawFilePath -> Action
Other RawFilePath
action
getAction :: Device -> Maybe Action
getAction :: Device -> Maybe Action
getAction Device
dev
| CString
c_action CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
== CString
forall a. Ptr a
nullPtr = Maybe Action
forall a. Maybe a
Nothing
| Bool
otherwise = Action -> Maybe Action
forall a. a -> Maybe a
Just (Action -> Maybe Action) -> Action -> Maybe Action
forall a b. (a -> b) -> a -> b
$ IO Action -> Action
forall a. IO a -> a
unsafePerformIO (IO Action -> Action) -> IO Action -> Action
forall a b. (a -> b) -> a -> b
$
RawFilePath -> Action
marshalAction (RawFilePath -> Action) -> IO RawFilePath -> IO Action
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CString -> IO RawFilePath
packCString CString
c_action
where
c_action :: CString
c_action = Device -> CString
c_getAction Device
dev
foreign import ccall unsafe "udev_device_get_sysattr_value"
c_getSysattrValue :: Device -> CString -> CString
getSysattrValue :: Device -> ByteString -> IO ByteString
getSysattrValue :: Device -> RawFilePath -> IO RawFilePath
getSysattrValue Device
dev RawFilePath
sysattr =
CString -> IO RawFilePath
packCString (CString -> IO RawFilePath) -> IO CString -> IO RawFilePath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< RawFilePath -> (CString -> IO CString) -> IO CString
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
sysattr (CString -> IO CString
forall (m :: * -> *) a. Monad m => a -> m a
return (CString -> IO CString)
-> (CString -> CString) -> CString -> IO CString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Device -> CString -> CString
c_getSysattrValue Device
dev)
foreign import ccall unsafe "udev_device_set_sysattr_value"
c_setSysattrValue :: Device -> CString -> CString -> IO CInt
setSysattrValue :: Device
-> ByteString
-> ByteString
-> IO ()
setSysattrValue :: Device -> RawFilePath -> RawFilePath -> IO ()
setSysattrValue Device
dev RawFilePath
sysattr RawFilePath
value =
(CInt -> Bool) -> String -> IO CInt -> IO ()
forall a. (a -> Bool) -> String -> IO a -> IO ()
throwErrnoIf_ (CInt
0 CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
<) String
"setSysattrValue" (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$
RawFilePath -> (CString -> IO CInt) -> IO CInt
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
sysattr ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
c_sysattr ->
RawFilePath -> (CString -> IO CInt) -> IO CInt
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
value ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
c_value ->
Device -> CString -> CString -> IO CInt
c_setSysattrValue Device
dev CString
c_sysattr CString
c_value
foreign import ccall unsafe "udev_device_get_sysattr_list_entry"
c_getSysAttrListEntry :: Device -> IO List
getSysattrListEntry :: Device -> IO List
getSysattrListEntry :: Device -> IO List
getSysattrListEntry = Device -> IO List
c_getSysAttrListEntry
{-# INLINE getSysattrListEntry #-}
toMaybe :: CULLong -> Maybe Int
toMaybe :: CULLong -> Maybe Int
toMaybe CULLong
0 = Maybe Int
forall a. Maybe a
Nothing
toMaybe CULLong
n = Int -> Maybe Int
forall a. a -> Maybe a
Just (CULLong -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CULLong
n)
{-# INLINE toMaybe #-}
foreign import ccall unsafe "udev_device_get_seqnum"
c_getSeqnum :: Device -> IO CULLong
getSeqnum :: Device -> IO (Maybe Int)
getSeqnum :: Device -> IO (Maybe Int)
getSeqnum Device
dev = CULLong -> Maybe Int
toMaybe (CULLong -> Maybe Int) -> IO CULLong -> IO (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Device -> IO CULLong
c_getSeqnum Device
dev
{-# INLINE getSeqnum #-}
foreign import ccall unsafe "udev_device_get_usec_since_initialized"
c_getUsecSinceInitialized :: Device -> IO CULLong
getUsecSinceInitialized :: Device -> IO (Maybe Int)
getUsecSinceInitialized :: Device -> IO (Maybe Int)
getUsecSinceInitialized Device
dev = CULLong -> Maybe Int
toMaybe (CULLong -> Maybe Int) -> IO CULLong -> IO (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Device -> IO CULLong
c_getUsecSinceInitialized Device
dev
foreign import ccall unsafe "udev_device_has_tag"
c_hasTag :: Device -> CString -> IO CInt
hasTag :: Device -> ByteString -> IO Bool
hasTag :: Device -> RawFilePath -> IO Bool
hasTag Device
dev RawFilePath
tag =
(CInt
1 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
==) (CInt -> Bool) -> IO CInt -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
RawFilePath -> (CString -> IO CInt) -> IO CInt
forall a. RawFilePath -> (CString -> IO a) -> IO a
useAsCString RawFilePath
tag (\ CString
c_tag ->
Device -> CString -> IO CInt
c_hasTag Device
dev CString
c_tag)