{-# LINE 1 "Graphics/X11/Xshape.hsc" #-}
--------------------------------------------------------------------
{-# LINE 2 "Graphics/X11/Xshape.hsc" #-}
-- |
-- Module    : Graphics.X11.Xshape
-- Copyright : (c) Haskell.org, 2009
-- License   : BSD3
--
-- Maintainer: Ewan Higgs <ewan_higgs@yahoo.co.uk>
-- Stability : unstable 
-- Portability: unportable 
--
--------------------------------------------------------------------
--
-- Interface to Xshape API
--

module Graphics.X11.Xshape( xshapeQueryExtension
                          , xshapeQueryVersion
                          , xshapeCombineRegion 
                          , xshapeCombineRectangles 
                          , xshapeCombineMask 
                          , xshapeCombineShape 
                          , xshapeOffsetShape 
                          , xshapeQueryExtents 
                          , xshapeSelectInput 
                          , xshapeInputSelected
                          , xshapeGetRectangles
                          , shapeQueryVersion
                          , shapeRectangles
                          , shapeMask
                          , shapeCombine
                          , shapeOffset
                          , shapeQueryExtents
                          , shapeSelectInput
                          , shapeInputSelected
                          , shapeGetRectangles
                          , shapeSet
                          , shapeUnion
                          , shapeIntersect
                          , shapeSubtract
                          , shapeInvert
                          , shapeBounding
                          , shapeClip
                          , shapeInput
                          , shapeNotifyMask
                          , shapeNotify
                          , shapeNumberEvents
) where

import Foreign
import Foreign.C.Types
import Graphics.X11.Xlib

data XShapeEvent = XShapeEvent
                 { xse_type       :: CInt
                 , xse_serial     :: CUInt
                 , xse_send_event :: Bool
                 , xse_display    :: Display
                 , xse_window     :: Window
                 , xse_kind       :: CInt
                 , xse_x          :: CInt
                 , xse_y          :: CInt
                 , xse_width      :: CUInt
                 , xse_height     :: CUInt
                 , xse_time       :: Time
                 , xse_shaped     :: Bool 
                 }
shapeQueryVersion  :: CInt
shapeQueryVersion   = 0
shapeRectangles    :: CInt 
shapeRectangles     = 1
shapeMask          :: CInt 
shapeMask           = 2
shapeCombine       :: CInt 
shapeCombine        = 3
shapeOffset        :: CInt 
shapeOffset         = 4
shapeQueryExtents  :: CInt
shapeQueryExtents   = 5
shapeSelectInput   :: CInt
shapeSelectInput    = 6
shapeInputSelected :: CInt
shapeInputSelected  = 7
shapeGetRectangles :: CInt
shapeGetRectangles  = 8

shapeSet       :: CInt
shapeSet        = 0
shapeUnion     :: CInt
shapeUnion      = 1
shapeIntersect :: CInt
shapeIntersect  = 2
shapeSubtract  :: CInt
shapeSubtract   = 3
shapeInvert    :: CInt
shapeInvert     = 4

shapeBounding :: CInt
shapeBounding  = 0
shapeClip     :: CInt
shapeClip      = 1
shapeInput    :: CInt
shapeInput     = 2

shapeNotifyMask :: Int
shapeNotifyMask  = 1 `shiftL` 0
shapeNotify     :: Int
shapeNotify      = 0

shapeNumberEvents :: Int
shapeNumberEvents  = shapeNotify + 1


-- for XFree() (already included from Xdamage.h, but I don't know if I can count on that.)

{-# LINE 115 "Graphics/X11/Xshape.hsc" #-}


{-# LINE 117 "Graphics/X11/Xshape.hsc" #-}

xshapeQueryExtension :: Display -> IO (Maybe (CInt, CInt))
xshapeQueryExtension dpy = wrapPtr2 (cXshapeQueryExtension dpy) go
  where go False _ _                = Nothing
        go True eventbase errorbase = Just (fromIntegral eventbase, fromIntegral errorbase)

xshapeQueryVersion :: Display -> IO (Maybe (CInt, CInt))
xshapeQueryVersion dpy = wrapPtr2 (cXshapeQueryVersion dpy) go
  where go False _ _        = Nothing
        go True major minor = Just (fromIntegral major, fromIntegral minor)

foreign import ccall "XShapeQueryExtension"
  cXshapeQueryExtension :: Display -> Ptr CInt -> Ptr CInt -> IO Bool

foreign import ccall "XShapeQueryVersion"
  cXshapeQueryVersion :: Display -> Ptr CInt -> Ptr CInt -> IO Bool

foreign import ccall "XShapeCombineRegion"
  xshapeCombineRegion :: Display -> Window -> CInt -> CInt -> CInt -> Ptr Region -> CInt -> IO()

foreign import ccall "XShapeCombineRectangles"
  xshapeCombineRectangles :: Display -> Window -> CInt -> CInt -> CInt -> Ptr Rectangle -> CInt -> CInt -> CInt -> IO()

foreign import ccall "XShapeCombineMask"
  xshapeCombineMask :: Display -> Window -> CInt -> CInt -> CInt -> Pixmap -> CInt -> IO ()

foreign import ccall "XShapeCombineShape"
  xshapeCombineShape :: Display -> Window -> CInt -> CInt -> CInt -> Window -> CInt -> CInt -> IO ()


foreign import ccall "XShapeOffsetShape"
  xshapeOffsetShape :: Display -> Window -> CInt -> CInt -> CInt -> IO ()

foreign import ccall "XShapeQueryExtents"
  xshapeQueryExtents :: Display -> Window -> CInt -> CInt -> CUInt -> CUInt -> Bool -> CInt -> CInt -> CUInt -> CUInt -> IO ()

foreign import ccall "XShapeSelectInput"
  xshapeSelectInput :: Display -> Window -> CUInt -> IO()

foreign import ccall "XShapeInputSelected"
  xshapeInputSelected :: Display -> Window -> IO(CUInt)

  -- May leak the returned Rectangle; ask for help here
foreign import ccall "XShapeGetRectangles"
  xshapeGetRectangles :: Display -> Window -> CInt -> Ptr CInt -> Ptr CInt -> IO(Ptr Rectangle)

foreign import ccall "XFree"
  cXFree :: Ptr a -> IO CInt


wrapPtr2 :: (Storable a, Storable b) => (Ptr a -> Ptr b -> IO c) -> (c -> a -> b -> d) -> IO d
wrapPtr2 cfun f =
  withPool $ \pool -> do aptr <- pooledMalloc pool
                         bptr <- pooledMalloc pool
                         ret <- cfun aptr bptr
                         a <- peek aptr
                         b <- peek bptr
                         return (f ret a b)