can struct type itself be passed to function as pa

2019-07-31 15:56发布

While studying wayland protocol, I found code that functions takes struct type as parameter.

#include <wayland-server.h>    
static struct wl_compositor_interface compositor_interface =
        {&compositor_create_surface, &compositor_create_region};

    int main() {
        wl_global_create (display, &wl_compositor_interface, 3, NULL, 
                          &compositor_bind);
    }

signature of wl_global_create is

struct wl_global* wl_global_create  (struct wl_display *display,
                                     const struct wl_interface *interface,
                                     int    version,
                                     void *data,
                                     wl_global_bind_func_t bind)

wl_compositor_interface is structure type, not a variable name. but wl_global_create() take structure type as function parameter. can someone explain how this works?

the source code I read is here. https://github.com/eyelash/tutorials/blob/master/wayland-compositor/wayland-compositor.c

标签: c wayland
1条回答
神经病院院长
2楼-- · 2019-07-31 16:29

I browsed through the source code, and there is both a struct wl_compositor_interface and a variable wl_compositor_interface.

The included wayland_server.h includes, at the bottom, wayland-server-protocol.h. Unfortunately, this is not available online, but is generated at build time. You can get it with:

$ git clone git://anongit.freedesktop.org/wayland/wayland
$ cd wayland
$ mkdir prefix
$ ./autogen.sh --prefix=$(pwd)/prefix --disable-documentation
$ make protocol/wayland-server-protocol.h

In this file, it has the (somewhat confusing) definitions:

extern const struct wl_interface wl_compositor_interface; // On line 195
...
struct wl_compositor_interface { // Starting on line 986
    void (*create_surface)(struct wl_client *client,
                   struct wl_resource *resource,
                   uint32_t id);

    void (*create_region)(struct wl_client *client,
                  struct wl_resource *resource,
                  uint32_t id);
};

It's the struct that's being referenced the first time, and the variable the second.

查看更多
登录 后发表回答