{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

'GI.Gio.Objects.UnixInputStream.UnixInputStream' implements 'GI.Gio.Objects.InputStream.InputStream' for reading from a UNIX
file descriptor, including asynchronous operations. (If the file
descriptor refers to a socket or pipe, this will use @/poll()/@ to do
asynchronous I\/O. If it refers to a regular file, it will fall back
to doing asynchronous I\/O in another thread.)

Note that @\<gio\/gunixinputstream.h>@ belongs to the UNIX-specific GIO
interfaces, thus you have to use the @gio-unix-2.0.pc@ pkg-config
file when using it.
-}

module GI.Gio.Objects.UnixInputStream
    ( 

-- * Exported types
    UnixInputStream(..)                     ,
    IsUnixInputStream                       ,
    toUnixInputStream                       ,
    noUnixInputStream                       ,


 -- * Methods
-- ** getCloseFd #method:getCloseFd#
    UnixInputStreamGetCloseFdMethodInfo     ,
    unixInputStreamGetCloseFd               ,


-- ** getFd #method:getFd#
    UnixInputStreamGetFdMethodInfo          ,
    unixInputStreamGetFd                    ,


-- ** new #method:new#
    unixInputStreamNew                      ,


-- ** setCloseFd #method:setCloseFd#
    UnixInputStreamSetCloseFdMethodInfo     ,
    unixInputStreamSetCloseFd               ,




 -- * Properties
-- ** closeFd #attr:closeFd#
    UnixInputStreamCloseFdPropertyInfo      ,
    constructUnixInputStreamCloseFd         ,
    getUnixInputStreamCloseFd               ,
    setUnixInputStreamCloseFd               ,
    unixInputStreamCloseFd                  ,


-- ** fd #attr:fd#
    UnixInputStreamFdPropertyInfo           ,
    constructUnixInputStreamFd              ,
    getUnixInputStreamFd                    ,
    unixInputStreamFd                       ,




    ) 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.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
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 GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Gio.Interfaces.FileDescriptorBased as Gio.FileDescriptorBased
import {-# SOURCE #-} qualified GI.Gio.Interfaces.PollableInputStream as Gio.PollableInputStream
import {-# SOURCE #-} qualified GI.Gio.Objects.InputStream as Gio.InputStream

newtype UnixInputStream = UnixInputStream (ManagedPtr UnixInputStream)
foreign import ccall "g_unix_input_stream_get_type"
    c_g_unix_input_stream_get_type :: IO GType

instance GObject UnixInputStream where
    gobjectType _ = c_g_unix_input_stream_get_type
    

class GObject o => IsUnixInputStream o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError UnixInputStream a) =>
    IsUnixInputStream a
#endif
instance IsUnixInputStream UnixInputStream
instance Gio.InputStream.IsInputStream UnixInputStream
instance GObject.Object.IsObject UnixInputStream
instance Gio.FileDescriptorBased.IsFileDescriptorBased UnixInputStream
instance Gio.PollableInputStream.IsPollableInputStream UnixInputStream

toUnixInputStream :: IsUnixInputStream o => o -> IO UnixInputStream
toUnixInputStream = unsafeCastTo UnixInputStream

noUnixInputStream :: Maybe UnixInputStream
noUnixInputStream = Nothing

type family ResolveUnixInputStreamMethod (t :: Symbol) (o :: *) :: * where
    ResolveUnixInputStreamMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveUnixInputStreamMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveUnixInputStreamMethod "canPoll" o = Gio.PollableInputStream.PollableInputStreamCanPollMethodInfo
    ResolveUnixInputStreamMethod "clearPending" o = Gio.InputStream.InputStreamClearPendingMethodInfo
    ResolveUnixInputStreamMethod "close" o = Gio.InputStream.InputStreamCloseMethodInfo
    ResolveUnixInputStreamMethod "closeAsync" o = Gio.InputStream.InputStreamCloseAsyncMethodInfo
    ResolveUnixInputStreamMethod "closeFinish" o = Gio.InputStream.InputStreamCloseFinishMethodInfo
    ResolveUnixInputStreamMethod "createSource" o = Gio.PollableInputStream.PollableInputStreamCreateSourceMethodInfo
    ResolveUnixInputStreamMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveUnixInputStreamMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveUnixInputStreamMethod "hasPending" o = Gio.InputStream.InputStreamHasPendingMethodInfo
    ResolveUnixInputStreamMethod "isClosed" o = Gio.InputStream.InputStreamIsClosedMethodInfo
    ResolveUnixInputStreamMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveUnixInputStreamMethod "isReadable" o = Gio.PollableInputStream.PollableInputStreamIsReadableMethodInfo
    ResolveUnixInputStreamMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveUnixInputStreamMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveUnixInputStreamMethod "read" o = Gio.InputStream.InputStreamReadMethodInfo
    ResolveUnixInputStreamMethod "readAll" o = Gio.InputStream.InputStreamReadAllMethodInfo
    ResolveUnixInputStreamMethod "readAllAsync" o = Gio.InputStream.InputStreamReadAllAsyncMethodInfo
    ResolveUnixInputStreamMethod "readAllFinish" o = Gio.InputStream.InputStreamReadAllFinishMethodInfo
    ResolveUnixInputStreamMethod "readAsync" o = Gio.InputStream.InputStreamReadAsyncMethodInfo
    ResolveUnixInputStreamMethod "readBytes" o = Gio.InputStream.InputStreamReadBytesMethodInfo
    ResolveUnixInputStreamMethod "readBytesAsync" o = Gio.InputStream.InputStreamReadBytesAsyncMethodInfo
    ResolveUnixInputStreamMethod "readBytesFinish" o = Gio.InputStream.InputStreamReadBytesFinishMethodInfo
    ResolveUnixInputStreamMethod "readFinish" o = Gio.InputStream.InputStreamReadFinishMethodInfo
    ResolveUnixInputStreamMethod "readNonblocking" o = Gio.PollableInputStream.PollableInputStreamReadNonblockingMethodInfo
    ResolveUnixInputStreamMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveUnixInputStreamMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveUnixInputStreamMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveUnixInputStreamMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveUnixInputStreamMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveUnixInputStreamMethod "skip" o = Gio.InputStream.InputStreamSkipMethodInfo
    ResolveUnixInputStreamMethod "skipAsync" o = Gio.InputStream.InputStreamSkipAsyncMethodInfo
    ResolveUnixInputStreamMethod "skipFinish" o = Gio.InputStream.InputStreamSkipFinishMethodInfo
    ResolveUnixInputStreamMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveUnixInputStreamMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveUnixInputStreamMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveUnixInputStreamMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveUnixInputStreamMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveUnixInputStreamMethod "getCloseFd" o = UnixInputStreamGetCloseFdMethodInfo
    ResolveUnixInputStreamMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveUnixInputStreamMethod "getFd" o = UnixInputStreamGetFdMethodInfo
    ResolveUnixInputStreamMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveUnixInputStreamMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveUnixInputStreamMethod "setCloseFd" o = UnixInputStreamSetCloseFdMethodInfo
    ResolveUnixInputStreamMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveUnixInputStreamMethod "setPending" o = Gio.InputStream.InputStreamSetPendingMethodInfo
    ResolveUnixInputStreamMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveUnixInputStreamMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveUnixInputStreamMethod t UnixInputStream, O.MethodInfo info UnixInputStream p) => O.IsLabelProxy t (UnixInputStream -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveUnixInputStreamMethod t UnixInputStream, O.MethodInfo info UnixInputStream p) => O.IsLabel t (UnixInputStream -> p) where
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

