{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson and Iñaki García Etxebarria
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- Represents a file descriptor, which events to poll for, and which events
-- occurred.

#if (MIN_VERSION_haskell_gi_overloading(1,0,0) && !defined(__HADDOCK_VERSION__))
#define ENABLE_OVERLOADING
#endif

module GI.GLib.Structs.PollFD
    ( 

-- * Exported types
    PollFD(..)                              ,
    newZeroPollFD                           ,


 -- * Methods

#if defined(ENABLE_OVERLOADING)
    ResolvePollFDMethod                     ,
#endif



 -- * Properties


-- ** events #attr:events#
-- | a bitwise combination from t'GI.GLib.Flags.IOCondition', specifying which
--     events should be polled for. Typically for reading from a file
--     descriptor you would use 'GI.GLib.Flags.IOConditionIn' | 'GI.GLib.Flags.IOConditionHup' | 'GI.GLib.Flags.IOConditionErr', and
--     for writing you would use 'GI.GLib.Flags.IOConditionOut' | 'GI.GLib.Flags.IOConditionErr'.

    getPollFDEvents                         ,
#if defined(ENABLE_OVERLOADING)
    pollFD_events                           ,
#endif
    setPollFDEvents                         ,


-- ** fd #attr:fd#
-- | the file descriptor to poll (or a HANDLE on Win32)

    getPollFDFd                             ,
#if defined(ENABLE_OVERLOADING)
    pollFD_fd                               ,
#endif
    setPollFDFd                             ,


-- ** revents #attr:revents#
-- | a bitwise combination of flags from t'GI.GLib.Flags.IOCondition', returned
--     from the @/poll()/@ function to indicate which events occurred.

    getPollFDRevents                        ,
#if defined(ENABLE_OVERLOADING)
    pollFD_revents                          ,
#endif
    setPollFDRevents                        ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.BasicTypes as B.Types
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GArray as B.GArray
import qualified Data.GI.Base.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
import qualified Data.GI.Base.Signals as B.Signals
import qualified Control.Monad.IO.Class as MIO
import qualified Data.Coerce as Coerce
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP
import qualified GHC.OverloadedLabels as OL
import qualified GHC.Records as R


-- | Memory-managed wrapper type.
newtype PollFD = PollFD (SP.ManagedPtr PollFD)
    deriving (PollFD -> PollFD -> Bool
(PollFD -> PollFD -> Bool)
-> (PollFD -> PollFD -> Bool) -> Eq PollFD
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PollFD -> PollFD -> Bool
$c/= :: PollFD -> PollFD -> Bool
== :: PollFD -> PollFD -> Bool
$c== :: PollFD -> PollFD -> Bool
Eq)

instance SP.ManagedPtrNewtype PollFD where
    toManagedPtr :: PollFD -> ManagedPtr PollFD
toManagedPtr (PollFD ManagedPtr PollFD
p) = ManagedPtr PollFD
p

foreign import ccall "g_pollfd_get_type" c_g_pollfd_get_type :: 
    IO GType

type instance O.ParentTypes PollFD = '[]
instance O.HasParentTypes PollFD

instance B.Types.TypedObject PollFD where
    glibType :: IO GType
glibType = IO GType
c_g_pollfd_get_type

instance B.Types.GBoxed PollFD

-- | Convert 'PollFD' to and from 'Data.GI.Base.GValue.GValue'. See 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue (Maybe PollFD) where
    gvalueGType_ :: IO GType
gvalueGType_ = IO GType
c_g_pollfd_get_type
    gvalueSet_ :: Ptr GValue -> Maybe PollFD -> IO ()
gvalueSet_ Ptr GValue
gv Maybe PollFD
P.Nothing = Ptr GValue -> Ptr PollFD -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
B.GValue.set_boxed Ptr GValue
gv (Ptr PollFD
forall a. Ptr a
FP.nullPtr :: FP.Ptr PollFD)
    gvalueSet_ Ptr GValue
gv (P.Just PollFD
obj) = PollFD -> (Ptr PollFD -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
B.ManagedPtr.withManagedPtr PollFD
obj (Ptr GValue -> Ptr PollFD -> IO ()
forall a. Ptr GValue -> Ptr a -> IO ()
B.GValue.set_boxed Ptr GValue
gv)
    gvalueGet_ :: Ptr GValue -> IO (Maybe PollFD)
gvalueGet_ Ptr GValue
gv = do
        Ptr PollFD
ptr <- Ptr GValue -> IO (Ptr PollFD)
forall b. Ptr GValue -> IO (Ptr b)
B.GValue.get_boxed Ptr GValue
gv :: IO (Ptr PollFD)
        if Ptr PollFD
ptr Ptr PollFD -> Ptr PollFD -> Bool
forall a. Eq a => a -> a -> Bool
/= Ptr PollFD
forall a. Ptr a
FP.nullPtr
        then PollFD -> Maybe PollFD
forall a. a -> Maybe a
P.Just (PollFD -> Maybe PollFD) -> IO PollFD -> IO (Maybe PollFD)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ManagedPtr PollFD -> PollFD) -> Ptr PollFD -> IO PollFD
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
B.ManagedPtr.newBoxed ManagedPtr PollFD -> PollFD
PollFD Ptr PollFD
ptr
        else Maybe PollFD -> IO (Maybe PollFD)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PollFD
forall a. Maybe a
P.Nothing
        
    

-- | Construct a `PollFD` struct initialized to zero.
newZeroPollFD :: MonadIO m => m PollFD
newZeroPollFD :: forall (m :: * -> *). MonadIO m => m PollFD
newZeroPollFD = IO PollFD -> m PollFD
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO PollFD -> m PollFD) -> IO PollFD -> m PollFD
forall a b. (a -> b) -> a -> b
$ Int -> IO (Ptr PollFD)
forall a. GBoxed a => Int -> IO (Ptr a)
callocBoxedBytes Int
8 IO (Ptr PollFD) -> (Ptr PollFD -> IO PollFD) -> IO PollFD
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ManagedPtr PollFD -> PollFD) -> Ptr PollFD -> IO PollFD
forall a.
(HasCallStack, GBoxed a) =>
(ManagedPtr a -> a) -> Ptr a -> IO a
wrapBoxed ManagedPtr PollFD -> PollFD
PollFD

