CGO converting Xlib XEvent struct to byte array?

327 views Asked by At

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

There are 1 answers

0
Dave C On BEST ANSWER

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).