程式語言 - Wayland - Wayland Client (xdg-shell) - Add Listener



參考資訊:
https://jan.newmarch.name/Wayland/index.html
https://wayland.freedesktop.org/docs/html/apa.html
https://bugaevc.gitbooks.io/writing-wayland-clients/content/

main.c

#include <stdio.h>
#include <wayland-client.h>
  
void cb_handle(void *dat, struct wl_registry *reg, uint32_t id, const char *intf, uint32_t ver)
{
    printf("%s, intf:%s, ver:%u, id:%u\n", __func__, intf, ver, id);
}
  
void cb_remove(void *dat, struct wl_registry *reg, uint32_t id)
{
    printf("%s, %u\n", __func__, id);
}
  
int main(int argc, char **argv)
{
    struct wl_display *dis = wl_display_connect(NULL);
    struct wl_registry *reg = wl_display_get_registry(dis);
    struct wl_registry_listener cb = {
        .global = cb_handle,
        .global_remove = cb_remove
    };
  
    wl_registry_add_listener(reg, &cb, NULL);
    wl_display_dispatch(dis);
    wl_display_roundtrip(dis);

    wl_registry_destroy(reg);
    wl_display_disconnect(dis);
    return 0;
}

編譯、執行

$ mutter --nested --wayland &
$ gcc main.c -o main -lwayland-client
$ ./main
    cb_handle, intf:wl_compositor, ver:5, id:1
    cb_handle, intf:wl_drm, ver:2, id:2
    cb_handle, intf:wl_shm, ver:1, id:3
    cb_handle, intf:wl_output, ver:3, id:4
    cb_handle, intf:zxdg_output_manager_v1, ver:3, id:5
    cb_handle, intf:wl_data_device_manager, ver:3, id:6
    cb_handle, intf:zwp_primary_selection_device_manager_v1, ver:1, id:7
    cb_handle, intf:wl_subcompositor, ver:1, id:8
    cb_handle, intf:xdg_wm_base, ver:4, id:9
    cb_handle, intf:gtk_shell1, ver:5, id:10
    cb_handle, intf:wp_viewporter, ver:1, id:11
    cb_handle, intf:zwp_pointer_gestures_v1, ver:3, id:12
    cb_handle, intf:zwp_tablet_manager_v2, ver:1, id:13
    cb_handle, intf:wl_seat, ver:8, id:14
    cb_handle, intf:zwp_pointer_constraints_v1, ver:1, id:15
    cb_handle, intf:zxdg_exporter_v1, ver:1, id:16
    cb_handle, intf:zxdg_importer_v1, ver:1, id:17
    cb_handle, intf:zwp_linux_dmabuf_v1, ver:4, id:18
    cb_handle, intf:wp_single_pixel_buffer_manager_v1, ver:1, id:19
    cb_handle, intf:zwp_keyboard_shortcuts_inhibit_manager_v1, ver:1, id:20
    cb_handle, intf:zwp_text_input_manager_v3, ver:1, id:21
    cb_handle, intf:wp_presentation, ver:1, id:22
    cb_handle, intf:xdg_activation_v1, ver:1, id:23