instance tag ~ 'AttrSet => Constructible PollFD tag where
    new :: forall (m :: * -> *).
MonadIO m =>
(ManagedPtr PollFD -> PollFD) -> [AttrOp PollFD tag] -> m PollFD
new ManagedPtr PollFD -> PollFD
_ [AttrOp PollFD tag]
attrs = do
        PollFD
o <- m PollFD
forall (m :: * -> *). MonadIO m => m PollFD
newZeroPollFD
        PollFD -> [AttrOp PollFD 'AttrSet] -> m ()
forall o (m :: * -> *).
MonadIO m =>
o -> [AttrOp o 'AttrSet] -> m ()
GI.Attributes.set PollFD
o [AttrOp PollFD tag]
[AttrOp PollFD 'AttrSet]
attrs
        PollFD -> m PollFD
forall (m :: * -> *) a. Monad m => a -> m a
return PollFD
o


-- | Get the value of the “@fd@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #fd
-- @
getPollFDFd :: MonadIO m => PollFD -> m Int32
getPollFDFd :: forall (m :: * -> *). MonadIO m => PollFD -> m Int32
getPollFDFd PollFD
s = IO Int32 -> m Int32
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int32 -> m Int32) -> IO Int32 -> m Int32
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO Int32) -> IO Int32
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO Int32) -> IO Int32)
-> (Ptr PollFD -> IO Int32) -> IO Int32
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Int32
val <- Ptr Int32 -> IO Int32
forall a. Storable a => Ptr a -> IO a
peek (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Int32
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
0) :: IO Int32
    Int32 -> IO Int32
