한국어
Linux Programming
 

Wayland 의 Client Application 프로그래밍 시 기본 루틴을 설명

 

1. wl_display를 가져 와서 wl_registry를 얻는다. 

struct wl_display *display = NULL;

display = wl_display_connect(NULL);
struct wl_registry *registry = wl_display_get_registry(display);

 

2. 레지스트리를 스캔하고 wl_compositor 및 wl_shm_pool을 가져온다.

struct wl_compositor *compositor = NULL;

struct wl_shm_listener shm_listener = {
    shm_format
};

static void global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
      const char *interface, uint32_t version)
{
    if (strcmp(interface, "wl_compositor") == 0) {
        compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1);
    } else if (strcmp(interface, "wl_shell") == 0) {
        shell = wl_registry_bind(registry, id, &wl_shell_interface, 1);
    } else if (strcmp(interface, "wl_shm") == 0) {
        shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
        wl_shm_add_listener(shm, &shm_listener, NULL);
    }
}

static void global_registry_remover(void *data, struct wl_registry *registry, uint32_t id)
{
    printf("Got a registry losing event for %d\n", id);
}

static const struct wl_registry_listener registry_listener = {
    global_registry_handler,
    global_registry_remover
};

wl_registry_add_listener(registry, &registryListener, this);

 

3. wl_compositor 인터페이스를 사용하여 wl_surface를 생성한다.

struct wl_surface *surface = NULL;

surface = wl_compositor_create_surface(compositor);

 

4. 표면의 역할을 부여하기 위해 wl_shell 인터페이스를 사용

struct wl_shell_surface *shell_surface = NULL;
shell_surface = wl_shell_get_shell_surface(shell, surface); // 역할 부여

wl_shell_surface_set_toplevel(shell_surface);
wl_shell_surface_add_listener(shell_surface, &shell_surface_listener, NULL);

 

5. wl_shm 인터페이스를 사용하여 픽셀을 저장할 공유 메모리를 할당하고 공유 메모리 버퍼를 wl_surface에 연결한다.

void *shm_data;

int WIDTH = 480;
int HEIGHT = 360;

int os_create_anonymous_file(off_t size)
{
    static const char template[] = "/weston-shared-XXXXXX";
    const char *path;
    char *name;
    int fd;
    
    path = getenv("XDG_RUNTIME_DIR");
    if (!path) {
        errno = ENOENT;
        return -1;
    }
    
    name = malloc(strlen(path) + sizeof(template));
    if (!name)
        return -1;
    strcpy(name, path);
    strcat(name, template);
    
    fd = create_tmpfile_cloexec(name);
    
    free(name);
    
    if (fd < 0)
        return -1;
    
    if (ftruncate(fd, size) < 0) {
        close(fd);
        return -1;
    }
    
    return fd;
}

static struct wl_buffer *create_buffer() {
    struct wl_shm_pool *pool;
    int stride = WIDTH * 4; // 4 bytes per pixel
    int size = stride * HEIGHT;
    int fd;
    struct wl_buffer *buff;

    fd = os_create_anonymous_file(size);
    if (fd < 0) {
        fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size);
        exit(1);
    }
   
    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);
        exit(1);
    }

    pool = wl_shm_create_pool(shm, fd, size);
    buff = wl_shm_pool_create_buffer(pool, 0, WIDTH, HEIGHT, stride, WL_SHM_FORMAT_XRGB8888);
    //wl_buffer_add_listener(buffer, &buffer_listener, buffer);
    wl_shm_pool_destroy(pool);
    return buff;
}

static void create_shm() {
    buffer = create_buffer();
    wl_surface_attach(surface, buffer, 0, 0);
    wl_surface_commit(surface);
}

 

6. 공유 메모리 버퍼에 뭔가를 그린다.

static void paint_pixels() {
    int n;
    uint32_t *pixel = shm_data;

    fprintf(stderr, "Painting pixels\n");
    for (n =0; n < WIDTH * HEIGHT; n++) {
        *pixel++ = 0xffff;
    }
}

 

7. 이벤트 처리를 요청한다.

// 이벤트를 처리.
while (wl_display_dispatch(display) != -1) {
    ;
}