• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 static struct wl_registry_listener registry_listener = { &registry_add_object, &registry_remove_object };
37 
shell_surface_ping(void * data,struct wl_shell_surface * shell_surface,uint32_t serial)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 }
42 
shell_surface_configure(void * data,struct wl_shell_surface * shell_surface,uint32_t edges,int32_t width,int32_t height)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;
46 
47 	wl_egl_window_resize(window->egl_window, width, height, 0, 0);
48 }
49 
shell_surface_popup_done(void * data,struct wl_shell_surface * shell_surface)50 static void shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
51 {
52 
53 }
54 
55 static struct wl_shell_surface_listener shell_surface_listener = {
56 	&shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done
57 };
58 
create_window(struct window * window,int32_t width,int32_t height)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 }
67 
delete_window(struct window * window)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 }
74 
main_wl()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);
81 
82 	uint32_t width = 600;
83 	uint32_t height = 600;
84 
85 	struct window window;
86 	create_window(&window, width, height);
87 
88 	{
89 		EglState egl(display);
90 		EglSurface surface(egl, window.egl_window);
91 		GlScene scene;
92 
93 		scene.set_viewport(width, height);
94 
95 		int framenum = 0;
96 
97 		while (s_running) {
98 			wl_display_dispatch_pending(display);
99 
100 			surface.make_current();
101 			scene.draw(framenum++);
102 			surface.swap_buffers();
103 		}
104 	}
105 
106 	delete_window(&window);
107 	wl_display_disconnect(display);
108 }
109