module Graphics.XHB.Gen.Damage.Types
       (deserializeError, deserializeEvent, DAMAGE, ReportLevel(..),
        QueryVersion(..), QueryVersionReply(..), Create(..), Destroy(..),
        Subtract(..), Add(..), Notify(..))
       where
import Data.Word
import Foreign.C.Types
import Data.Bits
import Data.Binary.Put
import Data.Binary.Get
import Data.Typeable
import Control.Monad
import Control.Exception
import Data.List
import Graphics.XHB.Shared hiding (Event, Error)
import qualified Graphics.XHB.Shared
import Graphics.XHB.Gen.Xproto.Types
       hiding (deserializeError, deserializeEvent)
import qualified Graphics.XHB.Gen.Xproto.Types
import Graphics.XHB.Gen.XFixes.Types
       hiding (QueryVersion(..), QueryVersionReply(..), deserializeError,
               deserializeEvent)
import qualified Graphics.XHB.Gen.XFixes.Types
 
deserializeError :: Word8 -> Maybe (Get SomeError)
deserializeError _ = Nothing
 
deserializeEvent :: Word8 -> Maybe (Get SomeEvent)
deserializeEvent 0
  = return (liftM toEvent (deserialize :: Get Notify))
deserializeEvent _ = Nothing
 
newtype DAMAGE = MkDAMAGE Xid
                 deriving (Eq, Ord, Show, Serialize, Deserialize, XidLike)
 
data ReportLevel = ReportLevelRawRectangles
                 | ReportLevelDeltaRectangles
                 | ReportLevelBoundingBox
                 | ReportLevelNonEmpty
 
instance SimpleEnum ReportLevel where
        toValue ReportLevelRawRectangles{} = 0
        toValue ReportLevelDeltaRectangles{} = 1
        toValue ReportLevelBoundingBox{} = 2
        toValue ReportLevelNonEmpty{} = 3
        fromValue 0 = ReportLevelRawRectangles
        fromValue 1 = ReportLevelDeltaRectangles
        fromValue 2 = ReportLevelBoundingBox
        fromValue 3 = ReportLevelNonEmpty
 
data QueryVersion = MkQueryVersion{client_major_version_QueryVersion
                                   :: CARD32,
                                   client_minor_version_QueryVersion :: CARD32}
                  deriving (Show, Typeable)
 
instance ExtensionRequest QueryVersion where
        extensionId _ = "DAMAGE"
        serializeRequest x extOpCode
          = do putWord8 extOpCode
               putWord8 0
               let size__
                     = 4 + size (client_major_version_QueryVersion x) +
                         size (client_minor_version_QueryVersion x)
               serialize (convertBytesToRequestSize size__ :: INT16)
               serialize (client_major_version_QueryVersion x)
               serialize (client_minor_version_QueryVersion x)
               putSkip (requiredPadding size__)
 
data QueryVersionReply = MkQueryVersionReply{major_version_QueryVersionReply
                                             :: CARD32,
                                             minor_version_QueryVersionReply :: CARD32}
                       deriving (Show, Typeable)
 
instance Deserialize QueryVersionReply where
        deserialize
          = do skip 1
               skip 1
               skip 2
               length <- deserialize
               major_version <- deserialize
               minor_version <- deserialize
               skip 16
               let _ = isCard32 length
               return (MkQueryVersionReply major_version minor_version)
 
data Create = MkCreate{damage_Create :: DAMAGE,
                       drawable_Create :: DRAWABLE, level_Create :: CARD8}
            deriving (Show, Typeable)
 
instance ExtensionRequest Create where
        extensionId _ = "DAMAGE"
        serializeRequest x extOpCode
          = do putWord8 extOpCode
               putWord8 1
               let size__
                     = 4 + size (damage_Create x) + size (drawable_Create x) +
                         size (level_Create x)
                         + 3
               serialize (convertBytesToRequestSize size__ :: INT16)
               serialize (damage_Create x)
               serialize (drawable_Create x)
               serialize (level_Create x)
               putSkip 3
               putSkip (requiredPadding size__)
 
data Destroy = MkDestroy{damage_Destroy :: DAMAGE}
             deriving (Show, Typeable)
 
instance ExtensionRequest Destroy where
        extensionId _ = "DAMAGE"
        serializeRequest x extOpCode
          = do putWord8 extOpCode
               putWord8 2
               let size__ = 4 + size (damage_Destroy x)
               serialize (convertBytesToRequestSize size__ :: INT16)
               serialize (damage_Destroy x)
               putSkip (requiredPadding size__)
 
data Subtract = MkSubtract{damage_Subtract :: DAMAGE,
                           repair_Subtract :: REGION, parts_Subtract :: REGION}
              deriving (Show, Typeable)
 
instance ExtensionRequest Subtract where
        extensionId _ = "DAMAGE"
        serializeRequest x extOpCode
          = do putWord8 extOpCode
               putWord8 3
               let size__
                     = 4 + size (damage_Subtract x) + size (repair_Subtract x) +
                         size (parts_Subtract x)
               serialize (convertBytesToRequestSize size__ :: INT16)
               serialize (damage_Subtract x)
               serialize (repair_Subtract x)
               serialize (parts_Subtract x)
               putSkip (requiredPadding size__)
 
data Add = MkAdd{drawable_Add :: DRAWABLE, region_Add :: REGION}
         deriving (Show, Typeable)
 
instance ExtensionRequest Add where
        extensionId _ = "DAMAGE"
        serializeRequest x extOpCode
          = do putWord8 extOpCode
               putWord8 4
               let size__ = 4 + size (drawable_Add x) + size (region_Add x)
               serialize (convertBytesToRequestSize size__ :: INT16)
               serialize (drawable_Add x)
               serialize (region_Add x)
               putSkip (requiredPadding size__)
 
data Notify = MkNotify{level_Notify :: CARD8,
                       drawable_Notify :: DRAWABLE, damage_Notify :: DAMAGE,
                       timestamp_Notify :: TIMESTAMP, area_Notify :: RECTANGLE,
                       geometry_Notify :: RECTANGLE}
            deriving (Show, Typeable)
 
instance Graphics.XHB.Shared.Event Notify
 
instance Deserialize Notify where
        deserialize
          = do skip 1
               level <- deserialize
               skip 2
               drawable <- deserialize
               damage <- deserialize
               timestamp <- deserialize
               area <- deserialize
               geometry <- deserialize
               return (MkNotify level drawable damage timestamp area geometry)