版本比较

密钥

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

wl_display_roundtrip() 阻塞当前执行流,等待服务端处理完所有异步请求,这里就是请求是指wl_display_get_registry发送的获取所有registry对象的请求。

第2段

代码块
// Obtain the wl_registry and fetch the list of globals
    struct wl_registry *registry = wl_display_get_registry(display);
    wl_registry_add_listener(registry, &registry_listener, NULL);
    if (wl_display_roundtrip(display) == -1) {
        return EXIT_FAILURE;
    }
 
    // Check that all globals we require are available
    if (shm == NULL || compositor == NULL || xdg_wm_base == NULL) {
        fprintf(stderr, "no wl_shm, wl_compositor or xdg_wm_base support\n");
        return EXIT_FAILURE;
    }
    

...

xdg_wm_base_get_xdg_surface创建一块绑定到surface的xdg_surface(应用窗口surface)。

代码块
// Create a wl_surface, a xdg_surface and a xdg_toplevel
    surface = wl_compositor_create_surface(compositor);
    struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(xdg_wm_base, surface);
    xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
 
    xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL);
    xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL);
 
    // Perform the initial commit and wait for the first configure event
    wl_surface_commit(surface);
    while (wl_display_dispatch(display) != -1 && !configured) {
        // This space intentionally left blank
    }

...

wl_surface_attach将buffer绑定到surface

create and commit buffer

代码块
create_shm_file(void)
{
    int retries = 100;
    do {
        char name[] = "/wl_shm-XXXXXX";
        randname(name + sizeof(name) - 7);
        --retries;
        int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
        if (fd >= 0) {
            shm_unlink(name);
            return fd;
        }
    } while (retries > 0 && errno == EEXIST);
    return -1;
}
 
static struct wl_buffer *create_buffer(void) {
    int stride = width * 4;
    int size = stride * height;
 
    // Allocate a shared memory file with the right size
    int fd = create_shm_file(size);
    if (fd < 0) {
        fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
        return NULL;
    }
 
    // Map the shared memory file
    shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (shm_data == MAP_FAILED) {
        fprintf(stderr, "mmap failed: %m\n");
        close(fd);
        return NULL;
    }
 
    // Create a wl_buffer from our shared memory file descriptor
    struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
    struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, width, height,stride, WL_SHM_FORMAT_ARGB8888);
    wl_shm_pool_destroy(pool);
 
    // Now that we've mapped the file and created the wl_buffer, we no longer
    // need to keep file descriptor opened
    close(fd);
 
    // Copy pixels into our shared memory file (MagickImage is from cat.h)
    memcpy(shm_data, MagickImage, size);
 
    return buffer;
}
 
// Create a wl_buffer, attach it to the surface and commit the surface
    struct wl_buffer *buffer = create_buffer();
    if (buffer == NULL) {
        return EXIT_FAILURE;
    }
 
    wl_surface_attach(surface, buffer, 0, 0);
    

...