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