-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegl_window.cpp
More file actions
292 lines (239 loc) · 8.43 KB
/
Copy pathegl_window.cpp
File metadata and controls
292 lines (239 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <wayland-client-core.h>
#include <wayland-client.h>
#include <wayland-client-protocol.h>
#include <wayland-egl.h> // Wayland EGL MUST be included before EGL headers
#include "xdg-shell-client-protocol.h"
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <GLES2/gl2.h>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>
#include <sys/time.h>
/* Wayland code */
struct client_state {
/* Globals */
struct wl_display *wl_display;
struct wl_registry *wl_registry;
struct wl_shm *wl_shm;
struct wl_compositor *wl_compositor;
struct xdg_wm_base *xdg_wm_base;
/* Objects */
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_egl_window *egl_window;
struct wl_region *region;
EGLNativeDisplayType native_display;
EGLNativeWindowType native_window;
uint16_t window_width, window_height;
EGLDisplay display;
EGLContext context;
EGLSurface surface;
bool program_alive;
int32_t old_w, old_h;
};
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h,
struct wl_array *states) {
struct client_state *state = (struct client_state *)data;
// no window geometry event, ignore
if(w == 0 && h == 0) return;
// window resized
if(state->old_w != w && state->old_h != h) {
state->old_w = w;
state->old_h = h;
wl_egl_window_resize(state->native_window, w, h, 0, 0);
wl_surface_commit(state->wl_surface);
}
}
static void xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_toplevel) {
struct client_state *state = (struct client_state *)data;
state->program_alive = false;
}
struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_handle_configure,
.close = xdg_toplevel_handle_close,
};
static void xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
// confirm that you exist to the compositor
xdg_surface_ack_configure(xdg_surface, serial);
}
const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_configure,
};
static void xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
xdg_wm_base_pong(xdg_wm_base, serial);
}
const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = xdg_wm_base_ping,
};
void create_native_window(struct client_state* state, char *title, int width, int height) {
state->old_w = width;
state->old_h = height;
state->region = wl_compositor_create_region(state->wl_compositor);
wl_region_add(state->region, 0, 0, width, height);
wl_surface_set_opaque_region(state->wl_surface, state->region);
struct wl_egl_window *egl_window =
wl_egl_window_create(state->wl_surface, width, height);
if (egl_window == EGL_NO_SURFACE) {
fprintf(stderr, "No window... \n");
exit(1);
}
state->window_width = width;
state->window_height = height;
state->native_window = egl_window;
}
EGLBoolean create_egl_context (struct client_state* state) {
EGLint numConfigs;
EGLint majorVersion;
EGLint minorVersion;
EGLContext context;
EGLSurface surface;
EGLConfig config;
EGLint fbAttribs[] = {
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 contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
EGLDisplay display = eglGetDisplay( state->native_display );
if ( display == EGL_NO_DISPLAY )
{
fprintf(stderr, "No EGL Display...\n");
return EGL_FALSE;
}
// Initialize EGL
if ( !eglInitialize(display, &majorVersion, &minorVersion) )
{
fprintf(stderr, "No Initialisation...\n");
return EGL_FALSE;
}
// Get configs
if ( (eglGetConfigs(display, NULL, 0, &numConfigs) != EGL_TRUE) || (numConfigs == 0))
{
fprintf(stderr, "No configuration...\n");
return EGL_FALSE;
}
// Choose config
if ( (eglChooseConfig(display, fbAttribs, &config, 1, &numConfigs) != EGL_TRUE) || (numConfigs != 1))
{
fprintf(stderr,"No configuration...\n");
return EGL_FALSE;
}
// Create a surface
surface = eglCreateWindowSurface(display, config, state->native_window, NULL);
if ( surface == EGL_NO_SURFACE )
{
fprintf(stderr, "No surface...\n");
return EGL_FALSE;
}
// Create a GL context
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs );
if ( context == EGL_NO_CONTEXT ) {
fprintf(stderr, "No context...\n");
return EGL_FALSE;
}
// Make the context current
if (!eglMakeCurrent(display, surface, surface, context) )
{
fprintf(stderr, "Could not make the current window current !\n");
return EGL_FALSE;
}
state->display = display;
state->surface = surface;
state->context = context;
return EGL_TRUE;
}
EGLBoolean create_window_with_egl_context(struct client_state* state, char *title, int width, int height) {
create_native_window(state, title, width, height);
return create_egl_context(state);
}
void draw() {
struct timeval tv;
gettimeofday(&tv, NULL);
double time = double (tv.tv_sec % 10) / 10.0;
glClearColor((float)time, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
void refresh_window(struct client_state *state) {
eglSwapBuffers(state->display, state->surface);
}
static void global_registry_handler(void *data, struct wl_registry *registry, uint32_t id,
const char *interface, uint32_t version)
{
struct client_state *state = (struct client_state *)data;
fprintf(stderr, "Got a registry event for %s id %d\n", interface, id);
if (strcmp(interface, "wl_compositor") == 0) {
state->wl_compositor = static_cast<wl_compositor *>(wl_registry_bind(registry, id, &wl_compositor_interface, 1));
} else if(strcmp(interface, xdg_wm_base_interface.name) == 0) {
state->xdg_wm_base = static_cast<xdg_wm_base *>(wl_registry_bind(registry, id,
&xdg_wm_base_interface, 1));
xdg_wm_base_add_listener(state->xdg_wm_base, &xdg_wm_base_listener, data);
}
}
static void global_registry_remover (void *data, struct wl_registry *registry, uint32_t id) {
fprintf(stderr, "Got a registry losing event for %d\n", id);
}
const struct wl_registry_listener listener = {
global_registry_handler,
global_registry_remover
};
static void get_server_references(struct client_state* client_state) {
struct wl_display * display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "Can't connect to wayland display.\n");
exit(1);
}
struct wl_registry *wl_registry =
wl_display_get_registry(display);
wl_registry_add_listener(wl_registry, &listener, client_state);
// This call the attached listener global_registry_handler
wl_display_dispatch(display);
wl_display_roundtrip(display);
// If at this point, global_registry_handler didn't set the
// compositor, nor the shell, bailout !
if (client_state->wl_compositor == NULL || client_state->xdg_wm_base == NULL) {
fprintf(stderr, "No compositor or XDG\n");
exit(1);
} else {
client_state->native_display = display;
}
}
void destroy_window(struct client_state* client_state) {
eglDestroySurface(client_state->display, client_state->surface);
wl_egl_window_destroy(client_state->native_window);
xdg_toplevel_destroy(client_state->xdg_toplevel);
xdg_surface_destroy(client_state->xdg_surface);
wl_surface_destroy(client_state->wl_surface);
eglDestroyContext(client_state->display, client_state->context);
}
int main() {
struct client_state state = {0};
get_server_references(&state);
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
if (state.wl_surface == NULL) {
fprintf(stderr, "No compositor surface.\n");
exit(1);
}
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface);
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
xdg_toplevel_set_title(state.xdg_toplevel, "Wayland EGL example");
xdg_toplevel_add_listener(state.xdg_toplevel, &xdg_toplevel_listener, &state);
wl_surface_commit(state.wl_surface);
create_window_with_egl_context(&state, "EGL", WINDOW_WIDTH, WINDOW_HEIGHT);
state.program_alive = true;
while (state.program_alive) {
wl_display_dispatch_pending(state.native_display);
draw();
refresh_window(&state);
}
destroy_window(&state);
wl_display_disconnect(state.native_display);
}