程式語言 - Wayland - Client - Create Surface



參考資訊:
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 <string.h>
#include <wayland-client.h>

struct wl_shell *shell = NULL;
struct wl_display *dis = NULL;
struct wl_surface *surf = NULL;
struct wl_registry *reg = NULL;
struct wl_compositor *comp = NULL;
struct wl_shell_surface *shell_surf = NULL;
   
void cb_handle(void *dat, struct wl_registry *reg, uint32_t id, const char *intf, uint32_t ver)
{
    if (strcmp(intf, "wl_compositor") == 0) {
        comp = wl_registry_bind(reg, id, &wl_compositor_interface, 3);
    }
    else if (strcmp(intf, "wl_shell") == 0) {
        shell = wl_registry_bind(reg, id, &wl_shell_interface, 1);
    }
}
    
void cb_remove(void *dat, struct wl_registry *reg, uint32_t id)
{
}
    
struct wl_registry_listener cb = {
    .global = cb_handle,
    .global_remove = cb_remove
};

int main(int argc, char **argv)
{
    dis = wl_display_connect(NULL);
    reg = wl_display_get_registry(dis);

    wl_registry_add_listener(reg, &cb, NULL);
    wl_display_roundtrip(dis);
    printf("comp = 0x%08x\n", comp);
    printf("shell = 0x%08x\n", shell);

    surf = wl_compositor_create_surface(comp);
    printf("surf = 0x%08x\n", surf);

    shell_surf = wl_shell_get_shell_surface(shell, surf);
    printf("shell_surf = 0x%08x\n", shell_surf);

    wl_shell_surface_set_toplevel(shell_surf);
    wl_display_roundtrip(dis);

    wl_shell_surface_destroy(shell_surf);
    wl_shell_destroy(shell);
    wl_surface_destroy(surf);
    wl_compositor_destroy(comp);
    wl_registry_destroy(reg);
    wl_display_disconnect(dis);
    return 0;
}

編譯、執行

$ gcc main.c -o main -lwayland-client
$ ./main
    comp = 0x2fc37320
    shell = 0x2fc373b0
    surf = 0x2fc37220
    shell_surf = 0x2fc37280