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

Opaque class for defining and scheduling IO jobs.
-}

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

module GI.Gio.Structs.IOSchedulerJob
    (

-- * Exported types
    IOSchedulerJob(..)                      ,
    noIOSchedulerJob                        ,


 -- * Methods
-- ** sendToMainloop #method:sendToMainloop#

#if ENABLE_OVERLOADING
    IOSchedulerJobSendToMainloopMethodInfo  ,
#endif
    iOSchedulerJobSendToMainloop            ,


-- ** sendToMainloopAsync #method:sendToMainloopAsync#

#if ENABLE_OVERLOADING
    IOSchedulerJobSendToMainloopAsyncMethodInfo,
#endif
    iOSchedulerJobSendToMainloopAsync       ,




    ) 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.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.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 GI.GLib.Callbacks as GLib.Callbacks

-- | Memory-managed wrapper type.
newtype IOSchedulerJob = IOSchedulerJob (ManagedPtr IOSchedulerJob)
-- XXX Wrapping a foreign struct/union with no known destructor or size, leak?
instance WrappedPtr IOSchedulerJob where
    wrappedPtrCalloc = return nullPtr
    wrappedPtrCopy = return
    wrappedPtrFree = Nothing

-- | A convenience alias for `Nothing` :: `Maybe` `IOSchedulerJob`.
noIOSchedulerJob :: Maybe IOSchedulerJob
noIOSchedulerJob = Nothing


#if ENABLE_OVERLOADING
instance O.HasAttributeList IOSchedulerJob
type instance O.AttributeList IOSchedulerJob = IOSchedulerJobAttributeList
type IOSchedulerJobAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method IOSchedulerJob::send_to_mainloop
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "job", argType = TInterface (Name {namespace = "Gio", name = "IOSchedulerJob"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GIOSchedulerJob", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "func", argType = TInterface (Name {namespace = "GLib", name = "SourceFunc"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSourceFunc callback that will be called in the original thread", sinceVersion = Nothing}, argScope = ScopeTypeNotified, argClosure = 2, argDestroy = 3, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "data to pass to @func", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "notify", argType = TInterface (Name {namespace = "GLib", name = "DestroyNotify"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GDestroyNotify for @user_data, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "g_io_scheduler_job_send_to_mainloop" g_io_scheduler_job_send_to_mainloop ::
    Ptr IOSchedulerJob ->                   -- job : TInterface (Name {namespace = "Gio", name = "IOSchedulerJob"})
    FunPtr GLib.Callbacks.C_SourceFunc ->   -- func : TInterface (Name {namespace = "GLib", name = "SourceFunc"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    FunPtr GLib.Callbacks.C_DestroyNotify -> -- notify : TInterface (Name {namespace = "GLib", name = "DestroyNotify"})
    IO CInt

{-# DEPRECATED iOSchedulerJobSendToMainloop ["Use @/g_main_context_invoke()/@."] #-}
{- |
Used from an I\/O job to send a callback to be run in the thread
that the job was started from, waiting for the result (and thus
blocking the I\/O job).
-}
iOSchedulerJobSendToMainloop ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    IOSchedulerJob
    {- ^ /@job@/: a 'GI.Gio.Structs.IOSchedulerJob.IOSchedulerJob' -}
    -> GLib.Callbacks.SourceFunc
    {- ^ /@func@/: a 'GI.GLib.Callbacks.SourceFunc' callback that will be called in the original thread -}
    -> m Bool
    {- ^ __Returns:__ The return value of /@func@/ -}
iOSchedulerJobSendToMainloop job func = liftIO $ do
    job' <- unsafeManagedPtrGetPtr job
    func' <- GLib.Callbacks.mk_SourceFunc (GLib.Callbacks.wrap_SourceFunc Nothing (GLib.Callbacks.drop_closures_SourceFunc func))
    let userData = castFunPtrToPtr func'
    let notify = safeFreeFunPtrPtr
    result <- g_io_scheduler_job_send_to_mainloop job' func' userData notify
    let result' = (/= 0) result
    touchManagedPtr job
    return result'

#if ENABLE_OVERLOADING
data IOSchedulerJobSendToMainloopMethodInfo
instance (signature ~ (GLib.Callbacks.SourceFunc -> m Bool), MonadIO m) => O.MethodInfo IOSchedulerJobSendToMainloopMethodInfo IOSchedulerJob signature where
    overloadedMethod _ = iOSchedulerJobSendToMainloop

#endif

-- method IOSchedulerJob::send_to_mainloop_async
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "job", argType = TInterface (Name {namespace = "Gio", name = "IOSchedulerJob"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GIOSchedulerJob", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "func", argType = TInterface (Name {namespace = "GLib", name = "SourceFunc"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSourceFunc callback that will be called in the original thread", sinceVersion = Nothing}, argScope = ScopeTypeNotified, argClosure = 2, argDestroy = 3, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "data to pass to @func", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "notify", argType = TInterface (Name {namespace = "GLib", name = "DestroyNotify"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GDestroyNotify for @user_data, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_io_scheduler_job_send_to_mainloop_async" g_io_scheduler_job_send_to_mainloop_async ::
    Ptr IOSchedulerJob ->                   -- job : TInterface (Name {namespace = "Gio", name = "IOSchedulerJob"})
    FunPtr GLib.Callbacks.C_SourceFunc ->   -- func : TInterface (Name {namespace = "GLib", name = "SourceFunc"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    FunPtr GLib.Callbacks.C_DestroyNotify -> -- notify : TInterface (Name {namespace = "GLib", name = "DestroyNotify"})
    IO ()

{-# DEPRECATED iOSchedulerJobSendToMainloopAsync ["Use @/g_main_context_invoke()/@."] #-}
{- |
Used from an I\/O job to send a callback to be run asynchronously in
the thread that the job was started from. The callback will be run
when the main loop is available, but at that time the I\/O job might
have finished. The return value from the callback is ignored.

Note that if you are passing the /@userData@/ from 'GI.Gio.Functions.ioSchedulerPushJob'
on to this function you have to ensure that it is not freed before
/@func@/ is called, either by passing 'Nothing' as /@notify@/ to
'GI.Gio.Functions.ioSchedulerPushJob' or by using refcounting for /@userData@/.
-}
iOSchedulerJobSendToMainloopAsync ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    IOSchedulerJob
    {- ^ /@job@/: a 'GI.Gio.Structs.IOSchedulerJob.IOSchedulerJob' -}
    -> GLib.Callbacks.SourceFunc
    {- ^ /@func@/: a 'GI.GLib.Callbacks.SourceFunc' callback that will be called in the original thread -}
    -> m ()
iOSchedulerJobSendToMainloopAsync job func = liftIO $ do
    job' <- unsafeManagedPtrGetPtr job
    func' <- GLib.Callbacks.mk_SourceFunc (GLib.Callbacks.wrap_SourceFunc Nothing (GLib.Callbacks.drop_closures_SourceFunc func))
    let userData = castFunPtrToPtr func'
    let notify = safeFreeFunPtrPtr
    g_io_scheduler_job_send_to_mainloop_async job' func' userData notify
    touchManagedPtr job
    return ()

#if ENABLE_OVERLOADING
data IOSchedulerJobSendToMainloopAsyncMethodInfo
instance (signature ~ (GLib.Callbacks.SourceFunc -> m ()), MonadIO m) => O.MethodInfo IOSchedulerJobSendToMainloopAsyncMethodInfo IOSchedulerJob signature where
    overloadedMethod _ = iOSchedulerJobSendToMainloopAsync

#endif

#if ENABLE_OVERLOADING
type family ResolveIOSchedulerJobMethod (t :: Symbol) (o :: *) :: * where
    ResolveIOSchedulerJobMethod "sendToMainloop" o = IOSchedulerJobSendToMainloopMethodInfo
    ResolveIOSchedulerJobMethod "sendToMainloopAsync" o = IOSchedulerJobSendToMainloopAsyncMethodInfo
    ResolveIOSchedulerJobMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveIOSchedulerJobMethod t IOSchedulerJob, O.MethodInfo info IOSchedulerJob p) => OL.IsLabel t (IOSchedulerJob -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

#endif