    {-# LANGUAGE DeriveDataTypeable #-}
    {-# LANGUAGE DeriveGeneric #-}
    {-# LANGUAGE OverloadedStrings #-}
    module SDL.Time
      ( -- * Time Measurement
        ticks
      , time

        -- * Timer
      , delay
      , TimerCallback
      , Timer
      , RetriggerTimer(..)
      , addTimer
      , removeTimer
      ) where

    import Control.Monad.IO.Class (MonadIO, liftIO)
    import Data.Data (Data)
    import Data.Typeable
    import Data.Word
    import Foreign
    import GHC.Generics (Generic)

    import SDL.Internal.Exception

    import qualified SDL.Raw.Timer as Raw
    import qualified SDL.Raw.Types as Raw

    -- | Number of milliseconds since library initialization.
    --
    -- See @<https://wiki.libsdl.org/SDL_GetTicks SDL_GetTicks>@ for C documentation.
    ticks :: MonadIO m => m Word32
    ticks :: forall (m :: Type -> Type). MonadIO m => m Word32
    ticks = m Word32
    forall (m :: Type -> Type). MonadIO m => m Word32
    Raw.getTicks

    -- | The current time in seconds since some arbitrary starting point (consist over the life of the application).
    --
    -- This time is derived from the system's performance counter - see @<https://wiki.libsdl.org/SDL_GetPerformanceFrequency SDL_GetPerformanceFrequency>@ and @<https://wiki.libsdl.org/SDL_GetPerformanceCounter SDL_GetPerformanceCounter>@ for C documentation about the implementation.
    time :: (Fractional a, MonadIO m) => m a
    time :: forall a (m :: Type -> Type). (Fractional a, MonadIO m) => m a
    time = do
      Word64
    freq <- m Word64
    forall (m :: Type -> Type). MonadIO m => m Word64
    Raw.getPerformanceFrequency
      Word64
    cnt <- m Word64
    forall (m :: Type -> Type). MonadIO m => m Word64
    Raw.getPerformanceCounter
      a -> m a
    forall a. a -> m a
    forall (m :: Type -> Type) a. Monad m => a -> m a
    return (a -> m a) -> a -> m a
    forall a b. (a -> b) -> a -> b
    $ Word64 -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    cnt a -> a -> a
    forall a. Fractional a => a -> a -> a
    / Word64 -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    freq

    -- | Wait a specified number of milliseconds before returning.
    --
    -- Users are generally recommended to use 'threadDelay' instead, to take advantage of the abilities of the Haskell runtime.
    --
    -- See @<https://wiki.libsdl.org/SDL_Delay SDL_Delay>@ for C documentation.
    delay :: MonadIO m => Word32 -> m ()
    delay :: forall (m :: Type -> Type). MonadIO m => Word32 -> m ()
    delay = Word32 -> m ()
    forall (m :: Type -> Type). MonadIO m => Word32 -> m ()
    Raw.delay

    -- | 'RetriggerTimer' allows a callback to inform SDL if the timer should be retriggered or cancelled
    data RetriggerTimer
      = Reschedule Word32
        -- ^ Retrigger the timer again in a given number of milliseconds.
      | Cancel
        -- ^ Cancel future invocations of this timer.
      deriving (Typeable RetriggerTimer
    Typeable RetriggerTimer =>
    (forall (c :: Type -> Type).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> RetriggerTimer -> c RetriggerTimer)
    -> (forall (c :: Type -> Type).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c RetriggerTimer)
    -> (RetriggerTimer -> Constr)
    -> (RetriggerTimer -> DataType)
    -> (forall (t :: Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c RetriggerTimer))
    -> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c RetriggerTimer))
    -> ((forall b. Data b => b -> b)
        -> RetriggerTimer -> RetriggerTimer)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r)
    -> (forall u.
        (forall d. Data d => d -> u) -> RetriggerTimer -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> RetriggerTimer -> u)
    -> (forall (m :: Type -> Type).
        Monad m =>
        (forall d. Data d => d -> m d)
        -> RetriggerTimer -> m RetriggerTimer)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> RetriggerTimer -> m RetriggerTimer)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d)
        -> RetriggerTimer -> m RetriggerTimer)
    -> Data RetriggerTimer
    RetriggerTimer -> Constr
    RetriggerTimer -> DataType
    (forall b. Data b => b -> b) -> RetriggerTimer -> RetriggerTimer
    forall a.
    Typeable a =>
    (forall (c :: Type -> Type).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> a -> c a)
    -> (forall (c :: Type -> Type).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c a)
    -> (a -> Constr)
    -> (a -> DataType)
    -> (forall (t :: Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c a))
    -> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
    -> ((forall b. Data b => b -> b) -> a -> a)
    -> (forall r r'.
        (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall r r'.
        (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall u. (forall d. Data d => d -> u) -> a -> [u])
    -> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
    -> (forall (m :: Type -> Type).
        Monad m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> Data a
    forall u.
    Int -> (forall d. Data d => d -> u) -> RetriggerTimer -> u
    forall u. (forall d. Data d => d -> u) -> RetriggerTimer -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c RetriggerTimer
    forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> RetriggerTimer -> c RetriggerTimer
    forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c RetriggerTimer)
    forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c RetriggerTimer)
    $cgfoldl :: forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> RetriggerTimer -> c RetriggerTimer
    gfoldl :: forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> RetriggerTimer -> c RetriggerTimer
    $cgunfold :: forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c RetriggerTimer
    gunfold :: forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c RetriggerTimer
    $ctoConstr :: RetriggerTimer -> Constr
    toConstr :: RetriggerTimer -> Constr
    $cdataTypeOf :: RetriggerTimer -> DataType
    dataTypeOf :: RetriggerTimer -> DataType
    $cdataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c RetriggerTimer)
    dataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c RetriggerTimer)
    $cdataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c RetriggerTimer)
    dataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c RetriggerTimer)
    $cgmapT :: (forall b. Data b => b -> b) -> RetriggerTimer -> RetriggerTimer
    gmapT :: (forall b. Data b => b -> b) -> RetriggerTimer -> RetriggerTimer
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> RetriggerTimer -> r
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> RetriggerTimer -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> RetriggerTimer -> [u]
    $cgmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> RetriggerTimer -> u
    gmapQi :: forall u.
    Int -> (forall d. Data d => d -> u) -> RetriggerTimer -> u
    $cgmapM :: forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    gmapM :: forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    $cgmapMp :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    gmapMp :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    $cgmapMo :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    gmapMo :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> RetriggerTimer -> m RetriggerTimer
    Data, RetriggerTimer -> RetriggerTimer -> Bool
    (RetriggerTimer -> RetriggerTimer -> Bool)
    -> (RetriggerTimer -> RetriggerTimer -> Bool) -> Eq RetriggerTimer
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: RetriggerTimer -> RetriggerTimer -> Bool
    == :: RetriggerTimer -> RetriggerTimer -> Bool
    $c/= :: RetriggerTimer -> RetriggerTimer -> Bool
    /= :: RetriggerTimer -> RetriggerTimer -> Bool
    Eq, (forall x. RetriggerTimer -> Rep RetriggerTimer x)
    -> (forall x. Rep RetriggerTimer x -> RetriggerTimer)
    -> Generic RetriggerTimer
    forall x. Rep RetriggerTimer x -> RetriggerTimer
    forall x. RetriggerTimer -> Rep RetriggerTimer x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. RetriggerTimer -> Rep RetriggerTimer x
    from :: forall x. RetriggerTimer -> Rep RetriggerTimer x
    $cto :: forall x. Rep RetriggerTimer x -> RetriggerTimer
    to :: forall x. Rep RetriggerTimer x -> RetriggerTimer
    Generic, Eq RetriggerTimer
    Eq RetriggerTimer =>
    (RetriggerTimer -> RetriggerTimer -> Ordering)
    -> (RetriggerTimer -> RetriggerTimer -> Bool)
    -> (RetriggerTimer -> RetriggerTimer -> Bool)
    -> (RetriggerTimer -> RetriggerTimer -> Bool)
    -> (RetriggerTimer -> RetriggerTimer -> Bool)
    -> (RetriggerTimer -> RetriggerTimer -> RetriggerTimer)
    -> (RetriggerTimer -> RetriggerTimer -> RetriggerTimer)
    -> Ord RetriggerTimer
    RetriggerTimer -> RetriggerTimer -> Bool
    RetriggerTimer -> RetriggerTimer -> Ordering
    RetriggerTimer -> RetriggerTimer -> RetriggerTimer
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: RetriggerTimer -> RetriggerTimer -> Ordering
    compare :: RetriggerTimer -> RetriggerTimer -> Ordering
    $c< :: RetriggerTimer -> RetriggerTimer -> Bool
    < :: RetriggerTimer -> RetriggerTimer -> Bool
    $c<= :: RetriggerTimer -> RetriggerTimer -> Bool
    <= :: RetriggerTimer -> RetriggerTimer -> Bool
    $c> :: RetriggerTimer -> RetriggerTimer -> Bool
    > :: RetriggerTimer -> RetriggerTimer -> Bool
    $c>= :: RetriggerTimer -> RetriggerTimer -> Bool
    >= :: RetriggerTimer -> RetriggerTimer -> Bool
    $cmax :: RetriggerTimer -> RetriggerTimer -> RetriggerTimer
    max :: RetriggerTimer -> RetriggerTimer -> RetriggerTimer
    $cmin :: RetriggerTimer -> RetriggerTimer -> RetriggerTimer
    min :: RetriggerTimer -> RetriggerTimer -> RetriggerTimer
    Ord, ReadPrec [RetriggerTimer]
    ReadPrec RetriggerTimer
    Int -> ReadS RetriggerTimer
    ReadS [RetriggerTimer]
    (Int -> ReadS RetriggerTimer)
    -> ReadS [RetriggerTimer]
    -> ReadPrec RetriggerTimer
    -> ReadPrec [RetriggerTimer]
    -> Read RetriggerTimer
    forall a.
    (Int -> ReadS a)
    -> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
    $creadsPrec :: Int -> ReadS RetriggerTimer
    readsPrec :: Int -> ReadS RetriggerTimer
    $creadList :: ReadS [RetriggerTimer]
    readList :: ReadS [RetriggerTimer]
    $creadPrec :: ReadPrec RetriggerTimer
    readPrec :: ReadPrec RetriggerTimer
    $creadListPrec :: ReadPrec [RetriggerTimer]
    readListPrec :: ReadPrec [RetriggerTimer]
    Read, Int -> RetriggerTimer -> ShowS
    [RetriggerTimer] -> ShowS
    RetriggerTimer -> String
    (Int -> RetriggerTimer -> ShowS)
    -> (RetriggerTimer -> String)
    -> ([RetriggerTimer] -> ShowS)
    -> Show RetriggerTimer
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> RetriggerTimer -> ShowS
    showsPrec :: Int -> RetriggerTimer -> ShowS
    $cshow :: RetriggerTimer -> String
    show :: RetriggerTimer -> String
    $cshowList :: [RetriggerTimer] -> ShowS
    showList :: [RetriggerTimer] -> ShowS
    Show, Typeable)

    -- | A 'TimerCallback' is called with the interval size of the callback. It can return information as to whether or not the timer should continue to exist.
    type TimerCallback = Word32 -> IO RetriggerTimer

    -- | A timer created by 'addTimer'. This 'Timer' can be removed with 'removeTimer'.
    newtype Timer =
      Timer {Timer -> IO Bool
    runTimerRemoval :: IO Bool}

    -- | Set up a callback function to be run on a separate thread after the specified number of milliseconds has elapsed.
    --
    -- See @<https://wiki.libsdl.org/SDL_AddTimer SDL_AddTimer>@ for C documentation.
    addTimer :: MonadIO m => Word32 -> TimerCallback -> m Timer
    addTimer :: forall (m :: Type -> Type).
    MonadIO m =>
    Word32 -> TimerCallback -> m Timer
    addTimer Word32
    timeout TimerCallback
    callback = IO Timer -> m Timer
    forall a. IO a -> m a
    forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
    liftIO (IO Timer -> m Timer) -> IO Timer -> m Timer
    forall a b. (a -> b) -> a -> b
    $ do
        TimerCallback
    cb <- (Word32 -> Ptr () -> IO Word32) -> IO TimerCallback
    Raw.mkTimerCallback Word32 -> Ptr () -> IO Word32
    wrappedCb
        TimerID
    tid <- Text -> Text -> IO TimerID -> IO TimerID
    forall a (m :: Type -> Type).
    (Eq a, MonadIO m, Num a) =>
    Text -> Text -> m a -> m a
    throwIf0 Text
    "addTimer" Text
    "SDL_AddTimer" (IO TimerID -> IO TimerID) -> IO TimerID -> IO TimerID
    forall a b. (a -> b) -> a -> b
    $ Word32 -> TimerCallback -> Ptr () -> IO TimerID
    forall (m :: Type -> Type).
    MonadIO m =>
    Word32 -> TimerCallback -> Ptr () -> m TimerID
    Raw.addTimer Word32
    timeout TimerCallback
    cb Ptr ()
    forall a. Ptr a
    nullPtr
        Timer -> IO Timer
    forall a. a -> IO a
    forall (m :: Type -> Type) a. Monad m => a -> m a
    return (IO Bool -> Timer
    Timer (IO Bool -> Timer) -> IO Bool -> Timer
    forall a b. (a -> b) -> a -> b
    $ TimerCallback -> TimerID -> IO Bool
    auxRemove TimerCallback
    cb TimerID
    tid)
      where
        wrappedCb :: Word32 -> Ptr () -> IO Word32
        wrappedCb :: Word32 -> Ptr () -> IO Word32
    wrappedCb Word32
    w Ptr ()
    _ = do
          RetriggerTimer
    next <- TimerCallback
    callback Word32
    w
          Word32 -> IO Word32
    forall a. a -> IO a
    forall (m :: Type -> Type) a. Monad m => a -> m a
    return (Word32 -> IO Word32) -> Word32 -> IO Word32
    forall a b. (a -> b) -> a -> b
    $ case RetriggerTimer
    next of
            RetriggerTimer
    Cancel       -> Word32
    0
            Reschedule Word32
    n -> Word32
    n

        auxRemove :: Raw.TimerCallback -> Raw.TimerID -> IO Bool
        auxRemove :: TimerCallback -> TimerID -> IO Bool
    auxRemove TimerCallback
    cb TimerID
    tid = do
          Bool
    isSuccess <- TimerID -> IO Bool
    forall (m :: Type -> Type). MonadIO m => TimerID -> m Bool
    Raw.removeTimer TimerID
    tid
          if (Bool
    isSuccess)
            then TimerCallback -> IO ()
    forall a. FunPtr a -> IO ()
    freeHaskellFunPtr TimerCallback
    cb IO () -> IO Bool -> IO Bool
    forall a b. IO a -> IO b -> IO b
    forall (m :: Type -> Type) a b. Monad m => m a -> m b -> m b
    >> Bool -> IO Bool
    forall a. a -> IO a
    forall (m :: Type -> Type) a. Monad m => a -> m a
    return Bool
    True
            else Bool -> IO Bool
    forall a. a -> IO a
    forall (m :: Type -> Type) a. Monad m => a -> m a
    return Bool
    False

    -- | Remove a 'Timer'.
    --
    -- See @<https://wiki.libsdl.org/SDL_RemoveTimer SDL_RemoveTimer>@ for C documentation.
    removeTimer :: MonadIO m => Timer -> m Bool
    removeTimer :: forall (m :: Type -> Type). MonadIO m => Timer -> m Bool
    removeTimer Timer
    f = IO Bool -> m Bool
    forall a. IO a -> m a
    forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
    liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
    forall a b. (a -> b) -> a -> b
    $ Timer -> IO Bool
    runTimerRemoval Timer
    f