Interaction with XKB API with hsc2hs
I am trying to access various bits and beans in the XKB API . This is my test code:
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
import Foreign.C.Types
#include <X11/XKBlib.h>
#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
data XkbDescRec = XkbDescRec { names :: Ptr XkbNamesRec } deriving (Show)
data XkbNamesRec = XkbNamesRec { groups :: Ptr Word64 } -- Ignore me
foreign import ccall unsafe "X11/XKBlib.h XkbAllocKeyboard"
xkbAllocKeyboard :: IO (Ptr XkbDescRec)
instance Storable XkbDescRec where
sizeOf _ = (#size XkbDescRec)
alignment _ = (#alignment XkbDescRec)
peek ptr = do
names <- (#peek XkbDescRec, names) ptr
return $ XkbDescRec names
main = do
xkbDescPtr <- xkbAllocKeyboard
print xkbDescPtr -- (1)
peek xkbDescPtr >>= print -- (2)
While (1) outputs 0x0000000001777d80
, which sounds like a valid address, (2) emits XkbDescRec {names = 0x0000000000000000}
.
I don't know if I am using FFI in the wrong way or if I misunderstood the structure of the XkbDescRec structure detailed in the link.
+3
source to share
1 answer
The XkbAllocKeyboard creates an empty XkbDescRec for the programmer to populate. The correct method is to use XkbGetKeyboard. I did the following, changes and necessary bits:
import Graphics.X11.Xlib (openDisplay, Display(..))
foreign import ccall unsafe "X11/XKBlib.h XkbGetKeyboard"
xkbGetKeyboard :: Display -> CUInt -> CUInt -> IO (Ptr XkbDescRec)
main = do
dpy <- openDisplay ""
xkbDescPtr <- xkbGetKeyboard dpy 0x7f (#const XkbUseCoreKbd)
XkbDescRep is filled in correctly, and 0x7f is the mask for "everything!"
+1
source to share