c - CGO converting Xlib XEvent struct to byte array? -


i creating simple window manager (code based of c code in tinywm) in golang. use xlib, using cgo, header is:

// #cgo ldflags: -lx11 // #include <x11/xlib.h> 

and have variable declaration, like:

event := c.xevent{} 

and then, use assign it, later, in event loop:

c.xnextevent(display, &event) // yes, display defined 

but when try access properties on event, such xbutton or xkey, error:

event.xbutton undefined (type c.xevent has no field or method xbutton) 

and when @ cgo output xevent, looks this, in _cgo_gotypes.go file:

type _ctype_xevent [192] byte 

and cant figure out going on, although have hunch type [192] byte wrong c struct type. in case helps, xevent struct looks in c library:

typedef union _xevent {      int type;   /* must not 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; 

as mentioned in cgo documentation:

as go doesn't have support c's union type in general case, c's union types represented go byte array same length.

another question: golang cgo: converting union field go type
or a go-nuts mailing list post may of further help.

in short, won't able use or interface c code uses union. @ minimum you'll need set unsafe.pointer manipulate field/type manually , example looks particularly annoying case (i.e. it's not union on few different kinds of integers above linked cases are).

given names impression may want create "event" interface in go , implement each of required event types go types implement interface. write code (in go or c) converts to/from c union based on first c.sizeof(int) bytes of go "union"/[]byte (i think first int type field included in each if x… event types).


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -