{-| PRIVATE MODULE
Reference:
<http://www.alsa-project.org/alsa-doc/alsa-lib/group___sequencer.html>
-}

{-# LANGUAGE ForeignFunctionInterface #-}
module Sound.ALSA.Sequencer.Sequencer where

import qualified Sound.ALSA.Sequencer.Marshal.Sequencer as Seq
import qualified Sound.ALSA.Exception as Exc

import qualified Foreign.C.Types as C
import Foreign.C.Types (CSize, )
import Foreign.C.String (CString, withCAString, peekCString, )
import Foreign.Ptr (Ptr, )
import Foreign.Marshal.Alloc (alloca, )
import Foreign.Storable (peek, )

import Data.Word (Word, )

import Control.Exception (bracket, )


-- | Creates a new handle and opens a connection to the kernel sequencer
-- interface. After a client is created successfully,
-- a 'ClientStart' event is broadcast to the announce port.
-- May throw an exception.
-- See also: 'open_lconf', 'close', 'get_seq_type',
--   'get_seq_name', 'set_blocking', 'get_client_id'.

open
  :: Seq.OpenMode mode
        -- Read\/Write permissions
  => String
        {- ^
        The sequencer's \"name\".
        This is not a name that you make up for your own purposes;
        it has special significance to the ALSA library.
        Usually you need to pass 'defaultName' here
        or simply use 'openDefault'.
        -}
  -> Seq.BlockMode    -- ^ Blocking behavior
  -> IO (Seq.T mode)  -- ^ Handle to the sequencer.

open :: forall mode. OpenMode mode => String -> BlockMode -> IO (T mode)
open String
t BlockMode
bm = forall mode. (mode -> IO (T mode)) -> IO (T mode)
withOpenMode forall a b. (a -> b) -> a -> b
$ \mode
om -> forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Core)
p -> forall a. String -> (CString -> IO a) -> IO a
withCAString String
t forall a b. (a -> b) -> a -> b
$ \CString
s ->
  do forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"open" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr (Ptr Core) -> CString -> CInt -> CInt -> IO CInt
snd_seq_open Ptr (Ptr Core)
p CString
s (forall mode. OpenMode mode => mode -> CInt
Seq.expOpenMode mode
om) (BlockMode -> CInt
Seq.expBlockMode BlockMode
bm)
     forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall mode. Ptr Core -> T mode
Seq.Cons forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Core)
p

openDefault
  :: Seq.OpenMode mode
        -- Read\/Write permissions
  => Seq.BlockMode    -- ^ Blocking behavior
  -> IO (Seq.T mode)  -- ^ Handle to the sequencer.
openDefault :: forall mode. OpenMode mode => BlockMode -> IO (T mode)
openDefault = forall mode. OpenMode mode => String -> BlockMode -> IO (T mode)
open String
defaultName


foreign import ccall unsafe "alsa/asoundlib.h snd_seq_open"
  snd_seq_open :: Ptr (Ptr Seq.Core) -> CString -> C.CInt -> C.CInt -> IO C.CInt

withOpenMode :: (mode -> IO (Seq.T mode)) -> IO (Seq.T mode)
withOpenMode :: forall mode. (mode -> IO (T mode)) -> IO (T mode)
withOpenMode mode -> IO (T mode)
f = mode -> IO (T mode)
f forall a. HasCallStack => a
undefined


-- | Close the sequencer. Closes the sequencer client and releases its
-- resources. After a client is closed, an event with 'ClientExit' is
-- broadcast to announce port. The connection between other clients are
-- disconnected. Call this just before exiting your program.
-- NOTE: we could put this in a finalizer for the handle?

close
  :: Seq.T mode   -- ^ handle to the sequencer
  -> IO ()
close :: forall mode. T mode -> IO ()
close (Seq.Cons Ptr Core
h) = forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"close" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> IO CInt
snd_seq_close Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_close"
  snd_seq_close :: Ptr Seq.Core -> IO C.CInt


