{-# LINE 1 "src/Sound/Pulse/Serverinfo.hsc" #-}
{-
{-# LINE 2 "src/Sound/Pulse/Serverinfo.hsc" #-}
    Copyright 2016 Markus Ongyerth

    This file is part of pulseaudio-hs.

    Monky 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 3 of the License, or
    (at your option) any later version.

    Monky 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.

    You should have received a copy of the GNU Lesser General Public License
    along with pulseaudio-hs.  If not, see <http://www.gnu.org/licenses/>.
-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-|
Module      : Sound.Pulse.Serverinfo
Description : provides the time type used for pa_server_info.
Maintianer  : ongy
Stability   : experimental
-}
module Sound.Pulse.Serverinfo
    ( ServerInfo(..)
    , getServerInfo
    , getServerInfoM
    )
where


{-# LINE 36 "src/Sound/Pulse/Serverinfo.hsc" #-}

{-# LINE 37 "src/Sound/Pulse/Serverinfo.hsc" #-}

import Control.Applicative ((<$>), (<*>))
import Data.Word (Word32, Word)
import Sound.Pulse
import Sound.Pulse.SampleSpec
import Sound.Pulse.ChannelPosition
import Sound.Pulse.Context
import Sound.Pulse.Userdata
import Sound.Pulse.Operation

import Foreign.Storable
import Foreign.Ptr
import Foreign.C.String

-- |The type used for pa_server_info
data ServerInfo = ServerInfo
    { userName          :: String
    , hostName          :: String
    , serverVersion     :: String
    , serverName        :: String
    , sampleSpec        :: SampleSpec
    , defaultSinkName   :: String
    , defaultSourceName :: String
    , cookie            :: Word32
    , channelMap        :: ChannelMap
    } deriving (Eq, Show)

instance Storable ServerInfo where
    sizeOf _ = (200)
{-# LINE 66 "src/Sound/Pulse/Serverinfo.hsc" #-}
    alignment _ = (8)
{-# LINE 67 "src/Sound/Pulse/Serverinfo.hsc" #-}
    peek p = ServerInfo
        <$> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 0) p)
{-# LINE 69 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 8) p)
{-# LINE 70 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 16) p)
{-# LINE 71 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 24) p)
{-# LINE 72 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (\hsc_ptr -> peekByteOff hsc_ptr 32) p
{-# LINE 73 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 48) p)
{-# LINE 74 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (peekCString =<< (\hsc_ptr -> peekByteOff hsc_ptr 56) p)
{-# LINE 75 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (\hsc_ptr -> peekByteOff hsc_ptr 64) p
{-# LINE 76 "src/Sound/Pulse/Serverinfo.hsc" #-}
        <*> (\hsc_ptr -> peekByteOff hsc_ptr 68) p
{-# LINE 77 "src/Sound/Pulse/Serverinfo.hsc" #-}
    
    poke _ _ = error "PA: no poke for ServerInfo yet" -- TODO

type ServerInfoCB = Context -> Ptr ServerInfo -> Ptr Userdata -> IO ()
foreign import ccall "wrapper" mkServerInfoCB :: ServerInfoCB -> IO (FunPtr ServerInfoCB)

foreign import ccall "pa_context_get_server_info" pa_context_get_server_info :: Context -> FunPtr ServerInfoCB -> Ptr Userdata -> IO (Ptr UOperation)

-- |Get the server info.
getServerInfo
    :: Context -- ^Context for the server connection
    -> (ServerInfo -> IO ())
    -> IO ()
getServerInfo cxt fun = do
    funP <- mkServerInfoCB $ \_ ptr fP -> do
        fun =<< peek ptr
        freeHaskellFunPtr (castPtrToFunPtr fP)
    _ <- ptrToOperation =<< pa_context_get_server_info cxt funP (castFunPtrToPtr funP)
    return ()

getServerInfoM :: Pulse ServerInfo
getServerInfoM = Pulse getServerInfo