1
2 #include <wayland-client.h>
3 #include <wayland-egl.h>
4
5 #include <string.h>
6 #include <stdio.h>
7
8 #include "cube.h"
9 #include "cube-egl.h"
10 #include "cube-gles2.h"
11
12 static struct wl_compositor* s_compositor = NULL;
13 static struct wl_shell* s_shell = NULL;
14 static char s_running = 1;
15
16 struct window {
17 struct wl_surface* surface;
18 struct wl_shell_surface* shell_surface;
19 struct wl_egl_window* egl_window;
20 };
21
22 // listeners
registry_add_object(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)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 }
30
registry_remove_object(void * data,struct wl_registry * registry,uint32_t name)31 static void registry_remove_object(void* data, struct wl_registry* registry, uint32_t name)
32 {
33 }
34
35 static struct wl_registry_listener registry_listener = { ®istry_add_object, ®istry_remove_object };
36
shell_surface_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)37 static void shell_surface_ping(void* data, struct wl_shell_surface* shell_surface, uint32_t serial)
38 {
39 wl_shell_surface_pong(shell_surface, serial);
40 }
41
shell_surface_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)42 static void shell_surface_configure(void* data, struct wl_shell_surface* shell_surface, uint32_t edges, int32_t width, int32_t height)
43 {
44 struct window* window = (struct window*)data;
45
46 wl_egl_window_resize(window->egl_window, width, height, 0, 0);
47 }
48
shell_surface_popup_done(void * data,struct wl_shell_surface * shell_surface)49 static void shell_surface_popup_done(void* data, struct wl_shell_surface* shell_surface)
50 {
51 }
52
53 static struct wl_shell_surface_listener shell_surface_listener = {
54 &shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done
55 };
56
create_window(struct window * window,int32_t width,int32_t height)57 static void create_window(struct window* window, int32_t width, int32_t height)
58 {
59 window->surface = wl_compositor_create_surface(s_compositor);
60 window->shell_surface = wl_shell_get_shell_surface(s_shell, window->surface);
61 wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
62 wl_shell_surface_set_toplevel(window->shell_surface);
63 window->egl_window = wl_egl_window_create(window->surface, width, height);
64 }
65
delete_window(struct window * window)66 static void delete_window(struct window* window)
67 {
68 wl_egl_window_destroy(window->egl_window);
69 wl_shell_surface_destroy(window->shell_surface);
70 wl_surface_destroy(window->surface);
71 }
72
main_wl()73 void main_wl()
74 {
75 struct wl_display* display = wl_display_connect(NULL);
76 struct wl_registry* registry = wl_display_get_registry(display);
77 wl_registry_add_listener(registry, ®istry_listener, NULL);
78 wl_display_roundtrip(display);
79
80 uint32_t width = 600;
81 uint32_t height = 600;
82
83 struct window window;
84 create_window(&window, width, height);
85
86 {
87 EglState egl(display);
88 EglSurface surface(egl, window.egl_window);
89 GlScene scene;
90
91 scene.set_viewport(width, height);
92
93 int framenum = 0;
94
95 while (s_running) {
96 wl_display_dispatch_pending(display);
97
98 surface.make_current();
99 scene.draw(framenum++);
100 surface.swap_buffers();
101 }
102 }
103
104 delete_window(&window);
105 wl_display_disconnect(display);
106 }
107