我正试图访问XKB API中的各种位和bob。到目前为止,这是我的测试代码:
{-# 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结构的结构。
答案 0 :(得分:1)
XkbAllocKeyboard生成一个空白的XkbDescRec,由程序员填充。正确的方法是使用XkbGetKeyboard。我做了如下,更改和必要的位:
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正确填充,0x7f是&#34;所有内容的掩码!&#34;