我試圖訪問XKB API中的各種位和bobs。這是到目前爲止我的測試代碼:使用hsc2hs與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結構的結構。
我開始認爲我誤解了XkbAllocKeyboard,並始終將該指針初始化爲空。如果有人能確認我會很樂意接受。 – Sarah 2014-09-04 21:30:04