kmscube: wayland
[android/external-libkmsxx.git] / kmscube / cube-wl.cpp
2 #include <wayland-client.h>
3 #include <wayland-egl.h>
5 #include <string.h>
6 #include <stdio.h>
8 #include "cube.h"
9 #include "cube-egl.h"
10 #include "cube-gles2.h"
12 static struct wl_compositor *s_compositor = NULL;
13 static struct wl_shell *s_shell = NULL;
14 static char s_running = 1;
16 struct window {
17         struct wl_surface *surface;
18         struct wl_shell_surface *shell_surface;
19         struct wl_egl_window *egl_window;
20 };
22 // listeners
23 static void registry_add_object(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
24 {
25         if (!strcmp(interface, "wl_compositor"))
26                 s_compositor = (struct wl_compositor*)wl_registry_bind(registry, name, &wl_compositor_interface, 0);
27         else if (!strcmp(interface, "wl_shell"))
28                 s_shell = (struct wl_shell*)wl_registry_bind(registry, name, &wl_shell_interface, 0);
29 }
31 static void registry_remove_object(void *data, struct wl_registry *registry, uint32_t name)
32 {
34 }
36 static struct wl_registry_listener registry_listener = { &registry_add_object, &registry_remove_object };
38 static void shell_surface_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial)
39 {
40         wl_shell_surface_pong(shell_surface, serial);
41 }
43 static void shell_surface_configure(void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height)
44 {
45         struct window *window = (struct window*)data;
47         wl_egl_window_resize(window->egl_window, width, height, 0, 0);
48 }
50 static void shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
51 {
53 }
55 static struct wl_shell_surface_listener shell_surface_listener = {
56         &shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done
57 };
59 static void create_window(struct window *window, int32_t width, int32_t height)
60 {
61         window->surface = wl_compositor_create_surface(s_compositor);
62         window->shell_surface = wl_shell_get_shell_surface(s_shell, window->surface);
63         wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
64         wl_shell_surface_set_toplevel(window->shell_surface);
65         window->egl_window = wl_egl_window_create(window->surface, width, height);
66 }
68 static void delete_window(struct window *window)
69 {
70         wl_egl_window_destroy(window->egl_window);
71         wl_shell_surface_destroy(window->shell_surface);
72         wl_surface_destroy(window->surface);
73 }
75 void main_wl()
76 {
77         struct wl_display *display = wl_display_connect(NULL);
78         struct wl_registry *registry = wl_display_get_registry(display);
79         wl_registry_add_listener(registry, &registry_listener, NULL);
80         wl_display_roundtrip(display);
82         uint32_t width = 600;
83         uint32_t height = 600;
85         struct window window;
86         create_window(&window, width, height);
88         {
89                 EglState egl(display);
90                 EglSurface surface(egl, window.egl_window);
91                 GlScene scene;
93                 scene.set_viewport(width, height);
95                 int framenum = 0;
97                 while (s_running) {
98                         wl_display_dispatch_pending(display);
100                         surface.make_current();
101                         scene.draw(framenum++);
102                         surface.swap_buffers();
103                 }
104         }
106         delete_window(&window);
107         wl_display_disconnect(display);