1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
{-# LANGUAGE DeriveDataTypeable #-}
-----------------------------------------------------------------------------
-- |
-- Module : Graphics.X11.Xlib.Types
-- Copyright : (c) Alastair Reid, 1999-2003
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : provisional
-- Portability : portable
--
-- A collection of type declarations for interfacing with Xlib.
--
-----------------------------------------------------------------------------
-- #hide
module Graphics.X11.Xlib.Types(
Display(..), Screen(..), Visual(..), GC(..), GCValues, SetWindowAttributes,
VisualInfo(..),
Image(..), Point(..), Rectangle(..), Arc(..), Segment(..), Color(..),
Pixel, Position, Dimension, Angle, ScreenNumber, Buffer
) where
import Graphics.X11.Types
-- import Control.Monad( zipWithM_ )
import Data.Int
import Data.Word
import Foreign.C.Types
-- import Foreign.Marshal.Alloc( allocaBytes )
import Foreign.Ptr
import Foreign.Storable( Storable(..) )
#if __GLASGOW_HASKELL__
import Data.Data
#endif
import Data.Default.Class
#include "HsXlib.h"
----------------------------------------------------------------
-- Types
----------------------------------------------------------------
-- | pointer to an X11 @Display@ structure
newtype Display = Display (Ptr Display)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | pointer to an X11 @Screen@ structure
newtype Screen = Screen (Ptr Screen)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | pointer to an X11 @Visual@ structure
newtype Visual = Visual (Ptr Visual)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | pointer to an X11 @GC@ structure
newtype GC = GC (Ptr GC)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | pointer to an X11 @XGCValues@ structure
newtype GCValues = GCValues (Ptr GCValues)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | pointer to an X11 @XSetWindowAttributes@ structure
newtype SetWindowAttributes = SetWindowAttributes (Ptr SetWindowAttributes)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
-- | counterpart of an X11 @XVisualInfo@ structure
data VisualInfo = VisualInfo {
visualInfo_visual :: Visual,
visualInfo_visualID :: VisualID,
visualInfo_screen :: ScreenNumber,
visualInfo_depth :: CInt,
visualInfo_class :: CInt,
visualInfo_redMask :: CULong,
visualInfo_greenMask :: CULong,
visualInfo_blueMask :: CULong,
visualInfo_colormapSize :: CInt,
visualInfo_bitsPerRGB :: CInt
}
#if __GLASGOW_HASKELL__
deriving (Eq, Show, Typeable)
#else
deriving (Eq, Show)
#endif
instance Default VisualInfo where
def = VisualInfo {
visualInfo_visual = Visual nullPtr,
visualInfo_visualID = 0,
visualInfo_screen = 0,
visualInfo_depth = 0,
visualInfo_class = 0,
visualInfo_redMask = 0,
visualInfo_greenMask = 0,
visualInfo_blueMask = 0,
visualInfo_colormapSize = 0,
visualInfo_bitsPerRGB = 0
}
instance Storable VisualInfo where
sizeOf _ = #size XVisualInfo
alignment _ = alignment (undefined::CInt)
peek p = do
visual <- Visual `fmap` #{peek XVisualInfo, visual} p
visualID <- #{peek XVisualInfo, visualid} p
screen <- #{peek XVisualInfo, screen} p
depth <- #{peek XVisualInfo, depth} p
class_ <- #{peek XVisualInfo, class} p
redMask <- #{peek XVisualInfo, red_mask} p
greenMask <- #{peek XVisualInfo, green_mask} p
blueMask <- #{peek XVisualInfo, blue_mask} p
colormapSize <- #{peek XVisualInfo, colormap_size} p
bitsPerRGB <- #{peek XVisualInfo, bits_per_rgb} p
return $ VisualInfo {
visualInfo_visual = visual,
visualInfo_visualID = visualID,
visualInfo_screen = screen,
visualInfo_depth = depth,
visualInfo_class = class_,
visualInfo_redMask = redMask,
visualInfo_greenMask = greenMask,
visualInfo_blueMask = blueMask,
visualInfo_colormapSize = colormapSize,
visualInfo_bitsPerRGB = bitsPerRGB
}
poke p info = do
#{poke XVisualInfo, visual} p visualPtr
#{poke XVisualInfo, visualid} p $ visualInfo_visualID info
#{poke XVisualInfo, screen} p $ visualInfo_screen info
#{poke XVisualInfo, depth} p $ visualInfo_depth info
#{poke XVisualInfo, class} p $ visualInfo_class info
#{poke XVisualInfo, red_mask} p $ visualInfo_redMask info
#{poke XVisualInfo, green_mask} p $ visualInfo_greenMask info
#{poke XVisualInfo, blue_mask} p $ visualInfo_blueMask info
#{poke XVisualInfo, colormap_size} p $
visualInfo_colormapSize info
#{poke XVisualInfo, bits_per_rgb} p $
visualInfo_bitsPerRGB info
where
~(Visual visualPtr) = visualInfo_visual info
-- | pointer to an X11 @XImage@ structure
newtype Image = Image (Ptr Image)
#if __GLASGOW_HASKELL__
deriving (Eq, Ord, Show, Typeable, Data)
#else
deriving (Eq, Ord, Show)
#endif
type Pixel = #{type unsigned long}
type Position = #{type int}
type Dimension = #{type unsigned int}
type Angle = CInt
type ScreenNumber = Word32
type Buffer = CInt
----------------------------------------------------------------
-- Short forms used in structs
----------------------------------------------------------------
type ShortPosition = CShort
type ShortDimension = CUShort
type ShortAngle = CShort
peekPositionField :: Ptr a -> CInt -> IO Position
peekPositionField ptr off = do
v <- peekByteOff ptr (fromIntegral off)
return (fromIntegral (v::ShortPosition))
peekDimensionField :: Ptr a -> CInt -> IO Dimension
peekDimensionField ptr off = do
v <- peekByteOff ptr (fromIntegral off)
return (fromIntegral (v::ShortDimension))
peekAngleField :: Ptr a -> CInt -> IO Angle
peekAngleField ptr off = do
v <- peekByteOff ptr (fromIntegral off)
return (fromIntegral (v::ShortAngle))
pokePositionField :: Ptr a -> CInt -> Position -> IO ()
pokePositionField ptr off v =
pokeByteOff ptr (fromIntegral off) (fromIntegral v::ShortPosition)
pokeDimensionField :: Ptr a -> CInt -> Dimension -> IO ()
pokeDimensionField ptr off v =
pokeByteOff ptr (fromIntegral off) (fromIntegral v::ShortDimension)
pokeAngleField :: Ptr a -> CInt -> Angle -> IO ()
pokeAngleField ptr off v =
pokeByteOff ptr (fromIntegral off) (fromIntegral v::ShortAngle)
----------------------------------------------------------------
-- Point
----------------------------------------------------------------
-- | counterpart of an X11 @XPoint@ structure
data Point = Point { pt_x :: !Position, pt_y :: !Position }
#if __GLASGOW_HASKELL__
deriving (Eq, Show, Typeable, Data)
#else
deriving (Eq, Show)
#endif
instance Storable Point where
sizeOf _ = #{size XPoint}
alignment _ = alignment (undefined::CInt)
peek p = do
x <- peekPositionField p #{offset XPoint,x}
y <- peekPositionField p #{offset XPoint,y}
return (Point x y)
poke p (Point x y) = do
pokePositionField p #{offset XPoint,x} x
pokePositionField p #{offset XPoint,y} y
----------------------------------------------------------------
-- Rectangle
----------------------------------------------------------------
-- | counterpart of an X11 @XRectangle@ structure
data Rectangle = Rectangle {
rect_x :: !Position,
rect_y :: !Position,
rect_width :: !Dimension,
rect_height :: !Dimension
}
#if __GLASGOW_HASKELL__
deriving (Eq, Read, Show, Typeable, Data)
#else
deriving (Eq, Read, Show)
#endif
instance Storable Rectangle where
sizeOf _ = #{size XRectangle}
alignment _ = alignment (undefined::CInt)
peek p = do
x <- peekPositionField p #{offset XRectangle,x}
y <- peekPositionField p #{offset XRectangle,y}
width <- peekDimensionField p #{offset XRectangle,width}
height <- peekDimensionField p #{offset XRectangle,height}
return (Rectangle x y width height)
poke p (Rectangle x y width height) = do
pokePositionField p #{offset XRectangle,x} x
pokePositionField p #{offset XRectangle,y} y
pokeDimensionField p #{offset XRectangle,width} width
pokeDimensionField p #{offset XRectangle,height} height
----------------------------------------------------------------
-- Arc
----------------------------------------------------------------
-- | counterpart of an X11 @XArc@ structure
data Arc = Arc {
arc_x :: Position,
arc_y :: Position,
arc_width :: Dimension,
arc_height :: Dimension,
arc_angle1 :: Angle,
arc_angle2 :: Angle
}
#if __GLASGOW_HASKELL__
deriving (Eq, Show, Typeable)
#else
deriving (Eq, Show)
#endif
instance Storable Arc where
sizeOf _ = #{size XArc}
alignment _ = alignment (undefined::CInt)
peek p = do
x <- peekPositionField p #{offset XArc,x}
y <- peekPositionField p #{offset XArc,y}
width <- peekDimensionField p #{offset XArc,width}
height <- peekDimensionField p #{offset XArc,height}
angle1 <- peekAngleField p #{offset XArc,angle1}
angle2 <- peekAngleField p #{offset XArc,angle2}
return (Arc x y width height angle1 angle2)
poke p (Arc x y width height angle1 angle2) = do
pokePositionField p #{offset XArc,x} x
pokePositionField p #{offset XArc,y} y
pokeDimensionField p #{offset XArc,width} width
pokeDimensionField p #{offset XArc,height} height
pokeAngleField p #{offset XArc,angle1} angle1
pokeAngleField p #{offset XArc,angle2} angle2
----------------------------------------------------------------
-- Segment
----------------------------------------------------------------
-- | counterpart of an X11 @XSegment@ structure
data Segment = Segment {
seg_x1 :: Position,
seg_y1 :: Position,
seg_x2 :: Position,
seg_y2 :: Position
}
#if __GLASGOW_HASKELL__
deriving (Eq, Show, Typeable, Data)
#else
deriving (Eq, Show)
#endif
instance Storable Segment where
sizeOf _ = #{size XSegment}
alignment _ = alignment (undefined::CInt)
peek p = do
x1 <- peekPositionField p #{offset XSegment,x1}
y1 <- peekPositionField p #{offset XSegment,y1}
x2 <- peekPositionField p #{offset XSegment,x2}
y2 <- peekPositionField p #{offset XSegment,y2}
return (Segment x1 y1 x2 y2)
poke p (Segment x1 y1 x2 y2) = do
pokePositionField p #{offset XSegment,x1} x1
pokePositionField p #{offset XSegment,y1} y1
pokePositionField p #{offset XSegment,x2} x2
pokePositionField p #{offset XSegment,y2} y2
----------------------------------------------------------------
-- Color
----------------------------------------------------------------
-- | counterpart of an X11 @XColor@ structure
data Color = Color {
color_pixel :: Pixel,
color_red :: Word16,
color_green :: Word16,
color_blue :: Word16,
color_flags :: Word8
}
#if __GLASGOW_HASKELL__
deriving (Eq, Show, Typeable, Data)
#else
deriving (Eq, Show)
#endif
instance Storable Color where
sizeOf _ = #{size XColor}
alignment _ = alignment (undefined::CInt)
peek p = do
pixel <- #{peek XColor,pixel} p
red <- #{peek XColor,red} p
green <- #{peek XColor,green} p
blue <- #{peek XColor,blue} p
flags <- #{peek XColor,flags} p
return (Color pixel red green blue flags)
poke p (Color pixel red green blue flags) = do
#{poke XColor,pixel} p pixel
#{poke XColor,red} p red
#{poke XColor,green} p green
#{poke XColor,blue} p blue
#{poke XColor,flags} p flags
----------------------------------------------------------------
-- End
----------------------------------------------------------------
|