Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2014 Jason Ekstrand |
| 3 | * Copyright © 2011 Benjamin Franzke |
| 4 | * Copyright © 2010 Intel Corporation |
| 5 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 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: |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 12 | * |
Bryce Harrington | 1f6b0d1 | 2015-06-10 22:48:59 -0700 | [diff] [blame] | 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. |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 24 | */ |
| 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 Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 39 | #include "shared/os-compatibility.h" |
Jonas Ådahl | 2a22933 | 2015-11-17 16:00:32 +0800 | [diff] [blame] | 40 | #include "xdg-shell-unstable-v5-client-protocol.h" |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 41 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 42 | #include "scaler-client-protocol.h" |
| 43 | |
| 44 | int print_debug = 0; |
| 45 | |
| 46 | struct 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 Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 53 | struct zwp_fullscreen_shell_v1 *fshell; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 54 | struct wl_shm *shm; |
| 55 | uint32_t formats; |
| 56 | }; |
| 57 | |
| 58 | struct buffer { |
| 59 | struct wl_buffer *buffer; |
| 60 | uint32_t *shm_data; |
| 61 | int busy; |
| 62 | }; |
| 63 | |
| 64 | enum window_flags { |
| 65 | WINDOW_FLAG_USE_VIEWPORT = 0x1, |
| 66 | WINDOW_FLAG_ROTATING_TRANSFORM = 0x2, |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 67 | WINDOW_FLAG_USE_DAMAGE_BUFFER = 0x4, |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct 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 | |
| 92 | static int running = 1; |
| 93 | |
| 94 | static void |
| 95 | buffer_release(void *data, struct wl_buffer *buffer) |
| 96 | { |
| 97 | struct buffer *mybuf = data; |
| 98 | |
| 99 | mybuf->busy = 0; |
| 100 | } |
| 101 | |
| 102 | static const struct wl_buffer_listener buffer_listener = { |
| 103 | buffer_release |
| 104 | }; |
| 105 | |
| 106 | static int |
| 107 | create_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 | |
| 144 | static void |
| 145 | handle_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 | |
| 151 | static void |
| 152 | handle_close(void *data, struct xdg_surface *xdg_surface) |
| 153 | { |
| 154 | running = 0; |
| 155 | } |
| 156 | |
| 157 | static const struct xdg_surface_listener xdg_surface_listener = { |
| 158 | handle_configure, |
| 159 | handle_close, |
| 160 | }; |
| 161 | |
| 162 | static float |
| 163 | bounded_randf(float a, float b) |
| 164 | { |
| 165 | return a + ((float)rand() / (float)RAND_MAX) * (b - a); |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | window_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 | |
| 193 | static void |
| 194 | window_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 | |
| 236 | static struct window * |
| 237 | create_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 | |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 265 | if (display->compositor_version < 4 && |
| 266 | (flags & WINDOW_FLAG_USE_DAMAGE_BUFFER)) { |
| 267 | fprintf(stderr, "wl_surface.damage_buffer unsupported in " |
| 268 | "wl_surface version %d\n", |
| 269 | display->compositor_version); |
| 270 | exit(1); |
| 271 | } |
| 272 | |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 273 | window = calloc(1, sizeof *window); |
| 274 | if (!window) |
| 275 | return NULL; |
| 276 | |
| 277 | window->callback = NULL; |
| 278 | window->display = display; |
| 279 | window->width = width; |
| 280 | window->height = height; |
| 281 | window->border = 10; |
| 282 | window->flags = flags; |
| 283 | window->transform = transform; |
| 284 | window->scale = scale; |
| 285 | |
| 286 | window_init_game(window); |
| 287 | |
| 288 | window->surface = wl_compositor_create_surface(display->compositor); |
| 289 | |
| 290 | if (window->flags & WINDOW_FLAG_USE_VIEWPORT) |
| 291 | window->viewport = wl_scaler_get_viewport(display->scaler, |
| 292 | window->surface); |
| 293 | |
| 294 | if (display->shell) { |
| 295 | window->xdg_surface = |
| 296 | xdg_shell_get_xdg_surface(display->shell, |
| 297 | window->surface); |
| 298 | |
| 299 | assert(window->xdg_surface); |
| 300 | |
| 301 | xdg_surface_add_listener(window->xdg_surface, |
| 302 | &xdg_surface_listener, window); |
| 303 | |
| 304 | xdg_surface_set_title(window->xdg_surface, "simple-damage"); |
| 305 | } else if (display->fshell) { |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 306 | zwp_fullscreen_shell_v1_present_surface(display->fshell, |
| 307 | window->surface, |
| 308 | ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT, |
| 309 | NULL); |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 310 | } else { |
| 311 | assert(0); |
| 312 | } |
| 313 | |
| 314 | /* Initialise damage to full surface, so the padding gets painted */ |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 315 | if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) { |
| 316 | wl_surface_damage_buffer(window->surface, 0, 0, |
| 317 | INT32_MAX, INT32_MAX); |
| 318 | } else { |
| 319 | wl_surface_damage(window->surface, 0, 0, INT32_MAX, INT32_MAX); |
| 320 | } |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 321 | return window; |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | destroy_window(struct window *window) |
| 326 | { |
| 327 | if (window->callback) |
| 328 | wl_callback_destroy(window->callback); |
| 329 | |
| 330 | if (window->buffers[0].buffer) |
| 331 | wl_buffer_destroy(window->buffers[0].buffer); |
| 332 | if (window->buffers[1].buffer) |
| 333 | wl_buffer_destroy(window->buffers[1].buffer); |
| 334 | |
| 335 | if (window->xdg_surface) |
| 336 | xdg_surface_destroy(window->xdg_surface); |
| 337 | if (window->viewport) |
| 338 | wl_viewport_destroy(window->viewport); |
| 339 | wl_surface_destroy(window->surface); |
| 340 | free(window); |
| 341 | } |
| 342 | |
| 343 | static struct buffer * |
| 344 | window_next_buffer(struct window *window) |
| 345 | { |
| 346 | struct buffer *buffer; |
| 347 | int ret = 0, bwidth, bheight; |
| 348 | |
| 349 | if (!window->buffers[0].busy) |
| 350 | buffer = &window->buffers[0]; |
| 351 | else if (!window->buffers[1].busy) |
| 352 | buffer = &window->buffers[1]; |
| 353 | else |
| 354 | return NULL; |
| 355 | |
| 356 | switch (window->transform) { |
| 357 | default: |
| 358 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 359 | case WL_OUTPUT_TRANSFORM_180: |
| 360 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 361 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 362 | bwidth = window->width * window->scale; |
| 363 | bheight = window->height * window->scale; |
| 364 | break; |
| 365 | case WL_OUTPUT_TRANSFORM_90: |
| 366 | case WL_OUTPUT_TRANSFORM_270: |
| 367 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 368 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 369 | bwidth = window->height * window->scale; |
| 370 | bheight = window->width * window->scale; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | if (!buffer->buffer) { |
| 375 | ret = create_shm_buffer(window->display, buffer, |
| 376 | bwidth, bheight, |
| 377 | WL_SHM_FORMAT_ARGB8888); |
| 378 | |
| 379 | if (ret < 0) |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | return buffer; |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | paint_box(uint32_t *pixels, int pitch, int x, int y, int width, int height, |
| 388 | uint32_t color) |
| 389 | { |
| 390 | int i, j; |
| 391 | |
| 392 | for (j = y; j < y + height; ++j) |
| 393 | for (i = x; i < x + width; ++i) |
| 394 | pixels[i + j * pitch] = color; |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | paint_circle(uint32_t *pixels, int pitch, float x, float y, int radius, |
| 399 | uint32_t color) |
| 400 | { |
| 401 | int i, j; |
| 402 | |
| 403 | for (j = y - radius; j <= (int)(y + radius); ++j) |
| 404 | for (i = x - radius; i <= (int)(x + radius); ++i) |
| 405 | if ((j+0.5f-y)*(j+0.5f-y) + (i+0.5f-x)*(i+0.5f-x) <= radius * radius) |
| 406 | pixels[i + j * pitch] = color; |
| 407 | } |
| 408 | |
| 409 | static void |
| 410 | window_get_transformed_ball(struct window *window, float *bx, float *by) |
| 411 | { |
| 412 | float wx, wy; |
| 413 | |
| 414 | wx = window->ball.x; |
| 415 | wy = window->ball.y; |
| 416 | |
| 417 | switch (window->transform) { |
| 418 | default: |
| 419 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 420 | *bx = wx; |
| 421 | *by = wy; |
| 422 | break; |
| 423 | case WL_OUTPUT_TRANSFORM_90: |
| 424 | *bx = window->height - wy; |
| 425 | *by = wx; |
| 426 | break; |
| 427 | case WL_OUTPUT_TRANSFORM_180: |
| 428 | *bx = window->width - wx; |
| 429 | *by = window->height - wy; |
| 430 | break; |
| 431 | case WL_OUTPUT_TRANSFORM_270: |
| 432 | *bx = wy; |
| 433 | *by = window->width - wx; |
| 434 | break; |
| 435 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 436 | *bx = window->width - wx; |
| 437 | *by = wy; |
| 438 | break; |
| 439 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 440 | *bx = window->height - wy; |
| 441 | *by = window->width - wx; |
| 442 | break; |
| 443 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 444 | *bx = wx; |
| 445 | *by = window->height - wy; |
| 446 | break; |
| 447 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 448 | *bx = wy; |
| 449 | *by = wx; |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | *bx *= window->scale; |
| 454 | *by *= window->scale; |
| 455 | |
| 456 | if (window->viewport) { |
| 457 | /* We're drawing half-size because of the viewport */ |
| 458 | *bx /= 2; |
| 459 | *by /= 2; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | static const struct wl_callback_listener frame_listener; |
| 464 | |
| 465 | static void |
| 466 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
| 467 | { |
| 468 | struct window *window = data; |
| 469 | struct buffer *buffer; |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 470 | int off_x = 0, off_y = 0; |
| 471 | int bwidth, bheight, bborder, bpitch, bradius; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 472 | float bx, by; |
| 473 | |
| 474 | buffer = window_next_buffer(window); |
| 475 | if (!buffer) { |
| 476 | fprintf(stderr, |
| 477 | !callback ? "Failed to create the first buffer.\n" : |
| 478 | "Both buffers busy at redraw(). Server bug?\n"); |
| 479 | abort(); |
| 480 | } |
| 481 | |
| 482 | /* Rotate the damage, but keep the even/odd parity so the |
| 483 | * dimensions of the buffers don't change */ |
| 484 | if (window->flags & WINDOW_FLAG_ROTATING_TRANSFORM) |
| 485 | window->transform = (window->transform + 2) % 8; |
| 486 | |
| 487 | switch (window->transform) { |
| 488 | default: |
| 489 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 490 | case WL_OUTPUT_TRANSFORM_180: |
| 491 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 492 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 493 | bwidth = window->width * window->scale; |
| 494 | bheight = window->height * window->scale; |
| 495 | break; |
| 496 | case WL_OUTPUT_TRANSFORM_90: |
| 497 | case WL_OUTPUT_TRANSFORM_270: |
| 498 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 499 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 500 | bwidth = window->height * window->scale; |
| 501 | bheight = window->width * window->scale; |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | bpitch = bwidth; |
| 506 | |
| 507 | bborder = window->border * window->scale; |
| 508 | bradius = window->ball.radius * window->scale; |
| 509 | |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 510 | if (window->viewport) { |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 511 | int tx, ty; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 512 | /* Fill the whole thing with red to detect viewport errors */ |
| 513 | paint_box(buffer->shm_data, bpitch, 0, 0, bwidth, bheight, |
| 514 | 0xffff0000); |
| 515 | |
| 516 | /* The buffer is the same size. However, we crop it |
| 517 | * and scale it up by a factor of 2 */ |
| 518 | bborder /= 2; |
| 519 | bradius /= 2; |
| 520 | bwidth /= 2; |
| 521 | bheight /= 2; |
| 522 | |
| 523 | /* Offset the drawing region */ |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 524 | tx = (window->width / 3) * window->scale; |
| 525 | ty = (window->height / 5) * window->scale; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 526 | switch (window->transform) { |
| 527 | default: |
| 528 | case WL_OUTPUT_TRANSFORM_NORMAL: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 529 | off_y = ty; |
| 530 | off_x = tx; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 531 | break; |
| 532 | case WL_OUTPUT_TRANSFORM_90: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 533 | off_y = tx; |
| 534 | off_x = bwidth - ty; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 535 | break; |
| 536 | case WL_OUTPUT_TRANSFORM_180: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 537 | off_y = bheight - ty; |
| 538 | off_x = bwidth - tx; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 539 | break; |
| 540 | case WL_OUTPUT_TRANSFORM_270: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 541 | off_y = bheight - tx; |
| 542 | off_x = ty; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 543 | break; |
| 544 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 545 | off_y = ty; |
| 546 | off_x = bwidth - tx; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 547 | break; |
| 548 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 549 | off_y = bheight - tx; |
| 550 | off_x = bwidth - ty; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 551 | break; |
| 552 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 553 | off_y = bheight - ty; |
| 554 | off_x = tx; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 555 | break; |
| 556 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 557 | off_y = tx; |
| 558 | off_x = ty; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | wl_viewport_set_source(window->viewport, |
| 562 | wl_fixed_from_int(window->width / 3), |
| 563 | wl_fixed_from_int(window->height / 5), |
| 564 | wl_fixed_from_int(window->width / 2), |
| 565 | wl_fixed_from_int(window->height / 2)); |
| 566 | } |
| 567 | |
| 568 | /* Paint the border */ |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 569 | paint_box(buffer->shm_data, bpitch, off_x, off_y, |
| 570 | bwidth, bborder, 0xffffffff); |
| 571 | paint_box(buffer->shm_data, bpitch, off_x, off_y, |
| 572 | bborder, bheight, 0xffffffff); |
| 573 | paint_box(buffer->shm_data, bpitch, off_x + bwidth - bborder, off_y, |
| 574 | bborder, bheight, 0xffffffff); |
| 575 | paint_box(buffer->shm_data, bpitch, off_x, off_y + bheight - bborder, |
| 576 | bwidth, bborder, 0xffffffff); |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 577 | |
| 578 | /* fill with translucent */ |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 579 | paint_box(buffer->shm_data, bpitch, off_x + bborder, off_y + bborder, |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 580 | bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000); |
| 581 | |
| 582 | /* Damage where the ball was */ |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 583 | if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) { |
| 584 | window_get_transformed_ball(window, &bx, &by); |
| 585 | wl_surface_damage_buffer(window->surface, |
| 586 | bx - bradius + off_x, |
| 587 | by - bradius + off_y, |
| 588 | bradius * 2 + 1, |
| 589 | bradius * 2 + 1); |
| 590 | } else { |
| 591 | wl_surface_damage(window->surface, |
| 592 | window->ball.x - window->ball.radius, |
| 593 | window->ball.y - window->ball.radius, |
| 594 | window->ball.radius * 2 + 1, |
| 595 | window->ball.radius * 2 + 1); |
| 596 | } |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 597 | window_advance_game(window, time); |
| 598 | |
| 599 | window_get_transformed_ball(window, &bx, &by); |
| 600 | |
| 601 | /* Paint the ball */ |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 602 | paint_circle(buffer->shm_data, bpitch, off_x + bx, off_y + by, |
| 603 | bradius, 0xff00ff00); |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 604 | |
| 605 | if (print_debug) { |
| 606 | printf("Ball now located at (%f, %f)\n", |
| 607 | window->ball.x, window->ball.y); |
| 608 | |
| 609 | printf("Circle painted at (%f, %f), radius %d\n", bx, by, |
| 610 | bradius); |
| 611 | |
| 612 | printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n", |
Derek Foreman | 29b846e | 2015-11-18 16:32:33 -0600 | [diff] [blame] | 613 | (int)(bx - bradius) + off_x, |
| 614 | (int)(by - bradius) + off_y, |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 615 | bradius * 2 + 1, bradius * 2 + 1); |
| 616 | } |
| 617 | |
| 618 | /* Damage where the ball is now */ |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 619 | if (window->flags & WINDOW_FLAG_USE_DAMAGE_BUFFER) { |
| 620 | wl_surface_damage_buffer(window->surface, |
| 621 | bx - bradius + off_x, |
| 622 | by - bradius + off_y, |
| 623 | bradius * 2 + 1, |
| 624 | bradius * 2 + 1); |
| 625 | } else { |
| 626 | wl_surface_damage(window->surface, |
| 627 | window->ball.x - window->ball.radius, |
| 628 | window->ball.y - window->ball.radius, |
| 629 | window->ball.radius * 2 + 1, |
| 630 | window->ball.radius * 2 + 1); |
| 631 | } |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 632 | wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
| 633 | |
| 634 | if (window->display->compositor_version >= 2 && |
| 635 | (window->transform != WL_OUTPUT_TRANSFORM_NORMAL || |
| 636 | window->flags & WINDOW_FLAG_ROTATING_TRANSFORM)) |
| 637 | wl_surface_set_buffer_transform(window->surface, |
| 638 | window->transform); |
| 639 | |
| 640 | if (window->viewport) |
| 641 | wl_viewport_set_destination(window->viewport, |
| 642 | window->width, |
| 643 | window->height); |
| 644 | |
| 645 | if (window->scale != 1) |
| 646 | wl_surface_set_buffer_scale(window->surface, |
| 647 | window->scale); |
| 648 | |
| 649 | if (callback) |
| 650 | wl_callback_destroy(callback); |
| 651 | |
| 652 | window->callback = wl_surface_frame(window->surface); |
| 653 | wl_callback_add_listener(window->callback, &frame_listener, window); |
| 654 | wl_surface_commit(window->surface); |
| 655 | buffer->busy = 1; |
| 656 | } |
| 657 | |
| 658 | static const struct wl_callback_listener frame_listener = { |
| 659 | redraw |
| 660 | }; |
| 661 | |
| 662 | static void |
| 663 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 664 | { |
| 665 | struct display *d = data; |
| 666 | |
| 667 | d->formats |= (1 << format); |
| 668 | } |
| 669 | |
| 670 | struct wl_shm_listener shm_listener = { |
| 671 | shm_format |
| 672 | }; |
| 673 | |
| 674 | static void |
| 675 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 676 | { |
| 677 | xdg_shell_pong(shell, serial); |
| 678 | } |
| 679 | |
| 680 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 681 | xdg_shell_ping, |
| 682 | }; |
| 683 | |
Jasper St. Pierre | 5ba1e1d | 2015-02-13 14:02:02 +0800 | [diff] [blame] | 684 | #define XDG_VERSION 5 /* The version of xdg-shell that we implement */ |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 685 | #ifdef static_assert |
| 686 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 687 | "Interface version doesn't match implementation version"); |
| 688 | #endif |
| 689 | |
| 690 | static void |
| 691 | registry_handle_global(void *data, struct wl_registry *registry, |
| 692 | uint32_t id, const char *interface, uint32_t version) |
| 693 | { |
| 694 | struct display *d = data; |
| 695 | |
| 696 | if (strcmp(interface, "wl_compositor") == 0) { |
| 697 | if (d->compositor_version > (int)version) { |
| 698 | fprintf(stderr, "Compositor does not support " |
| 699 | "wl_surface version %d\n", d->compositor_version); |
| 700 | exit(1); |
| 701 | } |
| 702 | |
| 703 | if (d->compositor_version < 0) |
| 704 | d->compositor_version = version; |
| 705 | |
| 706 | d->compositor = |
| 707 | wl_registry_bind(registry, |
| 708 | id, &wl_compositor_interface, |
| 709 | d->compositor_version); |
| 710 | } else if (strcmp(interface, "wl_scaler") == 0 && version >= 2) { |
| 711 | d->scaler = wl_registry_bind(registry, |
| 712 | id, &wl_scaler_interface, 2); |
| 713 | } else if (strcmp(interface, "xdg_shell") == 0) { |
| 714 | d->shell = wl_registry_bind(registry, |
| 715 | id, &xdg_shell_interface, 1); |
| 716 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
| 717 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 718 | } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) { |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 719 | d->fshell = wl_registry_bind(registry, |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 720 | id, &zwp_fullscreen_shell_v1_interface, 1); |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 721 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 722 | d->shm = wl_registry_bind(registry, |
| 723 | id, &wl_shm_interface, 1); |
| 724 | wl_shm_add_listener(d->shm, &shm_listener, d); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | static void |
| 729 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 730 | uint32_t name) |
| 731 | { |
| 732 | } |
| 733 | |
| 734 | static const struct wl_registry_listener registry_listener = { |
| 735 | registry_handle_global, |
| 736 | registry_handle_global_remove |
| 737 | }; |
| 738 | |
| 739 | static struct display * |
| 740 | create_display(int version) |
| 741 | { |
| 742 | struct display *display; |
| 743 | |
| 744 | display = malloc(sizeof *display); |
| 745 | if (display == NULL) { |
| 746 | fprintf(stderr, "out of memory\n"); |
| 747 | exit(1); |
| 748 | } |
| 749 | display->display = wl_display_connect(NULL); |
| 750 | assert(display->display); |
| 751 | |
| 752 | display->compositor_version = version; |
| 753 | display->formats = 0; |
| 754 | display->registry = wl_display_get_registry(display->display); |
| 755 | wl_registry_add_listener(display->registry, |
| 756 | ®istry_listener, display); |
| 757 | wl_display_roundtrip(display->display); |
| 758 | if (display->shm == NULL) { |
| 759 | fprintf(stderr, "No wl_shm global\n"); |
| 760 | exit(1); |
| 761 | } |
| 762 | |
| 763 | wl_display_roundtrip(display->display); |
| 764 | |
| 765 | if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) { |
| 766 | fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n"); |
| 767 | exit(1); |
| 768 | } |
| 769 | |
| 770 | return display; |
| 771 | } |
| 772 | |
| 773 | static void |
| 774 | destroy_display(struct display *display) |
| 775 | { |
| 776 | if (display->shm) |
| 777 | wl_shm_destroy(display->shm); |
| 778 | |
| 779 | if (display->shell) |
| 780 | xdg_shell_destroy(display->shell); |
| 781 | |
| 782 | if (display->fshell) |
Jonas Ådahl | 496adb3 | 2015-11-17 16:00:27 +0800 | [diff] [blame] | 783 | zwp_fullscreen_shell_v1_release(display->fshell); |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 784 | |
| 785 | if (display->scaler) |
| 786 | wl_scaler_destroy(display->scaler); |
| 787 | |
| 788 | if (display->compositor) |
| 789 | wl_compositor_destroy(display->compositor); |
| 790 | |
| 791 | wl_registry_destroy(display->registry); |
| 792 | wl_display_flush(display->display); |
| 793 | wl_display_disconnect(display->display); |
| 794 | free(display); |
| 795 | } |
| 796 | |
| 797 | static void |
| 798 | signal_int(int signum) |
| 799 | { |
| 800 | running = 0; |
| 801 | } |
| 802 | |
| 803 | static void |
| 804 | print_usage(int retval) |
| 805 | { |
| 806 | printf( |
| 807 | "usage: weston-simple-damage [options]\n\n" |
| 808 | "options:\n" |
| 809 | " -h, --help\t\tPring this help\n" |
| 810 | " --verbose\t\tPrint verbose log information\n" |
| 811 | " --version=VERSION\tVersion of wl_surface to use\n" |
| 812 | " --width=WIDTH\t\tWidth of the window\n" |
| 813 | " --height=HEIGHT\tHeight of the window\n" |
| 814 | " --scale=SCALE\t\tScale factor for the surface\n" |
| 815 | " --transform=TRANSFORM\tTransform for the surface\n" |
| 816 | " --rotating-transform\tUse a different buffer_transform for each frame\n" |
| 817 | " --use-viewport\tUse wl_viewport\n" |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 818 | " --use-damage-buffer\tUse damage_buffer to post damage\n" |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 819 | ); |
| 820 | |
| 821 | exit(retval); |
| 822 | } |
| 823 | |
| 824 | static int |
| 825 | parse_transform(const char *str, enum wl_output_transform *transform) |
| 826 | { |
| 827 | int i; |
| 828 | static const struct { |
| 829 | const char *name; |
| 830 | enum wl_output_transform transform; |
| 831 | } names[] = { |
| 832 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 833 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 834 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 835 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 836 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 837 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 838 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 839 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 840 | }; |
| 841 | |
| 842 | for (i = 0; i < 8; i++) { |
| 843 | if (strcmp(names[i].name, str) == 0) { |
| 844 | *transform = names[i].transform; |
| 845 | return 1; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | int |
| 853 | main(int argc, char **argv) |
| 854 | { |
| 855 | struct sigaction sigint; |
| 856 | struct display *display; |
| 857 | struct window *window; |
| 858 | int i, ret = 0; |
| 859 | int version = -1; |
| 860 | int width = 300, height = 200, scale = 1; |
| 861 | enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 862 | enum window_flags flags = 0; |
| 863 | |
| 864 | for (i = 1; i < argc; ++i) { |
| 865 | if (strcmp(argv[i], "--help") == 0 || |
| 866 | strcmp(argv[i], "-h") == 0) { |
| 867 | print_usage(0); |
| 868 | } else if (sscanf(argv[i], "--version=%d", &version) > 0) { |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 869 | if (version < 1 || version > 4) { |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 870 | fprintf(stderr, "Unsupported wl_surface version: %d\n", |
| 871 | version); |
| 872 | return 1; |
| 873 | } |
| 874 | continue; |
| 875 | } else if (strcmp(argv[i], "--verbose") == 0) { |
| 876 | print_debug = 1; |
| 877 | continue; |
| 878 | } else if (sscanf(argv[i], "--width=%d", &width) > 0) { |
| 879 | continue; |
| 880 | } else if (sscanf(argv[i], "--height=%d", &height) > 0) { |
| 881 | continue; |
| 882 | } else if (strncmp(argv[i], "--transform=", 12) == 0 && |
| 883 | parse_transform(argv[i] + 12, &transform) > 0) { |
| 884 | continue; |
| 885 | } else if (strcmp(argv[i], "--rotating-transform") == 0) { |
| 886 | flags |= WINDOW_FLAG_ROTATING_TRANSFORM; |
| 887 | continue; |
| 888 | } else if (sscanf(argv[i], "--scale=%d", &scale) > 0) { |
| 889 | continue; |
| 890 | } else if (strcmp(argv[i], "--use-viewport") == 0) { |
| 891 | flags |= WINDOW_FLAG_USE_VIEWPORT; |
| 892 | continue; |
Derek Foreman | fb1e126 | 2015-11-18 16:32:34 -0600 | [diff] [blame^] | 893 | } else if (strcmp(argv[i], "--use-damage-buffer") == 0) { |
| 894 | flags |= WINDOW_FLAG_USE_DAMAGE_BUFFER; |
| 895 | continue; |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 896 | } else { |
| 897 | printf("Invalid option: %s\n", argv[i]); |
| 898 | print_usage(255); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | display = create_display(version); |
| 903 | |
| 904 | window = create_window(display, width, height, transform, scale, flags); |
| 905 | if (!window) |
| 906 | return 1; |
| 907 | |
| 908 | sigint.sa_handler = signal_int; |
| 909 | sigemptyset(&sigint.sa_mask); |
| 910 | sigint.sa_flags = SA_RESETHAND; |
| 911 | sigaction(SIGINT, &sigint, NULL); |
| 912 | |
| 913 | redraw(window, NULL, 0); |
| 914 | |
| 915 | while (running && ret != -1) |
| 916 | ret = wl_display_dispatch(display->display); |
| 917 | |
| 918 | fprintf(stderr, "simple-shm exiting\n"); |
| 919 | destroy_window(window); |
| 920 | destroy_display(display); |
| 921 | |
| 922 | return 0; |
| 923 | } |