kmscube: split into parts
[android/external-libkmsxx.git] / kmscube / cube-wl.cpp
2 #include <wayland-client.h>
3 #include <wayland-egl.h>
5 #include <EGL/egl.h>
6 #include <GL/gl.h>
8 #include <string.h>
10 static struct wl_compositor *s_compositor = NULL;
11 static struct wl_shell *s_shell = NULL;
12 static EGLDisplay s_egl_display;
13 static char s_running = 1;
15 struct window {
16         EGLContext egl_context;
17         struct wl_surface *surface;
18         struct wl_shell_surface *shell_surface;
19         struct wl_egl_window *egl_window;
20         EGLSurface egl_surface;
21 };
23 // listeners
24 static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
25 {
26         if (!strcmp(interface, "wl_compositor"))
27                 s_compositor = (struct wl_compositor*)wl_registry_bind(registry, name, &wl_compositor_interface, 0);
28         else if (!strcmp(interface, "wl_shell"))
29                 s_shell = (struct wl_shell*)wl_registry_bind(registry, name, &wl_shell_interface, 0);
30 }
32 static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name)
33 {
35 }
37 static struct wl_registry_listener registry_listener = { &registry_add_object, &registry_remove_object };
39 static void shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial)
40 {
41         wl_shell_surface_pong(shell_surface, serial);
42 }
44 static void shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height)
45 {
46         struct window *window = (struct window*)data;
48         wl_egl_window_resize(window->egl_window, width, height, 0, 0);
49 }
51 static void shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
52 {
54 }
56 static struct wl_shell_surface_listener shell_surface_listener = { &shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done };
58 static void create_window(struct window *window, int32_t width, int32_t height)
59 {
60         eglBindAPI(EGL_OPENGL_API);
61         EGLint attributes[] = {
62                 EGL_RED_SIZE,   8,
63                 EGL_GREEN_SIZE, 8,
64                 EGL_BLUE_SIZE,  8,
65                 EGL_NONE
66         };
67         EGLConfig config;
68         EGLint num_config;
69         eglChooseConfig(s_egl_display, attributes, &config, 1, &num_config);
70         window->egl_context = eglCreateContext(s_egl_display, config, EGL_NO_CONTEXT, NULL);
72         window->surface = wl_compositor_create_surface(s_compositor);
73         window->shell_surface = wl_shell_get_shell_surface(s_shell, window->surface);
74         wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
75         wl_shell_surface_set_toplevel(window->shell_surface);
76         window->egl_window = wl_egl_window_create(window->surface, width, height);
77         window->egl_surface = eglCreateWindowSurface(s_egl_display, config, window->egl_window, NULL);
78         eglMakeCurrent(s_egl_display, window->egl_surface, window->egl_surface, window->egl_context);
79 }
81 static void delete_window(struct window *window)
82 {
83         eglDestroySurface(s_egl_display, window->egl_surface);
84         wl_egl_window_destroy(window->egl_window);
85         wl_shell_surface_destroy(window->shell_surface);
86         wl_surface_destroy(window->surface);
87         eglDestroyContext(s_egl_display, window->egl_context);
88 }
90 static void draw_window(struct window *window)
91 {
92         glClearColor(0.0, 1.0, 0.0, 1.0);
93         glClear(GL_COLOR_BUFFER_BIT);
94         eglSwapBuffers(s_egl_display, window->egl_surface);
95 }
97 int main_wl()
98 {
99         struct wl_display *display = wl_display_connect(NULL);
100         struct wl_registry *registry = wl_display_get_registry(display);
101         wl_registry_add_listener(registry, &registry_listener, NULL);
102         wl_display_roundtrip(display);
104         s_egl_display = eglGetDisplay(display);
105         eglInitialize(s_egl_display, NULL, NULL);
107         uint32_t width = 600;
108         uint32_t height = 600;
110         struct window window;
111         create_window(&window, width, height);
113         while (s_running) {
114                 wl_display_dispatch_pending(display);
115                 draw_window(&window);
116         }
118         delete_window(&window);
119         eglTerminate(s_egl_display);
120         wl_display_disconnect(display);
121         return 0;