-- VVV Prop "close-fd"
   -- Type: TBasicType TBoolean
   -- Flags: [PropertyReadable,PropertyWritable]
   -- Nullable: (Just False,Just False)

getUnixInputStreamCloseFd :: (MonadIO m, IsUnixInputStream o) => o -> m Bool
getUnixInputStreamCloseFd obj = liftIO $ getObjectPropertyBool obj "close-fd"

setUnixInputStreamCloseFd :: (MonadIO m, IsUnixInputStream o) => o -> Bool -> m ()
setUnixInputStreamCloseFd obj val = liftIO $ setObjectPropertyBool obj "close-fd" val

constructUnixInputStreamCloseFd :: (IsUnixInputStream o) => Bool -> IO (GValueConstruct o)
constructUnixInputStreamCloseFd val = constructObjectPropertyBool "close-fd" val

data UnixInputStreamCloseFdPropertyInfo
instance AttrInfo UnixInputStreamCloseFdPropertyInfo where
    type AttrAllowedOps UnixInputStreamCloseFdPropertyInfo = '[ 'AttrSet, 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint UnixInputStreamCloseFdPropertyInfo = (~) Bool
    type AttrBaseTypeConstraint UnixInputStreamCloseFdPropertyInfo = IsUnixInputStream
    type AttrGetType UnixInputStreamCloseFdPropertyInfo = Bool
    type AttrLabel UnixInputStreamCloseFdPropertyInfo = "close-fd"
    type AttrOrigin UnixInputStreamCloseFdPropertyInfo = UnixInputStream
    attrGet _ = getUnixInputStreamCloseFd
    attrSet _ = setUnixInputStreamCloseFd
    attrConstruct _ = constructUnixInputStreamCloseFd
    attrClear _ = undefined

