module Sound.Alsa.Sequencer.Errors where
import Foreign.C.Types(CInt)
import Foreign.C.String(CString,peekCString)
import Control.Exception(throwDyn,catchDyn)
import Data.Word
import Data.Typeable
data AlsaException = AlsaException
{ exception_code :: !Word
, exception_description :: !String
}
instance Eq AlsaException where
x == y = exception_code x == exception_code y
instance Ord AlsaException where
compare x y = compare (exception_code x) (exception_code y)
instance Typeable AlsaException where
typeOf _ = mkTyConApp (mkTyCon "Sound.Alsa.Sequencer.AlsaException") []
strerror :: CInt -> IO String
strerror x = peekCString =<< snd_strerror x
foreign import ccall "alsa/asoundlib.h snd_strerror"
snd_strerror :: CInt -> IO CString
alsa_catch :: IO a -> (AlsaException -> IO a) -> IO a
alsa_catch = catchDyn
check_error :: CInt -> IO Word
check_error = check_error' fromIntegral (const Nothing)
check_error' :: (CInt -> a) -> (CInt -> Maybe a) -> CInt -> IO a
check_error' ok err x
| x >= 0 = return (ok x)
| otherwise = case err x of
Just a -> return a
_ -> do msg <- strerror x
throwDyn AlsaException
{ exception_code = fromIntegral (negate x)
, exception_description = msg
}
check_error_ :: CInt -> IO ()
check_error_ x = check_error x >> return ()