forall (m :: * -> *) a. Monad m => a -> m a
return Int32
val

-- | Set the value of the “@fd@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #fd 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDFd :: MonadIO m => PollFD -> Int32 -> m ()
setPollFDFd :: forall (m :: * -> *). MonadIO m => PollFD -> Int32 -> m ()
setPollFDFd PollFD
s Int32
val = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO ()) -> IO ()) -> (Ptr PollFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Ptr Int32 -> Int32 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Int32
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
0) (Int32
val :: Int32)

#if defined(ENABLE_OVERLOADING)
data PollFDFdFieldInfo
instance AttrInfo PollFDFdFieldInfo where
    type AttrBaseTypeConstraint PollFDFdFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDFdFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDFdFieldInfo = (~) Int32
    type AttrTransferTypeConstraint PollFDFdFieldInfo = (~)Int32
    type AttrTransferType PollFDFdFieldInfo = Int32
    type AttrGetType PollFDFdFieldInfo = Int32
    type AttrLabel PollFDFdFieldInfo = "fd"
    type AttrOrigin PollFDFdFieldInfo = PollFD
    attrGet = getPollFDFd
    attrSet = setPollFDFd
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v
    dbgAttrInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.GLib.Structs.PollFD.fd"
        , O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-glib-2.0.26/docs/GI-GLib-Structs-PollFD.html#g:attr:fd"
        })

pollFD_fd :: AttrLabelProxy "fd"
pollFD_fd = AttrLabelProxy

#endif


-- | Get the value of the “@events@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #events
-- @
getPollFDEvents :: MonadIO m => PollFD -> m Word16
getPollFDEvents :: forall (m :: * -> *). MonadIO m => PollFD -> m Word16
getPollFDEvents PollFD
s = IO Word16 -> m Word16
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Word16 -> m Word16) -> IO Word16 -> m Word16
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO Word16) -> IO Word16
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO Word16) -> IO Word16)
-> (Ptr PollFD -> IO Word16) -> IO Word16
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Word16
val <- Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
4) :: IO Word16
    Word16 -> IO Word16
forall (m :: * -> *) a. Monad m => a -> m a
return Word16
val

-- | Set the value of the “@events@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #events 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDEvents :: MonadIO m => PollFD -> Word16 -> m ()
setPollFDEvents :: forall (m :: * -> *). MonadIO m => PollFD -> Word16 -> m ()
setPollFDEvents PollFD
s Word16
val = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO ()) -> IO ()) -> (Ptr PollFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Ptr Word16 -> Word16 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
4) (Word16
val :: Word16)

#if defined(ENABLE_OVERLOADING)
data PollFDEventsFieldInfo
instance AttrInfo PollFDEventsFieldInfo where
    type AttrBaseTypeConstraint PollFDEventsFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDEventsFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDEventsFieldInfo = (~) Word16
    type AttrTransferTypeConstraint PollFDEventsFieldInfo = (~)Word16
    type AttrTransferType PollFDEventsFieldInfo = Word16
    type AttrGetType PollFDEventsFieldInfo = Word16
    type AttrLabel PollFDEventsFieldInfo = "events"
    type AttrOrigin PollFDEventsFieldInfo = PollFD
    attrGet = getPollFDEvents
    attrSet = setPollFDEvents
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v
    dbgAttrInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.GLib.Structs.PollFD.events"
        , O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-glib-2.0.26/docs/GI-GLib-Structs-PollFD.html#g:attr:events"
        })

pollFD_events :: AttrLabelProxy "events"
pollFD_events = AttrLabelProxy

#endif


-- | Get the value of the “@revents@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.get' pollFD #revents
-- @
getPollFDRevents :: MonadIO m => PollFD -> m Word16
getPollFDRevents :: forall (m :: * -> *). MonadIO m => PollFD -> m Word16
getPollFDRevents PollFD
s = IO Word16 -> m Word16
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Word16 -> m Word16) -> IO Word16 -> m Word16
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO Word16) -> IO Word16
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO Word16) -> IO Word16)
-> (Ptr PollFD -> IO Word16) -> IO Word16
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Word16
val <- Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
6) :: IO Word16
    Word16 -> IO Word16
