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