blob: 0353cccb04bc851a62d7ab628a9d57b00f57b0f0 [file] [log] [blame]
Jason Ekstrand549a53f2014-04-05 09:22:15 -05001/*
2 * Copyright © 2014 Jason Ekstrand
3 * Copyright © 2011 Benjamin Franzke
4 * Copyright © 2010 Intel Corporation
5 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -07006 * 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:
Jason Ekstrand549a53f2014-04-05 09:22:15 -050012 *
Bryce Harrington1f6b0d12015-06-10 22:48:59 -070013 * 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.
Jason Ekstrand549a53f2014-04-05 09:22:15 -050024 */
25
26#include <config.h>
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdbool.h>
32#include <assert.h>
33#include <unistd.h>
34#include <sys/mman.h>
35#include <sys/time.h>
36#include <signal.h>
37
38#include <wayland-client.h>
Jon Cruz4678bab2015-06-15 15:37:07 -070039#include "shared/os-compatibility.h"
Jonas Ådahl2a229332015-11-17 16:00:32 +080040#include "xdg-shell-unstable-v5-client-protocol.h"
Jonas Ådahl496adb32015-11-17 16:00:27 +080041#include "fullscreen-shell-unstable-v1-client-protocol.h"
Jason Ekstrand549a53f2014-04-05 09:22:15 -050042#include "scaler-client-protocol.h"
43
44int print_debug = 0;
45
46struct display {
47 struct wl_display *display;
48 struct wl_registry *registry;
49 int compositor_version;
50 struct wl_compositor *compositor;
51 struct wl_scaler *scaler;
52 struct xdg_shell *shell;
Jonas Ådahl496adb32015-11-17 16:00:27 +080053 struct zwp_fullscreen_shell_v1 *fshell;
Jason Ekstrand549a53f2014-04-05 09:22:15 -050054 struct wl_shm *shm;
55 uint32_t formats;
56};
57
58struct buffer {
59 struct wl_buffer *buffer;
60 uint32_t *shm_data;
61 int busy;
62};
63
64enum window_flags {
65 WINDOW_FLAG_USE_VIEWPORT = 0x1,
66 WINDOW_FLAG_ROTATING_TRANSFORM = 0x2,
Derek Foremanfb1e1262015-11-18 16:32:34 -060067 WINDOW_FLAG_USE_DAMAGE_BUFFER = 0x4,
Jason Ekstrand549a53f2014-04-05 09:22:15 -050068};
69
70struct window {
71 struct display *display;
72 int width, height, border;
73 struct wl_surface *surface;
74 struct wl_viewport *viewport;
75 struct xdg_surface *xdg_surface;
76 struct wl_callback *callback;
77 struct buffer buffers[2];
78 struct buffer *prev_buffer;
79
80 enum window_flags flags;
81 int scale;
82 enum wl_output_transform transform;
83
84 struct {
85 float x, y; /* position in pixels */
86 float dx, dy; /* velocity in pixels/second */
87 int radius; /* radius in pixels */
88 uint32_t prev_time;
89 } ball;
90};
91
92static int running = 1;
93
94static void
95buffer_release(void *data, struct wl_buffer *buffer)
96{
97 struct buffer *mybuf = data;
98
99 mybuf->busy = 0;
100}
101
102static const struct wl_buffer_listener buffer_listener = {
103 buffer_release
104};
105
106static int
107create_shm_buffer(struct display *display, struct buffer *buffer,
108 int width, int height, uint32_t format)
109{
110 struct wl_shm_pool *pool;
111 int fd, size, pitch;
112 void *data;
113
114 pitch = width * 4;
115 size = pitch * height;
116
117 fd = os_create_anonymous_file(size);
118 if (fd < 0) {
119 fprintf(stderr, "creating a buffer file for %d B failed: %m\n",
120 size);
121 return -1;
122 }
123
124 data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
125 if (data == MAP_FAILED) {
126 fprintf(stderr, "mmap failed: %m\n");
127 close(fd);
128 return -1;
129 }
130
131 pool = wl_shm_create_pool(display->shm, fd, size);
132 buffer->buffer = wl_shm_pool_create_buffer(pool, 0,
133 width, height,
134 pitch, format);
135 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
136 wl_shm_pool_destroy(pool);
137 close(fd);
138
139 buffer->shm_data = data;
140
141 return 0;
142}
143
144static void
145handle_configure(void *data, struct xdg_surface *surface,
146 int32_t width, int32_t height, struct wl_array *states,
147 uint32_t serial)
148{
149}
150
151static void
152handle_close(void *data, struct xdg_surface *xdg_surface)
153{
154 running = 0;
155}
156
157static const struct xdg_surface_listener xdg_surface_listener = {
158 handle_configure,
159 handle_close,
160};
161
162static float
163bounded_randf(float a, float b)
164{
165 return a + ((float)rand() / (float)RAND_MAX) * (b - a);
166}
167
168static void
169window_init_game(struct window *window)
170{
171 int ax1, ay1, ax2, ay2; /* playable arena size */
172 struct timeval tv;
173
174 gettimeofday(&tv, NULL);
175 srand(tv.tv_usec);
176
177 window->ball.radius = 10;
178
179 ax1 = window->border + window->ball.radius;
180 ay1 = window->border + window->ball.radius;
181 ax2 = window->width - window->border - window->ball.radius;
182 ay2 = window->height - window->border - window->ball.radius;
183
184 window->ball.x = bounded_randf(ax1, ax2);
185 window->ball.y = bounded_randf(ay1, ay2);
186
187 window->ball.dx = bounded_randf(0, window->width);
188 window->ball.dy = bounded_randf(0, window->height);
189
190 window->ball.prev_time = 0;
191}
192
193static void
194window_advance_game(struct window *window, uint32_t timestamp)
195{
196 int ax1, ay1, ax2, ay2; /* Arena size */
197 float dt;
198
199 if (window->ball.prev_time == 0) {
200 /* first pass, don't do anything */
201 window->ball.prev_time = timestamp;
202 return;
203 }
204
205 /* dt in seconds */
206 dt = (float)(timestamp - window->ball.prev_time) / 1000.0f;
207
208 ax1 = window->border + window->ball.radius;
209 ay1 = window->border + window->ball.radius;
210 ax2 = window->width - window->border - window->ball.radius;
211 ay2 = window->height - window->border - window->ball.radius;
212
213 window->ball.x += window->ball.dx * dt;
214 while (window->ball.x < ax1 || ax2 < window->ball.x) {
215 if (window->ball.x < ax1)
216 window->ball.x = 2 * ax1 - window->ball.x;
217 if (ax2 <= window->ball.x)
218 window->ball.x = 2 * ax2 - window->ball.x;
219
220 window->ball.dx *= -1.0f;
221 }
222
223 window->ball.y += window->ball.dy * dt;
224 while (window->ball.y < ay1 || ay2 < window->ball.y) {
225 if (window->ball.y < ay1)
226 window->ball.y = 2 * ay1 - window->ball.y;
227 if (ay2 <= window->ball.y)
228 window->ball.y = 2 * ay2 - window->ball.y;
229
230 window->ball.dy *= -1.0f;
231 }
232
233 window->ball.prev_time = timestamp;
234}
235
236static struct window *
237create_window(struct display *display, int width, int height,
238 enum wl_output_transform transform, int scale,
239 enum window_flags flags)
240{
241 struct window *window;
242
243 if (display->compositor_version < 2 &&
244 (transform != WL_OUTPUT_TRANSFORM_NORMAL ||
245 flags & WINDOW_FLAG_ROTATING_TRANSFORM)) {
246 fprintf(stderr, "wl_surface.buffer_transform unsupported in "
247 "wl_surface version %d\n",
248 display->compositor_version);
249 exit(1);
250 }
251
252 if (display->compositor_version < 3 &&
253 (! (flags & WINDOW_FLAG_USE_VIEWPORT)) && scale != 1) {
254 fprintf(stderr, "wl_surface.buffer_scale unsupported in "
255 "wl_surface version %d\n",
256 display->compositor_version);
257 exit(1);
258 }
259
260 if (display->scaler == NULL && (flags & WINDOW_FLAG_USE_VIEWPORT)) {
261 fprintf(stderr, "Compositor does not support wl_viewport");
262 exit(1);
263 }
264
Christopher Michaele1434d32015-12-20 07:41:52 -0500265 if (display->compositor_version <
266 WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION &&
Derek Foremanfb1e1262015-11-18 16:32:34 -0600267 (flags & WINDOW_FLAG_USE_DAMAGE_BUFFER)) {
268 fprintf(stderr, "wl_surface.damage_buffer unsupported in "
269 "wl_surface version %d\n",
270 display->compositor_version);
271 exit(1);
272 }
273
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500274 window = calloc(1, sizeof *window);
275 if (!window)
276 return NULL;
277
278 window->callback = NULL;
279 window->display = display;
280 window->width = width;
281 window->height = height;
282 window->border = 10;
283 window->flags = flags;
284 window->transform = transform;
285 window->scale = scale;
286
287 window_init_game(window);
288
289 window->surface = wl_compositor_create_surface(display->compositor);
290
291 if (window->flags & WINDOW_FLAG_USE_VIEWPORT)
292 window->viewport = wl_scaler_get_viewport(display->scaler,
293 window->surface);
294
295 if (display->shell) {
296 window->xdg_surface =
297 xdg_shell_get_xdg_surface(display->shell,
298 window->surface);
299
300 assert(window->xdg_surface);
301
302 xdg_surface_add_listener(window->xdg_surface,
303 &xdg_surface_listener, window);
304
305 xdg_surface_set_title(window->xdg_surface, "simple-damage");
306 } else if (display->fshell) {
Jonas Ådahl496adb32015-11-17 16:00:27 +0800307 zwp_fullscreen_shell_v1_present_surface(display->fshell,
308 window->surface,
309 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
310 NULL);
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500311 } else {
312 assert(0);
313 }
314
315 /* Initialise damage to full surface, so the padding gets painted */
Derek Foremanfb1e1262015-11-18 16:32:34 -0600316 if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
317 wl_surface_damage_buffer(window->surface, 0, 0,
318 INT32_MAX, INT32_MAX);
319 } else {
320 wl_surface_damage(window->surface, 0, 0, INT32_MAX, INT32_MAX);
321 }
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500322 return window;
323}
324
325static void
326destroy_window(struct window *window)
327{
328 if (window->callback)
329 wl_callback_destroy(window->callback);
330
331 if (window->buffers[0].buffer)
332 wl_buffer_destroy(window->buffers[0].buffer);
333 if (window->buffers[1].buffer)
334 wl_buffer_destroy(window->buffers[1].buffer);
335
336 if (window->xdg_surface)
337 xdg_surface_destroy(window->xdg_surface);
338 if (window->viewport)
339 wl_viewport_destroy(window->viewport);
340 wl_surface_destroy(window->surface);
341 free(window);
342}
343
344static struct buffer *
345window_next_buffer(struct window *window)
346{
347 struct buffer *buffer;
348 int ret = 0, bwidth, bheight;
349
350 if (!window->buffers[0].busy)
351 buffer = &window->buffers[0];
352 else if (!window->buffers[1].busy)
353 buffer = &window->buffers[1];
354 else
355 return NULL;
356
357 switch (window->transform) {
358 default:
359 case WL_OUTPUT_TRANSFORM_NORMAL:
360 case WL_OUTPUT_TRANSFORM_180:
361 case WL_OUTPUT_TRANSFORM_FLIPPED:
362 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
363 bwidth = window->width * window->scale;
364 bheight = window->height * window->scale;
365 break;
366 case WL_OUTPUT_TRANSFORM_90:
367 case WL_OUTPUT_TRANSFORM_270:
368 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
369 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
370 bwidth = window->height * window->scale;
371 bheight = window->width * window->scale;
372 break;
373 }
374
375 if (!buffer->buffer) {
376 ret = create_shm_buffer(window->display, buffer,
377 bwidth, bheight,
378 WL_SHM_FORMAT_ARGB8888);
379
380 if (ret < 0)
381 return NULL;
382 }
383
384 return buffer;
385}
386
387static void
388paint_box(uint32_t *pixels, int pitch, int x, int y, int width, int height,
389 uint32_t color)
390{
391 int i, j;
392
393 for (j = y; j < y + height; ++j)
394 for (i = x; i < x + width; ++i)
395 pixels[i + j * pitch] = color;
396}
397
398static void
399paint_circle(uint32_t *pixels, int pitch, float x, float y, int radius,
400 uint32_t color)
401{
402 int i, j;
403
404 for (j = y - radius; j <= (int)(y + radius); ++j)
405 for (i = x - radius; i <= (int)(x + radius); ++i)
406 if ((j+0.5f-y)*(j+0.5f-y) + (i+0.5f-x)*(i+0.5f-x) <= radius * radius)
407 pixels[i + j * pitch] = color;
408}
409
410static void
411window_get_transformed_ball(struct window *window, float *bx, float *by)
412{
413 float wx, wy;
414
415 wx = window->ball.x;
416 wy = window->ball.y;
417
418 switch (window->transform) {
419 default:
420 case WL_OUTPUT_TRANSFORM_NORMAL:
421 *bx = wx;
422 *by = wy;
423 break;
424 case WL_OUTPUT_TRANSFORM_90:
425 *bx = window->height - wy;
426 *by = wx;
427 break;
428 case WL_OUTPUT_TRANSFORM_180:
429 *bx = window->width - wx;
430 *by = window->height - wy;
431 break;
432 case WL_OUTPUT_TRANSFORM_270:
433 *bx = wy;
434 *by = window->width - wx;
435 break;
436 case WL_OUTPUT_TRANSFORM_FLIPPED:
437 *bx = window->width - wx;
438 *by = wy;
439 break;
440 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
441 *bx = window->height - wy;
442 *by = window->width - wx;
443 break;
444 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
445 *bx = wx;
446 *by = window->height - wy;
447 break;
448 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
449 *bx = wy;
450 *by = wx;
451 break;
452 }
453
454 *bx *= window->scale;
455 *by *= window->scale;
456
457 if (window->viewport) {
458 /* We're drawing half-size because of the viewport */
459 *bx /= 2;
460 *by /= 2;
461 }
462}
463
464static const struct wl_callback_listener frame_listener;
465
466static void
467redraw(void *data, struct wl_callback *callback, uint32_t time)
468{
469 struct window *window = data;
470 struct buffer *buffer;
Derek Foreman29b846e2015-11-18 16:32:33 -0600471 int off_x = 0, off_y = 0;
472 int bwidth, bheight, bborder, bpitch, bradius;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500473 float bx, by;
474
475 buffer = window_next_buffer(window);
476 if (!buffer) {
477 fprintf(stderr,
478 !callback ? "Failed to create the first buffer.\n" :
479 "Both buffers busy at redraw(). Server bug?\n");
480 abort();
481 }
482
483 /* Rotate the damage, but keep the even/odd parity so the
484 * dimensions of the buffers don't change */
485 if (window->flags & WINDOW_FLAG_ROTATING_TRANSFORM)
486 window->transform = (window->transform + 2) % 8;
487
488 switch (window->transform) {
489 default:
490 case WL_OUTPUT_TRANSFORM_NORMAL:
491 case WL_OUTPUT_TRANSFORM_180:
492 case WL_OUTPUT_TRANSFORM_FLIPPED:
493 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
494 bwidth = window->width * window->scale;
495 bheight = window->height * window->scale;
496 break;
497 case WL_OUTPUT_TRANSFORM_90:
498 case WL_OUTPUT_TRANSFORM_270:
499 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
500 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
501 bwidth = window->height * window->scale;
502 bheight = window->width * window->scale;
503 break;
504 }
505
506 bpitch = bwidth;
507
508 bborder = window->border * window->scale;
509 bradius = window->ball.radius * window->scale;
510
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500511 if (window->viewport) {
Derek Foreman29b846e2015-11-18 16:32:33 -0600512 int tx, ty;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500513 /* Fill the whole thing with red to detect viewport errors */
514 paint_box(buffer->shm_data, bpitch, 0, 0, bwidth, bheight,
515 0xffff0000);
516
517 /* The buffer is the same size. However, we crop it
518 * and scale it up by a factor of 2 */
519 bborder /= 2;
520 bradius /= 2;
521 bwidth /= 2;
522 bheight /= 2;
523
524 /* Offset the drawing region */
Derek Foreman29b846e2015-11-18 16:32:33 -0600525 tx = (window->width / 3) * window->scale;
526 ty = (window->height / 5) * window->scale;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500527 switch (window->transform) {
528 default:
529 case WL_OUTPUT_TRANSFORM_NORMAL:
Derek Foreman29b846e2015-11-18 16:32:33 -0600530 off_y = ty;
531 off_x = tx;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500532 break;
533 case WL_OUTPUT_TRANSFORM_90:
Derek Foreman29b846e2015-11-18 16:32:33 -0600534 off_y = tx;
535 off_x = bwidth - ty;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500536 break;
537 case WL_OUTPUT_TRANSFORM_180:
Derek Foreman29b846e2015-11-18 16:32:33 -0600538 off_y = bheight - ty;
539 off_x = bwidth - tx;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500540 break;
541 case WL_OUTPUT_TRANSFORM_270:
Derek Foreman29b846e2015-11-18 16:32:33 -0600542 off_y = bheight - tx;
543 off_x = ty;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500544 break;
545 case WL_OUTPUT_TRANSFORM_FLIPPED:
Derek Foreman29b846e2015-11-18 16:32:33 -0600546 off_y = ty;
547 off_x = bwidth - tx;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500548 break;
549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Derek Foreman29b846e2015-11-18 16:32:33 -0600550 off_y = bheight - tx;
551 off_x = bwidth - ty;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500552 break;
553 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Derek Foreman29b846e2015-11-18 16:32:33 -0600554 off_y = bheight - ty;
555 off_x = tx;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500556 break;
557 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Derek Foreman29b846e2015-11-18 16:32:33 -0600558 off_y = tx;
559 off_x = ty;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500560 break;
561 }
562 wl_viewport_set_source(window->viewport,
563 wl_fixed_from_int(window->width / 3),
564 wl_fixed_from_int(window->height / 5),
565 wl_fixed_from_int(window->width / 2),
566 wl_fixed_from_int(window->height / 2));
567 }
568
569 /* Paint the border */
Derek Foreman29b846e2015-11-18 16:32:33 -0600570 paint_box(buffer->shm_data, bpitch, off_x, off_y,
571 bwidth, bborder, 0xffffffff);
572 paint_box(buffer->shm_data, bpitch, off_x, off_y,
573 bborder, bheight, 0xffffffff);
574 paint_box(buffer->shm_data, bpitch, off_x + bwidth - bborder, off_y,
575 bborder, bheight, 0xffffffff);
576 paint_box(buffer->shm_data, bpitch, off_x, off_y + bheight - bborder,
577 bwidth, bborder, 0xffffffff);
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500578
579 /* fill with translucent */
Derek Foreman29b846e2015-11-18 16:32:33 -0600580 paint_box(buffer->shm_data, bpitch, off_x + bborder, off_y + bborder,
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500581 bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000);
582
583 /* Damage where the ball was */
Derek Foremanfb1e1262015-11-18 16:32:34 -0600584 if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
585 window_get_transformed_ball(window, &bx, &by);
586 wl_surface_damage_buffer(window->surface,
587 bx - bradius + off_x,
588 by - bradius + off_y,
589 bradius * 2 + 1,
590 bradius * 2 + 1);
591 } else {
592 wl_surface_damage(window->surface,
593 window->ball.x - window->ball.radius,
594 window->ball.y - window->ball.radius,
595 window->ball.radius * 2 + 1,
596 window->ball.radius * 2 + 1);
597 }
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500598 window_advance_game(window, time);
599
600 window_get_transformed_ball(window, &bx, &by);
601
602 /* Paint the ball */
Derek Foreman29b846e2015-11-18 16:32:33 -0600603 paint_circle(buffer->shm_data, bpitch, off_x + bx, off_y + by,
604 bradius, 0xff00ff00);
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500605
606 if (print_debug) {
607 printf("Ball now located at (%f, %f)\n",
608 window->ball.x, window->ball.y);
609
610 printf("Circle painted at (%f, %f), radius %d\n", bx, by,
611 bradius);
612
613 printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n",
Derek Foreman29b846e2015-11-18 16:32:33 -0600614 (int)(bx - bradius) + off_x,
615 (int)(by - bradius) + off_y,
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500616 bradius * 2 + 1, bradius * 2 + 1);
617 }
618
619 /* Damage where the ball is now */
Derek Foremanfb1e1262015-11-18 16:32:34 -0600620 if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) {
621 wl_surface_damage_buffer(window->surface,
622 bx - bradius + off_x,
623 by - bradius + off_y,
624 bradius * 2 + 1,
625 bradius * 2 + 1);
626 } else {
627 wl_surface_damage(window->surface,
628 window->ball.x - window->ball.radius,
629 window->ball.y - window->ball.radius,
630 window->ball.radius * 2 + 1,
631 window->ball.radius * 2 + 1);
632 }
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500633 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
634
635 if (window->display->compositor_version >= 2 &&
636 (window->transform != WL_OUTPUT_TRANSFORM_NORMAL ||
637 window->flags & WINDOW_FLAG_ROTATING_TRANSFORM))
638 wl_surface_set_buffer_transform(window->surface,
639 window->transform);
640
641 if (window->viewport)
642 wl_viewport_set_destination(window->viewport,
643 window->width,
644 window->height);
645
646 if (window->scale != 1)
647 wl_surface_set_buffer_scale(window->surface,
648 window->scale);
649
650 if (callback)
651 wl_callback_destroy(callback);
652
653 window->callback = wl_surface_frame(window->surface);
654 wl_callback_add_listener(window->callback, &frame_listener, window);
655 wl_surface_commit(window->surface);
656 buffer->busy = 1;
657}
658
659static const struct wl_callback_listener frame_listener = {
660 redraw
661};
662
663static void
664shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
665{
666 struct display *d = data;
667
668 d->formats |= (1 << format);
669}
670
671struct wl_shm_listener shm_listener = {
672 shm_format
673};
674
675static void
676xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)
677{
678 xdg_shell_pong(shell, serial);
679}
680
681static const struct xdg_shell_listener xdg_shell_listener = {
682 xdg_shell_ping,
683};
684
Jasper St. Pierre5ba1e1d2015-02-13 14:02:02 +0800685#define XDG_VERSION 5 /* The version of xdg-shell that we implement */
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500686#ifdef static_assert
687static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,
688 "Interface version doesn't match implementation version");
689#endif
690
691static void
692registry_handle_global(void *data, struct wl_registry *registry,
693 uint32_t id, const char *interface, uint32_t version)
694{
695 struct display *d = data;
696
697 if (strcmp(interface, "wl_compositor") == 0) {
698 if (d->compositor_version > (int)version) {
699 fprintf(stderr, "Compositor does not support "
700 "wl_surface version %d\n", d->compositor_version);
701 exit(1);
702 }
703
704 if (d->compositor_version < 0)
705 d->compositor_version = version;
706
707 d->compositor =
708 wl_registry_bind(registry,
709 id, &wl_compositor_interface,
710 d->compositor_version);
711 } else if (strcmp(interface, "wl_scaler") == 0 && version >= 2) {
712 d->scaler = wl_registry_bind(registry,
713 id, &wl_scaler_interface, 2);
714 } else if (strcmp(interface, "xdg_shell") == 0) {
715 d->shell = wl_registry_bind(registry,
716 id, &xdg_shell_interface, 1);
717 xdg_shell_use_unstable_version(d->shell, XDG_VERSION);
718 xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);
Jonas Ådahl496adb32015-11-17 16:00:27 +0800719 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500720 d->fshell = wl_registry_bind(registry,
Jonas Ådahl496adb32015-11-17 16:00:27 +0800721 id, &zwp_fullscreen_shell_v1_interface, 1);
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500722 } else if (strcmp(interface, "wl_shm") == 0) {
723 d->shm = wl_registry_bind(registry,
724 id, &wl_shm_interface, 1);
725 wl_shm_add_listener(d->shm, &shm_listener, d);
726 }
727}
728
729static void
730registry_handle_global_remove(void *data, struct wl_registry *registry,
731 uint32_t name)
732{
733}
734
735static const struct wl_registry_listener registry_listener = {
736 registry_handle_global,
737 registry_handle_global_remove
738};
739
740static struct display *
741create_display(int version)
742{
743 struct display *display;
744
745 display = malloc(sizeof *display);
746 if (display == NULL) {
747 fprintf(stderr, "out of memory\n");
748 exit(1);
749 }
750 display->display = wl_display_connect(NULL);
751 assert(display->display);
752
753 display->compositor_version = version;
754 display->formats = 0;
755 display->registry = wl_display_get_registry(display->display);
756 wl_registry_add_listener(display->registry,
757 &registry_listener, display);
758 wl_display_roundtrip(display->display);
759 if (display->shm == NULL) {
760 fprintf(stderr, "No wl_shm global\n");
761 exit(1);
762 }
763
764 wl_display_roundtrip(display->display);
765
766 if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {
767 fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");
768 exit(1);
769 }
770
771 return display;
772}
773
774static void
775destroy_display(struct display *display)
776{
777 if (display->shm)
778 wl_shm_destroy(display->shm);
779
780 if (display->shell)
781 xdg_shell_destroy(display->shell);
782
783 if (display->fshell)
Jonas Ådahl496adb32015-11-17 16:00:27 +0800784 zwp_fullscreen_shell_v1_release(display->fshell);
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500785
786 if (display->scaler)
787 wl_scaler_destroy(display->scaler);
788
789 if (display->compositor)
790 wl_compositor_destroy(display->compositor);
791
792 wl_registry_destroy(display->registry);
793 wl_display_flush(display->display);
794 wl_display_disconnect(display->display);
795 free(display);
796}
797
798static void
799signal_int(int signum)
800{
801 running = 0;
802}
803
804static void
805print_usage(int retval)
806{
807 printf(
808 "usage: weston-simple-damage [options]\n\n"
809 "options:\n"
810 " -h, --help\t\tPring this help\n"
811 " --verbose\t\tPrint verbose log information\n"
812 " --version=VERSION\tVersion of wl_surface to use\n"
813 " --width=WIDTH\t\tWidth of the window\n"
814 " --height=HEIGHT\tHeight of the window\n"
815 " --scale=SCALE\t\tScale factor for the surface\n"
816 " --transform=TRANSFORM\tTransform for the surface\n"
817 " --rotating-transform\tUse a different buffer_transform for each frame\n"
818 " --use-viewport\tUse wl_viewport\n"
Derek Foremanfb1e1262015-11-18 16:32:34 -0600819 " --use-damage-buffer\tUse damage_buffer to post damage\n"
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500820 );
821
822 exit(retval);
823}
824
825static int
826parse_transform(const char *str, enum wl_output_transform *transform)
827{
828 int i;
829 static const struct {
830 const char *name;
831 enum wl_output_transform transform;
832 } names[] = {
833 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
834 { "90", WL_OUTPUT_TRANSFORM_90 },
835 { "180", WL_OUTPUT_TRANSFORM_180 },
836 { "270", WL_OUTPUT_TRANSFORM_270 },
837 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
838 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
839 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
840 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
841 };
842
843 for (i = 0; i < 8; i++) {
844 if (strcmp(names[i].name, str) == 0) {
845 *transform = names[i].transform;
846 return 1;
847 }
848 }
849
850 return 0;
851}
852
853int
854main(int argc, char **argv)
855{
856 struct sigaction sigint;
857 struct display *display;
858 struct window *window;
859 int i, ret = 0;
860 int version = -1;
861 int width = 300, height = 200, scale = 1;
862 enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;
863 enum window_flags flags = 0;
864
865 for (i = 1; i < argc; ++i) {
866 if (strcmp(argv[i], "--help") == 0 ||
867 strcmp(argv[i], "-h") == 0) {
868 print_usage(0);
869 } else if (sscanf(argv[i], "--version=%d", &version) > 0) {
Derek Foremanfb1e1262015-11-18 16:32:34 -0600870 if (version < 1 || version > 4) {
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500871 fprintf(stderr, "Unsupported wl_surface version: %d\n",
872 version);
873 return 1;
874 }
875 continue;
876 } else if (strcmp(argv[i], "--verbose") == 0) {
877 print_debug = 1;
878 continue;
879 } else if (sscanf(argv[i], "--width=%d", &width) > 0) {
880 continue;
881 } else if (sscanf(argv[i], "--height=%d", &height) > 0) {
882 continue;
883 } else if (strncmp(argv[i], "--transform=", 12) == 0 &&
884 parse_transform(argv[i] + 12, &transform) > 0) {
885 continue;
886 } else if (strcmp(argv[i], "--rotating-transform") == 0) {
887 flags |= WINDOW_FLAG_ROTATING_TRANSFORM;
888 continue;
889 } else if (sscanf(argv[i], "--scale=%d", &scale) > 0) {
890 continue;
891 } else if (strcmp(argv[i], "--use-viewport") == 0) {
892 flags |= WINDOW_FLAG_USE_VIEWPORT;
893 continue;
Derek Foremanfb1e1262015-11-18 16:32:34 -0600894 } else if (strcmp(argv[i], "--use-damage-buffer") == 0) {
895 flags |= WINDOW_FLAG_USE_DAMAGE_BUFFER;
896 continue;
Jason Ekstrand549a53f2014-04-05 09:22:15 -0500897 } else {
898 printf("Invalid option: %s\n", argv[i]);
899 print_usage(255);
900 }
901 }
902
903 display = create_display(version);
904
905 window = create_window(display, width, height, transform, scale, flags);
906 if (!window)
907 return 1;
908
909 sigint.sa_handler = signal_int;
910 sigemptyset(&sigint.sa_mask);
911 sigint.sa_flags = SA_RESETHAND;
912 sigaction(SIGINT, &sigint, NULL);
913
914 redraw(window, NULL, 0);
915
916 while (running && ret != -1)
917 ret = wl_display_dispatch(display->display);
918
919 fprintf(stderr, "simple-shm exiting\n");
920 destroy_window(window);
921 destroy_display(display);
922
923 return 0;
924}