summaryrefslogtreecommitdiffhomepage
path: root/src/XGraphic.hs
blob: 416177f1880159d908a1e6e76b31033d3fd37de4 (plain)
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
{-# LANGUAGE ForeignFunctionInterface #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  XGraphic
-- Copyright   :  Copyright © 2013 Edward O'Callaghan. All Rights Reserved.
-- License     :  BSD3
--
-- Maintainer  :  Edward O'Callaghan - <victoredwardocallaghan@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-----------------------------------------------------------------------------

module XGraphic
    ( readBitmapFile
    ) where

import Graphics.X11.Xlib
--import Graphics.X11.Xlib.Misc
import Foreign
import Foreign.C

-- | interface to the X11 library function @XWriteBitmapFile@.
readBitmapFile :: Display -> Drawable -> String
                  -> IO (Either String (Dimension, Dimension, Pixmap, Maybe CInt, Maybe CInt))
readBitmapFile display d filename =
  withCString filename $ \ c_filename ->
  alloca $ \ width_return ->
  alloca $ \ height_return ->
  alloca $ \ bitmap_return ->
  alloca $ \ x_hot_return ->
  alloca $ \ y_hot_return -> do
    rv <- xReadBitmapFile display d c_filename width_return height_return
         bitmap_return x_hot_return y_hot_return
    width <- peek width_return
    height <- peek height_return
    bitmap <- peek bitmap_return
    x_hot <- peek x_hot_return
    y_hot <- peek y_hot_return
    let m_x_hot | x_hot == -1 = Nothing
                | otherwise  = Just x_hot
        m_y_hot | y_hot == -1 = Nothing
                | otherwise  = Just y_hot
    case rv of
        0 -> return $ Right (fromIntegral width, fromIntegral height, bitmap, m_x_hot, m_y_hot)
        1 -> return $ Left "readBitmapFile: BitmapOpenFailed"
        2 -> return $ Left "readBitmapFile: BitmapFileInvalid"
        3 -> return $ Left "readBitmapFile: BitmapNoMemory"
        _ -> return $ Left "readBitmapFile: BitmapUnknownError"
foreign import ccall unsafe "X11/Xlib.h XReadBitmapFile"
  xReadBitmapFile :: Display -> Drawable -> CString -> Ptr CInt -> Ptr CInt
                     -> Ptr Pixmap -> Ptr CInt -> Ptr CInt -> IO CInt