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> |
| 39 | #include "../shared/os-compatibility.h" |
| 40 | #include "xdg-shell-client-protocol.h" |
| 41 | #include "fullscreen-shell-client-protocol.h" |
| 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; |
| 53 | struct _wl_fullscreen_shell *fshell; |
| 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, |
| 67 | }; |
| 68 | |
| 69 | struct 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 | |
| 91 | static int running = 1; |
| 92 | |
| 93 | static void |
| 94 | buffer_release(void *data, struct wl_buffer *buffer) |
| 95 | { |
| 96 | struct buffer *mybuf = data; |
| 97 | |
| 98 | mybuf->busy = 0; |
| 99 | } |
| 100 | |
| 101 | static const struct wl_buffer_listener buffer_listener = { |
| 102 | buffer_release |
| 103 | }; |
| 104 | |
| 105 | static int |
| 106 | create_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 | |
| 143 | static void |
| 144 | handle_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 | |
| 150 | static void |
| 151 | handle_close(void *data, struct xdg_surface *xdg_surface) |
| 152 | { |
| 153 | running = 0; |
| 154 | } |
| 155 | |
| 156 | static const struct xdg_surface_listener xdg_surface_listener = { |
| 157 | handle_configure, |
| 158 | handle_close, |
| 159 | }; |
| 160 | |
| 161 | static float |
| 162 | bounded_randf(float a, float b) |
| 163 | { |
| 164 | return a + ((float)rand() / (float)RAND_MAX) * (b - a); |
| 165 | } |
| 166 | |
| 167 | static void |
| 168 | window_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 | |
| 192 | static void |
| 193 | window_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 | |
| 235 | static struct window * |
| 236 | create_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) { |
| 297 | _wl_fullscreen_shell_present_surface(display->fshell, |
| 298 | window->surface, |
| 299 | _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT, |
| 300 | NULL); |
| 301 | } 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 | |
| 311 | static void |
| 312 | destroy_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 | |
| 330 | static struct buffer * |
| 331 | window_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 | |
| 373 | static void |
| 374 | paint_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 | |
| 384 | static void |
| 385 | paint_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 | |
| 396 | static void |
| 397 | window_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 | |
| 450 | static const struct wl_callback_listener frame_listener; |
| 451 | |
| 452 | static void |
| 453 | redraw(void *data, struct wl_callback *callback, uint32_t time) |
| 454 | { |
| 455 | struct window *window = data; |
| 456 | struct buffer *buffer; |
| 457 | int off_x, off_y, bwidth, bheight, bborder, bpitch, bradius; |
| 458 | uint32_t *buffer_data; |
| 459 | 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 | |
| 497 | buffer_data = buffer->shm_data; |
| 498 | if (window->viewport) { |
| 499 | /* 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 */ |
| 511 | off_x = (window->width / 3) * window->scale; |
| 512 | off_y = (window->height / 5) * window->scale; |
| 513 | switch (window->transform) { |
| 514 | default: |
| 515 | case WL_OUTPUT_TRANSFORM_NORMAL: |
| 516 | buffer_data += off_y * bpitch + off_x; |
| 517 | break; |
| 518 | case WL_OUTPUT_TRANSFORM_90: |
| 519 | buffer_data += off_x * bpitch + (bwidth - off_y); |
| 520 | break; |
| 521 | case WL_OUTPUT_TRANSFORM_180: |
| 522 | buffer_data += (bheight - off_y) * bpitch + |
| 523 | (bwidth - off_x); |
| 524 | break; |
| 525 | case WL_OUTPUT_TRANSFORM_270: |
| 526 | buffer_data += (bheight - off_x) * bpitch + off_y; |
| 527 | break; |
| 528 | case WL_OUTPUT_TRANSFORM_FLIPPED: |
| 529 | buffer_data += off_y * bpitch + (bwidth - off_x); |
| 530 | break; |
| 531 | case WL_OUTPUT_TRANSFORM_FLIPPED_90: |
| 532 | buffer_data += (bheight - off_x) * bpitch + |
| 533 | (bwidth - off_y); |
| 534 | break; |
| 535 | case WL_OUTPUT_TRANSFORM_FLIPPED_180: |
| 536 | buffer_data += (bheight - off_y) * bpitch + off_x; |
| 537 | break; |
| 538 | case WL_OUTPUT_TRANSFORM_FLIPPED_270: |
| 539 | buffer_data += off_x * bpitch + off_y; |
| 540 | break; |
| 541 | } |
| 542 | wl_viewport_set_source(window->viewport, |
| 543 | wl_fixed_from_int(window->width / 3), |
| 544 | wl_fixed_from_int(window->height / 5), |
| 545 | wl_fixed_from_int(window->width / 2), |
| 546 | wl_fixed_from_int(window->height / 2)); |
| 547 | } |
| 548 | |
| 549 | /* Paint the border */ |
| 550 | paint_box(buffer_data, bpitch, 0, 0, bwidth, bborder, 0xffffffff); |
| 551 | paint_box(buffer_data, bpitch, 0, 0, bborder, bheight, 0xffffffff); |
| 552 | paint_box(buffer_data, bpitch, |
| 553 | bwidth - bborder, 0, bborder, bheight, 0xffffffff); |
| 554 | paint_box(buffer_data, bpitch, |
| 555 | 0, bheight - bborder, bwidth, bborder, 0xffffffff); |
| 556 | |
| 557 | /* fill with translucent */ |
| 558 | paint_box(buffer_data, bpitch, bborder, bborder, |
| 559 | bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000); |
| 560 | |
| 561 | /* Damage where the ball was */ |
| 562 | wl_surface_damage(window->surface, |
| 563 | window->ball.x - window->ball.radius, |
| 564 | window->ball.y - window->ball.radius, |
| 565 | window->ball.radius * 2 + 1, |
| 566 | window->ball.radius * 2 + 1); |
| 567 | |
| 568 | window_advance_game(window, time); |
| 569 | |
| 570 | window_get_transformed_ball(window, &bx, &by); |
| 571 | |
| 572 | /* Paint the ball */ |
| 573 | paint_circle(buffer_data, bpitch, bx, by, bradius, 0xff00ff00); |
| 574 | |
| 575 | if (print_debug) { |
| 576 | printf("Ball now located at (%f, %f)\n", |
| 577 | window->ball.x, window->ball.y); |
| 578 | |
| 579 | printf("Circle painted at (%f, %f), radius %d\n", bx, by, |
| 580 | bradius); |
| 581 | |
| 582 | printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n", |
| 583 | (int)(bx - bradius), (int)(by - bradius), |
| 584 | bradius * 2 + 1, bradius * 2 + 1); |
| 585 | } |
| 586 | |
| 587 | /* Damage where the ball is now */ |
| 588 | wl_surface_damage(window->surface, |
| 589 | window->ball.x - window->ball.radius, |
| 590 | window->ball.y - window->ball.radius, |
| 591 | window->ball.radius * 2 + 1, |
| 592 | window->ball.radius * 2 + 1); |
| 593 | |
| 594 | wl_surface_attach(window->surface, buffer->buffer, 0, 0); |
| 595 | |
| 596 | if (window->display->compositor_version >= 2 && |
| 597 | (window->transform != WL_OUTPUT_TRANSFORM_NORMAL || |
| 598 | window->flags & WINDOW_FLAG_ROTATING_TRANSFORM)) |
| 599 | wl_surface_set_buffer_transform(window->surface, |
| 600 | window->transform); |
| 601 | |
| 602 | if (window->viewport) |
| 603 | wl_viewport_set_destination(window->viewport, |
| 604 | window->width, |
| 605 | window->height); |
| 606 | |
| 607 | if (window->scale != 1) |
| 608 | wl_surface_set_buffer_scale(window->surface, |
| 609 | window->scale); |
| 610 | |
| 611 | if (callback) |
| 612 | wl_callback_destroy(callback); |
| 613 | |
| 614 | window->callback = wl_surface_frame(window->surface); |
| 615 | wl_callback_add_listener(window->callback, &frame_listener, window); |
| 616 | wl_surface_commit(window->surface); |
| 617 | buffer->busy = 1; |
| 618 | } |
| 619 | |
| 620 | static const struct wl_callback_listener frame_listener = { |
| 621 | redraw |
| 622 | }; |
| 623 | |
| 624 | static void |
| 625 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 626 | { |
| 627 | struct display *d = data; |
| 628 | |
| 629 | d->formats |= (1 << format); |
| 630 | } |
| 631 | |
| 632 | struct wl_shm_listener shm_listener = { |
| 633 | shm_format |
| 634 | }; |
| 635 | |
| 636 | static void |
| 637 | xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial) |
| 638 | { |
| 639 | xdg_shell_pong(shell, serial); |
| 640 | } |
| 641 | |
| 642 | static const struct xdg_shell_listener xdg_shell_listener = { |
| 643 | xdg_shell_ping, |
| 644 | }; |
| 645 | |
Jasper St. Pierre | 5ba1e1d | 2015-02-13 14:02:02 +0800 | [diff] [blame] | 646 | #define XDG_VERSION 5 /* The version of xdg-shell that we implement */ |
Jason Ekstrand | 549a53f | 2014-04-05 09:22:15 -0500 | [diff] [blame] | 647 | #ifdef static_assert |
| 648 | static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT, |
| 649 | "Interface version doesn't match implementation version"); |
| 650 | #endif |
| 651 | |
| 652 | static void |
| 653 | registry_handle_global(void *data, struct wl_registry *registry, |
| 654 | uint32_t id, const char *interface, uint32_t version) |
| 655 | { |
| 656 | struct display *d = data; |
| 657 | |
| 658 | if (strcmp(interface, "wl_compositor") == 0) { |
| 659 | if (d->compositor_version > (int)version) { |
| 660 | fprintf(stderr, "Compositor does not support " |
| 661 | "wl_surface version %d\n", d->compositor_version); |
| 662 | exit(1); |
| 663 | } |
| 664 | |
| 665 | if (d->compositor_version < 0) |
| 666 | d->compositor_version = version; |
| 667 | |
| 668 | d->compositor = |
| 669 | wl_registry_bind(registry, |
| 670 | id, &wl_compositor_interface, |
| 671 | d->compositor_version); |
| 672 | } else if (strcmp(interface, "wl_scaler") == 0 && version >= 2) { |
| 673 | d->scaler = wl_registry_bind(registry, |
| 674 | id, &wl_scaler_interface, 2); |
| 675 | } else if (strcmp(interface, "xdg_shell") == 0) { |
| 676 | d->shell = wl_registry_bind(registry, |
| 677 | id, &xdg_shell_interface, 1); |
| 678 | xdg_shell_use_unstable_version(d->shell, XDG_VERSION); |
| 679 | xdg_shell_add_listener(d->shell, &xdg_shell_listener, d); |
| 680 | } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) { |
| 681 | d->fshell = wl_registry_bind(registry, |
| 682 | id, &_wl_fullscreen_shell_interface, 1); |
| 683 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 684 | d->shm = wl_registry_bind(registry, |
| 685 | id, &wl_shm_interface, 1); |
| 686 | wl_shm_add_listener(d->shm, &shm_listener, d); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | static void |
| 691 | registry_handle_global_remove(void *data, struct wl_registry *registry, |
| 692 | uint32_t name) |
| 693 | { |
| 694 | } |
| 695 | |
| 696 | static const struct wl_registry_listener registry_listener = { |
| 697 | registry_handle_global, |
| 698 | registry_handle_global_remove |
| 699 | }; |
| 700 | |
| 701 | static struct display * |
| 702 | create_display(int version) |
| 703 | { |
| 704 | struct display *display; |
| 705 | |
| 706 | display = malloc(sizeof *display); |
| 707 | if (display == NULL) { |
| 708 | fprintf(stderr, "out of memory\n"); |
| 709 | exit(1); |
| 710 | } |
| 711 | display->display = wl_display_connect(NULL); |
| 712 | assert(display->display); |
| 713 | |
| 714 | display->compositor_version = version; |
| 715 | display->formats = 0; |
| 716 | display->registry = wl_display_get_registry(display->display); |
| 717 | wl_registry_add_listener(display->registry, |
| 718 | ®istry_listener, display); |
| 719 | wl_display_roundtrip(display->display); |
| 720 | if (display->shm == NULL) { |
| 721 | fprintf(stderr, "No wl_shm global\n"); |
| 722 | exit(1); |
| 723 | } |
| 724 | |
| 725 | wl_display_roundtrip(display->display); |
| 726 | |
| 727 | if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) { |
| 728 | fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n"); |
| 729 | exit(1); |
| 730 | } |
| 731 | |
| 732 | return display; |
| 733 | } |
| 734 | |
| 735 | static void |
| 736 | destroy_display(struct display *display) |
| 737 | { |
| 738 | if (display->shm) |
| 739 | wl_shm_destroy(display->shm); |
| 740 | |
| 741 | if (display->shell) |
| 742 | xdg_shell_destroy(display->shell); |
| 743 | |
| 744 | if (display->fshell) |
| 745 | _wl_fullscreen_shell_release(display->fshell); |
| 746 | |
| 747 | if (display->scaler) |
| 748 | wl_scaler_destroy(display->scaler); |
| 749 | |
| 750 | if (display->compositor) |
| 751 | wl_compositor_destroy(display->compositor); |
| 752 | |
| 753 | wl_registry_destroy(display->registry); |
| 754 | wl_display_flush(display->display); |
| 755 | wl_display_disconnect(display->display); |
| 756 | free(display); |
| 757 | } |
| 758 | |
| 759 | static void |
| 760 | signal_int(int signum) |
| 761 | { |
| 762 | running = 0; |
| 763 | } |
| 764 | |
| 765 | static void |
| 766 | print_usage(int retval) |
| 767 | { |
| 768 | printf( |
| 769 | "usage: weston-simple-damage [options]\n\n" |
| 770 | "options:\n" |
| 771 | " -h, --help\t\tPring this help\n" |
| 772 | " --verbose\t\tPrint verbose log information\n" |
| 773 | " --version=VERSION\tVersion of wl_surface to use\n" |
| 774 | " --width=WIDTH\t\tWidth of the window\n" |
| 775 | " --height=HEIGHT\tHeight of the window\n" |
| 776 | " --scale=SCALE\t\tScale factor for the surface\n" |
| 777 | " --transform=TRANSFORM\tTransform for the surface\n" |
| 778 | " --rotating-transform\tUse a different buffer_transform for each frame\n" |
| 779 | " --use-viewport\tUse wl_viewport\n" |
| 780 | ); |
| 781 | |
| 782 | exit(retval); |
| 783 | } |
| 784 | |
| 785 | static int |
| 786 | parse_transform(const char *str, enum wl_output_transform *transform) |
| 787 | { |
| 788 | int i; |
| 789 | static const struct { |
| 790 | const char *name; |
| 791 | enum wl_output_transform transform; |
| 792 | } names[] = { |
| 793 | { "normal", WL_OUTPUT_TRANSFORM_NORMAL }, |
| 794 | { "90", WL_OUTPUT_TRANSFORM_90 }, |
| 795 | { "180", WL_OUTPUT_TRANSFORM_180 }, |
| 796 | { "270", WL_OUTPUT_TRANSFORM_270 }, |
| 797 | { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED }, |
| 798 | { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 }, |
| 799 | { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 }, |
| 800 | { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 }, |
| 801 | }; |
| 802 | |
| 803 | for (i = 0; i < 8; i++) { |
| 804 | if (strcmp(names[i].name, str) == 0) { |
| 805 | *transform = names[i].transform; |
| 806 | return 1; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | int |
| 814 | main(int argc, char **argv) |
| 815 | { |
| 816 | struct sigaction sigint; |
| 817 | struct display *display; |
| 818 | struct window *window; |
| 819 | int i, ret = 0; |
| 820 | int version = -1; |
| 821 | int width = 300, height = 200, scale = 1; |
| 822 | enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL; |
| 823 | enum window_flags flags = 0; |
| 824 | |
| 825 | for (i = 1; i < argc; ++i) { |
| 826 | if (strcmp(argv[i], "--help") == 0 || |
| 827 | strcmp(argv[i], "-h") == 0) { |
| 828 | print_usage(0); |
| 829 | } else if (sscanf(argv[i], "--version=%d", &version) > 0) { |
| 830 | if (version < 1 || version > 3) { |
| 831 | fprintf(stderr, "Unsupported wl_surface version: %d\n", |
| 832 | version); |
| 833 | return 1; |
| 834 | } |
| 835 | continue; |
| 836 | } else if (strcmp(argv[i], "--verbose") == 0) { |
| 837 | print_debug = 1; |
| 838 | continue; |
| 839 | } else if (sscanf(argv[i], "--width=%d", &width) > 0) { |
| 840 | continue; |
| 841 | } else if (sscanf(argv[i], "--height=%d", &height) > 0) { |
| 842 | continue; |
| 843 | } else if (strncmp(argv[i], "--transform=", 12) == 0 && |
| 844 | parse_transform(argv[i] + 12, &transform) > 0) { |
| 845 | continue; |
| 846 | } else if (strcmp(argv[i], "--rotating-transform") == 0) { |
| 847 | flags |= WINDOW_FLAG_ROTATING_TRANSFORM; |
| 848 | continue; |
| 849 | } else if (sscanf(argv[i], "--scale=%d", &scale) > 0) { |
| 850 | continue; |
| 851 | } else if (strcmp(argv[i], "--use-viewport") == 0) { |
| 852 | flags |= WINDOW_FLAG_USE_VIEWPORT; |
| 853 | continue; |
| 854 | } else { |
| 855 | printf("Invalid option: %s\n", argv[i]); |
| 856 | print_usage(255); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | display = create_display(version); |
| 861 | |
| 862 | window = create_window(display, width, height, transform, scale, flags); |
| 863 | if (!window) |
| 864 | return 1; |
| 865 | |
| 866 | sigint.sa_handler = signal_int; |
| 867 | sigemptyset(&sigint.sa_mask); |
| 868 | sigint.sa_flags = SA_RESETHAND; |
| 869 | sigaction(SIGINT, &sigint, NULL); |
| 870 | |
| 871 | redraw(window, NULL, 0); |
| 872 | |
| 873 | while (running && ret != -1) |
| 874 | ret = wl_display_dispatch(display->display); |
| 875 | |
| 876 | fprintf(stderr, "simple-shm exiting\n"); |
| 877 | destroy_window(window); |
| 878 | destroy_display(display); |
| 879 | |
| 880 | return 0; |
| 881 | } |