1 /*
2  * Copyright © 2014 Jason Ekstrand
3  * Copyright © 2011 Benjamin Franzke
4  * Copyright © 2010 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <assert.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <errno.h>
39 
40 #include <wayland-client.h>
41 #include "shared/os-compatibility.h"
42 #include <libweston/zalloc.h>
43 #include "xdg-shell-client-protocol.h"
44 #include "fullscreen-shell-unstable-v1-client-protocol.h"
45 #include "viewporter-client-protocol.h"
46 
47 int print_debug = 0;
48 
49 struct display {
50 	struct wl_display *display;
51 	struct wl_registry *registry;
52 	int compositor_version;
53 	struct wl_compositor *compositor;
54 	struct wp_viewporter *viewporter;
55 	struct xdg_wm_base *wm_base;
56 	struct zwp_fullscreen_shell_v1 *fshell;
57 	struct wl_shm *shm;
58 	uint32_t formats;
59 };
60 
61 struct buffer {
62 	struct wl_buffer *buffer;
63 	uint32_t *shm_data;
64 	int busy;
65 };
66 
67 enum window_flags {
68 	WINDOW_FLAG_USE_VIEWPORT = 0x1,
69 	WINDOW_FLAG_ROTATING_TRANSFORM = 0x2,
70 	WINDOW_FLAG_USE_DAMAGE_BUFFER = 0x4,
71 };
72 
73 struct window {
74 	struct display *display;
75 	int width, height, border;
76 	struct wl_surface *surface;
77 	struct wp_viewport *viewport;
78 	struct xdg_surface *xdg_surface;
79 	struct xdg_toplevel *xdg_toplevel;
80 	struct wl_callback *callback;
81 	struct buffer buffers[2];
82 	struct buffer *prev_buffer;
83 	bool wait_for_configure;
84 
85 	enum window_flags flags;
86 	int scale;
87 	enum wl_output_transform transform;
88 
89 	struct {
90 		float x, y; /* position in pixels */
91 		float dx, dy; /* velocity in pixels/second */
92 		int radius; /* radius in pixels */
93 		uint32_t prev_time;
94 	} ball;
95 };
96 
97 static int running = 1;
98 
99 static void
100 redraw(void *data, struct wl_callback *callback, uint32_t time);
101 
102 static void
buffer_release(void * data,struct wl_buffer * buffer)103 buffer_release(void *data, struct wl_buffer *buffer)
104 {
105 	struct buffer *mybuf = data;
106 
107 	mybuf->busy = 0;
108 }
109 
110 static const struct wl_buffer_listener buffer_listener = {
111 	buffer_release
112 };
113 
114 static int
create_shm_buffer(struct display * display,struct buffer * buffer,int width,int height,uint32_t format)115 create_shm_buffer(struct display *display, struct buffer *buffer,
116 		  int width, int height, uint32_t format)
117 {
118 	struct wl_shm_pool *pool;
119 	int fd, size, pitch;
120 	void *data;
121 
122 	pitch = width * 4;
123 	size = pitch * height;
124 
125 	fd = os_create_anonymous_file(size);
126 	if (fd < 0) {
127 		fprintf(stderr, "creating a buffer file for %d B failed: %s\n",
128 			size, strerror(errno));
129 		return -1;
130 	}
131 
132 	data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
133 	if (data == MAP_FAILED) {
134 		fprintf(stderr, "mmap failed: %s\n", strerror(errno));
135 		close(fd);
136 		return -1;
137 	}
138 
139 	pool = wl_shm_create_pool(display->shm, fd, size);
140 	buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
141 						   width, height,
142 						   pitch, format);
143 	wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
144 	wl_shm_pool_destroy(pool);
145 	close(fd);
146 
147 	buffer->shm_data = data;
148 
149 	return 0;
150 }
151 
152 static void
xdg_surface_handle_configure(void * data,struct xdg_surface * surface,uint32_t serial)153 xdg_surface_handle_configure(void *data, struct xdg_surface *surface,
154 			     uint32_t serial)
155 {
156 	struct window *window = data;
157 
158 	xdg_surface_ack_configure(surface, serial);
159 
160 	if (window->wait_for_configure) {
161 		redraw(window, NULL, 0);
162 		window->wait_for_configure = false;
163 	}
164 }
165 
166 static const struct xdg_surface_listener xdg_surface_listener = {
167 	xdg_surface_handle_configure,
168 };
169 
170 static void
xdg_toplevel_handle_configure(void * data,struct xdg_toplevel * toplevel,int32_t width,int32_t height,struct wl_array * states)171 xdg_toplevel_handle_configure(void *data, struct xdg_toplevel *toplevel,
172 			      int32_t width, int32_t height,
173 			      struct wl_array *states)
174 {
175 }
176 
177 static void
xdg_toplevel_handle_close(void * data,struct xdg_toplevel * xdg_toplevel)178 xdg_toplevel_handle_close(void *data, struct xdg_toplevel *xdg_toplevel)
179 {
180 	running = 0;
181 }
182 
183 static const struct xdg_toplevel_listener xdg_toplevel_listener = {
184 	xdg_toplevel_handle_configure,
185 	xdg_toplevel_handle_close,
186 };
187 
188 static float
bounded_randf(float a,float b)189 bounded_randf(float a, float b)
190 {
191 	return a + ((float)rand() / (float)RAND_MAX) * (b - a);
192 }
193 
194 static void
window_init_game(struct window * window)195 window_init_game(struct window *window)
196 {
197 	int ax1, ay1, ax2, ay2; /* playable arena size */
198 	struct timeval tv;
199 
200 	gettimeofday(&tv, NULL);
201 	srand(tv.tv_usec);
202 
203 	window->ball.radius = 10;
204 
205 	ax1 = window->border + window->ball.radius;
206 	ay1 = window->border + window->ball.radius;
207 	ax2 = window->width - window->border - window->ball.radius;
208 	ay2 = window->height - window->border - window->ball.radius;
209 
210 	window->ball.x = bounded_randf(ax1, ax2);
211 	window->ball.y = bounded_randf(ay1, ay2);
212 
213 	window->ball.dx = bounded_randf(0, window->width);
214 	window->ball.dy = bounded_randf(0, window->height);
215 
216 	window->ball.prev_time = 0;
217 }
218 
219 static void
window_advance_game(struct window * window,uint32_t timestamp)220 window_advance_game(struct window *window, uint32_t timestamp)
221 {
222 	int ax1, ay1, ax2, ay2; /* Arena size */
223 	float dt;
224 
225 	if (window->ball.prev_time == 0) {
226 		/* first pass, don't do anything */
227 		window->ball.prev_time = timestamp;
228 		return;
229 	}
230 
231 	/* dt in seconds */
232 	dt = (float)(timestamp - window->ball.prev_time) / 1000.0f;
233 
234 	ax1 = window->border + window->ball.radius;
235 	ay1 = window->border + window->ball.radius;
236 	ax2 = window->width - window->border - window->ball.radius;
237 	ay2 = window->height - window->border - window->ball.radius;
238 
239 	window->ball.x += window->ball.dx * dt;
240 	while (window->ball.x < ax1 || ax2 < window->ball.x) {
241 		if (window->ball.x < ax1)
242 			window->ball.x = 2 * ax1 - window->ball.x;
243 		if (ax2 <= window->ball.x)
244 			window->ball.x = 2 * ax2 - window->ball.x;
245 
246 		window->ball.dx *= -1.0f;
247 	}
248 
249 	window->ball.y += window->ball.dy * dt;
250 	while (window->ball.y < ay1 || ay2 < window->ball.y) {
251 		if (window->ball.y < ay1)
252 			window->ball.y = 2 * ay1 - window->ball.y;
253 		if (ay2 <= window->ball.y)
254 			window->ball.y = 2 * ay2 - window->ball.y;
255 
256 		window->ball.dy *= -1.0f;
257 	}
258 
259 	window->ball.prev_time = timestamp;
260 }
261 
262 static struct window *
create_window(struct display * display,int width,int height,enum wl_output_transform transform,int scale,enum window_flags flags)263 create_window(struct display *display, int width, int height,
264 	      enum wl_output_transform transform, int scale,
265 	      enum window_flags flags)
266 {
267 	struct window *window;
268 
269 	if (display->compositor_version < 2 &&
270 	    (transform != WL_OUTPUT_TRANSFORM_NORMAL ||
271 	     flags & WINDOW_FLAG_ROTATING_TRANSFORM)) {
272 		fprintf(stderr, "wl_surface.buffer_transform unsupported in "
273 				"wl_surface version %d\n",
274 			display->compositor_version);
275 		exit(1);
276 	}
277 
278 	if (display->compositor_version < 3 &&
279 	    (! (flags & WINDOW_FLAG_USE_VIEWPORT)) && scale != 1) {
280 		fprintf(stderr, "wl_surface.buffer_scale unsupported in "
281 				"wl_surface version %d\n",
282 			display->compositor_version);
283 		exit(1);
284 	}
285 
286 	if (display->viewporter == NULL && (flags & WINDOW_FLAG_USE_VIEWPORT)) {
287 		fprintf(stderr, "Compositor does not support wp_viewport");
288 		exit(1);
289 	}
290 
291 	if (display->compositor_version <
292 	    WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION &&
293 	    (flags & WINDOW_FLAG_USE_DAMAGE_BUFFER)) {
294 		fprintf(stderr, "wl_surface.damage_buffer unsupported in "
295 				"wl_surface version %d\n",
296 			display->compositor_version);
297 		exit(1);
298 	}
299 
300 	window = zalloc(sizeof *window);
301 	if (!window)
302 		return NULL;
303 
304 	window->callback = NULL;
305 	window->display = display;
306 	window->width = width;
307 	window->height = height;
308 	window->border = 10;
309 	window->flags = flags;
310 	window->transform = transform;
311 	window->scale = scale;
312 
313 	window_init_game(window);
314 
315 	window->surface = wl_compositor_create_surface(display->compositor);
316 
317 	if (window->flags & WINDOW_FLAG_USE_VIEWPORT)
318 		window->viewport = wp_viewporter_get_viewport(display->viewporter,
319 							      window->surface);
320 
321 	if (display->wm_base) {
322 		window->xdg_surface =
323 			xdg_wm_base_get_xdg_surface(display->wm_base,
324 						    window->surface);
325 
326 		assert(window->xdg_surface);
327 
328 		xdg_surface_add_listener(window->xdg_surface,
329 					 &xdg_surface_listener, window);
330 
331 		window->xdg_toplevel =
332 			xdg_surface_get_toplevel(window->xdg_surface);
333 
334 		assert(window->xdg_toplevel);
335 
336 		xdg_toplevel_add_listener(window->xdg_toplevel,
337 					  &xdg_toplevel_listener, window);
338 
339 		xdg_toplevel_set_title(window->xdg_toplevel, "simple-damage");
340 
341 		window->wait_for_configure = true;
342 		wl_surface_commit(window->surface);
343 	} else if (display->fshell) {
344 		zwp_fullscreen_shell_v1_present_surface(display->fshell,
345 							window->surface,
346 							ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
347 							NULL);
348 	} else {
349 		assert(0);
350 	}
351 
352 	/* Initialise damage to full surface, so the padding gets painted */
353 	if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
354 		wl_surface_damage_buffer(window->surface, 0, 0,
355 					 INT32_MAX, INT32_MAX);
356 	} else {
357 		wl_surface_damage(window->surface, 0, 0, INT32_MAX, INT32_MAX);
358 	}
359 	return window;
360 }
361 
362 static void
destroy_window(struct window * window)363 destroy_window(struct window *window)
364 {
365 	if (window->callback)
366 		wl_callback_destroy(window->callback);
367 
368 	if (window->buffers[0].buffer)
369 		wl_buffer_destroy(window->buffers[0].buffer);
370 	if (window->buffers[1].buffer)
371 		wl_buffer_destroy(window->buffers[1].buffer);
372 
373 	if (window->xdg_toplevel)
374 		xdg_toplevel_destroy(window->xdg_toplevel);
375 	if (window->xdg_surface)
376 		xdg_surface_destroy(window->xdg_surface);
377 	if (window->viewport)
378 		wp_viewport_destroy(window->viewport);
379 	wl_surface_destroy(window->surface);
380 	free(window);
381 }
382 
383 static struct buffer *
window_next_buffer(struct window * window)384 window_next_buffer(struct window *window)
385 {
386 	struct buffer *buffer;
387 	int ret = 0, bwidth, bheight;
388 
389 	if (!window->buffers[0].busy)
390 		buffer = &window->buffers[0];
391 	else if (!window->buffers[1].busy)
392 		buffer = &window->buffers[1];
393 	else
394 		return NULL;
395 
396 	switch (window->transform) {
397 	default:
398 	case WL_OUTPUT_TRANSFORM_NORMAL:
399 	case WL_OUTPUT_TRANSFORM_180:
400 	case WL_OUTPUT_TRANSFORM_FLIPPED:
401 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
402 		bwidth = window->width * window->scale;
403 		bheight = window->height * window->scale;
404 		break;
405 	case WL_OUTPUT_TRANSFORM_90:
406 	case WL_OUTPUT_TRANSFORM_270:
407 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
408 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
409 		bwidth = window->height * window->scale;
410 		bheight = window->width * window->scale;
411 		break;
412 	}
413 
414 	if (!buffer->buffer) {
415 		ret = create_shm_buffer(window->display, buffer,
416 					bwidth, bheight,
417 					WL_SHM_FORMAT_ARGB8888);
418 
419 		if (ret < 0)
420 			return NULL;
421 	}
422 
423 	return buffer;
424 }
425 
426 static void
paint_box(uint32_t * pixels,int pitch,int x,int y,int width,int height,uint32_t color)427 paint_box(uint32_t *pixels, int pitch, int x, int y, int width, int height,
428 	  uint32_t color)
429 {
430 	int i, j;
431 
432 	for (j = y; j < y + height; ++j)
433 		for (i = x; i < x + width; ++i)
434 			pixels[i + j * pitch] = color;
435 }
436 
437 static void
paint_circle(uint32_t * pixels,int pitch,float x,float y,int radius,uint32_t color)438 paint_circle(uint32_t *pixels, int pitch, float x, float y, int radius,
439 	     uint32_t color)
440 {
441 	int i, j;
442 
443 	for (j = y - radius; j <= (int)(y + radius); ++j)
444 		for (i = x - radius; i <= (int)(x + radius); ++i)
445 			if ((j+0.5f-y)*(j+0.5f-y) + (i+0.5f-x)*(i+0.5f-x) <= radius * radius)
446 				pixels[i + j * pitch] = color;
447 }
448 
449 static void
window_get_transformed_ball(struct window * window,float * bx,float * by)450 window_get_transformed_ball(struct window *window, float *bx, float *by)
451 {
452 	float wx, wy;
453 
454 	wx = window->ball.x;
455 	wy = window->ball.y;
456 
457 	switch (window->transform) {
458 	default:
459 	case WL_OUTPUT_TRANSFORM_NORMAL:
460 		*bx = wx;
461 		*by = wy;
462 		break;
463 	case WL_OUTPUT_TRANSFORM_90:
464 		*bx = window->height - wy;
465 		*by = wx;
466 		break;
467 	case WL_OUTPUT_TRANSFORM_180:
468 		*bx = window->width - wx;
469 		*by = window->height - wy;
470 		break;
471 	case WL_OUTPUT_TRANSFORM_270:
472 		*bx = wy;
473 		*by = window->width - wx;
474 		break;
475 	case WL_OUTPUT_TRANSFORM_FLIPPED:
476 		*bx = window->width - wx;
477 		*by = wy;
478 		break;
479 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
480 		*bx = window->height - wy;
481 		*by = window->width - wx;
482 		break;
483 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
484 		*bx = wx;
485 		*by = window->height - wy;
486 		break;
487 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
488 		*bx = wy;
489 		*by = wx;
490 		break;
491 	}
492 
493 	*bx *= window->scale;
494 	*by *= window->scale;
495 
496 	if (window->viewport) {
497 		/* We're drawing half-size because of the viewport */
498 		*bx /= 2;
499 		*by /= 2;
500 	}
501 }
502 
503 static const struct wl_callback_listener frame_listener;
504 
505 static void
redraw(void * data,struct wl_callback * callback,uint32_t time)506 redraw(void *data, struct wl_callback *callback, uint32_t time)
507 {
508 	struct window *window = data;
509 	struct buffer *buffer;
510 	int off_x = 0, off_y = 0;
511 	int bwidth, bheight, bborder, bpitch, bradius;
512 	float bx, by;
513 
514 	buffer = window_next_buffer(window);
515 	if (!buffer) {
516 		fprintf(stderr,
517 			!callback ? "Failed to create the first buffer.\n" :
518 			"Both buffers busy at redraw(). Server bug?\n");
519 		abort();
520 	}
521 
522 	/* Rotate the damage, but keep the even/odd parity so the
523 	 * dimensions of the buffers don't change */
524 	if (window->flags & WINDOW_FLAG_ROTATING_TRANSFORM)
525 		window->transform = (window->transform + 2) % 8;
526 
527 	switch (window->transform) {
528 	default:
529 	case WL_OUTPUT_TRANSFORM_NORMAL:
530 	case WL_OUTPUT_TRANSFORM_180:
531 	case WL_OUTPUT_TRANSFORM_FLIPPED:
532 	case WL_OUTPUT_TRANSFORM_FLIPPED_180:
533 		bwidth = window->width * window->scale;
534 		bheight = window->height * window->scale;
535 		break;
536 	case WL_OUTPUT_TRANSFORM_90:
537 	case WL_OUTPUT_TRANSFORM_270:
538 	case WL_OUTPUT_TRANSFORM_FLIPPED_90:
539 	case WL_OUTPUT_TRANSFORM_FLIPPED_270:
540 		bwidth = window->height * window->scale;
541 		bheight = window->width * window->scale;
542 		break;
543 	}
544 
545 	bpitch = bwidth;
546 
547 	bborder = window->border * window->scale;
548 	bradius = window->ball.radius * window->scale;
549 
550 	if (window->viewport) {
551 		int tx, ty;
552 		/* Fill the whole thing with red to detect viewport errors */
553 		paint_box(buffer->shm_data, bpitch, 0, 0, bwidth, bheight,
554 			  0xffff0000);
555 
556 		/* The buffer is the same size.  However, we crop it
557 		 * and scale it up by a factor of 2 */
558 		bborder /= 2;
559 		bradius /= 2;
560 		bwidth /= 2;
561 		bheight /= 2;
562 
563 		/* Offset the drawing region */
564 		tx = (window->width / 3) * window->scale;
565 		ty = (window->height / 5) * window->scale;
566 		switch (window->transform) {
567 		default:
568 		case WL_OUTPUT_TRANSFORM_NORMAL:
569 			off_y = ty;
570 			off_x = tx;
571 			break;
572 		case WL_OUTPUT_TRANSFORM_90:
573 			off_y = tx;
574 			off_x = bwidth - ty;
575 			break;
576 		case WL_OUTPUT_TRANSFORM_180:
577 			off_y = bheight - ty;
578 			off_x = bwidth - tx;
579 			break;
580 		case WL_OUTPUT_TRANSFORM_270:
581 			off_y = bheight - tx;
582 			off_x = ty;
583 			break;
584 		case WL_OUTPUT_TRANSFORM_FLIPPED:
585 			off_y = ty;
586 			off_x = bwidth - tx;
587 			break;
588 		case WL_OUTPUT_TRANSFORM_FLIPPED_90:
589 			off_y = bheight - tx;
590 			off_x = bwidth - ty;
591 			break;
592 		case WL_OUTPUT_TRANSFORM_FLIPPED_180:
593 			off_y = bheight - ty;
594 			off_x = tx;
595 			break;
596 		case WL_OUTPUT_TRANSFORM_FLIPPED_270:
597 			off_y = tx;
598 			off_x = ty;
599 			break;
600 		}
601 		wp_viewport_set_source(window->viewport,
602 				       wl_fixed_from_int(window->width / 3),
603 				       wl_fixed_from_int(window->height / 5),
604 				       wl_fixed_from_int(window->width / 2),
605 				       wl_fixed_from_int(window->height / 2));
606 	}
607 
608 	/* Paint the border */
609 	paint_box(buffer->shm_data, bpitch, off_x, off_y,
610 		  bwidth, bborder, 0xffffffff);
611 	paint_box(buffer->shm_data, bpitch, off_x, off_y,
612 		  bborder, bheight, 0xffffffff);
613 	paint_box(buffer->shm_data, bpitch, off_x + bwidth - bborder, off_y,
614 		  bborder, bheight, 0xffffffff);
615 	paint_box(buffer->shm_data, bpitch, off_x, off_y + bheight - bborder,
616 		  bwidth, bborder, 0xffffffff);
617 
618 	/* fill with translucent */
619 	paint_box(buffer->shm_data, bpitch, off_x + bborder, off_y + bborder,
620 		  bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000);
621 
622 	/* Damage where the ball was */
623 	if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
624 		window_get_transformed_ball(window, &bx, &by);
625 		wl_surface_damage_buffer(window->surface,
626 					 bx - bradius + off_x,
627 					 by - bradius + off_y,
628 					 bradius * 2 + 1,
629 					 bradius * 2 + 1);
630 	} else {
631 		wl_surface_damage(window->surface,
632 				  window->ball.x - window->ball.radius,
633 				  window->ball.y - window->ball.radius,
634 				  window->ball.radius * 2 + 1,
635 				  window->ball.radius * 2 + 1);
636 	}
637 	window_advance_game(window, time);
638 
639 	window_get_transformed_ball(window, &bx, &by);
640 
641 	/* Paint the ball */
642 	paint_circle(buffer->shm_data, bpitch, off_x + bx, off_y + by,
643 		     bradius, 0xff00ff00);
644 
645 	if (print_debug) {
646 		printf("Ball now located at (%f, %f)\n",
647 		       window->ball.x, window->ball.y);
648 
649 		printf("Circle painted at (%f, %f), radius %d\n", bx, by,
650 		       bradius);
651 
652 		printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n",
653 		       (int)(bx - bradius) + off_x,
654 		       (int)(by - bradius) + off_y,
655 		       bradius * 2 + 1, bradius * 2 + 1);
656 	}
657 
658 	/* Damage where the ball is now */
659 	if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
660 		wl_surface_damage_buffer(window->surface,
661 					 bx - bradius + off_x,
662 					 by - bradius + off_y,
663 					 bradius * 2 + 1,
664 					 bradius * 2 + 1);
665 	} else {
666 		wl_surface_damage(window->surface,
667 				  window->ball.x - window->ball.radius,
668 				  window->ball.y - window->ball.radius,
669 				  window->ball.radius * 2 + 1,
670 				  window->ball.radius * 2 + 1);
671 	}
672 	wl_surface_attach(window->surface, buffer->buffer, 0, 0);
673 
674 	if (window->display->compositor_version >= 2 &&
675 	    (window->transform != WL_OUTPUT_TRANSFORM_NORMAL ||
676 	     window->flags & WINDOW_FLAG_ROTATING_TRANSFORM))
677 		wl_surface_set_buffer_transform(window->surface,
678 						window->transform);
679 
680 	if (window->viewport)
681 		wp_viewport_set_destination(window->viewport,
682 					    window->width,
683 					    window->height);
684 
685 	if (window->scale != 1)
686 		wl_surface_set_buffer_scale(window->surface,
687 					    window->scale);
688 
689 	if (callback)
690 		wl_callback_destroy(callback);
691 
692 	window->callback = wl_surface_frame(window->surface);
693 	wl_callback_add_listener(window->callback, &frame_listener, window);
694 	wl_surface_commit(window->surface);
695 	buffer->busy = 1;
696 }
697 
698 static const struct wl_callback_listener frame_listener = {
699 	redraw
700 };
701 
702 static void
shm_format(void * data,struct wl_shm * wl_shm,uint32_t format)703 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
704 {
705 	struct display *d = data;
706 
707 	d->formats |= (1 << format);
708 }
709 
710 struct wl_shm_listener shm_listener = {
711 	shm_format
712 };
713 
714 static void
xdg_wm_base_ping(void * data,struct xdg_wm_base * shell,uint32_t serial)715 xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
716 {
717 	xdg_wm_base_pong(shell, serial);
718 }
719 
720 static const struct xdg_wm_base_listener wm_base_listener = {
721 	xdg_wm_base_ping,
722 };
723 
724 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)725 registry_handle_global(void *data, struct wl_registry *registry,
726 		       uint32_t id, const char *interface, uint32_t version)
727 {
728 	struct display *d = data;
729 
730 	if (strcmp(interface, "wl_compositor") == 0) {
731 		if (d->compositor_version > (int)version) {
732 			fprintf(stderr, "Compositor does not support "
733 				"wl_surface version %d\n", d->compositor_version);
734 			exit(1);
735 		}
736 
737 		if (d->compositor_version < 0)
738 			d->compositor_version = version;
739 
740 		d->compositor =
741 			wl_registry_bind(registry,
742 					 id, &wl_compositor_interface,
743 					 d->compositor_version);
744 	} else if (strcmp(interface, "wp_viewporter") == 0) {
745 		d->viewporter = wl_registry_bind(registry, id,
746 						 &wp_viewporter_interface, 1);
747 	} else if (strcmp(interface, "xdg_wm_base") == 0) {
748 		d->wm_base = wl_registry_bind(registry,
749 					      id, &xdg_wm_base_interface, 1);
750 		xdg_wm_base_add_listener(d->wm_base, &wm_base_listener, d);
751 	} else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
752 		d->fshell = wl_registry_bind(registry,
753 					     id, &zwp_fullscreen_shell_v1_interface, 1);
754 	} else if (strcmp(interface, "wl_shm") == 0) {
755 		d->shm = wl_registry_bind(registry,
756 					  id, &wl_shm_interface, 1);
757 		wl_shm_add_listener(d->shm, &shm_listener, d);
758 	}
759 }
760 
761 static void
registry_handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)762 registry_handle_global_remove(void *data, struct wl_registry *registry,
763 			      uint32_t name)
764 {
765 }
766 
767 static const struct wl_registry_listener registry_listener = {
768 	registry_handle_global,
769 	registry_handle_global_remove
770 };
771 
772 static struct display *
create_display(int version)773 create_display(int version)
774 {
775 	struct display *display;
776 
777 	display = malloc(sizeof *display);
778 	if (display == NULL) {
779 		fprintf(stderr, "out of memory\n");
780 		exit(1);
781 	}
782 	display->display = wl_display_connect(NULL);
783 	assert(display->display);
784 
785 	display->compositor_version = version;
786 	display->formats = 0;
787 	display->registry = wl_display_get_registry(display->display);
788 	wl_registry_add_listener(display->registry,
789 				 &registry_listener, display);
790 	wl_display_roundtrip(display->display);
791 	if (display->shm == NULL) {
792 		fprintf(stderr, "No wl_shm global\n");
793 		exit(1);
794 	}
795 
796 	wl_display_roundtrip(display->display);
797 
798 	if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
799 		fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
800 		exit(1);
801 	}
802 
803 	return display;
804 }
805 
806 static void
destroy_display(struct display * display)807 destroy_display(struct display *display)
808 {
809 	if (display->shm)
810 		wl_shm_destroy(display->shm);
811 
812 	if (display->wm_base)
813 		xdg_wm_base_destroy(display->wm_base);
814 
815 	if (display->fshell)
816 		zwp_fullscreen_shell_v1_release(display->fshell);
817 
818 	if (display->viewporter)
819 		wp_viewporter_destroy(display->viewporter);
820 
821 	if (display->compositor)
822 		wl_compositor_destroy(display->compositor);
823 
824 	wl_registry_destroy(display->registry);
825 	wl_display_flush(display->display);
826 	wl_display_disconnect(display->display);
827 	free(display);
828 }
829 
830 static void
signal_int(int signum)831 signal_int(int signum)
832 {
833 	running = 0;
834 }
835 
836 static void
print_usage(int retval)837 print_usage(int retval)
838 {
839 	printf(
840 		"usage: weston-simple-damage [options]\n\n"
841 		"options:\n"
842 		"  -h, --help\t\tPring this help\n"
843 		"  --verbose\t\tPrint verbose log information\n"
844 		"  --version=VERSION\tVersion of wl_surface to use\n"
845 		"  --width=WIDTH\t\tWidth of the window\n"
846 		"  --height=HEIGHT\tHeight of the window\n"
847 		"  --scale=SCALE\t\tScale factor for the surface\n"
848 		"  --transform=TRANSFORM\tTransform for the surface\n"
849 		"  --rotating-transform\tUse a different buffer_transform for each frame\n"
850 		"  --use-viewport\tUse wp_viewport\n"
851 		"  --use-damage-buffer\tUse damage_buffer to post damage\n"
852 	);
853 
854 	exit(retval);
855 }
856 
857 static int
parse_transform(const char * str,enum wl_output_transform * transform)858 parse_transform(const char *str, enum wl_output_transform *transform)
859 {
860 	int i;
861 	static const struct {
862 		const char *name;
863 		enum wl_output_transform transform;
864 	} names[] = {
865 		{ "normal",	WL_OUTPUT_TRANSFORM_NORMAL },
866 		{ "90",		WL_OUTPUT_TRANSFORM_90 },
867 		{ "180",	WL_OUTPUT_TRANSFORM_180 },
868 		{ "270",	WL_OUTPUT_TRANSFORM_270 },
869 		{ "flipped",	WL_OUTPUT_TRANSFORM_FLIPPED },
870 		{ "flipped-90",	WL_OUTPUT_TRANSFORM_FLIPPED_90 },
871 		{ "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
872 		{ "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
873 	};
874 
875 	for (i = 0; i < 8; i++) {
876 		if (strcmp(names[i].name, str) == 0) {
877 			*transform = names[i].transform;
878 			return 1;
879 		}
880 	}
881 
882 	return 0;
883 }
884 
885 int
main(int argc,char ** argv)886 main(int argc, char **argv)
887 {
888 	struct sigaction sigint;
889 	struct display *display;
890 	struct window *window;
891 	int i, ret = 0;
892 	int version = -1;
893 	int width = 300, height = 200, scale = 1;
894 	enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
895 	enum window_flags flags = 0;
896 
897 	for (i = 1; i < argc; ++i) {
898 		if (strcmp(argv[i], "--help") == 0 ||
899 		    strcmp(argv[i], "-h") == 0) {
900 			print_usage(0);
901 		} else if (sscanf(argv[i], "--version=%d", &version) > 0) {
902 			if (version < 1 || version > 4) {
903 				fprintf(stderr, "Unsupported wl_surface version: %d\n",
904 					version);
905 				return 1;
906 			}
907 			continue;
908 		} else if (strcmp(argv[i], "--verbose") == 0) {
909 			print_debug = 1;
910 			continue;
911 		} else if (sscanf(argv[i], "--width=%d", &width) > 0) {
912 			continue;
913 		} else if (sscanf(argv[i], "--height=%d", &height) > 0) {
914 			continue;
915 		} else if (strncmp(argv[i], "--transform=", 12) == 0 &&
916 			   parse_transform(argv[i] + 12, &transform) > 0) {
917 			continue;
918 		} else if (strcmp(argv[i], "--rotating-transform") == 0) {
919 			flags |= WINDOW_FLAG_ROTATING_TRANSFORM;
920 			continue;
921 		} else if (sscanf(argv[i], "--scale=%d", &scale) > 0) {
922 			continue;
923 		} else if (strcmp(argv[i], "--use-viewport") == 0) {
924 			flags |= WINDOW_FLAG_USE_VIEWPORT;
925 			continue;
926 		} else if (strcmp(argv[i], "--use-damage-buffer") == 0) {
927 			flags |= WINDOW_FLAG_USE_DAMAGE_BUFFER;
928 			continue;
929 		} else {
930 			printf("Invalid option: %s\n", argv[i]);
931 			print_usage(255);
932 		}
933 	}
934 
935 	display = create_display(version);
936 
937 	window = create_window(display, width, height, transform, scale, flags);
938 	if (!window)
939 		return 1;
940 
941 	sigint.sa_handler = signal_int;
942 	sigemptyset(&sigint.sa_mask);
943 	sigint.sa_flags = SA_RESETHAND;
944 	sigaction(SIGINT, &sigint, NULL);
945 
946 	if (!window->wait_for_configure)
947 		redraw(window, NULL, 0);
948 
949 	while (running && ret != -1)
950 		ret = wl_display_dispatch(display->display);
951 
952 	fprintf(stderr, "simple-shm exiting\n");
953 	destroy_window(window);
954 	destroy_display(display);
955 
956 	return 0;
957 }
958