Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getWindowPropertyPtr returning IO (Maybe (ForeignPtr a, Int)) #104

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Graphics/X11/Xlib/Extras.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Graphics.X11.XScreenSaver
import Graphics.X11.Xlib
import Graphics.X11.Xlib.Internal
import Graphics.X11.Xlib.Types
import Foreign (Storable, Ptr, peek, poke, pokeArray, peekElemOff, peekByteOff, pokeByteOff, peekArray, throwIfNull, nullPtr, sizeOf, alignment, alloca, with, throwIf, Word8, Word16, #{type unsigned long}, Int32, plusPtr, castPtr, withArrayLen, setBit, testBit, allocaBytes, FunPtr)
import Foreign (Storable, Ptr, peek, poke, pokeArray, peekElemOff, peekByteOff, pokeByteOff, peekArray, throwIfNull, nullPtr, sizeOf, alignment, alloca, with, throwIf, Word8, Word16, #{type unsigned long}, Int32, plusPtr, castPtr, withArrayLen, setBit, testBit, allocaBytes, FunPtr, ForeignPtr, newForeignPtr, withForeignPtr)
import Foreign.C.Types
import Foreign.C.String
import Control.Monad
Expand Down Expand Up @@ -1363,8 +1363,8 @@ foreign import ccall unsafe "XlibExtras.h XDeleteProperty"
foreign import ccall unsafe "XlibExtras.h XGetWindowProperty"
xGetWindowProperty :: Display -> Window -> Atom -> CLong -> CLong -> Bool -> Atom -> Ptr Atom -> Ptr CInt -> Ptr CULong -> Ptr CULong -> Ptr (Ptr CUChar) -> IO Status

rawGetWindowProperty :: Storable a => Int -> Display -> Atom -> Window -> IO (Maybe [a])
rawGetWindowProperty bits d atom w =
getWindowPropertyPtr :: Storable a => Int -> Display -> Atom -> Window -> IO (Maybe (ForeignPtr a, Int))
getWindowPropertyPtr bits d atom w =
alloca $ \actual_type_return ->
alloca $ \actual_format_return ->
alloca $ \nitems_return ->
Expand All @@ -1388,10 +1388,10 @@ rawGetWindowProperty bits d atom w =
getprop prop_ptr nitems actual_format
| actual_format == 0 = return Nothing -- Property not found
| actual_format /= bits = xFree prop_ptr >> return Nothing
| otherwise = do
retval <- peekArray nitems (castPtr prop_ptr)
_ <- xFree prop_ptr
return $ Just retval
| otherwise = (\p -> Just (p, nitems)) <$> newForeignPtr xFreePtr (castPtr prop_ptr)

rawGetWindowProperty :: Storable a => Int -> Display -> Atom -> Window -> IO (Maybe [a])
rawGetWindowProperty bits d atom w = getWindowPropertyPtr bits d atom w >>= mapM (\(p,n) -> withForeignPtr p (peekArray n))

getWindowProperty8 :: Display -> Atom -> Window -> IO (Maybe [CChar])
getWindowProperty8 = rawGetWindowProperty 8
Expand Down
3 changes: 2 additions & 1 deletion Graphics/X11/Xlib/Internal.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
--
-----------------------------------------------------------------------------

module Graphics.X11.Xlib.Internal (xFree) where
module Graphics.X11.Xlib.Internal (xFree, xFreePtr) where

import Foreign
import Foreign.C.Types

foreign import ccall unsafe "XFree" xFree :: Ptr a -> IO CInt
foreign import ccall unsafe "&XFree" xFreePtr :: FinalizerPtr a