-- VVV Prop "fd"
   -- Type: TBasicType TInt
   -- Flags: [PropertyReadable,PropertyWritable,PropertyConstructOnly]
   -- Nullable: (Just False,Nothing)

getUnixInputStreamFd :: (MonadIO m, IsUnixInputStream o) => o -> m Int32
getUnixInputStreamFd obj = liftIO $ getObjectPropertyInt32 obj "fd"

constructUnixInputStreamFd :: (IsUnixInputStream o) => Int32 -> IO (GValueConstruct o)
constructUnixInputStreamFd val = constructObjectPropertyInt32 "fd" val

data UnixInputStreamFdPropertyInfo
instance AttrInfo UnixInputStreamFdPropertyInfo where
    type AttrAllowedOps UnixInputStreamFdPropertyInfo = '[ 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint UnixInputStreamFdPropertyInfo = (~) Int32
    type AttrBaseTypeConstraint UnixInputStreamFdPropertyInfo = IsUnixInputStream
    type AttrGetType UnixInputStreamFdPropertyInfo = Int32
    type AttrLabel UnixInputStreamFdPropertyInfo = "fd"
    type AttrOrigin UnixInputStreamFdPropertyInfo = UnixInputStream
    attrGet _ = getUnixInputStreamFd
    attrSet _ = undefined
    attrConstruct _ = constructUnixInputStreamFd
    attrClear _ = undefined