with
  :: Seq.OpenMode mode
        -- Read\/Write permissions
  => String
        {- ^
        The sequencer's \"name\".
        This is not a name that you make up for your own purposes;
        it has special significance to the ALSA library.
        Usually you need to pass 'defaultName' here
        or simply use 'withDefault'.
        -}
  -> Seq.BlockMode
        -- ^ Blocking behavior
  -> (Seq.T mode -> IO a)
        -- ^ Action on the sequencer, the result must be computed strictly.
  -> IO a
with :: forall mode a.
OpenMode mode =>
String -> BlockMode -> (T mode -> IO a) -> IO a
with String
t BlockMode
bm =
   forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (forall mode. OpenMode mode => String -> BlockMode -> IO (T mode)
open String
t BlockMode
bm) forall mode. T mode -> IO ()
close

withDefault
  :: Seq.OpenMode mode
  => Seq.BlockMode
  -> (Seq.T mode -> IO a)
  -> IO a
withDefault :: forall mode a.
OpenMode mode =>
BlockMode -> (T mode -> IO a) -> IO a
withDefault = forall mode a.
OpenMode mode =>
String -> BlockMode -> (T mode -> IO a) -> IO a
with String
defaultName


-- | This is the name that should be passed to 'open' in most cases.
defaultName :: String
defaultName :: String
defaultName = String
"default"


-- | Get identifier of a sequencer handle.
-- It is the same identifier specified in the call to 'open'.
getName
  :: Seq.T mode  -- ^ sequencer handle
  -> IO String   -- ^ ALSA identifier for the handle
getName :: forall mode. T mode -> IO String
getName (Seq.Cons Ptr Core
h) = CString -> IO String
peekCString forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> IO CString
snd_seq_name Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_name"
  snd_seq_name :: Ptr Seq.Core -> IO CString


-- | Change the blocking mode of the given client.
-- In block mode, the client falls into sleep when it fills the output
-- pool with events, or when it demands events from an empty input pool.
-- memory pool with full events. Clients that are sleeping due to
-- loack of space in the output pool are woken when a certain
-- amount of free space becomes available (see 'set_output_room').
setBlocking
  :: Seq.T mode     -- ^ sequencer handle
  -> Seq.BlockMode  -- ^ blocking mode
  -> IO ()
setBlocking :: forall mode. T mode -> BlockMode -> IO ()
setBlocking (Seq.Cons Ptr Core
h) BlockMode
m = forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_blocking" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CInt -> IO CInt
snd_seq_nonblock Ptr Core
h(BlockMode -> CInt
Seq.expBlockMode BlockMode
m)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_nonblock"
  snd_seq_nonblock :: Ptr Seq.Core -> C.CInt -> IO C.CInt



-- Buffers ---------------------------------------------------------------------

-- | Return the byte size of the output buffer.
getOutputBufferSize
  :: Seq.T mode   -- ^ Sequencer handle.
  -> IO Word  -- ^ Size of output buffer in bytes.

getOutputBufferSize :: forall mode. T mode -> IO Word
getOutputBufferSize (Seq.Cons Ptr Core
h) =
  forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Ptr Core -> IO CSize
snd_seq_get_output_buffer_size Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_get_output_buffer_size"
  snd_seq_get_output_buffer_size :: Ptr Seq.Core -> IO CSize


-- | Resize of the output buffer.
-- This function clears all output events (see 'drop_output').
setOutputBufferSize
  :: Seq.T mode   -- ^ Sequencer handle.
  -> Word     -- ^ New buffer size in bytes.
  -> IO ()

