{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
module SDL.Power
(
getPowerInfo
, PowerState(..)
, BatteryState(..)
, Charge(..)
) where
import Control.Applicative
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Data (Data)
import Data.Typeable
import Foreign.C.Types
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils
import Foreign.Storable
import GHC.Generics (Generic)
import qualified SDL.Raw as Raw
getPowerInfo :: (Functor m, MonadIO m) => m PowerState
getPowerInfo =
liftIO $
alloca $ \secsPtr ->
alloca $ \pctPtr -> do
state <- Raw.getPowerInfo secsPtr pctPtr
let peekCharge = liftA2 Charge (maybePeek peek secsPtr) (maybePeek peek pctPtr)
case state of
Raw.SDL_POWERSTATE_ON_BATTERY -> fmap (Battery Draining) peekCharge
Raw.SDL_POWERSTATE_CHARGING -> fmap (Battery Charging) peekCharge
Raw.SDL_POWERSTATE_CHARGED -> fmap (Battery Charged) peekCharge
Raw.SDL_POWERSTATE_NO_BATTERY -> pure Mains
_ -> pure UnknownPowerState
data PowerState
= Battery BatteryState Charge
| Mains
| UnknownPowerState
deriving (Eq, Generic, Ord, Read, Show, Typeable)
data BatteryState
= Draining
| Charged
| Charging
deriving (Bounded, Data, Enum, Eq, Generic, Ord, Read, Show, Typeable)
data Charge =
Charge {chargeSecondsLeft :: Maybe CInt
,chargePercent :: Maybe CInt
}
deriving (Eq, Generic, Ord, Read, Show, Typeable)