조회 수 5407 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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) {
    ;
}

 

TAG •

  1. No Image

    리눅스에서 시리얼 포트 사용 및 접근 권한

    Date2024.05.21 Bymakersweb Views3852
    Read More
  2. No Image

    리눅스 Qt 응용프로그램 AppImage 로 구축

    Date2024.01.07 Bymakersweb Views3511
    Read More
  3. No Image

    GRUB의 timeout 설정

    Date2023.11.07 Bymakersweb Views4021
    Read More
  4. lubuntu 22.04 LTS 설치

    Date2023.01.23 Bymakersweb Views6680
    Read More
  5. No Image

    시스템에서 사용 가능한 D-Bus 서비스를 보려면?

    Date2022.12.29 Bymakersweb Views6482
    Read More
  6. No Image

    리눅스에서 네트워크 구성

    Date2022.06.11 Bymakersweb Views6418
    Read More
  7. D-Bus ObjectManager

    Date2022.02.12 Bymakersweb Views3301
    Read More
  8. No Image

    ifconfig 는 대부분 ip 명령으로 대체

    Date2022.02.12 Bymakersweb Views4882
    Read More
  9. SocketCAN 유틸 사용방법

    Date2022.02.05 Bymakersweb Views16089
    Read More
  10. No Image

    dbus-broker를 기본 DBus 구현으로 설정

    Date2021.01.20 Bymakersweb Views4859
    Read More
  11. 리눅스 오디오 스택과 아키텍처

    Date2020.09.02 Bymakersweb Views6252
    Read More
  12. No Image

    wayland-scanner 를 통해 Wayland 프로토콜 코드생성

    Date2020.06.08 Bymakersweb Views4681
    Read More
  13. No Image

    Wayland 의 Client Application 프로그래밍 기본 루틴

    Date2020.06.04 Bymakersweb Views5407
    Read More
  14. No Image

    Wayland 의 주요 객체들

    Date2020.06.04 Bymakersweb Views4521
    Read More
  15. No Image

    Weston 의 설명 및 관련 컴포넌트

    Date2020.06.03 Bymakersweb Views6444
    Read More
  16. No Image

    64비트 리눅스에서 32비트 응용프로그램을 실행하려면

    Date2020.02.29 Bymakersweb Views4757
    Read More
  17. No Image

    initramfs (initial ram file system: 초기 램 파일 시스템)

    Date2020.02.25 Bymakersweb Views6143
    Read More
  18. No Image

    플랫폼 디바이스 드라이버 개발 시 많이 사용되는 커널 API 및 매크로

    Date2020.01.28 Bymakersweb Views8260
    Read More
  19. No Image

    PATH에 새로운 경로 추가

    Date2019.09.19 Bymakersweb Views3065
    Read More
  20. No Image

    리눅스 컴파일러 최신으로 업데이트

    Date2018.12.26 Bylinux Views4216
    Read More
Board Pagination Prev 1 2 3 Next
/ 3