dongweishi2028 2015-06-14 22:28
浏览 81
已采纳

CGO将Xlib XEvent结构转换为字节数组?

I am creating a simple window manager (code based of the c code in tinywm) in Golang. To use Xlib, I am using cgo, so my header is:

// #cgo LDFLAGS: -lX11
// #include <X11/Xlib.h>

And I have a variable declaration, like:

event := C.XEvent{}

And then, I use this to assign to it, later, in the event loop:

C.XNextEvent(display, &event) // Yes, display is defined

But when I try to access properties on the event, such as xbutton or xkey, I get an error:

event.xbutton undefined (type C.XEvent has no field or method xbutton)

And when I look at the cgo output for XEvent, it looks like this, in the _cgo_gotypes.go file:

type _Ctype_XEvent [192] byte

And I cant figure out what is going on, although I have a hunch that the type [192] byte is very wrong for a C struct type. In case this helps, the XEvent struct looks like this in the C library:

typedef union _XEvent {

    int type;   /* must not be changed */
    XAnyEvent xany;
    XKeyEvent xkey;
    XButtonEvent xbutton;
    XMotionEvent xmotion;
    XCrossingEvent xcrossing;
    XFocusChangeEvent xfocus;
    XExposeEvent xexpose;
    XGraphicsExposeEvent xgraphicsexpose;
    XNoExposeEvent xnoexpose;
    XVisibilityEvent xvisibility;
    XCreateWindowEvent xcreatewindow;
    XDestroyWindowEvent xdestroywindow;
    XUnmapEvent xunmap;
    XMapEvent xmap;
    XMapRequestEvent xmaprequest;
    XReparentEvent xreparent;
    XConfigureEvent xconfigure;
    XGravityEvent xgravity;
    XResizeRequestEvent xresizerequest;
    XConfigureRequestEvent xconfigurerequest;
    XCirculateEvent xcirculate;
    XCirculateRequestEvent xcirculaterequest;
    XPropertyEvent xproperty;
    XSelectionClearEvent xselectionclear;
    XSelectionRequestEvent xselectionrequest;
    XSelectionEvent xselection;
    XColormapEvent xcolormap;
    XClientMessageEvent xclient;
    XMappingEvent xmapping;
    XErrorEvent xerror;
    XKeymapEvent xkeymap;
    long pad[24];
} XEvent;
  • 写回答

1条回答 默认 最新

  • dongshubang7816 2015-06-15 18:29
    关注

    As mentioned in the cgo documentation:

    As Go doesn't have support for C's union type in the general case, C's union types are represented as a Go byte array with the same length.

    Another SO question: Golang CGo: converting union field to Go type
    or a go-nuts mailing list post may be of further help.

    In short, you won't be able to just simply use or simply interface with C code that uses a union. At a minimum you'll need set something like an unsafe.Pointer to manipulate the field/type manually and your example looks like a particularly annoying case (i.e. it's not just a union over a few different kinds of integers as above linked cases are).

    Given the names I get the impression you may want to create an "event" interface in Go and implement each of the required event types as Go types that implement that interface. Then write code (in Go or C) that converts to/from the C union based on the first C.sizeof(int) bytes of the Go "union"/[]byte (I think the first int type field is likely included in each if the X… event types).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