instance O.HasAttributeList UnixInputStream
type instance O.AttributeList UnixInputStream = UnixInputStreamAttributeList
type UnixInputStreamAttributeList = ('[ '("closeFd", UnixInputStreamCloseFdPropertyInfo), '("fd", UnixInputStreamFdPropertyInfo)] :: [(Symbol, *)])

unixInputStreamCloseFd :: AttrLabelProxy "closeFd"
unixInputStreamCloseFd = AttrLabelProxy

unixInputStreamFd :: AttrLabelProxy "fd"
unixInputStreamFd = AttrLabelProxy

type instance O.SignalList UnixInputStream = UnixInputStreamSignalList
type UnixInputStreamSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method UnixInputStream::new
-- method type : Constructor
-- Args : [Arg {argCName = "fd", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a UNIX file descriptor", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "close_fd", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE to close the file descriptor when done", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "UnixInputStream"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_input_stream_new" g_unix_input_stream_new :: 
    Int32 ->                                -- fd : TBasicType TInt
    CInt ->                                 -- close_fd : TBasicType TBoolean
    IO (Ptr UnixInputStream)

{- |
Creates a new 'GI.Gio.Objects.UnixInputStream.UnixInputStream' for the given /@fd@/.

If /@closeFd@/ is 'True', the file descriptor will be closed
when the stream is closed.
-}
unixInputStreamNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Int32
    {- ^ /@fd@/: a UNIX file descriptor -}
    -> Bool
    {- ^ /@closeFd@/: 'True' to close the file descriptor when done -}
    -> m UnixInputStream
    {- ^ __Returns:__ a new 'GI.Gio.Objects.UnixInputStream.UnixInputStream' -}
unixInputStreamNew fd closeFd = liftIO $ do
    let closeFd' = (fromIntegral . fromEnum) closeFd
    result <- g_unix_input_stream_new fd closeFd'
    checkUnexpectedReturnNULL "unixInputStreamNew" result
    result' <- (wrapObject UnixInputStream) result
    return result'

-- method UnixInputStream::get_close_fd
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "UnixInputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GUnixInputStream", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_input_stream_get_close_fd" g_unix_input_stream_get_close_fd :: 
    Ptr UnixInputStream ->                  -- stream : TInterface (Name {namespace = "Gio", name = "UnixInputStream"})
    IO CInt

{- |
Returns whether the file descriptor of /@stream@/ will be
closed when the stream is closed.

@since 2.20
-}
unixInputStreamGetCloseFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixInputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixInputStream.UnixInputStream' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the file descriptor is closed when done -}
unixInputStreamGetCloseFd stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_unix_input_stream_get_close_fd stream'
    let result' = (/= 0) result
    touchManagedPtr stream
    return result'

data UnixInputStreamGetCloseFdMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsUnixInputStream a) => O.MethodInfo UnixInputStreamGetCloseFdMethodInfo a signature where
    overloadedMethod _ = unixInputStreamGetCloseFd

-- method UnixInputStream::get_fd
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "UnixInputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GUnixInputStream", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt)
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_input_stream_get_fd" g_unix_input_stream_get_fd :: 
    Ptr UnixInputStream ->                  -- stream : TInterface (Name {namespace = "Gio", name = "UnixInputStream"})
    IO Int32

{- |
Return the UNIX file descriptor that the stream reads from.

@since 2.20
-}
unixInputStreamGetFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixInputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixInputStream.UnixInputStream' -}
    -> m Int32
    {- ^ __Returns:__ The file descriptor of /@stream@/ -}
unixInputStreamGetFd stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_unix_input_stream_get_fd stream'
    touchManagedPtr stream
    return result

data UnixInputStreamGetFdMethodInfo
instance (signature ~ (m Int32), MonadIO m, IsUnixInputStream a) => O.MethodInfo UnixInputStreamGetFdMethodInfo a signature where
    overloadedMethod _ = unixInputStreamGetFd

-- method UnixInputStream::set_close_fd
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "UnixInputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GUnixInputStream", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "close_fd", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE to close the file descriptor when done", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_input_stream_set_close_fd" g_unix_input_stream_set_close_fd :: 
    Ptr UnixInputStream ->                  -- stream : TInterface (Name {namespace = "Gio", name = "UnixInputStream"})
    CInt ->                                 -- close_fd : TBasicType TBoolean
    IO ()

{- |
Sets whether the file descriptor of /@stream@/ shall be closed
when the stream is closed.

@since 2.20
-}
unixInputStreamSetCloseFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixInputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixInputStream.UnixInputStream' -}
    -> Bool
    {- ^ /@closeFd@/: 'True' to close the file descriptor when done -}
    -> m ()
unixInputStreamSetCloseFd stream closeFd = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    let closeFd' = (fromIntegral . fromEnum) closeFd
    g_unix_input_stream_set_close_fd stream' closeFd'
    touchManagedPtr stream
    return ()

data UnixInputStreamSetCloseFdMethodInfo
instance (signature ~ (Bool -> m ()), MonadIO m, IsUnixInputStream a) => O.MethodInfo UnixInputStreamSetCloseFdMethodInfo a signature where
    overloadedMethod _ = unixInputStreamSetCloseFd