Steward
分享是一種喜悅、更是一種幸福
手機 - PINE64 PinePhone - Sailfish OS - Wayland (Client) - Draw Pixel
參考資訊:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #include <stdint.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/mman.h> #include <syscall.h> #include <unistd.h> #include <fcntl.h> #include <wayland-client.h> #define WIDTH 720 #define HEIGHT 1440 #define STRIDE (WIDTH * 2) #define SIZE (STRIDE * HEIGHT) #define SHM_NAME "/tmp/shm" struct wl_shm *shm = NULL ; struct wl_buffer *buf = NULL ; struct wl_shell *shell = NULL ; struct wl_display *dis = NULL ; struct wl_surface *surf = NULL ; struct wl_registry *reg = NULL ; struct wl_shm_pool *pool = 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_shm" ) == 0) { shm = wl_registry_bind (reg, id, & wl_shm_interface , 1); } 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_dispatch (dis); wl_display_roundtrip (dis); surf = wl_compositor_create_surface ( comp ); shell_surf = wl_shell_get_shell_surface (shell, surf); wl_shell_surface_set_toplevel (shell_surf); unlink ( SHM_NAME ); int fd = open ( SHM_NAME , O_RDWR | O_EXCL | O_CREAT ); ftruncate (fd, SIZE ); uint16_t * addr = mmap ( NULL , SIZE , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 0); pool = wl_shm_create_pool (shm, fd, SIZE ); buf = wl_shm_pool_create_buffer (pool, 0, WIDTH , HEIGHT , STRIDE , WL_SHM_FORMAT_RGB565 ); wl_shm_pool_destroy (pool); printf ( "mmap = 0x%08x\n" , addr ); int x = 0, y = 0; uint16_t *p = addr ; for (y = 0; y < HEIGHT ; y++) { for (x = 0; x < WIDTH ; x++) { *p++ = 0xf800; } } wl_surface_attach (surf, buf, 0, 0); wl_surface_commit (surf); wl_display_dispatch (dis); wl_display_roundtrip (dis); usleep (3000000); wl_shell_surface_destroy (shell_surf); wl_shell_destroy (shell); wl_surface_destroy (surf); wl_buffer_destroy (buf); wl_shm_destroy (shm); wl_compositor_destroy ( comp ); wl_registry_destroy (reg); wl_display_disconnect (dis); munmap ( addr , SIZE ); close (fd); unlink ( SHM_NAME ); return 0; } |
編譯、執行
$ gcc main.c -o main -lwayland-client $ ./main mmap = 0x4023a000