{-# LINE 2 "./System/Glib/MainLoop.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) General
--
-- Author : Axel Simon, Manuel M. T. Chakravarty, Duncan Coutts
--
-- Created: 11 October 2005
--
-- Copyright (C) 2000..2005 Axel Simon, Manuel M. T. Chakravarty, Duncan Coutts
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- main event loop, and events
--
module System.Glib.MainLoop (
  HandlerId,
  timeoutAdd,
  timeoutAddFull,
  timeoutRemove,
  idleAdd,
  idleRemove,
  IOCondition(..),
  inputAdd,
  inputRemove,
  Priority,
  priorityLow,
  priorityDefaultIdle,
  priorityHighIdle,
  priorityDefault,
  priorityHigh,
  MainLoop,
  mainLoopNew,
  mainLoopRun,
  mainLoopQuit,
  mainLoopIsRunning,
  MainContext,
  mainContextNew,
  mainContextDefault,
  mainContextIteration,
  mainContextFindSourceById,
  Source(..),
  sourceAttach,
  sourceSetPriority,
  sourceGetPriority,
  sourceDestroy,

  sourceIsDestroyed

  ) where

import Control.Monad (liftM)

import System.Glib.FFI
import System.Glib.Flags
import System.Glib.GObject (DestroyNotify, destroyFunPtr)


{-# LINE 71 "./System/Glib/MainLoop.chs" #-}

type SourceFunc = FunPtr (((Ptr ()) -> (IO CInt)))
{-# LINE 73 "./System/Glib/MainLoop.chs" #-}

foreign import ccall "wrapper" mkSourceFunc :: (Ptr () -> IO (CInt)) -> IO SourceFunc

type HandlerId = (CUInt)
{-# LINE 77 "./System/Glib/MainLoop.chs" #-}

-- Turn a function into a function pointer and a destructor pointer.
--
makeCallback :: IO (CInt) -> IO (SourceFunc, DestroyNotify)
makeCallback :: IO CInt -> IO (SourceFunc, DestroyNotify)
makeCallback IO CInt
fun = do
  SourceFunc
funPtr <- (Ptr () -> IO CInt) -> IO SourceFunc
mkSourceFunc (IO CInt -> Ptr () -> IO CInt
forall a b. a -> b -> a
const IO CInt
fun)
  (SourceFunc, DestroyNotify) -> IO (SourceFunc, DestroyNotify)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (SourceFunc
funPtr, DestroyNotify
destroyFunPtr)

-- | Sets a function to be called at regular intervals, with the default
-- priority 'priorityDefault'. The function is called repeatedly until it
-- returns @False@, after which point the timeout function will not be called
-- again. The first call to the function will be at the end of the first interval.
--
-- Note that timeout functions may be delayed, due to the processing of other
-- event sources. Thus they should not be relied on for precise timing. After
-- each call to the timeout function, the time of the next timeout is
-- recalculated based on the current time and the given interval (it does not
-- try to 'catch up' time lost in delays).
--
timeoutAdd :: IO Bool -> Int -> IO HandlerId
timeoutAdd :: IO Bool -> Priority -> IO HandlerId
timeoutAdd IO Bool
fun Priority
msec = IO Bool -> Priority -> Priority -> IO HandlerId
timeoutAddFull IO Bool
fun Priority
priorityDefault Priority
msec

-- | Sets a function to be called at regular intervals, with the given
-- priority. The function is called repeatedly until it returns @False@, after
-- which point the timeout function will not be called again. The first call
-- to the function will be at the end of the first interval.
--
-- Note that timeout functions may be delayed, due to the processing of other
-- event sources. Thus they should not be relied on for precise timing. After
-- each call to the timeout function, the time of the next timeout is
-- recalculated based on the current time and the given interval (it does not
-- try to 'catch up' time lost in delays).
--
timeoutAddFull :: IO Bool -> Priority -> Int -> IO HandlerId
timeoutAddFull :: IO Bool -> Priority -> Priority -> IO HandlerId
timeoutAddFull IO Bool
fun Priority
pri Priority
msec = do
  (SourceFunc
funPtr, DestroyNotify
dPtr) <- IO CInt -> IO (SourceFunc, DestroyNotify)
makeCallback ((Bool -> CInt) -> IO Bool -> IO CInt
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> CInt
forall a. Num a => Bool -> a
fromBool IO Bool
fun)
  CInt
-> HandlerId
-> SourceFunc
-> Ptr ()
-> DestroyNotify
-> IO HandlerId
g_timeout_add_full
{-# LINE 114 "./System/Glib/MainLoop.chs" #-}
    (fromIntegral pri)
    (Priority -> HandlerId
forall a b. (Integral a, Num b) => a -> b
fromIntegral Priority
msec)
    SourceFunc
funPtr
    (SourceFunc -> Ptr ()
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr SourceFunc
funPtr)
    DestroyNotify
dPtr

-- | Remove a previously added timeout handler by its 'HandlerId'.
--
timeoutRemove :: HandlerId -> IO ()
timeoutRemove :: HandlerId -> IO ()
timeoutRemove HandlerId
id = HandlerId -> IO CInt
g_source_remove HandlerId
id IO CInt -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Add a callback that is called whenever the system is idle.
--
-- * A priority can be specified via an integer. This should usually be
-- 'priorityDefaultIdle'.
--
-- * If the function returns @False@ it will be removed.
--
idleAdd :: IO Bool -> Priority -> IO HandlerId
idleAdd :: IO Bool -> Priority -> IO HandlerId
idleAdd IO Bool
fun Priority
pri = do
  (SourceFunc
funPtr, DestroyNotify
dPtr) <- IO CInt -> IO (SourceFunc, DestroyNotify)
makeCallback ((Bool -> CInt) -> IO Bool -> IO CInt
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> CInt
forall a. Num a => Bool -> a
fromBool IO Bool
fun)
  CInt -> SourceFunc -> Ptr () -> DestroyNotify -> IO HandlerId
g_idle_add_full (Priority -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Priority
pri) SourceFunc
funPtr
    (SourceFunc -> Ptr ()
forall a b. FunPtr a -> Ptr b
castFunPtrToPtr SourceFunc
funPtr) DestroyNotify
dPtr

-- | Remove a previously added idle handler by its 'HandlerId'.
--
idleRemove :: HandlerId -> IO ()
idleRemove :: HandlerId -> IO ()
idleRemove HandlerId
id = HandlerId -> IO CInt
g_source_remove HandlerId
id IO CInt -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Flags representing a condition to watch for on a file descriptor.
--
-- [@IOIn@] There is data to read.
-- [@IOOut@] Data can be written (without blocking).
-- [@IOPri@] There is urgent data to read.
-- [@IOErr@] Error condition.
-- [@IOHup@] Hung up (the connection has been broken, usually for
-- pipes and sockets).
-- [@IOInvalid@] Invalid request. The file descriptor is not open.
--
data IOCondition = IOIn
                 | IOOut
                 | IOPri
                 | IOErr
                 | IOHup
                 | IOInvalid
                 deriving (IOCondition -> IOCondition -> Bool
(IOCondition -> IOCondition -> Bool)
-> (IOCondition -> IOCondition -> Bool) -> Eq IOCondition
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IOCondition -> IOCondition -> Bool
== :: IOCondition -> IOCondition -> Bool
$c/= :: IOCondition -> IOCondition -> Bool
/= :: IOCondition -> IOCondition -> Bool
Eq,IOCondition
IOCondition -> IOCondition -> Bounded IOCondition
forall a. a -> a -> Bounded a
$cminBound :: IOCondition
minBound :: IOCondition
$cmaxBound :: IOCondition
maxBound :: IOCondition
Bounded)
instance Enum IOCondition where
  fromEnum IOIn = 1
  fromEnum IOOut = 4
  fromEnum IOPri = 2
  fromEnum IOErr = 8
  fromEnum IOHup = 16
  fromEnum IOInvalid = 32

  toEnum 1 = IOIn
  toEnum 4 = IOOut
  toEnum 2 = IOPri
  toEnum 8 = IOErr
  toEnum 16 = IOHup
  toEnum 32 = IOInvalid
  toEnum unmatched = error ("IOCondition.toEnum: Cannot match " ++ show unmatched)

  succ :: IOCondition -> IOCondition
succ IOCondition
IOIn = IOCondition
IOOut
  succ IOCondition
IOOut = IOCondition
IOPri
  succ IOCondition
IOPri = IOCondition
IOErr
  succ IOCondition
IOErr = IOCondition
IOHup
  succ IOCondition
IOHup = IOCondition
IOInvalid
  succ IOCondition
_ = IOCondition
forall a. HasCallStack => a
undefined

  pred IOOut = IOIn
  pred IOPri = IOOut
  pred IOErr = IOPri
  pred IOHup = IOErr
  pred IOInvalid = IOHup
  pred _ = undefined

  enumFromTo :: IOCondition -> IOCondition -> [IOCondition]
enumFromTo IOCondition
x IOCondition
y | IOCondition -> Priority
forall a. Enum a => a -> Priority
fromEnum IOCondition
x Priority -> Priority -> Bool
forall a. Eq a => a -> a -> Bool
== IOCondition -> Priority
forall a. Enum a => a -> Priority
fromEnum IOCondition
y = [ IOCondition
y ]
                 | otherwise = x : enumFromTo (succ x) y
  enumFrom x = enumFromTo x IOInvalid
  enumFromThen :: IOCondition -> IOCondition -> [IOCondition]
enumFromThen IOCondition
_ IOCondition
_ =     [Char] -> [IOCondition]
forall a. HasCallStack => [Char] -> a
error [Char]
"Enum IOCondition: enumFromThen not implemented"
  enumFromThenTo :: IOCondition -> IOCondition -> IOCondition -> [IOCondition]
enumFromThenTo IOCondition
_ IOCondition
_ IOCondition
_ =     [Char] -> [IOCondition]
forall a. HasCallStack => [Char] -> a
error [Char]
"Enum IOCondition: enumFromThenTo not implemented"

{-# LINE 161 "./System/Glib/MainLoop.chs" #-}
instance Flags IOCondition

newtype IOChannel = IOChannel (Ptr (IOChannel))
{-# LINE 164 "./System/Glib/MainLoop.chs" #-}
type IOFunc = FunPtr (((Ptr IOChannel) -> (CInt -> ((Ptr ()) -> (IO CInt)))))
{-# LINE 165 "./System/Glib/MainLoop.chs" #-}

foreign import ccall "wrapper" mkIOFunc :: (Ptr IOChannel -> CInt -> Ptr () -> IO (CInt)) -> IO IOFunc

type FD = Int

-- | Adds the file descriptor into the main event loop with the given priority.
--
inputAdd ::
    FD -- ^ a file descriptor
 -> [IOCondition] -- ^ the condition to watch for
 -> Priority -- ^ the priority of the event source
 -> IO Bool -- ^ the function to call when the condition is satisfied.
                  -- The function should return False if the event source
                  -- should be removed.
 -> IO HandlerId -- ^ the event source id
inputAdd fd conds pri fun = do
  funPtr <- mkIOFunc (\_ _ _ -> liftM fromBool fun)
  channel <- g_io_channel_unix_new (fromIntegral fd)
  (\(IOChannel arg1) arg2 arg3 arg4 arg5 arg6 -> g_io_add_watch_full arg1 arg2 arg3 arg4 arg5 arg6)
{-# LINE 184 "./System/Glib/MainLoop.chs" #-}
    (IOChannel channel)
    (fromIntegral pri)
    ((fromIntegral . fromFlags) conds)
    funPtr
    (castFunPtrToPtr funPtr)
    destroyFunPtr

inputRemove :: HandlerId -> IO ()
inputRemove id = g_source_remove id >> return ()

-- Standard priorities







-- | Priorities for installing callbacks.
--
type Priority = Int

priorityHigh :: Int
priorityHigh :: Priority
priorityHigh = -Priority
100

priorityDefault :: Int
priorityDefault :: Priority
priorityDefault = Priority
0

priorityHighIdle :: Int
priorityHighIdle :: Priority
priorityHighIdle = Priority
100

priorityDefaultIdle :: Int
priorityDefaultIdle :: Priority
priorityDefaultIdle = Priority
200

priorityLow :: Int
priorityLow :: Priority
priorityLow = Priority
300

-- | A main event loop abstraction.
newtype MainLoop = MainLoop (ForeignPtr (MainLoop))
{-# LINE 223 "./System/Glib/MainLoop.chs" #-}

-- | An opaque datatype representing a set of sources to be handled in
-- a main loop.
newtype MainContext = MainContext (ForeignPtr (MainContext))
{-# LINE 227 "./System/Glib/MainLoop.chs" #-}

-- | Create a new 'MainLoop'.
mainLoopNew :: Maybe MainContext -- ^ @context@ - the context to use, or 'Nothing' to use the default context
            -> Bool -- ^ @isRunning@ - 'True' to indicate that the loop is running; 'False' otherwise
            -> IO MainLoop -- ^ the new 'MainLoop'
mainLoopNew :: Maybe MainContext -> Bool -> IO MainLoop
mainLoopNew Maybe MainContext
context Bool
isRunning =
    do let context' :: MainContext
context' = MainContext
-> (MainContext -> MainContext) -> Maybe MainContext -> MainContext
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ForeignPtr MainContext -> MainContext
MainContext ForeignPtr MainContext
forall a. ForeignPtr a
nullForeignPtr) MainContext -> MainContext
forall a. a -> a
id Maybe MainContext
context
       Ptr MainLoop
loopPtr <- (\(MainContext ForeignPtr MainContext
arg1) CInt
arg2 -> ForeignPtr MainContext
-> (Ptr MainContext -> IO (Ptr MainLoop)) -> IO (Ptr MainLoop)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainContext
arg1 ((Ptr MainContext -> IO (Ptr MainLoop)) -> IO (Ptr MainLoop))
-> (Ptr MainContext -> IO (Ptr MainLoop)) -> IO (Ptr MainLoop)
forall a b. (a -> b) -> a -> b
$ \Ptr MainContext
argPtr1 ->Ptr MainContext -> CInt -> IO (Ptr MainLoop)
g_main_loop_new Ptr MainContext
argPtr1 CInt
arg2) MainContext
context' (CInt -> IO (Ptr MainLoop)) -> CInt -> IO (Ptr MainLoop)
forall a b. (a -> b) -> a -> b
$ Bool -> CInt
forall a. Num a => Bool -> a
fromBool Bool
isRunning
       (ForeignPtr MainLoop -> MainLoop)
-> IO (ForeignPtr MainLoop) -> IO MainLoop
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ForeignPtr MainLoop -> MainLoop
MainLoop (IO (ForeignPtr MainLoop) -> IO MainLoop)
-> IO (ForeignPtr MainLoop) -> IO MainLoop
forall a b. (a -> b) -> a -> b
$ Ptr MainLoop -> FinalizerPtr MainLoop -> IO (ForeignPtr MainLoop)
forall a. Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
newForeignPtr Ptr MainLoop
loopPtr FinalizerPtr MainLoop
mainLoopFinalizer
foreign import ccall unsafe "&g_main_loop_unref"
    mainLoopFinalizer :: FunPtr (Ptr MainLoop -> IO ())

-- | Runs a main loop until 'mainLoopQuit' is called on the
-- loop. If this is called for the thread of the loop's
-- 'MainContext', it will process events from the loop, otherwise it
-- will simply wait.
mainLoopRun :: MainLoop
            -> IO ()
mainLoopRun :: MainLoop -> IO ()
mainLoopRun MainLoop
loop =
    (\(MainLoop ForeignPtr MainLoop
arg1) -> ForeignPtr MainLoop -> (Ptr MainLoop -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainLoop
arg1 ((Ptr MainLoop -> IO ()) -> IO ())
-> (Ptr MainLoop -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr MainLoop
argPtr1 ->Ptr MainLoop -> IO ()
g_main_loop_run Ptr MainLoop
argPtr1) MainLoop
loop

-- | Stops a 'MainLoop' from running. Any calls to mainLoopRun for the
-- loop will return.
mainLoopQuit :: MainLoop
             -> IO ()
mainLoopQuit :: MainLoop -> IO ()
mainLoopQuit MainLoop
loop =
    (\(MainLoop ForeignPtr MainLoop
arg1) -> ForeignPtr MainLoop -> (Ptr MainLoop -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainLoop
arg1 ((Ptr MainLoop -> IO ()) -> IO ())
-> (Ptr MainLoop -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr MainLoop
argPtr1 ->Ptr MainLoop -> IO ()
g_main_loop_quit Ptr MainLoop
argPtr1) MainLoop
loop

-- | Checks to see if the main loop is currently being run via mainLoopRun.
mainLoopIsRunning :: MainLoop
                  -> IO Bool
mainLoopIsRunning :: MainLoop -> IO Bool
mainLoopIsRunning MainLoop
loop =
    (CInt -> Bool) -> IO CInt -> IO Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ (\(MainLoop ForeignPtr MainLoop
arg1) -> ForeignPtr MainLoop -> (Ptr MainLoop -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainLoop
arg1 ((Ptr MainLoop -> IO CInt) -> IO CInt)
-> (Ptr MainLoop -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr MainLoop
argPtr1 ->Ptr MainLoop -> IO CInt
g_main_loop_is_running Ptr MainLoop
argPtr1) MainLoop
loop

-- | Gets a 'MainLoop's context.
mainLoopGetContext :: MainLoop
                   -> MainContext
mainLoopGetContext :: MainLoop -> MainContext
mainLoopGetContext MainLoop
loop =
    ForeignPtr MainContext -> MainContext
MainContext (ForeignPtr MainContext -> MainContext)
-> ForeignPtr MainContext -> MainContext
forall a b. (a -> b) -> a -> b
$ IO (ForeignPtr MainContext) -> ForeignPtr MainContext
forall a. IO a -> a
unsafePerformIO (IO (ForeignPtr MainContext) -> ForeignPtr MainContext)
-> IO (ForeignPtr MainContext) -> ForeignPtr MainContext
forall a b. (a -> b) -> a -> b
$
        (\(MainLoop ForeignPtr MainLoop
arg1) -> ForeignPtr MainLoop
-> (Ptr MainLoop -> IO (Ptr MainContext)) -> IO (Ptr MainContext)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainLoop
arg1 ((Ptr MainLoop -> IO (Ptr MainContext)) -> IO (Ptr MainContext))
-> (Ptr MainLoop -> IO (Ptr MainContext)) -> IO (Ptr MainContext)
forall a b. (a -> b) -> a -> b
$ \Ptr MainLoop
argPtr1 ->Ptr MainLoop -> IO (Ptr MainContext)
g_main_loop_get_context Ptr MainLoop
argPtr1) MainLoop
loop IO (Ptr MainContext)
-> (Ptr MainContext -> IO (ForeignPtr MainContext))
-> IO (ForeignPtr MainContext)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (Ptr MainContext
 -> FinalizerPtr MainContext -> IO (ForeignPtr MainContext))
-> FinalizerPtr MainContext
-> Ptr MainContext
-> IO (ForeignPtr MainContext)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Ptr MainContext
-> FinalizerPtr MainContext -> IO (ForeignPtr MainContext)
forall a. Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr MainContext
mainContextFinalizer

foreign import ccall unsafe "&g_main_context_unref"
    mainContextFinalizer :: FunPtr (Ptr MainContext -> IO ())

-- | Creates a new 'MainContext'.
mainContextNew :: IO MainContext
mainContextNew :: IO MainContext
mainContextNew =
    IO (Ptr MainContext) -> IO MainContext
newContextMarshal IO (Ptr MainContext)
g_main_context_new
{-# LINE 276 "./System/Glib/MainLoop.chs" #-}

-- | The default 'MainContext'. This is the main context used for main
-- loop functions when a main loop is not explicitly specified.
mainContextDefault :: MainContext
mainContextDefault :: MainContext
mainContextDefault =
    IO MainContext -> MainContext
forall a. IO a -> a
unsafePerformIO (IO MainContext -> MainContext) -> IO MainContext -> MainContext
forall a b. (a -> b) -> a -> b
$ IO (Ptr MainContext) -> IO MainContext
newContextMarshal IO (Ptr MainContext)
g_main_context_default
{-# LINE 282 "./System/Glib/MainLoop.chs" #-}

newContextMarshal :: IO (Ptr MainContext) -> IO MainContext
newContextMarshal IO (Ptr MainContext)
action =
    do Ptr MainContext
ptr <- IO (Ptr MainContext)
action
       (ForeignPtr MainContext -> MainContext)
-> IO (ForeignPtr MainContext) -> IO MainContext
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ForeignPtr MainContext -> MainContext
MainContext (IO (ForeignPtr MainContext) -> IO MainContext)
-> IO (ForeignPtr MainContext) -> IO MainContext
forall a b. (a -> b) -> a -> b
$ Ptr MainContext
-> FinalizerPtr MainContext -> IO (ForeignPtr MainContext)
forall a. Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
newForeignPtr Ptr MainContext
ptr FinalizerPtr MainContext
mainContextFinalizer

-- | Runs a single iteration for the given main loop. This involves
-- checking to see if any event sources are ready to be processed,
-- then if no events sources are ready and @mayBlock@ is 'True',
-- waiting for a source to become ready, then dispatching the
-- highest priority events sources that are ready. Note that even
-- when @mayBlock@ is 'True', it is still possible for
-- 'mainContextIteration' to return 'False', since the the wait
-- may be interrupted for other reasons than an event source
-- becoming ready.
mainContextIteration :: MainContext
                     -> Bool
                     -> IO Bool
mainContextIteration :: MainContext -> Bool -> IO Bool
mainContextIteration MainContext
context Bool
mayBlock =
    (CInt -> Bool) -> IO CInt -> IO Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ (\(MainContext ForeignPtr MainContext
arg1) CInt
arg2 -> ForeignPtr MainContext -> (Ptr MainContext -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainContext
arg1 ((Ptr MainContext -> IO CInt) -> IO CInt)
-> (Ptr MainContext -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr MainContext
argPtr1 ->Ptr MainContext -> CInt -> IO CInt
g_main_context_iteration Ptr MainContext
argPtr1 CInt
arg2) MainContext
context (Bool -> CInt
forall a. Num a => Bool -> a
fromBool Bool
mayBlock)

mainContextFindSourceById :: MainContext
                          -> HandlerId
                          -> IO Source
mainContextFindSourceById :: MainContext -> HandlerId -> IO Source
mainContextFindSourceById MainContext
context HandlerId
id =
    (\(MainContext ForeignPtr MainContext
arg1) HandlerId
arg2 -> ForeignPtr MainContext
-> (Ptr MainContext -> IO (Ptr ())) -> IO (Ptr ())
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainContext
arg1 ((Ptr MainContext -> IO (Ptr ())) -> IO (Ptr ()))
-> (Ptr MainContext -> IO (Ptr ())) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ \Ptr MainContext
argPtr1 ->Ptr MainContext -> HandlerId -> IO (Ptr ())
g_main_context_find_source_by_id Ptr MainContext
argPtr1 HandlerId
arg2) MainContext
context (HandlerId -> HandlerId
forall a b. (Integral a, Num b) => a -> b
fromIntegral HandlerId
id) IO (Ptr ()) -> (Ptr () -> IO Source) -> IO Source
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr Source -> IO Source
newSource (Ptr Source -> IO Source)
-> (Ptr () -> Ptr Source) -> Ptr () -> IO Source
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr () -> Ptr Source
forall a b. Ptr a -> Ptr b
castPtr

newtype Source = Source (ForeignPtr (Source))
{-# LINE 309 "./System/Glib/MainLoop.chs" #-}
newSource :: Ptr Source
          -> IO Source
newSource :: Ptr Source -> IO Source
newSource Ptr Source
sourcePtr =
    (ForeignPtr Source -> Source)
-> IO (ForeignPtr Source) -> IO Source
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ForeignPtr Source -> Source
Source (IO (ForeignPtr Source) -> IO Source)
-> IO (ForeignPtr Source) -> IO Source
forall a b. (a -> b) -> a -> b
$ Ptr Source -> FinalizerPtr Source -> IO (ForeignPtr Source)
forall a. Ptr a -> FinalizerPtr a -> IO (ForeignPtr a)
newForeignPtr Ptr Source
sourcePtr FinalizerPtr Source
sourceFinalizer
foreign import ccall unsafe "&g_source_unref"
    sourceFinalizer :: FunPtr (Ptr Source -> IO ())

sourceAttach :: Source
             -> MainContext
             -> IO HandlerId
sourceAttach :: Source -> MainContext -> IO HandlerId
sourceAttach Source
source MainContext
context =
    (HandlerId -> HandlerId) -> IO HandlerId -> IO HandlerId
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM HandlerId -> HandlerId
forall a b. (Integral a, Num b) => a -> b
fromIntegral (IO HandlerId -> IO HandlerId) -> IO HandlerId -> IO HandlerId
forall a b. (a -> b) -> a -> b
$ (\(Source ForeignPtr Source
arg1) (MainContext ForeignPtr MainContext
arg2) -> ForeignPtr Source -> (Ptr Source -> IO HandlerId) -> IO HandlerId
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Source
arg1 ((Ptr Source -> IO HandlerId) -> IO HandlerId)
-> (Ptr Source -> IO HandlerId) -> IO HandlerId
forall a b. (a -> b) -> a -> b
$ \Ptr Source
argPtr1 ->ForeignPtr MainContext
-> (Ptr MainContext -> IO HandlerId) -> IO HandlerId
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr MainContext
arg2 ((Ptr MainContext -> IO HandlerId) -> IO HandlerId)
-> (Ptr MainContext -> IO HandlerId) -> IO HandlerId
forall a b. (a -> b) -> a -> b
$ \Ptr MainContext
argPtr2 ->Ptr Source -> Ptr MainContext -> IO HandlerId
g_source_attach Ptr Source
argPtr1 Ptr MainContext
argPtr2) Source
source MainContext
context

sourceSetPriority :: Source
                  -> Priority
                  -> IO ()
sourceSetPriority :: Source -> Priority -> IO ()
sourceSetPriority Source
source Priority
priority =
    (\(Source ForeignPtr Source
arg1) CInt
arg2 -> ForeignPtr Source -> (Ptr Source -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Source
arg1 ((Ptr Source -> IO ()) -> IO ()) -> (Ptr Source -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Source
argPtr1 ->Ptr Source -> CInt -> IO ()
g_source_set_priority Ptr Source
argPtr1 CInt
arg2) Source
source (CInt -> IO ()) -> CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Priority -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Priority
priority

sourceGetPriority :: Source
                  -> IO Priority
sourceGetPriority :: Source -> IO Priority
sourceGetPriority Source
source =
    (CInt -> Priority) -> IO CInt -> IO Priority
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Priority
forall a b. (Integral a, Num b) => a -> b
fromIntegral (IO CInt -> IO Priority) -> IO CInt -> IO Priority
forall a b. (a -> b) -> a -> b
$ (\(Source ForeignPtr Source
arg1) -> ForeignPtr Source -> (Ptr Source -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Source
arg1 ((Ptr Source -> IO CInt) -> IO CInt)
-> (Ptr Source -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr Source
argPtr1 ->Ptr Source -> IO CInt
g_source_get_priority Ptr Source
argPtr1) Source
source

sourceDestroy :: Source
              -> IO ()
sourceDestroy :: Source -> IO ()
sourceDestroy Source
source =
    (\(Source ForeignPtr Source
arg1) -> ForeignPtr Source -> (Ptr Source -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Source
arg1 ((Ptr Source -> IO ()) -> IO ()) -> (Ptr Source -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Source
argPtr1 ->Ptr Source -> IO ()
g_source_destroy Ptr Source
argPtr1) Source
source


sourceIsDestroyed :: Source
                  -> IO Bool
sourceIsDestroyed :: Source -> IO Bool
sourceIsDestroyed Source
source =
    (CInt -> Bool) -> IO CInt -> IO Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ (\(Source ForeignPtr Source
arg1) -> ForeignPtr Source -> (Ptr Source -> IO CInt) -> IO CInt
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Source
arg1 ((Ptr Source -> IO CInt) -> IO CInt)
-> (Ptr Source -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \Ptr Source
argPtr1 ->Ptr Source -> IO CInt
g_source_is_destroyed Ptr Source
argPtr1) Source
source


sourceRemove :: HandlerId
             -> IO Bool
sourceRemove :: HandlerId -> IO Bool
sourceRemove HandlerId
tag =
    (CInt -> Bool) -> IO CInt -> IO Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (IO CInt -> IO Bool) -> IO CInt -> IO Bool
forall a b. (a -> b) -> a -> b
$ HandlerId -> IO CInt
g_source_remove (HandlerId -> IO CInt) -> HandlerId -> IO CInt
forall a b. (a -> b) -> a -> b
$ HandlerId -> HandlerId
forall a b. (Integral a, Num b) => a -> b
fromIntegral HandlerId
tag

foreign import ccall unsafe "g_timeout_add_full"
  g_timeout_add_full :: (CInt -> (CUInt -> ((FunPtr ((Ptr ()) -> (IO CInt))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO CUInt))))))

foreign import ccall safe "g_source_remove"
  g_source_remove :: (CUInt -> (IO CInt))

foreign import ccall unsafe "g_idle_add_full"
  g_idle_add_full :: (CInt -> ((FunPtr ((Ptr ()) -> (IO CInt))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO CUInt)))))

foreign import ccall unsafe "g_io_channel_unix_new"
  g_io_channel_unix_new :: (CInt -> (IO (Ptr IOChannel)))

foreign import ccall unsafe "g_io_add_watch_full"
  g_io_add_watch_full :: ((Ptr IOChannel) -> (CInt -> (CInt -> ((FunPtr ((Ptr IOChannel) -> (CInt -> ((Ptr ()) -> (IO CInt))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO CUInt)))))))

foreign import ccall safe "g_main_loop_new"
  g_main_loop_new :: ((Ptr MainContext) -> (CInt -> (IO (Ptr MainLoop))))

foreign import ccall safe "g_main_loop_run"
  g_main_loop_run :: ((Ptr MainLoop) -> (IO ()))

foreign import ccall safe "g_main_loop_quit"
  g_main_loop_quit :: ((Ptr MainLoop) -> (IO ()))

foreign import ccall safe "g_main_loop_is_running"
  g_main_loop_is_running :: ((Ptr MainLoop) -> (IO CInt))

foreign import ccall safe "g_main_loop_get_context"
  g_main_loop_get_context :: ((Ptr MainLoop) -> (IO (Ptr MainContext)))

foreign import ccall safe "g_main_context_new"
  g_main_context_new :: (IO (Ptr MainContext))

foreign import ccall safe "g_main_context_default"
  g_main_context_default :: (IO (Ptr MainContext))

foreign import ccall safe "g_main_context_iteration"
  g_main_context_iteration :: ((Ptr MainContext) -> (CInt -> (IO CInt)))

foreign import ccall safe "g_main_context_find_source_by_id"
  g_main_context_find_source_by_id :: ((Ptr MainContext) -> (CUInt -> (IO (Ptr ()))))

foreign import ccall safe "g_source_attach"
  g_source_attach :: ((Ptr Source) -> ((Ptr MainContext) -> (IO CUInt)))

foreign import ccall safe "g_source_set_priority"
  g_source_set_priority :: ((Ptr Source) -> (CInt -> (IO ())))

foreign import ccall safe "g_source_get_priority"
  g_source_get_priority :: ((Ptr Source) -> (IO CInt))

foreign import ccall safe "g_source_destroy"
  g_source_destroy :: ((Ptr Source) -> (IO ()))

foreign import ccall safe "g_source_is_destroyed"
  g_source_is_destroyed :: ((Ptr Source) -> (IO CInt))