我正在尝试访问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产生一个空白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是“一切”的掩码。
关于haskell - 通过hsc2hs与XKB API接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25674775/