1 /*
2 * Copyright © 2011 Benjamin Franzke
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdbool.h>
29 #include <math.h>
30 #include <assert.h>
31 #include <signal.h>
32
33 #include <linux/input.h>
34
35 #include <wayland-egl.h>
36
37 #include <GLES2/gl2.h>
38 #include <EGL/egl.h>
39 #include <EGL/eglext.h>
40
41 #include <wms-client-protocol.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 #include "shared/helpers.h"
46 #include "shared/platform.h"
47 #include "shared/weston-egl-ext.h"
48
49 #define LOG(fmt, ...) printf("simple egl: " fmt "\n", ##__VA_ARGS__)
50
51 struct window;
52 struct seat;
53
54 struct display {
55 struct wl_display *display;
56 struct wl_registry *registry;
57 struct wl_compositor *compositor;
58 // struct wl_seat *seat;
59 // struct wl_pointer *pointer;
60 // struct wl_touch *touch;
61 // struct wl_keyboard *keyboard;
62 // struct wl_shm *shm;
63 struct wms *wms;
64 struct {
65 EGLDisplay dpy;
66 EGLContext ctx;
67 EGLConfig conf;
68 } egl;
69 struct window *window;
70
71 PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC swap_buffers_with_damage;
72 };
73
74 struct geometry {
75 int width, height;
76 };
77
78 struct window {
79 struct display *display;
80 struct geometry geometry, window_size;
81 struct {
82 GLuint rotation_uniform;
83 GLuint pos;
84 GLuint col;
85 } gl;
86
87 uint32_t benchmark_time, frames;
88 struct wl_egl_window *native;
89 struct wl_surface *surface;
90 EGLSurface egl_surface;
91 struct wl_callback *callback;
92 int fullscreen, maximized, opaque, buffer_size, frame_sync, delay;
93 bool wait_for_configure;
94 };
95
96 static const char *vert_shader_text =
97 "uniform mat4 rotation;\n"
98 "attribute vec4 pos;\n"
99 "attribute vec4 color;\n"
100 "varying vec4 v_color;\n"
101 "void main() {\n"
102 " gl_Position = rotation * pos;\n"
103 " v_color = color;\n"
104 "}\n";
105
106 static const char *frag_shader_text =
107 "precision mediump float;\n"
108 "varying vec4 v_color;\n"
109 "void main() {\n"
110 " gl_FragColor = v_color;\n"
111 "}\n";
112
113 static int running = 1;
114
115 static void
init_egl(struct display * display,struct window * window)116 init_egl(struct display *display, struct window *window)
117 {
118 static const struct {
119 char *extension, *entrypoint;
120 } swap_damage_ext_to_entrypoint[] = {
121 {
122 .extension = "EGL_EXT_swap_buffers_with_damage",
123 .entrypoint = "eglSwapBuffersWithDamageEXT",
124 },
125 {
126 .extension = "EGL_KHR_swap_buffers_with_damage",
127 .entrypoint = "eglSwapBuffersWithDamageKHR",
128 },
129 };
130
131 static const EGLint context_attribs[] = {
132 EGL_CONTEXT_CLIENT_VERSION, 2,
133 EGL_NONE
134 };
135 const char *extensions;
136
137 EGLint config_attribs[] = {
138 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
139 EGL_RED_SIZE, 1,
140 EGL_GREEN_SIZE, 1,
141 EGL_BLUE_SIZE, 1,
142 EGL_ALPHA_SIZE, 1,
143 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
144 EGL_NONE
145 };
146
147 EGLint major, minor, n, count, i, size;
148 EGLConfig *configs;
149 EGLBoolean ret;
150
151 if (window->opaque || window->buffer_size == 16)
152 config_attribs[9] = 0;
153
154 display->egl.dpy =
155 weston_platform_get_egl_display(EGL_PLATFORM_WAYLAND_KHR,
156 display->display, NULL);
157 assert(display->egl.dpy);
158
159 ret = eglInitialize(display->egl.dpy, &major, &minor);
160 assert(ret == EGL_TRUE);
161 ret = eglBindAPI(EGL_OPENGL_ES_API);
162 assert(ret == EGL_TRUE);
163
164 if (!eglGetConfigs(display->egl.dpy, NULL, 0, &count) || count < 1)
165 assert(0);
166
167 configs = calloc(count, sizeof *configs);
168 assert(configs);
169
170 ret = eglChooseConfig(display->egl.dpy, config_attribs,
171 configs, count, &n);
172 assert(ret && n >= 1);
173
174 for (i = 0; i < n; i++) {
175 eglGetConfigAttrib(display->egl.dpy,
176 configs[i], EGL_BUFFER_SIZE, &size);
177 if (window->buffer_size == size) {
178 display->egl.conf = configs[i];
179 break;
180 }
181 }
182 free(configs);
183 if (display->egl.conf == NULL) {
184 fprintf(stderr, "did not find config with buffer size %d\n",
185 window->buffer_size);
186 exit(EXIT_FAILURE);
187 }
188
189 display->egl.ctx = eglCreateContext(display->egl.dpy,
190 display->egl.conf,
191 EGL_NO_CONTEXT, context_attribs);
192 assert(display->egl.ctx);
193
194 display->swap_buffers_with_damage = NULL;
195 extensions = eglQueryString(display->egl.dpy, EGL_EXTENSIONS);
196 if (extensions &&
197 weston_check_egl_extension(extensions, "EGL_EXT_buffer_age")) {
198 for (i = 0; i < (int) ARRAY_LENGTH(swap_damage_ext_to_entrypoint); i++) {
199 if (weston_check_egl_extension(extensions,
200 swap_damage_ext_to_entrypoint[i].extension)) {
201 /* The EXTPROC is identical to the KHR one */
202 display->swap_buffers_with_damage =
203 (PFNEGLSWAPBUFFERSWITHDAMAGEEXTPROC)
204 eglGetProcAddress(swap_damage_ext_to_entrypoint[i].entrypoint);
205 break;
206 }
207 }
208 }
209
210 if (display->swap_buffers_with_damage)
211 printf("has EGL_EXT_buffer_age and %s\n", swap_damage_ext_to_entrypoint[i].extension);
212
213 }
214
215 static void
fini_egl(struct display * display)216 fini_egl(struct display *display)
217 {
218 eglTerminate(display->egl.dpy);
219 eglReleaseThread();
220 }
221
222 static GLuint
create_shader(struct window * window,const char * source,GLenum shader_type)223 create_shader(struct window *window, const char *source, GLenum shader_type)
224 {
225 GLuint shader;
226 GLint status;
227
228 shader = glCreateShader(shader_type);
229 assert(shader != 0);
230
231 glShaderSource(shader, 1, (const char **) &source, NULL);
232 glCompileShader(shader);
233
234 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
235 if (!status) {
236 char log[1000];
237 GLsizei len;
238 glGetShaderInfoLog(shader, 1000, &len, log);
239 fprintf(stderr, "Error: compiling %s: %.*s\n",
240 shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment",
241 len, log);
242 exit(1);
243 }
244
245 return shader;
246 }
247
248 static void
init_gl(struct window * window)249 init_gl(struct window *window)
250 {
251 GLuint frag, vert;
252 GLuint program;
253 GLint status;
254
255 frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER);
256 vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER);
257
258 program = glCreateProgram();
259 glAttachShader(program, frag);
260 glAttachShader(program, vert);
261 glLinkProgram(program);
262
263 glGetProgramiv(program, GL_LINK_STATUS, &status);
264 if (!status) {
265 char log[1000];
266 GLsizei len;
267 glGetProgramInfoLog(program, 1000, &len, log);
268 fprintf(stderr, "Error: linking:\n%.*s\n", len, log);
269 exit(1);
270 }
271
272 glUseProgram(program);
273
274 window->gl.pos = 0;
275 window->gl.col = 1;
276
277 glBindAttribLocation(program, window->gl.pos, "pos");
278 glBindAttribLocation(program, window->gl.col, "color");
279 glLinkProgram(program);
280
281 window->gl.rotation_uniform =
282 glGetUniformLocation(program, "rotation");
283 }
284
285 static void
create_surface(struct window * window)286 create_surface(struct window *window)
287 {
288 struct display *display = window->display;
289 EGLBoolean ret;
290
291 window->surface = wl_compositor_create_surface(display->compositor);
292 // wms window create.
293 wms_create_window(window->display->wms, window->surface, 0, 0);
294 wl_display_flush(window->display->display);
295 wl_display_roundtrip(window->display->display);
296 wl_display_roundtrip(window->display->display);
297
298 window->native =
299 wl_egl_window_create(window->surface,
300 window->geometry.width,
301 window->geometry.height);
302 window->egl_surface =
303 weston_platform_create_egl_surface(display->egl.dpy,
304 display->egl.conf,
305 window->native, NULL);
306
307 window->wait_for_configure = false;
308 wl_surface_commit(window->surface);
309
310 ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface,
311 window->egl_surface, window->display->egl.ctx);
312 assert(ret == EGL_TRUE);
313
314 if (!window->frame_sync)
315 eglSwapInterval(display->egl.dpy, 0);
316 }
317
318 static void
destroy_surface(struct window * window)319 destroy_surface(struct window *window)
320 {
321 /* Required, otherwise segfault in egl_dri2.c: dri2_make_current()
322 * on eglReleaseThread(). */
323 eglMakeCurrent(window->display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
324 EGL_NO_CONTEXT);
325
326 weston_platform_destroy_egl_surface(window->display->egl.dpy,
327 window->egl_surface);
328 wl_egl_window_destroy(window->native);
329
330 wl_surface_destroy(window->surface);
331
332 if (window->callback)
333 wl_callback_destroy(window->callback);
334 }
335
336 static void
redraw(void * data,struct wl_callback * callback,uint32_t time)337 redraw(void *data, struct wl_callback *callback, uint32_t time)
338 {
339 struct window *window = data;
340 struct display *display = window->display;
341 static const GLfloat verts[3][2] = {
342 { -0.5, -0.5 },
343 { 0.5, -0.5 },
344 { 0, 0.5 }
345 };
346 static const GLfloat colors[3][3] = {
347 { 1, 0, 0 },
348 { 0, 1, 0 },
349 { 0, 0, 1 }
350 };
351 GLfloat angle;
352 GLfloat rotation[4][4] = {
353 { 1, 0, 0, 0 },
354 { 0, 1, 0, 0 },
355 { 0, 0, 1, 0 },
356 { 0, 0, 0, 1 }
357 };
358 static const uint32_t speed_div = 5, benchmark_interval = 5;
359 struct wl_region *region;
360 EGLint rect[4];
361 EGLint buffer_age = 0;
362 struct timeval tv;
363
364 assert(window->callback == callback);
365 window->callback = NULL;
366
367 if (callback)
368 wl_callback_destroy(callback);
369
370 gettimeofday(&tv, NULL);
371 time = tv.tv_sec * 1000 + tv.tv_usec / 1000;
372 if (window->frames == 0)
373 window->benchmark_time = time;
374 if (time - window->benchmark_time > (benchmark_interval * 1000)) {
375 printf("%d frames in %d seconds: %f fps\n",
376 window->frames,
377 benchmark_interval,
378 (float) window->frames / benchmark_interval);
379 window->benchmark_time = time;
380 window->frames = 0;
381 }
382
383 angle = (time / speed_div) % 360 * M_PI / 180.0;
384 rotation[0][0] = cos(angle);
385 rotation[0][2] = sin(angle);
386 rotation[2][0] = -sin(angle);
387 rotation[2][2] = cos(angle);
388
389 if (display->swap_buffers_with_damage)
390 eglQuerySurface(display->egl.dpy, window->egl_surface,
391 EGL_BUFFER_AGE_EXT, &buffer_age);
392
393 glViewport(0, 0, window->geometry.width, window->geometry.height);
394
395 glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE,
396 (GLfloat *) rotation);
397
398 glClearColor(0.0, 0.0, 0.0, 0.5);
399 glClear(GL_COLOR_BUFFER_BIT);
400
401 glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts);
402 glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors);
403 glEnableVertexAttribArray(window->gl.pos);
404 glEnableVertexAttribArray(window->gl.col);
405
406 glDrawArrays(GL_TRIANGLES, 0, 3);
407
408 glDisableVertexAttribArray(window->gl.pos);
409 glDisableVertexAttribArray(window->gl.col);
410
411 usleep(window->delay);
412
413 if (window->opaque || window->fullscreen) {
414 region = wl_compositor_create_region(window->display->compositor);
415 wl_region_add(region, 0, 0,
416 window->geometry.width,
417 window->geometry.height);
418 wl_surface_set_opaque_region(window->surface, region);
419 wl_region_destroy(region);
420 } else {
421 wl_surface_set_opaque_region(window->surface, NULL);
422 }
423
424 if (display->swap_buffers_with_damage && buffer_age > 0) {
425 rect[0] = window->geometry.width / 4 - 1;
426 rect[1] = window->geometry.height / 4 - 1;
427 rect[2] = window->geometry.width / 2 + 2;
428 rect[3] = window->geometry.height / 2 + 2;
429 display->swap_buffers_with_damage(display->egl.dpy,
430 window->egl_surface,
431 rect, 1);
432 } else {
433 fprintf(stderr,"pss %s %d, %s\n",__func__,__LINE__,__FILE__);
434 eglSwapBuffers(display->egl.dpy, window->egl_surface);
435 }
436 window->frames++;
437 }
438
439 /**
440 static void
441 pointer_handle_enter(void *data, struct wl_pointer *pointer,
442 uint32_t serial, struct wl_surface *surface,
443 wl_fixed_t sx, wl_fixed_t sy)
444 {
445 struct display *display = data;
446 struct wl_buffer *buffer;
447
448 }
449
450 static void
451 pointer_handle_leave(void *data, struct wl_pointer *pointer,
452 uint32_t serial, struct wl_surface *surface)
453 {
454 }
455
456 static void
457 pointer_handle_motion(void *data, struct wl_pointer *pointer,
458 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
459 {
460 }
461
462 static void
463 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
464 uint32_t serial, uint32_t time, uint32_t button,
465 uint32_t state)
466 {
467 struct display *display = data;
468 }
469
470 static void
471 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
472 uint32_t time, uint32_t axis, wl_fixed_t value)
473 {
474 }
475
476 static const struct wl_pointer_listener pointer_listener = {
477 pointer_handle_enter,
478 pointer_handle_leave,
479 pointer_handle_motion,
480 pointer_handle_button,
481 pointer_handle_axis,
482 };
483
484 static void
485 touch_handle_down(void *data, struct wl_touch *wl_touch,
486 uint32_t serial, uint32_t time, struct wl_surface *surface,
487 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
488 {
489 struct display *d = (struct display *)data;
490 }
491
492 static void
493 touch_handle_up(void *data, struct wl_touch *wl_touch,
494 uint32_t serial, uint32_t time, int32_t id)
495 {
496 }
497
498 static void
499 touch_handle_motion(void *data, struct wl_touch *wl_touch,
500 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
501 {
502 }
503
504 static void
505 touch_handle_frame(void *data, struct wl_touch *wl_touch)
506 {
507 }
508
509 static void
510 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
511 {
512 }
513
514 static const struct wl_touch_listener touch_listener = {
515 touch_handle_down,
516 touch_handle_up,
517 touch_handle_motion,
518 touch_handle_frame,
519 touch_handle_cancel,
520 };
521
522 static void
523 keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
524 uint32_t format, int fd, uint32_t size)
525 {
526 // Just so we don’t leak the keymap fd
527 close(fd);
528 }
529
530 static void
531 keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
532 uint32_t serial, struct wl_surface *surface,
533 struct wl_array *keys)
534 {
535 }
536
537 static void
538 keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
539 uint32_t serial, struct wl_surface *surface)
540 {
541 }
542
543 static void
544 keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
545 uint32_t serial, uint32_t time, uint32_t key,
546 uint32_t state)
547 {
548 struct display *d = data;
549
550 if (key == KEY_ESC && state)
551 running = 0;
552 }
553
554 static void
555 keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
556 uint32_t serial, uint32_t mods_depressed,
557 uint32_t mods_latched, uint32_t mods_locked,
558 uint32_t group)
559 {
560 }
561
562 static const struct wl_keyboard_listener keyboard_listener = {
563 keyboard_handle_keymap,
564 keyboard_handle_enter,
565 keyboard_handle_leave,
566 keyboard_handle_key,
567 keyboard_handle_modifiers,
568 };
569
570 static void
571 seat_handle_capabilities(void *data, struct wl_seat *seat,
572 enum wl_seat_capability caps)
573 {
574 struct display *d = data;
575
576 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) {
577 d->pointer = wl_seat_get_pointer(seat);
578 wl_pointer_add_listener(d->pointer, &pointer_listener, d);
579 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) {
580 wl_pointer_destroy(d->pointer);
581 d->pointer = NULL;
582 }
583
584 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
585 d->keyboard = wl_seat_get_keyboard(seat);
586 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
587 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
588 wl_keyboard_destroy(d->keyboard);
589 d->keyboard = NULL;
590 }
591
592 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !d->touch) {
593 d->touch = wl_seat_get_touch(seat);
594 wl_touch_set_user_data(d->touch, d);
595 wl_touch_add_listener(d->touch, &touch_listener, d);
596 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && d->touch) {
597 wl_touch_destroy(d->touch);
598 d->touch = NULL;
599 }
600 }
601
602 static const struct wl_seat_listener seat_listener = {
603 seat_handle_capabilities,
604 };
605 */
606
WindowShotError(void * data,struct wms * wms,uint32_t error,uint32_t window_id)607 static void WindowShotError(void *data, struct wms *wms, uint32_t error, uint32_t window_id)
608 {
609 LOG("WindowShotError error: %d", error);
610 LOG("WindowShotError window_id: %d", window_id);
611 }
612
WindowShotDone(void * data,struct wms * wms,uint32_t window_id,int32_t fd,int32_t width,int32_t height,int32_t stride,uint32_t format,uint32_t seconds,uint32_t nanoseconds)613 static void WindowShotDone(void *data,
614 struct wms *wms,
615 uint32_t window_id, int32_t fd,
616 int32_t width, int32_t height, int32_t stride,
617 uint32_t format, uint32_t seconds, uint32_t nanoseconds)
618 {
619 LOG("WindowShotDone window_id: %d", window_id);
620 LOG("WindowShotDone fd: %d", fd);
621 LOG("WindowShotDone width: %d", width);
622 LOG("WindowShotDone height: %d", height);
623 LOG("WindowShotDone stride: %d", stride);
624 LOG("WindowShotDone format: %d", format);
625 LOG("WindowShotDone seconds: %d", seconds);
626 LOG("WindowShotDone nanoseconds: %d", nanoseconds);
627
628 LOG("windowshot OK!!!");
629
630 close(fd);
631 }
632
ScreenShotError(void * data,struct wms * wms,uint32_t error,uint32_t screen_id)633 static void ScreenShotError(void *data, struct wms *wms, uint32_t error, uint32_t screen_id)
634 {
635 LOG("ScreenShotError error: %d", error);
636 LOG("ScreenShotError screen_id: %d", screen_id);
637 }
638
ScreenShotDone(void * data,struct wms * wms,uint32_t screen_id,int32_t fd,int32_t width,int32_t height,int32_t stride,uint32_t format,uint32_t seconds,uint32_t nanoseconds)639 static void ScreenShotDone(void *data,
640 struct wms *wms,
641 uint32_t screen_id, int32_t fd,
642 int32_t width, int32_t height, int32_t stride,
643 uint32_t format, uint32_t seconds, uint32_t nanoseconds)
644 {
645 LOG("ScreenShotDone screen_id: %d", screen_id);
646 LOG("ScreenShotDone fd: %d", fd);
647 LOG("ScreenShotDone width: %d", width);
648 LOG("ScreenShotDone height: %d", height);
649 LOG("ScreenShotDone stride: %d", stride);
650 LOG("ScreenShotDone format: %d", format);
651 LOG("ScreenShotDone seconds: %d", seconds);
652 LOG("ScreenShotDone nanoseconds: %d", nanoseconds);
653
654 LOG("screenshot OK!!!");
655 close(fd);
656 }
657
ReplyStatus(void * data,struct wms * wms,uint32_t status)658 static void ReplyStatus(void *data, struct wms *wms, uint32_t status)
659 {
660 LOG("ReplyStatus status: %d", status);
661 }
662
DisplayMode(void * data,struct wms * wms,uint32_t flag)663 static void DisplayMode(void *data, struct wms *wms, uint32_t flag)
664 {
665 LOG("DisplayMode flag: %d", flag);
666 }
667
ScreenUpdate(void * data,struct wms * wms,uint32_t screen_id,const char * name,uint32_t update_state,int width,int height)668 void ScreenUpdate(void *data,
669 struct wms *wms,
670 uint32_t screen_id,
671 const char *name,
672 uint32_t update_state,
673 int width, int height)
674 {
675 LOG("screenUpdate screen_id: %d", screen_id);
676 LOG("screenUpdate name: %s", name);
677 LOG("screenUpdate update_state: %d", update_state);
678 LOG("screenUpdate width: %d", width);
679 LOG("screenUpdate height: %d", height);
680
681 if (update_state == WMS_SCREEN_STATUS_ADD) {
682 LOG("screen add. ");
683 }
684 else {
685 LOG("screen destroy.");
686 }
687 }
688
WindowUpdate(void * data,struct wms * wms,uint32_t update_state,uint32_t window_id,int32_t x,int32_t y,int32_t width,int32_t height)689 void WindowUpdate(void *data, struct wms *wms, uint32_t update_state, uint32_t window_id,
690 int32_t x, int32_t y, int32_t width, int32_t height)
691 {
692 struct display *d = data;
693 LOG("WindowUpdate window_id: %d", window_id);
694 LOG("WindowUpdate update_state: %d", update_state);
695 LOG("WindowUpdate x:%d, y:%d", x, y);
696 LOG("WindowUpdate width:%d, height:%d", width, height);
697
698 if (update_state == WMS_WINDOW_STATUS_CREATED) {
699 LOG("window %d create. ", window_id);
700 d->window->geometry.width = width;
701 d->window->geometry.height = height;
702 }
703 else if (update_state == WMS_WINDOW_STATUS_FAILED) {
704 LOG("window create failed. ");
705 } else {
706 LOG("window %d destroy. ", window_id);
707 }
708 }
709
710
711 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)712 registry_handle_global(void *data, struct wl_registry *registry,
713 uint32_t name, const char *interface, uint32_t version)
714 {
715 struct display *d = data;
716
717 if (strcmp(interface, "wl_compositor") == 0) {
718 d->compositor =
719 wl_registry_bind(registry, name,
720 &wl_compositor_interface,
721 MIN(version, 4));
722 } /**else if (strcmp(interface, "wl_seat") == 0) {
723 d->seat = wl_registry_bind(registry, name,
724 &wl_seat_interface, 1);
725 wl_seat_add_listener(d->seat, &seat_listener, d);
726 } else if (strcmp(interface, "wl_shm") == 0) {
727 d->shm = wl_registry_bind(registry, name,
728 &wl_shm_interface, 1);
729 } */else if (strcmp(interface, "wms") == 0) {
730 d->wms = (struct wms *)wl_registry_bind(registry, name, &wms_interface, 1);
731
732 static struct wms_listener wmsListener = {
733 WindowUpdate,
734 ScreenUpdate,
735 DisplayMode,
736 ReplyStatus,
737 ScreenShotDone,
738 ScreenShotError,
739 WindowShotDone,
740 WindowShotError
741 };
742 wms_add_listener(d->wms, &wmsListener, d);
743 wl_display_flush(d->display);
744 wl_display_roundtrip(d->display);
745 }
746 }
747
748 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)749 registry_handle_global_remove(void *data, struct wl_registry *registry,
750 uint32_t name)
751 {
752 }
753
754 static const struct wl_registry_listener registry_listener = {
755 registry_handle_global,
756 registry_handle_global_remove
757 };
758
759 static void
signal_int(int signum)760 signal_int(int signum)
761 {
762 running = 0;
763 }
764
765 static void
usage(int error_code)766 usage(int error_code)
767 {
768 fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n"
769 " -d <us>\tBuffer swap delay in microseconds\n"
770 " -f\tRun in fullscreen mode\n"
771 " -o\tCreate an opaque surface\n"
772 " -s\tUse a 16 bpp EGL config\n"
773 " -b\tDon't sync to compositor redraw (eglSwapInterval 0)\n"
774 " -h\tThis help text\n\n");
775
776 exit(error_code);
777 }
778
779 int
main(int argc,char ** argv)780 main(int argc, char **argv)
781 {
782 struct sigaction sigint;
783 struct display display = { 0 };
784 struct window window = { 0 };
785 int i, ret = 0;
786
787 window.display = &display;
788 display.window = &window;
789 window.geometry.width = 250;
790 window.geometry.height = 250;
791 window.window_size = window.geometry;
792 window.buffer_size = 32;
793 window.frame_sync = 1;
794 window.delay = 0;
795
796 for (i = 1; i < argc; i++) {
797 if (strcmp("-d", argv[i]) == 0 && i+1 < argc)
798 window.delay = atoi(argv[++i]);
799 else if (strcmp("-f", argv[i]) == 0)
800 window.fullscreen = 1;
801 else if (strcmp("-o", argv[i]) == 0)
802 window.opaque = 1;
803 else if (strcmp("-s", argv[i]) == 0)
804 window.buffer_size = 16;
805 else if (strcmp("-b", argv[i]) == 0)
806 window.frame_sync = 0;
807 else if (strcmp("-h", argv[i]) == 0)
808 usage(EXIT_SUCCESS);
809 else
810 usage(EXIT_FAILURE);
811 }
812
813 display.display = wl_display_connect(NULL);
814 assert(display.display);
815
816 display.registry = wl_display_get_registry(display.display);
817 wl_registry_add_listener(display.registry,
818 ®istry_listener, &display);
819
820 wl_display_roundtrip(display.display);
821 init_egl(&display, &window);
822 create_surface(&window);
823 init_gl(&window);
824
825 sigint.sa_handler = signal_int;
826 sigemptyset(&sigint.sa_mask);
827 sigint.sa_flags = SA_RESETHAND;
828 sigaction(SIGINT, &sigint, NULL);
829
830 /* The mainloop here is a little subtle. Redrawing will cause
831 * EGL to read events so we can just call
832 * wl_display_dispatch_pending() to handle any events that got
833 * queued up as a side effect. */
834 while (running && ret != -1) {
835 if (window.wait_for_configure) {
836 ret = wl_display_dispatch(display.display);
837 } else {
838 ret = wl_display_dispatch_pending(display.display);
839 redraw(&window, NULL, 0);
840 }
841 }
842
843 fprintf(stderr, "simple-egl exiting\n");
844
845 destroy_surface(&window);
846 fini_egl(&display);
847
848 if (display.compositor)
849 wl_compositor_destroy(display.compositor);
850
851 wl_registry_destroy(display.registry);
852 wl_display_flush(display.display);
853 wl_display_disconnect(display.display);
854
855 return 0;
856 }
857