setOutputBufferSize :: forall mode. T mode -> Word -> IO ()
setOutputBufferSize (Seq.Cons Ptr Core
h) Word
x =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_output_buffer_size" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CSize -> IO CInt
snd_seq_set_output_buffer_size Ptr Core
h (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_set_output_buffer_size"
  snd_seq_set_output_buffer_size :: Ptr Seq.Core -> CSize -> IO C.CInt


-- | Return the byte size of input buffer.
getInputBufferSize
  :: Seq.T mode   -- ^ Sequencer handle.
  -> IO Word  -- ^ Size of input buffer in bytes.

getInputBufferSize :: forall mode. T mode -> IO Word
getInputBufferSize (Seq.Cons Ptr Core
h) =
  forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Ptr Core -> IO CSize
snd_seq_get_input_buffer_size Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_get_input_buffer_size"
   snd_seq_get_input_buffer_size :: Ptr Seq.Core -> IO CSize


-- | Resize the input buffer.
-- This function clears all input events (see 'drop_input').
setInputBufferSize
  :: Seq.T mode   -- ^ Sequencer handle.
  -> Word     -- ^ New byffer size in bytes.
  -> IO ()

setInputBufferSize :: forall mode. T mode -> Word -> IO ()
setInputBufferSize (Seq.Cons Ptr Core
h) Word
x =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_input_buffer_size" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CSize -> IO CInt
snd_seq_set_input_buffer_size Ptr Core
h (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_set_input_buffer_size"
  snd_seq_set_input_buffer_size :: Ptr Seq.Core -> CSize -> IO C.CInt



-- Pool management -------------------------------------------------------------

-- | Resize the output memory pool.
setPoolOutput
  :: Seq.T mode   -- ^ Sequencer handle.
  -> Word     -- ^ New size in bytes.
  -> IO ()

setPoolOutput :: forall mode. T mode -> Word -> IO ()
setPoolOutput (Seq.Cons Ptr Core
h) Word
x =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_pool_output" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CSize -> IO CInt
snd_seq_set_client_pool_output Ptr Core
h (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_set_client_pool_output"
  snd_seq_set_client_pool_output :: Ptr Seq.Core -> CSize -> IO C.CInt


-- | Specify how much space should become free before waking clients
-- that are blocked due to a lack of space in the output pool.
setPoolOutputRoom
  :: Seq.T mode   -- ^ Sequencer handle.
  -> Word     -- ^ Number of bytes need to wake up.
  -> IO ()

setPoolOutputRoom :: forall mode. T mode -> Word -> IO ()
setPoolOutputRoom (Seq.Cons Ptr Core
h) Word
x =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_pool_output_room" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CSize -> IO CInt
snd_seq_set_client_pool_output_room Ptr Core
h (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_set_client_pool_output_room"
  snd_seq_set_client_pool_output_room :: Ptr Seq.Core -> CSize -> IO C.CInt


-- | Reset the output pool.
resetPoolOutput
  :: Seq.T mode   -- ^ Sequencer handle.
  -> IO ()

resetPoolOutput :: forall mode. T mode -> IO ()
resetPoolOutput (Seq.Cons Ptr Core
h) =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"reset_pool_output" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> IO CInt
snd_seq_reset_pool_output Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_reset_pool_output"
  snd_seq_reset_pool_output :: Ptr Seq.Core -> IO C.CInt



-- | Resize the input memory pool.
setPoolInput
  :: Seq.T mode   -- ^ Sequencer handle.
  -> Word     -- ^ New size in bytes.
  -> IO ()

setPoolInput :: forall mode. T mode -> Word -> IO ()
setPoolInput (Seq.Cons Ptr Core
h) Word
x =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"set_pool_input" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> CSize -> IO CInt
snd_seq_set_client_pool_input Ptr Core
h (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
x)

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_set_client_pool_input"
  snd_seq_set_client_pool_input :: Ptr Seq.Core -> CSize -> IO C.CInt


-- | Reset the input pool.
resetPoolInput
  :: Seq.T mode   -- ^ Sequencer handle.
  -> IO ()

resetPoolInput :: forall mode. T mode -> IO ()
resetPoolInput (Seq.Cons Ptr Core
h) =
  forall a. Integral a => String -> a -> IO ()
Exc.checkResult_ String
"reset_pool_input" forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ptr Core -> IO CInt
snd_seq_reset_pool_input Ptr Core
h

foreign import ccall unsafe "alsa/asoundlib.h snd_seq_reset_pool_input"
  snd_seq_reset_pool_input :: Ptr Seq.Core -> IO C.CInt