我正在尝试访问XKB API中的各种位和鲍勃。到目前为止,这是我的测试代码:
{-# 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)
虽然 (1) 输出0x0000000001777d80
,这听起来像是一个有效的地址,但 (2) 发出XkbDescRec {names = 0x0000000000000000}
。
我不知道我是否以错误的方式使用了 FFI,或者我是否误解了链接中详述的 XkbDescRec 结构的结构。