參考資訊:
https://jan.newmarch.name/Wayland/index.html
https://wayland.freedesktop.org/docs/html/apa.html
https://bugaevc.gitbooks.io/writing-wayland-clients/content/
https://gist.github.com/Miouyouyou/ca15af1c7f2696f66b0e013058f110b4
main.c
#include <string.h> #include <stdlib.h> #include <wayland-server.h> #include <wayland-client.h> #include <wayland-client-protocol.h> #include <wayland-egl.h> #include <EGL/egl.h> #include <EGL/eglplatform.h> #include <GLES2/gl2.h> #define LCD_W 720 #define LCD_H 1440 struct wl_shell *wl_shell = NULL; struct wl_region *wl_region = NULL; struct wl_display *wl_display = NULL; struct wl_surface *wl_surface = NULL; struct wl_registry *wl_registry = NULL; struct wl_egl_window *wl_egl_window = NULL; struct wl_compositor *wl_compositor = NULL; struct wl_shell_surface *wl_shell_surface = NULL; 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) { wl_compositor = wl_registry_bind(registry, id, &wl_compositor_interface, 1); } else if (strcmp(interface, "wl_shell") == 0) { wl_shell = wl_registry_bind(registry, id, &wl_shell_interface, 1); } } static void global_registry_remover(void *data, struct wl_registry *registry, uint32_t id) { } const struct wl_registry_listener listener = { global_registry_handler, global_registry_remover }; int main(int argc, char **argv) { wl_display = wl_display_connect(NULL); wl_registry = wl_display_get_registry(wl_display); wl_registry_add_listener(wl_registry, &listener, NULL); wl_display_dispatch(wl_display); wl_display_roundtrip(wl_display); wl_surface = wl_compositor_create_surface(wl_compositor); wl_shell_surface = wl_shell_get_shell_surface(wl_shell, wl_surface); wl_shell_surface_set_toplevel(wl_shell_surface); wl_region = wl_compositor_create_region(wl_compositor); wl_region_add(wl_region, 0, 0, LCD_W, LCD_H); wl_surface_set_opaque_region(wl_surface, wl_region); wl_egl_window = wl_egl_window_create(wl_surface, LCD_W, LCD_H); EGLContext egl_context; EGLSurface egl_surface; EGLint egl_numConfigs; EGLint egl_majorVersion; EGLint egl_minorVersion; EGLConfig egl_config; EGLint egl_attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_NONE }; EGLint egl_context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; EGLDisplay egl_display = eglGetDisplay(wl_display); eglInitialize(egl_display, &egl_majorVersion, &egl_minorVersion); eglGetConfigs(egl_display, NULL, 0, &egl_numConfigs); eglChooseConfig(egl_display, egl_attribs, &egl_config, 1, &egl_numConfigs); egl_surface = eglCreateWindowSurface(egl_display, egl_config, wl_egl_window, NULL); egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, egl_context_attribs); eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context); int cc = 60; while (cc--) { wl_display_dispatch_pending(wl_display); switch (cc % 3) { case 0: glClearColor(1.0, 0.0, 0.0, 1.0); break; case 1: glClearColor(0.0, 1.0, 0.0, 1.0); break; case 2: glClearColor(0.0, 0.0, 1.0, 1.0); break; } glClear(GL_COLOR_BUFFER_BIT); eglSwapBuffers(egl_display, egl_surface); } eglDestroySurface(egl_display, egl_surface); eglDestroyContext(egl_display, egl_context); wl_egl_window_destroy(wl_egl_window); eglTerminate(egl_display); wl_region_destroy(wl_region); wl_shell_surface_destroy(wl_shell_surface); wl_shell_destroy(wl_shell); wl_surface_destroy(wl_surface); wl_compositor_destroy(wl_compositor); wl_registry_destroy(wl_registry); wl_display_disconnect(wl_display); return 0; }
編譯、執行
$ gcc main.c -o main -lwayland-client -lwayland-egl -lEGL -lGLESv2 $ ./main