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

The `GMainLoop` struct is an opaque data type
representing the main event loop of a GLib or GTK+ application.
-}

module GI.GLib.Structs.MainLoop
    ( 

-- * Exported types
    MainLoop(..)                            ,
    noMainLoop                              ,


 -- * Methods
-- ** mainLoopGetContext
    mainLoopGetContext                      ,


-- ** mainLoopIsRunning
    mainLoopIsRunning                       ,


-- ** mainLoopNew
    mainLoopNew                             ,


-- ** mainLoopQuit
    mainLoopQuit                            ,


-- ** mainLoopRef
    mainLoopRef                             ,


-- ** mainLoopRun
    mainLoopRun                             ,


-- ** mainLoopUnref
    mainLoopUnref                           ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.GLib.Types
import GI.GLib.Callbacks

newtype MainLoop = MainLoop (ForeignPtr MainLoop)
foreign import ccall "g_main_loop_get_type" c_g_main_loop_get_type :: 
    IO GType

instance BoxedObject MainLoop where
    boxedType _ = c_g_main_loop_get_type

noMainLoop :: Maybe MainLoop
noMainLoop = Nothing

-- method MainLoop::new
-- method type : Constructor
-- Args : [Arg {argName = "context", argType = TInterface "GLib" "MainContext", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "is_running", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "context", argType = TInterface "GLib" "MainContext", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "is_running", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "GLib" "MainLoop"
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_new" g_main_loop_new :: 
    Ptr MainContext ->                      -- context : TInterface "GLib" "MainContext"
    CInt ->                                 -- is_running : TBasicType TBoolean
    IO (Ptr MainLoop)


mainLoopNew ::
    (MonadIO m) =>
    Maybe (MainContext) ->                  -- context
    Bool ->                                 -- is_running
    m MainLoop
mainLoopNew context is_running = liftIO $ do
    maybeContext <- case context of
        Nothing -> return nullPtr
        Just jContext -> do
            let jContext' = unsafeManagedPtrGetPtr jContext
            return jContext'
    let is_running' = (fromIntegral . fromEnum) is_running
    result <- g_main_loop_new maybeContext is_running'
    checkUnexpectedReturnNULL "g_main_loop_new" result
    result' <- (wrapBoxed MainLoop) result
    whenJust context touchManagedPtr
    return result'

-- method MainLoop::get_context
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "GLib" "MainContext"
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_get_context" g_main_loop_get_context :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO (Ptr MainContext)


mainLoopGetContext ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m MainContext
mainLoopGetContext _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- g_main_loop_get_context _obj'
    checkUnexpectedReturnNULL "g_main_loop_get_context" result
    result' <- (newBoxed MainContext) result
    touchManagedPtr _obj
    return result'

-- method MainLoop::is_running
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_is_running" g_main_loop_is_running :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO CInt


mainLoopIsRunning ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m Bool
mainLoopIsRunning _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- g_main_loop_is_running _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'

-- method MainLoop::quit
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_quit" g_main_loop_quit :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO ()


mainLoopQuit ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m ()
mainLoopQuit _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_main_loop_quit _obj'
    touchManagedPtr _obj
    return ()

-- method MainLoop::ref
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "GLib" "MainLoop"
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_ref" g_main_loop_ref :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO (Ptr MainLoop)


mainLoopRef ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m MainLoop
mainLoopRef _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    result <- g_main_loop_ref _obj'
    checkUnexpectedReturnNULL "g_main_loop_ref" result
    result' <- (wrapBoxed MainLoop) result
    touchManagedPtr _obj
    return result'

-- method MainLoop::run
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_run" g_main_loop_run :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO ()


mainLoopRun ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m ()
mainLoopRun _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_main_loop_run _obj'
    touchManagedPtr _obj
    return ()

-- method MainLoop::unref
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "GLib" "MainLoop", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_main_loop_unref" g_main_loop_unref :: 
    Ptr MainLoop ->                         -- _obj : TInterface "GLib" "MainLoop"
    IO ()


mainLoopUnref ::
    (MonadIO m) =>
    MainLoop ->                             -- _obj
    m ()
mainLoopUnref _obj = liftIO $ do
    let _obj' = unsafeManagedPtrGetPtr _obj
    g_main_loop_unref _obj'
    touchManagedPtr _obj
    return ()