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 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号