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