2 * Copyright © 2014 Jason Ekstrand
3 * Copyright © 2011 Benjamin Franzke
4 * Copyright © 2010 Intel Corporation
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 #include <wayland-client.h>
38 #include "../shared/os-compatibility.h"
39 #include "xdg-shell-client-protocol.h"
40 #include "fullscreen-shell-client-protocol.h"
41 #include "scaler-client-protocol.h"
46 struct wl_display *display;
47 struct wl_registry *registry;
48 int compositor_version;
49 struct wl_compositor *compositor;
50 struct wl_scaler *scaler;
51 struct xdg_shell *shell;
52 struct _wl_fullscreen_shell *fshell;
58 struct wl_buffer *buffer;
64 WINDOW_FLAG_USE_VIEWPORT = 0x1,
65 WINDOW_FLAG_ROTATING_TRANSFORM = 0x2,
69 struct display *display;
70 int width, height, border;
71 struct wl_surface *surface;
72 struct wl_viewport *viewport;
73 struct xdg_surface *xdg_surface;
74 struct wl_callback *callback;
75 struct buffer buffers[2];
76 struct buffer *prev_buffer;
78 enum window_flags flags;
80 enum wl_output_transform transform;
83 float x, y; /* position in pixels */
84 float dx, dy; /* velocity in pixels/second */
85 int radius; /* radius in pixels */
90 static int running = 1;
93 buffer_release(void *data, struct wl_buffer *buffer)
95 struct buffer *mybuf = data;
100 static const struct wl_buffer_listener buffer_listener = {
105 create_shm_buffer(struct display *display, struct buffer *buffer,
106 int width, int height, uint32_t format)
108 struct wl_shm_pool *pool;
113 size = pitch * height;
115 fd = os_create_anonymous_file(size);
117 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
122 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
123 if (data == MAP_FAILED) {
124 fprintf(stderr, "mmap failed: %m\n");
129 pool = wl_shm_create_pool(display->shm, fd, size);
130 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
133 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
134 wl_shm_pool_destroy(pool);
137 buffer->shm_data = data;
143 handle_configure(void *data, struct xdg_surface *surface,
144 int32_t width, int32_t height, struct wl_array *states,
150 handle_close(void *data, struct xdg_surface *xdg_surface)
155 static const struct xdg_surface_listener xdg_surface_listener = {
161 bounded_randf(float a, float b)
163 return a + ((float)rand() / (float)RAND_MAX) * (b - a);
167 window_init_game(struct window *window)
169 int ax1, ay1, ax2, ay2; /* playable arena size */
172 gettimeofday(&tv, NULL);
175 window->ball.radius = 10;
177 ax1 = window->border + window->ball.radius;
178 ay1 = window->border + window->ball.radius;
179 ax2 = window->width - window->border - window->ball.radius;
180 ay2 = window->height - window->border - window->ball.radius;
182 window->ball.x = bounded_randf(ax1, ax2);
183 window->ball.y = bounded_randf(ay1, ay2);
185 window->ball.dx = bounded_randf(0, window->width);
186 window->ball.dy = bounded_randf(0, window->height);
188 window->ball.prev_time = 0;
192 window_advance_game(struct window *window, uint32_t timestamp)
194 int ax1, ay1, ax2, ay2; /* Arena size */
197 if (window->ball.prev_time == 0) {
198 /* first pass, don't do anything */
199 window->ball.prev_time = timestamp;
204 dt = (float)(timestamp - window->ball.prev_time) / 1000.0f;
206 ax1 = window->border + window->ball.radius;
207 ay1 = window->border + window->ball.radius;
208 ax2 = window->width - window->border - window->ball.radius;
209 ay2 = window->height - window->border - window->ball.radius;
211 window->ball.x += window->ball.dx * dt;
212 while (window->ball.x < ax1 || ax2 < window->ball.x) {
213 if (window->ball.x < ax1)
214 window->ball.x = 2 * ax1 - window->ball.x;
215 if (ax2 <= window->ball.x)
216 window->ball.x = 2 * ax2 - window->ball.x;
218 window->ball.dx *= -1.0f;
221 window->ball.y += window->ball.dy * dt;
222 while (window->ball.y < ay1 || ay2 < window->ball.y) {
223 if (window->ball.y < ay1)
224 window->ball.y = 2 * ay1 - window->ball.y;
225 if (ay2 <= window->ball.y)
226 window->ball.y = 2 * ay2 - window->ball.y;
228 window->ball.dy *= -1.0f;
231 window->ball.prev_time = timestamp;
234 static struct window *
235 create_window(struct display *display, int width, int height,
236 enum wl_output_transform transform, int scale,
237 enum window_flags flags)
239 struct window *window;
241 if (display->compositor_version < 2 &&
242 (transform != WL_OUTPUT_TRANSFORM_NORMAL ||
243 flags & WINDOW_FLAG_ROTATING_TRANSFORM)) {
244 fprintf(stderr, "wl_surface.buffer_transform unsupported in "
245 "wl_surface version %d\n",
246 display->compositor_version);
250 if (display->compositor_version < 3 &&
251 (! (flags & WINDOW_FLAG_USE_VIEWPORT)) && scale != 1) {
252 fprintf(stderr, "wl_surface.buffer_scale unsupported in "
253 "wl_surface version %d\n",
254 display->compositor_version);
258 if (display->scaler == NULL && (flags & WINDOW_FLAG_USE_VIEWPORT)) {
259 fprintf(stderr, "Compositor does not support wl_viewport");
263 window = calloc(1, sizeof *window);
267 window->callback = NULL;
268 window->display = display;
269 window->width = width;
270 window->height = height;
272 window->flags = flags;
273 window->transform = transform;
274 window->scale = scale;
276 window_init_game(window);
278 window->surface = wl_compositor_create_surface(display->compositor);
280 if (window->flags & WINDOW_FLAG_USE_VIEWPORT)
281 window->viewport = wl_scaler_get_viewport(display->scaler,
284 if (display->shell) {
285 window->xdg_surface =
286 xdg_shell_get_xdg_surface(display->shell,
289 assert(window->xdg_surface);
291 xdg_surface_add_listener(window->xdg_surface,
292 &xdg_surface_listener, window);
294 xdg_surface_set_title(window->xdg_surface, "simple-damage");
295 } else if (display->fshell) {
296 _wl_fullscreen_shell_present_surface(display->fshell,
298 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,
304 /* Initialise damage to full surface, so the padding gets painted */
305 wl_surface_damage(window->surface, 0, 0, INT32_MAX, INT32_MAX);
311 destroy_window(struct window *window)
313 if (window->callback)
314 wl_callback_destroy(window->callback);
316 if (window->buffers[0].buffer)
317 wl_buffer_destroy(window->buffers[0].buffer);
318 if (window->buffers[1].buffer)
319 wl_buffer_destroy(window->buffers[1].buffer);
321 if (window->xdg_surface)
322 xdg_surface_destroy(window->xdg_surface);
323 if (window->viewport)
324 wl_viewport_destroy(window->viewport);
325 wl_surface_destroy(window->surface);
329 static struct buffer *
330 window_next_buffer(struct window *window)
332 struct buffer *buffer;
333 int ret = 0, bwidth, bheight;
335 if (!window->buffers[0].busy)
336 buffer = &window->buffers[0];
337 else if (!window->buffers[1].busy)
338 buffer = &window->buffers[1];
342 switch (window->transform) {
344 case WL_OUTPUT_TRANSFORM_NORMAL:
345 case WL_OUTPUT_TRANSFORM_180:
346 case WL_OUTPUT_TRANSFORM_FLIPPED:
347 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
348 bwidth = window->width * window->scale;
349 bheight = window->height * window->scale;
351 case WL_OUTPUT_TRANSFORM_90:
352 case WL_OUTPUT_TRANSFORM_270:
353 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
354 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
355 bwidth = window->height * window->scale;
356 bheight = window->width * window->scale;
360 if (!buffer->buffer) {
361 ret = create_shm_buffer(window->display, buffer,
363 WL_SHM_FORMAT_ARGB8888);
373 paint_box(uint32_t *pixels, int pitch, int x, int y, int width, int height,
378 for (j = y; j < y + height; ++j)
379 for (i = x; i < x + width; ++i)
380 pixels[i + j * pitch] = color;
384 paint_circle(uint32_t *pixels, int pitch, float x, float y, int radius,
389 for (j = y - radius; j <= (int)(y + radius); ++j)
390 for (i = x - radius; i <= (int)(x + radius); ++i)
391 if ((j+0.5f-y)*(j+0.5f-y) + (i+0.5f-x)*(i+0.5f-x) <= radius * radius)
392 pixels[i + j * pitch] = color;
396 window_get_transformed_ball(struct window *window, float *bx, float *by)
403 switch (window->transform) {
405 case WL_OUTPUT_TRANSFORM_NORMAL:
409 case WL_OUTPUT_TRANSFORM_90:
410 *bx = window->height - wy;
413 case WL_OUTPUT_TRANSFORM_180:
414 *bx = window->width - wx;
415 *by = window->height - wy;
417 case WL_OUTPUT_TRANSFORM_270:
419 *by = window->width - wx;
421 case WL_OUTPUT_TRANSFORM_FLIPPED:
422 *bx = window->width - wx;
425 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
426 *bx = window->height - wy;
427 *by = window->width - wx;
429 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
431 *by = window->height - wy;
433 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
439 *bx *= window->scale;
440 *by *= window->scale;
442 if (window->viewport) {
443 /* We're drawing half-size because of the viewport */
449 static const struct wl_callback_listener frame_listener;
452 redraw(void *data, struct wl_callback *callback, uint32_t time)
454 struct window *window = data;
455 struct buffer *buffer;
456 int off_x, off_y, bwidth, bheight, bborder, bpitch, bradius;
457 uint32_t *buffer_data;
460 buffer = window_next_buffer(window);
463 !callback ? "Failed to create the first buffer.\n" :
464 "Both buffers busy at redraw(). Server bug?\n");
468 /* Rotate the damage, but keep the even/odd parity so the
469 * dimensions of the buffers don't change */
470 if (window->flags & WINDOW_FLAG_ROTATING_TRANSFORM)
471 window->transform = (window->transform + 2) % 8;
473 switch (window->transform) {
475 case WL_OUTPUT_TRANSFORM_NORMAL:
476 case WL_OUTPUT_TRANSFORM_180:
477 case WL_OUTPUT_TRANSFORM_FLIPPED:
478 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
479 bwidth = window->width * window->scale;
480 bheight = window->height * window->scale;
482 case WL_OUTPUT_TRANSFORM_90:
483 case WL_OUTPUT_TRANSFORM_270:
484 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
485 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
486 bwidth = window->height * window->scale;
487 bheight = window->width * window->scale;
493 bborder = window->border * window->scale;
494 bradius = window->ball.radius * window->scale;
496 buffer_data = buffer->shm_data;
497 if (window->viewport) {
498 /* Fill the whole thing with red to detect viewport errors */
499 paint_box(buffer->shm_data, bpitch, 0, 0, bwidth, bheight,
502 /* The buffer is the same size. However, we crop it
503 * and scale it up by a factor of 2 */
509 /* Offset the drawing region */
510 off_x = (window->width / 3) * window->scale;
511 off_y = (window->height / 5) * window->scale;
512 switch (window->transform) {
514 case WL_OUTPUT_TRANSFORM_NORMAL:
515 buffer_data += off_y * bpitch + off_x;
517 case WL_OUTPUT_TRANSFORM_90:
518 buffer_data += off_x * bpitch + (bwidth - off_y);
520 case WL_OUTPUT_TRANSFORM_180:
521 buffer_data += (bheight - off_y) * bpitch +
524 case WL_OUTPUT_TRANSFORM_270:
525 buffer_data += (bheight - off_x) * bpitch + off_y;
527 case WL_OUTPUT_TRANSFORM_FLIPPED:
528 buffer_data += off_y * bpitch + (bwidth - off_x);
530 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
531 buffer_data += (bheight - off_x) * bpitch +
534 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
535 buffer_data += (bheight - off_y) * bpitch + off_x;
537 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
538 buffer_data += off_x * bpitch + off_y;
541 wl_viewport_set_source(window->viewport,
542 wl_fixed_from_int(window->width / 3),
543 wl_fixed_from_int(window->height / 5),
544 wl_fixed_from_int(window->width / 2),
545 wl_fixed_from_int(window->height / 2));
548 /* Paint the border */
549 paint_box(buffer_data, bpitch, 0, 0, bwidth, bborder, 0xffffffff);
550 paint_box(buffer_data, bpitch, 0, 0, bborder, bheight, 0xffffffff);
551 paint_box(buffer_data, bpitch,
552 bwidth - bborder, 0, bborder, bheight, 0xffffffff);
553 paint_box(buffer_data, bpitch,
554 0, bheight - bborder, bwidth, bborder, 0xffffffff);
556 /* fill with translucent */
557 paint_box(buffer_data, bpitch, bborder, bborder,
558 bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000);
560 /* Damage where the ball was */
561 wl_surface_damage(window->surface,
562 window->ball.x - window->ball.radius,
563 window->ball.y - window->ball.radius,
564 window->ball.radius * 2 + 1,
565 window->ball.radius * 2 + 1);
567 window_advance_game(window, time);
569 window_get_transformed_ball(window, &bx, &by);
572 paint_circle(buffer_data, bpitch, bx, by, bradius, 0xff00ff00);
575 printf("Ball now located at (%f, %f)\n",
576 window->ball.x, window->ball.y);
578 printf("Circle painted at (%f, %f), radius %d\n", bx, by,
581 printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n",
582 (int)(bx - bradius), (int)(by - bradius),
583 bradius * 2 + 1, bradius * 2 + 1);
586 /* Damage where the ball is now */
587 wl_surface_damage(window->surface,
588 window->ball.x - window->ball.radius,
589 window->ball.y - window->ball.radius,
590 window->ball.radius * 2 + 1,
591 window->ball.radius * 2 + 1);
593 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
595 if (window->display->compositor_version >= 2 &&
596 (window->transform != WL_OUTPUT_TRANSFORM_NORMAL ||
597 window->flags & WINDOW_FLAG_ROTATING_TRANSFORM))
598 wl_surface_set_buffer_transform(window->surface,
601 if (window->viewport)
602 wl_viewport_set_destination(window->viewport,
606 if (window->scale != 1)
607 wl_surface_set_buffer_scale(window->surface,
611 wl_callback_destroy(callback);
613 window->callback = wl_surface_frame(window->surface);
614 wl_callback_add_listener(window->callback, &frame_listener, window);
615 wl_surface_commit(window->surface);
619 static const struct wl_callback_listener frame_listener = {
624 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
626 struct display *d = data;
628 d->formats |= (1 << format);
631 struct wl_shm_listener shm_listener = {
636 xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
638 xdg_shell_pong(shell, serial);
641 static const struct xdg_shell_listener xdg_shell_listener = {
645 #define XDG_VERSION 4 /* The version of xdg-shell that we implement */
647 static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
648 "Interface version doesn't match implementation version");
652 registry_handle_global(void *data, struct wl_registry *registry,
653 uint32_t id, const char *interface, uint32_t version)
655 struct display *d = data;
657 if (strcmp(interface, "wl_compositor") == 0) {
658 if (d->compositor_version > (int)version) {
659 fprintf(stderr, "Compositor does not support "
660 "wl_surface version %d\n", d->compositor_version);
664 if (d->compositor_version < 0)
665 d->compositor_version = version;
668 wl_registry_bind(registry,
669 id, &wl_compositor_interface,
670 d->compositor_version);
671 } else if (strcmp(interface, "wl_scaler") == 0 && version >= 2) {
672 d->scaler = wl_registry_bind(registry,
673 id, &wl_scaler_interface, 2);
674 } else if (strcmp(interface, "xdg_shell") == 0) {
675 d->shell = wl_registry_bind(registry,
676 id, &xdg_shell_interface, 1);
677 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
678 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
679 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
680 d->fshell = wl_registry_bind(registry,
681 id, &_wl_fullscreen_shell_interface, 1);
682 } else if (strcmp(interface, "wl_shm") == 0) {
683 d->shm = wl_registry_bind(registry,
684 id, &wl_shm_interface, 1);
685 wl_shm_add_listener(d->shm, &shm_listener, d);
690 registry_handle_global_remove(void *data, struct wl_registry *registry,
695 static const struct wl_registry_listener registry_listener = {
696 registry_handle_global,
697 registry_handle_global_remove
700 static struct display *
701 create_display(int version)
703 struct display *display;
705 display = malloc(sizeof *display);
706 if (display == NULL) {
707 fprintf(stderr, "out of memory\n");
710 display->display = wl_display_connect(NULL);
711 assert(display->display);
713 display->compositor_version = version;
714 display->formats = 0;
715 display->registry = wl_display_get_registry(display->display);
716 wl_registry_add_listener(display->registry,
717 ®istry_listener, display);
718 wl_display_roundtrip(display->display);
719 if (display->shm == NULL) {
720 fprintf(stderr, "No wl_shm global\n");
724 wl_display_roundtrip(display->display);
726 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
727 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
735 destroy_display(struct display *display)
738 wl_shm_destroy(display->shm);
741 xdg_shell_destroy(display->shell);
744 _wl_fullscreen_shell_release(display->fshell);
747 wl_scaler_destroy(display->scaler);
749 if (display->compositor)
750 wl_compositor_destroy(display->compositor);
752 wl_registry_destroy(display->registry);
753 wl_display_flush(display->display);
754 wl_display_disconnect(display->display);
759 signal_int(int signum)
765 print_usage(int retval)
768 "usage: weston-simple-damage [options]\n\n"
770 " -h, --help\t\tPring this help\n"
771 " --verbose\t\tPrint verbose log information\n"
772 " --version=VERSION\tVersion of wl_surface to use\n"
773 " --width=WIDTH\t\tWidth of the window\n"
774 " --height=HEIGHT\tHeight of the window\n"
775 " --scale=SCALE\t\tScale factor for the surface\n"
776 " --transform=TRANSFORM\tTransform for the surface\n"
777 " --rotating-transform\tUse a different buffer_transform for each frame\n"
778 " --use-viewport\tUse wl_viewport\n"
785 parse_transform(const char *str, enum wl_output_transform *transform)
788 static const struct {
790 enum wl_output_transform transform;
792 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
793 { "90", WL_OUTPUT_TRANSFORM_90 },
794 { "180", WL_OUTPUT_TRANSFORM_180 },
795 { "270", WL_OUTPUT_TRANSFORM_270 },
796 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
797 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
798 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
799 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
802 for (i = 0; i < 8; i++) {
803 if (strcmp(names[i].name, str) == 0) {
804 *transform = names[i].transform;
813 main(int argc, char **argv)
815 struct sigaction sigint;
816 struct display *display;
817 struct window *window;
820 int width = 300, height = 200, scale = 1;
821 enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
822 enum window_flags flags = 0;
824 for (i = 1; i < argc; ++i) {
825 if (strcmp(argv[i], "--help") == 0 ||
826 strcmp(argv[i], "-h") == 0) {
828 } else if (sscanf(argv[i], "--version=%d", &version) > 0) {
829 if (version < 1 || version > 3) {
830 fprintf(stderr, "Unsupported wl_surface version: %d\n",
835 } else if (strcmp(argv[i], "--verbose") == 0) {
838 } else if (sscanf(argv[i], "--width=%d", &width) > 0) {
840 } else if (sscanf(argv[i], "--height=%d", &height) > 0) {
842 } else if (strncmp(argv[i], "--transform=", 12) == 0 &&
843 parse_transform(argv[i] + 12, &transform) > 0) {
845 } else if (strcmp(argv[i], "--rotating-transform") == 0) {
846 flags |= WINDOW_FLAG_ROTATING_TRANSFORM;
848 } else if (sscanf(argv[i], "--scale=%d", &scale) > 0) {
850 } else if (strcmp(argv[i], "--use-viewport") == 0) {
851 flags |= WINDOW_FLAG_USE_VIEWPORT;
854 printf("Invalid option: %s\n", argv[i]);
859 display = create_display(version);
861 window = create_window(display, width, height, transform, scale, flags);
865 sigint.sa_handler = signal_int;
866 sigemptyset(&sigint.sa_mask);
867 sigint.sa_flags = SA_RESETHAND;
868 sigaction(SIGINT, &sigint, NULL);
870 redraw(window, NULL, 0);
872 while (running && ret != -1)
873 ret = wl_display_dispatch(display->display);
875 fprintf(stderr, "simple-shm exiting\n");
876 destroy_window(window);
877 destroy_display(display);