forall (m :: * -> *) a. Monad m => a -> m a
return Word16
val

-- | Set the value of the “@revents@” field.
-- When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to
-- 
-- @
-- 'Data.GI.Base.Attributes.set' pollFD [ #revents 'Data.GI.Base.Attributes.:=' value ]
-- @
setPollFDRevents :: MonadIO m => PollFD -> Word16 -> m ()
setPollFDRevents :: forall (m :: * -> *). MonadIO m => PollFD -> Word16 -> m ()
setPollFDRevents PollFD
s Word16
val = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ PollFD -> (Ptr PollFD -> IO ()) -> IO ()
forall a c.
(HasCallStack, ManagedPtrNewtype a) =>
a -> (Ptr a -> IO c) -> IO c
withManagedPtr PollFD
s ((Ptr PollFD -> IO ()) -> IO ()) -> (Ptr PollFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr PollFD
ptr -> do
    Ptr Word16 -> Word16 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr PollFD
ptr Ptr PollFD -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
6) (Word16
val :: Word16)

#if defined(ENABLE_OVERLOADING)
data PollFDReventsFieldInfo
instance AttrInfo PollFDReventsFieldInfo where
    type AttrBaseTypeConstraint PollFDReventsFieldInfo = (~) PollFD
    type AttrAllowedOps PollFDReventsFieldInfo = '[ 'AttrSet, 'AttrGet]
    type AttrSetTypeConstraint PollFDReventsFieldInfo = (~) Word16
    type AttrTransferTypeConstraint PollFDReventsFieldInfo = (~)Word16
    type AttrTransferType PollFDReventsFieldInfo = Word16
    type AttrGetType PollFDReventsFieldInfo = Word16
    type AttrLabel PollFDReventsFieldInfo = "revents"
    type AttrOrigin PollFDReventsFieldInfo = PollFD
    attrGet = getPollFDRevents
    attrSet = setPollFDRevents
    attrConstruct = undefined
    attrClear = undefined
    attrTransfer _ v = do
        return v
    dbgAttrInfo = P.Just (O.ResolvedSymbolInfo {
        O.resolvedSymbolName = "GI.GLib.Structs.PollFD.revents"
        , O.resolvedSymbolURL = "https://hackage.haskell.org/package/gi-glib-2.0.26/docs/GI-GLib-Structs-PollFD.html#g:attr:revents"
        })

pollFD_revents :: AttrLabelProxy "revents"
pollFD_revents = AttrLabelProxy

#endif



#if defined(ENABLE_OVERLOADING)
instance O.HasAttributeList PollFD
type instance O.AttributeList PollFD = PollFDAttributeList
type PollFDAttributeList = ('[ '("fd", PollFDFdFieldInfo), '("events", PollFDEventsFieldInfo), '("revents", PollFDReventsFieldInfo)] :: [(Symbol, *)])
#endif

#if defined(ENABLE_OVERLOADING)
type family ResolvePollFDMethod (t :: Symbol) (o :: *) :: * where
    ResolvePollFDMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolvePollFDMethod t PollFD, O.OverloadedMethod info PollFD p) => OL.IsLabel t (PollFD -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#if MIN_VERSION_base(4,13,0)
instance (info ~ ResolvePollFDMethod t PollFD, O.OverloadedMethod info PollFD p, R.HasField t PollFD p) => R.HasField t PollFD p where
    getField = O.overloadedMethod @info

#endif

instance (info ~ ResolvePollFDMethod t PollFD, O.OverloadedMethodInfo info PollFD) => OL.IsLabel t (O.MethodProxy info PollFD) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.MethodProxy
#else
    fromLabel _ = O.MethodProxy
#endif

#endif