{-# LINE 1 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
--------------------------------------------------------------------------------
{-# LINE 2 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
-- |
-- Module    : Sound.ALSA.Sequencer.Marshal.Event
-- Copyright : (c) Henning Thielemann, 2011
--             (c) Iavor S. Diatchki, 2007
-- License   : BSD3
--
-- Maintainer: Henning Thielemann
-- Stability : provisional
--
-- PRIVATE MODULE.
--
-- Here we have the various types used by the library,
-- and how they are imported\/exported to C.
--
-- NOTE: In the translations bellow we make the following assumptions
-- about the sizes of C types.
-- CChar  = 8 bits
-- CShort = 16 bit
-- CInt   = 32 bits
--------------------------------------------------------------------------------

module Sound.ALSA.Sequencer.Marshal.Event where


{-# LINE 26 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

import qualified Sound.ALSA.Sequencer.Marshal.Address as Addr
import qualified Sound.ALSA.Sequencer.Marshal.Queue as Queue
import qualified Sound.ALSA.Sequencer.Marshal.RealTime as RealTime

import Foreign.C.Types (CUInt, )
import Foreign.Storable
          (Storable, sizeOf, alignment, peek, poke, pokeByteOff, peekByteOff, )
import Foreign.Ptr (Ptr, castPtr, nullPtr, plusPtr, )
import Foreign.Marshal.Alloc (alloca, )
import Data.Monoid (mappend, )
import qualified Data.FlagSet as FlagSet
import Data.Int (Int16, Int32, )
import Data.Word (Word8, Word16, Word32, )
import Data.Array (Array, (!), accumArray, )


data Flag
type Flags     = FlagSet.T Word8 Flag
type FlagValue = FlagSet.MaskedValue Word8 Flag


timeStampTick, timeStampReal :: FlagValue
timeStampTick =
   FlagSet.MaskedValue
      1
{-# LINE 52 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      0
{-# LINE 53 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
timeStampReal =
   FlagSet.MaskedValue
      1
{-# LINE 56 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      1
{-# LINE 57 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


timeModeAbs, timeModeRel :: FlagValue
timeModeAbs =
   FlagSet.MaskedValue
      2
{-# LINE 63 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      0
{-# LINE 64 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
timeModeRel =
   FlagSet.MaskedValue
      2
{-# LINE 67 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      2
{-# LINE 68 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


eventLengthFixed, eventLengthVariable, eventLengthVarUser :: FlagValue
eventLengthFixed =
   FlagSet.MaskedValue
      12
{-# LINE 74 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      0
{-# LINE 75 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
eventLengthVariable =
   FlagSet.MaskedValue
      12
{-# LINE 78 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      4
{-# LINE 79 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
eventLengthVarUser =
   FlagSet.MaskedValue
      12
{-# LINE 82 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      8
{-# LINE 83 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


priorityHigh, priorityNormal :: FlagValue
priorityHigh =
   FlagSet.MaskedValue
      16
{-# LINE 89 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      16
{-# LINE 90 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
priorityNormal =
   FlagSet.MaskedValue
      16
{-# LINE 93 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
      0
{-# LINE 94 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


data Connect = Connect
   { connSource :: !Addr.T
   , connDest   :: !Addr.T
   } deriving (Show,Eq,Ord)

instance Storable Connect where
  sizeOf _    = (4)
{-# LINE 103 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  alignment _ = 4 -- XXX
  peek p      = do s <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 105 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   d <- (\hsc_ptr -> peekByteOff hsc_ptr 2) p
{-# LINE 106 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   return Connect { connSource = s, connDest = d }
  poke p v    = (\hsc_ptr -> pokeByteOff hsc_ptr 0) p (connSource v)
{-# LINE 108 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 2)   p (connDest v)
{-# LINE 109 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


data TimeStamp      = TickTime !Word32
                    | RealTime !RealTime.T
                      deriving Show

peekTimestamp :: Flags -> Ptr TimeStamp -> IO TimeStamp
peekTimestamp flags p =
  if FlagSet.match flags timeStampTick
    then fmap TickTime $ peek $ castPtr p
    else fmap RealTime $ peek $ castPtr p

pokeTimestamp :: Ptr TimeStamp -> TimeStamp -> IO FlagValue
pokeTimestamp p ts = case ts of
  TickTime t -> poke (castPtr p) t >> return timeStampTick
  RealTime t -> poke (castPtr p) t >> return timeStampReal



newtype InstrCluster = InstrCluster CUInt
  deriving (Show,Eq,Ord,Num,Enum,Storable)

data Instr = Instr
   { instrCluster :: !InstrCluster
    -- XXX: perhaps use Sample?
   , instrStd     :: !Word32
   , instrBank    :: !Word16
   , instrPrg     :: !Word16
   } deriving (Show)

{-
instance Storable Instr where
  sizeOf _    = #{size snd_seq_instr_t}
  alignment _ = 4 -- XXX
  peek p      = do cl <- #{peek snd_seq_instr_t, cluster} p
                   st <- #{peek snd_seq_instr_t, std} p
                   ba <- #{peek snd_seq_instr_t, bank} p
                   pr <- #{peek snd_seq_instr_t, prg} p
                   return Instr { instr_cluster = cl
                                , instr_std     = st
                                , instr_bank    = ba
                                , instr_prg     = pr
                                }
  poke p v    = #{poke snd_seq_instr_t, cluster} p (instr_cluster v)
             >> #{poke snd_seq_instr_t, std}     p (instr_std v)
             >> #{poke snd_seq_instr_t, bank}    p (instr_bank v)
             >> #{poke snd_seq_instr_t, prg}     p (instr_prg v)
-}


data Note = Note
   { noteChannel      :: !Word8
   , noteNote         :: !Word8
   , noteVelocity     :: !Word8
   , noteOffVelocity  :: !Word8
   , noteDuration     :: !Word32
   } deriving (Show)


instance Storable Note where
  sizeOf _    = (8)
{-# LINE 170 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  alignment _ = 4 -- XXX
  peek p      = do c  <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 172 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   n  <- (\hsc_ptr -> peekByteOff hsc_ptr 1) p
{-# LINE 173 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   v  <- (\hsc_ptr -> peekByteOff hsc_ptr 2) p
{-# LINE 174 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   ov <- (\hsc_ptr -> peekByteOff hsc_ptr 3) p
{-# LINE 175 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   d  <- (\hsc_ptr -> peekByteOff hsc_ptr 4) p
{-# LINE 176 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   return Note { noteChannel = c
                               , noteNote = n
                               , noteVelocity = v
                               , noteOffVelocity = ov
                               , noteDuration = d
                               }
  poke p v    = (\hsc_ptr -> pokeByteOff hsc_ptr 0)      p (noteChannel v)
{-# LINE 183 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 1)         p (noteNote v)
{-# LINE 184 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 2)     p (noteVelocity v)
{-# LINE 185 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 3) p (noteOffVelocity v)
{-# LINE 186 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 4)     p (noteDuration v)
{-# LINE 187 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


data Ctrl = Ctrl
   { ctrlChannel  :: !Word8
   , ctrlParam    :: !Word32
   , ctrlValue    :: !Int32
   } deriving (Show)

instance Storable Ctrl where
  sizeOf _    = (12)
{-# LINE 197 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  alignment _ = 4 -- XXX
  peek p      = do ct <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 199 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   pa <- (\hsc_ptr -> peekByteOff hsc_ptr 4) p
{-# LINE 200 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   va <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 201 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   return Ctrl { ctrlChannel = ct
                               , ctrlParam   = pa
                               , ctrlValue   = va
                               }
  poke p v    = (\hsc_ptr -> pokeByteOff hsc_ptr 0) p (ctrlChannel v)
{-# LINE 206 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 4)   p (ctrlParam v)
{-# LINE 207 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 8)   p (ctrlValue v)
{-# LINE 208 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}



data Sample = Sample
   { sampleStd  :: !Word32
   , sampleBank :: !Word16
   , samplePrg  :: !Word16
   } deriving (Show)

{-
instance Storable Sample where
  sizeOf _    = #{size snd_seq_ev_sample_t}
  alignment _ = 4 -- XXX
  peek p      = do st <- #{peek snd_seq_ev_sample_t, std} p
                   ba <- #{peek snd_seq_ev_sample_t, bank} p
                   pr <- #{peek snd_seq_ev_sample_t, prg} p
                   return Sample { sample_std     = st
                                 , sample_bank    = ba
                                 , sample_prg     = pr
                                 }
  poke p v    = #{poke snd_seq_ev_sample_t, std}     p (sampleStd v)
             >> #{poke snd_seq_ev_sample_t, bank}    p (sampleBank v)
             >> #{poke snd_seq_ev_sample_t, prg}     p (samplePrg v)
-}


newtype Cluster = Cluster
   { clusterCluster :: InstrCluster
   } deriving (Show, Eq, Storable)


-- | These are all 14 bit values.
data Volume = Volume
   { volumeVolume  :: !Int16
   , volumeLR      :: !Int16
   , volumeFR      :: !Int16
   , volumeDU      :: !Int16
   } deriving (Show)

{-
instance Storable Volume where
  sizeOf _    = #{size snd_seq_ev_volume_t}
  alignment _ = 4 -- XXX
  peek p      = do v <- #{peek snd_seq_ev_volume_t, volume} p
                   l <- #{peek snd_seq_ev_volume_t, lr} p
                   f <- #{peek snd_seq_ev_volume_t, fr} p
                   d <- #{peek snd_seq_ev_volume_t, du} p
                   return Volume { volume_volume  = v
                                 , volume_lr      = l
                                 , volume_fr      = f
                                 , volume_du      = d
                                 }
  poke p v    = #{poke snd_seq_ev_volume_t, volume} p (volumeVolume v)
             >> #{poke snd_seq_ev_volume_t, lr}     p (volumeLR v)
             >> #{poke snd_seq_ev_volume_t, fr}     p (volumeFR v)
             >> #{poke snd_seq_ev_volume_t, du}     p (volumeDU v)
-}


data Custom = Custom { custom0, custom1, custom2 :: !Word32 }
                      deriving (Show)

instance Storable Custom where
  sizeOf _    = (12)
{-# LINE 272 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  alignment _ = 4 -- XXX
  peek p      = do d0 <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 274 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   d1 <- (\hsc_ptr -> peekByteOff hsc_ptr 4) p
{-# LINE 275 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   d2 <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
{-# LINE 276 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
                   return Custom { custom0 = d0
                                 , custom1 = d1
                                 , custom2 = d2
                                 }
  poke p v    = (\hsc_ptr -> pokeByteOff hsc_ptr 0) p (custom0 v)
{-# LINE 281 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 4) p (custom1 v)
{-# LINE 282 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
             >> (\hsc_ptr -> pokeByteOff hsc_ptr 8) p (custom2 v)
{-# LINE 283 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}



data T = Cons
   { highPriority   :: !Bool
   , tag            :: !Word8
   , queue          :: !Queue.T
   , timestamp      :: !TimeStamp
   , source         :: !Addr.T
   , dest           :: !Addr.T
   , body           :: !Data
   } deriving Show

instance Storable T where
  sizeOf _    = (28)
{-# LINE 298 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  alignment _ = 4 -- XXX
  peek p =
    do ty    <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
{-# LINE 301 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       flags <- (\hsc_ptr -> peekByteOff hsc_ptr 1) p
{-# LINE 302 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       tg    <- (\hsc_ptr -> peekByteOff hsc_ptr 2) p
{-# LINE 303 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       q     <- (\hsc_ptr -> peekByteOff hsc_ptr 3) p
{-# LINE 304 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       time  <- peekTimestamp flags ((\hsc_ptr -> hsc_ptr `plusPtr` 4) p)
{-# LINE 305 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       src   <- (\hsc_ptr -> peekByteOff hsc_ptr 12) p
{-# LINE 306 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       dst   <- (\hsc_ptr -> peekByteOff hsc_ptr 14) p
{-# LINE 307 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       d     <- (peekData ! ty) ((\hsc_ptr -> hsc_ptr `plusPtr` 16) p)
{-# LINE 308 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
       return Cons
         { highPriority = not $ FlagSet.match flags priorityNormal
         , tag = tg
         , queue = q
         , timestamp = time
         , source = src
         , dest = dst
         , body = d
         }
  poke p e = do
    { ty <- pokeData ((\hsc_ptr -> hsc_ptr `plusPtr` 16) p) (body e)
{-# LINE 319 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 0) p ty
{-# LINE 320 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 2) p (tag e)
{-# LINE 321 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 3) p (queue e)
{-# LINE 322 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; real <- pokeTimestamp ((\hsc_ptr -> hsc_ptr `plusPtr` 4) p) (timestamp e)
{-# LINE 323 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 12) p (source e)
{-# LINE 324 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 14) p (dest e)
{-# LINE 325 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ; let flags =
             (if highPriority e
                then priorityHigh
                else priorityNormal)
             `mappend` real
             `mappend` eventLengthFixed
    ; (\hsc_ptr -> pokeByteOff hsc_ptr 1) p (FlagSet.fromMaskedValue flags)
{-# LINE 332 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    }

allocaEv :: T -> (Ptr T -> IO a) -> IO a
allocaEv e h = alloca (\p -> poke p e >> h p)

allocaMaybeEv :: Maybe T -> (Ptr T -> IO a) -> IO a
allocaMaybeEv me h =
  maybe (h nullPtr) (\e -> allocaEv e h) me

pokeBody ::
  (Storable d, Type e) =>
  Ptr Data -> e -> d -> IO Word8
pokeBody p e d =
  poke (castPtr p) d >> return (expEv e)

pokeData :: Ptr Data -> Data -> IO Word8
pokeData p dt = case dt of
  NoteEv   e d -> pokeBody p e d
  CtrlEv   e d -> pokeBody p e d
  AddrEv   e d -> pokeBody p e d
  ConnEv   e d -> pokeBody p e d
  CustomEv e d -> pokeBody p e d
  EmptyEv  e   -> return (expEv e)


peekData :: Array Word8 (Ptr Data -> IO Data)
peekData = accumArray (const id) unknown (0,255)
  [ -- result events (2)
    (0, unknown)
{-# LINE 361 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (1, unknown)
{-# LINE 362 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

    -- note events (4)
  , (5,     peekNoteEv ANote)
{-# LINE 365 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (6,   peekNoteEv NoteOn)
{-# LINE 366 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (7,  peekNoteEv NoteOff)
{-# LINE 367 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (8, peekNoteEv KeyPress)
{-# LINE 368 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

    -- control events (12)
  , (10,  peekCtrlEv Controller)
{-# LINE 371 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (11,   peekCtrlEv PgmChange)
{-# LINE 372 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (12,   peekCtrlEv ChanPress)
{-# LINE 373 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (13,   peekCtrlEv PitchBend)
{-# LINE 374 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (14,   peekCtrlEv Control14)
{-# LINE 375 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (15, peekCtrlEv NonRegParam)
{-# LINE 376 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (16,    peekCtrlEv RegParam)
{-# LINE 377 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (20,     peekCtrlEv SongPos)
{-# LINE 378 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (21,     peekCtrlEv SongSel)
{-# LINE 379 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (22,      peekCtrlEv QFrame)
{-# LINE 380 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (23,    peekCtrlEv TimeSign)
{-# LINE 381 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (24,     peekCtrlEv KeySign)
{-# LINE 382 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  -- queue control (10)
  , (30, unknown)
{-# LINE 385 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (31, unknown)
{-# LINE 386 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (32, unknown)
{-# LINE 387 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (33, unknown)
{-# LINE 388 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (34, unknown)
{-# LINE 389 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (35, unknown)
{-# LINE 390 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (36, unknown)
{-# LINE 391 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (37, unknown)
{-# LINE 392 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (38, unknown)
{-# LINE 393 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (39, unknown)
{-# LINE 394 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  -- misc (3)
  , (40, peekEmptyEv TuneRequest)
{-# LINE 397 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (41,        peekEmptyEv Reset)
{-# LINE 398 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (42,      peekEmptyEv Sensing)
{-# LINE 399 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  , (50, peekCustomEv Echo)
{-# LINE 401 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (51,  peekCustomEv OSS)
{-# LINE 402 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  -- networking (8)
  , (60,  peekAddrEv ClientStart)
{-# LINE 405 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (61,   peekAddrEv ClientExit)
{-# LINE 406 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (62, peekAddrEv ClientChange)
{-# LINE 407 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (63,    peekAddrEv PortStart)
{-# LINE 408 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (64,     peekAddrEv PortExit)
{-# LINE 409 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (65,   peekAddrEv PortChange)
{-# LINE 410 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (66,   peekConnEv PortSubscribed)
{-# LINE 411 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (67, peekConnEv PortUnsubscribed)
{-# LINE 412 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

{-
  , (#{const SND_SEQ_EVENT_SAMPLE}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_CLUSTER}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_START}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_STOP}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_FREQ}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_VOLUME}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_LOOP}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_POSITION}, unknown)
  , (#{const SND_SEQ_EVENT_SAMPLE_PRIVATE1}, unknown)
-}
  , (90, peekCustomEv User0)
{-# LINE 425 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (91, peekCustomEv User1)
{-# LINE 426 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (92, peekCustomEv User2)
{-# LINE 427 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (93, peekCustomEv User3)
{-# LINE 428 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (94, peekCustomEv User4)
{-# LINE 429 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (95, peekCustomEv User5)
{-# LINE 430 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (96, peekCustomEv User6)
{-# LINE 431 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (97, peekCustomEv User7)
{-# LINE 432 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (98, peekCustomEv User8)
{-# LINE 433 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (99, peekCustomEv User9)
{-# LINE 434 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

{-
  , (#{const SND_SEQ_EVENT_INSTR_BEGIN}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_END}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_INFO}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_INFO_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_FINFO}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_FINFO_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_RESET}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_STATUS}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_STATUS_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_PUT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_GET}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_GET_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_FREE}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_LIST}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_LIST_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_CLUSTER}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_CLUSTER_GET}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_CLUSTER_RESULT}, unknown)
  , (#{const SND_SEQ_EVENT_INSTR_CHANGE}, unknown)
-}

  , (130, unknown)
{-# LINE 458 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (131, unknown)
{-# LINE 459 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  , (135, unknown)
{-# LINE 461 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (136, unknown)
{-# LINE 462 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (137, unknown)
{-# LINE 463 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (138, unknown)
{-# LINE 464 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  , (138, unknown)
{-# LINE 465 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

  , (255, peekEmptyEv None)
{-# LINE 467 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  ]

  where unknown = peekEmptyEv Unknown


data NoteEv   = ANote | NoteOn | NoteOff | KeyPress
                deriving (Show, Eq, Ord, Enum, Bounded)

data CtrlEv   = Controller | PgmChange | ChanPress
              | PitchBend | Control14
              | NonRegParam | RegParam
              | SongPos | SongSel
              | QFrame
              | TimeSign | KeySign
                deriving (Show, Eq, Ord, Enum, Bounded)

data QueueEv  = QueueStart
              | QueueContinue
              | QueueStop
              | QueueSetPosTick
              | QueueSetPosTime
              | QueueTempo
              | QueueClock
              | QueueTick
              | QueueSkew
              | QueueSyncPos
                deriving (Show, Eq, Ord, Enum, Bounded)

data EmptyEv  = TuneRequest | Reset | Sensing | None | Unknown
                deriving (Show, Eq, Ord, Enum, Bounded)

data CustomEv = Echo | OSS
              | User0 | User1 | User2 | User3 | User4
              | User5 | User6 | User7 | User8 | User9
                deriving (Show, Eq, Ord, Enum, Bounded)

data AddrEv   = ClientStart | ClientExit | ClientChange
              | PortStart | PortExit | PortChange
                deriving (Show, Eq, Ord, Enum, Bounded)

data ConnEv   = PortSubscribed | PortUnsubscribed
                deriving (Show, Eq, Ord, Enum, Bounded)


class Type e where
  expEv :: e -> Word8

instance Type NoteEv where 
 expEv e = case e of
  ANote    -> 5
{-# LINE 517 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  NoteOn   -> 6
{-# LINE 518 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  NoteOff  -> 7
{-# LINE 519 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  KeyPress -> 8
{-# LINE 520 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type CtrlEv where 
 expEv e = case e of
  Controller  -> 10
{-# LINE 524 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  PgmChange   -> 11
{-# LINE 525 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  ChanPress   -> 12
{-# LINE 526 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  PitchBend   -> 13
{-# LINE 527 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  Control14   -> 14
{-# LINE 528 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  NonRegParam -> 15
{-# LINE 529 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  RegParam    -> 16
{-# LINE 530 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  SongPos     -> 20
{-# LINE 531 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  SongSel     -> 21
{-# LINE 532 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QFrame      -> 22
{-# LINE 533 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  TimeSign    -> 23
{-# LINE 534 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  KeySign     -> 24
{-# LINE 535 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type QueueEv where 
 expEv e = case e of
  QueueStart      -> 30
{-# LINE 539 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueContinue   -> 31
{-# LINE 540 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueStop       -> 32
{-# LINE 541 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueSetPosTick -> 33
{-# LINE 542 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueSetPosTime -> 34
{-# LINE 543 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueTempo      -> 35
{-# LINE 544 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueClock      -> 36
{-# LINE 545 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueTick       -> 37
{-# LINE 546 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueSkew       -> 38
{-# LINE 547 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  QueueSyncPos    -> 39
{-# LINE 548 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type EmptyEv where 
 expEv e = case e of
  TuneRequest -> 40
{-# LINE 552 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  Reset       -> 41
{-# LINE 553 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  Sensing     -> 42
{-# LINE 554 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  None        -> 255
{-# LINE 555 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  Unknown     -> 255
{-# LINE 556 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type CustomEv where 
 expEv e = case e of
  Echo  -> 50
{-# LINE 560 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  OSS   -> 51
{-# LINE 561 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User0 -> 90
{-# LINE 562 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User1 -> 91
{-# LINE 563 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User2 -> 92
{-# LINE 564 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User3 -> 93
{-# LINE 565 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User4 -> 94
{-# LINE 566 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User5 -> 95
{-# LINE 567 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User6 -> 96
{-# LINE 568 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User7 -> 97
{-# LINE 569 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User8 -> 98
{-# LINE 570 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  User9 -> 99
{-# LINE 571 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type AddrEv where 
  expEv e = case e of
    ClientStart -> 60
{-# LINE 575 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ClientExit -> 61
{-# LINE 576 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    ClientChange -> 62
{-# LINE 577 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    PortStart -> 63
{-# LINE 578 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    PortExit -> 64
{-# LINE 579 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
    PortChange -> 65
{-# LINE 580 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}

instance Type ConnEv where 
 expEv e = case e of
  PortSubscribed   -> 66
{-# LINE 584 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}
  PortUnsubscribed -> 67
{-# LINE 585 "src/Sound/ALSA/Sequencer/Marshal/Event.hsc" #-}


maxEventType :: EmptyEv
maxEventType = maxBound


peekBody ::
  (Storable d) =>
  (d -> Data) -> Ptr Data -> IO Data
peekBody makeBody p =
  fmap makeBody (peek (castPtr p))

peekNoteEv :: NoteEv -> Ptr Data -> IO Data
peekNoteEv e = peekBody (NoteEv e)

peekCtrlEv :: CtrlEv -> Ptr Data -> IO Data
peekCtrlEv e = peekBody (CtrlEv e)

peekAddrEv :: AddrEv -> Ptr Data -> IO Data
peekAddrEv e = peekBody (AddrEv e)

peekConnEv :: ConnEv -> Ptr Data -> IO Data
peekConnEv e = peekBody (ConnEv e)

peekEmptyEv :: EmptyEv -> Ptr Data -> IO Data
peekEmptyEv e _ = return (EmptyEv e)

peekCustomEv :: CustomEv -> Ptr Data -> IO Data
peekCustomEv e = peekBody (CustomEv e)


data Data
  = NoteEv NoteEv Note
  | CtrlEv CtrlEv Ctrl
  | AddrEv AddrEv Addr.T
  | ConnEv ConnEv Connect
  | EmptyEv EmptyEv
  | CustomEv CustomEv Custom
    deriving Show