Hello all,
I'm starting work on getting Go able to interact with Wayland
compositors, and I'm running into trouble with a simple wrapping of
libwayland-client. (I mean it only as a toy demo, so I can get my feet
wet.)
Basically, the Wayland API is full of function pointers and uses
callbacks heavily. This poses a problem with cgo because it's
difficult to switch between C and Go function values. I want to use
something like [1] to work around it. It isn't pretty, but it should
be able to work. (I know I'll get hosed if Wayland uses multiple
threads, but as of right now, Wayland doesn't use multiple threads.
Besides, are there any other choices?)
Now, for my real problem. One of the central structs used in Wayland
does not seem to be translatable to a Go type, and I'm not sure why.
Here is a minimal test case [2] that demonstrates my problem (you'll
need the Wayland client libraries installed to run, but I've hopefully
included enough so that you don't have to). The output of [2] is
simply: "Type *[0]uint8". Also, the output of `go tool cgo --godefs`
is here [3].
Obviously, the type of "*[0]uint8" is prohibitory. But is there a way
to work around this?
What I was thinking was that there was something about the wl_display
struct that cannot be translated to Go. So to inspect more closely, I
copied out the wl_display struct (and all of its necessary ancillary
types) and created an isolated example (that does not require Wayland
to run) to see if I could reproduce the *[0]uint8 type. [4]
Interestingly, the output of this program is correct: "Type:
*main._Ctype_struct_wl_display". Inspecting further, here is the
output of `go tool cgo --godefs` [5]. Aha! Notice that the "Update"
and "Global_handler" members have type "*[0]byte". They are function
pointers; could that be why they are getting bogus types?
Can anyone explain what is going on here? Why does the entire
wl_display struct get chopped to *[0]uint8 when wrapping
libwayland-client, but not when the type is reproduced exactly outside
of the Wayland libs? Is it possible to work with *[0]uint8 values?
It would be even better if there is a way to work around this.
(Also, cgo looks nearly required to proceed forward with Wayland and
Go at this point. Otherwise I'd happily do this in pure Go. Namely,
EGL itself links to libwayland-client and therefore needs
libwayland-client values to work. It's possible to do a pure Go
implementation, but it would have to do software rendering. Of course,
one could write EGL in Go, I guess........)
[1] -
http://stackoverflow.com/a/6147097/619216
[2] -
http://pastebin.com/neHSyTSc
[3] -
http://pastebin.com/9TfdeE2S
[4] -
http://pastebin.com/ZYJvvfBu
[5] -
http://pastebin.com/vS53fsYW
- Andrew