{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- Opaque class for defining and scheduling IO jobs.

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

module GI.Gio.Structs.IOSchedulerJob
    ( 

-- * Exported types
    IOSchedulerJob(..)                      ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolveIOSchedulerJobMethod             ,
#endif


-- ** sendToMainloop #method:sendToMainloop#

#if defined(ENABLE_OVERLOADING)
    IOSchedulerJobSendToMainloopMethodInfo  ,
#endif
    iOSchedulerJobSendToMainloop            ,


-- ** sendToMainloopAsync #method:sendToMainloopAsync#

#if defined(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.BasicTypes as B.Types
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.GI.Base.Signals as B.Signals
import qualified Control.Monad.IO.Class as MIO
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 (SP.ManagedPtr IOSchedulerJob)
    deriving (IOSchedulerJob -> IOSchedulerJob -> Bool
(IOSchedulerJob -> IOSchedulerJob -> Bool)
-> (IOSchedulerJob -> IOSchedulerJob -> Bool) -> Eq IOSchedulerJob
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IOSchedulerJob -> IOSchedulerJob -> Bool
$c/= :: IOSchedulerJob -> IOSchedulerJob -> Bool
== :: IOSchedulerJob -> IOSchedulerJob -> Bool
$c== :: IOSchedulerJob -> IOSchedulerJob -> Bool
Eq)

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

-- XXX Wrapping a foreign struct/union with no known destructor or size, leak?
instance BoxedPtr IOSchedulerJob where
    boxedPtrCopy :: IOSchedulerJob -> IO IOSchedulerJob
boxedPtrCopy = IOSchedulerJob -> IO IOSchedulerJob
forall (m :: * -> *) a. Monad m => a -> m a
return
    boxedPtrFree :: IOSchedulerJob -> IO ()
boxedPtrFree = \IOSchedulerJob
_x -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()


#if defined(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 t'GI.Gio.Structs.IOSchedulerJob.IOSchedulerJob'
    -> GLib.Callbacks.SourceFunc
    -- ^ /@func@/: a t'GI.GLib.Callbacks.SourceFunc' callback that will be called in the original thread
    -> m Bool
    -- ^ __Returns:__ The return value of /@func@/
iOSchedulerJobSendToMainloop :: IOSchedulerJob -> SourceFunc -> m Bool
iOSchedulerJobSendToMainloop IOSchedulerJob
job SourceFunc
func = SourceFunc -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (SourceFunc -> m Bool) -> SourceFunc -> m Bool
forall a b. (a -> b) -> a -> b
$ do
    Ptr IOSchedulerJob
job' <- IOSchedulerJob -> IO (Ptr IOSchedulerJob)
forall a. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr a)
unsafeManagedPtrGetPtr IOSchedulerJob
job
    FunPtr C_SourceFunc
func' <- C_SourceFunc -> IO (FunPtr C_SourceFunc)
GLib.Callbacks.mk_SourceFunc (Maybe (Ptr (FunPtr C_SourceFunc))
-> SourceFunc_WithClosures -> C_SourceFunc
GLib.Callbacks.wrap_SourceFunc Maybe (Ptr (FunPtr C_SourceFunc))
forall a. Maybe a
Nothing (SourceFunc -> SourceFunc_WithClosures
GLib.Callbacks.drop_closures_SourceFunc SourceFunc
func))
    let userData :: Ptr ()
userData = FunPtr C_SourceFunc -> Ptr ()
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr FunPtr C_SourceFunc
func'
    let notify :: FunPtr (Ptr a -> IO ())
notify = FunPtr (Ptr a -> IO ())
forall a. FunPtr (Ptr a -> IO ())
safeFreeFunPtrPtr
    CInt
result <- Ptr IOSchedulerJob
-> FunPtr C_SourceFunc
-> Ptr ()
-> FunPtr C_DestroyNotify
-> IO CInt
g_io_scheduler_job_send_to_mainloop Ptr IOSchedulerJob
job' FunPtr C_SourceFunc
func' Ptr ()
userData FunPtr C_DestroyNotify
forall a. FunPtr (Ptr a -> IO ())
notify
    let result' :: Bool
result' = (CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0) CInt
result
    IOSchedulerJob -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr IOSchedulerJob
job
    Bool -> SourceFunc
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
result'

#if defined(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 'P.Nothing' as /@notify@/ to
-- 'GI.Gio.Functions.ioSchedulerPushJob' or by using refcounting for /@userData@/.
iOSchedulerJobSendToMainloopAsync ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    IOSchedulerJob
    -- ^ /@job@/: a t'GI.Gio.Structs.IOSchedulerJob.IOSchedulerJob'
    -> GLib.Callbacks.SourceFunc
    -- ^ /@func@/: a t'GI.GLib.Callbacks.SourceFunc' callback that will be called in the original thread
    -> m ()
iOSchedulerJobSendToMainloopAsync :: IOSchedulerJob -> SourceFunc -> m ()
iOSchedulerJobSendToMainloopAsync IOSchedulerJob
job SourceFunc
func = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    Ptr IOSchedulerJob
job' <- IOSchedulerJob -> IO (Ptr IOSchedulerJob)
forall a. (HasCallStack, ManagedPtrNewtype a) => a -> IO (Ptr a)
unsafeManagedPtrGetPtr IOSchedulerJob
job
    FunPtr C_SourceFunc
func' <- C_SourceFunc -> IO (FunPtr C_SourceFunc)
GLib.Callbacks.mk_SourceFunc (Maybe (Ptr (FunPtr C_SourceFunc))
-> SourceFunc_WithClosures -> C_SourceFunc
GLib.Callbacks.wrap_SourceFunc Maybe (Ptr (FunPtr C_SourceFunc))
forall a. Maybe a
Nothing (SourceFunc -> SourceFunc_WithClosures
GLib.Callbacks.drop_closures_SourceFunc SourceFunc
func))
    let userData :: Ptr ()
userData = FunPtr C_SourceFunc -> Ptr ()
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr FunPtr C_SourceFunc
func'
    let notify :: FunPtr (Ptr a -> IO ())
notify = FunPtr (Ptr a -> IO ())
forall a. FunPtr (Ptr a -> IO ())
safeFreeFunPtrPtr
    Ptr IOSchedulerJob
-> FunPtr C_SourceFunc -> Ptr () -> FunPtr C_DestroyNotify -> IO ()
g_io_scheduler_job_send_to_mainloop_async Ptr IOSchedulerJob
job' FunPtr C_SourceFunc
func' Ptr ()
userData FunPtr C_DestroyNotify
forall a. FunPtr (Ptr a -> IO ())
notify
    IOSchedulerJob -> IO ()
forall a. ManagedPtrNewtype a => a -> IO ()
touchManagedPtr IOSchedulerJob
job
    () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

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

#endif

#if defined(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 @info
#else
    fromLabel _ = O.overloadedMethod @info
#endif

#endif